Skip to content
Open
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions packages/transformers/src/backends/onnx.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ export function deviceToExecutionProviders(device = null) {
throw new Error(`Unsupported device: "${device}". Should be one of: ${supportedDevices.join(', ')}.`);
}

/**
* Get the list of supported devices for the current platform, sorted by
* priority/performance (best first, CPU/WASM last).
*
* This is the same ordering used internally when `device` is set to `"auto"`.
* Returns a shallow copy so callers can mutate or iterate without affecting
* library internals.
*
* @returns {import("../utils/devices.js").DeviceType[]} The supported devices.
*/
export function getSupportedDevices() {
return /** @type {import("../utils/devices.js").DeviceType[]} */ ([...supportedDevices]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Now that deviceToExecutionProviders is exported publicly, returning supportedDevices directly for device === "auto" exposes mutable internal state:

case 'auto':
    return supportedDevices;

A caller can accidentally or intentionally mutate the returned array, for example deviceToExecutionProviders('auto').length = 0, which changes the library's internal device order for later calls. getSupportedDevices() already avoids this by returning [...supportedDevices]; this path should do the same or otherwise return an immutable copy.

}

/**
* Currently, Transformers.js doesn't support simultaneous loading of sessions in WASM/WebGPU.
* For this reason, we need to chain the loading calls.
Expand Down
3 changes: 3 additions & 0 deletions packages/transformers/src/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export { random } from './utils/random.js';

export { DynamicCache } from './cache_utils.js';

// Device utilities
export { deviceToExecutionProviders, getSupportedDevices } from './backends/onnx.js';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This adds runtime exports from the package entry point, but the checked-in generated declaration files are not updated. packages/transformers/types/transformers.d.ts still does not export deviceToExecutionProviders or getSupportedDevices, and packages/transformers/types/backends/onnx.d.ts still does not contain getSupportedDevices.

TypeScript consumers importing the new public API from @huggingface/transformers will therefore get a type error even though the runtime export exists. Please run the type generation step or add the generated declaration updates to this PR.


// Cache and file management
export { ModelRegistry } from './utils/model_registry/ModelRegistry.js';

Expand Down
Loading