-
Notifications
You must be signed in to change notification settings - Fork 3k
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-native] Add transpose op #21986
Merged
fs-eire
merged 8 commits into
microsoft:fs-eire/webgpu-ep
from
axinging:transpose_webgpunative
Sep 11, 2024
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
22affbc
[webgpu-native] Add transpose op
axinging 9745148
Nit
axinging a9c3759
Fix comments
axinging 92cec7d
Rebase
axinging ed4134f
Rebase 2
axinging 65f549c
Merge version
axinging c3189f6
Merge remote-tracking branch 'origin/fs-eire/webgpu-ep' into transpos…
fs-eire d77ab42
fix and revise transpose (naive)
fs-eire 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 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,111 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "core/common/inlined_containers.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" | ||
|
||
namespace onnxruntime { | ||
namespace webgpu { | ||
|
||
ONNX_OPERATOR_VERSIONED_KERNEL_EX( | ||
Transpose, | ||
kOnnxDomain, | ||
1, 12, | ||
kCudaExecutionProvider, | ||
(*KernelDefBuilder::Create()) | ||
.TypeConstraint("T", DataTypeImpl::AllFixedSizeTensorTypes()), | ||
Transpose); | ||
|
||
ONNX_OPERATOR_KERNEL_EX( | ||
Transpose, | ||
kOnnxDomain, | ||
13, | ||
kCudaExecutionProvider, | ||
(*KernelDefBuilder::Create()) | ||
.TypeConstraint("T", DataTypeImpl::AllFixedSizeTensorTypes()), | ||
Transpose); | ||
|
||
ONNX_OPERATOR_KERNEL_EX( | ||
Transpose, | ||
kOnnxDomain, | ||
17, | ||
kCudaExecutionProvider, | ||
(*KernelDefBuilder::Create()) | ||
.TypeConstraint("T", DataTypeImpl::AllFixedSizeTensorTypes()), | ||
Transpose); | ||
|
||
const std::string AppendPermFunction(std::string_view input_name, std::string_view output_name, gsl::span<const size_t> perm) { | ||
std::ostringstream ss; | ||
ss.imbue(std::locale::classic()); | ||
ss << "fn perm(i: " << output_name << "_indices_t" | ||
<< ")->" << input_name << "_indices_t " | ||
<< "{\n var a: " << input_name << "_indices_t;\n"; | ||
for (auto i = 0; i < perm.size(); ++i) { | ||
ss << " a[" << perm[i] << "] = i[" << i << "];\n"; | ||
} | ||
ss << " return a;\n}\n"; | ||
return ss.str(); | ||
} | ||
|
||
Status TransposeProgram::GenerateShaderCode(ShaderHelper& shader) const { | ||
const auto input_name{"x"}; | ||
const auto output_name{"y"}; | ||
const auto& input = shader.AddInput(input_name, | ||
ShaderVariable::UseUniform | ShaderVariable::UseIndicesTypeAlias); | ||
const auto& output = shader.AddOutput(output_name, | ||
ShaderVariable::UseUniform | ShaderVariable::UseIndicesTypeAlias); | ||
shader.AppendImplementation(AppendPermFunction(input_name, output_name, this->perm_)); | ||
shader.SetMainFunctionBody(shader.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size"), | ||
" let indices = ", output.OffsetToIndices("global_idx"), | ||
";\n" | ||
" let x_indices = perm(indices); \n", | ||
output.SetByOffset("global_idx", input.GetByIndices("x_indices"))); | ||
return Status::OK(); | ||
} | ||
|
||
Status Transpose::ComputeInternal(ComputeContext& context) const { | ||
const auto* input_tensor = context.Input(0); | ||
const TensorShape& input_shape = input_tensor->Shape(); | ||
int32_t rank = gsl::narrow_cast<int32_t>(input_shape.NumDimensions()); | ||
|
||
TensorShapeVector output_dims(rank); | ||
InlinedVector<size_t> default_perm(rank); | ||
const InlinedVector<size_t>* p_perm = nullptr; | ||
ORT_RETURN_IF_ERROR(ComputeOutputShape(*input_tensor, output_dims, default_perm, p_perm)); | ||
TensorShape output_shape(output_dims); | ||
auto* output_tensor = context.Output(0, output_shape); | ||
|
||
uint32_t output_size = gsl::narrow_cast<int32_t>(input_tensor->Shape().Size()); | ||
TransposeProgram program{*p_perm}; | ||
program | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. perm should either be a part of cache hint or uniform. currently it seems different perm may use the same shader... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
.CacheHint(absl::StrJoin(*p_perm, "-")) | ||
.AddInputs({{input_tensor, ProgramTensorMetadataDependency::Rank}}) | ||
.AddOutputs({output_tensor}) | ||
.SetDispatchGroupSize((output_size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE) | ||
.AddUniformVariables({ | ||
{static_cast<uint32_t>(output_size)}, | ||
}); | ||
return context.RunProgram(program); | ||
} | ||
|
||
#define WEBGPU_TRANSPOSE_KERNEL(OP_TYPE, VERSION, KERNEL_CLASS, TYPE) \ | ||
ONNX_OPERATOR_KERNEL_EX( \ | ||
OP_TYPE, kOnnxDomain, VERSION, kWebGpuExecutionProvider, \ | ||
KernelDefBuilder().TypeConstraint("T", TYPE), \ | ||
KERNEL_CLASS); | ||
|
||
#define WEBGPU_TRANSPOSE_VERSIONED_KERNEL(OP_TYPE, VERSION_FROM, VERSION_TO, KERNEL_CLASS, TYPE) \ | ||
ONNX_OPERATOR_VERSIONED_KERNEL_EX( \ | ||
OP_TYPE, kOnnxDomain, VERSION_FROM, VERSION_TO, kWebGpuExecutionProvider, \ | ||
KernelDefBuilder().TypeConstraint("T", TYPE), \ | ||
KERNEL_CLASS); | ||
|
||
WEBGPU_TRANSPOSE_VERSIONED_KERNEL(Transpose, 1, 12, Transpose, WebGpuSupportedFloatTypes()) | ||
WEBGPU_TRANSPOSE_KERNEL(Transpose, 13, Transpose, WebGpuSupportedFloatTypes()) | ||
WEBGPU_TRANSPOSE_KERNEL(Transpose, 17, Transpose, WebGpuSupportedFloatTypes()) | ||
|
||
} // namespace webgpu | ||
} // 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,37 @@ | ||
// 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/cpu/tensor/transpose.h" | ||
#include "core/providers/webgpu/webgpu_kernel.h" | ||
#include "core/providers/webgpu/program.h" | ||
|
||
namespace onnxruntime { | ||
namespace webgpu { | ||
|
||
class TransposeProgram final : public Program<TransposeProgram> { | ||
public: | ||
TransposeProgram(const gsl::span<const size_t>& permutations) | ||
: Program{"Transpose"}, perm_(permutations.begin(), permutations.end()) { | ||
} | ||
|
||
Status GenerateShaderCode(ShaderHelper& sh) const override; | ||
|
||
WEBGPU_PROGRAM_DEFINE_UNIFORM_VARIABLES({"output_size", ProgramUniformVariableDataType::Uint32}); | ||
|
||
private: | ||
InlinedVector<size_t> perm_; | ||
}; | ||
|
||
class Transpose final : public WebGpuKernel, public TransposeBase { | ||
public: | ||
Transpose(const OpKernelInfo& info) : WebGpuKernel{info}, TransposeBase{info} { | ||
} | ||
|
||
Status ComputeInternal(ComputeContext& context) const override; | ||
}; | ||
|
||
} // namespace webgpu | ||
} // 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may need a discussion: which is better? we can choose one to use then keep consistent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done gsl::narrow_cast<>