File size: 24,328 Bytes
dc72977
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
from dataclasses import replace
import hashlib
import json
from pathlib import Path
import sys
from types import SimpleNamespace

import pytest
import yaml

import dlb.adapters.capture as capture_module
from dlb.adapters.base import AdapterError
from dlb.adapters.candi import CANDIAdapter
from dlb.adapters.duo import DuoAdapter
from dlb.adapters.flm import FLMAdapter
from dlb.adapters.mdlm import MDLMAdapter
from dlb.command import main as command_main
from dlb.checkpoints import load_checkpoint_manifest
from dlb.registry import load_registry
from dlb.runner import RunRequest, _resolve_checkpoint_provenance


ROOT = Path(__file__).parents[1]


def request(model: str, dataset: str, steps: int = 32, samples: int = 17) -> RunRequest:
    return RunRequest(
        run_id=f"{model}-{dataset}-steps-{steps}",
        model_id=model,
        dataset_id=dataset,
        step_count=steps,
        seed=7,
        sample_count=samples,
    )


def run_dir(root: Path, item: RunRequest) -> Path:
    return (
        root
        / "results"
        / "samples"
        / item.dataset_id
        / item.model_id
        / f"steps_{item.step_count}"
    )


def override(command: list[str], key: str) -> str:
    prefix = key + "="
    values = [argument[len(prefix) :] for argument in command if argument.startswith(prefix)]
    assert len(values) == 1, (key, command)
    return values[0]


def test_hf_masked_lm_adapter_only_truncates_duo_extra_vocab_class() -> None:
    """Catch Duo's HF checkpoint leaking its non-tokenizer runtime class into sampling."""

    class FakeLogits:
        def __init__(self, last_dim: int):
            self.shape = (2, 3, last_dim)

        def __getitem__(self, key):
            assert key == (Ellipsis, slice(None, 50_257, None))
            return FakeLogits(50_257)

    class FakeMaskedLM:
        def __init__(self, *, model_type: str, bare_logits: bool = False):
            self.config = SimpleNamespace(model_type=model_type, vocab_size=50_258)
            self.bare_logits = bare_logits

        def forward(self, input_ids, timesteps):
            del input_ids, timesteps
            logits = FakeLogits(50_258)
            if self.bare_logits:
                return logits
            return SimpleNamespace(logits=logits)

    duo = capture_module._adapt_hf_masked_lm_backbone(
        FakeMaskedLM(model_type="DUO")
    )
    duo_bare = capture_module._adapt_hf_masked_lm_backbone(
        FakeMaskedLM(model_type="DUO", bare_logits=True)
    )
    other = capture_module._adapt_hf_masked_lm_backbone(
        FakeMaskedLM(model_type="MDLM")
    )

    duo_logits = duo(
        x=[[0, 0, 0], [0, 0, 0]],
        sigma=[1, 1],
    )
    duo_bare_logits = duo_bare(
        x=[[0, 0, 0], [0, 0, 0]],
        sigma=[1, 1],
    )
    other_logits = other(
        x=[[0, 0, 0], [0, 0, 0]],
        sigma=[1, 1],
    )

    assert duo_logits.shape == (2, 3, 50_257)
    assert duo_bare_logits.shape == (2, 3, 50_257)
    assert other_logits.shape == (2, 3, 50_258)


def test_duo_sampler_vocab_uses_canonical_gpt2_size_when_tokenizer_adds_pad() -> None:
    owner = SimpleNamespace(
        vocab_size=50_258,
        backbone=SimpleNamespace(
            config=SimpleNamespace(model_type="DUO", vocab_size=50_258)
        ),
    )

    assert capture_module._duo_canonical_sampler_vocab_size(owner) == 50_257

    owner.vocab_size = 50_257
    assert capture_module._duo_canonical_sampler_vocab_size(owner) is None


def prepare_conversion_root(tmp_path: Path) -> Path:
    (tmp_path / "artifacts").mkdir()
    (tmp_path / "configs").mkdir()
    (tmp_path / "configs" / "experiments.yaml").write_text(
        (ROOT / "configs" / "experiments.yaml").read_text(encoding="utf-8"),
        encoding="utf-8",
    )
    (tmp_path / "artifacts" / "data.yaml").write_text(
        (ROOT / "artifacts" / "data.yaml").read_text(encoding="utf-8"),
        encoding="utf-8",
    )
    (tmp_path / "artifacts" / "checkpoints.yaml").write_text(
        (ROOT / "artifacts" / "checkpoints.yaml").read_text(encoding="utf-8"),
        encoding="utf-8",
    )
    return tmp_path


def canonical_conversion_request(root: Path, item: RunRequest) -> RunRequest:
    registry = load_registry(root / "configs" / "experiments.yaml")
    manifest_model = load_checkpoint_manifest(root / "artifacts" / "checkpoints.yaml")
    support = registry.models[item.model_id].datasets[item.dataset_id]
    manifest = root / "artifacts" / "checkpoints.yaml"
    manifest_sha256 = hashlib.sha256(manifest.read_bytes()).hexdigest()
    if support.train_recipe:
        recipe = manifest_model.recipes[support.train_recipe]
        checkpoint = root / recipe.output / str(recipe.sampling_checkpoint)
        checkpoint.parent.mkdir(parents=True, exist_ok=True)
        checkpoint.write_bytes(b"ckp")
        provenance = _resolve_checkpoint_provenance(root, item, support.train_recipe)
        return replace(
            item,
            checkpoint_sha256=provenance.sha256,
            checkpoint_lock_id=provenance.lock_id,
            checkpoint_selection=provenance.selection,
            checkpoint_teacher_family=provenance.teacher_family,
        )

    coverage = manifest_model.coverage[(item.model_id, item.dataset_id)]
    resource = coverage.resource
    selected_path = coverage.path or "model.safetensors"
    files = [
        {
            "path": f"checkpoints/fixture/{resource}/{selected_path}",
            "size_bytes": 3,
            "sha256": "b" * 64,
        }
    ]
    (root / "artifacts" / "checkpoint_lock.json").write_text(
        json.dumps(
            {
                "manifest_sha256": manifest_sha256,
                "resources": {resource: {"status": "downloaded", "files": files}},
            }
        ),
        encoding="utf-8",
    )
    provenance = _resolve_checkpoint_provenance(root, item, support.train_recipe)
    return replace(
        item,
        checkpoint_sha256=provenance.sha256,
        checkpoint_lock_id=provenance.lock_id,
        checkpoint_selection=provenance.selection,
        checkpoint_teacher_family=provenance.teacher_family,
    )


def write_standard_output(path: Path, texts: list[str]) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(
        json.dumps({"generative_ppl": 1.0, "entropy": 2.0, "generated_seqs": texts}),
        encoding="utf-8",
    )


def write_capture(
    path: Path, count: int, token_length: int, *, duplicate_id: bool = False
) -> None:
    samples = [
        {
            "sample_id": 0 if duplicate_id and index == 1 else index,
            "text": f"sample {index}",
            "token_ids": [index % 100 + 1] * token_length,
        }
        for index in range(count)
    ]
    path.write_text(
        json.dumps({"schema": "dlb-upstream-token-capture-v1", "samples": samples}),
        encoding="utf-8",
    )


@pytest.mark.parametrize(
    ("adapter", "model", "upstream"),
    [
        (FLMAdapter(), "flm", "flm"),
        (DuoAdapter(), "duo", "duo"),
        (MDLMAdapter(), "mdlm", "mdlm"),
        (CANDIAdapter(), "candi", "candi"),
    ],
)
def test_commands_reference_real_pinned_entrypoints_and_actual_hydra_keys(
    adapter, model: str, upstream: str
) -> None:
    """Catch adapters drifting to a nonexistent entrypoint or invented override."""

    item = request(model, "owt")
    command = adapter.render_command(item, run_dir(ROOT, item), dry_run=True)
    entrypoint = Path(override(command, "--upstream-entrypoint"))

    assert entrypoint == ROOT / "upstreams" / upstream / "main.py"
    assert entrypoint.is_file()
    config = yaml.safe_load((entrypoint.parent / "configs" / "config.yaml").read_text())
    data_config = override(command, "data")
    assert (entrypoint.parent / "configs" / "data" / f"{data_config}.yaml").is_file()
    for key in ("sampling.steps", "sampling.num_sample_batches", "loader.eval_batch_size"):
        section, child = key.split(".")
        assert child in config[section]
        assert override(command, key)


