|
| 1 | +# Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt). |
| 2 | +# Source for "Build a Large Language Model From Scratch" |
| 3 | +# - https://www.manning.com/books/build-a-large-language-model-from-scratch |
| 4 | +# Code: https://github.com/rasbt/LLMs-from-scratch |
| 5 | + |
| 6 | +import importlib |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +import pytest |
| 10 | +import torch |
| 11 | + |
| 12 | +from llms_from_scratch.utils import import_definitions_from_notebook |
| 13 | + |
| 14 | + |
| 15 | +transformers_installed = importlib.util.find_spec("transformers") is not None |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def nb_imports(): |
| 20 | + nb_dir = Path(__file__).resolve().parents[1] |
| 21 | + mod = import_definitions_from_notebook(nb_dir, "standalone-llama32.ipynb") |
| 22 | + return mod |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def dummy_input(): |
| 27 | + torch.manual_seed(123) |
| 28 | + return torch.randint(0, 100, (1, 8)) # batch size 1, seq length 8 |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def dummy_cfg_base(): |
| 33 | + return { |
| 34 | + "vocab_size": 100, |
| 35 | + "emb_dim": 32, # hidden_size |
| 36 | + "hidden_dim": 64, # intermediate_size (FFN) |
| 37 | + "n_layers": 2, |
| 38 | + "n_heads": 4, |
| 39 | + "head_dim": 8, |
| 40 | + "n_kv_groups": 1, |
| 41 | + "dtype": torch.float32, |
| 42 | + "rope_base": 500_000.0, |
| 43 | + "rope_freq": { |
| 44 | + "factor": 8.0, |
| 45 | + "low_freq_factor": 1.0, |
| 46 | + "high_freq_factor": 4.0, |
| 47 | + "original_context_length": 8192, |
| 48 | + }, |
| 49 | + "context_length": 64, |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | +@torch.inference_mode() |
| 54 | +def test_dummy_llama3_forward(dummy_cfg_base, dummy_input, nb_imports): |
| 55 | + torch.manual_seed(123) |
| 56 | + model = nb_imports.Llama3Model(dummy_cfg_base) |
| 57 | + out = model(dummy_input) |
| 58 | + assert out.shape == (1, dummy_input.size(1), dummy_cfg_base["vocab_size"]) |
| 59 | + |
| 60 | + |
| 61 | +@torch.inference_mode() |
| 62 | +@pytest.mark.skipif(not transformers_installed, reason="transformers not installed") |
| 63 | +def test_llama3_base_equivalence_with_transformers(nb_imports): |
| 64 | + from transformers.models.llama import LlamaConfig, LlamaForCausalLM |
| 65 | + cfg = { |
| 66 | + "vocab_size": 257, |
| 67 | + "context_length": 8192, |
| 68 | + "emb_dim": 32, |
| 69 | + "n_heads": 4, |
| 70 | + "n_layers": 2, |
| 71 | + "hidden_dim": 64, |
| 72 | + "n_kv_groups": 2, |
| 73 | + "rope_base": 500_000.0, |
| 74 | + "rope_freq": { |
| 75 | + "factor": 32.0, |
| 76 | + "low_freq_factor": 1.0, |
| 77 | + "high_freq_factor": 4.0, |
| 78 | + "original_context_length": 8192, |
| 79 | + }, |
| 80 | + "dtype": torch.float32, |
| 81 | + } |
| 82 | + |
| 83 | + ours = nb_imports.Llama3Model(cfg) |
| 84 | + |
| 85 | + hf_cfg = LlamaConfig( |
| 86 | + vocab_size=cfg["vocab_size"], |
| 87 | + hidden_size=cfg["emb_dim"], |
| 88 | + num_attention_heads=cfg["n_heads"], |
| 89 | + num_key_value_heads=cfg["n_kv_groups"], |
| 90 | + num_hidden_layers=cfg["n_layers"], |
| 91 | + intermediate_size=cfg["hidden_dim"], |
| 92 | + max_position_embeddings=cfg["context_length"], |
| 93 | + rms_norm_eps=1e-5, |
| 94 | + attention_bias=False, |
| 95 | + rope_theta=cfg["rope_base"], |
| 96 | + tie_word_embeddings=False, |
| 97 | + attn_implementation="eager", |
| 98 | + torch_dtype=torch.float32, |
| 99 | + rope_scaling={ |
| 100 | + "type": "llama3", |
| 101 | + "factor": cfg["rope_freq"]["factor"], |
| 102 | + "low_freq_factor": cfg["rope_freq"]["low_freq_factor"], |
| 103 | + "high_freq_factor": cfg["rope_freq"]["high_freq_factor"], |
| 104 | + "original_max_position_embeddings": cfg["rope_freq"]["original_context_length"], |
| 105 | + }, |
| 106 | + ) |
| 107 | + theirs = LlamaForCausalLM(hf_cfg) |
| 108 | + |
| 109 | + hf_state = theirs.state_dict() |
| 110 | + nb_imports.load_weights_into_llama(ours, {"n_layers": cfg["n_layers"], "hidden_dim": cfg["hidden_dim"]}, hf_state) |
| 111 | + |
| 112 | + x = torch.randint(0, cfg["vocab_size"], (2, 8), dtype=torch.long) |
| 113 | + ours_logits = ours(x) |
| 114 | + theirs_logits = theirs(x).logits.to(ours_logits.dtype) |
| 115 | + |
| 116 | + torch.testing.assert_close(ours_logits, theirs_logits, rtol=1e-5, atol=1e-5) |
0 commit comments