- Python 3.10+
- CUDA-capable GPU (recommended for Part 4, not needed for Part 1-3)
- Installation:
conda create -n cs288a2 python=3.10 -y
conda activate cs288a2
pip install -r requirements.txtRun tests from within each part's directory:
# Part 1: Tokenization
cd part1
python -m pytest tests/ -v
# Part 2: Transformer Model
cd part2
python -m pytest tests/ -v
# Part 3: NN Utilities
cd part3
python -m pytest tests/ -vOr run all tests from the source directory:
cd source
python -m pytest part1/tests/ part2/tests/ part3/tests/ -vAfter completing Parts 1-3, you can train and evaluate models for bonus points.
cd part4
python train_baseline.pyThis will:
- Train a BPE tokenizer on TinyStories
- Pretrain a transformer language model
- Fine-tune on multiple-choice QA
- Evaluate using zero-shot prompting
- Save predictions to
part4/outputs/
# Quick test run (smaller model, fewer steps)
python train_baseline.py --quick
# Medium configuration
python train_baseline.py --medium
# Full training (default)
python train_baseline.pyAfter training, prediction files are saved to part4/outputs/:
finetuned_predictions.json- Fine-tuned model predictionsprompting_predictions.json- Zero-shot prompting predictions
These files are required for Part 4 bonus points.
Create your submission zip file:
bash create_submission.sh- Fine-tuned model (12 pts): 30% accuracy = 0 pts, 50% = full pts (linear scale)
- Prompting model (8 pts): 0% boost = 0 pts, 2%+ boost over fine-tuned = full pts
train_bpe(): Train BPE vocabulary from text corpusTokenizer._bpe(): Apply BPE merges to a tokenTokenizer._encode_chunk(): Encode text to token IDsTokenizer.decode(): Decode token IDs to text
Linear: Linear transformation layerEmbedding: Token embedding layerRMSNorm: Root mean square layer normalizationsoftmax(): Numerically stable softmaxsilu(): SiLU activation functionSwiGLU: Gated feed-forward networkRotaryPositionEmbedding: RoPE positional encodingscaled_dot_product_attention(): Attention mechanismMultiHeadSelfAttention: Multi-head attentionMultiHeadSelfAttentionWithRoPE: Attention with RoPETransformerBlock: Single transformer layerTransformerLM: Complete language modelcount_flops_per_token(): FLOPs estimationestimate_memory_bytes(): Memory estimation
softmax(): Numerically stable softmax (for training)cross_entropy(): Cross-entropy lossgradient_clipping(): Gradient norm clippingtoken_accuracy(): Token-level accuracyperplexity(): Language model perplexity
- Do NOT modify function signatures or class interfaces
- Do NOT add dependencies beyond
requirements.txt - Ensure your code passes local tests before submitting
- The autograder runs additional hidden tests
- Use the provided fixtures for testing