Transformers documentation

ハードウェア

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.22.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ハードウェア

Transformers は、PyTorch バックエンドを通じて、CPU、NVIDIA GPU、AMD GPU、Apple Silicon、Google TPU、AWS Trainium インスタンスなど、さまざまなハードウェアで動作します。

  • トレーニングでは、TrainingArgumentsがバックエンドを自動的に検出します。
  • 推論では、自動検出がすべてのバックエンドをカバーするわけではないため、デバイスの文字列を渡して明示的に選択します。このページでは、各バックエンドのデバイス文字列とパッケージ、TrainingArgumentsがそれをどのように設定するか、そして Transformers がどのデバイスを使用するかをどのように決定するかを説明します。

お使いのハードウェアを確認

各バックエンドには、device_mapまたはdeviceに渡すデバイス文字列があります。CPUとApple Siliconは、標準のtorchパッケージで動作します。その他のバックエンドには、PyTorchにバックエンドを登録するPyTorch拡張パッケージが必要です。

ハードウェアデバイス文字列パッケージ
CPUcputorch
NVIDIA GPUcudatorch
AMD GPUcudatorch(ROCm ビルド)
AWS Trainiumneurontorch-neuronx
Google TPUtputorch-tpu
Intel GPUxputorch
Apple Siliconmpstorch
Intel Gaudihpuhabana-torch-plugin
Huawei Ascend NPUnputorch-npu
Cambricon MLUmlutorch-mlu
Moore Threads GPUmusatorch-musa

この表にハードウェアバックエンドが記載されていない場合、Transformers がそれをサポートしていると考える場合は、issue を作成してください。

Transformers がデバイスをどのように選択するか

Transformers は、トレーニングと推論で異なるデバイスを選択します。

トレーニング

TrainingArguments は Accelerate を通じてバックエンドを自動的に検出します。分散実行では、各プロセスが Accelerate に割り当てられたデバイスを使用します。単一プロセスの場合、TrainingArguments は最初に利用可能なバックエンドを選択し、利用可能なバックエンドがない場合は CPU にフォールバックします。CPU でのトレーニングを強制するには、use_cpu=True を設定してください。

推論

from_pretrained() はデフォルトでモデルを CPU に読み込みます。

device_map="auto" を設定すると、利用可能なアクセラレーターにモデルが配置されます。自動配置は Accelerate に依存しており、CUDA、Intel XPU、Intel Gaudi (HPU)、Apple Silicon (MPS) などのバックエンドを検出しますが、すべてのバックエンドをカバーするわけではありません。AWS Trainium や Google TPU などの他のバックエンドを使用する場合は、device_map にデバイス文字列を明示的に渡してください。

from transformers import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-0.6B", device_map="neuron")
Update on GitHub