क्वांटाइज़ेशन से मॉडल छोटे बनाना

Llama 3 के साथ फाइन-ट्यूनिंग

Francesca Donadoni

Curriculum Manager, DataCamp

क्वांटाइज़ेशन क्या है?

 

  • मॉडल प्रिसिजन घटाना
  • 32-bit float से:
    • 8-bit integer
    • 4-bit integer
  • क्वांटाइज़ेशन-अवेयर ट्रेनिंग

सारगर्भित ब्लॉक.jpg

Llama 3 के साथ फाइन-ट्यूनिंग

क्वांटाइज़ेशन के प्रकार

 

  • वेट क्वांटाइज़ेशन: वेट की प्रिसिजन घटाएँ
  • एक्टिवेशन क्वांटाइज़ेशन: एक्टिवेशन वैल्यू की प्रिसिजन घटती है
  • पोस्ट-ट्रेनिंग क्वांटाइज़ेशन: ट्रेनिंग के बाद मॉडल प्रिसिजन घटाएँ
Llama 3 के साथ फाइन-ट्यूनिंग

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)
Llama 3 के साथ फाइन-ट्यूनिंग

क्वांटाइज़ेशन के साथ मॉडल लोड करना

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
)
Llama 3 के साथ फाइन-ट्यूनिंग

क्वांटाइज़्ड मॉडल का उपयोग

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.
Llama 3 के साथ फाइन-ट्यूनिंग

क्वांटाइज़्ड मॉडल को फाइन-ट्यून करना

  • फुल क्वांटाइज़ेशन फाइन-ट्यूनिंग सपोर्ट नहीं करता
  • 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()
Llama 3 के साथ फाइन-ट्यूनिंग

अभ्यास करते हैं!

Llama 3 के साथ फाइन-ट्यूनिंग

Preparing Video For Download...