-
Notifications
You must be signed in to change notification settings - Fork 1.1k
V4 worker pipeline package #1516
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
Open
nico-martin
wants to merge
8
commits into
main
Choose a base branch
from
v4-worker-pipeline-package
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f12861a
init implementation done
nico-martin 2476be9
moved tests to ts
nico-martin 6dd265d
some type improvements
nico-martin 2ed9a90
added callback bridge
nico-martin f4f3390
Update packages/transformers-webworker/tests/webWorkerPipeline.test.ts
nico-martin 7b2fe90
Update packages/transformers-webworker/src/webWorkerPipeline.ts
nico-martin 6c9ba7c
Update packages/transformers-webworker/src/utils/callback-bridge/Call…
nico-martin fa607ca
Update packages/transformers-webworker/tests/webWorkerPipeline.test.ts
nico-martin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| node_modules | ||
| dist | ||
| types | ||
| coverage | ||
| *.log | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # @huggingface/transformers-webworker | ||
|
|
||
| Web Worker utilities for Transformers.js - a lightweight package designed to be used in Web Workers. | ||
|
|
||
| ## Installation | ||
|
|
||
| You need to install both this package and the main transformers package: | ||
|
|
||
| ```bash | ||
| npm install @huggingface/transformers @huggingface/transformers-webworker | ||
| ``` | ||
|
|
||
| Or with pnpm: | ||
|
|
||
| ```bash | ||
| pnpm add @huggingface/transformers @huggingface/transformers-webworker | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| This package provides utilities for using Transformers.js pipelines in Web Workers. | ||
|
|
||
| ### In Your Main Thread | ||
|
|
||
| Use `webWorkerPipeline` to communicate with a Web Worker running a pipeline: | ||
|
|
||
| ```typescript | ||
| import { webWorkerPipeline } from '@huggingface/transformers-webworker'; | ||
|
|
||
| // Create your worker | ||
| const worker = new Worker('./worker.js'); | ||
|
|
||
| // Initialize the pipeline with options | ||
| const classifier = await webWorkerPipeline( | ||
| worker, | ||
| 'sentiment-analysis', | ||
| 'Xenova/distilbert-base-uncased-finetuned-sst-2-english', | ||
| { | ||
| // Options like progress_callback are supported via callback bridge | ||
| progress_callback: (progress) => { | ||
| console.log('Progress:', progress); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| // Use it like a regular pipeline | ||
| const result = await classifier('I love this!'); | ||
| console.log(result); | ||
| ``` | ||
|
|
||
| ### In Your Web Worker | ||
|
|
||
| Use `webWorkerPipelineHandler` to handle pipeline requests: | ||
|
|
||
| ```typescript | ||
| // worker.js | ||
| import { webWorkerPipelineHandler } from '@huggingface/transformers-webworker'; | ||
|
|
||
| const handler = webWorkerPipelineHandler(); | ||
| self.onmessage = handler.onmessage; | ||
| ``` | ||
|
|
||
| > **Note:** The handler internally uses `pipeline` from `@huggingface/transformers` to create and cache pipeline instances. | ||
|
|
||
| ## Options and Limitations | ||
|
|
||
| ### Function Callbacks | ||
|
|
||
| Function callbacks like `progress_callback` are automatically handled via a callback bridge and will execute in the main thread: | ||
|
|
||
| ```typescript | ||
| const pipe = await webWorkerPipeline(worker, 'text-generation', 'model', { | ||
| progress_callback: (progress) => { | ||
| console.log('Loading:', progress); | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| ### GPU Acceleration | ||
|
|
||
| Use the `device` parameter to enable GPU acceleration. The worker will handle GPU context creation: | ||
|
|
||
| ```typescript | ||
| // ✅ Correct: Use device parameter | ||
| await webWorkerPipeline(worker, 'text-generation', 'model', { | ||
| device: 'webgpu' // or 'webnn' | ||
| }); | ||
|
|
||
| // ❌ Incorrect: Don't pass GPU objects in session_options | ||
| await webWorkerPipeline(worker, 'text-generation', 'model', { | ||
| session_options: { | ||
| executionProviders: [{ name: 'webgpu', device: gpuDevice }] // Won't work! | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| **Note:** `session_options` cannot contain GPU devices, WebNN contexts, or typed arrays as these are not serializable across worker boundaries. | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| # Install dependencies | ||
| pnpm install | ||
|
|
||
| # Build the package | ||
| pnpm build | ||
|
|
||
| # Development mode with watch | ||
| pnpm dev | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * For a detailed explanation regarding each configuration property, visit: | ||
| * https://jestjs.io/docs/configuration | ||
| */ | ||
|
|
||
| export default { | ||
| clearMocks: true, | ||
| collectCoverage: true, | ||
| coverageDirectory: "coverage", | ||
| coveragePathIgnorePatterns: ["node_modules", "tests"], | ||
| coverageProvider: "v8", | ||
| roots: ["./tests/"], | ||
| preset: "ts-jest/presets/default-esm", | ||
| transform: { | ||
| "^.+\\.tsx?$": [ | ||
| "ts-jest", | ||
| { | ||
| useESM: true, | ||
| tsconfig: { | ||
| moduleResolution: "node", | ||
| esModuleInterop: true, | ||
| skipLibCheck: true, | ||
| isolatedModules: true, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| moduleNameMapper: { | ||
| "^(\\.{1,2}/.*)\\.js$": "$1", | ||
| }, | ||
| moduleFileExtensions: ["ts", "tsx", "js", "jsx", "mjs"], | ||
| testEnvironment: "node", | ||
| extensionsToTreatAsEsm: [".ts"], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| { | ||
| "name": "@huggingface/transformers-webworker", | ||
| "version": "0.0.1", | ||
| "description": "Web Worker utilities for Transformers.js", | ||
| "main": "./dist/index.js", | ||
| "types": "./types/src/index.d.ts", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./types/src/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "format": "prettier --write . --ignore-path ../../.prettierignore", | ||
| "format:check": "prettier --check . --ignore-path ../../.prettierignore", | ||
| "typegen": "tsc --build", | ||
| "dev": "node scripts/dev.mjs", | ||
| "build": "node scripts/build.mjs && pnpm typegen", | ||
| "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/huggingface/transformers.js.git" | ||
| }, | ||
| "keywords": [ | ||
| "transformers", | ||
| "transformers.js", | ||
| "webworker", | ||
| "huggingface", | ||
| "machine learning" | ||
| ], | ||
| "author": "Hugging Face", | ||
| "license": "Apache-2.0", | ||
| "bugs": { | ||
| "url": "https://github.com/huggingface/transformers.js/issues" | ||
| }, | ||
| "homepage": "https://github.com/huggingface/transformers.js#readme", | ||
| "peerDependencies": { | ||
| "@huggingface/transformers": "^4.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@huggingface/transformers": "workspace:*", | ||
| "@jest/globals": "^30.2.0", | ||
| "@types/jest": "^30.0.0", | ||
| "esbuild": "^0.27.2", | ||
| "jest": "^30.2.0", | ||
| "jest-environment-node": "^30.2.0", | ||
| "ts-jest": "^29.2.5", | ||
| "typescript": "5.9.3" | ||
| }, | ||
| "files": [ | ||
| "src", | ||
| "dist", | ||
| "types", | ||
| "README.md", | ||
| "LICENSE" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { OUT_DIR } from "./build/constants.mjs"; | ||
| import prepareOutDir from "../../../scripts/prepareOutDir.mjs"; | ||
| import { colors, createLogger } from "../../../scripts/logger.mjs"; | ||
| import { buildAll } from "./build/buildAll.mjs"; | ||
|
|
||
| const log = createLogger("transformers-webworker"); | ||
|
|
||
| log.section("BUILD"); | ||
| log.info("Building transformers-webworker with esbuild...\n"); | ||
|
|
||
| const startTime = performance.now(); | ||
|
|
||
| try { | ||
| prepareOutDir(OUT_DIR); | ||
|
|
||
| await buildAll(log); | ||
|
|
||
| const endTime = performance.now(); | ||
| const duration = (endTime - startTime).toFixed(2); | ||
| log.success(`All builds completed in ${colors.bright}${duration}ms${colors.reset}\n`); | ||
| } catch (error) { | ||
| log.error(`Build failed: ${error.message}`); | ||
| console.error(error); | ||
| process.exit(1); | ||
| } |
44 changes: 44 additions & 0 deletions
44
packages/transformers-webworker/scripts/build/buildAll.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { build as esbuild } from "esbuild"; | ||
| import path from "node:path"; | ||
| import { OUT_DIR, ROOT_DIR, getEsbuildProdConfig } from "./constants.mjs"; | ||
| import { reportSize } from "../../../../scripts/reportSize.mjs"; | ||
| import { colors } from "../../../../scripts/logger.mjs"; | ||
| import { BUILD_TARGETS } from "./targets.mjs"; | ||
|
|
||
| /** | ||
| * Helper function to create build configurations. | ||
| */ | ||
| async function buildTarget({ name = "", suffix = ".js", format = "esm", externalModules = [] }, log) { | ||
| const regularFile = `index${name}${suffix}`; | ||
| const minFile = `index${name}.min${suffix}`; | ||
|
|
||
| log.build(`Building ${colors.bright}${regularFile}${colors.reset}...`); | ||
| await esbuild({ | ||
| ...getEsbuildProdConfig(ROOT_DIR), | ||
| format, | ||
| outfile: path.join(OUT_DIR, regularFile), | ||
| external: externalModules, | ||
| }); | ||
| reportSize(path.join(OUT_DIR, regularFile), log); | ||
|
|
||
| log.build(`Building ${colors.bright}${minFile}${colors.reset}...`); | ||
| await esbuild({ | ||
| ...getEsbuildProdConfig(ROOT_DIR), | ||
| format, | ||
| outfile: path.join(OUT_DIR, minFile), | ||
| minify: true, | ||
| external: externalModules, | ||
| legalComments: "none", | ||
| }); | ||
| reportSize(path.join(OUT_DIR, minFile), log); | ||
| } | ||
|
|
||
| /** | ||
| * Build all targets for production | ||
| */ | ||
| export async function buildAll(log) { | ||
| for (const target of BUILD_TARGETS) { | ||
| log.section(target.name); | ||
| await buildTarget(target.config, log); | ||
| } | ||
| } |
44 changes: 44 additions & 0 deletions
44
packages/transformers-webworker/scripts/build/buildAllWithWatch.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { context } from "esbuild"; | ||
| import path from "node:path"; | ||
| import { rebuildPlugin } from "../../../../scripts/rebuildPlugin.mjs"; | ||
| import { OUT_DIR, ROOT_DIR, getEsbuildDevConfig } from "./constants.mjs"; | ||
| import { BUILD_TARGETS } from "./targets.mjs"; | ||
|
|
||
| /** | ||
| * Create an esbuild context for a single build target | ||
| */ | ||
| async function createBuildContext(targetName, targetConfig, log) { | ||
| const { name, suffix, format, externalModules } = targetConfig; | ||
| const outputFile = `index${name}${suffix}`; | ||
|
|
||
| const plugins = [rebuildPlugin(targetName, log)]; | ||
|
|
||
| return context({ | ||
| ...getEsbuildDevConfig(ROOT_DIR), | ||
| format, | ||
| outfile: path.join(OUT_DIR, outputFile), | ||
| external: externalModules, | ||
| plugins, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Build all targets in watch mode for development | ||
| * @returns {Promise<Array>} Array of esbuild contexts that can be disposed later | ||
| */ | ||
| export async function buildAllWithWatch(log) { | ||
| log.dim("Creating build contexts...\n"); | ||
|
|
||
| // Create contexts for all targets | ||
| const contexts = await Promise.all(BUILD_TARGETS.map((target) => createBuildContext(target.name, target.config, log))); | ||
|
|
||
| log.dim("Starting initial build...\n"); | ||
|
|
||
| // Wait for the initial builds to complete | ||
| await Promise.all(contexts.map((ctx) => ctx.rebuild())); | ||
|
|
||
| // Start watching all targets | ||
| await Promise.all(contexts.map((ctx) => ctx.watch())); | ||
|
|
||
| return contexts; | ||
| } |
25 changes: 25 additions & 0 deletions
25
packages/transformers-webworker/scripts/build/constants.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| export const DIST_FOLDER = "dist"; | ||
| export const EXTERNAL_MODULES = ["@huggingface/transformers"]; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| export const ROOT_DIR = path.join(__dirname, "../.."); | ||
| export const OUT_DIR = path.join(ROOT_DIR, DIST_FOLDER); | ||
|
|
||
| export const getEsbuildDevConfig = (rootDir) => ({ | ||
| bundle: true, | ||
| treeShaking: true, | ||
| logLevel: "info", | ||
| entryPoints: [path.join(rootDir, "src/index.ts")], | ||
| platform: "browser", | ||
| format: "esm", | ||
| sourcemap: true, | ||
| }); | ||
|
|
||
| export const getEsbuildProdConfig = (rootDir) => ({ | ||
| ...getEsbuildDevConfig(rootDir), | ||
| logLevel: "warning", | ||
| sourcemap: false, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { EXTERNAL_MODULES } from "./constants.mjs"; | ||
|
|
||
| /** | ||
| * Build target configuration | ||
| * Each target defines a specific build variant | ||
| */ | ||
| export const BUILD_TARGETS = [ | ||
| { | ||
| name: "Web Worker Build (ESM)", | ||
| config: { | ||
| name: "", | ||
| suffix: ".js", | ||
| format: "esm", | ||
| externalModules: EXTERNAL_MODULES, | ||
| }, | ||
| }, | ||
| ]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
typesentry points to./types/src/index.d.ts, but withoutDir: "types"andinclude: ["src/**/*"]the declaration output will typically betypes/index.d.ts(nosrc/segment). Aligntypes/exports.typeswith the actual emitted declaration path to avoid broken typings for consumers.