-
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.
### Description <!-- Describe your changes. --> ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> --------- Co-authored-by: Yulong Wang <[email protected]>
- Loading branch information
Showing
7 changed files
with
184 additions
and
30 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
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 "fast_gelu.h" | ||
Check warning on line 4 in onnxruntime/contrib_ops/webgpu/bert/fast_gelu.cc GitHub Actions / Optional Lint C++
|
||
#include "core/providers/webgpu/shader_helper.h" | ||
#include "core/providers/webgpu/webgpu_supported_types.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& input = shader.AddInput("input", ShaderVariable::UseUniform | ShaderVariable::UseValueTypeAlias); | ||
const auto& output = shader.AddOutput("output", ShaderVariable::UseUniform); | ||
|
||
std::string add_bias = ""; | ||
Check warning on line 26 in onnxruntime/contrib_ops/webgpu/bert/fast_gelu.cc GitHub Actions / Optional Lint C++
|
||
if (Inputs().size() > 1) { | ||
const auto& bias = shader.AddInput("bias", ShaderVariable::UseUniform | ShaderVariable::UseShapeAndStride); | ||
add_bias = bias_components_ == 1 ? " let bias_offset = global_idx * 4;\n" | ||
" x += input_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" | ||
: " x += " + bias.GetByOffset("global_idx % uniforms.bias_shape") + ";\n"; | ||
} | ||
|
||
shader.MainFunctionBody(shader.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size"), | ||
" var x = ", input.GetByOffset("global_idx"), ";\n", | ||
add_bias, | ||
" let y = x * (0.5 + 0.5 * tanh(x * (0.035677408136300125 * x * x + 0.7978845608028654)));\n ", | ||
Check warning on line 41 in onnxruntime/contrib_ops/webgpu/bert/fast_gelu.cc GitHub Actions / Optional Lint C++
|
||
output.SetByOffset("global_idx", "y")); | ||
|
||
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 = SafeInt<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 = SafeInt<uint32_t>(bias->Shape().Size()); | ||
if (bias_size % 4 == 0) { | ||
bias_components = 4; | ||
bias_size = bias_size / 4; | ||
} | ||
} | ||
|
||
FastGeluProgram program{bias_components}; | ||
program.Input({input, ProgramTensorMetadataDependency::Type, {vec_size}, 4}) | ||
.Output({output, ProgramTensorMetadataDependency::None, {vec_size}, 4}) | ||
.DispatchGroupSize((vec_size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE) | ||
.UniformVariable({vec_size}); | ||
|
||
if (bias != nullptr) { | ||
program.Input({bias, ProgramTensorMetadataDependency::TypeAndRank, {bias_size}, bias_components}) | ||
.CacheHint(std::to_string(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; | ||
Check warning on line 13 in onnxruntime/contrib_ops/webgpu/bert/fast_gelu.h GitHub Actions / Optional Lint C++
|
||
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
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