"""Deterministic quantization error-budget gate for invented evidence.""" from dataclasses import asdict, dataclass, fields import hashlib import json import re IDENTITY = re.compile(r"[A-Za-z0-9][A-Za-z0-9_.:/@-]{0,127}") SHA256 = re.compile(r"[a-f0-9]{64}") FORMATS = ("fp32", "bf16", "fp16", "fp8", "int8", "int4") def identity(value): if type(value) is not str or not IDENTITY.fullmatch(value): raise ValueError("exact bounded identity required") return value def integer(value, lower, upper): if type(value) is not int or not lower <= value <= upper: raise ValueError("bounded non-boolean integer required") return value def exact_bool(value): if type(value) is not bool: raise ValueError("exact boolean required") return value def sequence(value, lower, upper): if type(value) not in (tuple, list): raise ValueError("bounded tuple or list required") integer(len(value), lower, upper) return tuple(value) def identities(value, lower, upper): result = sequence(value, lower, upper) for item in result: identity(item) if len(set(result)) != len(result): raise ValueError("duplicate identity") return result def scope_tuple(value): return identities(value, 8, 8) def sha256_text(value): if type(value) is not str or not SHA256.fullmatch(value): raise ValueError("exact lowercase SHA-256 required") return value def digest(value): encoded = json.dumps( value, sort_keys=True, separators=(",", ":"), allow_nan=False ).encode() return hashlib.sha256(encoded).hexdigest() def seal(record): if type(record.content_id) is not str: raise ValueError("content digest requires exact string") data = asdict(record) data.pop("content_id") return digest(data) def validate_record(record, cls): if type(record) is not cls: raise ValueError("concrete frozen record required") try: rebuilt = cls(**{field.name: getattr(record, field.name) for field in fields(cls)}) except (AttributeError, TypeError) as error: raise ValueError("malformed record") from error if record != rebuilt or record.content_id != rebuilt.content_id: raise ValueError("noncanonical or modified record") return rebuilt def ceil_div(numerator, denominator): integer(numerator, 0, 10**30) integer(denominator, 1, 10**30) return (numerator + denominator - 1) // denominator @dataclass(frozen=True) class QuantizationContract: scope: tuple max_accuracy_drop_bps: int max_boundary_drop_bps: int max_p99_abs_error_microunits: int max_saturation_ppm: int min_compression_permille: int min_throughput_permille: int minimum_eval_examples: int minimum_boundary_examples: int minimum_calibration_values: int require_telemetry: bool content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) integer(self.max_accuracy_drop_bps, 0, 10_000) integer(self.max_boundary_drop_bps, 0, 10_000) integer(self.max_p99_abs_error_microunits, 0, 10**12) integer(self.max_saturation_ppm, 0, 1_000_000) integer(self.min_compression_permille, 1, 1_000_000) integer(self.min_throughput_permille, 1, 1_000_000) integer(self.minimum_eval_examples, 1, 100_000_000) integer(self.minimum_boundary_examples, 1, 100_000_000) integer(self.minimum_calibration_values, 1, 10**15) if exact_bool(self.require_telemetry) is not True: raise ValueError("quantization contract requires complete telemetry") expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("quantization contract digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class QuantizationEvidence: scope: tuple contract_content_id: str profile_id: str backend_revision: str baseline_format: str candidate_format: str calibration_digest: str eval_examples: int baseline_correct: int candidate_correct: int boundary_examples: int baseline_boundary_correct: int candidate_boundary_correct: int calibration_values: int saturated_values: int p99_abs_error_microunits: int baseline_bytes: int candidate_bytes: int baseline_throughput_milli_tps: int candidate_throughput_milli_tps: int telemetry_complete: bool content_id: str = "" def __post_init__(self): object.__setattr__(self, "scope", scope_tuple(self.scope)) sha256_text(self.contract_content_id) identity(self.profile_id) identity(self.backend_revision) if type(self.baseline_format) is not str or self.baseline_format not in FORMATS: raise ValueError("unknown exact baseline format") if type(self.candidate_format) is not str or self.candidate_format not in FORMATS: raise ValueError("unknown exact candidate format") if self.baseline_format == self.candidate_format: raise ValueError("candidate format must differ from baseline") sha256_text(self.calibration_digest) integer(self.eval_examples, 1, 100_000_000) integer(self.baseline_correct, 0, self.eval_examples) integer(self.candidate_correct, 0, self.eval_examples) integer(self.boundary_examples, 1, self.eval_examples) integer(self.baseline_boundary_correct, 0, self.boundary_examples) integer(self.candidate_boundary_correct, 0, self.boundary_examples) non_boundary_examples = self.eval_examples - self.boundary_examples if not 0 <= self.baseline_correct - self.baseline_boundary_correct <= non_boundary_examples: raise ValueError("baseline boundary aggregate is not a possible subset") if not 0 <= self.candidate_correct - self.candidate_boundary_correct <= non_boundary_examples: raise ValueError("candidate boundary aggregate is not a possible subset") integer(self.calibration_values, 1, 10**15) integer(self.saturated_values, 0, self.calibration_values) integer(self.p99_abs_error_microunits, 0, 10**12) integer(self.baseline_bytes, 1, 10**18) integer(self.candidate_bytes, 1, 10**18) integer(self.baseline_throughput_milli_tps, 1, 10**15) integer(self.candidate_throughput_milli_tps, 1, 10**15) exact_bool(self.telemetry_complete) expected = seal(self) if self.content_id and self.content_id != expected: raise ValueError("quantization evidence digest mismatch") object.__setattr__(self, "content_id", expected) @dataclass(frozen=True) class QuantizationReport: decision: str violations: tuple accuracy_drop_bps: int boundary_drop_bps: int saturation_ppm: int compression_permille: int throughput_permille: int evidence_id: str claim: str = "LOCAL_QUANTIZATION_GATE_NOT_HARDWARE_OR_QUALITY_BENCHMARK" def audit_quantization( contract: QuantizationContract, evidence: QuantizationEvidence ) -> QuantizationReport: """Apply an integer error budget to invented aggregate evidence.""" contract = validate_record(contract, QuantizationContract) evidence = validate_record(evidence, QuantizationEvidence) if evidence.scope != contract.scope or evidence.contract_content_id != contract.content_id: raise ValueError("evidence belongs to another quantization contract") accuracy_drop_bps = ceil_div( max(0, evidence.baseline_correct - evidence.candidate_correct) * 10_000, evidence.eval_examples, ) boundary_drop_bps = ceil_div( max( 0, evidence.baseline_boundary_correct - evidence.candidate_boundary_correct, ) * 10_000, evidence.boundary_examples, ) saturation_ppm = ceil_div( evidence.saturated_values * 1_000_000, evidence.calibration_values, ) compression_permille = ( evidence.baseline_bytes * 1_000 ) // evidence.candidate_bytes throughput_permille = ( evidence.candidate_throughput_milli_tps * 1_000 ) // evidence.baseline_throughput_milli_tps readiness = [] if contract.require_telemetry and not evidence.telemetry_complete: readiness.append("telemetry-incomplete") if evidence.eval_examples < contract.minimum_eval_examples: readiness.append("eval-sample") if evidence.boundary_examples < contract.minimum_boundary_examples: readiness.append("boundary-sample") if evidence.calibration_values < contract.minimum_calibration_values: readiness.append("calibration-sample") regressions = [] if accuracy_drop_bps > contract.max_accuracy_drop_bps: regressions.append("accuracy-drop") if boundary_drop_bps > contract.max_boundary_drop_bps: regressions.append("boundary-drop") if evidence.p99_abs_error_microunits > contract.max_p99_abs_error_microunits: regressions.append("p99-absolute-error") if saturation_ppm > contract.max_saturation_ppm: regressions.append("saturation-rate") if compression_permille < contract.min_compression_permille: regressions.append("compression-benefit") if throughput_permille < contract.min_throughput_permille: regressions.append("throughput-benefit") violations = tuple(readiness + regressions) if regressions: decision = "REJECT_PROFILE" elif readiness: decision = "HOLD_FOR_EVIDENCE" else: decision = "ACCEPT_BOUNDED_PROFILE" evidence_id = digest( { "contract": contract.content_id, "evidence": evidence.content_id, "decision": decision, "violations": violations, "metrics": { "accuracy_drop_bps": accuracy_drop_bps, "boundary_drop_bps": boundary_drop_bps, "saturation_ppm": saturation_ppm, "compression_permille": compression_permille, "throughput_permille": throughput_permille, }, } ) return QuantizationReport( decision, violations, accuracy_drop_bps, boundary_drop_bps, saturation_ppm, compression_permille, throughput_permille, evidence_id, ) def illustrative_fixture(): scope = ( "quantization-policy-v1", "baseline-model-v7", "candidate-profile-int4-v2", "calibration-set-v5", "eval-set-v9", "boundary-slice-v3", "backend-kernel-v4", "fixture-v1", ) contract = QuantizationContract( scope, 75, 200, 20_000, 10_000, 1_500, 1_100, 10_000, 1_000, 100_000, True, ) evidence = QuantizationEvidence( scope, contract.content_id, "int4-weight-only-group128-v2", "invented-backend-v4", "bf16", "int4", digest({"calibration": "invented-v5", "examples": 2_000}), 10_000, 9_200, 9_150, 1_000, 850, 835, 100_000, 500, 15_000, 16_000_000_000, 4_500_000_000, 80_000, 120_000, True, ) return contract, evidence def main(): report = audit_quantization(*illustrative_fixture()) print("example=illustrative_only") print(f"decision={report.decision}") print( f"accuracy_drop_bps={report.accuracy_drop_bps};" f"boundary_drop_bps={report.boundary_drop_bps}" ) print( f"saturation_ppm={report.saturation_ppm};" f"compression_permille={report.compression_permille};" f"throughput_permille={report.throughput_permille}" ) print(f"claim={report.claim}") if __name__ == "__main__": main()