|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "core/providers/webgpu/shader_helper.h" |
| 5 | +#include "core/providers/webgpu/webgpu_supported_types.h" |
| 6 | +#include "core/providers/webgpu/math/unary_elementwise_ops.h" |
| 7 | +#include "contrib_ops/webgpu/bert/bias_gelu.h" |
| 8 | +#include "contrib_ops/webgpu/webgpu_contrib_kernels.h" |
| 9 | + |
| 10 | +namespace onnxruntime { |
| 11 | +namespace contrib { |
| 12 | +namespace webgpu { |
| 13 | + |
| 14 | +ONNX_OPERATOR_KERNEL_EX( |
| 15 | + BiasGelu, |
| 16 | + kMSDomain, |
| 17 | + 1, |
| 18 | + kWebGpuExecutionProvider, |
| 19 | + (*KernelDefBuilder::Create()) |
| 20 | + .TypeConstraint("T", WebGpuSupportedFloatTypes()), |
| 21 | + BiasGelu); |
| 22 | + |
| 23 | +Status BiasGeluProgram::GenerateShaderCode(ShaderHelper& shader) const { |
| 24 | + const auto& x = shader.AddInput("x", ShaderUsage::UseUniform | ShaderUsage::UseValueTypeAlias); |
| 25 | + const auto& bias = shader.AddInput("bias", ShaderUsage::UseUniform | ShaderUsage::UseShapeAndStride); |
| 26 | + const auto& y = shader.AddOutput("y", ShaderUsage::UseUniform); |
| 27 | + |
| 28 | + shader.AdditionalImplementation() << onnxruntime::webgpu::ErfImpl; |
| 29 | + shader.MainFunctionBody() << shader.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size") |
| 30 | + << " var a = " << x.GetByOffset("global_idx") << ";\n"; |
| 31 | + |
| 32 | + // Add bias to input |
| 33 | + if (bias_components_ == 1) { |
| 34 | + shader.MainFunctionBody() << " let bias_offset = global_idx * 4;\n" |
| 35 | + " a += x_value_t(" |
| 36 | + << bias.GetByOffset("bias_offset % uniforms.bias_shape") << ", " |
| 37 | + << bias.GetByOffset("(bias_offset + 1) % uniforms.bias_shape") << ", " |
| 38 | + << bias.GetByOffset("(bias_offset + 2) % uniforms.bias_shape") << ", " |
| 39 | + << bias.GetByOffset("(bias_offset + 3) % uniforms.bias_shape") << ");\n"; |
| 40 | + } else { |
| 41 | + shader.MainFunctionBody() << " a += " << bias.GetByOffset("global_idx % uniforms.bias_shape") + ";\n"; |
| 42 | + } |
| 43 | + |
| 44 | + // Apply GELU activation: 0.5 * a * (1.0 + erf(a * 0.7071067811865475)) |
| 45 | + shader.MainFunctionBody() << y.SetByOffset("global_idx", onnxruntime::webgpu::GeluExpr); |
| 46 | + |
| 47 | + return Status::OK(); |
| 48 | +} |
| 49 | + |
| 50 | +Status BiasGelu::ComputeInternal(onnxruntime::webgpu::ComputeContext& context) const { |
| 51 | + const auto* input = context.Input(0); |
| 52 | + const auto* bias = context.Input(1); |
| 53 | + auto* output = context.Output(0, input->Shape()); |
| 54 | + |
| 55 | + uint32_t data_size = onnxruntime::narrow<uint32_t>(output->Shape().Size()); |
| 56 | + if (data_size == 0) { |
| 57 | + return Status::OK(); |
| 58 | + } |
| 59 | + |
| 60 | + const auto& input_shape = input->Shape(); |
| 61 | + const auto& bias_shape = bias->Shape(); |
| 62 | + |
| 63 | + // Validate inputs |
| 64 | + if (input_shape.NumDimensions() < 1 || bias_shape.NumDimensions() != 1) { |
| 65 | + return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, |
| 66 | + "BiasGelu: input must have at least 1 dimension and bias must be 1-dimensional."); |
| 67 | + } |
| 68 | + |
| 69 | + if (input_shape.GetDims().back() != bias_shape.GetDims().back()) { |
| 70 | + return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, |
| 71 | + "BiasGelu: bias must match the last dimension of input."); |
| 72 | + } |
| 73 | + |
| 74 | + const auto vec_size = (data_size + 3) / 4; |
| 75 | + uint32_t bias_size = onnxruntime::narrow<uint32_t>(bias->Shape().Size()); |
| 76 | + int bias_components = 1; |
| 77 | + |
| 78 | + if (bias_size % 4 == 0) { |
| 79 | + bias_components = 4; |
| 80 | + bias_size = bias_size / 4; |
| 81 | + } |
| 82 | + |
| 83 | + BiasGeluProgram program{bias_components}; |
| 84 | + program.AddInput({input, ProgramTensorMetadataDependency::Type, {vec_size}, 4}) |
| 85 | + .AddInput({bias, ProgramTensorMetadataDependency::TypeAndRank, {bias_size}, bias_components}) |
| 86 | + .AddOutput({output, ProgramTensorMetadataDependency::None, {vec_size}, 4}) |
| 87 | + .SetDispatchGroupSize((vec_size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE) |
| 88 | + .AddUniformVariable({vec_size}); |
| 89 | + |
| 90 | + return context.RunProgram(program); |
| 91 | +} |
| 92 | + |
| 93 | +} // namespace webgpu |
| 94 | +} // namespace contrib |
| 95 | +} // namespace onnxruntime |
0 commit comments