File size: 34,786 Bytes
d5b20f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
"""Build fixed, family-separated decisions from pinned public sources.

The benchmark's converters are an external, revision-pinned dependency. This
module selects records, checks separation, and records their provenance.
"""

import argparse
from collections import Counter, defaultdict
from collections.abc import Iterator
from dataclasses import dataclass
import hashlib
import importlib
import json
import math
from pathlib import Path
import random
import re
import string
import sys
import tarfile
from typing import Protocol, cast
import zipfile

import httpx
from PIL import Image, ImageDraw

from autojev.events import record
from autojev.types import Content, Example, JSONValue, Label, Question, Target

NIMBLE_REVISION = "d2387fc0b32d1173bfc995395c076a25a2a107c9"
SEED = 20260920


@dataclass(frozen=True)
class Source:
    repository: str
    revision: str
    config: str | None = None


SOURCES = {
    "boolq": Source("google/boolq", "35b264d03638db9f4ce671b711558bf7ff0f80d5"),
    "squad2": Source("rajpurkar/squad_v2", "3ffb306f725f7d2ce8394bc1873b24868140c412"),
    "paws": Source("google-research-datasets/paws", "161ece9501cf0a11f3e48bd356eaa82de46d6a09", "labeled_final"),
    "civil_comments": Source("google/civil_comments", "f2970eb3a55777454c94069077cc8d9b5866312d"),
    "aegis2": Source("nvidia/Aegis-AI-Content-Safety-Dataset-2.0", "d86bb8bedff51d25ac834ab7838f1cc61acb7a2c"),
    "pubmedqa": Source("qiaojin/PubMedQA", "9001f2853fb87cab8d220904e0de81ac6973b318", "pqa_labeled"),
}
ARCHIVES = {
    "vitaminc": ("https://github.com/TalSchuster/talschuster.github.io/raw/master/static/vitaminc.zip", "vitaminc.zip"),
    "multinli": ("https://cims.nyu.edu/~sbowman/multinli/multinli_1.0.zip", "multinli_1.0.zip"),
    "massive": ("https://amazon-massive-nlu-dataset.s3.amazonaws.com/amazon-massive-dataset-1.1.tar.gz", "massive-1.1.tar.gz"),
}
ARCHIVE_SHA256 = {
    "vitaminc": "49d82dc1690cbee420d18e2c26f687a7937710bb211845d2571430dfd4dc0337",
    "multinli": "049f507b9e36b1fcb756cfd5aeb3b7a0cfcb84bf023793652987f7e7e0957822",
    "massive": "4cba5faa11c71437928e17cb1b9b3d8b8e727e7ea363a3a9a8045e19c0491577",
}
TASKS = ("vitaminc-dev", "massive-en-US", "massive-de-DE", "boolq", "squad2",
         "paws", "multinli", "civil_comments", "aegis2", "pubmedqa")
TRAIN_QUOTAS = dict(zip(TASKS, (16000, 10000, 10000, 7000, 14000, 12000, 12000, 8000, 6000, 0)))
TEST_QUOTAS = dict(zip(TASKS, (920, 540, 540, 460, 460, 385, 460, 460, 385, 385)))
DEV_QUOTAS = dict(zip(TASKS, (275, 160, 160, 140, 140, 115, 140, 140, 115, 115)))
TEMPERATURE_QUOTAS = dict(zip(TASKS, (185, 110, 110, 90, 90, 75, 90, 90, 75, 75)))
KNOWN_SPLITS = {"vitaminc": "dev", "massive": "test", "boolq": "validation",
                "squad2": "validation", "paws": "test", "multinli": "dev_matched",
                "civil_comments": "test", "aegis2": "test", "pubmedqa": "train"}
DEVELOPMENT_SPLITS = {**KNOWN_SPLITS, "massive": "dev", "paws": "validation",
                      "civil_comments": "validation", "aegis2": "validation"}
FINAL_SPLITS = {**KNOWN_SPLITS, "vitaminc": "test"}


class Converter(Protocol):
    SOURCE_URL: str
    LICENSE: str

    def record(self, raw: dict[str, JSONValue], subset: str = "") -> dict[str, JSONValue] | None: ...


