본문 바로가기
Computer/LLM

nanoGPT, readme

by hexists 2023. 3. 8.

https://github.com/karpathy/nanoGPT

 

GitHub - karpathy/nanoGPT: The simplest, fastest repository for training/finetuning medium-sized GPTs.

The simplest, fastest repository for training/finetuning medium-sized GPTs. - GitHub - karpathy/nanoGPT: The simplest, fastest repository for training/finetuning medium-sized GPTs.

github.com

일단 이것부터 공부해보는 걸로...

어디서 들었는데,,, 간단하게 GPT를 공부해 볼 수 있다고 함...

 

한 페이지에 code 리뷰까지 분석해보려고 했으나, readme만 정리해도 내용이 많아서 나눠서 정리합니다.


 

The simplest, fastest repository for training/finetuning medium-sized GPTs. It is a rewrite of minGPT that prioritizes teeth over education.

가장 간단하고 빠른 중간 사이즈의 GPT, miniGPT를 재작성한 코드(miniGPT가 있는지 몰랐음)

- miniGPT: https://github.com/karpathy/minGPT

 

GitHub - karpathy/minGPT: A minimal PyTorch re-implementation of the OpenAI GPT (Generative Pretrained Transformer) training

A minimal PyTorch re-implementation of the OpenAI GPT (Generative Pretrained Transformer) training - GitHub - karpathy/minGPT: A minimal PyTorch re-implementation of the OpenAI GPT (Generative Pret...

github.com

Still under active development, but currently the file train.py reproduces GPT-2 (124M) on OpenWebText, running on a single 8XA100 40GB node in about 4 days of training.

현재는 GPT-2 (124M = 1억 2400만) OpenWebText를 A100 8개로 4일 학습 가능

 

The code itself is plain and readable: train.py is a ~300-line boilerplate training loop and model.py a ~300-line GPT model definition, which can optionally load the GPT-2 weights from OpenAI. 

train.py, model.py 300줄로 작성되어 있음

 

transformes, datasets, tiktoken, wandb, tqdm 필요

(mac에서 설치할 때는 nightly version을 설치하는게 좋다고 함)

 

quick start를 보면 세익스피어 글을 가지고 간단히 모델을 돌려볼 수 있음

1) 먼저 raw text를 integer로 변환하고

$ python data/shakespeare_char/prepare.py

2) 학습 가능, gpu가 있다면 바로 실행하면 되고, A100 기준 3분 걸리고 val loss 1.4697
GPU가 없으면 옵션을 지정해서 실행하면 됨, 모델 좀 줄이고 max_iters를 2000, 실행한지 30분 정도 됐는데 600 iters

#GPU에서 실행시
$ python sample.py --out_dir=out-shakespeare-char

#mac에서 실행시
$ python train.py config/train_shakespeare_char.py --device=cpu --compile=False --eval_iters=20 --log_interval=1 --block_size=64 --batch_size=12 --n_layer=4 --n_head=4 --n_embd=128 --max_iters=2000 --lr_decay_iters=2000 --dropout=0.0

#silicon에서는 device=mps를 지정하면 2~3배 빠르다고 함
$ --device=cpu

3) 학습이 끝나면 실행해 볼 수 있음, 학습이 안 끝나서 아직 못해봄... 뭘 생성했는지 잘 모르겠음

$ python sample.py --out_dir=out-shakespeare-char

 

quick start는 여기까지


다음은 reproducing GPT-2

 

1) 먼저 OpenWebText를 tokenize

OpenWebText에서는 2가지 버전을 제공함

- Plug and Play Version

17,103,059 documents
65.86 GB uncompressed text
28 GB compressed including text and metadata

- Raw Scrapes Version

69,547,149 documents
193.89gb uncompressed text.
79gb compressed including text and metadata

$ python train.py eval_gpt2
$ python train.py eval_gpt2_medium
$ python train.py eval_gpt2_large
$ python train.py eval_gpt2_xl

 

하지만, 실제로는 다른 데이터를 사용함... 어떻게 정리된 데이터인지 파악 필요

https://huggingface.co/datasets/openwebtext (cc. https://skylion007.github.io/OpenWebTextCorpus/)

코드에는 아래와 같이 데이터 사이즈가 정리되어 있음

# takes 54GB in huggingface .cache dir, about 8M documents (8,013,769), link

$ python data/openwebtext/prepare.py

 

2) GPT-2(124M) 학습, 8X A100 40GB가 필요

$ torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py

pytorch DDP를 이용하면 4일 학습을 하면 loss가 2.85 정도로 떨어짐.

cluster 환경인 경우에는 아래와 같이 실행하면 됨, interconnect 실험에 좋다고 함(ex, iperf3)

Run on the first (master) node with example IP 123.456.123.456:
$ torchrun --nproc_per_node=8 --nnodes=2 --node_rank=0 --master_addr=123.456.123.456 --master_port=1234 train.py
Run on the worker node:
$ torchrun --nproc_per_node=8 --nnodes=2 --node_rank=1 --master_addr=123.456.123.456 --master_port=1234 train.py

gpu가 1개라면... 아래처럼 실행하고 환경변수를 적절히 수정해서 사용하면 됨

$ python train.py

다음은 baselines

아래 명령어로 baselines를 확인해 볼 수 있음

$ python train.py eval_gpt2
$ python train.py eval_gpt2_medium
$ python train.py eval_gpt2_large
$ python train.py eval_gpt2_xl

However, we have to note that GPT-2 was trained on (closed, never released) WebText, while OpenWebText is just a best-effort open reproduction of this dataset. This means there is a dataset domain gap.

하지만, OWT는 GPT-2가 사용한 데이터셋을 재생산한 것이라서 차이가 있을 수 있음


finetuning

파인튜닝은 openai GPT-2와 다른 점이 없음, 단일 gpu에서 몇 분 걸림

$ python train.py config/finetune_shakespeare.py

이렇게하면 하이퍼 파라미터를 config/finetune_shakespeare.py의 값을 사용함.

OOM이 발생하면 모델 사이즈를 줄이거나, 블럭 사이즈를 줄여보면 됨


samling / inference

sample 스크립트를 이용, 아래는 gpt2-xl을 사용한 예

$ python sample.py \
    --init_from=gpt2-xl \
    --start="What is the answer to life, the universe, and everything?" \
    --num_samples=5 --max_new_tokens=100

기타

- bench.py 사용하면 프로파일링에 좋음(train.py와 training loop 동일)

- torch.compile() 사용(nightly version), iteration 시간 단축(22년 12월 29일 기준)

- todos, troubleshooting은 여기에 정리는 생략

'Computer > LLM' 카테고리의 다른 글

MLM vs CLM  (0) 2023.04.20
InstructGPT Evaluation  (2) 2023.04.17
In-context Learning  (0) 2023.04.13
nanoGPT, prepare.py  (0) 2023.03.16
ChatGPT  (0) 2023.03.07