|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +#include <string> |
| 4 | +#include <utility> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +#include "core/providers/webgpu/webgpu_utils.h" |
| 8 | +#include "core/providers/webgpu/nn/im2col_matmul.h" |
| 9 | +#include "core/providers/webgpu/nn/activation_util.h" |
| 10 | + |
| 11 | +namespace onnxruntime { |
| 12 | +namespace webgpu { |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +// TODO: move to common header. |
| 17 | +template <typename T> |
| 18 | +inline T ceil_div(T numerator, T denominator) { |
| 19 | + return (numerator + denominator - 1) / denominator; |
| 20 | +} |
| 21 | + |
| 22 | +// Chooses the optimal tile size (M, N) for the im2col operation. |
| 23 | +// This tile size is performance-tuned and varies depending on the target device. |
| 24 | +std::pair<uint32_t, uint32_t> ChooseTileSize(uint32_t im2col_m, uint32_t im2col_n) { |
| 25 | + // Define a list of preferred (tile_m, tile_n) pairs in descending order of preference. |
| 26 | + const std::vector<std::pair<uint32_t, uint32_t>> kTileSizes = { |
| 27 | + std::make_pair(32, 64), |
| 28 | + std::make_pair(16, 64), |
| 29 | + }; |
| 30 | + |
| 31 | + for (const auto& tile_pair : kTileSizes) { |
| 32 | + const uint32_t tile_m = tile_pair.first; |
| 33 | + const uint32_t tile_n = tile_pair.second; |
| 34 | + |
| 35 | + const uint32_t dispatch_m = ceil_div(im2col_m, tile_m); |
| 36 | + const uint32_t dispatch_n = ceil_div(im2col_n, tile_n); |
| 37 | + const uint32_t dispatch = dispatch_m * dispatch_n; |
| 38 | + |
| 39 | + if (dispatch >= 128) { |
| 40 | + return tile_pair; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // If none of the tile sizes meet the dispatch >=128 requirement, |
| 45 | + return kTileSizes.back(); |
| 46 | +} |
| 47 | + |
| 48 | +// Add support for more devices. |
| 49 | +bool IsDeviceSupported(ComputeContext& context) { |
| 50 | + const wgpu::AdapterInfo& adapter_info = context.AdapterInfo(); |
| 51 | + |
| 52 | + if (adapter_info.vendor == std::string_view("intel")) { |
| 53 | + if (adapter_info.architecture == std::string_view("xe-2lpg")) { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace |
| 62 | + |
| 63 | +Status OIHW2OHWIProgram::GenerateShaderCode(ShaderHelper& shader) const { |
| 64 | + const auto& src = shader.AddInput("src", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 65 | + const auto& output = shader.AddOutput("output", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 66 | + |
| 67 | + return WGSL_TEMPLATE_APPLY(shader, "nn/oihw_to_ohwi.wgsl.template", |
| 68 | + WGSL_TEMPLATE_VARIABLE(output, output), |
| 69 | + WGSL_TEMPLATE_VARIABLE(src, src)); |
| 70 | +} |
| 71 | + |
| 72 | +Status Im2ColMatMulProgram::GenerateShaderCode(ShaderHelper& shader) const { |
| 73 | + const auto& src = shader.AddInput("src", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 74 | + const auto& weight = shader.AddInput("weight", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 75 | + if (has_bias_) { |
| 76 | + shader.AddInput("bias", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 77 | + } |
| 78 | + const auto& output = shader.AddOutput("output", ShaderUsage::UseValueTypeAlias | ShaderUsage::UseElementTypeAlias); |
| 79 | + |
| 80 | + ORT_ENFORCE(tile_m_ == 16 || tile_m_ == 32, "tile_m must be 16 or 32."); |
| 81 | + ORT_ENFORCE(tile_n_ == 64, "tile_n must be 64."); |
| 82 | + |
| 83 | + return WGSL_TEMPLATE_APPLY(shader, "nn/im2col_matmul.wgsl.template", |
| 84 | + WGSL_TEMPLATE_PARAMETER(has_bias, has_bias_), |
| 85 | + WGSL_TEMPLATE_PARAMETER(tile_m, tile_m_), |
| 86 | + WGSL_TEMPLATE_PARAMETER(tile_n, tile_n_), |
| 87 | + WGSL_TEMPLATE_PARAMETER(use_subgroup, use_subgroup_), |
| 88 | + WGSL_TEMPLATE_VARIABLE(output, output), |
| 89 | + WGSL_TEMPLATE_VARIABLE(src, src), |
| 90 | + WGSL_TEMPLATE_VARIABLE(weight, weight)); |
| 91 | +} |
| 92 | + |
| 93 | +Status ApplyIm2ColMatMulProgram(ComputeContext& context, |
| 94 | + bool is_channels_last, |
| 95 | + const std::vector<uint32_t>& dilations, |
| 96 | + const std::vector<uint32_t>& pads, |
| 97 | + const std::vector<uint32_t>& strides, |
| 98 | + Tensor* output) { |
| 99 | + const auto* src = context.Input<Tensor>(0); |
| 100 | + const auto* weight = context.Input<Tensor>(1); |
| 101 | + const bool has_bias = context.InputCount() > 2; |
| 102 | + const auto* bias = has_bias ? context.Input<Tensor>(2) : nullptr; |
| 103 | + |
| 104 | + // Transpose OIHW Weight to OHWI |
| 105 | + // TODO: Move to `Transpose` |
| 106 | + // TODO: Use prepack |
| 107 | + TensorShape weight_shape = weight->Shape(); |
| 108 | + const uint32_t channel_output = onnxruntime::narrow<uint32_t>(weight_shape[0]); |
| 109 | + const uint32_t channel_input = onnxruntime::narrow<uint32_t>(weight_shape[1]); |
| 110 | + const uint32_t kernel_height = onnxruntime::narrow<uint32_t>(weight_shape[2]); |
| 111 | + const uint32_t kernel_width = onnxruntime::narrow<uint32_t>(weight_shape[3]); |
| 112 | + |
| 113 | + TensorShape ohwi_weight_shape{channel_output, kernel_height, kernel_width, channel_input}; |
| 114 | + Tensor ohwi_weight = context.CreateGPUTensor(weight->DataType(), ohwi_weight_shape); |
| 115 | + OIHW2OHWIProgram transpose_program{}; |
| 116 | + transpose_program.SetWorkgroupSize(64); |
| 117 | + |
| 118 | + const uint32_t Ci_tiles = ceil_div(channel_input, 64u); |
| 119 | + transpose_program.SetDispatchGroupSize(channel_output, Ci_tiles); |
| 120 | + |
| 121 | + transpose_program.AddInput({weight, |
| 122 | + ProgramTensorMetadataDependency::TypeAndRank}); |
| 123 | + transpose_program.AddOutput({&ohwi_weight, |
| 124 | + ProgramTensorMetadataDependency::TypeAndRank}); |
| 125 | + transpose_program.AddUniformVariables({{channel_output}, |
| 126 | + {channel_input}, |
| 127 | + {kernel_height}, |
| 128 | + {kernel_width}, |
| 129 | + {Ci_tiles}, |
| 130 | + {ceil_div(kernel_height * kernel_height, 4u)}}); |
| 131 | + ORT_RETURN_IF_ERROR(context.RunProgram(transpose_program)); |
| 132 | + |
| 133 | + // im2col-matmul |
| 134 | + const TensorShape src_shape = src->Shape(); |
| 135 | + const TensorShape output_shape = output->Shape(); |
| 136 | + |
| 137 | + const uint32_t batch = onnxruntime::narrow<uint32_t>(src_shape[0]); |
| 138 | + const uint32_t src_height = onnxruntime::narrow<uint32_t>(src_shape[is_channels_last ? 1 : 2]); |
| 139 | + const uint32_t src_width = onnxruntime::narrow<uint32_t>(src_shape[is_channels_last ? 2 : 3]); |
| 140 | + const uint32_t output_height = onnxruntime::narrow<uint32_t>(output_shape[is_channels_last ? 1 : 2]); |
| 141 | + const uint32_t output_width = onnxruntime::narrow<uint32_t>(output_shape[is_channels_last ? 2 : 3]); |
| 142 | + |
| 143 | + const uint32_t im2col_m = output_height * output_width; |
| 144 | + const uint32_t im2col_k = kernel_height * kernel_width * channel_input; |
| 145 | + const uint32_t im2col_n = channel_output; |
| 146 | + |
| 147 | + const auto [tile_m, tile_n] = ChooseTileSize(im2col_m, im2col_n); |
| 148 | + const uint32_t workgroup_size = tile_n; |
| 149 | + |
| 150 | + // Check the device's subgroup size before shader compilation to avoid potential performance penalties |
| 151 | + // associated with conditional checks in the shader runtime. |
| 152 | + // |
| 153 | + // Ensure the subgroup size must be greater than or equal to `tile_m` to safely enable `use_subgroup`. |
| 154 | + // If the status of this condition is uncertain, the feature must be disabled. |
| 155 | + const bool use_subgroup = false; |
| 156 | + Im2ColMatMulProgram im2col_mm_program{has_bias, tile_m, tile_n, use_subgroup}; |
| 157 | + im2col_mm_program.SetWorkgroupSize(workgroup_size); |
| 158 | + |
| 159 | + const uint32_t M_tiles = ceil_div(im2col_m, tile_m); |
| 160 | + const uint32_t N_tiles = ceil_div(im2col_n, tile_n); |
| 161 | + im2col_mm_program.SetDispatchGroupSize(M_tiles, N_tiles, batch); |
| 162 | + |
| 163 | + im2col_mm_program.AddInput({src, |
| 164 | + ProgramTensorMetadataDependency::TypeAndRank, |
| 165 | + 4}); |
| 166 | + im2col_mm_program.AddInput({&ohwi_weight, |
| 167 | + ProgramTensorMetadataDependency::TypeAndRank, |
| 168 | + 4}); |
| 169 | + if (has_bias) { |
| 170 | + im2col_mm_program.AddInput({bias, |
| 171 | + ProgramTensorMetadataDependency::TypeAndRank}); |
| 172 | + } |
| 173 | + im2col_mm_program.AddOutput({output, |
| 174 | + ProgramTensorMetadataDependency::TypeAndRank}); |
| 175 | + im2col_mm_program.AddUniformVariables({{batch}, |
| 176 | + {src_height}, |
| 177 | + {src_width}, |
| 178 | + {channel_input}, |
| 179 | + {kernel_height}, |
| 180 | + {kernel_width}, |
| 181 | + {output_height}, |
| 182 | + {output_width}, |
| 183 | + {im2col_m}, |
| 184 | + {im2col_k}, |
| 185 | + {im2col_n}, |
| 186 | + {M_tiles}, |
| 187 | + {N_tiles}, |
| 188 | + {ceil_div(ceil_div(im2col_k, 4u), 4u)}, |
| 189 | + {dilations}, |
| 190 | + {pads}, |
| 191 | + {strides}}); |
| 192 | + im2col_mm_program.CacheHint(has_bias, tile_m, tile_n, use_subgroup); |
| 193 | + |
| 194 | + return context.RunProgram(im2col_mm_program); |
| 195 | +} |
| 196 | + |
| 197 | +bool CanApplyIm2ColMatMulProgram(ComputeContext& context, |
| 198 | + const bool is_channels_last, |
| 199 | + const ActivationKind activation_kind, |
| 200 | + const TensorShape weight_shape, |
| 201 | + const AutoPadType auto_pad, |
| 202 | + const uint32_t group) { |
| 203 | + if (!IsDeviceSupported(context)) { |
| 204 | + return false; |
| 205 | + } |
| 206 | + |
| 207 | + // TODO: Support !is_channels_last |
| 208 | + // TODO: Support fuse |
| 209 | + // TODO: Support auto pad |
| 210 | + // TODO: Support group conv |
| 211 | + if (!is_channels_last || activation_kind != ActivationKind::None || auto_pad != AutoPadType::NOTSET || group != 1) { |
| 212 | + return false; |
| 213 | + } |
| 214 | + |
| 215 | + // TODO: Support conv1d |
| 216 | + // TODO: Support conv2d_1x1 |
| 217 | + const uint32_t kernel_height = onnxruntime::narrow<uint32_t>(weight_shape[2]); |
| 218 | + const uint32_t kernel_width = onnxruntime::narrow<uint32_t>(weight_shape[3]); |
| 219 | + if (kernel_height == 1 || kernel_width == 1) { |
| 220 | + return false; |
| 221 | + } |
| 222 | + |
| 223 | + // TODO: Support channel input vec1 |
| 224 | + const uint32_t channel_input = onnxruntime::narrow<uint32_t>(weight_shape[1]); |
| 225 | + if (channel_input % 4 != 0) { |
| 226 | + return false; |
| 227 | + } |
| 228 | + |
| 229 | + return true; |
| 230 | +} |
| 231 | + |
| 232 | +} // namespace webgpu |
| 233 | +} // namespace onnxruntime |
0 commit comments