Skip to content

feat: export deviceToExecutionProviders and getSupportedDevices#1646

Open
TimPietrusky wants to merge 1 commit intohuggingface:mainfrom
TimPietrusky:export-device-utils
Open

feat: export deviceToExecutionProviders and getSupportedDevices#1646
TimPietrusky wants to merge 1 commit intohuggingface:mainfrom
TimPietrusky:export-device-utils

Conversation

@TimPietrusky
Copy link
Copy Markdown

Summary

Export two device utility functions from the public API so downstream libraries can implement their own device fallback logic:

  • deviceToExecutionProviders(device) — maps a device string (e.g. "auto", "cuda", "cpu") to the corresponding ONNX execution providers list. Already existed internally, just not re-exported.
  • getSupportedDevices() — returns a copy of the platform-specific supportedDevices array in priority order (e.g. ["cuda", "webgpu", "cpu"] on Linux x64, ["coreml", "webgpu", "cpu"] on macOS).

Why

When device="auto" and a provider crashes hard (e.g. CUDA when libcudnn.so is missing — see #1642), downstream code needs to retry providers individually. Without access to the device list, libraries have to hardcode their own copy of the platform detection logic, which drifts when transformers.js updates.

Example usage

import { getSupportedDevices } from '@huggingface/transformers';

// Walk devices one at a time, catching provider crashes
for (const device of getSupportedDevices()) {
  try {
    model = await AutoModel.from_pretrained(modelId, { device });
    break;
  } catch (err) {
    console.warn(`device=${device} failed: ${err.message}`);
  }
}

Changes

  • src/backends/onnx.js: added getSupportedDevices() function
  • src/transformers.js: re-exported deviceToExecutionProviders and getSupportedDevices

Closes #1645

Export two device utility functions from the public API:

- `deviceToExecutionProviders(device)` — maps a device string to
  the ONNX execution providers list (already existed internally)
- `getSupportedDevices()` — returns the platform-specific supported
  devices array in priority order (new convenience wrapper)

This lets downstream libraries implement their own device fallback
logic without hardcoding platform-specific device lists.

Closes huggingface#1645
@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export supportedDevices / deviceToExecutionProviders from public API

3 participants