generate() 는 함수가 아니라 state machine
밑단에서 model.generate() 는 GenerationConfig 객체로 구동되는 token-by-token 디코딩 루프. config 가 strategy (do_sample, num_beams), 한계 (max_new_tokens, min_new_tokens), 샘플링 분포 shaping (temperature, top_p, top_k, repetition_penalty), 정지 조건 (eos_token_id, stop_strings) 운반.
실제로 쓸 strategy 셋
- Greedy (
do_sample=False,num_beams=1): 매 step argmax. Deterministic. 툴 콜링, JSON, 코드 completion 의 디폴트. - Sampling (
do_sample=True): nucleus + top-k + temperature. 챗 디폴트. 시작점temperature=0.7,top_p=0.9. - Beam search (
num_beams=4): 후보 sequence 여러 개 explore. translation / summarization 처럼 “단일 best” 답 있을 때 유용. 비용 큼 —num_beams배수의 일.
정지 조건은 옵션 아님
eos_token_id 안 셋 한 게 모델이 “무한 루프” 도는 가장 흔한 이유. 모던 instruct 모델 종종 EOS 토큰 여러 개 (<|eot_id|>, <|end_of_turn|> 등) — 리스트로 넘겨. stop_strings 는 임의 substring 을 decode 후 매치 (느린데 유연함).