File size: 4,387 Bytes
f78d5a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Validate calibration against every serving input before constructing a model."""

import json
import math
import re
from pathlib import Path

from .io import sha256

BACKEND = "hf-adapter-fp16"
UNFITTED = "unfitted for v2"
FITTED = "fitted on the supplied calibration split only"
SERVING_FILES = (
    "model_config.json",
    "head.safetensors",
    "adapter/adapter_config.json",
    "adapter/adapter_model.safetensors",
    "processor/tokenizer.json",
    "processor/tokenizer_config.json",
    "processor/processor_config.json",
    "processor/chat_template.jinja",
)
LEGACY_FILES = (
    "head.safetensors",
    "adapter/adapter_model.safetensors",
    "model_config.json",
)


def positive_temperature(value):
    try:
        valid = type(value) in (int, float) and math.isfinite(value) and value > 0
    except OverflowError:
        valid = False
    if not valid:
        raise ValueError("Calibration temperature must be finite and positive")
    return float(value)


def model_identity(checkpoint):
    """Legacy identity retained for compatibility with completed result audits."""
    return {name: sha256(Path(checkpoint) / name) for name in LEGACY_FILES}


def serving_identity(checkpoint):
    root = Path(checkpoint).resolve()
    files = {}
    for name in SERVING_FILES:
        path = root / name
        if (
            path.is_symlink()
            or not path.resolve().is_relative_to(root)
            or not path.is_file()
        ):
            raise ValueError("Missing or unsafe serving file: " + name)
        files[name] = sha256(path)
    return {"version": 1, "files": files}


def validate_calibration(checkpoint, value, *, backend=BACKEND):
    required = {
        "temperature",
        "status",
        "model_files",
        "backend",
        "data_sha256",
        "n",
        "serving_identity",
    }
    if not isinstance(value, dict) or set(value) != required:
        raise ValueError(
            "Calibration requires versioned serving-file provenance; legacy "
            "metadata must be upgraded using verified evaluation evidence"
        )
    positive_temperature(value["temperature"])
    if backend != BACKEND or value["backend"] != backend:
        raise ValueError("Calibration backend is not qualified")
    if value["status"] != FITTED:
        raise ValueError("Calibration must record a fitted calibration split")
    if type(value["n"]) is not int or value["n"] <= 0:
        raise ValueError("Calibration example count must be a positive integer")
    if not isinstance(value["data_sha256"], str) or not re.fullmatch(
        r"[0-9a-f]{64}", value["data_sha256"]
    ):
        raise ValueError("Calibration data identity must be a SHA256 digest")
    binding = value["serving_identity"]
    if (
        not isinstance(binding, dict)
        or set(binding) != {"version", "files"}
        or type(binding["version"]) is not int
        or binding["version"] != 1
    ):
        raise ValueError("Unsupported calibration serving identity")
    actual = serving_identity(checkpoint)
    if binding != actual:
        raise ValueError("Calibration was fitted for different serving files")
    if value["model_files"] != {name: actual["files"][name] for name in LEGACY_FILES}:
        raise ValueError("Calibration has a stale legacy model identity")
    return value


def load_calibration(checkpoint, path, *, backend=BACKEND):
    try:
        value = json.loads(Path(path).read_text())
    except (OSError, ValueError) as error:
        raise ValueError("Cannot read calibration metadata") from error
    return validate_calibration(checkpoint, value, backend=backend)


def checkpoint_calibration(checkpoint, config, *, trainable=False):
    """Training starts unfitted; serving requires either unit T or valid binding."""
    if trainable:
        return 1.0, UNFITTED
    path = Path(checkpoint) / "calibration.json"
    if path.exists():
        value = load_calibration(checkpoint, path)
        return positive_temperature(value["temperature"]), value["status"]
    temperature = positive_temperature(config.get("temperature"))
    status = config.get("calibration_status")
    if (
        temperature != 1.0
        or not isinstance(status, str)
        or not status.startswith("unfitted")
    ):
        raise ValueError("Serving calibration is missing its verified binding")
    return 1.0, UNFITTED