Scaling & Optimization
Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.
Setup
- Open a fresh terminal in a new folder, e.g.
mkdir lab-11 && cd lab-11. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
from transformers import BitsAndBytesConfig, AutoModelForCausalLM
import torch
bnb = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
)
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-3.1-8B", quantization_config=bnb, device_map="auto"
)python 11_01_lab.pyfrom transformers import AutoModelForCausalLM, AutoTokenizer
target = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B-Instruct", device_map="auto")
draft = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.2-1B-Instruct", device_map="auto")
tok = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B-Instruct")
ids = tok("Explain speculative decoding:", return_tensors="pt").to(target.device)
out = target.generate(**ids, max_new_tokens=200, assistant_model=draft)
print(tok.decode(out[0], skip_special_tokens=True))python 11_02_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1Quantize and compare
Do — Load the same model in fp16, int8, and nf4. Measure VRAM, latency, and MMLU score for each.
Expected artifact — quant.csv with rows fp16/int8/nf4 and columns vram_gb, ms_per_token, mmlu.
Run free: - 2Speculative decoding speedup
Do — Use a 1B draft model to accelerate an 8B target. Measure tokens/sec for both.
Expected artifact — ≥1.5× speedup on greedy decoding; tokens/sec table in spec.md.
Run free: - 3Distill a small student
Do — Distill an 8B teacher into a 1.5B student on 10k completions. Compare student-before vs student-after on 30 prompts.
Expected artifact — Win-rate of student-after over student-before ≥60%, reported in distill.md.
Run free:
Authoritative sources
Independent primary sources used to check and corroborate the guidance on this page.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
