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] chrome V3 extension TypeError: URL.createObjectURL is not a function #14445

Closed
jonathanpeppers opened this issue Jan 26, 2023 · 17 comments
Closed
Labels
platform:web issues related to ONNX Runtime web; typically submitted using template

Comments

@jonathanpeppers
Copy link
Member

jonathanpeppers commented Jan 26, 2023

Describe the issue

We are using onnxruntime-web in a Chrome extension.

We are in the process of migrating to Chrome V3 format (worked great in V2) and hit the error:

TypeError: URL.createObjectURL is not a function
    at Object.locateFile (packed.js:formatted:18165:126)
    at O (packed.js:formatted:345:61)
    at Object.ib (packed.js:formatted:718:49)
    at Object.mb (packed.js:formatted:722:73)
    at fe (packed.js:formatted:594:48)
    at Te (packed.js:formatted:807:42)
    at ort-wasm-simd-threaded.wasm:0x772d56
    at ort-wasm-simd-threaded.wasm:0xeb64e
    at ort-wasm-simd-threaded.wasm:0x1c4048
    at ort-wasm-simd-threaded.wasm:0xe5575

URL.createObjectURL does not appear to be available to Chrome V3 extensions.

Is there a different way for us to use onnxruntime and avoid this API? Thanks!

To reproduce

You could install our extension: release.zip

Editing a multi-line text box (like GitHub) hits the error, and you can view from the extensions page:

image

Source code here: jonathanpeppers/inclusive-code-reviews-browser#90

Urgency

Medium. Chrome V2 extensions are being phased out this year, and this seems to be a blocker.

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.13.1

Execution Provider

WASM

@jonathanpeppers jonathanpeppers added the platform:web issues related to ONNX Runtime web; typically submitted using template label Jan 26, 2023
@fs-eire
Copy link
Contributor

fs-eire commented Jan 27, 2023

thank you for the feedback. it looks like URL.createObjectURL() cannot be used in a service worker. we are trying to find a way to fix this.

jonathanpeppers added a commit to jonathanpeppers/inclusive-code-reviews-browser that referenced this issue Jan 27, 2023
Context: microsoft/onnxruntime#14445

When moving to Chrome V3, we hit the error:

    TypeError: URL.createObjectURL is not a function
        at Object.locateFile (packed.js:formatted:18165:126)
        at O (packed.js:formatted:345:61)
        at Object.ib (packed.js:formatted:718:49)
        at Object.mb (packed.js:formatted:722:73)
        at fe (packed.js:formatted:594:48)
        at Te (packed.js:formatted:807:42)
        at ort-wasm-simd-threaded.wasm:0x772d56
        at ort-wasm-simd-threaded.wasm:0xeb64e
        at ort-wasm-simd-threaded.wasm:0x1c4048
        at ort-wasm-simd-threaded.wasm:0xe5575

It appears we can set `numThreads` to 1 to avoid this code path.

There appear to be different `.wasm` files:

    Uncompressed Compressed
    8667133      2411351  ort-wasm-simd-threaded.wasm
    8719890      2413744  ort-wasm-simd.wasm
    7908412      2248465  ort-wasm-threaded.wasm
    7960955      2252902  ort-wasm.wasm

And it looks like we can drop the two `*-threaded.wasm` files.

Size difference:

    --14513792 release.zip
    ++ 9853726 release.zip

The extension seems to work as before, still on Chrome V2 in main.
@jonathanpeppers
Copy link
Member Author

Thanks! It appears I can workaround with ort.env.wasm.numThreads = 1;, but I'm not sure exactly what the setting does:

jonathanpeppers/inclusive-code-reviews-browser#195

Is there a doc/readme that explains what the 4 different *.wasm files do?

@guschmue
Copy link
Contributor

guschmue commented Feb 2, 2023

One thing is that you might want to use chrome.runtime.getURL("asserts/model.onnx") to fetch the model.
That has been working well for me.
I can't explain why ort.env.wasm.numThreads=1 helps, need to debug it a little.

@fs-eire
Copy link
Contributor

fs-eire commented Feb 2, 2023

The error does not happen in loading the model, it happens when initializing the web assembly.

The reason why ort.env.wasm.numThreads=1 works is because the function URL.createObjectURL() will only be called when initializing multi-threaded version web assembly. The function is used to create a blob for 'ort-wasm-threaded.worker.js'. So setting to load the single-threaded version is a workaround.

