ग्रेडिएंट एक्यूम्यूलेशन

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Dennis Lee

Data Engineer, Amazon

वितरित ट्रेनिंग

 

 

कोर्स विषयों का फ्लोचार्ट: डेटा तैयारी, वितरित ट्रेनिंग, कुशल ट्रेनिंग, और ऑप्टिमाइज़र.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

कुशल ट्रेनिंग

 

 

कोर्स विषयों का फ्लोचार्ट: डेटा तैयारी, वितरित ट्रेनिंग, कुशल ट्रेनिंग, और ऑप्टिमाइज़र.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

ट्रेनिंग दक्षता बढ़ाना

 

 

मेमोरी, संचार, और गणना दक्षता दर्शाने वाले आइकन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

ट्रेनिंग दक्षता बढ़ाना

 

 

मेमोरी, संचार, और गणना दक्षता दर्शाने वाले आइकन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

ट्रेनिंग दक्षता बढ़ाना

 

 

मेमोरी, संचार, और गणना दक्षता दर्शाने वाले आइकन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

ग्रेडिएंट एक्यूम्यूलेशन से मेमोरी दक्षता बढ़ती है

 

 

मेमोरी, संचार, और गणना दक्षता दर्शाने वाले आइकन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

बड़े बैच का समस्या

  • बड़े बैच: मज़बूत ग्रेडिएंट, तेज़ लर्निंग
  • GPU मेमोरी बैच आकार सीमित करती है

 

 

बड़ा बैच OOM त्रुटि दे सकता है; छोटे बैच से ट्रेनिंग सफल होती है.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

ग्रेडिएंट एक्यूम्यूलेशन कैसे काम करता है?

एक बड़े बैच को छोटे बैचों में बाँटने का चित्र.

  • ग्रेडिएंट एक्यूम्यूलेशन: छोटे बैचों के ग्रेडिएंट जोड़ें
  • प्रभाव में बड़ा बैच पर ट्रेनिंग करें
  • जोड़ने के बाद पैरामीटर अपडेट करें

कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch, Accelerator, और Trainer

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch, Accelerator, और Trainer

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch, Accelerator, और Trainer

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))









कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))
    outputs = model(inputs, labels=targets)
    loss = outputs.loss







कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))
    outputs = model(inputs, labels=targets)
    loss = outputs.loss
    loss = loss / gradient_accumulation_steps







कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))
    outputs = model(inputs, labels=targets)
    loss = outputs.loss
    loss = loss / gradient_accumulation_steps
    loss.backward()







कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))
    outputs = model(inputs, labels=targets)
    loss = outputs.loss
    loss = loss / gradient_accumulation_steps
    loss.backward()
    if ((index + 1) 
        % gradient_accumulation_steps == 0):





कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch के साथ ग्रेडिएंट एक्यूम्यूलेशन

for index, batch in enumerate(dataloader):
    inputs, targets = (batch["input_ids"], 
                       batch["labels"])
    inputs, targets = (inputs.to(device), 
                       targets.to(device))
    outputs = model(inputs, labels=targets)
    loss = outputs.loss
    loss = loss / gradient_accumulation_steps
    loss.backward()
    if ((index + 1) 
        % gradient_accumulation_steps == 0):
        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()

कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch से Accelerator तक

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

PyTorch से Accelerator तक

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader): inputs, targets = (batch["input_ids"], batch["labels"])

कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader):

        inputs, targets = (batch["input_ids"],
                           batch["labels"])
        outputs = model(inputs, 
                        labels=targets)
        loss = outputs.loss





कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader):
    with accelerator.accumulate(model):
        inputs, targets = (batch["input_ids"],
                           batch["labels"])
        outputs = model(inputs, 
                        labels=targets)
        loss = outputs.loss




कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader):
    with accelerator.accumulate(model):
        inputs, targets = (batch["input_ids"],
                           batch["labels"])
        outputs = model(inputs, 
                        labels=targets)
        loss = outputs.loss
        accelerator.backward(loss)



कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader):
    with accelerator.accumulate(model):
        inputs, targets = (batch["input_ids"],
                           batch["labels"])
        outputs = model(inputs, 
                        labels=targets)
        loss = outputs.loss
        accelerator.backward(loss)



कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator के साथ ग्रेडिएंट एक्यूम्यूलेशन

accelerator = \
    Accelerator(gradient_accumulation_steps=2)

for index, batch in enumerate(dataloader):
    with accelerator.accumulate(model):
        inputs, targets = (batch["input_ids"],
                           batch["labels"])
        outputs = model(inputs, 
                        labels=targets)
        loss = outputs.loss
        accelerator.backward(loss)
        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()

कई बैचों के ग्रेडिएंट के योग के रूप में ग्रेडिएंट एक्यूम्यूलेशन.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator से Trainer तक

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Accelerator से Trainer तक

PyTorch, Accelerator, और Trainer के लिए उपयोग में सरलता बनाम अनुकूलन क्षमता तुलना चार्ट.

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Trainer के साथ ग्रेडिएंट एक्यूम्यूलेशन

training_args = TrainingArguments(output_dir="./results",
                                  evaluation_strategy="epoch",
                                  gradient_accumulation_steps=2)

trainer = Trainer(model=model, args=training_args, train_dataset=dataset["train"], eval_dataset=dataset["validation"], compute_metrics=compute_metrics)
trainer.train()
{'epoch': 1.0, 'eval_loss': 0.73, 'eval_accuracy': 0.03, 'eval_f1': 0.05}
{'epoch': 2.0, 'eval_loss': 0.68, 'eval_accuracy': 0.19, 'eval_f1': 0.25}
PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Passons à la pratique !

PyTorch के साथ कुशल AI मॉडल प्रशिक्षण

Preparing Video For Download...