Download starVLA/scoring/matched_random.py from mmmmmcyaaa/ReTime: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/starVLA/scoring/matched_random.py
- Command line
-
hf download hf://mmmmmcyaaa/ReTime/starVLA/scoring/matched_random.py
-
curl -L -o matched_random.py https://huggingface.co/mmmmmcyaaa/ReTime/resolve/main/starVLA/scoring/matched_random.py
4.26 kB
| """Matched-random controls for residual temporal proposals.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from typing import Any | |
| import numpy as np | |
| from starVLA.dataloader.infoflow_sampler import ValidAnchorManifest | |
| from starVLA.dataloader.temporal_proposal import TemporalProposal | |
| def _episode_seed(seed: int, dataset_index: int, episode_index: int) -> int: | |
| payload = f"{int(seed)}:{dataset_index}:{episode_index}".encode("utf-8") | |
| return int.from_bytes( | |
| hashlib.sha256(payload).digest()[:8], | |
| "little", | |
| signed=False, | |
| ) | |
| def build_matched_random_proposal( | |
| *, | |
| manifest: ValidAnchorManifest, | |
| residual_proposal: TemporalProposal, | |
| permutation_seed: int, | |
| ) -> TemporalProposal: | |
| """Permute residual masses within every trajectory and preserve all marginals.""" | |
| if residual_proposal.manifest_sha256 != manifest.sha256: | |
| raise ValueError("residual proposal belongs to a different manifest") | |
| if residual_proposal.score_kind != "residual": | |
| raise ValueError("matched-random control requires a residual source proposal") | |
| permuted_masses = [] | |
| for dataset_index, dataset_masses in enumerate( | |
| residual_proposal.proposal_masses | |
| ): | |
| permuted_dataset = [] | |
| for episode_index, source_mass in enumerate(dataset_masses): | |
| source = np.asarray(source_mass, dtype=np.float64) | |
| rng = np.random.default_rng( | |
| _episode_seed( | |
| permutation_seed, | |
| dataset_index, | |
| episode_index, | |
| ) | |
| ) | |
| permuted_dataset.append(source[rng.permutation(len(source))]) | |
| permuted_masses.append(permuted_dataset) | |
| identity: dict[str, Any] = { | |
| "schema_version": 1, | |
| "source_proposal_sha256": residual_proposal.sha256, | |
| "source_scorer_sha256": residual_proposal.scorer_sha256, | |
| "permutation_seed": int(permutation_seed), | |
| "permutation_scope": "within_trajectory", | |
| "mass_multiset_preserved": True, | |
| "probability_multiset_preserved": True, | |
| "importance_weight_multiset_preserved": True, | |
| } | |
| scorer_sha256 = hashlib.sha256( | |
| json.dumps( | |
| identity, | |
| sort_keys=True, | |
| separators=(",", ":"), | |
| ).encode("utf-8") | |
| ).hexdigest() | |
| proposal = TemporalProposal.from_masses( | |
| manifest=manifest, | |
| alpha=residual_proposal.alpha, | |
| masses=permuted_masses, | |
| score_kind="random", | |
| scorer_sha256=scorer_sha256, | |
| validation_certificate=identity, | |
| ) | |
| for dataset_index, (source_dataset, matched_dataset) in enumerate( | |
| zip( | |
| residual_proposal.proposal_masses, | |
| proposal.proposal_masses, | |
| ) | |
| ): | |
| for episode_index, (source_mass, matched_mass) in enumerate( | |
| zip(source_dataset, matched_dataset) | |
| ): | |
| if not np.array_equal( | |
| np.sort(np.asarray(source_mass)), | |
| np.sort(np.asarray(matched_mass)), | |
| ): | |
| raise RuntimeError( | |
| "matched-random mass multiset differs at " | |
| f"dataset={dataset_index} episode={episode_index}" | |
| ) | |
| source_q = residual_proposal.anchor_probabilities[ | |
| dataset_index | |
| ][episode_index] | |
| matched_q = proposal.anchor_probabilities[dataset_index][ | |
| episode_index | |
| ] | |
| source_w = residual_proposal.importance_weights[dataset_index][ | |
| episode_index | |
| ] | |
| matched_w = proposal.importance_weights[dataset_index][ | |
| episode_index | |
| ] | |
| if not np.allclose( | |
| np.sort(source_q), | |
| np.sort(matched_q), | |
| rtol=0.0, | |
| atol=1e-15, | |
| ): | |
| raise RuntimeError("matched-random probability multiset differs") | |
| if not np.allclose( | |
| np.sort(source_w), | |
| np.sort(matched_w), | |
| rtol=0.0, | |
| atol=1e-15, | |
| ): | |
| raise RuntimeError("matched-random weight multiset differs") | |
| return proposal | |