File size: 11,901 Bytes
9d8d25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c869f1f
 
 
9d8d25c
 
 
 
 
 
 
c869f1f
9d8d25c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# SPDX-License-Identifier: Apache-2.0
"""RDT2-FM action expert (RDT, 14 blocks, hidden 1024) and its 5-step Euler flow-matching sampler, fp32 torch.

Semantics (thu-ml/RDT2 models/rdt_runner.py, models/rdt/{model,blocks,attention,norm,pos_emb}.py @ 0797b4c6):
  s      = state_adaptor(state)            mlp3x_silu 20 -> 1024 -> 1024 -> 1024, state (1, 1, 20) raw (not normalized)
  s     += state_pos_emb                   (1, 1, 1024) checkpoint parameter
  a_0    = noise (1, 24, 20);  t_0 = 0;  for k in 0..4:  (t accumulated in the run dtype: t += 1/5)
     te_k  = TimestepEmbedder(t_k)          [cos | sin](t * 10000^(-j/128)), j < 128 -> Linear 256->1024, SiLU, Linear
     c_k   = concat(te_k, s)                (1, 2048)   -- "t" of RDTBlock / FinalLayer
     x     = concat(act_adaptor(a_k), register_tokens) + x_pos_emb          (1, 28, 1024)
     block i (adaLN = Linear(SiLU(c_k)) -> 9 x 1024: shift/scale/gate for attn, cross, mlp):
        x += g_a * SelfAttn(mod(rms(x)))        q,k RMS-normed per head (128), 8 q / 4 kv heads, no RoPE, no mask
        x += g_c * CrossAttn(mod(rms(x)), K_i, V_i, mask)   q RMS-normed; K/V = Qwen layer-i cache AS IS
                                                            (post-mRoPE K, no re-projection, no norm_k), mask = keys
        x += g_m * SwiGLU(mod(rms(x)))          w2(silu(w1 x) * w3 x), 1024 -> 2816 -> 1024
     v_k   = FinalLayer(x, c_k)[:, :24]     rms + adaLN(2) + Mlp 1024 -> 4096 (SiLU) -> 20; register rows dropped
     a_{k+1} = v_k * 0.2 + a_k
  action = (a_5 - offset) / scale            UMI LinearNormalizer['action'] (gripper channels 9 / 19 -> metres)

Host/device split used by the port (``precompute_*`` below, proven equal to the direct form by the host tests):
  ada_i(k) = W_i[:, :1024] @ silu(te_k)  +  W_i[:, 1024:] @ silu(s)  +  b_i     (SiLU is element-wise on the concat)
           = T_i[k] (input-independent, 5 x 14 x 9216, precomputed once)  +  S_i (per request, constant over steps)
"""
from __future__ import annotations

import math
from typing import Dict, List, Optional

import torch
import torch.nn.functional as F

from .config import CONFIG
from .ops import modulate, repeat_kv_heads, rms_norm

E = CONFIG.expert


def timestep_frequencies(t: torch.Tensor, dim: int = E.freq_dim, max_period: float = E.max_period) -> torch.Tensor:
    half = dim // 2
    freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half)
    args = t[:, None].float() * freqs[None]
    return torch.cat([torch.cos(args), torch.sin(args)], dim=-1)


def euler_timesteps(num_steps: int = E.num_steps, dtype: torch.dtype = torch.float32) -> List[torch.Tensor]:
    """t_k exactly as the official loop produces them (``timestep += 1/num_steps`` in the run dtype)."""
    t = torch.tensor([0.0], dtype=dtype)
    step = 1.0 / num_steps
    out = []
    for _ in range(num_steps):
        out.append(t.clone())
        t += step
    return out