class Serializer(Protocol):
    def jsonl_line(self, value: dict[str, JSONValue]) -> str: ...


class Arguments(argparse.Namespace):
    cache_root: Path
    source_cache: Path | None
    nimble_source: Path | None
    output: Path
    manifest: Path
    seed: int
    max_length: int
    train_scale: float
    image_train: int
    image_eval: int


def checksum(path: Path) -> str:
    with path.open("rb") as stream:
        return hashlib.file_digest(stream, "sha256").hexdigest()


def normalized(value: str) -> str:
    return " ".join(value.casefold().split())


def digest(value: str) -> str:
    return hashlib.sha256(value.encode()).hexdigest()


def ranking(value: str, seed: int) -> str:
    return digest(f"{seed}:{value}")


def download(url: str, path: Path) -> None:
    if path.exists():
        return
    path.parent.mkdir(parents=True, exist_ok=True)
    temporary = path.with_suffix(path.suffix + ".part")
    with httpx.stream("GET", url, follow_redirects=True, timeout=180) as response:
        response.raise_for_status()
        with temporary.open("wb") as stream:
            for chunk in response.iter_bytes(1024 * 1024):
                stream.write(chunk)
    temporary.replace(path)


def checkout(path: Path | None, raw: Path) -> Path:
    if path is not None:
        if (path / ".revision").read_text().strip() != NIMBLE_REVISION:
            raise ValueError("The supplied Nimble checkout is not the pinned revision")
        return path.resolve()
    path = raw / "nimble-source"
    if not path.exists():
        archive = raw / f"nimble-{NIMBLE_REVISION}.tar.gz"
        download(f"https://codeload.github.com/bespokelabsai/nimble/tar.gz/{NIMBLE_REVISION}", archive)
        with tarfile.open(archive) as bundle:
            bundle.extractall(raw, filter="data")
        (raw / f"nimble-{NIMBLE_REVISION}").rename(path)
        (path / ".revision").write_text(NIMBLE_REVISION + "\n")
    return path.resolve()


class RawData:
    def __init__(self, args: Arguments) -> None:
        self.cache = args.cache_root
        self.supplied = args.source_cache
        self.folder = args.output / "raw"
        self.files: dict[str, dict[str, str]] = {}

    def remember(self, path: Path, source: str) -> None:
        key = f"{source}/{path.name}"
        if key not in self.files:
            self.files[key] = {"name": path.name, "sha256": checksum(path)}

    def archive(self, source: str) -> Path:
        url, name = ARCHIVES[source]
        cached = self.supplied / name if self.supplied else self.folder / name
        if not cached.exists():
            cached = self.folder / name
            download(url, cached)
        self.remember(cached, source)
        if self.files[f"{source}/{cached.name}"]["sha256"] != ARCHIVE_SHA256[source]:
            raise ValueError(f"The {source} archive differs from the pinned checksum")
        return cached

    def rows(self, source: str, split: str, locale: str = "") -> Iterator[dict[str, JSONValue]]:
        if source == "massive":
            with tarfile.open(self.archive(source)) as archive:
                member = next(m for m in archive.getmembers() if m.name.endswith(f"/data/{locale}.jsonl"))
                stream = archive.extractfile(member)
                assert stream is not None
                for line in stream:
                    row = cast(dict[str, JSONValue], json.loads(line))
                    if row["partition"] == split:
                        yield row
            return
        if source in ("vitaminc", "multinli"):
            member_name = f"vitaminc/{split}.jsonl" if source == "vitaminc" else f"multinli_1.0/multinli_1.0_{split}.jsonl"
            with zipfile.ZipFile(self.archive(source)) as zip_archive, zip_archive.open(member_name) as zip_stream:
                for line in zip_stream:
                    yield cast(dict[str, JSONValue], json.loads(line))
            return
        cached = self.supplied / source / f"{split}.jsonl" if self.supplied else self.folder / source / f"{split}.jsonl"
        if cached.exists():
            self.remember(cached, source)
            with cached.open() as text_stream:
                for index, text_line in enumerate(text_stream):
                    row = cast(dict[str, JSONValue], json.loads(text_line))
                    if source == "civil_comments":
                        row["row_index"] = index
                    yield row
            return
        from datasets import Dataset, load_dataset

        spec = SOURCES[source]
        paths = sorted((self.cache / "datasets").glob(f"**/{spec.revision}/*-{split}*.arrow"))
        datasets: list[Dataset]
        if paths:
            datasets = [Dataset.from_file(str(path)) for path in paths]
            for path in paths:
                self.remember(path, source)
        else:
            datasets = [cast(Dataset, load_dataset(spec.repository, spec.config, split=split,
                                                   revision=spec.revision, cache_dir=str(self.folder / "hf")))]
        index = 0
        for dataset in datasets:
            for value in dataset:
                row = cast(dict[str, JSONValue], value)
                if source == "civil_comments":
                    row["row_index"] = index
                index += 1
                yield row


class Families:
    """Conservative PAWS groups: shared sentences or identical token bags connect."""

    def __init__(self, raw: RawData) -> None:
        self.parents: dict[str, str] = {}
        self.paws: dict[tuple[str, str], str] = {}
        for split in ("train", "validation", "test"):
            for row in raw.rows("paws", split):
                values: list[str] = []
                for key in ("sentence1", "sentence2"):
                    text = normalized(str(row[key]))
                    values.extend((digest(text), digest(" ".join(sorted(re.findall(r"\w+", text))))))
                roots = [self.find(value) for value in values]
                root = min(roots)
                for other in roots:
                    self.parents[other] = root
                self.paws[split, str(row["id"])] = root

    def find(self, value: str) -> str:
        self.parents.setdefault(value, value)
        while value != self.parents[value]:
            self.parents[value] = self.parents[self.parents[value]]
            value = self.parents[value]
        return value

    def key(self, source: str, split: str, raw: dict[str, JSONValue]) -> str:
        if source == "paws":
            return self.find(self.paws[split, str(raw["id"])])
        field = {"massive": "id", "vitaminc": "case_id", "multinli": "promptID", "pubmedqa": "pubid"}.get(source)
        if field:
            return str(raw[field])
        field = {"boolq": "passage", "squad2": "context", "aegis2": "prompt", "civil_comments": "text"}[source]
        return digest(normalized(str(raw[field])))


def task_source(task: str) -> tuple[str, str]:
    if task.startswith("massive-"):
        return "massive", task.removeprefix("massive-")
    return ("vitaminc", "") if task == "vitaminc-dev" else (task, "")


def convert(raw: dict[str, JSONValue], task: str, split: str, converter: Converter,
            families: Families) -> tuple[Example, dict[str, JSONValue]] | None:
    source, locale = task_source(task)
    value = converter.record(raw | {"partition": "test"} if source == "massive" else raw, locale)
    if value is None:
        return None
    model_input = cast(dict[str, JSONValue], value["input"])
    questions = cast(dict[str, Question], model_input["questions"])
    question = questions["decision"]
    reference = cast(dict[str, JSONValue], value["reference"])
    label = cast(Label, reference["target"])
    target: Target = label
    distribution = reference.get("distribution")
    if split == "train" and source != "pubmedqa" and isinstance(distribution, dict):
        if question["type"] == "noul":
            target = float(cast(float, distribution["true"]))
        elif question["type"] == "choice":
            target = [float(cast(float, distribution[key])) for key in question["criteria"]]
    provenance: dict[str, JSONValue] = {
        "dataset": source, "split": split, "upstream_id": value["id"], "url": converter.SOURCE_URL,
        "license": converter.LICENSE, "nimble_revision": NIMBLE_REVISION,
    }
    if source in SOURCES:
        provenance["revision"] = SOURCES[source].revision
    if distribution is not None:
        provenance["human_distribution"] = distribution
    identifier = f"{task}:{split}:{value['id']}"
    if split == "train" and source != "pubmedqa":
        # Some upstream training IDs name several distinct sentence pairs.
        identifier += ":" + digest(json.dumps(model_input["state"], sort_keys=True, ensure_ascii=False))[:16]
    result: Example = {
        "id": identifier, "suite": task,
        "family": families.key(source, split, raw), "state": cast(Content, model_input["state"]),
        "question": question, "target": target, "label": label, "source": provenance,
    }
    return result, value


def choose(rows: list[Example], quota: int, seed: int, excluded: set[str] | None = None) -> list[Example]:
    groups: dict[str, list[Example]] = defaultdict(list)
    for row in rows:
        if excluded is None or row["family"] not in excluded:
            groups[row["family"]].append(row)
    selected: list[Example] = []
    for family in sorted(groups, key=lambda key: ranking(key, seed)):
        group = groups[family]
        if len(selected) + len(group) <= quota:
            selected.extend(sorted(group, key=lambda row: row["id"]))
    return selected


def text_parts(row: Example) -> list[str]:
    state = row["state"]
    if isinstance(state, str):
        return [state]
    if isinstance(state, dict):
        return [value for value in state.values() if isinstance(value, str)]
    return [json.dumps(state, sort_keys=True)]


def state_key(row: Example) -> str:
    return digest(normalized(json.dumps(row["state"], sort_keys=True, ensure_ascii=False)))


class NearDuplicates:
    """Candidate shingle index followed by exact Jaccard checks; no label access."""

    def __init__(self) -> None:
        self.parts: list[set[str]] = []
        self.index: dict[str, list[int]] = defaultdict(list)

    @staticmethod
    def shingles(text: str) -> set[str]:
        words = re.findall(r"\w+", normalized(text))
        return {" ".join(words[index:index + 5]) for index in range(len(words) - 4)} if len(words) >= 20 else set()

    def add(self, row: Example) -> None:
        for text in text_parts(row):
            values = self.shingles(text)
            if values:
                index = len(self.parts)
                self.parts.append(values)
                for anchor in sorted(values, key=digest)[:8]:
                    self.index[anchor].append(index)

    def matches(self, row: Example) -> bool:
        for text in text_parts(row):
            values = self.shingles(text)
            if not values:
                continue
            candidates = {index for anchor in sorted(values, key=digest)[:8] for index in self.index.get(anchor, [])}
            for index in candidates:
                other = self.parts[index]
                if len(values & other) / len(values | other) >= 0.85:
                    return True
        return False


def write_rows(path: Path, rows: list[Example]) -> str:
    payload = "".join(json.dumps(row, ensure_ascii=True, allow_nan=False) + "\n" for row in rows)
    if path.exists() and path.read_text() != payload:
        raise FileExistsError(f"Refusing to replace a different frozen dataset: {path}")
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(payload)
    return checksum(path)


def images(count: int, split: str, seed: int, output: Path) -> list[Example]:
    rng = random.Random(seed)
    colors = {"red": "#df3030", "blue": "#3068da", "green": "#259951", "orange": "#ed911d"}
    shapes = ("circle", "square", "triangle")
    rows: list[Example] = []
    directory = output / "images" / split
    directory.mkdir(parents=True, exist_ok=True)
    for scene in range(math.ceil(count / 4)):
        picture = Image.new("RGB", (224, 224), "#f9f6ee")
        drawing = ImageDraw.Draw(picture)
        objects: list[tuple[str, str]] = []
        for x in (55, 169):
            color, shape = rng.choice(list(colors)), rng.choice(shapes)
            y, radius = rng.randint(85, 135), rng.randint(25, 40)
            box = (x - radius, y - radius, x + radius, y + radius)
            if shape == "circle":
                drawing.ellipse(box, fill=colors[color])
            elif shape == "square":
                drawing.rectangle(box, fill=colors[color])
            else:
                drawing.polygon(((x, y - radius), (x + radius, y + radius), (x - radius, y + radius)), fill=colors[color])
            objects.append((color, shape))
        image_path = directory / f"{scene:04}.png"
        picture.save(image_path)
        for side, (color, shape) in zip(("left", "right"), objects):
            for attribute, label, options in (("color", color, list(colors)), ("shape", shape, list(shapes))):
                rows.append({"id": f"images:{split}:{scene}:{side}:{attribute}", "suite": "image_retention",
                             "family": f"{split}:{scene}", "state": "Use the picture to answer the question.",
                             "images": [str(image_path)], "label": label, "target": label,
                             "question": {"type": "choice", "instructions": f"What is the {attribute} of the {side} object?",
                                          "criteria": dict.fromkeys(options)},
                             "source": {"dataset": "generated_geometry", "split": split,
                                        "seed": seed, "image_sha256": checksum(image_path), "license": "Apache-2.0"}})
    return rows[:count]


def validate(rows: list[Example]) -> None:
    identifiers: set[str] = set()
    for row in rows:
        if row["id"] in identifiers:
            raise ValueError(f"Duplicate example ID: {row['id']}")
        identifiers.add(row["id"])
        question, target, label = row["question"], row["target"], row["label"]
        if not row["family"]:
            raise ValueError("Missing family")
        if question["type"] == "choice":
            options = list(question["criteria"])
            if not isinstance(label, str) or label not in options:
                raise ValueError("Invalid Choice hard label")
            if isinstance(target, list):
                if len(target) != len(options) or abs(sum(target) - 1) > 1e-6 or any(not math.isfinite(p) or p < 0 for p in target):
                    raise ValueError("Invalid soft Choice target")
            elif target not in options:
                raise ValueError("Invalid Choice target")
        elif question["type"] == "noul":
            if type(label) is not bool or not isinstance(target, (bool, float)) or not 0 <= target <= 1:
                raise ValueError("Invalid Noul label/target")
        elif question["type"] == "score":  # team-1: ordered rubric, label = level index as a string ("0".."n-1")
            levels = [str(i) for i in range(len(question["criteria"]))]
            if not isinstance(label, str) or label not in levels:
                raise ValueError("Invalid Score hard label")
            if isinstance(target, list):
                if len(target) != len(levels) or abs(sum(target) - 1) > 1e-6 or any(not math.isfinite(p) or p < 0 for p in target):
                    raise ValueError("Invalid soft Score target")
            elif target not in levels:
                raise ValueError("Invalid Score target")
        else:
            raise ValueError("The accuracy experiment admits only Choice, Noul and Score examples")


def token_lengths(rows: list[Example], cache: Path) -> None:
    """Measure the exact shared prompt with the processor, without model weights."""
    from transformers import AutoProcessor
    from transformers.models.qwen3_vl.processing_qwen3_vl import Qwen3VLProcessor
    from autojev.model import BASE_MODEL, BASE_REVISION, decision_messages, open_image

    processor = cast(Qwen3VLProcessor, AutoProcessor.from_pretrained(
        BASE_MODEL, revision=BASE_REVISION, cache_dir=str(cache / "hub")))
    processor.image_processor.size = {"shortest_edge": 65536, "longest_edge": 262144}
    codes = [code for code in string.ascii_uppercase if len(processor.tokenizer.encode(code, add_special_tokens=False)) == 1]
    if len(codes) != 26:
        raise ValueError("The benchmark requires the model's first 26 answer codes")
    text_rows = [row for row in rows if not row.get("images")]
    for start in range(0, len(text_rows), 512):
        batch = text_rows[start:start + 512]
        texts = [processor.apply_chat_template(decision_messages(row, codes), tokenize=False,  # type: ignore[arg-type]
                                               add_generation_prompt=True, enable_thinking=False) for row in batch]
        encoded = cast(list[list[int]], processor.tokenizer(texts, padding=False, truncation=False)["input_ids"])
        for row, tokens in zip(batch, encoded):
            row["source"]["input_tokens"] = len(tokens)
    for row in rows:
        if not row.get("images"):
            continue
        text = processor.apply_chat_template(decision_messages(row, codes), tokenize=False,  # type: ignore[arg-type]
                                             add_generation_prompt=True, enable_thinking=False)
        encoded_images = processor(text=[text], images=[open_image(image) for image in row.get("images", [])])
        row["source"]["input_tokens"] = len(cast(list[list[int]], encoded_images["input_ids"])[0])


