Upload Piper adaptive B-spline code/starVLA/model/framework/VLM4A/QwenPI_v3_AdaptiveBSpline.py
Browse files
models/piper-pick-white-block-25hz-qwenpi-v3-adaptive-left-exclude-gripper-native-controls13-gb32-50k-20260905/code/starVLA/model/framework/VLM4A/QwenPI_v3_AdaptiveBSpline.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""QwenPI_v3 with interleaved adaptive-left B-spline parameters."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
from typing import List
|
| 6 |
+
|
| 7 |
+
import numpy as np
|
| 8 |
+
from scipy.interpolate import BSpline
|
| 9 |
+
|
| 10 |
+
from starVLA.model.framework.VLM4A.QwenPI_v3 import Qwen_PI_v3
|
| 11 |
+
from starVLA.model.tools import FRAMEWORK_REGISTRY
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def decode_adaptive_parameters(
|
| 15 |
+
parameters: np.ndarray,
|
| 16 |
+
*,
|
| 17 |
+
action_dim: int,
|
| 18 |
+
degree: int,
|
| 19 |
+
duration_min: int,
|
| 20 |
+
duration_max: int,
|
| 21 |
+
output_horizon: int | None = None,
|
| 22 |
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
| 23 |
+
"""Decode normalized rows on the native grid, preserving native lengths.
|
| 24 |
+
|
| 25 |
+
The first ``num_controls - degree`` durations define each plan's true
|
| 26 |
+
executable length. A dense return tensor is padded by repeating the last
|
| 27 |
+
native sample; callers must use the returned lengths as the validity mask.
|
| 28 |
+
``output_horizon`` is only a memory/safety ceiling, never a resampling size.
|
| 29 |
+
"""
|
| 30 |
+
values = np.asarray(parameters, dtype=np.float32)
|
| 31 |
+
if values.ndim != 3 or values.shape[2] != action_dim + 1:
|
| 32 |
+
raise ValueError(
|
| 33 |
+
f"Expected adaptive parameters [B, C, {action_dim + 1}], got {values.shape}"
|
| 34 |
+
)
|
| 35 |
+
if duration_min < 1 or duration_max <= duration_min:
|
| 36 |
+
raise ValueError("invalid duration normalization bounds")
|
| 37 |
+
if values.shape[1] <= degree:
|
| 38 |
+
raise ValueError("adaptive control count must exceed degree")
|
| 39 |
+
if output_horizon is not None and output_horizon < 1:
|
| 40 |
+
raise ValueError("output_horizon must be positive")
|
| 41 |
+
controls = values[..., :action_dim]
|
| 42 |
+
duration_normalized = values[..., action_dim]
|
| 43 |
+
durations = np.rint(
|
| 44 |
+
(duration_normalized + 1.0) * 0.5 * (duration_max - duration_min)
|
| 45 |
+
+ duration_min
|
| 46 |
+
).astype(np.int64)
|
| 47 |
+
durations = np.clip(durations, duration_min, duration_max)
|
| 48 |
+
executable_rows = values.shape[1] - degree
|
| 49 |
+
native_horizons = durations[:, :executable_rows].sum(axis=1, dtype=np.int64)
|
| 50 |
+
dense_horizon = (
|
| 51 |
+
int(native_horizons.max()) if output_horizon is None else int(output_horizon)
|
| 52 |
+
)
|
| 53 |
+
decoded = np.empty((values.shape[0], dense_horizon, action_dim), dtype=np.float32)
|
| 54 |
+
for batch_index in range(values.shape[0]):
|
| 55 |
+
distinct = np.concatenate(([0], np.cumsum(durations[batch_index])))
|
| 56 |
+
knots = np.concatenate((np.zeros(degree + 1, dtype=np.int64), distinct[1:]))
|
| 57 |
+
native_horizon = int(native_horizons[batch_index])
|
| 58 |
+
decoded_count = min(native_horizon, dense_horizon)
|
| 59 |
+
spline = BSpline(
|
| 60 |
+
knots,
|
| 61 |
+
controls[batch_index],
|
| 62 |
+
degree,
|
| 63 |
+
extrapolate=False,
|
| 64 |
+
axis=0,
|
| 65 |
+
)
|
| 66 |
+
decoded[batch_index, :decoded_count] = spline(
|
| 67 |
+
np.arange(decoded_count, dtype=np.float64)
|
| 68 |
+
)
|
| 69 |
+
if decoded_count < dense_horizon:
|
| 70 |
+
decoded[batch_index, decoded_count:] = decoded[batch_index, decoded_count - 1]
|
| 71 |
+
if not np.isfinite(decoded).all():
|
| 72 |
+
raise ValueError("Decoded adaptive B-spline actions contain non-finite values")
|
| 73 |
+
return decoded, durations, native_horizons
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@FRAMEWORK_REGISTRY.register("QwenPI_v3_AdaptiveBSpline")
|
| 77 |
+
class QwenPI_v3_AdaptiveBSpline(Qwen_PI_v3):
|
| 78 |
+
"""Predict 13 interleaved control/duration rows and decode a safe prefix."""
|
| 79 |
+
|
| 80 |
+
def __init__(self, config=None, **kwargs) -> None:
|
| 81 |
+
super().__init__(config=config, **kwargs)
|
| 82 |
+
representation = self.config.framework.get("action_representation")
|
| 83 |
+
if representation is None:
|
| 84 |
+
raise ValueError("framework.action_representation is required")
|
| 85 |
+
if str(representation.type) != "continuous_adaptive_bspline":
|
| 86 |
+
raise ValueError("adaptive framework requires continuous_adaptive_bspline")
|
| 87 |
+
if str(representation.mode) != "adaptive_left":
|
| 88 |
+
raise ValueError("adaptive framework requires adaptive_left mode")
|
| 89 |
+
if str(representation.policy_layout) != "control_point_duration_interleaved_v1":
|
| 90 |
+
raise ValueError("adaptive framework requires canonical control/duration interleaving")
|
| 91 |
+
self.spline_action_dim = int(representation.action_dim)
|
| 92 |
+
self.spline_degree = int(representation.degree)
|
| 93 |
+
self.spline_frequency_hz = float(representation.frequency_hz)
|
| 94 |
+
self.nominal_chunk_size = int(representation.nominal_chunk_size)
|
| 95 |
+
self.nominal_span_length_steps = int(representation.nominal_span_length_steps)
|
| 96 |
+
self.fit_tolerance = float(representation.fit_tolerance)
|
| 97 |
+
self.knot_insertion_excluded_dimensions = tuple(
|
| 98 |
+
int(value) for value in representation.knot_insertion_excluded_dimensions
|
| 99 |
+
)
|
| 100 |
+
self.spline_tokenizer_id = str(representation.tokenizer_id)
|
| 101 |
+
self.duration_min = int(representation.duration_min)
|
| 102 |
+
self.duration_max = int(representation.duration_max)
|
| 103 |
+
if str(representation.execution_horizon_mode) != "predicted_duration_sum":
|
| 104 |
+
raise ValueError("adaptive-left requires predicted_duration_sum execution")
|
| 105 |
+
self.max_native_horizon = int(representation.max_native_horizon)
|
| 106 |
+
if self.max_native_horizon < 1:
|
| 107 |
+
raise ValueError("max_native_horizon must be positive")
|
| 108 |
+
self.num_control_points = int(representation.num_control_points)
|
| 109 |
+
parameter_dim = int(representation.parameter_dim)
|
| 110 |
+
if self.spline_frequency_hz <= 0 or self.fit_tolerance <= 0:
|
| 111 |
+
raise ValueError("adaptive frequency and fit tolerance must be positive")
|
| 112 |
+
if (
|
| 113 |
+
(self.num_control_points - self.spline_degree)
|
| 114 |
+
* self.nominal_span_length_steps
|
| 115 |
+
!= self.nominal_chunk_size
|
| 116 |
+
):
|
| 117 |
+
raise ValueError("adaptive nominal span layout does not match chunk size")
|
| 118 |
+
if self.knot_insertion_excluded_dimensions != (6,):
|
| 119 |
+
raise ValueError("this Piper representation must exclude only gripper dimension 6")
|
| 120 |
+
if not self.spline_tokenizer_id:
|
| 121 |
+
raise ValueError("adaptive tokenizer_id must be nonempty")
|
| 122 |
+
if self.action_horizon != self.num_control_points:
|
| 123 |
+
raise ValueError("Action-head horizon must equal adaptive control count")
|
| 124 |
+
if parameter_dim != self.spline_action_dim + 1:
|
| 125 |
+
raise ValueError("Adaptive parameter_dim must be action_dim + one duration")
|
| 126 |
+
if int(self.config.framework.action_model.action_dim) != parameter_dim:
|
| 127 |
+
raise ValueError("Action-head dimension must match adaptive parameter_dim")
|
| 128 |
+
|
| 129 |
+
def decode_normalized_parameters(
|
| 130 |
+
self, parameters: np.ndarray
|
| 131 |
+
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
|
| 132 |
+
decoded, _, native_horizons = decode_adaptive_parameters(
|
| 133 |
+
parameters,
|
| 134 |
+
action_dim=self.spline_action_dim,
|
| 135 |
+
degree=self.spline_degree,
|
| 136 |
+
duration_min=self.duration_min,
|
| 137 |
+
duration_max=self.duration_max,
|
| 138 |
+
output_horizon=self.max_native_horizon,
|
| 139 |
+
)
|
| 140 |
+
effective_horizons = np.minimum(native_horizons, self.max_native_horizon)
|
| 141 |
+
batch_horizon = int(effective_horizons.max())
|
| 142 |
+
return decoded[:, :batch_horizon], effective_horizons, native_horizons
|
| 143 |
+
|
| 144 |
+
def predict_action_parameters(self, examples: List[dict] = None, **kwargs) -> dict:
|
| 145 |
+
return super().predict_action(examples=examples, **kwargs)
|
| 146 |
+
|
| 147 |
+
def predict_action(self, examples: List[dict] = None, **kwargs) -> dict:
|
| 148 |
+
predicted = self.predict_action_parameters(examples=examples, **kwargs)
|
| 149 |
+
decoded, action_lengths, uncapped_lengths = self.decode_normalized_parameters(
|
| 150 |
+
predicted["normalized_actions"]
|
| 151 |
+
)
|
| 152 |
+
return {
|
| 153 |
+
"normalized_actions": decoded,
|
| 154 |
+
"action_lengths": action_lengths,
|
| 155 |
+
"action_lengths_uncapped": uncapped_lengths,
|
| 156 |
+
}
|