File size: 12,601 Bytes
70ac094
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import json
import sys
from copy import deepcopy
from types import ModuleType, SimpleNamespace

import pytest

from scripts.build_quantized import (
    CalibrationArtifactError,
    DatasetSpec,
    _strip_revision_from_shell_model,
    artifact_sha256,
    build_awq,
    build_gptq,
    create_calibration_artifact,
    create_calibration_artifact_from_stream,
    token_ids_sha256,
    validate_calibration_artifact,
    verify_stream_row_count,
    write_calibration_receipt,
)


SPEC = DatasetSpec("fixture", "fixture/repo", "default", "train", "abc123", 6)


class ToyDataset(list):
    _fingerprint = "fixture-fingerprint"


class ToyTokenizer:
    name_or_path = "fixture/model"
    vocab_size = 128

    def __call__(self, text, **kwargs):
        values = [ord(character) for character in text]
        if kwargs.get("truncation") and kwargs.get("max_length") is not None:
            values = values[: kwargs["max_length"]]
        return {"input_ids": values, "attention_mask": [1] * len(values)}

    def get_vocab(self):
        return {chr(index): index for index in range(128)}

    def save_pretrained(self, path):
        return path


def _artifact(seed=0):
    dataset = ToyDataset(
        [
            {"text": "x"},
            {"text": "abcdefgh"},
            {"text": "ijklmnop"},
            {"text": "y"},
            {"text": "qrstuvwx"},
            {"text": "ABCDEFGH"},
        ]
    )
    return create_calibration_artifact(
        dataset,
        ToyTokenizer(),
        model_id="fixture/model",
        model_revision="model123",
        dataset_spec=SPEC,
        seed=seed,
        size=2,
        sequence_length=4,
    )


def test_calibration_artifact_is_deterministic_and_skips_short_documents():
    one = _artifact(seed=0)
    two = _artifact(seed=0)
    other = _artifact(seed=1)
    assert one == two
    assert one["artifact_sha256"] == artifact_sha256(one)
    assert one["selected_document_indices"] == [2, 5]
    assert one["skipped_short_document_count"] == 1
    assert other["selected_document_indices"] == [4, 2]
    assert one["artifact_sha256"] != other["artifact_sha256"]
    assert all(len(sample["input_ids"]) == 4 for sample in one["samples"])
    assert one["selected_token_hashes"] == [
        token_ids_sha256(sample["input_ids"]) for sample in one["samples"]
    ]


def test_stream_retrieval_preserves_exact_registered_permutation_order():
    dataset = ToyDataset(
        [
            {"text": "x"},
            {"text": "abcdefgh"},
            {"text": "ijklmnop"},
            {"text": "y"},
            {"text": "qrstuvwx"},
            {"text": "ABCDEFGH"},
        ]
    )
    indexed = create_calibration_artifact(
        dataset,
        ToyTokenizer(),
        model_id="fixture/model",
        model_revision="model123",
        dataset_spec=SPEC,
        seed=0,
        size=2,
        sequence_length=4,
    )
    streamed = create_calibration_artifact_from_stream(
        lambda: iter(dataset),
        ToyTokenizer(),
        model_id="fixture/model",
        model_revision="model123",
        dataset_spec=SPEC,
        seed=0,
        size=2,
        sequence_length=4,
        retrieval_window=2,
    )
    assert streamed["selected_document_indices"] == indexed["selected_document_indices"]
    assert streamed["selected_token_hashes"] == indexed["selected_token_hashes"]
    assert [sample["input_ids"] for sample in streamed["samples"]] == [
        sample["input_ids"] for sample in indexed["samples"]
    ]
    assert streamed["retrieval"] == {
        "strategy": "sequential-stream-index-retrieval",
        "window_size": 2,
        "passes": 2,
        "stream_rows_scanned": 10,
    }


def test_calibration_artifact_fails_when_too_few_documents_are_eligible():
    with pytest.raises(CalibrationArtifactError, match="only 0"):
        create_calibration_artifact(
            ToyDataset([{"text": "a"}, {"text": "bc"}]),
            ToyTokenizer(),
            model_id="fixture/model",
            model_revision="model123",
            dataset_spec=SPEC,
            seed=0,
            size=1,
            sequence_length=4,
        )


