-
Notifications
You must be signed in to change notification settings - Fork 901
Return buffer instead of file_path if cache unavailable for model loading #1280
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
Conversation
@PrafulB I made some minor modifications to also allow for custom cache mechanisms to work with import * as transformers from '@huggingface/transformers';
async function run() {
const pipe = await transformers.pipeline(
"sentiment-analysis",
"Xenova/distilbert-base-uncased-finetuned-sst-2-english",
{ dtype: "fp32" }
);
const result = await pipe("I love transformers.js!");
console.log(result);
}
// Default
await run();
// No file cache
transformers.env.useFSCache = false;
await run();
transformers.env.useFSCache = true;
// Custom cache that does nothing
class CustomCache {
async put(key, value) {
/* Simulate adding nothing to the cache (e.g., no more storage available) */
}
async match(key) {
/* Simulate a cache miss */
}
}
transformers.env.useCustomCache = true;
transformers.env.customCache = new CustomCache();
await run(); I'm hoping to include this fix in the next version (v3.5), so if possible, can you confirm these changes meet your requirements still? Thanks! |
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
The changes look good to me. If I could make one final suggestion though? In the case where both |
Created PR #1285 to track this and keep it separate from this one. |
Great point! 🧠 Thanks! |
This PR resolves #1249 and #1279 . When running in a Node.js environment, there's an edge case where the unavailability of a caching system causes an error when loading model files. The problematic code is here.. Can be verified by simply setting both
env.useFSCache
andenv.useCustomCache
to false.Specifically, in a Node.js environment,
getModelFile()
attempts to download the respective model.onnx (or equivalent) file, store it in a cache and return the path to it. In cases where caching is disabled or unavailable and a remote model is to be loaded,cache
stays undefined and thecache.match()
call throws an error. Can be verified by simply setting bothenv.useFSCache
andenv.useCustomCache
to false.This PR makes it so that the file_path is only requested if the environment is Node.js AND either of the two caching options are available. Otherwise, the buffer containing the model file is returned as is.