diff --git a/onnxruntime/contrib_ops/webgpu/bert/rotary_embedding.cc b/onnxruntime/contrib_ops/webgpu/bert/rotary_embedding.cc index 20e1583e0da8f..b33084d60cec3 100644 --- a/onnxruntime/contrib_ops/webgpu/bert/rotary_embedding.cc +++ b/onnxruntime/contrib_ops/webgpu/bert/rotary_embedding.cc @@ -27,7 +27,7 @@ Status RotaryEmbeddingProgram::GenerateShaderCode(ShaderHelper& shader) const { const auto& sin_cache = shader.AddInput("sin_cache", ShaderUsage::UseUniform); const auto& output = shader.AddOutput("output", ShaderUsage::UseUniform); // TODO: remove output_indices. - const auto& output_indices = shader.AddIndices("output_indices", false); + const auto& output_indices = shader.AddIndices("output_indices", ShaderUsage::None); const auto interleaved_str = interleaved_ ? "true" : "false"; shader.MainFunctionBody() << " let half_rotary_emb_dim = uniforms.cos_cache_shape[1];\n" " let bsnh = global_idx / uniforms.global_stride % uniforms.global_shape;\n" diff --git a/onnxruntime/core/providers/webgpu/shader_helper.cc b/onnxruntime/core/providers/webgpu/shader_helper.cc index 19cab9b178b1f..db14cb88d1963 100644 --- a/onnxruntime/core/providers/webgpu/shader_helper.cc +++ b/onnxruntime/core/providers/webgpu/shader_helper.cc @@ -104,12 +104,19 @@ const ShaderVariableHelper& ShaderHelper::AddOutput(const std::string& name, Sha return AddVariableImpl(false, name, usage, dims); } -const ShaderIndicesHelper& ShaderHelper::AddIndices(const std::string& name, bool use_uniform) { +const ShaderIndicesHelper& ShaderHelper::AddIndices(const std::string& name, ShaderUsage usage) { const size_t indices_index = indices_vars_.size(); + ORT_ENFORCE(indices_index < program_.Indices().size(), + "Too many indices in the program (", program_.Indices().size(), ")"); + + // usage of indices should not use flag other than UseUniform and UseIndicesTypeAlias + ORT_ENFORCE(!(usage & ~(ShaderUsage::UseUniform | ShaderUsage::UseIndicesTypeAlias)), + "Invalid usage for indices variable ", name); + return *indices_vars_.emplace_back( std::make_unique(name, ProgramVariableDataType::InvalidType, - use_uniform ? ShaderUsage::UseUniform : ShaderUsage::None, + usage, program_.Indices()[indices_index])); } diff --git a/onnxruntime/core/providers/webgpu/shader_helper.h b/onnxruntime/core/providers/webgpu/shader_helper.h index 64b4c054f93d4..e5f316a46bedc 100644 --- a/onnxruntime/core/providers/webgpu/shader_helper.h +++ b/onnxruntime/core/providers/webgpu/shader_helper.h @@ -86,7 +86,7 @@ class ShaderHelper final { ShaderUsage usage = ShaderUsage::UseIndicesTypeAlias | ShaderUsage::UseValueTypeAlias | ShaderUsage::UseUniform); // Add an indices variable to the shader. - const ShaderIndicesHelper& AddIndices(const std::string& name, bool use_uniform = true); + const ShaderIndicesHelper& AddIndices(const std::string& name, ShaderUsage usage = ShaderUsage::UseUniform); // Get the string stream for additional implementation code to the shader. inline OStringStream& AdditionalImplementation() {