def test_calibration_validation_rejects_tampering():
    artifact = _artifact()
    tampered = deepcopy(artifact)
    tampered["samples"][0]["input_ids"][0] += 1
    with pytest.raises(CalibrationArtifactError, match="token hash"):
        validate_calibration_artifact(
            tampered,
            tokenizer=ToyTokenizer(),
            model_id="fixture/model",
            model_revision="model123",
            dataset_spec=SPEC,
            seed=0,
            expected_size=2,
            expected_sequence_length=4,
        )


def test_gptq_and_awq_builders_consume_the_same_artifact(monkeypatch, tmp_path):
    calls = {}
    artifact = _artifact()

    class Config:
        def __init__(self, **kwargs):
            calls["config"] = kwargs

    class GPTQModel:
        @classmethod
        def load(cls, model_id, config, **kwargs):
            calls["gptq_load"] = (model_id, kwargs)
            return cls()

        def quantize(self, examples, **kwargs):
            calls["gptq_quantize"] = (examples, kwargs)

        def save(self, path):
            calls["gptq_save"] = path

    class AWQModel:
        @classmethod
        def from_pretrained(cls, model_id, **kwargs):
            calls["awq_load"] = (model_id, kwargs)
            return cls()

        def quantize(self, tokenizer, **kwargs):
            calls["awq_quantize"] = kwargs

        def save_quantized(self, path):
            calls["awq_save"] = path

    # build_gptq imports gptqmodel.utils.hf to install the revision shim; provide
    # a fake submodule so the from-import resolves against the mocked package.
    fake_hf = ModuleType("gptqmodel.utils.hf")
    fake_hf.build_shell_model = lambda loader, config, trust_remote_code=True, **kw: None
    fake_utils = ModuleType("gptqmodel.utils")
    fake_utils.hf = fake_hf
    monkeypatch.setitem(sys.modules, "gptqmodel", SimpleNamespace(GPTQConfig=Config, GPTQModel=GPTQModel))
    monkeypatch.setitem(sys.modules, "gptqmodel.utils", fake_utils)
    monkeypatch.setitem(sys.modules, "gptqmodel.utils.hf", fake_hf)
    monkeypatch.setitem(sys.modules, "awq", SimpleNamespace(AutoAWQForCausalLM=AWQModel))
    common = {
        "bits": 4,
        "model_id": "fixture/model",
        "model_revision": "model123",
        "trust_remote_code": True,
    }
    gptq_args = SimpleNamespace(**common, method="gptq", output_dir=tmp_path / "gptq")
    awq_args = SimpleNamespace(**common, method="awq", output_dir=tmp_path / "awq")
    tokenizer = ToyTokenizer()

    build_gptq(gptq_args, tokenizer, artifact)
    build_awq(awq_args, tokenizer, artifact)

    expected_ids = [sample["input_ids"] for sample in artifact["samples"]]
    assert [example["input_ids"] for example in calls["gptq_quantize"][0]] == expected_ids
    assert calls["awq_quantize"]["calib_data"] == expected_ids
    assert calls["awq_quantize"]["max_calib_samples"] == 2
    assert calls["awq_quantize"]["max_calib_seq_len"] == 4
    assert calls["config"] == {"bits": 4, "group_size": 128, "desc_act": False}
    assert calls["gptq_load"] == (
        "fixture/model",
        {"revision": "model123", "trust_remote_code": True},
    )
    assert calls["awq_load"] == (
        "fixture/model",
        {"revision": "model123", "trust_remote_code": True},
    )


def test_revision_shim_strips_only_revision_from_shell_model():
    received = {}

    def spy(loader, config, trust_remote_code=True, **kwargs):
        received["loader"] = loader
        received["config"] = config
        received["trust_remote_code"] = trust_remote_code
        received["kwargs"] = kwargs
        return "SHELL_MODEL"

    wrapped = _strip_revision_from_shell_model(spy)
    out = wrapped(
        "LOADER",
        "CONFIG",
        trust_remote_code=False,
        revision="pinnedrev",
        device_map=None,
        _fast_init=True,
        extra="untouched",
    )

    # Return value flows through, revision is dropped, every other kwarg and the
    # positional/keyword forwarding is byte-for-byte unchanged.
    assert out == "SHELL_MODEL"
    assert received["loader"] == "LOADER"
    assert received["config"] == "CONFIG"
    assert received["trust_remote_code"] is False
    assert received["kwargs"] == {"device_map": None, "_fast_init": True, "extra": "untouched"}
    assert "revision" not in received["kwargs"]
    assert wrapped._flipeval_strips_revision is True