def test_flm_and_fmlm_render_exact_paper_sampling_semantics() -> None:
    """Catch FLM losing Euler or FMLM losing its dataset-specific paper gamma."""

    flm = request("flm", "owt")
    fmlm_lm1b = request("fmlm", "lm1b", steps=8)
    fmlm_owt = request("fmlm", "owt", steps=8)

    flm_command = FLMAdapter().render_command(flm, run_dir(ROOT, flm), dry_run=True)
    lm1b_command = FLMAdapter().render_command(
        fmlm_lm1b, run_dir(ROOT, fmlm_lm1b), dry_run=True
    )
    owt_command = FLMAdapter().render_command(
        fmlm_owt, run_dir(ROOT, fmlm_owt), dry_run=True
    )

    assert override(flm_command, "sampling.solver") == "euler"
    assert override(flm_command, "sampling.steps") == "32"
    assert override(lm1b_command, "algo") == "fmlm"
    assert override(lm1b_command, "sampling.gamma") == "0.8"
    assert override(owt_command, "sampling.gamma") == "1.0"


def test_flm_family_commands_use_official_lightning_checkpoints(tmp_path: Path) -> None:
    """Catch FLM/FMLM official ckpts being routed through the HF AutoModel branch."""

    cells = (
        ("flm", "lm1b", "checkpoints/official/flm_ckpt/lm1b/lm1b_flm.ckpt"),
        ("flm", "owt", "checkpoints/official/flm_ckpt/owt/owt_flm.ckpt"),
        ("fmlm", "lm1b", "checkpoints/official/fmlm_ckpt/lm1b/lm1b_fmlm.ckpt"),
        ("fmlm", "owt", "checkpoints/official/fmlm_ckpt/owt/owt_fmlm.ckpt"),
    )
    adapter = FLMAdapter()
    root = prepare_conversion_root(tmp_path)
    (root / "upstreams" / "flm").mkdir(parents=True)
    (root / "upstreams" / "flm" / "main.py").write_text("# fixture\n", encoding="utf-8")

    for model, dataset, checkpoint in cells:
        item = request(model, dataset)
        command = adapter.render_command(item, run_dir(root, item), dry_run=True)

        assert override(command, "algo.backbone") == "dit"
        assert Path(override(command, "eval.checkpoint_path")) == root / checkpoint


@pytest.mark.parametrize(
    ("adapter", "model", "sampler_key", "sampler_value", "noise_value"),
    [
        (DuoAdapter(), "duo", "sampling.predictor", "ancestral", "ancestral"),
        (DuoAdapter(), "duo_dcd", "sampling.predictor", "ancestral", "ancestral"),
        (MDLMAdapter(), "mdlm", "sampling.predictor", "ddpm", "True"),
    ],
)
def test_discrete_commands_use_actual_ancestral_keys_without_invented_temperature(
    adapter, model: str, sampler_key: str, sampler_value: str, noise_value: str
) -> None:
    """Catch a non-ancestral sampler or a Hydra override absent from pinned configs."""

    item = request(model, "owt", steps=8)
    command = adapter.render_command(item, run_dir(ROOT, item), dry_run=True)

    assert override(command, sampler_key) == sampler_value
    assert override(command, "sampling.noise_removal") == noise_value
    assert not any(argument.startswith("sampling.temperature=") for argument in command)


def test_candi_command_uses_the_pinned_hybrid_sampler() -> None:
    """Catch CANDI being reduced to a discrete-only or continuous-only sampler."""

    item = request("candi", "owt", steps=16)
    command = CANDIAdapter().render_command(item, run_dir(ROOT, item), dry_run=True)

    assert override(command, "algo") == "candi"
    assert override(command, "algo.sampler") == "cached"
    assert override(command, "algo.mixed_coeff") == "0.5"
    assert override(command, "algo.step_size") == "1.0"
    assert override(command, "algo.temp") == "1.0"
    assert override(command, "sampling.steps") == "16"


