การทำให้โมเดลเล็กลงด้วย Quantization

Fine-Tuning กับ Llama 3

Francesca Donadoni

Curriculum Manager, DataCamp

Quantization คืออะไร?

 

  • ลดความแม่นยำของโมเดล
  • จาก 32-bit float เป็น:
    • 8-bit integer
    • 4-bit integer
  • Quantization-aware training

บล็อกนามธรรม

Fine-Tuning กับ Llama 3

ประเภทของ Quantization

 

  • Weight quantization: ลดความแม่นยำของน้ำหนักโมเดล
  • Activation quantization: ลดความแม่นยำของค่า activation
  • Post-Training Quantization: ลดความแม่นยำของโมเดลหลังการเทรน
Fine-Tuning กับ Llama 3

ตั้งค่า Quantization ด้วย bitsandbytes

from transformers import BitsAndBytesConfig

bnb_config = BitsAndBytesConfig(
  • กำหนด precision (load_in_4_bit, load_in_8_bit)
    load_in_4bit=True,
  • กำหนด quantization type ('fp4' หรือ 4-bit float, 'nf4' หรือ normalized 4-bit float)
    bnb_4bit_quant_type="nf4",
  • กำหนด compute precision (32-bit float หรือ 16-bit bfloat)
    bnb_4bit_compute_dtype=torch.bfloat16)
Fine-Tuning กับ Llama 3

โหลดโมเดลพร้อม Quantization

from transformers import BitsAndBytesConfig, AutoModelForCausalLM

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

model = AutoModelForCausalLM.from_pretrained( "nvidia/Llama3-ChatQA-1.5-8B",
quantization_config=bnb_config
)
Fine-Tuning กับ Llama 3

การใช้งานโมเดลที่ผ่าน Quantization

promptstr = """System: You are a helpful chatbot who answers questions about planets.
User: Explain the history of Mars
Assistant: """

inputs = tokenizer.encode(promptstr, return_tensors="pt")
outputs = model.generate(inputs, max_length=200)
decoded_outputs = tokenizer.decode(outputs[0, inputs.shape[1]:], skip_special_tokens = True)
print(decoded_outputs)
Here is a brief history of Mars:
- 4.6 billion years ago: Mars formed as part of the solar system.
- 3.8 billion years ago: Mars had a thick atmosphere and liquid water on its surface.
- 3.8 billion years ago to 3.5 billion years ago: Mars lost its magnetic field and atmosphere, 
and became a cold, dry planet.
- 3.5 billion years ago to present: Mars has been cold and dry, with a thin atmosphere.
Fine-Tuning กับ Llama 3

การ Fine-tune โมเดลที่ผ่าน Quantization

  • Quantization แบบเต็มไม่รองรับการ fine-tune
  • การปรับแต่งด้วย LoRA
trainer = SFTTrainer(
    model=model,

peft_config=peft_config,
train_dataset=ds, max_seq_length=250, dataset_text_field='conversation', tokenizer=tokenizer, args=training_arguments
)
trainer.train()
Fine-Tuning กับ Llama 3

มาฝึกกันเถอะ!

Fine-Tuning กับ Llama 3

Preparing Video For Download...