Subchapter 25.5
references/MODEL_REGISTRY.mdMarkdown6 KBView on GitHub
In Transformers.js v4, ModelRegistry provides a preflight API for model assets. You can inspect required files, estimate total download size, check cache state, and clear cached artifacts before calling pipeline().
This is useful for production UX where you want to:
import { ModelRegistry } from '@huggingface/transformers';ModelRegistry works with the same task/model/options you pass to pipeline().
Typical tuple:
const task = 'feature-extraction';
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
const modelOptions = { dtype: 'fp32' };Returns all files needed to initialize that pipeline configuration.
const files = await ModelRegistry.get_pipeline_files(task, modelId, modelOptions);
// Example: ['config.json', 'onnx/model.onnx', 'tokenizer.json', ...]Use this to build preflight checks and download manifests.
Returns metadata for a single file (including size when available).
const metadata = await ModelRegistry.get_file_metadata(modelId, 'onnx/model.onnx');
console.log(metadata);Use this to compute total transfer size and identify large artifacts.
Checks whether required files are already available in cache.
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
console.log(cached ? 'Ready offline' : 'Needs download');Use this to gate offline mode and skip unnecessary preload steps.
Clears cached assets for a specific pipeline tuple.
await ModelRegistry.clear_pipeline_cache(task, modelId, modelOptions);Use this for cache invalidation, testing, or space reclamation.
Returns precision/quantization formats available for the model.
const dtypes = await ModelRegistry.get_available_dtypes(modelId);
// Example: ['fp32', 'fp16', 'q4', 'q4f16']Use this to choose the best runtime profile (quality vs. speed vs. memory).
For robust loading UX:
get_pipeline_files(...).is_pipeline_cached(...).pipeline(...) and use progress_total in progress_callback.import { ModelRegistry, pipeline } from '@huggingface/transformers';
const task = 'feature-extraction';
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
const modelOptions = { dtype: 'q8' };
const files = await ModelRegistry.get_pipeline_files(task, modelId, modelOptions);
const metadata = await Promise.all(
files.map((file) => ModelRegistry.get_file_metadata(modelId, file))
);
const totalBytes = metadata.reduce((sum, item) => sum + (item?.size ?? 0), 0);
const totalMB = (totalBytes / 1024 / 1024).toFixed(2);
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
console.log({ fileCount: files.length, totalMB, cached });
const pipe = await pipeline(task, modelId, {
...modelOptions,
progress_callback: (info) => {
if (info.status === 'progress_total') {
console.log(`Loading: ${info.progress.toFixed(1)}%`);
}
},
});
await pipe.dispose();import { ModelRegistry, pipeline } from '@huggingface/transformers';
const task = 'text-generation';
const modelId = 'onnx-community/Qwen2.5-0.5B-Instruct';
const dtypes = await ModelRegistry.get_available_dtypes(modelId);
const preferred = dtypes.includes('q4') ? 'q4' : dtypes[0] ?? 'fp32';
const generator = await pipeline(task, modelId, { dtype: preferred });
// ... inference
await generator.dispose();import { ModelRegistry } from '@huggingface/transformers';
await ModelRegistry.clear_pipeline_cache(
'feature-extraction',
'onnx-community/all-MiniLM-L6-v2-ONNX',
{ dtype: 'fp32' }
);This avoids wiping unrelated model caches.
import { ModelRegistry, env, pipeline } from '@huggingface/transformers';
const task = 'feature-extraction';
const modelId = 'onnx-community/all-MiniLM-L6-v2-ONNX';
const modelOptions = { dtype: 'q8' };
const cached = await ModelRegistry.is_pipeline_cached(task, modelId, modelOptions);
if (!cached) {
throw new Error('Model not cached yet. Connect once to download assets.');
}
env.allowRemoteModels = false;
const pipe = await pipeline(task, modelId, { ...modelOptions, local_files_only: true });ModelRegistry before pipeline() when you need predictable download UX.progress_total for user-facing progress bars; keep per-file progress optional.clear_pipeline_cache(...) over broad cache deletion.is_pipeline_cached(...) with local_files_only: true and env.allowRemoteModels = false.pipeline() options and progress callbackenv settings for local/remote loading