Repository navigation
Expand file tree
/
Copy pathmodule.py
More file actions
305 lines (266 loc) · 10.1 KB
/
Copy pathmodule.py
File metadata and controls
305 lines (266 loc) · 10.1 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""Neural modules used by the clean INTACT implementation."""
from __future__ import annotations
import torch
import torch.nn.functional as F
from einops import rearrange
from torch import nn
def modulate(x: torch.Tensor, shift: torch.Tensor, scale: torch.Tensor) -> torch.Tensor:
return x * (1 + scale) + shift
class SIGReg(nn.Module):
"""Sketch Isotropic Gaussian Regularizer."""
def __init__(self, knots: int = 17, num_proj: int = 1024) -> None:
super().__init__()
self.num_proj = num_proj
t = torch.linspace(0, 3, knots, dtype=torch.float32)
dt = 3 / (knots - 1)
weights = torch.full((knots,), 2 * dt, dtype=torch.float32)
weights[[0, -1]] = dt
window = torch.exp(-t.square() / 2)
self.register_buffer("t", t)
self.register_buffer("phi", window)
self.register_buffer("weights", weights * window)
def forward(self, proj: torch.Tensor) -> torch.Tensor:
# Compute the statistic in fp32 so bf16 training remains numerically valid.
proj32 = proj.float()
directions = torch.randn(
proj32.size(-1), self.num_proj, device=proj.device, dtype=torch.float32
)
directions = directions / directions.norm(p=2, dim=0).clamp_min(1e-8)
x_t = (proj32 @ directions).unsqueeze(-1) * self.t
error = (
(x_t.cos().mean(-3) - self.phi).square()
+ x_t.sin().mean(-3).square()
)
statistic = (error @ self.weights) * proj32.size(-2)
return statistic.mean().to(dtype=proj.dtype)
class FeedForward(nn.Module):
def __init__(self, dim: int, hidden_dim: int, dropout: float = 0.0) -> None:
super().__init__()
self.net = nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, hidden_dim),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim),
nn.Dropout(dropout),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.net(x)
class Attention(nn.Module):
def __init__(
self, dim: int, heads: int = 8, dim_head: int = 64, dropout: float = 0.0
) -> None:
super().__init__()
inner_dim = dim_head * heads
self.heads = heads
self.dropout = dropout
self.norm = nn.LayerNorm(dim)
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias=False)
self.to_out = (
nn.Sequential(nn.Linear(inner_dim, dim), nn.Dropout(dropout))
if inner_dim != dim
else nn.Identity()
)
def forward(self, x: torch.Tensor, causal: bool = True) -> torch.Tensor:
x = self.norm(x)
qkv = self.to_qkv(x).chunk(3, dim=-1)
q, k, v = (
rearrange(value, "b t (h d) -> b h t d", h=self.heads) for value in qkv
)
dropout = self.dropout if self.training else 0.0
output = F.scaled_dot_product_attention(
q, k, v, dropout_p=dropout, is_causal=causal
)
return self.to_out(rearrange(output, "b h t d -> b t (h d)"))
class ConditionalBlock(nn.Module):
def __init__(
self, dim: int, heads: int, dim_head: int, mlp_dim: int, dropout: float = 0.0
) -> None:
super().__init__()
self.attn = Attention(dim, heads=heads, dim_head=dim_head, dropout=dropout)
self.mlp = FeedForward(dim, mlp_dim, dropout=dropout)
self.norm1 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
self.norm2 = nn.LayerNorm(dim, elementwise_affine=False, eps=1e-6)
self.modulation = nn.Sequential(nn.SiLU(), nn.Linear(dim, 6 * dim))
nn.init.zeros_(self.modulation[-1].weight)
nn.init.zeros_(self.modulation[-1].bias)
def forward(self, x: torch.Tensor, condition: torch.Tensor) -> torch.Tensor:
shift_a, scale_a, gate_a, shift_m, scale_m, gate_m = self.modulation(
condition
).chunk(6, dim=-1)
x = x + gate_a * self.attn(modulate(self.norm1(x), shift_a, scale_a))
x = x + gate_m * self.mlp(modulate(self.norm2(x), shift_m, scale_m))
return x
class ConditionalTransformer(nn.Module):
def __init__(
self,
input_dim: int,
hidden_dim: int,
output_dim: int,
depth: int,
heads: int,
dim_head: int,
mlp_dim: int,
dropout: float = 0.0,
) -> None:
super().__init__()
self.input_proj = (
nn.Linear(input_dim, hidden_dim) if input_dim != hidden_dim else nn.Identity()
)
self.condition_proj = (
nn.Linear(input_dim, hidden_dim) if input_dim != hidden_dim else nn.Identity()
)
self.layers = nn.ModuleList(
[
ConditionalBlock(hidden_dim, heads, dim_head, mlp_dim, dropout)
for _ in range(depth)
]
)
self.norm = nn.LayerNorm(hidden_dim)
self.output_proj = (
nn.Linear(hidden_dim, output_dim)
if hidden_dim != output_dim
else nn.Identity()
)
def forward(self, x: torch.Tensor, condition: torch.Tensor) -> torch.Tensor:
x = self.input_proj(x)
condition = self.condition_proj(condition)
for layer in self.layers:
x = layer(x, condition)
return self.output_proj(self.norm(x))
class Embedder(nn.Module):
"""Embed one flattened action block at each latent time step."""
def __init__(
self,
input_dim: int = 10,
smoothed_dim: int | None = None,
emb_dim: int = 192,
mlp_scale: int = 4,
) -> None:
super().__init__()
smoothed_dim = smoothed_dim or input_dim
self.patch_embed = nn.Conv1d(input_dim, smoothed_dim, kernel_size=1)
self.embed = nn.Sequential(
nn.Linear(smoothed_dim, mlp_scale * emb_dim),
nn.SiLU(),
nn.Linear(mlp_scale * emb_dim, emb_dim),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.patch_embed(x.float().permute(0, 2, 1)).permute(0, 2, 1)
return self.embed(x)
class MLP(nn.Module):
def __init__(
self,
input_dim: int,
hidden_dim: int,
output_dim: int | None = None,
norm_fn=nn.LayerNorm,
act_fn=nn.GELU,
) -> None:
super().__init__()
norm = norm_fn(hidden_dim) if norm_fn is not None else nn.Identity()
self.net = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
norm,
act_fn(),
nn.Linear(hidden_dim, output_dim or input_dim),
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.net(x)
class IntentActionActor(nn.Module):
"""Shared local/goal action law with INTACT's exact four-slot grammar.
The slots are ``[z_t, m_t, z_t * m_t, A(a_{t-1})]``. Both local and goal
intents use this same module and differ only in how ``m_t`` is constructed.
"""
def __init__(
self,
*,
embed_dim: int,
action_emb_dim: int,
action_dim: int,
hidden_dim: int = 1024,
depth: int = 3,
dropout: float = 0.0,
min_log_std: float = -5.0,
max_log_std: float = 2.0,
) -> None:
super().__init__()
if depth < 1:
raise ValueError("IntentActionActor depth must be positive")
self.embed_dim = embed_dim
self.action_emb_dim = action_emb_dim
self.action_dim = action_dim
self.min_log_std = min_log_std
self.max_log_std = max_log_std
layers: list[nn.Module] = [
nn.Linear(3 * embed_dim + action_emb_dim, hidden_dim),
nn.LayerNorm(hidden_dim),
nn.GELU(),
]
for _ in range(depth - 1):
layers.extend(
[
nn.Dropout(dropout),
nn.Linear(hidden_dim, hidden_dim),
nn.LayerNorm(hidden_dim),
nn.GELU(),
]
)
layers.append(nn.Linear(hidden_dim, 2 * action_dim))
self.net = nn.Sequential(*layers)
def actor_features(
self, z: torch.Tensor, intent: torch.Tensor, prev_act_emb: torch.Tensor
) -> torch.Tensor:
if z.shape != intent.shape:
raise ValueError(
f"z and intent must have the same shape, got {z.shape} and {intent.shape}"
)
return torch.cat([z, intent, z * intent, prev_act_emb], dim=-1)
def forward(
self, z: torch.Tensor, intent: torch.Tensor, prev_act_emb: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor]:
params = self.net(self.actor_features(z, intent, prev_act_emb))
mean, log_std = params.chunk(2, dim=-1)
return mean, log_std.clamp(self.min_log_std, self.max_log_std)
def action_mean(
self, z: torch.Tensor, intent: torch.Tensor, prev_act_emb: torch.Tensor
) -> torch.Tensor:
return self(z, intent, prev_act_emb)[0]
class ARPredictor(nn.Module):
"""Autoregressive action-conditioned latent predictor from LeWM."""
def __init__(
self,
*,
num_frames: int,
depth: int,
heads: int,
mlp_dim: int,
input_dim: int,
hidden_dim: int,
output_dim: int | None = None,
dim_head: int = 64,
dropout: float = 0.0,
emb_dropout: float = 0.0,
) -> None:
super().__init__()
self.pos_embedding = nn.Parameter(torch.randn(1, num_frames, input_dim))
self.dropout = nn.Dropout(emb_dropout)
self.transformer = ConditionalTransformer(
input_dim=input_dim,
hidden_dim=hidden_dim,
output_dim=output_dim or input_dim,
depth=depth,
heads=heads,
dim_head=dim_head,
mlp_dim=mlp_dim,
dropout=dropout,
)
def forward(self, x: torch.Tensor, condition: torch.Tensor) -> torch.Tensor:
length = x.size(1)
if length > self.pos_embedding.size(1):
raise ValueError(
f"Predictor received {length} frames, max is {self.pos_embedding.size(1)}"
)
return self.transformer(
self.dropout(x + self.pos_embedding[:, :length]), condition
)