Skip to content

[Enhancement] Add early device validation in pipeline() to prevent downloading/loading models before crashing on typos #47869

Description

@AryanSharma21

Feature request

Feature Description

Currently, if a user misspells or passes an invalid device argument when initializing transformers.pipeline() (e.g., passing device="mpx" instead of "mps" or "cuda"), the function proceeds to download and load the full model weights into RAM before validating the device.

Validating the device parameter early at the pipeline() entry point would save bandwidth, RAM/disk overhead, and developer time.

Current Internal Execution Flow & Behavior

When executing:

from transformers import pipeline

# Downloads gigabytes of model weights first... and then crashes at tensor placement
classifier = pipeline("text-classification", model="bert-base-uncased", device="mpx")

Proposed Solution
Add lightweight early validation at the very top of transformers.pipeline() before from_pretrained() or model initialization is triggered.

If device is a string, validate it against canonical device prefixes (cpu, cuda, mps, npu, xpu, hpu), integers, or torch.device instances. If invalid, fail immediately:

ValueError: Invalid device 'mpx'. Expected a valid device string (e.g., 'cpu', 'cuda', 'mps', 'npu', 'xpu'), an integer device ordinal (e.g., 0), or a torch.device instance.

Motivation

It is most likely that any user would be annoyed to see an entire model download and fail at tensor placement.

Your contribution

I am happy to open PR adding this early check and unit tests if the maintainers agree with this enhancement.

will open PR for this

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions