OpenGHz's picture
Add fair-comparison/flow-v1
0a3f650 verified
Raw History Blame Contribute Delete
2.07 kB
"""The identical compact point policy used for all action-coordinate arms."""
import torch
from pydantic import BaseModel, ConfigDict, PositiveInt
from torch import nn
class PolicyConfig(BaseModel, frozen=True):
model_config = ConfigDict(extra="forbid", use_attribute_docstrings=True)
width: PositiveInt = 128
"""Shared point and action feature width."""
horizon: PositiveInt = 8
"""Predicted actions; only the first is executed before observing again."""
state_dim: PositiveInt = 29
"""Joint position/velocity, TCP pose, observed target centroid and validity."""
use_velocity: bool = True
"""Include measured joint velocities; disabled only in the registered shortcut diagnostic."""
class PointChunkPolicy(nn.Module):
def __init__(self, config: PolicyConfig):
super().__init__()
self.config = config
w = config.width
self.point_encoder = nn.Sequential(
nn.Linear(6, w), nn.ReLU(), nn.Linear(w, w), nn.ReLU(), nn.Linear(w, w)
)
self.state_encoder = nn.Sequential(
nn.Linear(config.state_dim, w), nn.ReLU(), nn.Linear(w, w)
)
self.query = nn.Parameter(torch.randn(config.horizon, w) * 0.02)
self.decoder = nn.TransformerDecoder(
nn.TransformerDecoderLayer(
w, 4, 4 * w, dropout=0.0, batch_first=True, norm_first=True
),
num_layers=2,
)
self.output = nn.Linear(w, 10)
def forward(self, points, state):
if not self.config.use_velocity:
state = torch.cat(
[state[:, :9], torch.zeros_like(state[:, 9:18]), state[:, 18:]], dim=-1
)
features = self.point_encoder(points)
# Identical global aggregation for the three coordinate arms.
memory = torch.stack(
[features.amax(dim=1), features.mean(dim=1), self.state_encoder(state)],
dim=1,
)
queries = self.query[None].expand(points.shape[0], -1, -1)
return self.output(self.decoder(queries, memory))