def test_revision_shim_is_a_noop_when_revision_absent():
    received = {}

    def spy(loader, config, trust_remote_code=True, **kwargs):
        received["kwargs"] = kwargs
        return "SHELL_MODEL"

    wrapped = _strip_revision_from_shell_model(spy)
    out = wrapped("LOADER", "CONFIG", device_map=None, _fast_init=False)

    assert out == "SHELL_MODEL"
    assert received["kwargs"] == {"device_map": None, "_fast_init": False}


def test_checkpoint_receipts_prove_pairing(tmp_path):
    artifact = _artifact()
    common = {
        "bits": 4,
        "model_id": "fixture/model",
        "model_revision": "model123",
    }
    first = tmp_path / "gptq.json"
    second = tmp_path / "awq.json"
    write_calibration_receipt(first, artifact, SimpleNamespace(**common, method="gptq"))
    write_calibration_receipt(second, artifact, SimpleNamespace(**common, method="awq"))
    gptq = json.loads(first.read_text())
    awq = json.loads(second.read_text())
    assert gptq["artifact_sha256"] == awq["artifact_sha256"]
    assert gptq["selected_document_indices"] == awq["selected_document_indices"]
    assert gptq["selected_token_hashes"] == awq["selected_token_hashes"]


def test_verify_stream_row_count_accepts_exact_registered_count():
    rows = [{"text": str(index)} for index in range(SPEC.row_count)]
    assert verify_stream_row_count(lambda: iter(rows), SPEC) == SPEC.row_count


def test_verify_stream_row_count_fails_closed_on_understated_universe():
    rows = [{"text": str(index)} for index in range(SPEC.row_count - 1)]
    with pytest.raises(CalibrationArtifactError, match="registered row_count"):
        verify_stream_row_count(lambda: iter(rows), SPEC)


def test_verify_stream_row_count_fails_closed_on_overstated_universe():
    rows = [{"text": str(index)} for index in range(SPEC.row_count + 2)]
    with pytest.raises(CalibrationArtifactError, match="registered row_count"):
        verify_stream_row_count(lambda: iter(rows), SPEC)


def test_pinned_autoawq_preserves_pre_tokenized_calibration_ids():
    pytest.importorskip("awq")
    from awq.utils.calib_data import get_calib_dataset

    token_ids = [[1, 2, 3, 4], [5, 6, 7, 8]]
    blocks = get_calib_dataset(data=token_ids, n_samples=2, max_seq_len=4)
    assert [block.tolist()[0] for block in blocks] == token_ids


def test_pinned_gptqmodel_exposes_expected_api():
    pytest.importorskip("gptqmodel")
    from gptqmodel import GPTQConfig, GPTQModel

    assert GPTQConfig is not None
    assert GPTQModel is not None


def test_pinned_gptqmodel_builds_qwen2_shell_model_under_pinned_transformers():
    # Closes the gap the cell-2 canary exposed: importing gptqmodel succeeds,
    # but constructing the shell model through gptqmodel's own path leaks
    # `revision` into transformers>=5 from_config and dies. This is a real
    # CPU-side construction (weights stay on 'meta'; no GPU, no network).
    pytest.importorskip("gptqmodel")
    pytest.importorskip("transformers")
    from gptqmodel.utils.hf import build_shell_model
    from transformers import AutoModelForCausalLM, Qwen2Config

    config = Qwen2Config(
        vocab_size=256,
        hidden_size=64,
        intermediate_size=128,
        num_hidden_layers=2,
        num_attention_heads=4,
        num_key_value_heads=2,
        max_position_embeddings=128,
    )
    # build_shell_model unconditionally deletes device_map/_fast_init, so both
    # must be present; revision is the leaking hub-only kwarg.
    kwargs = dict(revision="pinnedrev", device_map=None, _fast_init=False)

    # The raw path still fails in exactly this shape under the pinned runtime.
    with pytest.raises(TypeError, match="revision"):
        build_shell_model(AutoModelForCausalLM, config=config, trust_remote_code=False, **dict(kwargs))

    # The shim recovers a clean construction on the same path.
    shimmed = _strip_revision_from_shell_model(build_shell_model)
    model = shimmed(AutoModelForCausalLM, config=config, trust_remote_code=False, **dict(kwargs))
    assert model.__class__.__name__ == "Qwen2ForCausalLM"