Skip to content

Conversation

@xenova
Copy link
Collaborator

@xenova xenova commented Jul 31, 2025

This is the official, long-awaited PR that introduces Transformers.js V4.

See benchmarks

https://huggingface.co/onnx-community/all-MiniLM-L6-v2-ONNX:

image

https://huggingface.co/onnx-community/bge-base-en-v1.5-ONNX:

image

Other issues:

xenova and others added 30 commits December 23, 2024 14:10
* ONNX Runtime improvements (experimental native webgpu; fix iOS) (#1231)

* customize the wasm paths

* update implementation

* allow using 'webgpu' in nodejs binding

* update version of onnxruntime-node

* Upgrade onnxruntime-web to same version as onnxruntime-node

* Update list of supported devices

---------

Co-authored-by: Joshua Lochner <[email protected]>

* customize the wasm paths (#1250)

* customize the wasm paths

* update implementation

* [internal] Add is_decoder option to session retrieval for preferred output location

* Update tests

* Formatting

* Bump ort versions

* Bump onnxruntime-node version

* Bump versions

* Bump ORT versions

* Bump versions

* Only check webgpu fp16 for non-node environments

* Fix

* Assume node supports webgpu

* Update ORT node support comment

* Relax test strictness

* Update conversion script versions

* Downgrade onnxslim

* cleanup

* Update package-lock.json

* Update onnxruntime versions

* Update post-build script

* Use built-in session release function

* Call garbage collection after each tokenizer test

* Do not double-throw error

* Fix race-condition in build process with file removal

* Update versions

* Bump jinja version

* [version] Update to 3.6.3

* Bump jinja version to support new features

* [version] Update to 3.6.3

* Add support for LFM2 models (#1367)

* Use prefix in lfm2 output location (#1369)

* Update package-lock.json

* Run `npm audit fix`

* Add special tokens in text-generation pipeline if tokenizer requires (#1370)

* Add special tokens in text-generation pipeline if tokenizer requires

* Fix logits processors tests

* Update bundles.test.js

* Update comment

* Formatting

* Add support for ModernBERT Decoder (#1371)

* Use from/to buffer instead of string

Actually fixes #1343

* Add support for Voxtral (#1373)

* Support longform voxtral processing (#1375)

* [version] Update to 3.7.0

* Add support for Arcee (#1377)

* Optimize tensor.slice() (#1381)

* Optimize tensor.slice()

The performance of executing `tensor.slice()` is super poor, especially for
the 'logits' tensor with large dimensions.

```
const logits = outputs.logits.slice(null, -1, null);`
```

This is because currently implementation of the `slice` method manually iterates
through each element and calculate indices which is a big time consuming if
the tensor shape is large.

For cases like `slice(null, -1, null)`, where the slicing operation is
contiguous along certain dimensions, which can be optimized by bulk copy
by using `TypeArray.subarray()` and `TypeArray.set()`.

* nit

* Add a few more tensor slice unit tests

---------

Co-authored-by: Joshua Lochner <[email protected]>

---------

Co-authored-by: Yulong Wang <[email protected]>
Co-authored-by: Wanming Lin <[email protected]>
xenova and others added 6 commits December 7, 2025 23:44
* suppress console.error while creating InferenceSession

* changed console suppress if not one of the misleading errors

* set default logSeverityLevel and also match the ONNX_WEB.env.logLevel

* indentation

* small fix

* some clean-up

* Apply suggestions from code review

Co-authored-by: Joshua Lochner <[email protected]>

* added LOG_LEVELS to the top of the file

---------

Co-authored-by: Joshua Lochner <[email protected]>
#1471)

* added wasm cache

* some refactoring of the hub.js and caching of the wasm factory

* fixed comment

* added string as cache return

* fixes after review

* Only return if match is found

* Return response even if cache doesn't exist

Don't throw error if we can't open cache or load file from cache, but we are able to make the request.

---------

Co-authored-by: Joshua Lochner <[email protected]>
Co-authored-by: Joshua Lochner <[email protected]>
@xmcp
Copy link

xmcp commented Dec 17, 2025

Hi @xenova ,

The two benchmark figures in this PR show pretty impressive speed improvement. Is this v4 vs v3? Or should we do something to the onnx file to achieve that speedup?

I tried the https://huggingface.co/Xenova/bge-small-zh-v1.5 model with the v4 branch + WebGPU FP16, but did not observe notable performance improvements over v3 at any batch size. So I am curious if I missed some steps. E.g., should I run the convert.py on the base BAAI/bge-small-zh-v1.5 model to make a new "optimized" version for that?

nico-martin and others added 4 commits December 21, 2025 17:57
* added blob check before cahing wasm or mjs file

* added propper handling for absolute/relative URLs

* clean up

* removed unneeded url check

* use isValidUrl
* added esuild

* fixed stream and stream/promises import

* changes after review

* Delete webpack.config.js

* Bump esbuild version

---------

Co-authored-by: Joshua Lochner <[email protected]>
* started refactoring

* started refactoring

* started refactoring

* added model class files

* added model class files

* added model class files

* all model classes in their own files

* refactored PreTrainedModel

* refactoring done, lets fix bugs

* added model-registry

* removed dev file

* changed casing

* refactored MODEL_TYPE_CONFIG

* fixed tests

* small refactoring

* moved model loader to its own file

* fixed ts errors

* big structure refactoring

* fixed build

* renamed _base/pre-trained-model.js and _base/output.js

* small casing changes

* Update src/models/ernie4_5/modeling_ernie4_5.js

Co-authored-by: Joshua Lochner <[email protected]>

* refactored models/utils.js

* fixed double MODEL_FOR_ definitions with registerTaskMappings helper

* auto/image_processing_auto.js export

* auto/image_processing_auto.js export

* Improve model mapping setup

* Fix LlavaPreTrainedModel

* Move llava_onevision to separate files

* Add missing exports

* Update jinja version

* Fix default class mapping

* Simplify registerTaskMappings

* Update registry.js

* Formatting in src/models

* Formatting in src

* Move model-specific ModelOutput to respective modeling files

* Final cleanup

* Cleanup model exports

* Fix Tensor type re-export

* Clean up registry exports

* Cleanup

* Simplify loadResourceFile

* Use positional arguments for repo id and filename

* Update global library exports

* Remove ts-expect-error

* Formatting

* let -> const

---------

Co-authored-by: Joshua Lochner <[email protected]>
Co-authored-by: Joshua Lochner <[email protected]>
@xenova
Copy link
Collaborator Author

xenova commented Jan 14, 2026

Hi @xmcp 👋 I've made an optimized export for that model at https://huggingface.co/onnx-community/bge-small-zh-v1.5-ONNX, which -- if you use and run on webgpu -- you should see performance gains for!

@nico-martin nico-martin mentioned this pull request Jan 20, 2026
xenova and others added 4 commits January 21, 2026 12:10
* Remove legacy tokenizer tests

* Update utils.test.js

* Update unit tests

* Refactor tokenizers.js

* Update streamers.js

* Update imports

* Initial tokenization migration

* Remove unnecessary index.js files

* Do not export PretrainedMixin

* Refactor models.js/tokenizers.js

* Update imports

* Update more imports

* Update folder structure and imports

* Delete tokenization_code_xlm_roberta.js

* Fix import path

* Fix typos

* Update import path
* Remove legacy tokenizer tests

* Update utils.test.js

* Update unit tests

* Refactor tokenizers.js

* Update streamers.js

* Update imports

* Initial tokenization migration

* Remove unnecessary index.js files

* Do not export PretrainedMixin

* Refactor models.js/tokenizers.js

* Update imports

* Update more imports

* Update folder structure and imports

* Delete tokenization_code_xlm_roberta.js

* Add support for FalconH1
* Setup test case

* Update audio-classification.js

* Update text-to-audio pipeline: implementation and types

* Implement tensor repeat and tile operations

* Optimize randn implementation

* Update automatic-speech-recognition.js

* Update question-answering.js

* Update image-classification.js

* Update image-to-image.js

* Update text-classification.js

* Update depth-estimation.js

* Update background-removal.js

* Update image-segmentation JSDoc

* Fix DQA JSDoc

* Remove unused type

* Update ObjectDetectionPipeline types

* Update Text2TextGenerationPipeline types (and subclasses)

* Update TokenClassificationPipeline types

* Update image-to-text.js

* Remove useless constructors

* Update zero-shot pipeline types

* Update image-segmentation.js

* Create pipeline tests for type checking

* Update tsconfig.json

* Update onnx.js

* Use defined types

* Support passing speaker embeddings tensor directly
* switched to pnpm workspaces

* updated github actions

* added comments

* Update tensor.js

* Formatting

* Update tsconfig.json

* Update tsconfig.json

* fixed circular reference error in pipelines/zero-shot-audio-classification.js

* Post-tsconfig updates

* Move transformers.js docs to package folder

* Move additional tests

* JSDoc update

* Version bumps

* Update incorrect test

* Update test_modeling_musicgen.js

* Update test_modeling_musicgen.js

* Update test_modeling_musicgen.js

* fixed broken symlink

* fixes after review

* Remove old conversion scripts

Users should use onnxruntime-genai or optimum directly

* Update .prettierrc

* Formatting

* Update readme/docs

* Move build scripts to parent folder

* Remove unused tests

* Remove old compare function

* Fix JSDoc

* Update generate.js

* Update inline descriptions

* Bump versions

* Update node imports

* Add module header to FileCache.js

* JSDoc updates

* Update tensor.js

* Move prettier config to package.json key

* Update FileCache.js

* Remove unused import

* Remove non-existent file include

* Prefer non-default exports

* Update doc module exports

* Update docs generation script

* merged tsconfigs and added contributing.md

* Update path_to_docs

* Formatting

* Formatting

* Formatting

* Update prettier usage

* Remove <code> tags from headers

* Swap docs-preview and docs-build commands

* ONNXRUNTIME_NODE_INSTALL=skip for doc-builder

* Update buildAll.mjs

* Update index

---------

Co-authored-by: Joshua Lochner <[email protected]>
@xenova xenova marked this pull request as ready for review January 29, 2026 03:34
@HuggingFaceDocBuilderDev

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment