File size: 1,223 Bytes
0a3f650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Predicted primitive boundaries in fixed-anchor action chunks, without an FSM."""

import torch


def phase_prefix(phases):
    """Count the first constant-label run; no observed change means horizon, not arrival."""
    phases = torch.as_tensor(phases)
    if (
        phases.ndim != 1
        or len(phases) != 30
        or not ((phases >= 0) & (phases < 6)).all()
    ):
        raise ValueError("Expected 30 predicted primitive indices in [0, 5]")
    changes = torch.nonzero(phases != phases[0]).flatten()
    return int(changes[0]) if len(changes) else 30


def chunk_selection(logits, mode):
    if logits.shape != (30, 6) or not torch.isfinite(logits).all():
        raise ValueError("Expected finite prior logits with shape 30 x 6")
    if mode not in ("full", "boundary", "endpoint"):
        raise ValueError("Unknown phase chunk execution mode")
    labels = logits.argmax(-1)
    prefix = phase_prefix(labels)
    return {
        "phase_labels": labels.tolist(),
        "phase_prefix_length": prefix,
        "boundary_kind": "phase_change" if prefix < 30 else "horizon",
        "consume_length": 30 if mode == "full" else prefix,
        "selected_index": (30 if mode == "full" else prefix) - 1,
    }