Skip to content

Commit 9bfd240

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

8 files changed

Lines changed: 433 additions & 0 deletions

File tree

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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
namespace {
24+
HRESULT EnumAdapter(DXGI_GPU_PREFERENCE gpuPreference,
25+
bool useGpu,
26+
ComPtr<IDXGIAdapter1> adapter) {
27+
ComPtr<IDXGIFactory6> dxgiFactory;
28+
WEBNN_RETURN_IF_FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)));
29+
if (useGpu) {
30+
UINT adapterIndex = 0;
31+
while (dxgiFactory->EnumAdapterByGpuPreference(adapterIndex++, gpuPreference,
32+
IID_PPV_ARGS(&adapter)) !=
33+
DXGI_ERROR_NOT_FOUND) {
34+
DXGI_ADAPTER_DESC1 pDesc;
35+
adapter->GetDesc1(&pDesc);
36+
// An adapter called the "Microsoft Basic Render Driver" is always present.
37+
// This adapter is a render-only device that has no display outputs. See here
38+
// for documentation on filtering WARP adapter:
39+
// https://docs.microsoft.com/en-us/windows/desktop/direct3ddxgi/d3d10-graphics-programming-guide-dxgi#new-info-about-enumerating-adapters-for-windows-8
40+
bool isSoftwareAdapter = pDesc.Flags == DXGI_ADAPTER_FLAG_SOFTWARE ||
41+
(pDesc.VendorId == 0x1414 && pDesc.DeviceId == 0x8c);
42+
if (!isSoftwareAdapter) {
43+
break;
44+
}
45+
}
46+
} else {
47+
WEBNN_RETURN_IF_FAILED(dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)));
48+
}
49+
return S_OK;
50+
}
51+
52+
} // namespace
53+
54+
Backend::Backend(InstanceBase* instance)
55+
: BackendConnection(instance, wnn::BackendType::DirectML) {
56+
}
57+
58+
MaybeError Backend::Initialize() {
59+
return {};
60+
}
61+
62+
ContextBase* Backend::CreateContext(ContextOptions const* options) {
63+
wnn::DevicePreference devicePreference =
64+
options == nullptr ? wnn::DevicePreference::Default : options->devicePreference;
65+
bool useGpu = devicePreference == wnn::DevicePreference::Cpu ? false : true;
66+
DXGI_GPU_PREFERENCE gpuPreference = DXGI_GPU_PREFERENCE_UNSPECIFIED;
67+
wnn::PowerPreference powerPreference =
68+
options == nullptr ? wnn::PowerPreference::Default : options->powerPreference;
69+
switch (powerPreference) {
70+
case wnn::PowerPreference::High_performance:
71+
gpuPreference = DXGI_GPU_PREFERENCE::DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE;
72+
break;
73+
case wnn::PowerPreference::Low_power:
74+
gpuPreference = DXGI_GPU_PREFERENCE::DXGI_GPU_PREFERENCE_MINIMUM_POWER;
75+
break;
76+
default:
77+
break;
78+
}
79+
80+
bool useDebugLayer = false;
81+
#ifdef _DEBUG
82+
useDebugLayer = true;
83+
#endif
84+
if (useDebugLayer) {
85+
ComPtr<ID3D12Debug> debug;
86+
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) {
87+
debug->EnableDebugLayer();
88+
}
89+
}
90+
91+
ComPtr<IDXGIAdapter1> adapter;
92+
if (FAILED(EnumAdapter(gpuPreference, useGpu, adapter))) {
93+
dawn::ErrorLog() << "Failed to enumerate adapters for creating the context.";
94+
return nullptr;
95+
}
96+
97+
// Create the D3D device.
98+
if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0,
99+
IID_PPV_ARGS(&mD3D12Device)))) {
100+
dawn::ErrorLog() << "Failed to create D3D device.";
101+
return nullptr;
102+
}
103+
104+
// Create the DirectML device.
105+
ComPtr<ID3D12DebugDevice> debugDevice;
106+
if (useDebugLayer && SUCCEEDED(mD3D12Device.As(&debugDevice))) {
107+
if (FAILED(DMLCreateDevice(mD3D12Device.Get(), DML_CREATE_DEVICE_FLAG_DEBUG,
108+
IID_PPV_ARGS(&mDMLDevice)))) {
109+
dawn::ErrorLog() << "Failed to create DML device with debug flag.";
110+
return nullptr;
111+
}
112+
} else {
113+
if (FAILED(DMLCreateDevice(mD3D12Device.Get(), DML_CREATE_DEVICE_FLAG_NONE,
114+
IID_PPV_ARGS(&mDMLDevice)))) {
115+
dawn::ErrorLog() << "Failed to create DML device with none flag.";
116+
return nullptr;
117+
}
118+
}
119+
120+
D3D12_COMMAND_QUEUE_DESC commandQueueDesc{};
121+
commandQueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
122+
commandQueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
123+
if (FAILED(mD3D12Device->CreateCommandQueue(&commandQueueDesc,
124+
IID_PPV_ARGS(&mCommandQueue)))) {
125+
dawn::ErrorLog() << "Failed to create command queue.";
126+
return nullptr;
127+
};
128+
129+
return Context::Create(mDMLDevice, mD3D12Device, mCommandQueue);
130+
}
131+
132+
BackendConnection* Connect(InstanceBase* instance) {
133+
Backend* backend = new Backend(instance);
134+
135+
if (instance->ConsumedError(backend->Initialize())) {
136+
delete backend;
137+
return nullptr;
138+
}
139+
140+
return backend;
141+
}
142+
143+
} // namespace webnn::native::dml