https://github.com/microsoft/onnxruntime/blob/main/js/web/lib/wasm/wasm-factory.ts#L124-L128

@jonathanpeppers
Copy link
Member Author

@fs-eire do you know any drawbacks to the single-threaded version?

We're not trying to call our model multiple times in parallel, really just sending 1 request at a time.

@guschmue
Copy link
Contributor

guschmue commented Feb 3, 2023

@fs-eire - got it.
It is this code here: https://github.com/microsoft/onnxruntime/blob/main/js/web/lib/wasm/wasm-factory.ts#L124.
Aand it sounds you cannot use URL.createObjectURL with manifest v3 (ie
https://groups.google.com/a/chromium.org/g/chromium-extensions/c/u0NH7L3v9L4).
If I understand the code correctly, we do this to avoid distributing the js files for simd.
We might want to find a different way of doing this.

@jonathanpeppers, the disadvantage for single threaded is only performance. Your model looked not very compute intensive so you might not notice.

jonathanpeppers added a commit to jonathanpeppers/inclusive-code-reviews-browser that referenced this issue Feb 3, 2023
Context: microsoft/onnxruntime#14445

When moving to Chrome V3, we hit the error:

    TypeError: URL.createObjectURL is not a function
        at Object.locateFile (packed.js:formatted:18165:126)
        at O (packed.js:formatted:345:61)
        at Object.ib (packed.js:formatted:718:49)
        at Object.mb (packed.js:formatted:722:73)
        at fe (packed.js:formatted:594:48)
        at Te (packed.js:formatted:807:42)
        at ort-wasm-simd-threaded.wasm:0x772d56
        at ort-wasm-simd-threaded.wasm:0xeb64e
        at ort-wasm-simd-threaded.wasm:0x1c4048
        at ort-wasm-simd-threaded.wasm:0xe5575

It appears we can set `numThreads` to 1 to avoid this code path.

There appear to be different `.wasm` files:

    Uncompressed Compressed
    8667133      2411351  ort-wasm-simd-threaded.wasm
    8719890      2413744  ort-wasm-simd.wasm
    7908412      2248465  ort-wasm-threaded.wasm
    7960955      2252902  ort-wasm.wasm

And it looks like we can drop the two `*-threaded.wasm` files.

Size difference:

    --14513792 release.zip
    ++ 9853726 release.zip

The extension seems to work as before, still on Chrome V2 in main.
@lizozom
Copy link

lizozom commented Jul 7, 2023

I got this error, specifically working with @xenova/transformers in a chrome extension.
I had this code throwing the same error:

import { env, pipeline } from '@xenova/transformers';
env.localModelPath = 'models/';
env.allowRemoteModels = false;
this.embedder = await pipeline("embeddings", EMBEDDING_MODEL_NAME);

I added env.backends.onnx.wasm.numThreads = 1; and it resolved the error.

@lizozom
Copy link

lizozom commented Jul 9, 2023

However, running single threaded slows everything down. I'd love to work on this if anyone points me in the right direction :-)

@xenova
Copy link

xenova commented Jul 14, 2023

@fs-eire @guschmue any updates on this? 😇 I've been working on improving my chrome extension demo for transformers.js, and being able to get this working would be great!

@guschmue
Copy link
Contributor

I can look at it - might be a little complicated to fix.

@MentalGear
Copy link

An update to allow multi-threading would be really beneficial !

@ianmarmour
Copy link

Would love to get an update on this. At the moment the the node library is pretty much unusable for me due to lacking support for onnxruntime-extensions (probably should be a separate bug report). But with this fixed we can just use rely on the web library for both web and node usage (we can now already but it would be great if threading worked properly)

@MentalGear
Copy link

Indeed this is blocking the efficient usage of the very popular transformers.js in the browser for local and private ML. @fs-eire @guschmue

@Dolidodzik
Copy link

I would love to see the update as well. I wanted to work with transformers.js to run LLMs in the browser but this issue makes it very inefficient due to lack of multithreading support.

@BrennanBarker
Copy link

@fs-eire @guschmue is there anything wrong in principle with converting the URL.createObjectURL calls to a simple Data URL?

@sebastianbenz
Copy link

Have you tried running onnxruntime-web inside an offscreen document? These are meant to supplement extension service workers in case additional APIs are required that are not available in a service worker.

@guschmue
Copy link
Contributor

this PR should fix this:
#20165

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

No branches or pull requests

10 participants