A straightforward method for training your LLM, from downloading data to generating text.
fix(models): stop saving the classic model's causal masks in checkpoints
FareedKhan-devb7bc6032df99
diff --git a/src/checkpoint.py b/src/checkpoint.pyindex f2e804a..e5263c5 100644--- a/src/checkpoint.py+++ b/src/checkpoint.py@@ -78,9 +78,12 @@ def model_state_from_checkpoint("""Return the model weights of a checkpoint with wrapper prefixes removed.Accepts both our checkpoint dicts (weights under ``model_state_dict``) and raw state dicts.+ Older checkpoints of the classic model also stored every head's causal mask (``.tril``);+ the model rebuilds those masks itself now, so they are dropped here and old files still load."""state = checkpoint["model_state_dict"] if "model_state_dict" in checkpoint else checkpoint- return strip_wrapper_prefixes(state, extra_prefixes)+ cleaned = strip_wrapper_prefixes(state, extra_prefixes)+ return {k: v for k, v in cleaned.items() if not k.endswith(".tril")}def model_config_from_checkpoint(checkpoint: Mapping[str, Any]) -> dict[str, Any]:diff --git a/src/models/attention.py b/src/models/attention.pyindex 8a3abcb..31580b8 100644--- a/src/models/attention.py+++ b/src/models/attention.py@@ -31,8 +31,9 @@ class Head(nn.Module):self.key = nn.Linear(n_embed, head_size, bias=False) # Key projectionself.query = nn.Linear(n_embed, head_size, bias=False) # Query projectionself.value = nn.Linear(n_embed, head_size, bias=False) # Value projection- # Lower triangular matrix for causal masking- self.register_buffer('tril', torch.tril(torch.ones(context_length, context_length)))+ # Lower triangular matrix for causal masking. It is rebuilt here every time, so it is not+ # saved in checkpoints (persistent=False); each head has one, which adds up quickly.+ self.register_buffer('tril', torch.tril(torch.ones(context_length, context_length)), persistent=False)def forward(self, x: torch.Tensor) -> torch.Tensor:"""diff --git a/tests/test_checkpoint_io.py b/tests/test_checkpoint_io.pyindex 4fc67aa..369fab4 100644--- a/tests/test_checkpoint_io.py+++ b/tests/test_checkpoint_io.py@@ -19,6 +19,7 @@ from config.post_training_config import SFTConfig, smokefrom src.checkpoint import (load_checkpoint,load_model_weights,+ model_state_from_checkpoint,strip_wrapper_prefixes,unwrap_model,)@@ -112,6 +113,19 @@ def test_legacy_checkpoint_dims_are_read_from_config(tmp_path: str) -> None:assert _weights_equal(load_model_from_ckpt(path, "cpu"), model)+def test_causal_masks_are_not_saved_and_old_checkpoints_with_them_still_load(tmp_path: str) -> None:+ """Every classic head used to save its (context x context) mask: ~1.6 GiB in the 400M config."""+ model = Transformer(n_head=4, n_embed=32, context_length=64, vocab_size=50, N_BLOCKS=2)+ assert not any(k.endswith(".tril") for k in model.state_dict())+ masks = {f"attn_blocks.{b}.attn.heads.{h}.tril": torch.tril(torch.ones(64, 64)) for b in range(2) for h in range(4)}+ path = os.path.join(tmp_path, "old_format.pt")+ torch.save({"model_state_dict": {**model.state_dict(), **masks}}, path)++ fresh = Transformer(n_head=4, n_embed=32, context_length=64, vocab_size=50, N_BLOCKS=2)+ fresh.load_state_dict(model_state_from_checkpoint(load_checkpoint(path))) # strict, like resume does+ assert _weights_equal(fresh, model)++def test_missing_parameters_fail_loudly(cfg: SFTConfig) -> None:model = build_model_from_config(cfg)partial = {k: v for k, v in model.state_dict().items() if not k.startswith("lm_head")}