Download fair-comparison/flow-v1/code/lim/keypose_stop.py from OpenGHz/keypose-vs-dense: direct link, hf CLI and curl.
- Browser
- Download file 3.03 kB
-
https://huggingface.co/datasets/OpenGHz/keypose-vs-dense/resolve/main/fair-comparison/flow-v1/code/lim/keypose_stop.py
- Command line
-
hf download hf://datasets/OpenGHz/keypose-vs-dense/fair-comparison/flow-v1/code/lim/keypose_stop.py
-
curl -L -o keypose_stop.py https://huggingface.co/datasets/OpenGHz/keypose-vs-dense/resolve/main/fair-comparison/flow-v1/code/lim/keypose_stop.py
3.03 kB
| """Terminal intent from query-zero features, without changing action10 semantics. | |
| Labels supervise whether to hold after this request completes. They are never | |
| model observations or a success signal. Training uses posterior features; | |
| inference logits must be read from the latest native zero-latent prior forward. | |
| """ | |
| import hashlib | |
| import torch | |
| from pydantic import BaseModel, ConfigDict | |
| from torch import nn | |
| class KeyposeStopHeadConfig(BaseModel, frozen=True): | |
| model_config = ConfigDict( | |
| arbitrary_types_allowed=True, extra="forbid", use_attribute_docstrings=True | |
| ) | |
| action_head: nn.Linear | |
| """Original native action10 projection, reused without reinitialization.""" | |
| class KeyposeStopHead(nn.Module): | |
| def __init__(self, config: KeyposeStopHeadConfig): | |
| super().__init__() | |
| self.config = config | |
| self.action = config.action_head | |
| if self.action.out_features != 10: | |
| raise ValueError("Stop intent requires the original keypose10 projection") | |
| device = self.action.weight.device | |
| devices = [device.index] if device.type == "cuda" else [] | |
| # Preserve subsequent context/EMA initialization and sampling/dropout RNG. | |
| with torch.random.fork_rng(devices=devices): | |
| self.stop = nn.Linear( | |
| self.action.in_features, | |
| 1, | |
| device=device, | |
| dtype=self.action.weight.dtype, | |
| ) | |
| self.stop_logits = None | |
| def forward(self, features): | |
| if features.ndim != 3 or features.shape[1] < 1: | |
| raise ValueError("Expected ACT decoder features batch x query x hidden") | |
| actions = self.action(features) | |
| self.stop_logits = self.stop(features[:, 0]).squeeze(-1) | |
| return actions | |
| def read_stop_logits(model): | |
| """Return batch logits from the latest forward without detaching or rerunning it.""" | |
| heads = [part for part in model.modules() if isinstance(part, KeyposeStopHead)] | |
| if len(heads) != 1: | |
| raise ValueError("Expected exactly one enabled keypose stop head") | |
| logits = heads[0].stop_logits | |
| if logits is None: | |
| raise RuntimeError("Stop logits require a completed policy forward") | |
| return logits | |
| def original_state_sha256(model): | |
| """Hash original parameters/buffers in native order, excluding only the new head. | |
| The complete state_dict hash intentionally changes when the head is present. | |
| Reversing the action wrapper's key prefix permits comparison with precision. | |
| """ | |
| digest = hashlib.sha256() | |
| for name, tensor in model.state_dict().items(): | |
| if ".action_head.dense." in name: | |
| continue | |
| name = name.replace(".action_head.primary.", ".action_head.") | |
| if ".action_head.stop." in name: | |
| continue | |
| original = name.replace(".action_head.action.", ".action_head.") | |
| digest.update(original.encode()) | |
| digest.update(tensor.detach().cpu().contiguous().numpy().tobytes()) | |
| return digest.hexdigest() | |