Download src/config.py from mlvar/tinystories-gpt-124m: direct link, hf CLI and curl.
- Browser
- Download file 4.05 kB
-
https://huggingface.co/mlvar/tinystories-gpt-124m/resolve/main/src/config.py
- Command line
-
hf download hf://mlvar/tinystories-gpt-124m/src/config.py
-
curl -L -o config.py https://huggingface.co/mlvar/tinystories-gpt-124m/resolve/main/src/config.py
4.05 kB
| """All hyperparameters in one place. Every other file imports from here.""" | |
| from dataclasses import dataclass, field | |
| class ModelConfig: | |
| """GPT-style decoder-only transformer architecture parameters. | |
| Defaults target ~124M parameters, matching the GPT-2 Small scale | |
| but with learned position embeddings instead of sinusoidal ones. | |
| """ | |
| # Vocabulary | |
| vocab_size: int = 50257 # GPT-2 BPE vocabulary size | |
| # Architecture | |
| d_model: int = 768 # Embedding dimension | |
| n_layers: int = 12 # Number of transformer blocks | |
| n_heads: int = 12 # Number of attention heads | |
| d_ff: int = 3072 # Feed-forward hidden dimension (4x d_model) | |
| max_seq_len: int = 1024 # Maximum context length | |
| dropout: float = 0.1 # Dropout rate | |
| bias: bool = True # Whether to use bias in linear layers (GPT-2 uses bias) | |
| # Derived | |
| d_head: int = field(init=False) | |
| def __post_init__(self): | |
| self.d_head = self.d_model // self.n_heads | |
| assert self.d_model % self.n_heads == 0, ( | |
| f"d_model ({self.d_model}) must be divisible by n_heads ({self.n_heads})" | |
| ) | |
| assert self.d_ff == 4 * self.d_model, ( | |
| f"Expected d_ff={4 * self.d_model} for standard 4x expansion, got {self.d_ff}" | |
| ) | |
| class TrainingConfig: | |
| """Training hyperparameters tuned for RTX 5080 16GB with bfloat16.""" | |
| # Batch | |
| micro_batch_size: int = 16 # Per forward pass | |
| grad_accum_steps: int = 4 # Gradient accumulation (effective batch = 16*4 = 64) | |
| seq_len: int = 1024 # Sequence length (must be <= ModelConfig.max_seq_len) | |
| # Optimizer | |
| peak_lr: float = 3e-4 # Peak learning rate after warmup | |
| min_lr: float = 1e-5 # Minimum LR at end of cosine decay | |
| weight_decay: float = 0.1 # AdamW weight decay (applied to non-bias, non-norm params) | |
| beta1: float = 0.9 # AdamW beta1 | |
| beta2: float = 0.95 # AdamW beta2 | |
| epsilon: float = 1e-8 # AdamW epsilon | |
| # Schedule | |
| max_steps: int = 50000 # Total optimizer steps | |
| warmup_steps: int = 500 # Linear warmup duration | |
| grad_clip: float = 1.0 # Global norm clipping threshold | |
| # Logging and checkpointing | |
| log_interval: int = 100 # Log training loss every N steps | |
| eval_interval: int = 500 # Evaluate on validation set every N steps | |
| eval_iters: int = 50 # Number of batches for validation estimate | |
| checkpoint_interval: int = 100 # Save checkpoint every N steps | |
| generate_interval: int = 1000 # Generate sample text every N steps | |
| max_checkpoints: int = 5 # Keep only the N most recent checkpoints | |
| # Hardware | |
| dtype: str = "bfloat16" # Mixed precision dtype | |
| num_workers: int = 0 # DataLoader workers (0 avoids Windows multiprocessing issues) | |
| seed: int = 42 # Random seed | |
| def effective_batch_size(self) -> int: | |
| return self.micro_batch_size * self.grad_accum_steps | |
| class Config: | |
| """Top-level config aggregating all sub-configs.""" | |
| model: ModelConfig = field(default_factory=ModelConfig) | |
| training: TrainingConfig = field(default_factory=TrainingConfig) | |
| # Paths | |
| data_dir: str = "data/tinystories" | |
| checkpoint_dir: str = "checkpoints" | |
| # Pre-built configs for testing / debugging | |
| def tiny_test_config() -> Config: | |
| """Tiny model for overfit testing. ~2.6M params.""" | |
| config = Config() | |
| config.model.vocab_size = 50304 # Rounded for efficiency | |
| config.model.d_model = 256 | |
| config.model.n_layers = 4 | |
| config.model.n_heads = 4 | |
| config.model.d_ff = 1024 | |
| config.model.d_head = 64 | |
| config.model.max_seq_len = 256 | |
| config.model.dropout = 0.0 | |
| config.training.seq_len = 256 | |
| config.training.micro_batch_size = 4 | |
| config.training.grad_accum_steps = 1 | |
| config.training.max_steps = 200 | |
| config.training.warmup_steps = 10 | |
| config.training.eval_interval = 50 | |
| config.training.log_interval = 10 | |
| config.training.checkpoint_interval = 0 # Disabled | |
| config.training.generate_interval = 0 # Disabled | |
| return config | |