def build(args: Arguments) -> None:
    args.output.mkdir(parents=True, exist_ok=True)
    raw = RawData(args)
    dependency = checkout(args.nimble_source, raw.folder)
    sys.path.insert(0, str(dependency))
    serializer = cast(Serializer, importlib.import_module("nimble.datasets.public_benchmarks"))
    families = Families(raw)
    folds: dict[str, list[Example]] = {name: [] for name in ("known", "test", "dev", "temperature", "train")}
    report: dict[str, object] = {"seed": args.seed, "nimble_revision": NIMBLE_REVISION, "tasks": {}, "sources": {}}
    task_reports = cast(dict[str, object], report["tasks"])
    source_reports = cast(dict[str, object], report["sources"])
    protected: dict[str, set[str]] = defaultdict(set)
    record("data_started", seed=args.seed, tasks=list(TASKS))
    for task in TASKS:
        source, locale = task_source(task)
        converter = cast(Converter, importlib.import_module(f"nimble.datasets.public_sources.{source}"))
        published = json.loads((dependency / f"docs/assets/public-benchmarks/subsets/{task}-manifest.json").read_text())
        known_ids = set(published["ids"])
        pools: dict[str, list[Example]] = defaultdict(list)
        exact_known: list[dict[str, JSONValue]] = []
        stats: Counter[str] = Counter()
        for split in sorted({KNOWN_SPLITS[source], FINAL_SPLITS[source], DEVELOPMENT_SPLITS[source]}):
            for value in raw.rows(source, split, locale):
                result = convert(value, task, split, converter, families)
                if result is None:
                    stats["invalid_heldout_rows"] += 1
                    continue
                row, upstream = result
                protected[source].add(row["family"])
                if split == KNOWN_SPLITS[source] and upstream["id"] in known_ids:
                    folds["known"].append(row)
                    exact_known.append(upstream)
                pools[split].append(row)
            unique_ids: dict[str, Example] = {}
            for candidate in pools[split]:
                previous_row = unique_ids.get(candidate["id"])
                if previous_row is not None:
                    if previous_row["state"] != candidate["state"] or previous_row["label"] != candidate["label"]:
                        raise ValueError("Conflicting rows share an upstream example ID")
                    stats["duplicate_heldout_ids_removed"] += 1
                unique_ids.setdefault(candidate["id"], candidate)
            pools[split] = list(unique_ids.values())
        known_payload = "".join(serializer.jsonl_line(row) for row in sorted(exact_known, key=lambda row: str(row["id"])))
        if digest(known_payload) != published["dataset_sha256"]:
            raise ValueError(f"Known benchmark content changed for {task}")
        # Stronger grouping can connect known rows to extra sibling examples.
        excluded = {row["family"] for row in folds["known"] if row["source"]["dataset"] == source}
        for fold, quota, split in (("test", TEST_QUOTAS[task], FINAL_SPLITS[source]),
                                   ("dev", DEV_QUOTAS[task], DEVELOPMENT_SPLITS[source]),
                                   ("temperature", TEMPERATURE_QUOTAS[task], DEVELOPMENT_SPLITS[source])):
            # MASSIVE locales must receive exactly the same family IDs.
            if locale == "de-DE":
                wanted = {row["family"] for row in folds[fold] if row["suite"] == "massive-en-US"}
                selected = [row for row in pools[split] if row["family"] in wanted]
                if len(selected) != len(wanted):
                    raise ValueError("A MASSIVE translation is missing")
            else:
                selected = choose(pools[split], quota, args.seed + {"test": 1, "dev": 2, "temperature": 3}[fold], excluded)
            folds[fold].extend(selected)
            excluded.update(row["family"] for row in selected)
            stats[fold] = len(selected)
        stats["known"] = len(exact_known)
        source_reports[source] = {"url": converter.SOURCE_URL, "license": converter.LICENSE,
                                   "revision": SOURCES[source].revision if source in SOURCES else None,
                                   "converter_sha256": checksum(dependency / f"nimble/datasets/public_sources/{source}.py")}
        task_reports[task] = dict(stats)
        record("data_task_partitioned", task=task, counts=dict(stats))

    # Keep folds disjoint even when repeated text carries different upstream IDs.
    protected_states: set[str] = set()
    protected_near = NearDuplicates()
    for fold in ("known", "test", "dev", "temperature"):
        if fold != "known":
            bad_families = {(str(row["source"]["dataset"]), row["family"]) for row in folds[fold]
                            if state_key(row) in protected_states or protected_near.matches(row)}
            folds[fold] = [row for row in folds[fold] if (str(row["source"]["dataset"]), row["family"]) not in bad_families]
            report[f"{fold}_duplicate_families_removed"] = len(bad_families)
        for row in folds[fold]:
            protected_states.add(state_key(row))
            protected_near.add(row)

    token_lengths([row for name, rows in folds.items() if name != "train" for row in rows], args.cache_root)
    early_partitions: dict[str, object] = {}
    for name in ("known", "test", "dev", "temperature"):
        rows = folds[name]
        overlong = {(str(row["source"]["dataset"]), row["family"]) for row in rows
                    if cast(int, row["source"]["input_tokens"]) > args.max_length}
        if name == "known" and overlong:
            raise ValueError("The known benchmark exceeds the token limit; never silently change its records")
        folds[name] = [row for row in rows if (str(row["source"]["dataset"]), row["family"]) not in overlong]
        report[f"{name}_overlong_families_excluded"] = len(overlong)
        validate(folds[name])
        early_partitions[name] = {"rows": len(folds[name]), "sha256": write_rows(args.output / f"{name}.jsonl", folds[name]),
                                  "tasks": dict(Counter(row["suite"] for row in folds[name]))}
    args.manifest.parent.mkdir(parents=True, exist_ok=True)
    (args.manifest.parent / "evaluation-partitions.json").write_text(json.dumps(early_partitions, indent=2) + "\n")
    record("evaluation_data_frozen", partitions=early_partitions)

    # Training-family sampling is label-blind. Soft human annotations are targets
    # only for sources that actually supply them; teacher confidence is not used.
    for task in TASKS:
        quota = int(TRAIN_QUOTAS[task] * args.train_scale)
        if not quota:
            continue
        source, locale = task_source(task)
        converter = cast(Converter, importlib.import_module(f"nimble.datasets.public_sources.{source}"))
        candidates: list[Example] = []
        stats = Counter[str]()
        pool_fraction = min(1.0, quota * 5 / {"vitaminc": 370653, "multinli": 392702, "civil_comments": 1804874}.get(source, 1))
        for value in raw.rows(source, "train", locale):
            family = families.key(source, "train", value)
            if family in protected[source]:
                stats["heldout_family_rows_excluded"] += 1
                continue
            if int(ranking(family, args.seed), 16) / 2**256 >= pool_fraction:
                continue
            result = convert(value, task, "train", converter, families)
            if result is None:
                stats["invalid_training_rows"] += 1
                continue
            row, _ = result
            candidates.append(row)
        bad = {row["family"] for row in candidates
               if state_key(row) in protected_states or protected_near.matches(row)}
        candidates = [row for row in candidates if row["family"] not in bad]
        unique: dict[str, Example] = {}
        conflicts: set[str] = set()
        for row in candidates:
            key = state_key(row)
            if key in unique and unique[key]["label"] != row["label"]:
                conflicts.add(key)
            unique.setdefault(key, row)
        candidates = [row for key, row in unique.items() if key not in conflicts]
        selected = choose(candidates, quota, args.seed + 4)
        if len(selected) < 0.8 * quota:
            raise ValueError(f"Too few training rows for {task}: {len(selected)} / {quota}")
        folds["train"].extend(selected)
        stats.update({"train": len(selected), "near_duplicate_families_excluded": len(bad),
                      "conflicting_state_labels_excluded": len(conflicts)})
        cast(dict[str, object], task_reports[task]).update(dict(stats))
        record("data_training_task", task=task, counts=dict(stats))

    folds["train"].extend(images(args.image_train, "train", args.seed + 100, args.output))
    folds["image_test"] = images(args.image_eval, "test", args.seed + 101, args.output)
    token_lengths(folds["train"] + folds["image_test"], args.cache_root)
    for name in ("train", "image_test"):
        rows = folds[name]
        overlong = {(str(row["source"]["dataset"]), row["family"]) for row in rows
                    if cast(int, row["source"]["input_tokens"]) > args.max_length}
        if name == "known" and overlong:
            raise ValueError("The known benchmark exceeds the model token limit; never silently change its records")
        folds[name] = [row for row in rows if (str(row["source"]["dataset"]), row["family"]) not in overlong]
        report[f"{name}_overlong_families_excluded"] = len(overlong)
    random.Random(args.seed).shuffle(folds["train"])
    partitions: dict[str, object] = {}
    previous: dict[tuple[str, str], str] = {}
    for name, rows in folds.items():
        validate(rows)
        # Known contains fixed upstream rows; sibling locales are one family.
        keys = {(str(row["source"]["dataset"]), row["family"]) for row in rows}
        for family_key in keys:
            if family_key in previous:
                raise ValueError(f"Family crossed {previous[family_key]} and {name}")
            previous[family_key] = name
        path = args.output / f"{name}.jsonl"
        partitions[name] = {"rows": len(rows), "families": len(keys), "sha256": write_rows(path, rows),
                            "tasks": dict(Counter(row["suite"] for row in rows)),
                            "tokens": sum(cast(int, row["source"]["input_tokens"]) for row in rows),
                            "maximum_input_tokens": max(cast(int, row["source"]["input_tokens"]) for row in rows),
                            "question_types": dict(Counter(row["question"]["type"] for row in rows))}
        identifiers = [{"id": row["id"], "dataset": row["source"]["dataset"], "family": row["family"]} for row in rows]
        identity_path = args.manifest.parent / "splits" / f"{name}.json"
        identity_path.parent.mkdir(parents=True, exist_ok=True)
        identity_path.write_text(json.dumps(identifiers, separators=(",", ":")) + "\n")
    report.update({"partitions": partitions, "raw_files": raw.files, "generator_sha256": checksum(Path(__file__)),
                   "quotas": {"train": TRAIN_QUOTAS, "test": TEST_QUOTAS, "dev": DEV_QUOTAS, "temperature": TEMPERATURE_QUOTAS},
                   "max_input_tokens": args.max_length, "train_scale": args.train_scale,
                   "family_policy": "Whole source families; shared MASSIVE IDs; normalized passages/prompts; PAWS connected sentence/token-bag components.",
                   "near_duplicate_policy": "Five-word shingle Jaccard >= 0.85, candidates retrieved by eight minimum SHA256 anchors; text parts below 20 words use exact checks only.",
                   "known_suite": "The 3247 original Choice/Noul records are verified against pinned Nimble checksums before adding provenance.",
                   "limitations": ["PAWS original generation mapping is unavailable (official URL returned 403); sentence/token-bag components and shingle checks are heuristic.",
                                   "Near-duplicate retrieval is approximate and does not prove absence of every paraphrase or pretraining overlap.",
                                   "PubMedQA labeled data are reserved for evaluation; no in-domain PubMedQA training in the first corpus.",
                                   "Image retention is controlled geometry, not a general vision benchmark.",
                                   "Fresh means unused in this project's tuning, not necessarily unseen by the base model or Jev."]})
    args.manifest.parent.mkdir(parents=True, exist_ok=True)
    args.manifest.write_text(json.dumps(report, indent=2) + "\n")
    record("data_frozen", manifest_sha256=checksum(args.manifest), partitions=partitions)
    print(json.dumps(partitions, indent=2), flush=True)


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--cache-root", type=Path, default=Path.home() / ".cache/huggingface")
    parser.add_argument("--source-cache", type=Path)
    parser.add_argument("--nimble-source", type=Path)
    parser.add_argument("--output", type=Path, default=Path("data"))
    parser.add_argument("--manifest", type=Path, default=Path("progress/data-manifest.json"))
    parser.add_argument("--seed", type=int, default=SEED)
    parser.add_argument("--max-length", type=int, default=8192)
    parser.add_argument("--train-scale", type=float, default=1.0)
    parser.add_argument("--image-train", type=int, default=1000)
    parser.add_argument("--image-eval", type=int, default=200)
    build(parser.parse_args(namespace=Arguments()))


if __name__ == "__main__":
    main()