Skip to content

Commit 3df47b5

Browse files
authored
fix 0 tensor issue in matmul and scatter_nd (microsoft#25326)
seen with gemma3n embedding model
1 parent 9b25b6a commit 3df47b5

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

onnxruntime/core/providers/webgpu/math/matmul.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Status MatMul::ComputeInternal(ComputeContext& context) const {
109109

110110
ORT_RETURN_IF_ERROR(helper.Compute(a->Shape(), b->Shape()));
111111
auto* output_tensor = context.Output(0, helper.OutputShape());
112+
if (output_tensor->Shape().Size() == 0) {
113+
// If the output tensor is empty, we can return early.
114+
return Status::OK();
115+
}
112116
bool has_bias = context.InputCount() > 2;
113117

114118
if (helper.N() < 8 && helper.K() < 8) { // call MatMulNaiveProgram

onnxruntime/core/providers/webgpu/tensor/scatter_nd.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ Status ScatterND::ComputeInternal(ComputeContext& context) const {
153153
const size_t components = 1;
154154
auto output_size = static_cast<uint32_t>((indices_shape.SizeToDimension(indices_rank - 1) + components - 1) / components);
155155
auto* output = context.Output(0, input_shape);
156+
if (output_size == 0) {
157+
// If the output tensor is empty, we can return early.
158+
return Status::OK();
159+
}
156160
MLDataType data_type = input->DataType();
157161
const void* source = input->DataRaw();
158162
void* target = output->MutableDataRaw();

0 commit comments

Comments
 (0)