Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web] WebGPU incorrect output for Segment Anything Model (SAM) #18311

Closed
xenova opened this issue Nov 7, 2023 · 3 comments · Fixed by #18321
Closed

[Web] WebGPU incorrect output for Segment Anything Model (SAM) #18311

xenova opened this issue Nov 7, 2023 · 3 comments · Fixed by #18321
Assignees
Labels
ep:WebGPU ort-web webgpu provider model:transformer issues related to a transformer model: BERT, GPT2, Hugging Face, Longformer, T5, etc. platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@xenova
Copy link

xenova commented Nov 7, 2023

Describe the issue

Attempting to run the original Segment Anything Models (SAM) results in warnings and incorrect output. cc @guschmue @fs-eire

Output

  • Incorrect (WebGPU) output:
    image

  • Correct (WASM) output:
    image

Warnings

An uncaught WebGPU validation error was raised: Tint WGSL reader failure: :47:20 error: index 3 out of bounds [0..2]
      inputIndices[3] = max(0, min(col, undefined - 1));
                   ^

 - While validating [ShaderModuleDescriptor]
 - While calling [Device].CreateShaderModule([ShaderModuleDescriptor]).
An uncaught WebGPU validation error was raised: [Invalid ShaderModule] is invalid.
 - While validating compute stage ([Invalid ShaderModule], entryPoint: main).
 - While calling [Device].CreateComputePipeline([ComputePipelineDescriptor]).
[Invalid ComputePipeline] is invalid.
 - While Validating GetBindGroupLayout (0) on [Invalid ComputePipeline]

To reproduce

Run the following code in a browser that supports WebGPU:

import 'https://cdn.jsdelivr.net/npm/[email protected]/dist/ort.webgpu.min.js'

ort.env.wasm.wasmPaths='https://cdn.jsdelivr.net/npm/[email protected]/dist/'

const options = {
    executionProviders: ['webgpu'],
    graphOptimizationLevel: 'disabled',
}


const path = './sam-vit-base/onnx/vision_encoder.onnx';
const session = await ort.InferenceSession.create(path, options);
console.log(session);

// Dummy inputs
const inputs = {
    pixel_values: new ort.Tensor('float32', new Float32Array(1 * 3 * 1024 * 1024).fill(2.248908281326294), [1, 3, 1024, 1024])
}

const output = await session.run(inputs);
console.log(output);

Urgency

high - Blocks WebGPU support in Transformers.js

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.16.1

Execution Provider

'webgpu' (WebGPU)

@xenova xenova added the platform:web issues related to ONNX Runtime web; typically submitted using template label Nov 7, 2023
@github-actions github-actions bot added the model:transformer issues related to a transformer model: BERT, GPT2, Hugging Face, Longformer, T5, etc. label Nov 7, 2023
@guschmue
Copy link
Contributor

guschmue commented Nov 7, 2023

We have this https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js/segment-anything example and I did not run into it but I used a newer dev build and used samexporter.
Looking into it today.

@guschmue guschmue added the ep:WebGPU ort-web webgpu provider label Nov 7, 2023
@guschmue
Copy link
Contributor

guschmue commented Nov 7, 2023

ah, the model inputs/outputs are quite a bit different than the models I use.
The fix from @qjia7 looks good, values for wasm and webgpu are close:

webgpu
[-0.032305262982845306, -0.03442259877920151, -0.04370711371302605, -0.044772855937480927, -0.04492305591702461,
wasm
[-0.032305341213941574, -0.03442276269197464, -0.043707218021154404, -0.04477302357554436, -0.04492315277457237,

waiting for CI to finish.

@satyajandhyala satyajandhyala self-assigned this Nov 7, 2023
guschmue pushed a commit that referenced this issue Nov 7, 2023
### Description
For Resize, when `noScale` is true, the shader can become very simple,
which is not related with `attributes.mode` anymore. So we should remove
those parts of shader code for simplification.

This PR can also fix #18311 since the `noScale` are all true in that
model.

However, #18311 also exposes that the Resize implementation for `linear`
mode has bug. It seems that the currently implementation always treat
the input as either 2d or 4d tensor, however, the actual input is 3d
tensor, that's why the shader compilation is failed. We may need to fix
it in a separate PR.
@xenova
Copy link
Author

xenova commented Nov 26, 2023

I've just tried 1.16.3, but the error still seems to be persist. Was the latest release meant to include this fix? cc @guschmue

kleiti pushed a commit to kleiti/onnxruntime that referenced this issue Mar 22, 2024
…t#18321)

### Description
For Resize, when `noScale` is true, the shader can become very simple,
which is not related with `attributes.mode` anymore. So we should remove
those parts of shader code for simplification.

This PR can also fix microsoft#18311 since the `noScale` are all true in that
model.

However, microsoft#18311 also exposes that the Resize implementation for `linear`
mode has bug. It seems that the currently implementation always treat
the input as either 2d or 4d tensor, however, the actual input is 3d
tensor, that's why the shader compilation is failed. We may need to fix
it in a separate PR.
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this issue May 9, 2024
…t#18321)

### Description
For Resize, when `noScale` is true, the shader can become very simple,
which is not related with `attributes.mode` anymore. So we should remove
those parts of shader code for simplification.

This PR can also fix microsoft#18311 since the `noScale` are all true in that
model.

However, microsoft#18311 also exposes that the Resize implementation for `linear`
mode has bug. It seems that the currently implementation always treat
the input as either 2d or 4d tensor, however, the actual input is 3d
tensor, that's why the shader compilation is failed. We may need to fix
it in a separate PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ep:WebGPU ort-web webgpu provider model:transformer issues related to a transformer model: BERT, GPT2, Hugging Face, Longformer, T5, etc. platform:web issues related to ONNX Runtime web; typically submitted using template
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants