|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "core/providers/webgpu/reduction/reduction_ops.h" |
| 5 | +#include <sstream> |
| 6 | +#include "core/framework/data_transfer_manager.h" |
| 7 | +#include "core/providers/webgpu/data_transfer.h" |
| 8 | +#include "core/providers/webgpu/shader_helper.h" |
| 9 | +#include "core/providers/webgpu/webgpu_supported_types.h" |
| 10 | + |
| 11 | +namespace onnxruntime { |
| 12 | +namespace webgpu { |
| 13 | + |
| 14 | +#define REGISTER_UNARY_ELEMENTWISE_VERSIONED_KERNEL(ReduceOp, begin, end) \ |
| 15 | + ONNX_OPERATOR_VERSIONED_KERNEL_EX( \ |
| 16 | + ReduceOp, \ |
| 17 | + kOnnxDomain, \ |
| 18 | + begin, end, \ |
| 19 | + kWebGpuExecutionProvider, \ |
| 20 | + (*KernelDefBuilder::Create()).TypeConstraint("T", WebGpuSupportedNumberTypes()), \ |
| 21 | + ReduceOp); |
| 22 | + |
| 23 | +#define REGISTER_UNARY_ELEMENTWISE_KERNEL(ReduceOp, version) \ |
| 24 | + ONNX_OPERATOR_KERNEL_EX( \ |
| 25 | + ReduceOp, \ |
| 26 | + kOnnxDomain, \ |
| 27 | + version, \ |
| 28 | + kWebGpuExecutionProvider, \ |
| 29 | + (*KernelDefBuilder::Create()).TypeConstraint("T", WebGpuSupportedNumberTypes()).InputMemoryType(OrtMemTypeCPUInput, 1), \ |
| 30 | + ReduceOp); |
| 31 | + |
| 32 | +REGISTER_UNARY_ELEMENTWISE_VERSIONED_KERNEL(ReduceMean, 1, 10); |
| 33 | +REGISTER_UNARY_ELEMENTWISE_VERSIONED_KERNEL(ReduceMean, 11, 12); |
| 34 | +REGISTER_UNARY_ELEMENTWISE_VERSIONED_KERNEL(ReduceMean, 13, 17); |
| 35 | +REGISTER_UNARY_ELEMENTWISE_KERNEL(ReduceMean, 18); |
| 36 | + |
| 37 | +Status ReduceKernelProgram::GenerateShaderCode(ShaderHelper& shader) const { |
| 38 | + const auto& input = shader.AddInput("input", ShaderUsage::UseUniform | ShaderUsage::UseIndicesTypeAlias | ShaderUsage::UseValueTypeAlias); |
| 39 | + const auto& output = shader.AddOutput("output", ShaderUsage::UseUniform | ShaderUsage::UseIndicesTypeAlias | ShaderUsage::UseValueTypeAlias); |
| 40 | + bool reduce_on_all_axes = no_op_with_empty_axes_ == false && axes_.empty(); |
| 41 | + std::string loop_header = code_[0]; |
| 42 | + std::string loop_body = "let current_element: input_value_t = " + input.GetByIndices("input_indices") + ";\n" + code_[1]; |
| 43 | + std::string loop_footer = code_[2]; |
| 44 | + const auto input_rank = input.Rank(); |
| 45 | + for (int i = 0, l = 0; i < input_rank; ++i) { |
| 46 | + if (reduce_on_all_axes || std::find(axes_.begin(), axes_.end(), i) != axes_.end()) { |
| 47 | + if (keepdims_) { |
| 48 | + l++; |
| 49 | + } |
| 50 | + std::stringstream ss; |
| 51 | + std::string index = "i" + std::to_string(i); |
| 52 | + ss << "for (var " << index << " : u32 = 0; " << index << " < " << input.IndicesGet("uniforms.input_shape", i) << "; " << index << "++) {\n"; |
| 53 | + ss << input.IndicesSet("input_indices", i, index) << ";\n"; |
| 54 | + ss << loop_body << "\n"; |
| 55 | + ss << "}\n"; |
| 56 | + loop_body = ss.str(); |
| 57 | + } else { |
| 58 | + std::stringstream ss; |
| 59 | + ss << loop_header << "\n"; |
| 60 | + std::string index = "i" + std::to_string(i); |
| 61 | + ss << "let " << index << " = " << output.IndicesGet("output_indices", l) << ";\n"; |
| 62 | + ss << input.IndicesSet("input_indices", i, index) << ";\n"; |
| 63 | + loop_header = ss.str(); |
| 64 | + l++; |
| 65 | + } |
| 66 | + } |
| 67 | + std::stringstream input_indices_init_value; |
| 68 | + for (int i = 0; i < input_rank - 1; ++i) { |
| 69 | + input_indices_init_value << "0, "; |
| 70 | + } |
| 71 | + input_indices_init_value << "0"; |
| 72 | + shader.MainFunctionBody() << shader.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size") |
| 73 | + << "let output_indices: output_indices_t = " << output.OffsetToIndices("global_idx") << ";\n" |
| 74 | + << "var input_indices: input_indices_t = input_indices_t(" << input_indices_init_value.str() << ");\n" |
| 75 | + << loop_header << loop_body << loop_footer; |
| 76 | + shader.MainFunctionBody() << output.SetByOffset("global_idx", "output_value"); |
| 77 | + return Status::OK(); |
| 78 | +} |
| 79 | + |
| 80 | +template <bool allow_multi_axes> |
| 81 | +Status ReduceKernel<allow_multi_axes>::ComputeInternal(ComputeContext& context) const { |
| 82 | + const auto* input_tensor = context.Input(0); |
| 83 | + InlinedVector<uint32_t> input_axes; |
| 84 | + auto rank = input_tensor->Shape().NumDimensions(); |
| 85 | + auto transform_axis = [rank](int64_t axis) { |
| 86 | + if (axis < 0) { |
| 87 | + axis += rank; |
| 88 | + } |
| 89 | + if (axis < 0 || static_cast<size_t>(axis) >= rank) { |
| 90 | + ORT_THROW("Axes values must be in the range [-rank, rank-1]. Got: ", axis); |
| 91 | + } |
| 92 | + return static_cast<uint32_t>(axis); |
| 93 | + }; |
| 94 | + // Check if axes input is provided and copy the axes values to input_axes |
| 95 | + if (context.InputCount() > 1) { |
| 96 | + ORT_ENFORCE(axes_.empty(), "Axes attribute may not be specified when axes input is also provided."); |
| 97 | + const Tensor* axes_tensor = context.Input<Tensor>(1); |
| 98 | + auto size = static_cast<size_t>(axes_tensor->Shape()[0]); |
| 99 | + const auto* data = axes_tensor->Data<int64_t>(); |
| 100 | + input_axes.reserve(size); |
| 101 | + std::transform(data, data + size, std::back_inserter(input_axes), transform_axis); |
| 102 | + } else { |
| 103 | + input_axes.reserve(axes_.size()); |
| 104 | + std::transform(axes_.begin(), axes_.end(), std::back_inserter(input_axes), transform_axis); |
| 105 | + } |
| 106 | + if (input_axes.empty()) { |
| 107 | + if (noop_with_empty_axes_ || rank == 0) { |
| 108 | + // If axes is empty and noop_with_empty_axes_ is true, it is a no-op according to the spec |
| 109 | + // If input tensor is a scalar, return the input tensor as is. |
| 110 | + // This is not correct for ReduceLogSum and ReduceSumSquare |
| 111 | + // TODO handle these cases separately. |
| 112 | + auto output = context.Output(0, input_tensor->Shape()); |
| 113 | + if (output->DataRaw() != input_tensor->DataRaw()) { |
| 114 | + ORT_RETURN_IF_ERROR(Info().GetDataTransferManager().CopyTensor(*input_tensor, *output)); |
| 115 | + } |
| 116 | + return Status::OK(); |
| 117 | + } else { |
| 118 | + // If axes is empty and noop_with_empty_axes_ is false, it is a reduction over all axes |
| 119 | + input_axes.resize(rank); |
| 120 | + std::iota(input_axes.begin(), input_axes.end(), 0); |
| 121 | + } |
| 122 | + } |
| 123 | + const auto code = GetOpSpecificCode(input_tensor, input_axes.size()); |
| 124 | + // Compute output shape |
| 125 | + std::vector<int64_t> output_shape; |
| 126 | + for (size_t i = 0; i < input_tensor->Shape().NumDimensions(); ++i) { |
| 127 | + if (std::find(input_axes.begin(), input_axes.end(), i) != input_axes.end()) { |
| 128 | + if (keepdims_) { |
| 129 | + output_shape.push_back(1); |
| 130 | + } |
| 131 | + } else { |
| 132 | + output_shape.push_back(input_tensor->Shape()[i]); |
| 133 | + } |
| 134 | + } |
| 135 | + TensorShape output_tensor_shape(output_shape); |
| 136 | + int64_t output_size = output_tensor_shape.Size(); |
| 137 | + ReduceKernelProgram program("ReduceMean", keepdims_, noop_with_empty_axes_, input_axes, code); |
| 138 | + program.AddInput({input_tensor, ProgramTensorMetadataDependency::TypeAndRank}) |
| 139 | + .AddOutput({context.Output(0, output_shape), ProgramTensorMetadataDependency::TypeAndRank}) |
| 140 | + .SetDispatchGroupSize((output_size + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE) |
| 141 | + .AddUniformVariables({{static_cast<uint32_t>(output_size)}, |
| 142 | + {static_cast<uint32_t>(noop_with_empty_axes_ ? 1 : 0)}, |
| 143 | + {input_axes}, |
| 144 | + {static_cast<uint32_t>(input_axes.size())}}); |
| 145 | + |
| 146 | + return context.RunProgram(program); |
| 147 | +} |
| 148 | + |
| 149 | +ReduceOpSpecificCode ReduceMean::GetOpSpecificCode(const Tensor* input_tensor, size_t axes_size) const { |
| 150 | + const TensorShape& input_shape = input_tensor->Shape(); |
| 151 | + size_t input_rank = input_shape.NumDimensions(); |
| 152 | + std::stringstream ss; |
| 153 | + ss << "var size: u32 = 1;\n" |
| 154 | + << "for (var i: u32 = 0; i < uniforms.axes_size; i += 1) { \n" |
| 155 | + << " let index = " << GetElementAt("uniforms.axes", "i", axes_size) << ";\n" |
| 156 | + << " size = size * " << GetElementAt("uniforms.input_shape", "index", input_rank) << ";\n" |
| 157 | + << "}\n" |
| 158 | + << "let output_value = output_value_t(sum / f32(size));"; |
| 159 | + ReduceOpSpecificCode code({"var sum = f32(0);", "sum += f32(current_element);", ss.str()}); |
| 160 | + return code; |
| 161 | +} |
| 162 | + |
| 163 | +Status ReduceMean::ComputeInternal(ComputeContext& ctx) const { |
| 164 | + return ReduceKernel<true>::ComputeInternal(ctx); |
| 165 | +} |
| 166 | + |
| 167 | +} // namespace webgpu |
| 168 | +} // namespace onnxruntime |
0 commit comments