-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implementation of WebGPU EP (#22591)
### Description This PR adds the actual implementation of the WebGPU EP based on #22318. This change includes the following: <details> <summary><b>core framework of WebGPU EP</b></summary> - WebGPU EP factory classes for: - handling WebGPU options - creating WebGPU EP instance - creating WebGPU context - WebGPU Execution Provider classes - GPU Buffer allocator - data transfer - Buffer management classes - Buffer Manager - BufferCacheManager - DisabledCacheManager - SimpleCacheManager - LazyReleaseCacheManager - BucketCacheManager - Program classes - Program (base) - Program Cache Key - Program Manager - Shader helper classes - Shader Helper - ShaderIndicesHelper - ShaderVariableHelper - Utils - GPU Query based profiler - compute context - string utils - Miscs - Python binding webgpu support (basic) </details> <details> <summary><b>Kernel implementation</b></summary> - onnx.ai (default opset): - Elementwise (math): Abs, Neg, Floor, Ceil, Reciprocal, Sqrt, Exp, Erf, Log, Sin, Cos, Tan, Asin, Acos, Atan, Sinh, Cosh, Asinh, Acosh, Atanh, Tanh, Not, Cast - Elementwise (activation): Sigmoid, HardSigmoid, Clip, Elu, Relu, LeakyRelu, ThresholdedRelu, Gelu - Binary (math): Add, Sub, Mul, Div, Pow, Equal, Greater, GreaterOrEqual, Less, LessOrEqual - (Tensors): Shape, Reshape, Squeeze, Unsqueeze - Where - Transpose - Concat - Expand - Gather - Tile - Range - LayerNormalization - com.microsoft - FastGelu - MatMulNBits - MultiHeadAttention - RotaryEmbedding - SkipLayerNormalization - LayerNormalization - SimplifiedLayerNormalization - SkipSimplifiedLayerNormalization </details> <details> <summary><b>Build, test and CI pipeline integration</b></summary> - build works for Windows, macOS and iOS - support onnxruntime_test_all and python node test - added a new unit test for `--use_external_dawn` build flag. - updated MacOS pipeline to build with WebGPU support - added a new pipeline for WebGPU Windows </details> This change does not include: - Node.js binding support for WebGPU (will be a separate PR)
- Loading branch information
Showing
102 changed files
with
10,241 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/providers/webgpu/shader_helper.h" | ||
#include "core/providers/webgpu/webgpu_supported_types.h" | ||
#include "core/providers/webgpu/math/unary_elementwise_ops.h" | ||
#include "contrib_ops/webgpu/bert/fast_gelu.h" | ||
#include "contrib_ops/webgpu/webgpu_contrib_kernels.h" | ||
|
||
namespace onnxruntime { | ||
namespace contrib { | ||
namespace webgpu { | ||
|
||
ONNX_OPERATOR_KERNEL_EX( | ||
FastGelu, | ||
kMSDomain, | ||
1, | ||
kWebGpuExecutionProvider, | ||
(*KernelDefBuilder::Create()) | ||
.TypeConstraint("T", WebGpuSupportedFloatTypes()), | ||
FastGelu); | ||
|
||
Status FastGeluProgram::GenerateShaderCode(ShaderHelper& shader) const { | ||
const auto& x = shader.AddInput("x", ShaderUsage::UseUniform | ShaderUsage::UseValueTypeAlias); | ||
const auto& y = shader.AddOutput("y", ShaderUsage::UseUniform); | ||
|
||
shader.AdditionalImplementation() << TanhImpl; | ||
shader.MainFunctionBody() << shader.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size") | ||
<< " var a = " << x.GetByOffset("global_idx") << ";\n"; | ||
if (Inputs().size() > 1) { | ||
const auto& bias = shader.AddInput("bias", ShaderUsage::UseUniform | ShaderUsage::UseShapeAndStride); | ||
if (bias_components_ == 1) { | ||
shader.MainFunctionBody() << " let bias_offset = global_idx * 4;\n" | ||
" a += x_value_t(" | ||
<< bias.GetByOffset("bias_offset % uniforms.bias_shape") << ", " | ||
<< bias.GetByOffset("(bias_offset + 1) % uniforms.bias_shape") << ", " | ||
<< bias.GetByOffset("(bias_offset + 2) % uniforms.bias_shape") << ", " | ||
<< bias.GetByOffset("(bias_offset + 3) % uniforms.bias_shape") << ");\n"; | ||
} else { | ||
shader.MainFunctionBody() << " a += " << bias.GetByOffset("global_idx % uniforms.bias_shape") + ";\n"; | ||
} | ||
} | ||
shader.MainFunctionBody() << y.SetByOffset("global_idx", onnxruntime::webgpu::FastGeluExpr); | ||
|
||
return Status::OK(); | ||
} | ||
|
||
Status FastGelu::ComputeInternal(onnxruntime::webgpu::ComputeContext& context) const { | ||
const auto* input = context.Input(0); | ||
const auto* bias = context.Input(1); | ||
auto* output = context.Output(0, input->Shape()); | ||
|
||
uint32_t data_size = gsl::narrow<uint32_t>(output->Shape().Size()); | ||
if (data_size == 0) { | ||
return Status::OK(); | ||
} | ||
|
||
const auto vec_size = (data_size + 3) / 4; | ||
uint32_t bias_size = 0; | ||
int bias_components = 1; | ||
|
||
if (bias != nullptr) { | ||
bias_size = gsl::narrow<uint32_t>(bias->Shape().Size()); | ||
if (bias_size % 4 == 0) { | ||
bias_components = 4; | ||
bias_size = bias_size / 4; | ||
} | ||
} | ||
|
||
FastGeluProgram program{bias_components}; | ||
program.AddInput({input, ProgramTensorMetadataDependency::Type, {vec_size}, 4}) | ||
.AddOutput({output, ProgramTensorMetadataDependency::None, {vec_size}, 4}) | ||
.SetDispatchGroupSize((vec_size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE) | ||
.AddUniformVariable({vec_size}); | ||
|
||
if (bias != nullptr) { | ||
program.AddInput({bias, ProgramTensorMetadataDependency::TypeAndRank, {bias_size}, bias_components}); | ||
} | ||
return context.RunProgram(program); | ||
} | ||
|
||
} // namespace webgpu | ||
} // namespace contrib | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "core/providers/webgpu/program.h" | ||
#include "core/providers/webgpu/webgpu_kernel.h" | ||
|
||
namespace onnxruntime { | ||
namespace contrib { | ||
namespace webgpu { | ||
|
||
using namespace onnxruntime::webgpu; | ||
using onnxruntime::webgpu::ComputeContext; | ||
|
||
class FastGeluProgram final : public Program<FastGeluProgram> { | ||
public: | ||
FastGeluProgram(int bias_components) : Program{"FastGelu"}, bias_components_{bias_components} { | ||
} | ||
|
||
Status GenerateShaderCode(ShaderHelper& sh) const override; | ||
|
||
WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"vec_size", ProgramUniformVariableDataType::Uint32}); | ||
|
||
private: | ||
int bias_components_; | ||
}; | ||
|
||
class FastGelu final : public WebGpuKernel { | ||
public: | ||
FastGelu(const OpKernelInfo& info) : WebGpuKernel(info) {} | ||
|
||
Status ComputeInternal(ComputeContext& context) const override; | ||
}; | ||
|
||
} // namespace webgpu | ||
} // namespace contrib | ||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/providers/webgpu/shader_helper.h" | ||
#include "core/providers/webgpu/webgpu_supported_types.h" | ||
#include "core/providers/webgpu/nn/layer_norm.h" | ||
#include "contrib_ops/webgpu/webgpu_contrib_kernels.h" | ||
|
||
namespace onnxruntime { | ||
namespace contrib { | ||
namespace webgpu { | ||
|
||
using namespace onnxruntime::webgpu; | ||
using onnxruntime::webgpu::ComputeContext; | ||
|
||
ONNX_OPERATOR_VERSIONED_KERNEL_EX( | ||
LayerNormalization, | ||
kOnnxDomain, | ||
1, | ||
16, | ||
kWebGpuExecutionProvider, | ||
(*KernelDefBuilder::Create()).TypeConstraint("T", WebGpuSupportedFloatTypes()), | ||
onnxruntime::webgpu::LayerNorm<false>); | ||
|
||
ONNX_OPERATOR_KERNEL_EX( | ||
SimplifiedLayerNormalization, | ||
kOnnxDomain, | ||
1, | ||
kWebGpuExecutionProvider, | ||
(*KernelDefBuilder::Create()).TypeConstraint("T", WebGpuSupportedFloatTypes()), | ||
onnxruntime::webgpu::LayerNorm<true>); | ||
|
||
} // namespace webgpu | ||
} // namespace contrib | ||
} // namespace onnxruntime |
Oops, something went wrong.