File size: 6,495 Bytes
5a46e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from __future__ import annotations

import json
from pathlib import Path

import pytest
import torch
from safetensors.torch import save_model
from tokenizers import Tokenizer
from tokenizers.models import WordLevel
from tokenizers.pre_tokenizers import Whitespace

from barunlm import BarunConfig, BarunLM
from barunlm.evaluation.generation import (
    INT8_GENERATION_VERSION,
    GenerationError,
    generate_manifest,
)
from barunlm.quantization import export_dynamic_int8_checkpoint
from barunlm.training.data import EXAMPLE_SCHEMA_VERSION, sha256_file


def _tokenizer() -> Tokenizer:
    vocabulary = {
        "<pad>": 0,
        "<unk>": 1,
        "<eos>": 2,
        "prompt": 3,
        "answer": 4,
    }
    tokenizer = Tokenizer(WordLevel(vocab=vocabulary, unk_token="<unk>"))
    tokenizer.pre_tokenizer = Whitespace()
    return tokenizer


def _checkpoint(path: Path) -> dict[str, str]:
    config = BarunConfig(
        vocab_size=5,
        dim=8,
        n_layers=1,
        n_heads=2,
        n_kv_heads=1,
        ffn_dim=16,
        max_seq_len=8,
        rope_fraction=0.5,
        local_window=4,
        full_attention_every=1,
        attention_gate=False,
        residual_select_every=0,
        tie_embeddings=False,
    )
    model = BarunLM(config)
    with torch.no_grad():
        for parameter in model.parameters():
            parameter.zero_()
    path.mkdir()
    save_model(model, path / "model.safetensors")
    config.save_json(path / "barun_config.json")
    _tokenizer().save(str(path / "tokenizer.json"))
    return {
        name: sha256_file(path / name)
        for name in ("model.safetensors", "barun_config.json", "tokenizer.json")
    }


def test_generation_preserves_raw_special_tokens_and_records_truncation(tmp_path: Path) -> None:
    checkpoint = tmp_path / "checkpoint"
    hashes = _checkpoint(checkpoint)
    manifest = tmp_path / "dev.jsonl"
    manifest.write_text(
        json.dumps(
            {
                "schema_version": EXAMPLE_SCHEMA_VERSION,
                "id": "one",
                "prompt": "prompt",
                "target": "answer",
                "metadata": {},
            }
        )
        + "\n",
        encoding="utf-8",
    )
    output = tmp_path / "predictions.jsonl"

    summary = generate_manifest(
        checkpoint_dir=checkpoint,
        manifest_path=manifest,
        manifest_sha256=sha256_file(manifest),
        predictions_path=output,
        device_name="cpu",
        batch_size=1,
        max_new_tokens=2,
        expected_checkpoint_sha256=hashes,
    )

    record = json.loads(output.read_text(encoding="utf-8"))
    assert record["prediction_raw"] == "<pad> <pad>"
    assert record["truncated"] is True
    assert record["generation_failure"] is None
    assert summary.generated == 1
    assert summary.truncated == 1
    assert summary.predictions_sha256 == sha256_file(output)
    assert "checkpoint_format" not in summary.to_dict()
    assert "quantization_manifest_sha256" not in summary.to_dict()


def _qengine() -> str:
    supported = tuple(
        engine for engine in torch.backends.quantized.supported_engines if engine != "none"
    )
    if not supported:
        raise AssertionError("the CPU test environment has no quantized engine")
    return "qnnpack" if "qnnpack" in supported else supported[0]


@pytest.mark.filterwarnings("ignore:torch.ao.quantization is deprecated:DeprecationWarning")
@pytest.mark.filterwarnings("ignore:torch.quantize_per_tensor.*:UserWarning")
@pytest.mark.filterwarnings("ignore:TypedStorage is deprecated:UserWarning")
def test_generation_loads_explicit_int8_without_float_fallback(tmp_path: Path) -> None:
    source = tmp_path / "source"
    hashes = _checkpoint(source)
    int8 = tmp_path / "int8"
    exported = export_dynamic_int8_checkpoint(
        source,
        int8,
        expected_source_sha256=hashes,
        qengine=_qengine(),
    )
    manifest = tmp_path / "dev.jsonl"
    manifest.write_text(
        json.dumps(
            {
                "schema_version": EXAMPLE_SCHEMA_VERSION,
                "id": "one",
                "prompt": "prompt",
                "target": "answer",
                "metadata": {},
            }
        )
        + "\n",
        encoding="utf-8",
    )

    summary = generate_manifest(
        checkpoint_dir=int8,
        checkpoint_format="int8",
        expected_int8_manifest_sha256=exported.manifest_sha256,
        manifest_path=manifest,
        manifest_sha256=sha256_file(manifest),
        predictions_path=tmp_path / "predictions.jsonl",
        device_name="cpu",
        batch_size=1,
        max_new_tokens=2,
    )

    receipt = summary.to_dict()
    assert summary.schema_version == INT8_GENERATION_VERSION
    assert receipt["checkpoint_format"] == "int8"
    assert receipt["quantization_manifest_sha256"] == exported.manifest_sha256
    assert receipt["checkpoint_sha256"] == dict(exported.artifact_sha256)
    assert receipt["source_checkpoint_sha256"] == hashes
    assert receipt["qengine"] == exported.qengine
    assert receipt["runtime"]["torch_version"] == str(torch.__version__)


@pytest.mark.parametrize(
    ("checkpoint_format", "device", "float_hashes", "int8_hash", "message"),
    [
        ("int8", "cuda", None, "a" * 64, "requires device_name='cpu'"),
        ("int8", "cpu", None, None, "requires an expected_int8_manifest"),
        ("int8", "cpu", {"model.safetensors": "a" * 64}, "a" * 64, "only with"),
        ("float", "cpu", None, "a" * 64, "only with"),
        ("automatic", "cpu", None, None, "explicitly 'float' or 'int8'"),
    ],
)
def test_generation_rejects_incompatible_checkpoint_arguments_before_writing(
    tmp_path: Path,
    checkpoint_format: str,
    device: str,
    float_hashes: dict[str, str] | None,
    int8_hash: str | None,
    message: str,
) -> None:
    predictions = tmp_path / "nested" / "predictions.jsonl"
    with pytest.raises(GenerationError, match=message):
        generate_manifest(
            checkpoint_dir=tmp_path / "missing-checkpoint",
            checkpoint_format=checkpoint_format,
            expected_checkpoint_sha256=float_hashes,
            expected_int8_manifest_sha256=int8_hash,
            manifest_path=tmp_path / "missing-manifest",
            manifest_sha256="b" * 64,
            predictions_path=predictions,
            device_name=device,
            batch_size=1,
            max_new_tokens=1,
        )
    assert not predictions.parent.exists()