tests.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. # File for internal use (unit tests)
  6. import pytest
  7. from train import main
  8. @pytest.fixture
  9. def gpt_config():
  10. return {
  11. "vocab_size": 50257,
  12. "ctx_len": 12, # small for testing efficiency
  13. "emb_dim": 32, # small for testing efficiency
  14. "n_heads": 4, # small for testing efficiency
  15. "n_layers": 2, # small for testing efficiency
  16. "drop_rate": 0.1,
  17. "qkv_bias": False
  18. }
  19. @pytest.fixture
  20. def other_hparams():
  21. return {
  22. "learning_rate": 5e-4,
  23. "num_epochs": 1, # small for testing efficiency
  24. "batch_size": 2,
  25. "weight_decay": 0.1
  26. }
  27. def test_main(gpt_config, other_hparams):
  28. train_losses, val_losses, tokens_seen, model = main(gpt_config, other_hparams)
  29. assert len(train_losses) == 39, "Unexpected number of training losses"
  30. assert len(val_losses) == 39, "Unexpected number of validation losses"
  31. assert len(tokens_seen) == 39, "Unexpected number of tokens seen"