WebGPU: add f32 cast in TransposeProgram for int32 input tensors #8616
+46
−2
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.
Problem
When transposing a tensor with int32 input, the WebGPU backend fails with WGSL shader compilation warning and produces incorrect output values.
Error:
type mismatch for argument 2 in call to 'setOutputAtIndex', expected 'f32', got 'i32'This occurs because
A[...]returnsi32when input is int32, butsetOutputAtIndexexpectsf32.Technical Background
The WebGPU backend stores all tensor data as float32 in GPU buffers by design. The output buffer is always declared as
array<f32>, andsetOutputAtIndex(as well assetOutputAtIndexI32) always writes f32 values. This means TransposeProgram must always output f32.However, certain operations like
OneHotproduce int32 tensors at runtime, even when the model graph declares float types. When such an int32 tensor flows into TransposeProgram, the shader reads fromA: array<i32>but attempts to pass the i32 value directly tosetOutputAtIndex, which expects f32. Since WGSL is strictly typed and does not allow implicit type conversion, the shader compilation fails.Fix
Add explicit
f32()cast around the array access.Notes
f32()on float32 is a no-op