File size: 2,966 Bytes
a0d66db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Causal flow matching with clean committed action tokens for the pinned π0.5 architecture."""

from __future__ import annotations

import jax
import jax.numpy as jnp
from openpi.models import model as openpi_model
from openpi.models.pi0 import make_attn_mask

# Eight physical channels are followed by zero padding in the DROID model input.
# This tag identifies a clean committed token under the model's shared noise-level embedding.
TAG = 8
MAX_DELAY = 4


def tag_prefix(actions, lengths):
    rows = actions[:, :MAX_DELAY]
    return rows.at[:, :, TAG].set(jnp.arange(MAX_DELAY)[None, :] < lengths[:, None])


def clamp_prefix(latent, prefix):
    first = jnp.where(prefix[..., TAG:TAG + 1] > 0, prefix, latent[:, :MAX_DELAY])
    return latent.at[:, :MAX_DELAY].set(first)


def prefix_loss(model, rng, observation, actions, *, train=False):
    preprocess, gaussian, times, delays = jax.random.split(rng, 4)
    observation = openpi_model.preprocess_observation(preprocess, observation, train=train)
    time = jax.random.beta(times, 1.5, 1, (actions.shape[0],)) * 0.999 + 0.001
    noise = jax.random.normal(gaussian, actions.shape)
    latent = time[:, None, None] * noise + (1 - time[:, None, None]) * actions
    # The validator normally uses four committed actions, with no delay at reset.
    choices = jnp.array([0, 0, 1, 2, 3, 4, 4, 4], dtype=jnp.int32)
    lengths = jax.random.choice(delays, choices, shape=(actions.shape[0],))
    latent = clamp_prefix(latent, tag_prefix(actions, lengths))
    context, visible, causal = model.embed_prefix(observation)
    suffix, valid, ar, conditioning = model.embed_suffix(observation, latent, time)
    mask = jnp.concatenate((visible, valid), axis=1)
    (_, output), _ = model.PaliGemma.llm(
        [context, suffix], mask=make_attn_mask(mask, jnp.concatenate((causal, ar))),
        positions=jnp.cumsum(mask, axis=1) - 1, adarms_cond=[None, conditioning],
    )
    velocity = model.action_out_proj(output[:, -model.action_horizon:])
    errors = jnp.mean(jnp.square(velocity - (noise - actions)), axis=-1)
    free = jnp.arange(model.action_horizon)[None, :] >= lengths[:, None]
    return errors * free * (model.action_horizon / free.sum(axis=-1, keepdims=True))


def sample_prefix(model, rng, observation, *, num_steps=10, noise=None, prefix=None):
    from refine import cached_velocity

    observation = openpi_model.preprocess_observation(None, observation, train=False)
    batch = observation.state.shape[0]
    if noise is None:
        noise = jax.random.normal(rng, (batch, model.action_horizon, model.action_dim))
    if prefix is None:
        prefix = jnp.zeros_like(noise[:, :MAX_DELAY])
    velocity = cached_velocity(model, observation)
    dt = 1.0 / num_steps

    def euler(index, latent):
        latent = clamp_prefix(latent, prefix)
        return latent - dt * velocity(latent, 1.0 - index * dt)

    return clamp_prefix(jax.lax.fori_loop(0, num_steps, euler, noise), prefix)