(코드 탐색) llama.cpp (code)

Legacy quantization (linear quantization) (PR)

  • type-0 (Q4_0, Q5_0): scale only (weight = scale * quantized)
  • type-1 (Q4_1, Q5_1): scale + bias (weight = scale * quantized + block minimum)
  • superblock 은 block (=weight group) 들의 scale, bias 를 quantize.
Namebiasbit for weightn_weights per blockbit for scale (per block)bit for min (per block)n_blocks per superblockbit per weight
Q2_KO21644162.5625
Q3_KX31666163.4375
Q4_KO4326684.5
Q5_KO5326685.5
Q6_KX61688166.5625
Q8_KX8256    
  • Q4_k 의 경우 bit per weight 계산 방법
    • FP16 s_of_scales (16)
    • FP16 s_of_mins (16)
    • 8 x 6 bit scales (48)
    • 8 x 6 bit biases (48)
    • 256 x 4 bit weights (1024)
    • (16 + 16 + 48 + 48 + 1024) / 256 = 4.5

Quantization mixes (PR)

  • Q3_K_S, Q5_K_M 등
  • 예시
    • Q3_K_S: Q3_K for all tensors
    • Q3_K_M: Q4_K for the attention.wv, attention.wo, and feed_forward.w2 tensors, else GGML_TYPE_Q3_K
    • Q3_K_L: Q5_K for the attention.wv, attention.wo, and feed_forward.w2 tensors, else GGML_TYPE_Q3_K

Importance matrix (PR)

  • wiki.train.raw 과 같은 데이터를 가지고, 각 weight 의 중요도를 input column 의 squared sum 으로 approximate 하여 quantize 시 활용.

I-Quantization (PR)

  • IQ2_XXS, IQ3_S 등
  • QuIP# 에서 sign flip, lattice 기법을 차용.

GGUF 모델 변환

  • HuggingFace checkpoint 를 GGUF 포맷으로 변환 필요 (convert-hf-to-gguf.py)
  • gguf-py 폴더는 GGUF 포맷을 read/write 하기 위한 python 코드들이 들어있음.

wrapper

  • ollama: gguf 서버를 내부적으로 띄운뒤, go 로 짜여진 코드로 request 하는 구조 (2024.07.05)
  • gpt4all: gguf 함수를 call 하는 구조. UI 와 연동되는 cpp 코드를 자체적으로 작성 (2024.07.05)