ホーム 暮らし Overview of Transformers.js v3

Overview of Transformers.js v3

真鍋 千夏 真鍋 千夏 • 更新: • 公開: • 読了約 18 分
目次

I found the following article interesting, so I have summarized it briefly.

1. Transformers.js v3

n8f0d4edd0884

"Transformers.js v3" is the latest version of the JavaScript edition of the machine learning library "Transformers" developed by HuggingFace.

The main features are as follows:

2. Installation

(1) Install the package.

npm i @huggingface/transformers

(2) Import the package.

import  pipeline  from "@huggingface/transformers";

3. WebGPU Support

3-1. WebGPU

"WebGPU" is a new web standard for high-speed graphics and computing. This API allows web developers to use the underlying system's GPU to perform high-performance computing directly within the browser. "WebGPU" is the successor to "WebGL", and because it can interact more directly with modern GPUs, performance is significantly improved.

3-2. Usage in Transformers.js v3

"ONNX Runtime Web" integration makes enabling WebGPU acceleration as simple as setting device: 'webgpu' when loading a model.

・Calculate text embeddings on WebGPU (Demo)

import  pipeline  from "@huggingface/transformers";

// パイプラインの準備
const extractor = await pipeline(
  "feature-extraction",
  "mixedbread-ai/mxbai-embed-xsmall-v1",
   device: "webgpu" ,
);

// 埋め込みの計算
const texts = ["Hello world!", "This is an example sentence."];
const embeddings = await extractor(texts,  pooling: "mean", normalize: true );
console.log(embeddings.tolist());
// [
//   [-0.016986183822155, 0.03228696808218956, -0.0013630966423079371, ... ],
//   [0.09050482511520386, 0.07207386940717697, 0.05762749910354614, ... ],
// ]

・Automatic speech recognition using OpenAI Whisper on WebGPU (Demo)

import  pipeline  from "@huggingface/transformers";

// パイプラインの準備
const transcriber = await pipeline(
  "automatic-speech-recognition",
  "onnx-community/whisper-tiny.en",
   device: "webgpu" ,
);

// URLから音声を書き起こす
const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav";
const output = await transcriber(url);
console.log(output);
//  text: ' And so my fellow Americans ask not what your country can do for you, ask what you can do for your country.' 

・Image classification using MobileNetV4 on WebGPU (Demo)

import  pipeline  from "@huggingface/transformers";

// パイプラインの準備
const classifier = await pipeline(
  "image-classification",
  "onnx-community/mobilenetv4_conv_small.e2400_r224_in1k",
   device: "webgpu" ,
);

// URLから画像を分類
const url = "https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg";
const output = await classifier(url);
console.log(output);
// [
//    label: 'tiger, Panthera tigris', score: 0.6149784922599792 ,
//    label: 'tiger cat', score: 0.30281734466552734 ,
//    label: 'tabby, tabby cat', score: 0.0019135422771796584 ,
//    label: 'lynx, catamount', score: 0.0012161266058683395 ,
//    label: 'Egyptian cat', score: 0.0011465961579233408 
// ]

4. New Quantization Format (dtype)

4-1. dtype

Before "Transformers.js v3", you used the quantized option, setting it to true or false to specify whether to use a quantized (q8) or full-precision (fp32) version of a model. Now, the ability to choose from a much larger list has been added using the dtype parameter.

The list of available quantizations depends on the model, but common ones include full-precision ("fp32"), half-precision ("fp16"), 8-bit ("q8", "int8", "uint8"), and 4-bit ("q4", "bnb4", "q4f16").

4-2. Basic Usage

・4-bit quantization of Qwen2.5-0.5B-Instruct (Demo)

import  pipeline  from "@huggingface/transformers";

// パイプラインの準備
const generator = await pipeline(
  "text-generation",
  "onnx-community/Qwen2.5-0.5B-Instruct",
   dtype: "q4", device: "webgpu" ,
);

// メッセージのリストの定義
const messages = [
   role: "system", content: "You are a helpful assistant." ,
   role: "user", content: "Tell me a funny joke." ,
];

// 応答を生成
const output = await generator(messages,  max_new_tokens: 128 );
console.log(output[0].generated_text.at(-1).content);
4-3. Per-module dtype

Some encoder/decoder models, such as "Whisper" and "Florence-2", are very sensitive to quantization settings (especially the encoder). For this reason, we have added the ability to select a dtype for each module by providing a mapping from module name to dtype.

・Florence-2 on WebGPU (Demo)

import  Florence2ForConditionalGeneration  from "@huggingface/transformers";

const model = await Florence2ForConditionalGeneration.from_pretrained(
  "onnx-community/Florence-2-base-ft",
  
    dtype: 
      embed_tokens: "fp16",
      vision_encoder: "fp16",
      encoder_model: "q4",
      decoder_model_merged: "q4",
    ,
    device: "webgpu",
  ,
);

5. Expansion of Supported Architectures

In this release, the total number of supported architectures has increased to 120 (see the full list), covering a wide range of input formats and tasks. Notable new architectures include "Phi-3", "Gemma & Gemma 2", "LLaVa", "Moondream", "Florence-2", "MusicGen", "Sapiens", "Depth Pro", "PyAnnote", and "RT-DETR".

6. Sample Projects and Templates

As part of the release, 25 new sample projects and templates have been published, focusing primarily on the introduction of WebGPU support. These include demos such as Phi-3.5 WebGPU and Whisper WebGPU.

7. Addition of Pre-trained Models

As of today's release, the community has converted over 1,200 models to be compatible with Transformers.js. A complete list of available models can be found here.

If you would like to convert your own models or fine-tune them, you can use the conversion script as follows.

python -m scripts.convert --quantize --model_id <model_name_or_path>

After uploading the generated files to the HuggingFace Hub, don't forget to add tags so that other users can easily find and use your model with transformers.js.

8. Multi-platform Support

Transformers.js v3 is now compatible with the three most popular server-side JavaScript runtimes: Node.js (ESM + CJS), Deno, and Bun.

8-1. Node.js

A widely used JavaScript runtime built on Chrome's V8. It has a massive ecosystem and supports a wide range of libraries and frameworks.

8-2. Deno

A modern runtime for JavaScript and TypeScript that is secure by default. It uses ES modules and also features experimental WebGPU support.

8-3. Bun

A fast JavaScript runtime optimized for performance. It includes a built-in bundler, transpiler, and package manager.



同じカテゴリの記事

真鍋 千夏
デジタルマーケター 真鍋 千夏
プロフィールを見る →

美容と暮らしの話題を担当。メーカーの公式発表、商品成分の公表資料、専門家の監修記事をもとに事実を整理します。

おすすめの関連記事