Skip to content

Commit 19fef0d

Browse files
committed
Create and initialize Device object: the first PR for rewriting DML backend
1 parent 294de6b commit 19fef0d

File tree

8 files changed

+401
-0
lines changed

8 files changed

+401
-0
lines changed

src/webnn/native/BUILD.gn

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,17 @@ source_set("sources") {
210210
}
211211
}
212212

213+
if (webnn_enable_dml) {
214+
sources += [
215+
"dml/BackendDML.cpp",
216+
"dml/BackendDML.h",
217+
"dml/ContextDML.cpp",
218+
"dml/ContextDML.h",
219+
"dml/GraphDML.cpp",
220+
"dml/GraphDML.h",
221+
]
222+
}
223+
213224
if (webnn_enable_dmlx) {
214225
if (webnn_enable_gpu_buffer == false) {
215226
sources += [
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2019 The Dawn Authors
2+
// Copyright 2022 The WebNN-native Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "webnn/native/dml/BackendDML.h"
17+
18+
#include "webnn/native/Instance.h"
19+
#include "webnn/native/dml/ContextDML.h"
20+
21+
namespace webnn::native::dml {
22+
23+
Backend::Backend(InstanceBase* instance)
24+
: BackendConnection(instance, wnn::BackendType::DirectML) {
25+
}
26+
27+
MaybeError Backend::Initialize() {
28+
return {};
29+
}
30+
31+
// An adapter called the "Microsoft Basic Render Driver" is always present. This adapter is a
32+
// render-only device that has no display outputs.
33+
inline bool IsSoftwareAdapter(IDXGIAdapter1* pAdapter) {
34+
DXGI_ADAPTER_DESC1 pDesc;
35+
pAdapter->GetDesc1(&pDesc);
36+
// See here for documentation on filtering WARP adapter:
37+
// https://docs.microsoft.com/en-us/windows/desktop/direct3ddxgi/d3d10-graphics-programming-guide-dxgi#new-info-about-enumerating-adapters-for-windows-8
38+
return pDesc.Flags == DXGI_ADAPTER_FLAG_SOFTWARE ||
39+
(pDesc.VendorId == 0x1414 && pDesc.DeviceId == 0x8c);
40+
}
41+
42+
HRESULT EnumAdapter(DXGI_GPU_PREFERENCE gpuPreference,
43+
bool useGpu,
44+
ComPtr<IDXGIAdapter1> adapter) {
45+
ComPtr<IDXGIFactory6> dxgiFactory;
46+
WEBNN_RETURN_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)));
47+
if (useGpu) {
48+
UINT adapterIndex = 0;
49+
while (dxgiFactory->EnumAdapterByGpuPreference(adapterIndex++, gpuPreference,
50+
IID_PPV_ARGS(&adapter)) !=
51+
DXGI_ERROR_NOT_FOUND) {
52+
if (!IsSoftwareAdapter(adapter.Get())) {
53+
break;
54+
}
55+
}
56+
} else {
57+
WEBNN_RETURN_IF_FAILED(dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)));
58+
}
59+
return S_OK;
60+
}
61+
62+
ContextBase* Backend::CreateContext(ContextOptions const* options) {
63+
wnn::DevicePreference devicePreference = options->devicePreference;
64+
bool useGpu = devicePreference == wnn::DevicePreference::Cpu ? false : true;
65+
DXGI_GPU_PREFERENCE gpuPreference = DXGI_GPU_PREFERENCE_UNSPECIFIED;
66+
wnn::PowerPreference powerPreference = options->powerPreference;
67+
switch (powerPreference) {
68+
case wnn::PowerPreference::High_performance:
69+
gpuPreference = DXGI_GPU_PREFERENCE::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE;
70+
break;
71+
case wnn::PowerPreference::Low_power:
72+
gpuPreference = DXGI_GPU_PREFERENCE::DXGI_GPU_PREFERENCE_MINIMUM_POWER;
73+
break;
74+
default:
75+
break;
76+
}
77+
78+
bool useDebugLayer = false;
79+
#ifdef _DEBUG
80+
useDebugLayer = true;
81+
#endif
82+
ComPtr<IDXGIAdapter1> adapter;
83+
if (FAILED(EnumAdapter(gpuPreference, useGpu, adapter))) {
84+
dawn::ErrorLog() << "Failed to enumerate adapters.";
85+
DAWN_ASSERT(0);
86+
}
87+
return Context::Create(adapter, useDebugLayer);
88+
}
89+
90+
BackendConnection* Connect(InstanceBase* instance) {
91+
Backend* backend = new Backend(instance);
92+
93+
if (instance->ConsumedError(backend->Initialize())) {
94+
delete backend;
95+
return nullptr;
96+
}
97+
98+
return backend;
99+
}
100+
101+
} // namespace webnn::native::dml

src/webnn/native/dml/BackendDML.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2019 The Dawn Authors
2+
// Copyright 2022 The WebNN-native Authors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#ifndef WEBNN_NATIVE_DML_BACKEND_DML_H_
17+
#define WEBNN_NATIVE_DML_BACKEND_DML_H_
18+
19+
#include <memory>
20+
#include "webnn/native/BackendConnection.h"
21+
#include "webnn/native/Context.h"
22+
#include "webnn/native/Error.h"
23+
24+
namespace webnn::native::dml {
25+
26+
class Backend : public BackendConnection {
27+
public:
28+
Backend(InstanceBase* instance);
29+
30+
MaybeError Initialize();
31+
ContextBase* CreateContext(ContextOptions const* options = nullptr) override;
32+
33+
private:
34+
};
35+
36+
} // namespace webnn::native::dml
37+
38+
#endif // WEBNN_NATIVE_DML_BACKEND_DML_H_
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The WebNN-native Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "webnn/native/dml/ContextDML.h"
16+
17+
#include "common/RefCounted.h"
18+
#include "webnn/native/dml/GraphDML.h"
19+
20+
namespace webnn::native::dml {
21+
22+
HRESULT Context::Initialize() {
23+
if (mUseDebugLayer) {
24+
ComPtr<ID3D12Debug> debug;
25+
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) {
26+
debug->EnableDebugLayer();
27+
}
28+
}
29+
WEBNN_RETURN_IF_FAILED(
30+
D3D12CreateDevice(mAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&mD3D12Device)));
31+
D3D12_COMMAND_QUEUE_DESC commandQueueDesc{};
32+
commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
33+
commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
34+
WEBNN_RETURN_IF_FAILED(
35+
mD3D12Device->CreateCommandQueue(&commandQueueDesc, IID_PPV_ARGS(&mCommandQueue)));
36+
WEBNN_RETURN_IF_FAILED(mD3D12Device->CreateCommandAllocator(
37+
D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandAllocator)));
38+
WEBNN_RETURN_IF_FAILED(mD3D12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT,
39+
mCommandAllocator.Get(), nullptr,
40+
IID_PPV_ARGS(&mCommandList)));
41+
42+
// Create the DirectML device.
43+
ComPtr<ID3D12DebugDevice> debugDevice;
44+
if (mUseDebugLayer && SUCCEEDED(mD3D12Device.As(&debugDevice))) {
45+
WEBNN_RETURN_IF_FAILED(DMLCreateDevice(mD3D12Device.Get(), DML_CREATE_DEVICE_FLAG_DEBUG,
46+
IID_PPV_ARGS(&mDevice)));
47+
} else {
48+
WEBNN_RETURN_IF_FAILED(DMLCreateDevice(mD3D12Device.Get(), DML_CREATE_DEVICE_FLAG_NONE,
49+
IID_PPV_ARGS(&mDevice)));
50+
}
51+
return S_OK;
52+
};
53+
54+
// static
55+
ContextBase* Context::Create(ComPtr<IDXGIAdapter1> adapter, bool useDebugLayer) {
56+
Context* context = new Context(adapter, useDebugLayer);
57+
if (FAILED(context->Initialize())) {
58+
dawn::ErrorLog() << "Failed to initialize Device.";
59+
return nullptr;
60+
}
61+
return context;
62+
}
63+
64+
Context::Context(ComPtr<IDXGIAdapter1> adapter, bool useDebugLayer)
65+
: mAdapter(std::move(adapter)), mUseDebugLayer(useDebugLayer) {
66+
}
67+
68+
GraphBase* Context::CreateGraphImpl() {
69+
return new Graph(this);
70+
}
71+
72+
} // namespace webnn::native::dml

