Trainer로 그래디언트 체크포인팅 적용하기
그래디언트 체크포인팅(gradient checkpointing)을 사용해 모델의 메모리 사용량을 줄이려고 합니다. Accelerator로 명시적인 학습 루프를 작성하는 방법은 이미 살펴보았으니, 이번에는 Trainer를 사용해 학습 루프 없이 더 간편하게 구현해 보겠습니다. trainer.train() 호출로 인해 실행에 다소 시간이 걸릴 수 있습니다.
그래디언트 체크포인팅을 사용하도록 Trainer의 인수를 설정하세요.
이 연습은 강의의 일부입니다
PyTorch로 AI 모델 효율적으로 학습시키기
연습 안내
TrainingArguments에서 그래디언트 누적 단계를 4로 설정하세요.TrainingArguments에서 그래디언트 체크포인팅을 활성화하세요.- 학습 인수를
Trainer에 전달하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
training_args = TrainingArguments(output_dir="./results",
evaluation_strategy="epoch",
# Use four gradient accumulation steps
gradient_accumulation_steps=____,
# Enable gradient checkpointing
gradient_checkpointing=____)
trainer = Trainer(model=model,
# Pass in the training arguments
args=____,
train_dataset=dataset["train"],
eval_dataset=dataset["validation"],
compute_metrics=compute_metrics)
trainer.train()