0

webnn: Remove illegal uses of aliases in headers in services/webnn/dml

See https://google.github.io/styleguide/cppguide.html#Aliases

Bug: 324541572
Change-Id: I70f37107b5abf07268fba4c8f6d3ab9ecef381aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5493853
Reviewed-by: ningxin hu <ningxin.hu@intel.com>
Reviewed-by: Rafael Cintron <rafael.cintron@microsoft.com>
Commit-Queue: Austin Sullivan <asully@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1293475}
This commit is contained in:
Austin Sullivan
2024-04-28 14:49:06 +00:00
committed by Chromium LUCI CQ
parent fd15d50773
commit c9780f27a1
13 changed files with 157 additions and 158 deletions

@ -17,6 +17,7 @@
#include "services/webnn/public/mojom/webnn_error.mojom.h"
namespace webnn::dml {
namespace {
base::unexpected<mojom::ErrorPtr> HandleAdapterFailure(
@ -37,7 +38,7 @@ base::unexpected<mojom::ErrorPtr> HandleAdapterFailure(
// static
base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::GetGpuInstance(
DML_FEATURE_LEVEL min_required_dml_feature_level,
ComPtr<IDXGIAdapter> dxgi_adapter) {
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter) {
// If the `Adapter` instance is created, add a reference and return it.
if (gpu_instance_) {
if (!gpu_instance_->IsDMLFeatureLevelSupported(
@ -59,14 +60,14 @@ Adapter::GetInstanceForTesting(
DML_FEATURE_LEVEL min_required_dml_feature_level) {
CHECK_IS_TEST();
ComPtr<IDXGIFactory1> factory;
Microsoft::WRL::ComPtr<IDXGIFactory1> factory;
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&factory));
if (FAILED(hr)) {
return HandleAdapterFailure(
mojom::Error::Code::kUnknownError,
"Failed to create an IDXGIFactory1 for testing.", hr);
}
ComPtr<IDXGIAdapter> dxgi_adapter;
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
hr = factory->EnumAdapters(0, &dxgi_adapter);
if (FAILED(hr)) {
return HandleAdapterFailure(
@ -108,7 +109,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::GetNpuInstance(
"Failed to get DXCoreCreateAdapterFactory function.");
}
ComPtr<IDXCoreAdapterFactory> dxcore_factory;
Microsoft::WRL::ComPtr<IDXCoreAdapterFactory> dxcore_factory;
HRESULT hr =
dxcore_create_adapter_factory_proc(IID_PPV_ARGS(&dxcore_factory));
if (FAILED(hr)) {
@ -119,7 +120,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::GetNpuInstance(
// adapter list only contains core-compute capable devices.
const std::array<GUID, 1> dx_guids = {
DXCORE_ADAPTER_ATTRIBUTE_D3D12_CORE_COMPUTE};
ComPtr<IDXCoreAdapterList> adapter_list;
Microsoft::WRL::ComPtr<IDXCoreAdapterList> adapter_list;
hr = dxcore_factory->CreateAdapterList(dx_guids.size(), dx_guids.data(),
IID_PPV_ARGS(&adapter_list));
if (FAILED(hr)) {
@ -128,10 +129,10 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::GetNpuInstance(
}
const uint32_t adapter_count = adapter_list->GetAdapterCount();
ComPtr<IDXCoreAdapter> dxcore_npu_adapter;
Microsoft::WRL::ComPtr<IDXCoreAdapter> dxcore_npu_adapter;
for (uint32_t adapter_index = 0; adapter_index < adapter_count;
++adapter_index) {
ComPtr<IDXCoreAdapter> dxcore_adapter;
Microsoft::WRL::ComPtr<IDXCoreAdapter> dxcore_adapter;
hr = adapter_list->GetAdapter(adapter_index, IID_PPV_ARGS(&dxcore_adapter));
if (FAILED(hr)) {
return HandleAdapterFailure(mojom::Error::Code::kUnknownError,
@ -162,7 +163,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::GetNpuInstance(
// static
base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
ComPtr<IUnknown> dxgi_or_dxcore_adapter,
Microsoft::WRL::ComPtr<IUnknown> dxgi_or_dxcore_adapter,
DML_FEATURE_LEVEL min_required_dml_feature_level) {
PlatformFunctions* platform_functions = PlatformFunctions::GetInstance();
if (!platform_functions) {
@ -178,7 +179,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
// Must be called before the D3D12 device is created.
auto d3d12_get_debug_interface_proc =
platform_functions->d3d12_get_debug_interface_proc();
ComPtr<ID3D12Debug> d3d12_debug;
Microsoft::WRL::ComPtr<ID3D12Debug> d3d12_debug;
if (SUCCEEDED(d3d12_get_debug_interface_proc(IID_PPV_ARGS(&d3d12_debug)))) {
d3d12_debug->EnableDebugLayer();
is_d3d12_debug_layer_enabled = true;
@ -186,7 +187,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
}
// Create d3d12 device.
ComPtr<ID3D12Device> d3d12_device;
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device;
// D3D_FEATURE_LEVEL_1_0_CORE allows Microsoft Compute Driver Model (MCDM)
// devices (NPUs) to be used. D3D_FEATURE_LEVEL_11_0 targets features
// supported by Direct3D 11.0.
@ -198,7 +199,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_command_list_type
D3D_FEATURE_LEVEL d3d_feature_level = D3D_FEATURE_LEVEL_11_0;
D3D12_COMMAND_LIST_TYPE command_list_type = D3D12_COMMAND_LIST_TYPE_DIRECT;
ComPtr<IDXCoreAdapter> dxcore_adapter;
Microsoft::WRL::ComPtr<IDXCoreAdapter> dxcore_adapter;
if (SUCCEEDED(dxgi_or_dxcore_adapter->QueryInterface(
IID_PPV_ARGS(&dxcore_adapter)))) {
d3d_feature_level = D3D_FEATURE_LEVEL_1_0_CORE;
@ -218,7 +219,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
// The d3d12 debug layer can also be enabled via Microsoft (R) DirectX Control
// Panel (dxcpl.exe) for any executable apps by users.
if (!is_d3d12_debug_layer_enabled) {
ComPtr<ID3D12DebugDevice> debug_device;
Microsoft::WRL::ComPtr<ID3D12DebugDevice> debug_device;
// Ignore failure.
d3d12_device->QueryInterface(IID_PPV_ARGS(&debug_device));
is_d3d12_debug_layer_enabled = (debug_device != nullptr);
@ -231,7 +232,7 @@ base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Adapter::Create(
}
// Create dml device.
ComPtr<IDMLDevice> dml_device;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device;
auto dml_create_device1_proc = platform_functions->dml_create_device1_proc();
hr = dml_create_device1_proc(d3d12_device.Get(), flags,
min_required_dml_feature_level,
@ -290,9 +291,9 @@ void Adapter::EnableDebugLayerForTesting() {
enable_d3d12_debug_layer_for_testing_ = true;
}
Adapter::Adapter(ComPtr<IUnknown> dxgi_or_dxcore_adapter,
ComPtr<ID3D12Device> d3d12_device,
ComPtr<IDMLDevice> dml_device,
Adapter::Adapter(Microsoft::WRL::ComPtr<IUnknown> dxgi_or_dxcore_adapter,
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device,
Microsoft::WRL::ComPtr<IDMLDevice> dml_device,
scoped_refptr<CommandQueue> command_queue,
DML_FEATURE_LEVEL max_supported_dml_feature_level,
bool is_uma)
@ -302,8 +303,8 @@ Adapter::Adapter(ComPtr<IUnknown> dxgi_or_dxcore_adapter,
command_queue_(std::move(command_queue)),
max_supported_dml_feature_level_(max_supported_dml_feature_level),
is_uma_(is_uma) {
ComPtr<IDXGIAdapter> dxgi_adapter;
ComPtr<IDXCoreAdapter> dxcore_adapter;
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
Microsoft::WRL::ComPtr<IDXCoreAdapter> dxcore_adapter;
if (SUCCEEDED(dxgi_or_dxcore_adapter_->QueryInterface(
IID_PPV_ARGS(&dxgi_adapter)))) {
CHECK_EQ(gpu_instance_, nullptr);
@ -318,8 +319,8 @@ Adapter::Adapter(ComPtr<IUnknown> dxgi_or_dxcore_adapter,
}
Adapter::~Adapter() {
ComPtr<IDXGIAdapter> dxgi_adapter;
ComPtr<IDXCoreAdapter> dxcore_adapter;
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter;
Microsoft::WRL::ComPtr<IDXCoreAdapter> dxcore_adapter;
if (SUCCEEDED(dxgi_or_dxcore_adapter_->QueryInterface(
IID_PPV_ARGS(&dxgi_adapter)))) {
CHECK_EQ(gpu_instance_, this);

@ -19,8 +19,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
class CommandQueue;
// Adapters represent physical devices and are responsible for device discovery.
@ -46,7 +44,7 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) Adapter final
// specify a lower feature level than what WebNN requires.
static base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> GetGpuInstance(
DML_FEATURE_LEVEL min_required_dml_feature_level,
ComPtr<IDXGIAdapter> dxgi_adapter);
Microsoft::WRL::ComPtr<IDXGIAdapter> dxgi_adapter);
// Similar to the `GetGpuInstance` method above, get the shared
// `Adapter` instance for NPU.
@ -90,9 +88,9 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) Adapter final
FRIEND_TEST_ALL_PREFIXES(WebNNAdapterTest, GetNpuInstance);
friend class base::RefCounted<Adapter>;
Adapter(ComPtr<IUnknown> dxgi_or_dxcore_adapter,
ComPtr<ID3D12Device> d3d12_device,
ComPtr<IDMLDevice> dml_device,
Adapter(Microsoft::WRL::ComPtr<IUnknown> dxgi_or_dxcore_adapter,
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device,
Microsoft::WRL::ComPtr<IDMLDevice> dml_device,
scoped_refptr<CommandQueue> command_queue,
DML_FEATURE_LEVEL max_supported_dml_feature_level,
bool is_uma);
@ -107,13 +105,13 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) Adapter final
// TODO(issues.chromium.org/331369802): Remove min/max DML feature level
// parameters for `dml::Adapter` creation in production code.
static base::expected<scoped_refptr<Adapter>, mojom::ErrorPtr> Create(
ComPtr<IUnknown> dxgi_or_dxcore_adapter,
Microsoft::WRL::ComPtr<IUnknown> dxgi_or_dxcore_adapter,
DML_FEATURE_LEVEL min_required_dml_feature_level);
ComPtr<IUnknown> dxgi_or_dxcore_adapter_;
Microsoft::WRL::ComPtr<IUnknown> dxgi_or_dxcore_adapter_;
ComPtr<ID3D12Device> d3d12_device_;
ComPtr<IDMLDevice> dml_device_;
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device_;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device_;
scoped_refptr<CommandQueue> command_queue_;
DML_FEATURE_LEVEL max_supported_dml_feature_level_ = DML_FEATURE_LEVEL_1_0;

@ -165,7 +165,7 @@ void CommandQueue::OnObjectSignaled(HANDLE object) {
}
}
void CommandQueue::WaitAsync(OnWaitAyncCallback callback) {
void CommandQueue::WaitAsync(base::OnceCallback<void(HRESULT hr)> callback) {
if (!object_watcher_.IsWatching()) {
CHECK(object_watcher_.StartWatchingMultipleTimes(fence_event_.get(), this));
}

@ -42,11 +42,10 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) CommandQueue
// thread may block the UI.
HRESULT WaitSyncForTesting();
using OnWaitAyncCallback = base::OnceCallback<void(HRESULT hr)>;
// It's an asynchronous method for DirectML graph implementation, which will
// not block the CPU. In case this method fails internally, the
// OnWaitAyncCallback accepts a HRESULT from it to handle.
void WaitAsync(OnWaitAyncCallback callback);
// not block the CPU. In case this method fails internally, `callback`
// accepts a HRESULT from it to handle.
void WaitAsync(base::OnceCallback<void(HRESULT hr)> callback);
void ReferenceUntilCompleted(Microsoft::WRL::ComPtr<IUnknown> object);
void ReleaseCompletedResources();

@ -28,8 +28,8 @@ D3D12_RESOURCE_BARRIER CreateUAVBarrier(ID3D12Resource* resource) {
// Static
std::unique_ptr<CommandRecorder> CommandRecorder::Create(
scoped_refptr<CommandQueue> queue,
ComPtr<IDMLDevice> dml_device) {
ComPtr<ID3D12CommandAllocator> command_allocator;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device) {
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> command_allocator;
RETURN_NULL_IF_FAILED(
GetD3D12Device(dml_device.Get())
->CreateCommandAllocator(queue->GetCommandListType(),
@ -39,7 +39,7 @@ std::unique_ptr<CommandRecorder> CommandRecorder::Create(
// Because the command list will be created in the open state, we won't want
// to close it right after its creation.
ComPtr<IDMLCommandRecorder> command_recorder;
Microsoft::WRL::ComPtr<IDMLCommandRecorder> command_recorder;
RETURN_NULL_IF_FAILED(
dml_device->CreateCommandRecorder(IID_PPV_ARGS(&command_recorder)));
@ -50,9 +50,9 @@ std::unique_ptr<CommandRecorder> CommandRecorder::Create(
CommandRecorder::CommandRecorder(
scoped_refptr<CommandQueue> command_queue,
ComPtr<IDMLDevice> dml_device,
ComPtr<ID3D12CommandAllocator> command_allocator,
ComPtr<IDMLCommandRecorder> command_recorder)
Microsoft::WRL::ComPtr<IDMLDevice> dml_device,
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> command_allocator,
Microsoft::WRL::ComPtr<IDMLCommandRecorder> command_recorder)
: command_queue_(std::move(command_queue)),
dml_device_(std::move(dml_device)),
d3d12_device_(GetD3D12Device(dml_device_.Get())),
@ -123,11 +123,12 @@ void CommandRecorder::ResourceBarrier(
barriers.data());
}
void CommandRecorder::CopyBufferRegion(ComPtr<ID3D12Resource> dst_buffer,
uint64_t dst_offset,
ComPtr<ID3D12Resource> src_buffer,
uint64_t src_offset,
uint64_t byte_length) {
void CommandRecorder::CopyBufferRegion(
Microsoft::WRL::ComPtr<ID3D12Resource> dst_buffer,
uint64_t dst_offset,
Microsoft::WRL::ComPtr<ID3D12Resource> src_buffer,
uint64_t src_offset,
uint64_t byte_length) {
CHECK(is_open_);
command_list_->CopyBufferRegion(dst_buffer.Get(), dst_offset,
src_buffer.Get(), src_offset, byte_length);
@ -145,7 +146,7 @@ HRESULT CommandRecorder::InitializeOperator(
CHECK(is_open_);
CHECK(compiled_operator);
ComPtr<IDMLOperatorInitializer> initializer;
Microsoft::WRL::ComPtr<IDMLOperatorInitializer> initializer;
IDMLCompiledOperator* compiled_operators[] = {compiled_operator};
RETURN_IF_FAILED(dml_device_->CreateOperatorInitializer(
/* operatorCount */ 1, compiled_operators, IID_PPV_ARGS(&initializer)));
@ -153,7 +154,7 @@ HRESULT CommandRecorder::InitializeOperator(
DML_BINDING_PROPERTIES initialization_binding_properties =
initializer->GetBindingProperties();
ComPtr<ID3D12DescriptorHeap> descriptor_heap;
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> descriptor_heap;
// Some operator initializers, such as Relu, requires 0 descriptors. However,
// the DirectML binding table requires valid CPU and GPU descriptor handles.
// So create a descriptor heap with at least 1 descriptor.
@ -175,7 +176,7 @@ HRESULT CommandRecorder::InitializeOperator(
descriptor_heap->GetGPUDescriptorHandleForHeapStart(),
.SizeInDescriptors =
initialization_binding_properties.RequiredDescriptorCount};
ComPtr<IDMLBindingTable> binding_table;
Microsoft::WRL::ComPtr<IDMLBindingTable> binding_table;
RETURN_IF_FAILED(dml_device_->CreateBindingTable(
&binding_table_desc, IID_PPV_ARGS(&binding_table)));
@ -184,7 +185,7 @@ HRESULT CommandRecorder::InitializeOperator(
auto temp_resource_size =
initialization_binding_properties.TemporaryResourceSize;
if (temp_resource_size > 0) {
ComPtr<ID3D12Resource> temp_resource;
Microsoft::WRL::ComPtr<ID3D12Resource> temp_resource;
RETURN_IF_FAILED(CreateDefaultBuffer(
d3d12_device_.Get(), temp_resource_size,
L"WebNN_Temporary_Buffer_For_Initialization", temp_resource));
@ -266,8 +267,8 @@ HRESULT CommandRecorder::InitializeOperator(
}
HRESULT CommandRecorder::ExecuteOperator(
ComPtr<IDMLCompiledOperator> compiled_operator,
ComPtr<ID3D12DescriptorHeap> descriptor_heap,
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator,
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> descriptor_heap,
base::span<const DML_BINDING_DESC> input_bindings,
base::span<const DML_BINDING_DESC> output_bindings,
const std::optional<DML_BINDING_DESC>& persistent_resource_binding,
@ -292,7 +293,7 @@ HRESULT CommandRecorder::ExecuteOperator(
.SizeInDescriptors =
execution_binding_properties.RequiredDescriptorCount};
// TODO(crbug.com/40272709): Consider reusing the binding table.
ComPtr<IDMLBindingTable> binding_table;
Microsoft::WRL::ComPtr<IDMLBindingTable> binding_table;
RETURN_IF_FAILED(dml_device_->CreateBindingTable(
&binding_table_desc, IID_PPV_ARGS(&binding_table)));

@ -17,8 +17,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
class CommandQueue;
// CommandRecorder is mainly responsible for the initialization and execution of
@ -29,7 +27,7 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) CommandRecorder final {
public:
static std::unique_ptr<CommandRecorder> Create(
scoped_refptr<CommandQueue> queue,
ComPtr<IDMLDevice> dml_device);
Microsoft::WRL::ComPtr<IDMLDevice> dml_device);
~CommandRecorder();
CommandRecorder(const CommandRecorder&) = delete;
@ -67,9 +65,9 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) CommandRecorder final {
// Record the buffer copy command. The destination and source buffers will be
// referenced until the GPU work has completed.
void CopyBufferRegion(ComPtr<ID3D12Resource> dst_buffer,
void CopyBufferRegion(Microsoft::WRL::ComPtr<ID3D12Resource> dst_buffer,
uint64_t dst_offset,
ComPtr<ID3D12Resource> src_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> src_buffer,
uint64_t src_offset,
uint64_t byte_length);
@ -124,34 +122,35 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) CommandRecorder final {
// This method ensures that all the required GPU resources will be kept alive
// until the operator execution has completed on the GPU.
HRESULT ExecuteOperator(
ComPtr<IDMLCompiledOperator> compiled_operator,
ComPtr<ID3D12DescriptorHeap> descriptor_heap,
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator,
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> descriptor_heap,
base::span<const DML_BINDING_DESC> input_bindings,
base::span<const DML_BINDING_DESC> output_bindings,
const std::optional<DML_BINDING_DESC>& persistent_resource_binding,
const std::optional<DML_BINDING_DESC>& temporary_resource_binding);
private:
CommandRecorder(scoped_refptr<CommandQueue> command_queue,
ComPtr<IDMLDevice> dml_device,
ComPtr<ID3D12CommandAllocator> command_allocator,
ComPtr<IDMLCommandRecorder> command_recorder);
CommandRecorder(
scoped_refptr<CommandQueue> command_queue,
Microsoft::WRL::ComPtr<IDMLDevice> dml_device,
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> command_allocator,
Microsoft::WRL::ComPtr<IDMLCommandRecorder> command_recorder);
bool is_open_ = false;
// The first call to `CloseAndExecute()` sets the first submitted fence value.
uint64_t last_submitted_fence_value_ = UINT64_MAX;
scoped_refptr<CommandQueue> command_queue_;
ComPtr<IDMLDevice> dml_device_;
ComPtr<ID3D12Device> d3d12_device_;
ComPtr<ID3D12CommandAllocator> command_allocator_;
ComPtr<ID3D12GraphicsCommandList> command_list_;
ComPtr<IDMLCommandRecorder> command_recorder_;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device_;
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device_;
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> command_allocator_;
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> command_list_;
Microsoft::WRL::ComPtr<IDMLCommandRecorder> command_recorder_;
// Keep the resources used by recorded commands. After commands submission,
// these resources would be kept alive until the command queue has completed
// the execution of these commands on GPU.
std::vector<ComPtr<IUnknown>> command_resources_;
std::vector<Microsoft::WRL::ComPtr<IUnknown>> command_resources_;
};
} // namespace webnn::dml

@ -41,7 +41,7 @@ uint32_t InputNode::GetGraphInputIndex() const {
}
OperatorNode::OperatorNode(uint32_t node_index,
ComPtr<IDMLOperator> dml_operator)
Microsoft::WRL::ComPtr<IDMLOperator> dml_operator)
: Node(Node::Type::kOperator),
node_index_(node_index),
dml_operator_(std::move(dml_operator)) {
@ -83,7 +83,7 @@ const TensorDesc& NodeOutput::GetTensorDesc() const {
return tensor_desc_;
}
GraphBuilder::GraphBuilder(ComPtr<IDMLDevice> dml_device)
GraphBuilder::GraphBuilder(Microsoft::WRL::ComPtr<IDMLDevice> dml_device)
: dml_device_(std::move(dml_device)) {}
GraphBuilder::GraphBuilder(GraphBuilder&& other) = default;
@ -103,7 +103,7 @@ const OperatorNode* GraphBuilder::CreateOperatorNode(
const void* operator_desc,
base::span<const NodeOutput*> inputs) {
DML_OPERATOR_DESC op_desc{.Type = type, .Desc = operator_desc};
ComPtr<IDMLOperator> dml_operator;
Microsoft::WRL::ComPtr<IDMLOperator> dml_operator;
RETURN_NULL_IF_FAILED(
dml_device_->CreateOperator(&op_desc, IID_PPV_ARGS(&dml_operator)));
@ -171,7 +171,7 @@ uint32_t GraphBuilder::CreateOutputEdge(const NodeOutput* node_output) {
return graph_output_index;
}
ComPtr<IDMLCompiledOperator> GraphBuilder::Compile(
Microsoft::WRL::ComPtr<IDMLCompiledOperator> GraphBuilder::Compile(
DML_EXECUTION_FLAGS flags) const {
TRACE_EVENT0("gpu", "dml::GraphBuilder::Compile");
// Ensure `dml_nodes` vector is ordered by node index of operator node.
@ -217,11 +217,11 @@ ComPtr<IDMLCompiledOperator> GraphBuilder::Compile(
base::checked_cast<uint32_t>(dml_intermediate_edges.size()),
.IntermediateEdges = dml_intermediate_edges.data()};
ComPtr<IDMLDevice1> dml_device1;
Microsoft::WRL::ComPtr<IDMLDevice1> dml_device1;
RETURN_NULL_IF_FAILED(
dml_device_->QueryInterface(IID_PPV_ARGS(&dml_device1)));
ComPtr<IDMLCompiledOperator> compiled_operator;
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator;
RETURN_NULL_IF_FAILED(dml_device1->CompileGraph(
&dml_graph_desc, flags, IID_PPV_ARGS(&compiled_operator)));
return compiled_operator;

@ -6,6 +6,7 @@
#define SERVICES_WEBNN_DML_GRAPH_BUILDER_H_
#include <DirectML.h>
#include <wrl.h>
#include <list>
#include <optional>
@ -18,8 +19,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
class InputNode;
class OperatorNode;
@ -75,7 +74,8 @@ class InputNode final : public Node {
// same order when creating `DML_GRAPH_DESC::Nodes`.
class OperatorNode final : public Node {
public:
OperatorNode(uint32_t operator_index, ComPtr<IDMLOperator> dml_operator);
OperatorNode(uint32_t operator_index,
Microsoft::WRL::ComPtr<IDMLOperator> dml_operator);
~OperatorNode() override;
uint32_t GetNodeIndex() const;
@ -83,7 +83,7 @@ class OperatorNode final : public Node {
private:
uint32_t node_index_ = 0;
ComPtr<IDMLOperator> dml_operator_;
Microsoft::WRL::ComPtr<IDMLOperator> dml_operator_;
DML_OPERATOR_GRAPH_NODE_DESC dml_operator_node_desc_;
};
@ -130,7 +130,7 @@ class NodeOutput {
// output.
class COMPONENT_EXPORT(WEBNN_SERVICE) GraphBuilder final {
public:
explicit GraphBuilder(ComPtr<IDMLDevice> device);
explicit GraphBuilder(Microsoft::WRL::ComPtr<IDMLDevice> device);
GraphBuilder(const GraphBuilder& other) = delete;
GraphBuilder& operator=(const GraphBuilder& other) = delete;
@ -175,10 +175,11 @@ class COMPONENT_EXPORT(WEBNN_SERVICE) GraphBuilder final {
// Notice that IDMLDevice1::CompileGraph may take a long time to compile
// shaders (if not cached before), so this method may block the current
// thread. Consider posting this method to the thread pool to avoid blocking.
ComPtr<IDMLCompiledOperator> Compile(DML_EXECUTION_FLAGS flags) const;
Microsoft::WRL::ComPtr<IDMLCompiledOperator> Compile(
DML_EXECUTION_FLAGS flags) const;
private:
ComPtr<IDMLDevice> dml_device_;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device_;
std::vector<DML_INPUT_GRAPH_EDGE_DESC> dml_input_edges_;
std::vector<DML_INTERMEDIATE_GRAPH_EDGE_DESC> dml_intermediate_edges_;

@ -21,7 +21,7 @@ class WebNNGraphBuilderTest : public TestBase {
void SetUp() override;
protected:
ComPtr<IDMLDevice> dml_device_;
Microsoft::WRL::ComPtr<IDMLDevice> dml_device_;
};
void WebNNGraphBuilderTest::SetUp() {

@ -24,8 +24,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
class Adapter;
class CommandRecorder;
class ContextImpl;
@ -93,8 +91,9 @@ class GraphImpl final : public WebNNGraphImpl {
// if the graph needs it. The resource should be kept alive until the GPU has
// completed the execution.
struct PersistentResource {
PersistentResource(uint64_t persistent_buffer_byte_length,
ComPtr<ID3D12Resource> persistent_buffer);
PersistentResource(
uint64_t persistent_buffer_byte_length,
Microsoft::WRL::ComPtr<ID3D12Resource> persistent_buffer);
~PersistentResource();
PersistentResource(const PersistentResource&) = delete;
PersistentResource& operator=(const PersistentResource&) = delete;
@ -102,7 +101,7 @@ class GraphImpl final : public WebNNGraphImpl {
PersistentResource(PersistentResource&&) = delete;
PersistentResource& operator=(PersistentResource&&) = delete;
ComPtr<ID3D12Resource> persistent_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> persistent_buffer;
DML_BUFFER_BINDING persistent_buffer_binding;
DML_BINDING_DESC persistent_buffer_binding_desc;
};
@ -113,32 +112,33 @@ class GraphImpl final : public WebNNGraphImpl {
// until the GPU has completed the execution. After that, the resources could
// be reused for next graph execution or be released.
struct ComputeResources {
ComputeResources(ComPtr<ID3D12DescriptorHeap> descriptor_heap,
AlignedByteLength<std::string> input_aligned_byte_length,
ComPtr<ID3D12Resource> upload_buffer,
ComPtr<ID3D12Resource> input_buffer,
AlignedByteLength<std::string> output_aligned_byte_length,
ComPtr<ID3D12Resource> output_buffer,
ComPtr<ID3D12Resource> readback_buffer,
uint64_t temporary_buffer_byte_length,
ComPtr<ID3D12Resource> temporary_buffer);
ComputeResources(
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> descriptor_heap,
AlignedByteLength<std::string> input_aligned_byte_length,
Microsoft::WRL::ComPtr<ID3D12Resource> upload_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> input_buffer,
AlignedByteLength<std::string> output_aligned_byte_length,
Microsoft::WRL::ComPtr<ID3D12Resource> output_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> readback_buffer,
uint64_t temporary_buffer_byte_length,
Microsoft::WRL::ComPtr<ID3D12Resource> temporary_buffer);
~ComputeResources();
ComputeResources(const ComputeResources&) = delete;
ComputeResources& operator=(const ComputeResources&) = delete;
ComputeResources(ComputeResources&&) = delete;
ComputeResources& operator=(ComputeResources&&) = delete;
ComPtr<ID3D12DescriptorHeap> descriptor_heap;
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> descriptor_heap;
AlignedByteLength<std::string> input_aligned_byte_length;
ComPtr<ID3D12Resource> upload_buffer;
ComPtr<ID3D12Resource> input_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> upload_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> input_buffer;
AlignedByteLength<std::string> output_aligned_byte_length;
ComPtr<ID3D12Resource> output_buffer;
ComPtr<ID3D12Resource> readback_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> output_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> readback_buffer;
ComPtr<ID3D12Resource> temporary_buffer;
Microsoft::WRL::ComPtr<ID3D12Resource> temporary_buffer;
std::optional<DML_BUFFER_BINDING> temporary_buffer_binding;
std::optional<DML_BINDING_DESC> temporary_buffer_binding_desc;
};
@ -170,7 +170,7 @@ class GraphImpl final : public WebNNGraphImpl {
GraphImpl(scoped_refptr<Adapter> adapter,
std::unique_ptr<CommandRecorder> command_recorder,
std::unique_ptr<PersistentResource> persistent_resource,
ComPtr<IDMLCompiledOperator> compiled_operator,
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator,
ComputeResourceInfo compute_resource_info,
GraphBufferBindingInfo graph_buffer_binding_info,
std::unique_ptr<ComputeResources> compute_resources);
@ -180,7 +180,7 @@ class GraphImpl final : public WebNNGraphImpl {
// this method may take long time to compile shaders (if not cached before),
// this method should run on a background thread rather than the current GPU
// main thread to avoid blocking.
static ComPtr<IDMLCompiledOperator> CompileOnBackgroundThread(
static Microsoft::WRL::ComPtr<IDMLCompiledOperator> CompileOnBackgroundThread(
GraphBuilder graph_builder,
bool pass_dml_execution_disable_meta_commands);
@ -207,7 +207,7 @@ class GraphImpl final : public WebNNGraphImpl {
std::unordered_map<uint64_t, uint32_t> constant_id_to_input_index_map,
GraphBufferBindingInfo graph_buffer_binding_info,
ComputeResourceInfo compute_resource_info,
ComPtr<IDMLCompiledOperator> compiled_operator);
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator);
// Create the GraphImpl instance and bind it to the mojom::WebNNGraph
// receiver, then run callback to send the pending remote to the
@ -219,7 +219,7 @@ class GraphImpl final : public WebNNGraphImpl {
base::WeakPtr<ContextImpl> context,
std::unique_ptr<CommandRecorder> command_recorder,
std::unique_ptr<PersistentResource> persistent_resource,
ComPtr<IDMLCompiledOperator> compiled_operator,
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator,
ComputeResourceInfo compute_resource_info,
GraphBufferBindingInfo graph_buffer_binding_info,
mojom::WebNNContext::CreateGraphCallback callback,
@ -276,7 +276,7 @@ class GraphImpl final : public WebNNGraphImpl {
std::unique_ptr<CommandRecorder> command_recorder_;
// IDMLCompiledOperator represents a compiled and initialized DML graph to be
// executed on GPU.
ComPtr<IDMLCompiledOperator> compiled_operator_;
Microsoft::WRL::ComPtr<IDMLCompiledOperator> compiled_operator_;
GraphBufferBindingInfo graph_buffer_binding_info_;
// The compute resource is pre-allocated after graph initialization and

@ -15,8 +15,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
// The TensorDesc wraps a tensor description (DML_TENSOR_DESC) needed by a DML
// graph. It owns the tensor's dimensions, strides and DML_BUFFER_TENSOR_DESC.
// The TensorDesc is prepared for building a DML graph's description

@ -159,9 +159,9 @@ std::vector<uint32_t> PermuteArray(base::span<const uint32_t> array,
return permuted_array;
}
ComPtr<ID3D12Device> GetD3D12Device(IDMLDevice* dml_device) {
Microsoft::WRL::ComPtr<ID3D12Device> GetD3D12Device(IDMLDevice* dml_device) {
CHECK(dml_device);
ComPtr<ID3D12Device> d3d12_device;
Microsoft::WRL::ComPtr<ID3D12Device> d3d12_device;
CHECK_EQ(dml_device->GetParentDevice(IID_PPV_ARGS(&d3d12_device)), S_OK);
return d3d12_device;
}
@ -207,8 +207,8 @@ D3D12_RESOURCE_BARRIER CreateTransitionBarrier(ID3D12Resource* resource,
}
void UploadBufferWithBarrier(CommandRecorder* command_recorder,
ComPtr<ID3D12Resource> dst_buffer,
ComPtr<ID3D12Resource> src_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> dst_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> src_buffer,
size_t buffer_size) {
// Copy the data from source buffer to destination buffer.
D3D12_RESOURCE_BARRIER barriers[1];
@ -226,10 +226,11 @@ void UploadBufferWithBarrier(CommandRecorder* command_recorder,
command_recorder->ResourceBarrier(barriers);
}
void ReadbackBufferWithBarrier(CommandRecorder* command_recorder,
ComPtr<ID3D12Resource> readback_buffer,
ComPtr<ID3D12Resource> default_buffer,
size_t buffer_size) {
void ReadbackBufferWithBarrier(
CommandRecorder* command_recorder,
Microsoft::WRL::ComPtr<ID3D12Resource> readback_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> default_buffer,
size_t buffer_size) {
// Copy the data from source buffer to destination buffer.
D3D12_RESOURCE_BARRIER barriers[1];
barriers[0] = CreateTransitionBarrier(default_buffer.Get(),
@ -254,7 +255,7 @@ mojom::ErrorPtr CreateError(mojom::Error::Code error_code,
HRESULT CreateDefaultBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource) {
Microsoft::WRL::ComPtr<ID3D12Resource>& resource) {
TRACE_EVENT2("gpu", "dml::CreateDefaultBuffer", "size", size, "name",
name_for_debugging);
auto heap_properties = CreateHeapProperties(D3D12_HEAP_TYPE_DEFAULT);
@ -273,7 +274,7 @@ HRESULT CreateDefaultBuffer(ID3D12Device* device,
HRESULT CreateUploadBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource) {
Microsoft::WRL::ComPtr<ID3D12Resource>& resource) {
TRACE_EVENT2("gpu", "dml::CreateUploadBuffer", "size", size, "name",
name_for_debugging);
auto heap_properties = CreateHeapProperties(D3D12_HEAP_TYPE_UPLOAD);
@ -291,7 +292,7 @@ HRESULT CreateUploadBuffer(ID3D12Device* device,
HRESULT CreateReadbackBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource) {
Microsoft::WRL::ComPtr<ID3D12Resource>& resource) {
CHECK(device);
TRACE_EVENT2("gpu", "dml::CreateReadbackBuffer", "size", size, "name",
name_for_debugging);
@ -307,10 +308,11 @@ HRESULT CreateReadbackBuffer(ID3D12Device* device,
return S_OK;
}
HRESULT CreateCustomUploadBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource) {
HRESULT CreateCustomUploadBuffer(
ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
Microsoft::WRL::ComPtr<ID3D12Resource>& resource) {
CHECK(device);
TRACE_EVENT2("gpu", "dml::CreateCustomUploadBuffer", "size", size, "name",
name_for_debugging);
@ -331,10 +333,11 @@ HRESULT CreateCustomUploadBuffer(ID3D12Device* device,
return S_OK;
}
HRESULT CreateCustomReadbackBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource) {
HRESULT CreateCustomReadbackBuffer(
ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
Microsoft::WRL::ComPtr<ID3D12Resource>& resource) {
CHECK(device);
TRACE_EVENT2("gpu", "dml::CreateCustomReadbackBuffer", "size", size, "name",
name_for_debugging);
@ -355,10 +358,11 @@ HRESULT CreateCustomReadbackBuffer(ID3D12Device* device,
return S_OK;
}
HRESULT CreateDescriptorHeap(ID3D12Device* device,
uint32_t num_descriptors,
const wchar_t* name_for_debugging,
ComPtr<ID3D12DescriptorHeap>& descriptor_heap) {
HRESULT CreateDescriptorHeap(
ID3D12Device* device,
uint32_t num_descriptors,
const wchar_t* name_for_debugging,
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap>& descriptor_heap) {
CHECK(device);
TRACE_EVENT2("gpu", "dml::CreateDescriptorHeap", "num_descriptors",
num_descriptors, "name", name_for_debugging);

@ -19,8 +19,6 @@
namespace webnn::dml {
using Microsoft::WRL::ComPtr;
uint64_t CalculateDMLBufferTensorSize(DML_TENSOR_DATA_TYPE data_type,
const std::vector<uint32_t>& dimensions,
const std::vector<uint32_t>& strides);
@ -37,7 +35,7 @@ std::vector<uint32_t> PermuteArray(base::span<const uint32_t> array,
base::span<const uint32_t> permutation);
// Gets the ID3D12Device used to create the IDMLDevice.
ComPtr<ID3D12Device> GetD3D12Device(IDMLDevice* dml_device);
Microsoft::WRL::ComPtr<ID3D12Device> GetD3D12Device(IDMLDevice* dml_device);
// Returns the maximum feature level supported by the DML device.
DML_FEATURE_LEVEL GetMaxSupportedDMLFeatureLevel(IDMLDevice* dml_device);
@ -53,17 +51,17 @@ D3D12_RESOURCE_BARRIER COMPONENT_EXPORT(WEBNN_SERVICE)
// for a single buffer or a big buffer combined from multiple buffers.
void COMPONENT_EXPORT(WEBNN_SERVICE)
UploadBufferWithBarrier(CommandRecorder* command_recorder,
ComPtr<ID3D12Resource> dst_buffer,
ComPtr<ID3D12Resource> src_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> dst_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> src_buffer,
size_t buffer_size);
// Helper function to readback data from GPU to CPU, the resource can be created
// for a single buffer or a big buffer combined from multiple buffers.
void COMPONENT_EXPORT(WEBNN_SERVICE)
ReadbackBufferWithBarrier(CommandRecorder* command_recorder,
ComPtr<ID3D12Resource> readback_buffer,
ComPtr<ID3D12Resource> default_buffer,
size_t buffer_size);
void COMPONENT_EXPORT(WEBNN_SERVICE) ReadbackBufferWithBarrier(
CommandRecorder* command_recorder,
Microsoft::WRL::ComPtr<ID3D12Resource> readback_buffer,
Microsoft::WRL::ComPtr<ID3D12Resource> default_buffer,
size_t buffer_size);
mojom::ErrorPtr CreateError(mojom::Error::Code error_code,
const std::string& error_message);
@ -77,7 +75,7 @@ HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateDefaultBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource);
Microsoft::WRL::ComPtr<ID3D12Resource>& resource);
// Create a resource with `size` bytes in D3D12_RESOURCE_STATE_GENERIC_READ
// state from the uploading heap of the owned D3D12 device.
@ -85,7 +83,7 @@ HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateUploadBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource);
Microsoft::WRL::ComPtr<ID3D12Resource>& resource);
// Create a resource with `size` bytes in D3D12_RESOURCE_STATE_COPY_DEST state
// from the reading-back heap of the owned D3D12 device.
@ -93,7 +91,7 @@ HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateReadbackBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource);
Microsoft::WRL::ComPtr<ID3D12Resource>& resource);
// Create a resource with `size` bytes in
// D3D12_RESOURCE_STATE_UNORDERED_ACCESS state and from a custom heap with CPU
@ -104,27 +102,27 @@ HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateCustomUploadBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource);
Microsoft::WRL::ComPtr<ID3D12Resource>& resource);
// Create a resource with `size` bytes in
// D3D12_RESOURCE_STATE_UNORDERED_ACCESS state and from a custom heap with CPU
// memory pool (D3D12_MEMORY_POOL_L0) optimized for CPU reading data back from
// GPU. This type of buffer should only be created for GPU with UMA (Unified
// Memory Architecture).
HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateCustomReadbackBuffer(ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
ComPtr<ID3D12Resource>& resource);
HRESULT COMPONENT_EXPORT(WEBNN_SERVICE) CreateCustomReadbackBuffer(
ID3D12Device* device,
uint64_t size,
const wchar_t* name_for_debugging,
Microsoft::WRL::ComPtr<ID3D12Resource>& resource);
// Create a descriptor heap with D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV type,
// D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE flag and large enough for the
// number of descriptors.
HRESULT COMPONENT_EXPORT(WEBNN_SERVICE)
CreateDescriptorHeap(ID3D12Device* device,
uint32_t num_descriptors,
const wchar_t* name_for_debugging,
ComPtr<ID3D12DescriptorHeap>& descriptor_heap);
HRESULT COMPONENT_EXPORT(WEBNN_SERVICE) CreateDescriptorHeap(
ID3D12Device* device,
uint32_t num_descriptors,
const wchar_t* name_for_debugging,
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap>& descriptor_heap);
} // namespace webnn::dml