बेसिक PyTorch के साथ mixed precision training
आप अपने language translation मॉडल के प्रशिक्षण को तेज करने के लिए low-precision floating point डेटा टाइप का उपयोग करेंगे. उदाहरण के लिए, 16-bit floating point डेटा टाइप (float16) उनके 32-bit समकक्ष (float32) के आधे आकार के होते हैं. इससे matrix multiplications और convolutions की गणनाएँ तेज हो जाती हैं. याद करें कि इसमें gradients को scale करना और operations को 16-bit floating point में cast करना शामिल है.
कुछ ऑब्जेक्ट पहले से लोड किए गए हैं: dataset, model, dataloader, और optimizer.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PyTorch के साथ कुशल AI मॉडल प्रशिक्षण
अभ्यास निर्देश
- लूप से पहले,
torch.amp.GradScalerका उपयोग करके gradients के लिए एक scaler परिभाषित करें. - लूप के भीतर,
torch.autocastको context manager के रूप में उपयोग करके operations को 16-bit floating point डेटा टाइप में cast करें. - लूप के भीतर, loss को scale करें और scaled gradients बनाने के लिए
.backward()कॉल करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Define a scaler for the gradients
scaler = torch.amp.____()
for batch in train_dataloader:
inputs, targets = batch["input_ids"], batch["labels"]
# Casts operations to mixed precision
with torch.____(device_type="cpu", dtype=torch.____):
outputs = model(inputs, labels=targets)
loss = outputs.loss
# Compute scaled gradients
scaler.____(loss).backward()
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()