🧭 RoPE + Context 확장
🪟 Sliding Window Attention & Group Query Attention
- 기존 Transformer의 어텐션은 메모리·속도 부담이 큼
- 이를 해결하기 위해 시퀀스를 작은 창(window) 단위로 나눠 어텐션 수행
- 전체 문장이 아닌 부분 단위만 계산해 긴 문서 처리 최적화 + 속도 향상
- 추가적으로 LLAMA처럼 Gemma2도 GQA을 도입하여 어텐션 연산에 대한 최적화를 통해 학습과 추론 과정에서 연산 속도가 향상

🧮 Logit Soft Capping
📘 지식 증류 (On-policy Distillation)
- 더 큰 teacher 모델의 출력을 사용해 작은 모델 학습
- 일반적인 증류는 teacher 모델의 응답을 기준으로 학습을 하다보면 student 모델에는 없는 토큰이 존재하여 분포 불일치 문제가 있는데, Gemma2는 On-policy Distillation을 통해 이를 해결
- 즉, student 모델이 생성한 문맥에 맞춰 teacher 모델이 응답의 순위를 부여하고, 부족한 응답에 대해서는 출력을 재생성함으로써 student 모델의 분포 일치를 유지하면서 부족한 응답에 대해서는 teacher 모델로 보강하여 좀 더 추론 능력을 향상시켰습니다.

🧬 모델 병합 (Model Merging with Warp)

사용한 패키지 버전
- flash_attn == 2.5.9.post1
- accelerate == 0.30.1
- sentencepiece == 0.2.0
- torch == 2.3.0
- transformers == 4.42.3
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, TextStreamer
import torch
tokenizer = AutoTokenizer.from_pretrained(
"google/gemma-2-9b-it",
)
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-2-9b-it",
torch_dtype=torch.bfloat16,
device_map='auto',
)
streamer = TextStreamer(tokenizer)
messages = [
{"role": "user", "content": "대한민국의 수도에 대해 알려줘"},
]
input_ids = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
terminators = [
tokenizer.eos_token_id,
tokenizer.convert_tokens_to_ids("<|end_of_turn|>")
]
outputs = model.generate(
input_ids,
max_new_tokens=512,
eos_token_id=terminators,
do_sample=False,
repetition_penalty=1.05,
streamer = streamer
)
response = outputs[0][input_ids.shape[-1]:]
print(tokenizer.decode(response, skip_special_tokens=True))
# 결과
대한민국의 수도는 **서울**입니다.
서울은 한국의 역사, 문화, 경제 중심지이며, 약 970만 명의 인구를 가진 대도시입니다.