src/webnn/native/dml/BackendDML.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 "dml_platform.h"
20+
#include "webnn/native/BackendConnection.h"
21+
#include "webnn/native/Context.h"
22+
23+
namespace webnn::native::dml {
24+
25+
class Backend : public BackendConnection {
26+
public:
27+
Backend(InstanceBase* instance);
28+
29+
MaybeError Initialize();
30+
ContextBase* CreateContext(ContextOptions const* options = nullptr) override;
31+
32+
private:
33+
ComPtr<IDMLDevice> mDMLDevice;
34+
ComPtr<ID3D12Device> mD3D12Device;
35+
ComPtr<ID3D12CommandQueue> mCommandQueue;
36+
};
37+
38+
} // namespace webnn::native::dml
39+
40+
#endif // WEBNN_NATIVE_DML_BACKEND_DML_H_
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 "webnn/native/dml/GraphDML.h"
18+
19+
namespace webnn::native::dml {
20+
21+
HRESULT Context::Initialize() {
22+
WEBNN_RETURN_IF_FAILED(mCommandRecorder.D3D12Device->CreateCommandAllocator(
23+
D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&mCommandRecorder.commandAllocator)));
24+
WEBNN_RETURN_IF_FAILED(mCommandRecorder.D3D12Device->CreateCommandList(
25+
0, D3D12_COMMAND_LIST_TYPE_DIRECT, mCommandRecorder.commandAllocator.Get(), nullptr,
26+
IID_PPV_ARGS(&mCommandRecorder.commandList)));
27+
return S_OK;
28+
};
29+
30+
// static
31+
ContextBase* Context::Create(ComPtr<IDMLDevice> DMLDevice,
32+
ComPtr<ID3D12Device> D3D12Device,
33+
ComPtr<ID3D12CommandQueue> commandQueue) {
34+
Context* context = new Context(DMLDevice, D3D12Device, commandQueue);
35+
if (FAILED(context->Initialize())) {
36+
dawn::ErrorLog() << "Failed to initialize Device.";
37+
delete context;
38+
return nullptr;
39+
}
40+
return context;
41+
}
42+
43+
Context::Context(ComPtr<IDMLDevice> DMLDevice,
44+
ComPtr<ID3D12Device> D3D12Device,
45+
ComPtr<ID3D12CommandQueue> commandQueue) {
46+
mCommandRecorder.DMLDevice = DMLDevice;
47+
mCommandRecorder.D3D12Device = D3D12Device;
48+
mCommandRecorder.commandQueue = commandQueue;
49+
}
50+
51+
GraphBase* Context::CreateGraphImpl() {
52+
return new Graph(this);
53+
}
54+
55+
} // namespace webnn::native::dml

src/webnn/native/dml/ContextDML.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
struct CommandRecorder {
27+
ComPtr<IDMLDevice> DMLDevice;
28+
ComPtr<ID3D12Device> D3D12Device;
29+
ComPtr<ID3D12CommandQueue> commandQueue;
30+
ComPtr<IDMLCommandRecorder> commandRecorder;
31+
ComPtr<ID3D12CommandAllocator> commandAllocator;
32+
ComPtr<ID3D12GraphicsCommandList> commandList;
33+
};
34+
35+
class Context : public ContextBase {
36+
public:
37+
static ContextBase* Create(ComPtr<IDMLDevice> DMLDevice,
38+
ComPtr<ID3D12Device> D3D12Device,
39+
ComPtr<ID3D12CommandQueue> commandQueue);
40+
~Context() override = default;
41+
42+
private:
43+
Context(ComPtr<IDMLDevice> DMLDevice,
44+
ComPtr<ID3D12Device> D3D12Device,
45+
ComPtr<ID3D12CommandQueue> commandQueue);
46+
HRESULT Initialize();
47+
48+
GraphBase* CreateGraphImpl() override;
49+
50+
CommandRecorder mCommandRecorder;
51+
};
52+
53+
} // namespace webnn::native::dml
54+
55+
#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

0 commit comments

Comments
 (0)