LLM Developer DocsLLM Docs

Loading the search index ...

↑ ↓ move, Enter open, Esc close

GitHub

tests/test_checkpoint.py

Checkpoint tests

Test file

Where it fits

A test file. It is not part of the pipeline: neither train.main nor talk.main reaches it. It is listed with what it verifies on Checkpoints & Tests.

Imports and used by

Imports from this repository
checkpoint.py, tokenizer.py, transformer.py
Other imports
json, numpy, pytest
Imported by
No file
Used by, in the pipeline
No file
Used by, in tests
No file

Mathematics used

No link record points to this file.

Functions and constants

Fixture: saves a checkpoint and returns its parts

Used by, in the pipeline
Nothing
Used by, in tests
tests.test_checkpoint.test_config_round_trips, tests.test_checkpoint.test_loaded_model_gives_identical_logits, tests.test_checkpoint.test_save_creates_the_three_files, tests.test_checkpoint.test_tok_file_is_plain_json, tests.test_checkpoint.test_tokenizer_round_trips, tests.test_checkpoint.test_weights_round_trip_exactly
Calls
checkpoint.save_checkpoint, tokenizer.build_tokenizer, transformer.ModelConfig, transformer.init_params

Source

tests/test_checkpoint.py76 linesGitHub: tests/test_checkpoint.py
import jsonimport numpy as npimport pytestfrom checkpoint import save_checkpoint, load_checkpointfrom tokenizer import build_tokenizerfrom transformer import ModelConfig, init_params, forwardCORPUS = "low low low lower newest newest\nthe low newest tower\n"@pytest.fixturedef saved(tmp_path):    tok = build_tokenizer(CORPUS, max_merges=8)    cfg = ModelConfig(tok.vocab_size, context_len=8, emb_dim=4,                      num_heads=2, ff_dim=8, num_layers=2)    params = init_params(cfg, np.random.default_rng(0))    prefix = str(tmp_path / "ckpt" / "model")        # "ckpt" does not exist yet    save_checkpoint(prefix, params, cfg, tok)    return prefix, params, cfg, tokdef test_save_creates_the_three_files(saved):    prefix = saved[0]    for ext in (".npz", ".json", ".tok.json"):        assert open(prefix + ext, "rb").read()       # exists and is not emptydef test_config_round_trips(saved):    prefix, _, cfg, _ = saved    assert load_checkpoint(prefix)[1] == cfgdef test_weights_round_trip_exactly(saved):    prefix, params, _, _ = saved    loaded = load_checkpoint(prefix)[0]    assert loaded.keys() == params.keys()    for k in params:        assert np.array_equal(loaded[k], params[k]), kdef test_tokenizer_round_trips(saved):    prefix, _, _, tok = saved    t2 = load_checkpoint(prefix)[2]    assert t2.itos == tok.itos    assert t2.stoi == tok.stoi    assert t2.vocab_size == tok.vocab_size    for s in ["low newest", "lower tower", "  low  ", "low!"]:   # "!" was never seen        assert t2.encode(s) == tok.encode(s), s        assert t2.decode(t2.encode(s)) == tok.decode(tok.encode(s)), s    assert t2.encode("low!")[-1] == 0                # unseen character is still <unk>def test_loaded_model_gives_identical_logits(saved):    prefix, params, cfg, tok = saved    p2, c2, t2 = load_checkpoint(prefix)    ids = np.array(tok.encode("low newest tower"))    a, _ = forward(params, ids, cfg)    b, _ = forward(p2, ids, c2)    assert np.array_equal(a, b)def test_non_ascii_characters_survive(tmp_path):    tok = build_tokenizer("café café über\n", max_merges=3)    cfg = ModelConfig(tok.vocab_size, 8, 4, 2, 8, 1)    prefix = str(tmp_path / "m")    save_checkpoint(prefix, init_params(cfg, np.random.default_rng(0)), cfg, tok)    t2 = load_checkpoint(prefix)[2]    assert t2.decode(t2.encode("café über")) == "café über"def test_tok_file_is_plain_json(saved):    d = json.load(open(saved[0] + ".tok.json", encoding="utf-8"))    assert set(d) == {"merges", "itos"}    assert d["itos"][0] == "<unk>"