def test_commands_accept_smoke_results_root_under_project_results(tmp_path: Path) -> None:
    """Catch adapters rejecting runner requests with --results-root results/smoke."""

    root = prepare_conversion_root(tmp_path)
    (root / "upstreams" / "candi").mkdir(parents=True)
    (root / "upstreams" / "candi" / "main.py").write_text("# fixture\n", encoding="utf-8")
    results_root = root / "results" / "smoke"
    item = replace(request("candi", "lm1b", steps=1, samples=1), results_root=str(results_root))
    output_dir = (
        results_root
        / "samples"
        / item.dataset_id
        / item.model_id
        / f"steps_{item.step_count}"
    )

    command = CANDIAdapter().render_command(item, output_dir, dry_run=True)

    assert Path(override(command, "--capture-path")).parent == output_dir


@pytest.mark.parametrize(
    ("adapter", "model", "dataset", "batch_size"),
    [
        (FLMAdapter(), "flm", "lm1b", 32),
        (FLMAdapter(), "fmlm", "owt", 16),
        (DuoAdapter(), "duo", "owt", 8),
        (MDLMAdapter(), "mdlm", "owt", 16),
        (CANDIAdapter(), "candi", "owt", 2),
    ],
)
def test_sample_batches_are_ceiled_from_the_configured_eval_batch(
    adapter, model: str, dataset: str, batch_size: int
) -> None:
    """Catch floor division silently generating fewer samples than requested."""

    item = request(model, dataset, samples=batch_size + 1)
    command = adapter.render_command(item, run_dir(ROOT, item), dry_run=True)

    assert override(command, "loader.eval_batch_size") == str(batch_size)
    assert override(command, "sampling.num_sample_batches") == "2"
    assert override(command, "trainer.devices") == "1"


def test_rendered_commands_have_only_concrete_nonempty_arguments() -> None:
    """Catch unresolved templates or sentinel paths reaching subprocess argv."""

    adapters = {
        "flm": FLMAdapter(),
        "fmlm": FLMAdapter(),
        "duo": DuoAdapter(),
        "duo_dcd": DuoAdapter(),
        "mdlm": MDLMAdapter(),
        "candi": CANDIAdapter(),
    }
    for model, adapter in adapters.items():
        for dataset in ("lm1b", "owt"):
            item = request(model, dataset)
            command = adapter.render_command(item, run_dir(ROOT, item), dry_run=True)
            entrypoint = Path(override(command, "--upstream-entrypoint"))
            data_config = override(command, "data")
            assert (entrypoint.parent / "configs" / "data" / f"{data_config}.yaml").is_file()
            assert command
            assert all(argument and "${" not in argument for argument in command)
            assert all("{" not in argument and "}" not in argument for argument in command)
            assert all("None" not in argument for argument in command)


def test_real_build_rejects_a_missing_checkpoint() -> None:
    """Catch non-dry execution silently relying on a future or wrong checkpoint."""

    item = request("flm", "owt")
    with pytest.raises(AdapterError, match="checkpoint"):
        FLMAdapter().build_command(item, run_dir(ROOT, item))


def test_conversion_prefers_captured_token_ids_and_trims_only_ceiling_excess(
    tmp_path: Path,
) -> None:
    """Catch re-tokenization despite IDs being available or nondeterministic trimming."""

    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("flm", "owt", samples=17))
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)
    texts = [f"sample {index}" for index in range(32)]
    write_standard_output(output_dir / "upstream_samples.json", texts)
    write_capture(output_dir / "upstream_token_ids.json", 32, 1024)

    records = list(FLMAdapter().convert_outputs(item, output_dir))

    assert len(records) == 17
    assert [record.sample_id for record in records] == list(range(17))
    assert records[1].token_ids == [2] * 1024
    metadata = json.loads((output_dir / "conversion_metadata.json").read_text())
    assert metadata["token_ids_source"] == "upstream"
    assert metadata["trimmed_samples"] == 15
    assert metadata["generation_seconds_source"] == "unavailable_excluded_sentinel"
    assert metadata["checkpoint_sha256"] == item.checkpoint_sha256
    assert metadata["checkpoint_lock_id"] == item.checkpoint_lock_id
    assert metadata["checkpoint_selection"] == item.checkpoint_selection
    assert metadata["teacher_family"] == "continuous_flm"
    assert all(record.generation_seconds == 0.0 for record in records)


