그래디언트 누적

PyTorch로 AI 모델 효율적으로 학습시키기

Dennis Lee

Data Engineer, Amazon

분산 학습

 

 

데이터 준비, 분산 학습, 효율적 학습, 옵티마이저 등 강의 주제를 나타내는 순서도.

PyTorch로 AI 모델 효율적으로 학습시키기

효율적인 학습

 

 

데이터 준비, 분산 학습, 효율적 학습, 옵티마이저 등 강의 주제를 나타내는 순서도.

PyTorch로 AI 모델 효율적으로 학습시키기

학습 효율성 향상

 

 

메모리 효율성, 통신 효율성, 연산 효율성을 나타내는 아이콘.

PyTorch로 AI 모델 효율적으로 학습시키기

학습 효율성 향상

 

 

메모리 효율성, 통신 효율성, 연산 효율성을 나타내는 아이콘.

PyTorch로 AI 모델 효율적으로 학습시키기

학습 효율성 향상

 

 

메모리 효율성, 통신 효율성, 연산 효율성을 나타내는 아이콘.

PyTorch로 AI 모델 효율적으로 학습시키기

그래디언트 누적은 메모리 효율성을 향상시킵니다

 

 

메모리 효율성, 통신 효율성, 연산 효율성을 나타내는 아이콘.

PyTorch로 AI 모델 효율적으로 학습시키기

대규모 배치 크기의 문제

  • 대규모 배치 크기: 더 빠른 학습을 위한 안정적인 그래디언트 추정
  • GPU 메모리가 배치 크기를 제한

 

 

대규모 배치 크기는 메모리 부족 오류를 일으킬 수 있고, 소규모 배치 크기는 학습을 성공적으로 완료할 수 있음을 보여주는 다이어그램.

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 모델 효율적으로 학습시키기

연습해 봅시다!

PyTorch로 AI 모델 효율적으로 학습시키기

Preparing Video For Download...