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] 'Worker': ArrayBuffer at index 0 is not detachable and could not be transferred when running inference session #19488

Closed
nezaBacar opened this issue Feb 10, 2024 · 5 comments · Fixed by #19677
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@nezaBacar
Copy link

nezaBacar commented Feb 10, 2024

Describe the issue

I load two models cca. 800MB into the browser. When I attempt to run inference using a 650MB model with the ort.env.wasm.proxy flag set to true, the application crashes with an error message: "TypeError: Failed to execute 'postMessage' on 'Worker': ArrayBuffer at index 0 is not detachable and could not be transferred." Do you have any suggestions for preventing this error? Alternatively, if I don't set the flag to true, the inference process runs smoothly, but it causes the UI to freeze. Are there any alternative methods to prevent the UI from freezing?

To reproduce

Urgency

It's somewhat urgent

ONNX Runtime Installation

Built from Source

ONNX Runtime Version or Commit ID

1.16

Execution Provider

'wasm'/'cpu' (WebAssembly CPU)

@nezaBacar nezaBacar added the platform:web issues related to ONNX Runtime web; typically submitted using template label Feb 10, 2024
@fs-eire
Copy link
Contributor

fs-eire commented Feb 16, 2024

This error means that one ArrayBuffer is used in a detached state. I may need to take a look at your code to figure out the detail.

@nezaBacar
Copy link
Author

nezaBacar commented Feb 18, 2024

Thanks for the fast response! I've added you to the demo repository. Just pop the model into the /demo/models folder. And make sure that the line 20 in index.html points to model.onnx and that the line 41 in diffusion.js is not commented. The problem seems to be in lines 91/92 in diffusion.js script. I noticed that if i don't run the model twice the issue does not occur.

@fs-eire
Copy link
Contributor

fs-eire commented Feb 23, 2024

The root cause if figured out.

When using proxy, input tensors' underlying buffer is transferred from main thread into worker thread to avoid memcpy. This will invalidate any object in main thread who uses that buffer. This is why this error come out because the underlying array buffer in latents is transferred into proxy worker now already.

    let noisePredText = await predict_noise(latents, timestep, positive);    // this works good
    let noisePredUnet = await predict_noise(latents, timestep, negative);    // this cannot work , because 'latents' is based on invalid buffer

I can make a fix to transfer the array buffer back to main thread after using it.

@nezaBacar
Copy link
Author

Thanks for your response. Oh I understand, yes please make the fix if possible. I would appreciate it so much.

@fs-eire
Copy link
Contributor

fs-eire commented Feb 29, 2024

@nezaBacar could you help to verify if the PR fixes the issue by consuming package built here: https://dev.azure.com/onnxruntime/onnxruntime/_build/results?buildId=1306843&view=artifacts&pathAsName=false&type=publishedArtifacts

@fs-eire fs-eire closed this as completed in f06164e Mar 1, 2024
zz002 pushed a commit to zz002/onnxruntime that referenced this issue Mar 7, 2024
### Description

When using proxy worker, input buffers should be transferred back to the
caller thread after `run()` call is done.

Fixes microsoft#19488
fs-eire added a commit that referenced this issue Mar 15, 2024
### Description

When using proxy worker, input buffers should be transferred back to the
caller thread after `run()` call is done.

Fixes #19488
fs-eire added a commit that referenced this issue Mar 15, 2024
### Description

When using proxy worker, input buffers should be transferred back to the
caller thread after `run()` call is done.

Fixes #19488
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this issue May 9, 2024
### Description

When using proxy worker, input buffers should be transferred back to the
caller thread after `run()` call is done.

Fixes microsoft#19488
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

2 participants