def test_conversion_rejects_missing_runner_checkpoint_provenance_before_reading_outputs(
    tmp_path: Path,
) -> None:
    """Catch direct conversion bypassing the runner's canonical checkpoint resolution."""

    root = prepare_conversion_root(tmp_path)
    item = request("flm", "owt", samples=1)
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)

    with pytest.raises(AdapterError, match="runner-resolved checkpoint provenance is required"):
        list(FLMAdapter().convert_outputs(item, output_dir))


def test_conversion_rejects_wrong_teacher_family_before_reading_outputs(
    tmp_path: Path,
) -> None:
    """Catch a valid checkpoint identity being relabeled as another teacher family."""

    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("flm", "owt", samples=1))
    item = replace(item, checkpoint_teacher_family="continuous_rdlm")
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)

    with pytest.raises(AdapterError, match="teacher family"):
        list(FLMAdapter().convert_outputs(item, output_dir))


def test_conversion_rejects_extra_missing_empty_duplicate_and_invalid_samples(
    tmp_path: Path,
) -> None:
    """Catch malformed upstream artifacts being normalized into plausible samples."""

    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("flm", "owt", samples=17))
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)
    write_standard_output(output_dir / "upstream_samples.json", [f"sample {i}" for i in range(33)])
    write_capture(output_dir / "upstream_token_ids.json", 33, 1024)
    with pytest.raises(AdapterError, match="expected 32 generated samples, found 33"):
        list(FLMAdapter().convert_outputs(item, output_dir))

    write_standard_output(output_dir / "upstream_samples.json", [f"sample {i}" for i in range(32)])
    write_capture(output_dir / "upstream_token_ids.json", 32, 1024, duplicate_id=True)
    with pytest.raises(AdapterError, match="duplicate sample_id"):
        list(FLMAdapter().convert_outputs(item, output_dir))

    write_capture(output_dir / "upstream_token_ids.json", 32, 1024)
    capture = json.loads((output_dir / "upstream_token_ids.json").read_text())
    capture["samples"][0]["token_ids"] = [50257] + [1] * 1023
    (output_dir / "upstream_token_ids.json").write_text(json.dumps(capture))
    with pytest.raises(AdapterError, match="invalid token"):
        list(FLMAdapter().convert_outputs(item, output_dir))

    write_capture(output_dir / "upstream_token_ids.json", 32, 1024)
    actual = json.loads((output_dir / "upstream_samples.json").read_text())
    actual["generated_seqs"][0] = "   "
    (output_dir / "upstream_samples.json").write_text(json.dumps(actual))
    with pytest.raises(AdapterError, match="empty text"):
        list(FLMAdapter().convert_outputs(item, output_dir))


def test_mdlm_conversion_accepts_only_the_documented_capture_format(tmp_path: Path) -> None:
    """Catch parsing MDLM's lossy final-batch stdout representation."""

    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("mdlm", "lm1b", samples=17))
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)
    write_capture(output_dir / "upstream_token_ids.json", 32, 128)

    records = list(MDLMAdapter().convert_outputs(item, output_dir))
    assert len(records) == 17
    assert records[0].token_ids == [1] * 128

    (output_dir / "upstream_token_ids.json").write_text(json.dumps(["not", "supported"]))
    with pytest.raises(AdapterError, match="unexpected capture format"):
        list(MDLMAdapter().convert_outputs(item, output_dir))


