File size: 6,901 Bytes
ee5b44c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import dataclasses
import hashlib
import json
from collections.abc import Mapping
from pathlib import Path
from typing import Any


FINGERPRINT_VERSION = 1
# The on-disk contract: what arrays a shard holds, what each one means, and
# what the label is.  A reader checks it before anything else and refuses a
# dataset it does not recognise, because two schemas cannot be pooled -- the
# labels come from different mixtures and the arrays have different dtypes.
#
# Bump it whenever identical Config values would produce meaningfully different
# samples or shard semantics, and say here what changed.
SAMPLE_GENERATOR_SCHEMA_VERSION = 1
DONE_CONFIG_STATE_KEY = "sample_generation_config"
RUNTIME_CONFIG_FIELDS = frozenset(
    {
        "dataset_name",
        "compute_chunk_size",
        "force",
        "out_dir",
        "workers",
    }
)


class DatasetConfigMismatchError(RuntimeError):
    """Raised when an existing dataset cannot safely be reused."""


def _as_mapping(config: Any) -> dict[str, Any]:
    if dataclasses.is_dataclass(config) and not isinstance(config, type):
        return dataclasses.asdict(config)
    if isinstance(config, Mapping):
        return dict(config)
    raise TypeError("config must be a dataclass instance or mapping")


def _json_compatible(value: Any) -> Any:
    """Config values, for the fingerprint that decides whether a run resumes.

    ``map_cache`` has a function of the same name and a different contract: it
    encodes cache keys, so it sorts and rejects non-finite floats.  This one
    keeps insertion order, which is part of what it hashes, and accepts
    dataclasses, because a Config is one.  Merging them would move every
    fingerprint.
    """

    if dataclasses.is_dataclass(value) and not isinstance(value, type):
        return _json_compatible(dataclasses.asdict(value))
    if isinstance(value, Mapping):
        return {str(key): _json_compatible(item) for key, item in value.items()}
    if isinstance(value, (list, tuple)):
        return [_json_compatible(item) for item in value]
    if isinstance(value, Path):
        return str(value)
    if value is None or isinstance(value, (bool, int, float, str)):
        return value
    raise TypeError(f"config value of type {type(value).__name__} is not JSON-compatible")


def sample_generation_config(config: Any) -> dict[str, Any]:
    """Return the config fields that determine generated sample data or its format."""
    raw = _as_mapping(config)
    return {
        key: _json_compatible(value)
        for key, value in raw.items()
        if key not in RUNTIME_CONFIG_FIELDS
    }


def sample_generation_fingerprint(config: Any) -> str:
    relevant = sample_generation_config(config)
    canonical = json.dumps(
        relevant,
        ensure_ascii=False,
        allow_nan=False,
        sort_keys=True,
        separators=(",", ":"),
    )
    return hashlib.sha256(canonical.encode("utf-8")).hexdigest()


def generation_config_state(config: Any) -> dict[str, Any]:
    relevant = sample_generation_config(config)
    return {
        "version": FINGERPRINT_VERSION,
        "generator_schema_version": SAMPLE_GENERATOR_SCHEMA_VERSION,
        "algorithm": "sha256",
        "sha256": sample_generation_fingerprint(relevant),
        "ignored_fields": sorted(RUNTIME_CONFIG_FIELDS),
        "config": relevant,
    }


def stamp_done_manifest(manifest: Mapping[str, Any], config: Any) -> dict[str, Any]:
    stamped = dict(manifest)
    stamped[DONE_CONFIG_STATE_KEY] = generation_config_state(config)
    return stamped


def _mismatch_details(stored: Any, current: Any) -> str:
    if not isinstance(stored, Mapping) or not isinstance(current, Mapping):
        return ""
    keys = sorted(set(stored) | set(current))
    changed = [key for key in keys if stored.get(key) != current.get(key)]
    if not changed:
        return ""
    preview = ", ".join(changed[:8])
    if len(changed) > 8:
        preview += f", ... (+{len(changed) - 8} more)"
    return f" Differing sample-generation fields: {preview}."


def _reuse_error(message: str) -> DatasetConfigMismatchError:
    return DatasetConfigMismatchError(
        f"{message} Refusing to reuse the existing shards; rerun with --force to regenerate them."
    )


def validate_done_for_reuse(
    done_manifest: Mapping[str, Any],
    current_config: Any,
) -> dict[str, Any]:
    """
    Validate that DONE belongs to the requested sample-generation configuration.

    Returns the canonical state for a fingerprinted DONE. Legacy completion
    files are deliberately rejected because a mutable config.json cannot prove
    which configuration generated their shards.
    """
    current_state = generation_config_state(current_config)
    stored_state = done_manifest.get(DONE_CONFIG_STATE_KEY)

    if stored_state is None:
        raise _reuse_error(
            "DONE.json predates trustworthy configuration fingerprints; legacy "
            "config.json cannot prove which settings generated the existing shards."
        )

    if not isinstance(stored_state, Mapping):
        raise _reuse_error(f"{DONE_CONFIG_STATE_KEY!r} in DONE.json is malformed.")
    if stored_state.get("version") != FINGERPRINT_VERSION:
        raise _reuse_error(
            "DONE.json uses an unsupported sample-generation fingerprint version "
            f"({stored_state.get('version')!r})."
        )
    if stored_state.get("generator_schema_version") != SAMPLE_GENERATOR_SCHEMA_VERSION:
        raise _reuse_error(
            "DONE.json was produced by an unsupported sample-generator schema "
            f"({stored_state.get('generator_schema_version')!r})."
        )
    if stored_state.get("algorithm") != "sha256":
        raise _reuse_error(
            "DONE.json uses an unsupported sample-generation fingerprint algorithm "
            f"({stored_state.get('algorithm')!r})."
        )

    stored_sha256 = stored_state.get("sha256")
    if not isinstance(stored_sha256, str) or len(stored_sha256) != 64:
        raise _reuse_error("DONE.json contains a malformed sample-generation fingerprint.")

    stored_config = stored_state.get("config")
    if stored_config is not None:
        if not isinstance(stored_config, Mapping):
            raise _reuse_error("DONE.json contains a malformed sample-generation config snapshot.")
        if sample_generation_fingerprint(stored_config) != stored_sha256:
            raise _reuse_error(
                "The sample-generation config snapshot in DONE.json does not match its fingerprint."
            )

    if stored_sha256 != current_state["sha256"]:
        details = _mismatch_details(stored_config, current_state["config"])
        raise _reuse_error(
            "The requested configuration does not match the configuration that generated DONE.json."
            + details
        )
    return current_state