Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions resources/litert-js/src/download-models.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,32 @@ const MODELS_TO_DOWNLOAD = [
}
];

const CACHE_FILE = path.join(MODEL_DIR, 'models_cache.json');

function getDownloadUrl(repo, filename, branch) {
return `${HUGGINGFACE_RESOLVE_URL}/${repo}/resolve/${branch}/${filename}`;
}

async function downloadModels() {
const forceDownload = process.argv.includes('--force');

if (!fs.existsSync(MODEL_DIR)) {
console.log(`Creating directory: **${MODEL_DIR}**`);
fs.mkdirSync(MODEL_DIR, { recursive: true });
}

if (!forceDownload && fs.existsSync(CACHE_FILE)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This PR is a great improvement to the benchmark, thanks Brendan!

However, this looks like all-or-nothing situation now. Meaning, if just one of the models has changed, all the models will be downloaded again. I think it is better if we check the model changes and skip downloading them one by one. Something like:

for (const modelInfo of MODELS_TO_DOWNLOAD) {
...
        const cachedEntry = cachedModels.find(m => m.filename === filename && m.repo === repo);
        const configMatches = cachedEntry && JSON.stringify(cachedEntry) === JSON.stringify(modelInfo);
        const fileExists = fs.existsSync(outputPath);

        if (!forceDownload && configMatches && fileExists) {
            console.log(`Model **${filename}** is up to date. Skipping.`);
            newCache.push(modelInfo);
            continue;
        }
// Download if missing or changed
...        
}

(The code snippet is Gemini suggested to my change request, so take it with a grain of salt)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yeah, I was trying to do something quick for testing. It's pretty easy to do per file though, so I'll change it. I added a little helper class in this other PR. Once that lands I'll update this one do the same thing.

try {
const cacheData = fs.readFileSync(CACHE_FILE, 'utf8');
if (cacheData === JSON.stringify(MODELS_TO_DOWNLOAD, null, 2)) {
console.log('Models are already up to date. Skipping download. Use --force to override.');
return;
}
} catch (err) {
console.warn(`Warning: Could not read cache file ${CACHE_FILE}:`, err.message);
}
}

console.log(`Starting TFLite model downloads to: **${MODEL_DIR}**`);

for (const modelInfo of MODELS_TO_DOWNLOAD) {
Expand Down Expand Up @@ -72,6 +88,14 @@ async function downloadModels() {
console.error(`Model download failed for ${repo}/${filename}:`, err.message);
}
}

try {
fs.writeFileSync(CACHE_FILE, JSON.stringify(MODELS_TO_DOWNLOAD, null, 2));
console.log(`\nUpdated cache file: ${CACHE_FILE}`);
} catch (err) {
console.error(`\nError writing cache file ${CACHE_FILE}:`, err.message);
}

console.log('TFLite download process finished.');
}

Expand Down