هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
What the course covers
How to load and run pretrained language models with Hugging Face and PyTorch.
- The difference between pretraining, transfer learning and full fine-tuning.
- How adapters and LoRA make fine-tuning much cheaper.
- How quantization and QLoRA fit large models on small GPUs.
- Soft prompts, plus the ethics of fine-tuning large language models.
| Module | Main focus |
|---|---|
| Module 1 · about 4 hours | Transformers and Fine-Tuning: Hugging Face vs PyTorch, loading pretrained models, inference, full fine-tuning |
| Module 2 · about 4 hours | Parameter Efficient Fine-Tuning: adapters, LoRA, quantization, QLoRA, soft prompts, ethics |
Module 1: Transformers and Fine-Tuning
Transformers and Fine-Tuning: Hugging Face vs PyTorch, loading pretrained models, inference, full fine-tuning.
- Pretraining teaches a model to predict the next token on a very large text corpus. The result is a base model.
- Hugging Face Transformers is the high-level library:
AutoTokenizer,AutoModelForCausalLM,pipeline. PyTorch is the lower-level tensor and autograd engine under it. - Load a pretrained model with
from_pretrained. The tokenizer must match the model. - Inference steps: tokenize the text, run the model, get logits, pick a token, decode.
generate()repeats this in a loop. - Transfer learning reuses learned weights for a new task. You can freeze the base and train a new head.
- Full fine-tuning updates every weight. It needs a lot of GPU memory and can cause catastrophic forgetting.
- Keep padding, truncation and
max_lengththe same at training and inference time. - Use a small learning rate and a few epochs. Watch the evaluation loss: if it rises, the model is overfitting.
TrainerandTrainingArgumentsrun the training loop, the batching, the evaluation and the checkpoints.
Module 2: Parameter Efficient Fine-Tuning (PEFT)
Parameter Efficient Fine-Tuning: adapters, LoRA, quantization, QLoRA, soft prompts, ethics.
- PEFT trains a small number of extra parameters and keeps the base model frozen. Less memory, smaller files, less forgetting.
- Adapters are small bottleneck layers added inside the model. Only they are trained.
- LoRA adds two low-rank matrices to a frozen weight: ΔW = B·A. Only A and B are trained.
- The LoRA rank r sets the size and the capacity of those matrices. A bigger r learns more and uses more memory.
- LoRA scaling uses
alpha / r.target_moduleschooses which layers get an adapter. - QLoRA loads the base model in 4-bit (NF4) and trains LoRA adapters on top. This fits large models on small GPUs.
- Quantization stores weights in fewer bits. It saves memory but can cost a little quality.
- Soft prompts learn virtual tokens or a prefix. The model weights stay frozen.
- Ethics: a fine-tuned model can copy bias or private data from its dataset. Check the licence and report the limits. Some base models also have their own use restrictions.
Check yourself
Answer each question first, then open it to check the answer.
1What does a tokenizer do? a) It stores the model weights · b) It trains the model · c) It splits text into tokens and gives each token an id · d) It saves GPU memory
c) It splits text into tokens and gives each token an id. The tokenizer cuts the text into tokens and maps each one to a number.
2Which library is the high-level library for pretrained transformer models? a) Hugging Face Transformers · b) NumPy · c) FAISS · d) PyTorch
a) Hugging Face Transformers. Hugging Face gives ready classes for models and tokenizers; PyTorch is the lower-level engine.
3What is a base model? a) A model with no weights · b) A model trained on a large corpus to predict the next token · c) A small database · d) A type of tokenizer
b) A model trained on a large corpus to predict the next token. Pretraining on next-token prediction over a large corpus gives a base model.
4In full fine-tuning, how many weights change? a) Only the last layer · b) No weights · c) All the weights · d) Only the tokenizer
c) All the weights. Full fine-tuning updates all weights, so it needs much more memory.
5What is a risk of full fine-tuning on a small, narrow dataset? a) The tokenizer stops working · b) The GPU gets bigger · c) The labels disappear · d) The model may forget skills it learned before
d) The model may forget skills it learned before. Narrow training can overwrite old skills; this is catastrophic forgetting.
6What does PEFT mean? a) Training only a small set of extra parameters · b) Training every weight with more data · c) Removing the tokenizer · d) Pretraining a model from zero
a) Training only a small set of extra parameters. PEFT freezes most weights and trains a small number of new ones.
7In LoRA, which weights are trained? a) The frozen base matrix · b) The two low-rank matrices A and B · c) The tokenizer tables · d) The output labels
b) The two low-rank matrices A and B. LoRA freezes W and trains only the low-rank matrices A and B.
8What does the LoRA rank r control? a) The size of the low-rank matrices · b) The number of documents · c) The GPU brand · d) The number of epochs
a) The size of the low-rank matrices. The rank r fixes the inner size of A and B, so it sets their capacity.
9What is QLoRA? a) LoRA with no training · b) A type of prompt · c) A vector database · d) LoRA on a 4-bit quantized base model
d) LoRA on a 4-bit quantized base model. QLoRA keeps the base model in 4-bit and trains LoRA adapters in higher precision.
10Why is quantization used? a) To make the model bigger · b) To store weights in fewer bits and save memory · c) To clean the dataset · d) To split text
b) To store weights in fewer bits and save memory. Fewer bits per weight means a smaller model in memory.
11What is a soft prompt? a) A prompt written by a human · b) A short answer · c) Learnable virtual tokens added to the input while the model stays frozen · d) A saved checkpoint
c) Learnable virtual tokens added to the input while the model stays frozen. Soft prompts learn vectors or tokens; the model weights do not change.
12What is an adapter? a) A small bottleneck layer added to the model and trained · b) A large dataset · c) A tokenizer file · d) A type of loss
a) A small bottleneck layer added to the model and trained. Adapters add small trainable layers between existing layers.
13Why merge LoRA weights into the base model after training? a) To make the model file smaller · b) To change the tokenizer · c) To delete the adapters · d) To get one model with no extra inference cost
d) To get one model with no extra inference cost. Merging gives a normal model file with no adapter step at inference.
Study and exam tips
Know the order of inference: tokenize, model, logits, decode.
- Keep the three fine-tuning ideas apart: full fine-tuning, feature extraction, PEFT.
- Remember the LoRA formula ΔW = B·A and that only A and B are trained.
- Link QLoRA to two things: 4-bit base model plus LoRA adapters.
- Be ready to compare Hugging Face (easy, high level) with PyTorch (flexible, low level).