src/webnn/native/dml/ContextDML.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2022 The WebNN-native Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef WEBNN_NATIVE_DML_CONTEXT_DML_H_
16+
#define WEBNN_NATIVE_DML_CONTEXT_DML_H_
17+
18+
#include "webnn/native/Context.h"
19+
20+
#include "common/Log.h"
21+
#include "dml_platform.h"
22+
#include "webnn/native/Graph.h"
23+
24+
namespace webnn::native::dml {
25+
26+
class Context : public ContextBase {
27+
public:
28+
static ContextBase* Create(ComPtr<IDXGIAdapter1> adapter, bool useDebugLayer);
29+
~Context() override = default;
30+
31+
private:
32+
Context(ComPtr<IDXGIAdapter1> adapter, bool useDebugLayer);
33+
HRESULT Initialize();
34+
35+
GraphBase* CreateGraphImpl() override;
36+
37+
ComPtr<IDMLDevice> mDevice;
38+
ComPtr<ID3D12Device> mD3D12Device;
39+
ComPtr<IDMLCommandRecorder> mCommandRecorder;
40+
ComPtr<ID3D12CommandQueue> mCommandQueue;
41+
ComPtr<ID3D12CommandAllocator> mCommandAllocator;
42+
ComPtr<ID3D12GraphicsCommandList> mCommandList;
43+
44+
ComPtr<IDXGIAdapter1> mAdapter;
45+
bool mUseDebugLayer = false;
46+
};
47+
48+
} // namespace webnn::native::dml
49+
50+
#endif // WEBNN_NATIVE_DML_CONTEXT_DML_H_

src/webnn/native/dml/GraphDML.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2022 The WebNN-native Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "webnn/native/dml/GraphDML.h"
16+
17+
#include "webnn/native/NamedInputs.h"
18+
#include "webnn/native/NamedOutputs.h"
19+
20+
namespace webnn::native ::dml {
21+
22+
Graph::Graph(Context* context) : GraphBase(context) {
23+
}
24+
25+
MaybeError Graph::CompileImpl() {
26+
return {};
27+
}
28+
29+
MaybeError Graph::ComputeImpl(NamedInputsBase* inputs, NamedOutputsBase* outputs) {
30+
return {};
31+
}
32+
33+
} // namespace webnn::native::dml

src/webnn/native/dml/GraphDML.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2022 The WebNN-native Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef WEBNN_NATIVE_DML_GRAPH_DML_H_
16+
#define WEBNN_NATIVE_DML_GRAPH_DML_H_
17+
18+
#include "webnn/native/Graph.h"
19+
#include "webnn/native/Operand.h"
20+
#include "webnn/native/Operator.h"
21+
#include "webnn/native/dml/ContextDML.h"
22+
#include "webnn/native/ops/BatchNorm.h"
23+
#include "webnn/native/ops/Binary.h"
24+
#include "webnn/native/ops/Clamp.h"
25+
#include "webnn/native/ops/Concat.h"
26+
#include "webnn/native/ops/Constant.h"
27+
#include "webnn/native/ops/Conv2d.h"
28+
#include "webnn/native/ops/Gemm.h"
29+
#include "webnn/native/ops/Gru.h"
30+
#include "webnn/native/ops/Input.h"
31+
#include "webnn/native/ops/InstanceNorm.h"
32+
#include "webnn/native/ops/LeakyRelu.h"
33+
#include "webnn/native/ops/Pad.h"
34+
#include "webnn/native/ops/Pool2d.h"
35+
#include "webnn/native/ops/Reduce.h"
36+
#include "webnn/native/ops/Resample2d.h"
37+
#include "webnn/native/ops/Reshape.h"
38+
#include "webnn/native/ops/Slice.h"
39+
#include "webnn/native/ops/Split.h"
40+
#include "webnn/native/ops/Squeeze.h"
41+
#include "webnn/native/ops/Transpose.h"
42+
#include "webnn/native/ops/Unary.h"
43+
44+
namespace webnn::native::dml {
45+
46+
class Graph : public GraphBase {
47+
public:
48+
explicit Graph(Context* context);
49+
~Graph() override = default;
50+
51+
private:
52+
MaybeError CompileImpl() override;
53+
MaybeError ComputeImpl(NamedInputsBase* inputs, NamedOutputsBase* outputs) override;
54+
};
55+
56+
} // namespace webnn::native::dml
57+
58+
#endif // WEBNN_NATIVE_DML_GRAPH_DML_H_

0 commit comments

Comments
 (0)