-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[WebGPU EP] SoftMax Implementation #23538
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
Merged
+344
−29
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e7e3737
Add Softmax kernel and transpose utility function for WebGPU executio…
vraspar f9b61db
Refactor Softmax implementation for WebGPU
vraspar 87de607
Refactor Softmax and remove debug logs
vraspar 2d8b47d
fix linting error
vraspar 91f635c
Refactor Softmax implementation for improved readability and consiste…
vraspar 5cff682
Revert changes made to shader_variable.h
vraspar e6d8a09
Fix variable declaration in softmax
vraspar a2c7069
Apply feedback: use more suitable data types
vraspar b1b4124
Apply Feedback: fix naming and formatting
vraspar 48f6e11
Refactor Softmax implementation: improve variable naming for clarity
vraspar 1f6689a
apply lint and fix build breaks
vraspar ab678d0
Add missing include for string in softmax implementation
vraspar c1117d4
Refactor Softmax implementation: change int64_t to size_t for input r…
vraspar 7ed3fef
Refactor Softmax implementation: cast axis to size_t for consistency
vraspar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,238 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "core/common/inlined_containers.h" | ||
| #include "core/providers/common.h" | ||
| #include "core/providers/webgpu/math/softmax.h" | ||
| #include "core/providers/webgpu/tensor/transpose.h" | ||
| #include "core/providers/cpu/tensor/utils.h" | ||
| #include "core/providers/webgpu/shader_variable.h" | ||
| #include "core/providers/webgpu/shader_helper.h" | ||
| #include "core/providers/webgpu/webgpu_supported_types.h" | ||
| namespace onnxruntime { | ||
| namespace webgpu { | ||
|
|
||
| ONNX_OPERATOR_VERSIONED_KERNEL_EX( | ||
| Softmax, | ||
| kOnnxDomain, | ||
| 1, 10, | ||
| kWebGpuExecutionProvider, | ||
| (*KernelDefBuilder::Create()) | ||
| .TypeConstraint("T", WebGpuSupportedNumberTypes()), | ||
| Softmax); | ||
|
|
||
| ONNX_OPERATOR_VERSIONED_KERNEL_EX( | ||
| Softmax, | ||
| kOnnxDomain, | ||
| 11, 12, | ||
| kWebGpuExecutionProvider, | ||
| (*KernelDefBuilder::Create()) | ||
| .TypeConstraint("T", WebGpuSupportedNumberTypes()), | ||
| Softmax); | ||
|
|
||
| ONNX_OPERATOR_KERNEL_EX( | ||
| Softmax, | ||
| kOnnxDomain, | ||
| 13, | ||
| kWebGpuExecutionProvider, | ||
| (*KernelDefBuilder::Create()) | ||
| .TypeConstraint("T", WebGpuSupportedNumberTypes()), | ||
| Softmax); | ||
|
|
||
| static std::string MaxVector(const std::string& name, int components) { | ||
| switch (components) { | ||
| case 1: | ||
| return name; | ||
| case 2: | ||
| return "max(" + name + ".x, " + name + ".y)"; | ||
| case 3: | ||
| return "max(max(" + name + ".x, " + name + ".y), " + name + ".z)"; | ||
| case 4: | ||
| return "max(max(" + name + ".x, " + name + ".y), max(" + name + ".z, " + name + ".w))"; | ||
| default: | ||
| ORT_THROW("Unsupported number of components: ", components); | ||
| } | ||
| } | ||
|
|
||
| static std::string SumVector(const std::string& x, int components) { | ||
| switch (components) { | ||
| case 1: | ||
| return x; | ||
| case 2: | ||
| return "(" + x + ".x + " + x + ".y" + ")"; | ||
| case 4: | ||
| return "(" + x + ".x + " + x + ".y + " + x + ".w + " + x + ".z" + ")"; | ||
| default: | ||
| ORT_THROW("Unsupported number of components: ", components); | ||
| } | ||
| } | ||
|
|
||
| static int GetMaxComponents(int64_t size) { | ||
| if (size % 4 == 0) { | ||
| return 4; | ||
| } else if (size % 2 == 0) { | ||
| return 2; | ||
| } | ||
| return 1; | ||
| } | ||
|
|
||
| Status SoftmaxProgram::GenerateShaderCode(ShaderHelper& shader) const { | ||
| // Add input and output variables | ||
| const auto& input = shader.AddInput("x", ShaderUsage::UseUniform | ShaderUsage::UseIndicesTypeAlias | ShaderUsage::UseValueTypeAlias); | ||
| shader.AddOutput("result", ShaderUsage::UseUniform | ShaderUsage::UseIndicesTypeAlias); | ||
| int components = input.NumComponents(); | ||
|
|
||
| const std::string thread_max_decl = is_fp32_ | ||
| ? "var thread_max = x_value_t(-3.402823e+38f);\n" | ||
| : "var thread_max = x_value_t(-65504.0h);\n"; | ||
|
|
||
| // Define shared memory for row max and row sum | ||
| shader.AdditionalImplementation() | ||
| << "var<workgroup> row_max_shared : x_value_t;\n" | ||
| << "var<workgroup> row_sum_shared : x_value_t;\n" | ||
| << "var<workgroup> thread_shared : array<x_value_t, " << wg_ << ">;\n"; | ||
|
|
||
| // Define helper functions to get and set values | ||
| shader.AdditionalImplementation() | ||
| << "fn getValue(row: i32, col: i32, row_stride: i32) -> x_value_t {\n" | ||
| << " let index = row * row_stride + col;\n" | ||
| << " return x[index];\n" | ||
| << "}\n" | ||
| << "fn setValue(row: i32, col: i32, row_stride: i32, value: x_value_t) {\n" | ||
| << " let index = row * row_stride + col;\n" | ||
| << " result[index] = value;\n" | ||
| << "}\n"; | ||
|
|
||
| // Main function body | ||
| shader.MainFunctionBody() | ||
| << " let gindex = i32(global_idx);\n" | ||
| << " let lindex = i32(local_idx);\n" | ||
| << " const wg = " << wg_ << ";\n" | ||
| << " let row = gindex / wg;\n" | ||
| << " let cols = uniforms.packedCols;\n" | ||
| << " let row_stride : i32 = uniforms.packedCols;\n" | ||
|
|
||
| // Find the row's max value | ||
| << thread_max_decl | ||
| << " for (var col = lindex; col < cols; col += wg) {\n" | ||
| << " let value = getValue(row, col, row_stride);\n" | ||
| << " thread_max = max(thread_max, value);\n" | ||
| << " }\n" | ||
| << " if (lindex < cols) {\n" | ||
| << " thread_shared[lindex] = thread_max;\n" | ||
| << " }\n" | ||
| << " workgroupBarrier();\n" | ||
|
|
||
| // Reduce to find the max value | ||
| << " var reduce_size = min(cols, wg);\n" | ||
| << " for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) {\n" | ||
| << " reduce_size = curr_size + (reduce_size & 1);\n" | ||
| << " if (lindex < curr_size) {\n" | ||
| << " thread_shared[lindex] = max(thread_shared[lindex], thread_shared[lindex + reduce_size]);\n" | ||
| << " }\n" | ||
| << " workgroupBarrier();\n" | ||
| << " }\n" | ||
| << " if (lindex == 0) {\n" | ||
| << " row_max_shared = x_value_t(" << MaxVector("thread_shared[0]", components) << ");\n" | ||
| << " }\n" | ||
| << " workgroupBarrier();\n" | ||
|
|
||
| // Find the row's sum of exponentials | ||
| << " var thread_sum = x_value_t(0.0);\n" | ||
| << " for (var col = lindex; col < cols; col += wg) {\n" | ||
| << " let sub_exp = exp(getValue(row, col, row_stride) - row_max_shared);\n" | ||
| << " thread_sum += sub_exp;\n" | ||
| << " }\n" | ||
| << " thread_shared[lindex] = thread_sum;\n" | ||
| << " workgroupBarrier();\n" | ||
|
|
||
| // Reduce to find the sum of exponentials | ||
| << " for (var curr_size = wg >> 1; curr_size > 0; curr_size = curr_size >> 1) {\n" | ||
| << " if (lindex < curr_size) {\n" | ||
| << " thread_shared[lindex] = thread_shared[lindex] + thread_shared[lindex + curr_size];\n" | ||
| << " }\n" | ||
| << " workgroupBarrier();\n" | ||
| << " }\n" | ||
| << " if (lindex == 0) {\n" | ||
| << " row_sum_shared = x_value_t(" << SumVector("thread_shared[0]", components) << ");\n" | ||
| << " }\n" | ||
| << " workgroupBarrier();\n" | ||
|
|
||
| // Calculate the final value for each element in the row | ||
| << " for (var col = lindex; col < cols; col += wg) {\n" | ||
| << " let value = exp(getValue(row, col, row_stride) - row_max_shared) / row_sum_shared;\n" | ||
| << " setValue(row, col, row_stride, value);\n" | ||
| << " }\n"; | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Softmax::ComputeInternal(ComputeContext& context) const { | ||
| const auto* input_tensor = context.Input(0); | ||
| const TensorShape& input_shape = input_tensor->Shape(); | ||
| size_t input_rank = input_shape.NumDimensions(); | ||
| auto* output_tensor = context.Output(0, input_shape); | ||
|
|
||
| // normalize axis | ||
| size_t axis = static_cast<size_t>(HandleNegativeAxis(axis_, input_rank)); | ||
| bool is_transpose_required = axis < input_rank - 1; | ||
|
|
||
| TensorShape transposed_input_shape; | ||
| Tensor transposed_input_tensor; | ||
| Tensor intermediate_output; | ||
| InlinedVector<size_t> perm(input_rank); | ||
|
|
||
| if (is_transpose_required) { | ||
| std::iota(std::begin(perm), std::end(perm), 0); | ||
| perm[axis] = input_rank - 1; | ||
| perm[input_rank - 1] = axis; | ||
|
|
||
| TensorShapeVector transposed_input_dims; | ||
| for (auto e : perm) { | ||
| transposed_input_dims.push_back(input_shape[e]); | ||
| } | ||
|
|
||
| transposed_input_shape = TensorShape(transposed_input_dims); | ||
| transposed_input_tensor = context.CreateGPUTensor(input_tensor->DataType(), transposed_input_shape); | ||
| ORT_RETURN_IF_ERROR(Transpose::DoTranspose(context, perm, *input_tensor, transposed_input_tensor)); | ||
| intermediate_output = context.CreateGPUTensor(output_tensor->DataType(), transposed_input_shape); | ||
| } | ||
|
|
||
| const int64_t cols = is_transpose_required ? transposed_input_shape[input_rank - 1] : input_shape[input_rank - 1]; | ||
| const int64_t rows = input_shape.Size() / cols; | ||
| const int64_t components = GetMaxComponents(cols); | ||
| const auto packed_cols = cols / components; | ||
| uint32_t workgroup_size = rows == 1 ? 256 : 64; | ||
| // check input tensor element type is float | ||
| const bool is_fp32 = input_tensor->GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT; | ||
|
|
||
| SoftmaxProgram program{workgroup_size, is_fp32}; | ||
| if (is_transpose_required) { | ||
| program | ||
| .AddInputs({{&transposed_input_tensor, ProgramTensorMetadataDependency::TypeAndRank, static_cast<int>(components)}}) | ||
| .AddOutputs({{&intermediate_output, ProgramTensorMetadataDependency::TypeAndRank, static_cast<int>(components)}}); | ||
| } else { | ||
| program | ||
| .AddInputs({{input_tensor, ProgramTensorMetadataDependency::TypeAndRank, static_cast<int>(components)}}) | ||
| .AddOutputs({{output_tensor, ProgramTensorMetadataDependency::TypeAndRank, static_cast<int>(components)}}); | ||
| } | ||
|
|
||
| program | ||
| .CacheHint(std::to_string(components), std::to_string(workgroup_size)) | ||
| .SetWorkgroupSize(workgroup_size) | ||
| .SetDispatchGroupSize(static_cast<uint32_t>(rows)) | ||
| .AddUniformVariables({{static_cast<int32_t>(packed_cols)}}); | ||
|
|
||
| ORT_RETURN_IF_ERROR(context.RunProgram(program)); | ||
|
|
||
| // If transpose was required, transpose the result back | ||
| if (is_transpose_required) { | ||
| ORT_RETURN_IF_ERROR(Transpose::DoTranspose(context, perm, intermediate_output, *output_tensor)); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
| } // namespace webgpu | ||
| } // namespace onnxruntime | ||
This file contains hidden or 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,54 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "core/providers/webgpu/webgpu_supported_types.h" | ||
| #include "core/providers/webgpu/webgpu_kernel.h" | ||
| #include "core/providers/webgpu/program.h" | ||
| #include "core/framework/op_kernel.h" | ||
|
|
||
| namespace onnxruntime { | ||
| namespace webgpu { | ||
|
|
||
| class Softmax final : public WebGpuKernel { | ||
| public: | ||
| Softmax(const OpKernelInfo& info) : WebGpuKernel{info} { | ||
| int opset_ = info.node().SinceVersion(); | ||
| int64_t axis; | ||
| Status status = info.GetAttr<int64_t>("axis", &axis); | ||
|
|
||
| if (status.IsOK()) { | ||
| axis_ = axis; | ||
| } else { | ||
| if (opset_ < 13) { | ||
| axis_ = 1; // opset-12 and below, the default axis value is 1 | ||
| } else { | ||
| axis_ = -1; // opset-13, the default axis value is -1 | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Status ComputeInternal(ComputeContext& context) const override; | ||
|
|
||
| private: | ||
| int64_t axis_; | ||
| }; | ||
|
|
||
| class SoftmaxProgram final : public Program<SoftmaxProgram> { | ||
| public: | ||
| SoftmaxProgram(uint32_t wg, bool is_fp32) | ||
| : Program{"Softmax"}, wg_{wg}, is_fp32_{is_fp32} { | ||
| } | ||
|
|
||
| Status GenerateShaderCode(ShaderHelper& sh) const override; | ||
|
|
||
| WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"packedCols", ProgramUniformVariableDataType::Int32}); | ||
|
|
||
| private: | ||
| uint32_t wg_; | ||
| bool is_fp32_; | ||
| }; | ||
|
|
||
| } // namespace webgpu | ||
| } // namespace onnxruntime |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.