일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- 딥러닝
- 코딩
- 코딩 테스트
- 네트워크
- 동적프로그래밍
- 알고리즘
- 프로그래머스
- 백준
- nlp
- 운영체제
- PYTHON
- DFS
- 순열
- AI
- dp
- BFS
- google coding competition
- 파이썬
- 코딩테스트
- kick start
- 동적 프로그래밍
- 브루트포스
- CSS
- OS
- 프로그래밍
- 킥스타트
- linux
- 리눅스
- 그래프
- 구글 킥스타트
- Today
- Total
오뚝이개발자
GPU 최적화, GPU 사용률 늘리는 방법 본문
GPU를 사용해 모델 학습을 돌리다보면 GPU의 사용률이 저조하게 나오는 경우가 있다. 이런 경우 충분한 자원이 있더라도 학습에 오랜 시간이 걸리게 된다. 이 때 필요한 것이 GPU 사용률을 최적화시키는 것이다. 방법은 많지만 그 중 가장 대표적인 방법을 소개하고자 한다. GPU 사용률이 저조한 이유는 대부분 CPU단에서 데이터 처리가 GPU에서의 처리 속도를 따라가지 못해 발생한다. 즉, GPU에서 처리할 데이터가 CPU에서 아직 프로세싱을 거쳐서 준비가 다 되지 않은 경우이다.
num_workers 설정해주기
pytorch와 huggingface의 transformers엔 모두 num_workers라는 파라미터를 설정해줄 수 있다. 이는 학습 도중 CPU의 작업에 몇 개의 코어를 사용할지를 결정하는 변수이다. huggingface의 transformers에선 dataloader_num_workers 파라미터값을 설정해주면 된다.
아래 코드는 transformers 패키지를 사용해 training arguments와 trainer를 선언하는 부분이다. 여기서 args에 dataloader_num_workers=4라고 넘겨주었는데 이는 코어 4개를 사용해 CPU 작업을 처리하겠다는 뜻이다. 이처럼 num_workers를 늘려줌으로 GPU가 처리하는 속도에 조금 더 맞추어 빠르게 CPU에서 데이터를 준비할 수 있어 GPU 사용률을 늘릴 수 있다.
args = Seq2SeqTrainingArguments(
output_dir = "saved_model",
overwrite_output_dir = True,
evaluation_strategy = "epoch",
save_strategy = "epoch",
learning_rate=2e-5,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
gradient_accumulation_steps=2,
weight_decay=0.01,
num_train_epochs=epochs,
predict_with_generate=True,
fp16=False,
dataloader_num_workers=4
)
trainer = Seq2SeqTrainer(
model,
args,
train_dataset=tokenized_datasets["train"],
eval_dataset=tokenized_datasets["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
compute_metrics=compute_metrics
)
Reference
- https://chaelin0722.github.io/etc/gpu_utility/
- https://huggingface.co/docs/transformers/main_classes/trainer
'AI > AI 개발' 카테고리의 다른 글
리눅스(Linux) 아나콘다(Anaconda) 설치 (0) | 2022.08.16 |
---|---|
RuntimeError: CUDA error: CUBLAS_STATUS_INVALID_VALUE when calling `cublasSgemm 에러 해결 (0) | 2022.07.09 |
1초 단위로 GPU 사용량과 메모리 확인하기(GPU 모니터링) (0) | 2022.06.28 |
허깅페이스(Huggingface) 모델 inference(pipeline) GPU로 돌리기 (2) | 2022.06.26 |
텍스트를 문장 단위로 분할하기(nltk, sentence tokenizing) (0) | 2022.05.15 |