Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[webgpu] Use pushErrorScope()/popErrorScope() once for an inference run #23438

Merged
merged 4 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 2 additions & 18 deletions onnxruntime/core/providers/webgpu/compute_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@ ComputeContext::ComputeContext(OpKernelContext& kernel_context)
}

void ComputeContext::PushErrorScope() {
if (webgpu_context_.ValidationMode() >= ValidationMode::Basic) {
webgpu_context_.Device().PushErrorScope(wgpu::ErrorFilter::Validation);
}
webgpu_context_.PushErrorScopeIfNoLowerThan(ValidationMode::Full);
}

Status ComputeContext::PopErrorScope() {
Status status{};

if (webgpu_context_.ValidationMode() >= ValidationMode::Basic) {
ORT_RETURN_IF_ERROR(webgpu_context_.Wait(
webgpu_context_.Device().PopErrorScope(
wgpu::CallbackMode::WaitAnyOnly, [](wgpu::PopErrorScopeStatus pop_status, wgpu::ErrorType error_type, char const* message, Status* status) {
ORT_ENFORCE(pop_status == wgpu::PopErrorScopeStatus::Success, "Instance dropped.");
if (error_type == wgpu::ErrorType::NoError) {
return;
}
*status = ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "WebGPU validation failed. ", message);
},
&status)));
}
return status;
return webgpu_context_.PopErrorScopeIfNoLowerThan(ValidationMode::Full);
}

} // namespace webgpu
Expand Down
23 changes: 23 additions & 0 deletions onnxruntime/core/providers/webgpu/webgpu_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,29 @@ void WebGpuContext::EndProfiling(TimePoint /* tp */, profiling::Events& events,
}
}

void WebGpuContext::PushErrorScopeIfNoLowerThan(webgpu::ValidationMode validation_mode) {
jchen10 marked this conversation as resolved.
Show resolved Hide resolved
if (ValidationMode() >= validation_mode) {
device_.PushErrorScope(wgpu::ErrorFilter::Validation);
}
}

Status WebGpuContext::PopErrorScopeIfNoLowerThan(webgpu::ValidationMode validation_mode) {
Status status{};
if (ValidationMode() >= validation_mode) {
ORT_RETURN_IF_ERROR(Wait(device_.PopErrorScope(
wgpu::CallbackMode::WaitAnyOnly,
[](wgpu::PopErrorScopeStatus pop_status, wgpu::ErrorType error_type, char const* message, Status* status) {
ORT_ENFORCE(pop_status == wgpu::PopErrorScopeStatus::Success, "Instance dropped.");
if (error_type == wgpu::ErrorType::NoError) {
return;
}
*status = ORT_MAKE_STATUS(ONNXRUNTIME, FAIL, "WebGPU validation failed. ", message);
},
&status)));
}
return status;
}

void WebGpuContext::Flush() {
if (!current_command_encoder_) {
return;
Expand Down
14 changes: 14 additions & 0 deletions onnxruntime/core/providers/webgpu/webgpu_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ class WebGpuContext final {
void CollectProfilingData(profiling::Events& events);
void EndProfiling(TimePoint, profiling::Events& events, profiling::Events& cached_events);

//
// Push error scope if the context's validation mode is no lower than the given mode.
//
// This is useful only when "skip_validation" is not set.
//
void PushErrorScopeIfNoLowerThan(webgpu::ValidationMode validation_mode);

//
// Pop error scope if the context's validation mode is no lower than the given mode.
//
// This is useful only when "skip_validation" is not set.
//
Status PopErrorScopeIfNoLowerThan(webgpu::ValidationMode validation_mode);

Status Run(ComputeContext& context, const ProgramBase& program);

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,8 @@ std::unique_ptr<profiling::EpProfiler> WebGpuExecutionProvider::GetProfiler() {
}

Status WebGpuExecutionProvider::OnRunStart(const onnxruntime::RunOptions& /*run_options*/) {
context_.PushErrorScopeIfNoLowerThan(ValidationMode::Basic);

if (profiler_->Enabled()) {
context_.StartProfiling();
}
Expand All @@ -858,7 +860,7 @@ Status WebGpuExecutionProvider::OnRunEnd(bool /* sync_stream */, const onnxrunti
context_.CollectProfilingData(profiler_->Events());
}

return Status::OK();
return context_.PopErrorScopeIfNoLowerThan(ValidationMode::Basic);
}

bool WebGpuExecutionProvider::IsGraphCaptureEnabled() const {
Expand Down
Loading