tantaraio / voy

🕸️🦀 A WASM vector similarity search written in Rust
https://www.npmjs.com/package/voy-search
Apache License 2.0
881 stars 31 forks source link

Consider releasing a version that does not require a bundler to the npm registry #6

Open GarciaLnk opened 1 year ago

GarciaLnk commented 1 year ago

Right now, it's impossible to use the library directly from npm without using a bundler like Webpack, consider releasing a build using the --target web flag to allow using it without a bundler, or with bundlers that don't properly support WASM yet (like Parcel v2).

DawChihLiou commented 1 year ago

Hey @GarciaLnk, thanks for your suggestion! I'll explore the options. At the meantime, you can directly import the .wasm file to start using it without a bundler. For example:

// cjs
const voy = require("voy-search/voy_search_bg.wasm");

// or esm
const voy = await import("voy-search/voy_search_bg.wasm");
GarciaLnk commented 1 year ago

AFAIK to import a WASM module as a native ES module you need a bundler that has proper support for it, which is the root of the issue (see: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers).

wonderwhy-er commented 1 year ago

Wanted to test it quickly by loading from CDN trough unpkg https://www.unpkg.com/browse/voy-search@0.1.3/

So I could add script tag to a html page like this: <script src="https://www.unpkg.com/voy-search@0.1.3/voy_search.js" type="module"></script>

That results in an error: Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/wasm". Strict MIME type checking is enforced for module scripts per HTML spec.

As far as I understand direct importing of wasm modules is not supported yet

I did manage to make it work with this code

import * as voy from 'https://www.unpkg.com/voy-search@0.1.3/voy_search_bg.js';
WebAssembly.instantiateStreaming(fetch("https://www.unpkg.com/voy-search@0.1.3/voy_search_bg.wasm"), {
    "./voy_search_bg.js": voy,
    },
}).then((module) => {
    voy.__wbg_set_wasm(module.instance.exports);
});

Sadly later trying to load my embeddings got an err:

image
DawChihLiou commented 1 year ago

@wonderwhy-er thanks so much for reporting. Could you attach your embeddings or show me how did you generate them so I can reproduce the error? Thanks!

wonderwhy-er commented 1 year ago

Ouh its my bad, it seems to work. I was using TensorFlow JS universal sentence encoder https://github.com/tensorflow/tfjs-models/blob/master/universal-sentence-encoder/README.md

And for whatever reasons in some cases it returns [number[]] instead of number[] for encoding I did Array.flat() on it and then it all worked!

DawChihLiou commented 1 year ago

Amazing! Have fun:)

MentalGear commented 1 year ago

Hey @wonderwhy-er I'm also looking to get voy work in the browser, potentially with up to 1GB or records. Would you be able to share your wasm solution?

wonderwhy-er commented 1 year ago

@MentalGear sorry missed your question, I since then switched to using other things My code was kinda like this https://jsfiddle.net/wonderwhy_er/k4bwLf2e/31/ But it does not work in jsfiddle and I do not have original code I had working anymore