Transformers documentation

트레이닝을 위한 torch.compile

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

트레이닝을 위한 torch.compile

torch.compile은 PyTorch 코드를 융합된 커널로 컴파일하여 실행 속도를 높입니다. 학습의 경우, 순전파와 역전파를 함께 추적하여 최적화된 커널로 컴파일함으로써 개별 op 발동 오버헤드를 줄이고, 연산을 융합하여 메모리 대역폭 사용량을 절감합니다.

TrainingArguments에서 torch_compile=True를 설정하여 활성화합니다. 추론은 forward pass만 컴파일하는 반면, 학습은 forward pass와 backward pass를 모두 컴파일합니다. 컴파일은 첫 번째 학습 단계에서 수행되므로, 이후 단계보다 훨씬 느리다는 점을 예상해야 합니다.

from transformers import TrainingArguments

args = TrainingArguments(
    ...,
    torch_compile=True,
    torch_compile_backend="inductor",
    torch_compile_mode="reduce-overhead",
)

백엔드

백엔드가 지정되지 않은 경우, TrainingArguments는 하드웨어에 따라 하나를 선택합니다. 대부분의 CPU와 GPU에서 기본값은 AOTAutograd를 사용하여 Triton 커널로 컴파일되며 대부분의 학습 워크로드에 적합한 inductor입니다. Intel Gaudi(HPU)의 경우 기본값은 hpu_backend입니다. AWS Trainium 및 Inferentia(Neuron)의 경우 기본값은 neuron입니다.

고정된 형태의 입력에 cudagraphs를 사용하세요.

Compile 모드

아래 표를 참고하여 torch.compile 모드를 선택하세요.

mode설명
default컴파일 시간과 런타임 간의 균형
reduce-overhead…
max-autotune컴파일 시점에 여러 커널 구현을 벤치마크하여 가장 빠른 것을 선택합니다(컴파일 시간이 더 오래 걸림)
max-autotune-no-cudagraphsCUDA graphs 없이 max-autotune와 동일

다음 단계

Update on GitHub