Skip to content

gguf: Add ability to load local file #656

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

Merged
merged 10 commits into from
May 10, 2024

Conversation

ngxson
Copy link
Member

@ngxson ngxson commented May 5, 2024

Being able to load a local gguf file can be useful when we want to debug a gguf file.

Without this PR, this ability could be done by using file-fetch. However, that won't work with big models, since the whole file is loaded into RAM.

This PR add a new RangeViewLocalFile internal class that extends RangeView. It redirects calls to fetchChunk() to fs.createReadStream with the appropriate byte range. This allows the library to read specific chunk from a local file.

For security reason, this ability is locked under localFile: boolean param. By default, it is disabled (i.e. when this library is run on hub backend, this param is disabled if unspecified)

  • Add test case to gguf.spec.ts
  • Being able to build with target=browser (only build, but will throw error on browser if being used)

@ngxson ngxson requested review from mishig25 and julien-c as code owners May 5, 2024 14:37
@coyotte508
Copy link
Member

coyotte508 commented May 6, 2024

Note that we already have FileBlob, that you could reuse: https://github.com/huggingface/huggingface.js/blob/4fdb7cb8fdcbcde3210150e684f3e31d17cd5bab/packages/hub/src/utils/FileBlob.ts

const blob  = await Fileblob.create("./local-file");

const chunk1 = await blob.slice(start, end).then(slice => slice.arrayBuffer());
const chunk2 = ...

There are also WebBlob and even a more generic createBlob that takes any URL - that are used by @huggingface/hub internally.

It could be copied over or shared. Or a small improvement: no need for streams to get a chunk you can do it like this:

const file = await open(this.path, "r");

try {
	const chunk = await file.read(buffer, 0, end-start, start);
} finally {
	await file.close();
}

And soon with typescript 5.5 beta:

await using file = await open(this.path, "r");

const chunk = await file.read(buffer, 0, end-start, start);

anyway, just passing notes, will leave it up to you folks :)

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.

i'm not opposed to this (an alternative would be to document how to simply start a local webserver from where your files are)

@ngxson
Copy link
Member Author

ngxson commented May 6, 2024

Thanks @coyotte508 for the suggestion. I ended up copying FileBlob into the gguf library, because:

  • Moving it to shared ==> I'm not sure how it will effect FileBlob.spec.ts
  • Via adding "@huggingface/hub": "workspace:^" to gguf/package.json ==> It doesn't compile, because FileBlob.ts is not exported

Otherwise, for now, it works fine on both node & browser. Feel free to tell me if you have other ideas.

@coyotte508
Copy link
Member

regarding conflicts: I think you can remove type-fest from the dev dependencies

@ngxson ngxson requested review from julien-c and mishig25 May 8, 2024 08:22
@mishig25 mishig25 merged commit ab84639 into huggingface:main May 10, 2024
mishig25 pushed a commit that referenced this pull request May 10, 2024
@mishig25
Copy link
Collaborator

@ngxson merged it 🚀

Would you mind adding a section on how to load locally in the readme https://github.com/huggingface/huggingface.js/blob/main/packages/gguf/README.md#usage ?

Thanks a lot !

@ngxson
Copy link
Member Author

ngxson commented May 10, 2024

@mishig25 Sure, thanks for reminding, I totally forgot that.

@ngxson ngxson mentioned this pull request May 12, 2024
mishig25 pushed a commit that referenced this pull request May 13, 2024
Follow up #655 and
#656 (comment)

Added some examples on how to use local file + strictly typed

---------

Co-authored-by: Julien Chaumond <[email protected]>
Co-authored-by: Mishig <[email protected]>
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.

4 participants