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

Hub: Move hash-wasm into vendor (emscripten) #682

Merged
merged 15 commits into from
May 21, 2024

Conversation

ngxson
Copy link
Contributor

@ngxson ngxson commented May 17, 2024

Resolves #221

In this PR, I moved source code of hash-wasm into vendor directory (c source code), also add compile script using emscripten. As a bonus, SIMD is also enabled, which should allow better performance.

I also added sha256-wrapper.ts as a thin wrapper for the compiled wasm module.

  • createSHA256 create the wasm module, works just like before.
  • createSHA256WorkerCode this function use Function.toString() trick to copy the module code into worker. This trick is already used in one of my project, wllama, so it should work on Firefox/Chrome/Safari (but feel free to test it further)

@ngxson ngxson marked this pull request as ready for review May 17, 2024 22:47
@ngxson ngxson requested a review from coyotte508 as a code owner May 17, 2024 22:47
@coyotte508
Copy link
Member

coyotte508 commented May 18, 2024

I published the package with tag wasm: npm add @hugginface/hub@wasm

I added some E2Es with Svelte and vite-build, which work great.

But then I tried including the file directly from a CDN: https://huggingface.co/spaces/coyotte508/hub-sha256

Reference links for the tests:

That said, I'm very happy with this PR :)


export function createSHA256WorkerCode(): string {
return `
const _scriptDir = "";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coyotte508 Thanks for testing that out.

I had a look on the minified version. Turns out _scriptDir get optimized out.

I attempt to this fix in 13df340 . Would you mind to redeploy it? Thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just in case, you can do things like:

if (typeof _scriptDir !== 'undefined' && _scriptDir) {
  ...
}

This should avoid failure when the variable is optimized out

Copy link
Contributor Author

@ngxson ngxson May 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, that's not really "optimized out" (Sorry my comment above was quite misleading - please ignore it).

What really happened is:

  • emscripten set _scriptDir to import.meta.url, just to know where script is. This is useful for example when the .js and .wasm files are compiled separately, which is not our case.
  • _scriptDir is provided at global scope of the module, so WasmModule.toString() cannot capture _scriptDir. That's why inside web worker code, I "fake" the value to empty string.

When the script is minified, _scriptDir is changed to another variable, that makes the "fake" value above no longer work. What I did in 13df340 was to use sed to remove it from the emscripten script.

@ngxson
Copy link
Contributor Author

ngxson commented May 18, 2024

When loading the unbundled dist file from https://cdn.jsdelivr.net/npm/@huggingface/hub@0.15.1-wasm/dist/browser/index.mjs , I get a different sha 256

After having a deeper look, turns out I made a silly mistake: Signature of TypedArray.subarray should be (start, end), but I thought it was (offset, length), see: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/subarray

It should be fixed in the last commit.

@ngxson
Copy link
Contributor Author

ngxson commented May 18, 2024

Just for fun, I also do a benchmark. Seems like we're gaining 20% performance: https://gist.github.com/ngxson/dfb4735e2f43b1b8b5d0c16113e84145

Function Time milisecs (512MB file)
hashBrowser 839.6666666666666
hashWasmOld 3146
hashWasmNew 2661.3333333333335

@coyotte508
Copy link
Member

Seems problems are fixed :)

image

Copy link
Member

@coyotte508 coyotte508 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting bundle is light as well!

image

(this is when loading the different versions of the libs)

Very nice improvement

@ngxson
Copy link
Contributor Author

ngxson commented May 18, 2024

Nice, thanks again for your help - that motivates me a lot. Feel free to merge (or to further test it) if you want. Bon weekend !

Copy link
Member

@julien-c julien-c left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice contrib

@coyotte508 coyotte508 merged commit fffe16e into huggingface:main May 21, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove dependency on hash-wasm
3 participants