|
55 | 55 | PI = Decimal( |
56 | 56 | "3.141592653589793238462643383279502884197169399375105820974944592307816406286" |
57 | 57 | ) |
| 58 | +OPEN_UNIT_MAX = float.fromhex("0x1.fffffffffffffp-1") |
58 | 59 |
|
59 | 60 | HYPOTHESIS_IDS = tuple( |
60 | 61 | [f"h_a/{arity}" for arity in DIRECT_ARITIES] |
|
101 | 102 | "hypothesis_ids": list(HYPOTHESIS_IDS), |
102 | 103 | "process_noise_index": ["episode_id", "phase", "phase_time", "carrier_id", "coordinate"], |
103 | 104 | "process_noise_phases": {"burn": [0, 31], "scored": [0, 127]}, |
| 105 | + "stability_probe_index": ["attempt", "probe_episode", "phase", "phase_time", "carrier_id", "coordinate"], |
104 | 106 | "initializer_index": ["model_id", "restart", "tensor_name", "all_parameter_axes_in_row_major_order"], |
| 107 | + "initializer_values": "glorot_initializer_value and rho_initializer_value in this executable", |
| 108 | + "open_uniform_binary64_top": "0x1.fffffffffffffp-1 when exact rational conversion rounds to 1.0", |
105 | 109 | "training_population_order": [ |
106 | 110 | "observational episode ordinal", |
107 | 111 | "intervention class 1..4", |
@@ -218,14 +222,22 @@ def fdiv(left: float, right: float) -> float: |
218 | 222 | return f64(f64(left) / f64(right)) |
219 | 223 |
|
220 | 224 |
|
221 | | -def open_uniform(payload: bytes, lane: int = 0) -> float: |
222 | | - if lane not in range(4): |
223 | | - raise ValueError("lane must be 0..3") |
224 | | - word = digest_words(payload)[lane] |
| 225 | +def open_uniform_word(word: int) -> float: |
| 226 | + """Map one uint64 word to the nearest admitted open binary64 value.""" |
| 227 | + |
| 228 | + if isinstance(word, bool) or not isinstance(word, int) or not 0 <= word < 2**64: |
| 229 | + raise ValueError("word must be an unsigned 64-bit integer") |
225 | 230 | with localcontext() as ctx: |
226 | 231 | ctx.prec = 96 |
227 | 232 | ctx.rounding = ROUND_HALF_EVEN |
228 | | - return f64((Decimal(word) + Decimal("0.5")) / (Decimal(2) ** 64)) |
| 233 | + value = f64((Decimal(word) + Decimal("0.5")) / (Decimal(2) ** 64)) |
| 234 | + return OPEN_UNIT_MAX if value >= 1.0 else value |
| 235 | + |
| 236 | + |
| 237 | +def open_uniform(payload: bytes, lane: int = 0) -> float: |
| 238 | + if lane not in range(4): |
| 239 | + raise ValueError("lane must be 0..3") |
| 240 | + return open_uniform_word(digest_words(payload)[lane]) |
229 | 241 |
|
230 | 242 |
|
231 | 243 | def _decimal(value: float | Decimal | int | str) -> Decimal: |
@@ -317,6 +329,18 @@ def deterministic_softplus(value: float) -> float: |
317 | 329 | return f64((Decimal(1) + x.exp()).ln()) |
318 | 330 |
|
319 | 331 |
|
| 332 | +def deterministic_softplus_inverse(value: float) -> float: |
| 333 | + """Inverse softplus under the reference Decimal-to-binary64 contract.""" |
| 334 | + |
| 335 | + if value <= 0: |
| 336 | + raise ValueError("softplus inverse input must be positive") |
| 337 | + with localcontext() as ctx: |
| 338 | + ctx.prec = 96 |
| 339 | + ctx.rounding = ROUND_HALF_EVEN |
| 340 | + x = _decimal(value) |
| 341 | + return f64((x.exp() - Decimal(1)).ln()) |
| 342 | + |
| 343 | + |
320 | 344 | def gaussian_cdf(value: float) -> float: |
321 | 345 | """Deterministic standard-normal CDF; tails are frozen at +/-8.""" |
322 | 346 |
|
@@ -348,7 +372,7 @@ def family_kind(family_id: str) -> str: |
348 | 372 | if family_id.startswith("capacity-only/"): |
349 | 373 | return "dense" |
350 | 374 | if "/tree" in family_id or family_id.startswith( |
351 | | - ("nested/", "wrong-tree/", "outer-cut/", "unnested/") |
| 375 | + ("nested/", "wrong-tree/", "outer-cut/") |
352 | 376 | ): |
353 | 377 | return "nested" |
354 | 378 | return "direct" |
@@ -389,6 +413,46 @@ def initializer_key( |
389 | 413 | ) |
390 | 414 |
|
391 | 415 |
|
| 416 | +def glorot_initializer_value( |
| 417 | + *, |
| 418 | + seed: int, |
| 419 | + arity: int, |
| 420 | + sigma_milli: int, |
| 421 | + family_id: str, |
| 422 | + restart: int, |
| 423 | + tensor_name: str, |
| 424 | + axes: Sequence[int], |
| 425 | + fan_in: int, |
| 426 | + fan_out: int, |
| 427 | +) -> float: |
| 428 | + """Return one exact Glorot-uniform initialized matrix scalar.""" |
| 429 | + |
| 430 | + if _uint(fan_in, "fan_in") == 0 or _uint(fan_out, "fan_out") == 0: |
| 431 | + raise ValueError("Glorot fan sizes must be positive") |
| 432 | + payload = initializer_key( |
| 433 | + seed=seed, |
| 434 | + arity=arity, |
| 435 | + sigma_milli=sigma_milli, |
| 436 | + family_id=family_id, |
| 437 | + restart=restart, |
| 438 | + tensor_name=tensor_name, |
| 439 | + axes=axes, |
| 440 | + ) |
| 441 | + bound = deterministic_sqrt(fdiv(6.0, fadd(fan_in, fan_out))) |
| 442 | + centered = fsub(fmul(2.0, open_uniform(payload)), 1.0) |
| 443 | + return fmul(centered, bound) |
| 444 | + |
| 445 | + |
| 446 | +def rho_initializer_value(sigma_milli: int) -> float: |
| 447 | + """Return the exact raw diagonal-variance initializer for one noise level.""" |
| 448 | + |
| 449 | + if sigma_milli not in NOISE_MILLI: |
| 450 | + raise ValueError("sigma_milli must be 10, 50, or 100") |
| 451 | + sigma = fdiv(sigma_milli, 1000) |
| 452 | + target = max(fsub(fmul(sigma, sigma), 1e-6), 1e-12) |
| 453 | + return deterministic_softplus_inverse(target) |
| 454 | + |
| 455 | + |
392 | 456 | def process_noise_key( |
393 | 457 | *, |
394 | 458 | seed: int, |
@@ -427,6 +491,42 @@ def coefficient_role(system_kind: str, attempt: int, outer: int | None = None) - |
427 | 491 | return f"{base}/attempt/{attempt}" |
428 | 492 |
|
429 | 493 |
|
| 494 | +def stability_probe_key( |
| 495 | + *, |
| 496 | + seed: int, |
| 497 | + arity: int, |
| 498 | + sigma_milli: int, |
| 499 | + system_kind: str, |
| 500 | + attempt: int, |
| 501 | + probe_ordinal: int, |
| 502 | + phase: str, |
| 503 | + phase_time: int, |
| 504 | + carrier_id: str, |
| 505 | + coordinate: int, |
| 506 | + outer: int | None = None, |
| 507 | +) -> bytes: |
| 508 | + """Emit one complete stability initial-state or noise scalar key.""" |
| 509 | + |
| 510 | + if not 0 <= probe_ordinal < 16: |
| 511 | + raise ValueError("probe_ordinal must be 0..15") |
| 512 | + if phase == "initial": |
| 513 | + if phase_time != 0: |
| 514 | + raise ValueError("stability initial state exists only at time 0") |
| 515 | + elif phase == "noise": |
| 516 | + if not 0 <= phase_time <= 31: |
| 517 | + raise ValueError("stability noise time must be 0..31") |
| 518 | + else: |
| 519 | + raise ValueError("stability phase must be initial or noise") |
| 520 | + return stream_bytes( |
| 521 | + seed=seed, |
| 522 | + arity=arity, |
| 523 | + sigma_milli=sigma_milli, |
| 524 | + domain="stability_probe", |
| 525 | + role=coefficient_role(system_kind, attempt, outer), |
| 526 | + indices=[attempt, f"stability/{probe_ordinal:02d}", phase, phase_time, carrier_id, coordinate], |
| 527 | + ) |
| 528 | + |
| 529 | + |
430 | 530 | def hypothesis_id(kind: str, arity: int | None = None) -> str: |
431 | 531 | if kind == "h_7" and arity is None: |
432 | 532 | return "h_7" |
@@ -597,6 +697,18 @@ def test_vectors() -> dict[str, object]: |
597 | 697 | seed=32, arity=7, sigma_milli=50, family_id="direct/7", restart=0, |
598 | 698 | tensor_name="w", axes=[4, 5, 1, 0] |
599 | 699 | ) |
| 700 | + glorot_value = glorot_initializer_value( |
| 701 | + seed=32, arity=7, sigma_milli=50, family_id="direct/7", restart=0, |
| 702 | + tensor_name="w", axes=[3, 5, 1, 0], fan_in=2, fan_out=2 |
| 703 | + ) |
| 704 | + probe_initial = stability_probe_key( |
| 705 | + seed=32, arity=7, sigma_milli=50, system_kind="direct", attempt=0, |
| 706 | + probe_ordinal=0, phase="initial", phase_time=0, carrier_id="3", coordinate=1 |
| 707 | + ) |
| 708 | + probe_noise = stability_probe_key( |
| 709 | + seed=32, arity=7, sigma_milli=50, system_kind="direct", attempt=0, |
| 710 | + probe_ordinal=0, phase="noise", phase_time=0, carrier_id="3", coordinate=1 |
| 711 | + ) |
600 | 712 | burn = process_noise_key( |
601 | 713 | seed=32, arity=7, sigma_milli=50, role="direct", |
602 | 714 | episode_id="obs/test/000", phase="burn", phase_time=0, |
@@ -637,12 +749,22 @@ def test_vectors() -> dict[str, object]: |
637 | 749 | "words_hex": [f"{word:016x}" for word in digest_words(payload)], |
638 | 750 | "uniform_0_f64": f64_hex(open_uniform(payload, 0)), |
639 | 751 | "normal_f64": f64_hex(standard_normal(payload)), |
| 752 | + "minimum_open_uniform_f64": f64_hex(open_uniform_word(0)), |
| 753 | + "maximum_open_uniform_f64": f64_hex(open_uniform_word(2**64 - 1)), |
640 | 754 | }, |
641 | 755 | "initializer": { |
642 | 756 | "complete_path_ascii": initializer_a.decode("ascii"), |
643 | 757 | "complete_path_sha256": hashlib.sha256(initializer_a).hexdigest(), |
644 | 758 | "different_carrier_sha256": hashlib.sha256(initializer_b).hexdigest(), |
645 | 759 | "distinct": initializer_a != initializer_b, |
| 760 | + "glorot_f64": f64_hex(glorot_value), |
| 761 | + "rho_sigma_050_f64": f64_hex(rho_initializer_value(50)), |
| 762 | + "unnested_kind": family_kind("unnested/7"), |
| 763 | + }, |
| 764 | + "stability_probe": { |
| 765 | + "initial_ascii": probe_initial.decode("ascii"), |
| 766 | + "noise_ascii": probe_noise.decode("ascii"), |
| 767 | + "distinct": probe_initial != probe_noise, |
646 | 768 | }, |
647 | 769 | "noise_phase": { |
648 | 770 | "burn_ascii": burn.decode("ascii"), |
@@ -707,6 +829,13 @@ def file_sha256(path: Path) -> str: |
707 | 829 | def verify(pin_path: Path) -> dict[str, object]: |
708 | 830 | pin = json.loads(pin_path.read_text(encoding="utf-8")) |
709 | 831 | here = Path(__file__).resolve() |
| 832 | + declared_path = pin.get("reference_path") |
| 833 | + if not isinstance(declared_path, str) or Path(declared_path).is_absolute(): |
| 834 | + raise RuntimeError("reference_path must be one relative path") |
| 835 | + if (pin_path.parent / declared_path).resolve() != here: |
| 836 | + raise RuntimeError("reference_path does not resolve to this executable") |
| 837 | + if pin.get("verification_command") != f"python {declared_path} verify": |
| 838 | + raise RuntimeError("verification_command does not invoke the pinned executable") |
710 | 839 | vectors = test_vectors() |
711 | 840 | vectors_path = pin_path.parent / pin["vectors_path"] |
712 | 841 | stored_vectors = json.loads(vectors_path.read_text(encoding="utf-8")) |
|
0 commit comments