@pytest.mark.parametrize(
    ("dataset", "length", "batch_size"), [("lm1b", 128, 32), ("owt", 1024, 16)]
)
def test_conversion_requires_exact_canonical_token_length_for_each_dataset(
    tmp_path: Path, dataset: str, length: int, batch_size: int
) -> None:
    """Catch short or long token sequences passing canonical conversion validation."""

    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("flm", dataset, samples=1))
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)
    texts = [f"sample {index}" for index in range(batch_size)]
    write_standard_output(output_dir / "upstream_samples.json", texts)

    write_capture(output_dir / "upstream_token_ids.json", batch_size, length)
    records = list(FLMAdapter().convert_outputs(item, output_dir))
    assert len(records[0].token_ids) == length

    write_capture(output_dir / "upstream_token_ids.json", batch_size, length - 1)
    with pytest.raises(AdapterError, match=f"expected {length} tokens"):
        list(FLMAdapter().convert_outputs(item, output_dir))

    write_capture(output_dir / "upstream_token_ids.json", batch_size, length + 1)
    with pytest.raises(AdapterError, match=f"expected {length} tokens"):
        list(FLMAdapter().convert_outputs(item, output_dir))


def test_text_only_conversion_pads_and_truncates_with_the_pinned_offline_tokenizer(
    tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
    """Catch text fallback emitting variable-length IDs or hiding its transformation."""

    class FakeTokenizer:
        eos_token = "<eos>"
        pad_token = None
        pad_token_id = 50256

        def __call__(self, texts, **settings):
            assert settings == {
                "add_special_tokens": False,
                "padding": "max_length",
                "truncation": True,
                "max_length": 1024,
                "return_attention_mask": False,
            }
            return {"input_ids": [[11] + [50256] * 1023 for _ in texts]}

    class FakeAutoTokenizer:
        @staticmethod
        def from_pretrained(name, **settings):
            assert name == "gpt2"
            assert settings == {
                "revision": "607a30d783dfa663caf39e06633721c8d4cfcd7e",
                "local_files_only": True,
            }
            return FakeTokenizer()

    monkeypatch.setitem(
        sys.modules, "transformers", SimpleNamespace(AutoTokenizer=FakeAutoTokenizer)
    )
    root = prepare_conversion_root(tmp_path)
    item = canonical_conversion_request(root, request("flm", "owt", samples=1))
    output_dir = run_dir(root, item)
    output_dir.mkdir(parents=True)
    write_standard_output(
        output_dir / "upstream_samples.json", [f"sample {index}" for index in range(16)]
    )

    records = list(FLMAdapter().convert_outputs(item, output_dir))
    metadata = json.loads((output_dir / "conversion_metadata.json").read_text())

    assert records[0].token_ids == [11] + [50256] * 1023
    assert metadata["token_ids_transformation"] == {
        "add_special_tokens": False,
        "max_length": 1024,
        "padding": "max_length",
        "pad_token_id": 50256,
        "truncation": True,
    }


def test_command_cli_dry_renders_six_models_on_both_datasets_without_writes(
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Catch dry-run omitting a cell or accidentally trying to acquire artifacts."""

    result = command_main(
        [
            "--root",
            str(ROOT),
            "--models",
            "flm,fmlm,duo,duo_dcd,mdlm,candi",
            "--datasets",
            "lm1b,owt",
            "--dry-run",
        ]
    )

    records = [json.loads(line) for line in capsys.readouterr().out.splitlines()]
    assert result == 0
    assert len(records) == 12
    assert {(record["model"], record["dataset"]) for record in records} == {
        (model, dataset)
        for model in ("flm", "fmlm", "duo", "duo_dcd", "mdlm", "candi")
        for dataset in ("lm1b", "owt")
    }
    assert all(record["status"] == "supported" and record["command"] for record in records)


def test_command_cli_emits_a_structured_unsupported_record(
    capsys: pytest.CaptureFixture[str],
) -> None:
    """Catch unsupported combinations terminating the whole render matrix."""

    assert command_main(["--root", str(ROOT), "--models", "rdlm", "--datasets", "owt", "--dry-run"]) == 0
    record = json.loads(capsys.readouterr().out)
    assert record["status"] == "unsupported"
    assert record["model"] == "rdlm"
    assert record["dataset"] == "owt"
    assert record["reason"]