Download fair-comparison/p2-v1/code/lim/keypose_phase.py from OpenGHz/keypose-vs-dense: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/datasets/OpenGHz/keypose-vs-dense/resolve/main/fair-comparison/p2-v1/code/lim/keypose_phase.py
- Command line
-
hf download hf://datasets/OpenGHz/keypose-vs-dense/fair-comparison/p2-v1/code/lim/keypose_phase.py
-
curl -L -o keypose_phase.py https://huggingface.co/datasets/OpenGHz/keypose-vs-dense/resolve/main/fair-comparison/p2-v1/code/lim/keypose_phase.py
2.75 kB
| """Auxiliary primitive classification from the same ACT decoder forward. | |
| Training features include the native CVAE posterior; inference uses its zero | |
| latent prior. Classification accuracy on training forwards is therefore not a | |
| substitute for prior-mode phase accuracy. Phase labels never enter either branch | |
| as observations, conditioning, or an execution state machine. | |
| """ | |
| import torch | |
| from pydantic import BaseModel, ConfigDict | |
| from torch import nn | |
| KEYPOSE_PHASE_NAMES = ( | |
| "approach", | |
| "grasp_pose", | |
| "close_gripper", | |
| "lift", | |
| "place_pose", | |
| "open_gripper", | |
| ) | |
| class KeyposePhaseHeadConfig(BaseModel, frozen=True): | |
| model_config = ConfigDict( | |
| arbitrary_types_allowed=True, extra="forbid", use_attribute_docstrings=True | |
| ) | |
| action_head: nn.Linear | |
| """Existing native action projection, reused without reinitialization.""" | |
| all_queries: bool = False | |
| """Classify every future action; false retains the original query-zero head.""" | |
| class KeyposePhaseHead(nn.Module): | |
| def __init__(self, config: KeyposePhaseHeadConfig): | |
| super().__init__() | |
| self.config = config | |
| self.action = config.action_head | |
| device = self.action.weight.device | |
| devices = [device.index] if device.type == "cuda" else [] | |
| # The extra head must not perturb later model/EMA construction, dropout, | |
| # native posterior sampling, or the training sample-order RNG. | |
| with torch.random.fork_rng(devices=devices): | |
| self.phase = nn.Linear( | |
| self.action.in_features, | |
| len(KEYPOSE_PHASE_NAMES), | |
| device=device, | |
| dtype=self.action.weight.dtype, | |
| ) | |
| self.phase_logits = None | |
| def forward(self, features): | |
| if features.ndim != 3 or features.shape[1] < 1: | |
| raise ValueError( | |
| "Expected ACT decoder features with shape batch, query, hidden" | |
| ) | |
| actions = self.action(features) | |
| self.phase_logits = self.phase( | |
| features if self.config.all_queries else features[:, 0] | |
| ) | |
| return actions | |
| def read_phase_logits(model): | |
| """Return six raw class logits from the latest forward, before/after normalization. | |
| Accept the native agent or any registered model wrapper. Does not invoke the | |
| network, sample a latent, detach gradients, or change the action output. | |
| """ | |
| heads = [part for part in model.modules() if isinstance(part, KeyposePhaseHead)] | |
| if len(heads) != 1: | |
| raise ValueError("Expected exactly one enabled keypose phase head") | |
| logits = heads[0].phase_logits | |
| if logits is None: | |
| raise RuntimeError("Phase logits require a completed policy forward") | |
| return logits | |