Running ONNX Runtime (Silero VAD) in Cloudflare Workers — complete solution with 0.7ms inference #12616
CosteGieF
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Running onnxruntime-web + Silero VAD in workerd
After a lot of digging, I got onnxruntime-web v1.24.1 + Silero VAD v5 running natively in a Cloudflare Worker. Avg inference time: 0.7ms. Sharing the full solution here since there was no prior art on this.
Why it fails out of the box
Three things block
ortin workerd:WebAssembly.compile()is blocked — workerd does not allow dynamic WASM compilation at runtimeimport.meta.urlis empty — ort uses it to resolve the.wasmfile path and throwsimport(variable)is rejected — workerd rejects non-string dynamic imports at module analysis timeThe solution — 3 patches at build time
Patch 1 — Pre-compile WASM via
wrangler.tomlrulesThis gives you a
WebAssembly.Module(already compiled at deploy time). Inject it viainstantiateWasmbefore ort initializes:Patch 2 — Silence the URL resolution error
Patch 3 — Kill dynamic
import(variable)in the bundled ort sourceReplace all
await import(nonStringVariable)patterns withPromise.reject(new Error('dynamic import not supported'))using a regex patch in your build script.Benchmark results
Resources
Hope this saves someone the debugging time!
Beta Was this translation helpful? Give feedback.
All reactions