LLM 기초LLM Fundamentals
토크나이저부터 GPT 구현, 디코딩, 스케일링 법칙, 추론 최적화, SFT까지.Tokenizers, a GPT from scratch, decoding, scaling laws, inference optimisation and SFT.
- 언어 모델링Language modeling35 분min
언어 모델이 정확히 무엇을 추정하는지(자기회귀 분해), 왜 손실이 토큰당 NLL이고 perplexity가 "실효 선택지 수"인지 유도하고, 문자 bigram·trigram 모델을 직접 세어 만들어 본다.What exactly a language model estimates (the autoregressive factorisation), a derivation of why the loss is per-token NLL and why perplexity is the "effective number of choices", and character bigram and trigram models built by counting.
- 토크나이저Tokenizers35 분min
왜 LLM이 글자도 단어도 아닌 subword를 쓰는지, byte-level BPE가 어떻게 학습·인코딩·디코딩되는지 직접 돌려 보고, 숫자·공백·한국어·glitch token 같은 토큰화 부작용이 어디서 오는지 설명한다.Why LLMs use subwords rather than characters or words, how byte-level BPE is trained, encodes and decodes (you run it yourself), and where tokenization side effects such as numbers, whitespace, Korean text and glitch tokens come from.
- GPT를 바닥부터GPT from scratch40 분min
토큰 id에서 loss까지 GPT의 forward를 한 줄씩 조립한다. 위치 정보를 넣는 세 방법을 비교하고 RoPE가 왜 상대 위치만 남기는지 유도하며, causal mask와 한 칸 밀린 target이 어떻게 T개의 예측을 한 번에 만드는지 확인한다.Assemble GPT's forward pass line by line, from token ids to the loss. Compare three ways of injecting position, derive why RoPE keeps only relative position, and see how the causal mask and the shifted-by-one targets yield T predictions at once.
- 디코딩Decoding35 분min
모델이 낸 다음 토큰 분포를 실제 텍스트로 바꾸는 규칙들. greedy·beam이 찾는 "가장 그럴듯한 시퀀스"와 sampling이 재현하는 "분포"의 차이, temperature를 logit 스케일링으로 유도하고 top-k·top-p·min-p가 분포를 어떻게 자르는지 직접 조작해 본다.The rules that turn the model's next-token distribution into actual text. The difference between the "most likely sequence" that greedy and beam search look for and the "distribution" that sampling reproduces; temperature derived as logit scaling, and a hands-on look at how top-k, top-p and min-p cut the distribution.
- 사전학습과 스케일링 법칙Pretraining and scaling laws40 분min
학습 compute C ≈ 6ND를 forward/backward 연산 수로 유도하고, Kaplan과 Chinchilla 스케일링 법칙이 "주어진 compute를 모델 크기와 토큰 수에 어떻게 나눌까"에 어떻게 답하는지, 왜 실제로는 일부러 over-training하는지 계산해 본다. 데이터 혼합·중복 제거·mid-training, 그리고 "창발"이 지표의 착시일 수 있다는 점까지 다룬다.Derive the training compute C ≈ 6ND from forward/backward operation counts, compute how the Kaplan and Chinchilla scaling laws answer "how should a given compute budget be split between model size and token count", and why in practice models are over-trained on purpose. Also covers data mixture, deduplication and mid-training, and how "emergence" can be an illusion of the metric.
- 추론 최적화Inference optimisation40 분min
LLM 추론이 왜 prefill은 계산에, decode는 메모리 대역폭에 묶이는지 산술 강도로 설명하고, KV cache 크기 공식과 GQA, continuous batching, PagedAttention, 양자화, MoE가 각각 어떤 병목을 푸는지 계산한다. speculative decoding의 수락 규칙이 target 분포를 정확히 보존함을 유도하고 시뮬레이션으로 확인한다.Explain with arithmetic intensity why LLM prefill is bound by compute and decode by memory bandwidth, and work out the KV-cache size formula and which bottleneck GQA, continuous batching, PagedAttention, quantisation and MoE each relieve. Derive that the acceptance rule of speculative decoding preserves the target distribution exactly, and confirm it by simulation.
- 사후학습 개관과 SFTPost-training overview and SFT35 분min
사전학습 모델이 어떻게 대화하는 모델이 되는지 사후학습 파이프라인 전체를 조망하고, 그 첫 단계인 SFT를 구현 수준에서 다룬다. chat template과 special token, 프롬프트 토큰 loss masking, 멀티턴과 packing의 attention mask, 데이터 규모와 품질, 그리고 SFT가 왜 forward KL 최소화인지.A view of the whole post-training pipeline that turns a pre-trained model into one that converses, then its first stage, SFT, at the implementation level: chat templates and special tokens, loss masking of prompt tokens, multi-turn data and the attention mask under packing, data scale and quality, and why SFT is forward-KL minimisation.