-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathwebgpu_context.h
192 lines (144 loc) · 6.03 KB
/
webgpu_context.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include <memory>
#include <mutex>
#include <webgpu/webgpu_cpp.h>
#include "core/common/common.h"
#include "core/framework/library_handles.h"
#include "core/providers/webgpu/webgpu_execution_provider.h"
#include "core/providers/webgpu/buffer_manager.h"
#include "core/providers/webgpu/program_manager.h"
namespace onnxruntime {
class Tensor;
namespace webgpu {
class WebGpuContext;
class ComputeContext;
class ProgramBase;
class WebGpuContextFactory {
public:
static WebGpuContext& CreateContext(int context_id,
WGPUInstance instance,
WGPUAdapter adapter,
WGPUDevice device,
ValidationMode validation_mode);
static WebGpuContext& GetContext(int context_id);
static void Cleanup();
private:
WebGpuContextFactory() {}
static std::unordered_map<int32_t, std::unique_ptr<WebGpuContext>> contexts_;
static std::mutex mutex_;
};
// Class WebGpuContext includes all necessary resources for the context.
class WebGpuContext final {
public:
void Initialize(const WebGpuExecutionProviderInfo& webgpu_ep_info, const void* dawn_proc_table);
Status Wait(wgpu::Future f);
const wgpu::Adapter& Adapter() const { return adapter_; }
const wgpu::Device& Device() const { return device_; }
const wgpu::AdapterInfo& AdapterInfo() const { return adapter_info_; }
const wgpu::Limits& DeviceLimits() const { return device_limits_; }
const wgpu::CommandEncoder& GetCommandEncoder() {
if (!current_command_encoder_) {
current_command_encoder_ = device_.CreateCommandEncoder();
}
return current_command_encoder_;
}
const wgpu::ComputePassEncoder& GetComputePassEncoder() {
if (!current_compute_pass_encoder_) {
auto& command_encoder = GetCommandEncoder();
wgpu::ComputePassDescriptor compute_pass_desc{};
if (is_profiling_ && query_type_ == TimestampQueryType::AtPasses) {
wgpu::ComputePassTimestampWrites timestampWrites = {
query_set_, num_pending_dispatches_ * 2, num_pending_dispatches_ * 2 + 1};
compute_pass_desc.timestampWrites = ×tampWrites;
}
current_compute_pass_encoder_ = command_encoder.BeginComputePass(&compute_pass_desc);
}
return current_compute_pass_encoder_;
}
void EndComputePass() {
if (current_compute_pass_encoder_) {
current_compute_pass_encoder_.End();
current_compute_pass_encoder_ = nullptr;
}
}
void Flush();
webgpu::BufferManager& BufferManager() const { return *buffer_mgr_; }
inline webgpu::ValidationMode ValidationMode() const {
return validation_mode_;
}
void StartProfiling();
void CollectProfilingData(profiling::Events& events);
void EndProfiling(TimePoint, profiling::Events& events, profiling::Events& cached_events);
Status Run(ComputeContext& context, const ProgramBase& program);
private:
enum class TimestampQueryType {
None = 0,
InsidePasses,
AtPasses
};
WebGpuContext(WGPUInstance instance, WGPUAdapter adapter, WGPUDevice device, webgpu::ValidationMode validation_mode)
: instance_{instance}, adapter_{adapter}, device_{device}, validation_mode_{validation_mode}, query_type_{TimestampQueryType::None} {}
ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(WebGpuContext);
std::vector<const char*> GetEnabledAdapterToggles() const;
std::vector<const char*> GetEnabledDeviceToggles() const;
std::vector<const char*> GetDisabledDeviceToggles() const;
std::vector<wgpu::FeatureName> GetAvailableRequiredFeatures(const wgpu::Adapter& adapter) const;
wgpu::RequiredLimits GetRequiredLimits(const wgpu::Adapter& adapter) const;
void WriteTimestamp(uint32_t query_index);
struct PendingKernelInfo {
PendingKernelInfo(std::string_view kernel_name,
std::string_view program_name,
std::string_view cache_key,
const std::vector<ProgramInput>& inputs,
const std::vector<ProgramOutput>& outputs)
: name{absl::StrJoin({kernel_name, program_name}, "_")}, cache_key{cache_key}, inputs{inputs}, outputs{outputs} {}
PendingKernelInfo(PendingKernelInfo&&) = default;
PendingKernelInfo& operator=(PendingKernelInfo&&) = default;
ORT_DISALLOW_COPY_AND_ASSIGNMENT(PendingKernelInfo);
std::string name;
std::string cache_key;
std::vector<ProgramInput> inputs;
std::vector<ProgramOutput> outputs;
};
struct PendingQueryInfo {
PendingQueryInfo(std::vector<PendingKernelInfo>&& kernels, wgpu::Buffer query_buffer)
: kernels{std::move(kernels)}, query_buffer{query_buffer} {}
PendingQueryInfo(PendingQueryInfo&&) = default;
PendingQueryInfo& operator=(PendingQueryInfo&&) = default;
ORT_DISALLOW_COPY_AND_ASSIGNMENT(PendingQueryInfo);
std::vector<PendingKernelInfo> kernels;
wgpu::Buffer query_buffer;
};
friend class WebGpuContextFactory;
std::once_flag init_flag_;
LibraryHandles modules_;
wgpu::Instance instance_;
wgpu::Adapter adapter_;
wgpu::Device device_;
webgpu::ValidationMode validation_mode_;
wgpu::AdapterInfo adapter_info_;
wgpu::Limits device_limits_;
wgpu::CommandEncoder current_command_encoder_;
wgpu::ComputePassEncoder current_compute_pass_encoder_;
std::unique_ptr<webgpu::BufferManager> buffer_mgr_;
std::unique_ptr<ProgramManager> program_mgr_;
uint32_t num_pending_dispatches_ = 0;
const uint32_t max_num_pending_dispatches_ = 16;
// profiling
TimestampQueryType query_type_;
wgpu::QuerySet query_set_;
wgpu::Buffer query_resolve_buffer_;
// info of kernels pending submission for a single batch
std::vector<PendingKernelInfo> pending_kernels_;
// info of queries pending appending to profiling events
std::vector<PendingQueryInfo> pending_queries_;
uint64_t gpu_timestamp_offset_ = 0;
bool is_profiling_ = false;
};
} // namespace webgpu
} // namespace onnxruntime