class ActionExpert:
    def __init__(self, sd: Dict[str, torch.Tensor], dtype: torch.dtype = torch.float32):
        self.dtype = dtype
        self.w = {k: v.to(dtype) for k, v in sd.items()}

    # ---------------------------------------------------------------- conditioning (host-side in the port)
    def mlp3(self, prefix: str, x: torch.Tensor) -> torch.Tensor:
        w = self.w
        x = F.linear(x, w[prefix + "0.weight"], w[prefix + "0.bias"])
        x = F.linear(F.silu(x), w[prefix + "2.weight"], w[prefix + "2.bias"])
        return F.linear(F.silu(x), w[prefix + "4.weight"], w[prefix + "4.bias"])

    def state_cond(self, state: torch.Tensor) -> torch.Tensor:
        """state (20,) or (1, 1, 20) -> s (1, 1024) = state_adaptor(state) + state_pos_emb."""
        s = self.mlp3("state_adaptor.", state.reshape(1, 1, -1).to(self.dtype))
        return (s + self.w["model.state_pos_emb"]).reshape(1, E.hidden)

    def time_emb(self, t: torch.Tensor) -> torch.Tensor:
        w = self.w
        f = timestep_frequencies(t).to(self.dtype)
        h = F.linear(f, w["model.t_embedder.mlp.0.weight"], w["model.t_embedder.mlp.0.bias"])
        return F.linear(F.silu(h), w["model.t_embedder.mlp.2.weight"], w["model.t_embedder.mlp.2.bias"])

    def ada(self, c: torch.Tensor):
        """Direct form: per-block (1, 9216) and final (1, 2048) modulation from c (1, 2048)."""
        w = self.w
        sc = F.silu(c)
        blocks = [F.linear(sc, w[f"model.blocks.{i}.adaLN_modulation.1.weight"], w[f"model.blocks.{i}.adaLN_modulation.1.bias"])
                  for i in range(E.depth)]
        final = F.linear(sc, w["model.final_layer.adaLN_modulation.1.weight"], w["model.final_layer.adaLN_modulation.1.bias"])
        return blocks, final

    def precompute_time_part(self, ts: List[torch.Tensor]):
        """Input-independent half: per step k, block i -> W_i[:, :1024] silu(te_k) + b_i (bias folded here)."""
        w = self.w
        H = E.hidden
        out = []
        for t in ts:
            st = F.silu(self.time_emb(t))
            blocks = [F.linear(st, w[f"model.blocks.{i}.adaLN_modulation.1.weight"][:, :H],
                               w[f"model.blocks.{i}.adaLN_modulation.1.bias"]) for i in range(E.depth)]
            final = F.linear(st, w["model.final_layer.adaLN_modulation.1.weight"][:, :H],
                             w["model.final_layer.adaLN_modulation.1.bias"])
            out.append((blocks, final))
        return out

    def precompute_state_part(self, s: torch.Tensor):
        """Per-request half (constant over the 5 steps): W_i[:, 1024:] silu(s)."""
        w = self.w
        H = E.hidden
        ss = F.silu(s)
        blocks = [F.linear(ss, w[f"model.blocks.{i}.adaLN_modulation.1.weight"][:, H:]) for i in range(E.depth)]
        final = F.linear(ss, w["model.final_layer.adaLN_modulation.1.weight"][:, H:])
        return blocks, final

    # ---------------------------------------------------------------- network (device-side in the port)
    def _attn_qk(self, p: str, x: torch.Tensor):
        w = self.w
        B, N, _ = x.shape
        q = F.linear(x, w[p + "wq.weight"]).view(B, N, E.heads, E.head_dim)
        return rms_norm(q, w[p + "norm_q.weight"], E.norm_eps)

    def self_attn(self, i: int, x: torch.Tensor) -> torch.Tensor:
        w = self.w
        p = f"model.blocks.{i}.attn."
        B, N, _ = x.shape
        q = self._attn_qk(p, x)
        kv = F.linear(x, w[p + "wkv.weight"]).view(B, N, E.kv_heads, E.head_dim, 2)  # k/v interleaved in the last dim
        k, v = kv.unbind(-1)
        k = rms_norm(k, w[p + "norm_k.weight"], E.norm_eps)
        rep = E.heads // E.kv_heads
        k, v = repeat_kv_heads(k, rep, 2), repeat_kv_heads(v, rep, 2)
        o = F.scaled_dot_product_attention(q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2),
                                           scale=1.0 / math.sqrt(E.head_dim))
        return F.linear(o.transpose(1, 2).reshape(B, N, -1), w[p + "wo.weight"])

    def cross_attn(self, i: int, x: torch.Tensor, K: torch.Tensor, V: torch.Tensor,
                   mask: Optional[torch.Tensor]) -> torch.Tensor:
        """K, V: (1, 4, L, 128) Qwen cache of layer i; mask: (1, L) bool (True = attend) or None."""
        w = self.w
        p = f"model.blocks.{i}.cross_attn."
        B, N, _ = x.shape
        q = self._attn_qk(p, x).transpose(1, 2)  # (1, 8, 28, 128)
        rep = E.heads // E.kv_heads
        k, v = repeat_kv_heads(K.to(self.dtype), rep, 1), repeat_kv_heads(V.to(self.dtype), rep, 1)
        m = None if mask is None else mask.reshape(B, 1, 1, -1).expand(-1, -1, N, -1)
        o = F.scaled_dot_product_attention(q, k, v, attn_mask=m, scale=1.0 / math.sqrt(E.head_dim))
        return F.linear(o.transpose(1, 2).reshape(B, N, -1), w[p + "wo.weight"])

    def ffn(self, i: int, x: torch.Tensor) -> torch.Tensor:
        w = self.w
        p = f"model.blocks.{i}.ffn."
        return F.linear(F.silu(F.linear(x, w[p + "w1.weight"])) * F.linear(x, w[p + "w3.weight"]), w[p + "w2.weight"])

    def block(self, i: int, x: torch.Tensor, ada: torch.Tensor, K, V, mask) -> torch.Tensor:
        w = self.w
        p = f"model.blocks.{i}."
        sa, ca, ga, sc, cc, gc, sm, cm, gm = ada.chunk(9, dim=1)
        h = x + ga.unsqueeze(1) * self.self_attn(i, modulate(rms_norm(x, w[p + "attn_norm.weight"], E.norm_eps), sa, ca))
        h = h + gc.unsqueeze(1) * self.cross_attn(
            i, modulate(rms_norm(h, w[p + "cross_norm.weight"], E.norm_eps), sc, cc), K, V, mask)
        return h + gm.unsqueeze(1) * self.ffn(i, modulate(rms_norm(h, w[p + "ffn_norm.weight"], E.norm_eps), sm, cm))

    def final(self, x: torch.Tensor, ada_final: torch.Tensor) -> torch.Tensor:
        w = self.w
        shift, scale = ada_final.chunk(2, dim=1)
        x = modulate(rms_norm(x, w["model.final_layer.ffn_norm.weight"], E.norm_eps), shift, scale)
        x = F.silu(F.linear(x, w["model.final_layer.ffn.fc1.weight"], w["model.final_layer.ffn.fc1.bias"]))
        return F.linear(x, w["model.final_layer.ffn.fc2.weight"], w["model.final_layer.ffn.fc2.bias"])

    def velocity(self, a: torch.Tensor, ada_blocks, ada_final, K, V, mask,
                 taps: Optional[Dict[str, torch.Tensor]] = None, step: int = 0) -> torch.Tensor:
        """One RDT forward. a: (1, 24, 20) noisy action; K/V: (14, 1, 4, L, 128)."""
        w = self.w
        x0 = self.mlp3("act_adaptor.", a.to(self.dtype))
        x = torch.cat([x0, w["model.register_tokens"]], dim=1) + w["model.x_pos_emb"]
        if taps is not None:
            taps[f"exp.step{step}.act_emb"] = x0
        for i in range(E.depth):
            x = self.block(i, x, ada_blocks[i], K[i], V[i], mask)
            if taps is not None:
                taps[f"exp.step{step}.block{i:02d}.out"] = x
        out = self.final(x, ada_final)
        if taps is not None:
            taps[f"exp.step{step}.final_out"] = out
        return out[:, :E.horizon]

    def sample(self, noise: torch.Tensor, state: torch.Tensor, K: torch.Tensor, V: torch.Tensor,
               mask: Optional[torch.Tensor], taps: Optional[Dict[str, torch.Tensor]] = None,
               decomposed: bool = True, num_steps: int = E.num_steps) -> torch.Tensor:
        """``num_steps``-step Euler sampling (default 5, the official one). Returns the normalized action (1, 24, 20)."""
        ts = euler_timesteps(num_steps, self.dtype)
        s = self.state_cond(state)
        if taps is not None:
            taps["exp.state_emb"] = s
        if decomposed:
            tparts = self.precompute_time_part(ts)
            sblocks, sfinal = self.precompute_state_part(s)
        a = noise.to(self.dtype)
        dt = 1.0 / num_steps
        for k, t in enumerate(ts):
            if decomposed:
                tb, tf = tparts[k]
                ada_blocks = [tb[i] + sblocks[i] for i in range(E.depth)]
                ada_final = tf + sfinal
            else:
                c = torch.cat([self.time_emb(t), s], dim=1)
                ada_blocks, ada_final = self.ada(c)
            if taps is not None:
                taps[f"exp.step{k}.t"] = t
                taps[f"exp.step{k}.noisy_action"] = a
                for i in range(E.depth):
                    taps[f"exp.step{k}.block{i:02d}.ada"] = ada_blocks[i]
            v = self.velocity(a, ada_blocks, ada_final, K, V, mask, taps, k)
            if taps is not None:
                taps[f"exp.step{k}.velocity"] = v
            a = v * dt + a
        return a


def unnormalize_action(x: torch.Tensor, norm: Dict[str, torch.Tensor]) -> torch.Tensor:
    return (x.float() - norm["offset"]) / norm["scale"]


def rescale_gripper_width(action: torch.Tensor) -> torch.Tensor:
    """README / deploy post-step (NOT part of RDTInferencer.step): gripper width [0, 0.088] -> [0, 0.1] m."""
    out = action.clone()
    for r in range(2):
        out[..., r * 10 + 9] = out[..., r * 10 + 9] / 0.088 * 0.1
    return out