Gradient Checkpointing ด้วย Accelerator
คุณกำลังปรับปรุงการใช้หน่วยความจำต่อเนื่อง เพื่อให้สามารถเทรนโมเดลแปลภาษาบนอุปกรณ์ของคุณได้ การสะสม gradient ช่วยให้เทรนด้วย batch size ที่ใหญ่ขึ้นได้อย่างมีประสิทธิภาพ ต่อยอดจากงานนี้โดยเพิ่ม gradient checkpointing เพื่อลด memory footprint ของโมเดล
model, train_dataloader และ accelerator ได้รับการกำหนดค่าไว้ล่วงหน้าแล้ว
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การเทรน AI Model อย่างมีประสิทธิภาพด้วย PyTorch
คำแนะนำการฝึกหัด
- เปิดใช้งาน gradient checkpointing บน
model - ตั้งค่า context manager ของ
Acceleratorเพื่อเปิดใช้งาน gradient accumulation บนmodel
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Enable gradient checkpointing on the model
____.____()
for batch in train_dataloader:
with accelerator.accumulate(model):
inputs, targets = batch["input_ids"], batch["labels"]
# Get the outputs from a forward pass of the model
____ = ____(____, labels=targets)
loss = outputs.loss
accelerator.backward(loss)
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
print(f"Loss = {loss}")