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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# Copyright (c) 2026 Simulacra Research Inc.
# SPDX-License-Identifier: Apache-2.0


from __future__ import annotations

import equinox as eqx
import jax
import jax.numpy as jnp
from jaxtyping import Array, Float, Int, PRNGKeyArray

from .odd_ops import Linear, _RMS
from .tree import (
    _inline_norm_forward,
    _tagged_bounded_ngpt_gain,
    _tagged_dense,
    _tagged_dense_no_bias,
    _tree_sphere,
)


def _pool_masked_softmax(scores, mask, axis):
    neg = jnp.asarray(-1.0e30, dtype=scores.dtype)
    scores = jnp.where(mask > 0, scores, neg)
    return jax.nn.softmax(scores, axis=axis)


class GDescriptorPool(eqx.Module):
    W_q: Float[Array, "d_g hq"]
    K: Linear
    V: Linear
    ln_in: _RMS
    n_heads: int = eqx.field(static=True)
    d_k: int = eqx.field(static=True)
    d_v: int = eqx.field(static=True)
    tag: str = eqx.field(static=True, default="")

    def __init__(
        self,
        d_g: int,
        d_x: int,
        *,
        key: PRNGKeyArray,
        n_heads: int = 4,
        d_k: int = 64,
        d_v: int = 64,
        tag: str = "",
    ):
        kq, kk, kv = jax.random.split(key, 3)
        self.W_q = jax.random.normal(kq, (d_g, n_heads * d_k)) * (d_g**-0.5)
        self.K = Linear(d_x, n_heads * d_k, key=kk)
        self.V = Linear(d_x, n_heads * d_v, key=kv)
        self.ln_in = _RMS(d_x)
        self.n_heads = n_heads
        self.d_k = d_k
        self.d_v = d_v
        self.tag = tag

    @property
    def d_out(self) -> int:
        return self.n_heads * self.d_v

    def __call__(
        self,
        g,
        xs,
        mask,
        *,
        kfac_structural_mask=None,
        kfac_update_mask=None,
        kfac_scan_shared: bool = False,
        kfac_repeat_ndim: int = 1,
        kfac_context_primal_reused_over_walkers: bool = False,
    ):

        n = xs.shape[0]
        xn = _inline_norm_forward(
            self.ln_in,
            xs,
            pathway="even",
            kfac_structural_mask=kfac_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
        q_structural_mask = (
            jnp.any(jnp.asarray(kfac_structural_mask).astype(bool))
            if kfac_update_mask is None and kfac_structural_mask is not None
            else kfac_update_mask
        )
        q = _tagged_dense_no_bias(
            self.W_q,
            g,
            tag_id=f"{self.tag}.W_q",
            pathway="even",
            kfac_structural_mask=q_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=0,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        ).reshape(self.n_heads, self.d_k)
        k = _tagged_dense(
            self.K.weight,
            self.K.bias,
            xn,
            tag_id=self.K._use_id,
            pathway="even",
            kfac_structural_mask=kfac_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        ).reshape(n, self.n_heads, self.d_k)
        v = _tagged_dense(
            self.V.weight,
            self.V.bias,
            xn,
            tag_id=self.V._use_id,
            pathway="even",
            kfac_structural_mask=kfac_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        ).reshape(n, self.n_heads, self.d_v)
        scores = jnp.einsum("hd,nhd->hn", q, k) / jnp.sqrt(
            jnp.asarray(self.d_k, dtype=xs.dtype)
        )
        attn = _pool_masked_softmax(scores, mask[None, :], axis=-1)
        out = jnp.einsum("hn,nhv->hv", attn, v)
        return out.reshape(-1)


def _global_update_parameters(d_g: int, d_pool: int, tap_dim: int, key):
    tap = int(tap_dim)
    if tap < 1 or tap >= int(d_g):
        raise ValueError("global tap dimension must be positive and smaller than d_g")
    d_in = tap + int(d_pool)
    d_hidden = 2 * int(d_g)
    k1, k2, k3 = jax.random.split(key, 3)
    return (
        jax.random.normal(k1, (d_in, d_hidden)) * (d_in**-0.5),
        jnp.zeros((d_hidden,)),
        jax.random.normal(k2, (d_hidden, d_g)) * (d_hidden**-0.5),
        jnp.zeros((d_g,)),
        jnp.ones((d_in,)),
        jax.random.normal(k3, (d_g, tap)) * (d_g**-0.5),
    )


def _global_delta(
    update,
    g,
    pool,
    *,
    kfac_structural_mask,
    kfac_g_structural_mask,
    kfac_scan_shared,
    kfac_repeat_ndim,
    kfac_context_primal_reused_over_walkers,
):
    from hamiltonzero.model.tree import _tagged_rms_eqx_style
    from hamiltonzero.model.fused_silu import fused_silu

    g_mask = (
        kfac_structural_mask
        if kfac_g_structural_mask is None
        else kfac_g_structural_mask
    )
    g_in = _tagged_dense_no_bias(
        update.g_tap_w,
        g,
        tag_id=f"{update.tag}.gtap",
        pathway="even",
        kfac_structural_mask=g_mask,
        kfac_scan_shared=(
            kfac_scan_shared if kfac_g_structural_mask is None else False
        ),
        kfac_repeat_ndim=(kfac_repeat_ndim if kfac_g_structural_mask is None else 0),
        kfac_context_primal_reused_over_walkers=(
            kfac_context_primal_reused_over_walkers
        ),
    )
    x = jnp.concatenate([g_in, pool.astype(g.dtype)])
    x = _tagged_rms_eqx_style(
        update.ln_s,
        x,
        tag_id=f"{update.tag}.ln",
        pathway="even",
        kfac_structural_mask=kfac_structural_mask,
        kfac_scan_shared=kfac_scan_shared,
        kfac_repeat_ndim=kfac_repeat_ndim,
        kfac_context_primal_reused_over_walkers=(
            kfac_context_primal_reused_over_walkers
        ),
    )
    hidden = fused_silu(
        _tagged_dense(
            update.w1,
            update.b1,
            x,
            tag_id=f"{update.tag}.ffn1",
            pathway="even",
            kfac_structural_mask=kfac_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
    )
    return _tagged_dense(
        update.w2,
        update.b2,
        hidden,
        tag_id=f"{update.tag}.ffn2",
        pathway="even",
        kfac_structural_mask=kfac_structural_mask,
        kfac_scan_shared=kfac_scan_shared,
        kfac_repeat_ndim=kfac_repeat_ndim,
        kfac_context_primal_reused_over_walkers=(
            kfac_context_primal_reused_over_walkers
        ),
    )


class ResidualGlobalUpdate(eqx.Module):
    w1: Float[Array, "d_in d_hidden"]
    b1: Float[Array, "d_hidden"]
    w2: Float[Array, "d_hidden d_g"]
    b2: Float[Array, "d_g"]
    ln_s: Float[Array, "d_in"]
    g_tap_w: Float[Array, "d_g tap_dim"]
    residual_gain: float = eqx.field(static=True)
    tag: str = eqx.field(static=True)

    def __init__(
        self,
        d_g: int,
        d_pool: int,
        *,
        key: PRNGKeyArray,
        tap_dim: int,
        residual_gain: float,
        tag: str,
    ):
        (
            self.w1,
            self.b1,
            self.w2,
            self.b2,
            self.ln_s,
            self.g_tap_w,
        ) = _global_update_parameters(d_g, d_pool, tap_dim, key)
        self.residual_gain = float(residual_gain)
        self.tag = tag

    def __call__(
        self,
        g,
        pool,
        *,
        kfac_structural_mask=None,
        kfac_g_structural_mask=None,
        kfac_scan_shared: bool = False,
        kfac_repeat_ndim: int = 0,
        kfac_context_primal_reused_over_walkers: bool = False,
    ):
        delta = _global_delta(
            self,
            g,
            pool,
            kfac_structural_mask=kfac_structural_mask,
            kfac_g_structural_mask=kfac_g_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
        return g + self.residual_gain * delta


class BoundaryGlobalUpdate(eqx.Module):
    w1: Float[Array, "d_in d_hidden"]
    b1: Float[Array, "d_hidden"]
    w2: Float[Array, "d_hidden d_g"]
    b2: Float[Array, "d_g"]
    ln_s: Float[Array, "d_in"]
    g_tap_w: Float[Array, "d_g tap_dim"]
    tag: str = eqx.field(static=True)

    def __init__(
        self, d_g: int, d_pool: int, *, key: PRNGKeyArray, tap_dim: int, tag: str
    ):
        (
            self.w1,
            self.b1,
            self.w2,
            self.b2,
            self.ln_s,
            self.g_tap_w,
        ) = _global_update_parameters(d_g, d_pool, tap_dim, key)
        self.tag = tag

    def __call__(
        self,
        g,
        pool,
        *,
        kfac_structural_mask=None,
        kfac_g_structural_mask=None,
        kfac_scan_shared: bool = False,
        kfac_repeat_ndim: int = 0,
        kfac_context_primal_reused_over_walkers: bool = False,
    ):
        delta = _global_delta(
            self,
            g,
            pool,
            kfac_structural_mask=kfac_structural_mask,
            kfac_g_structural_mask=kfac_g_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
        return _tree_sphere(g + delta)


class TreeGlobalUpdate(eqx.Module):
    w1: Float[Array, "d_in d_hidden"]
    b1: Float[Array, "d_hidden"]
    w2: Float[Array, "d_hidden d_g"]
    b2: Float[Array, "d_g"]
    ln_s: Float[Array, "d_in"]
    alpha: Float[Array, "d_g"]
    g_tap_w: Float[Array, "d_g tap_dim"]
    alpha_max: float = eqx.field(static=True)
    tag: str = eqx.field(static=True)

    def __init__(
        self,
        d_g: int,
        d_pool: int,
        *,
        key: PRNGKeyArray,
        tap_dim: int,
        alpha_init: float,
        alpha_max: float,
        tag: str,
    ):
        (
            self.w1,
            self.b1,
            self.w2,
            self.b2,
            self.ln_s,
            self.g_tap_w,
        ) = _global_update_parameters(d_g, d_pool, tap_dim, key)
        self.alpha = float(alpha_init) * jnp.ones((d_g,))
        self.alpha_max = float(alpha_max)
        self.tag = tag

    def __call__(
        self,
        g,
        pool,
        update_mask=None,
        *,
        kfac_structural_mask=None,
        kfac_g_structural_mask=None,
        kfac_scan_shared: bool = False,
        kfac_repeat_ndim: int = 0,
        kfac_context_primal_reused_over_walkers: bool = False,
    ):
        delta = _global_delta(
            self,
            g,
            pool,
            kfac_structural_mask=kfac_structural_mask,
            kfac_g_structural_mask=kfac_g_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
        skip = _tree_sphere(g)
        proposal = _tree_sphere(delta)
        direction = proposal - skip
        gain = _tagged_bounded_ngpt_gain(
            self.alpha,
            direction,
            max_gain=self.alpha_max,
            tag_id=f"{self.tag}.alpha",
            pathway="even",
            kfac_structural_mask=kfac_structural_mask,
            kfac_scan_shared=kfac_scan_shared,
            kfac_repeat_ndim=kfac_repeat_ndim,
            kfac_context_primal_reused_over_walkers=(
                kfac_context_primal_reused_over_walkers
            ),
        )
        updated = _tree_sphere(skip + gain * direction)
        if update_mask is None:
            return updated
        active = jnp.asarray(update_mask).astype(bool)
        while active.ndim < updated.ndim:
            active = active[..., None]
        return jnp.where(active, updated, g)


class EdgeRowColGlobalUpdate(eqx.Module):
    row_pool: GDescriptorPool
    col_pool: GDescriptorPool
    set_pool: GDescriptorPool
    update: BoundaryGlobalUpdate

    def __init__(
        self,
        d_g: int,
        d_edge: int,
        *,
        key: PRNGKeyArray,
        n_heads: int = 4,
        d_k: int = 64,
        d_v: int = 64,
        tag: str = "",
        tap_dim: int,
    ):
        kr, kc, ks, ku = jax.random.split(key, 4)
        self.row_pool = GDescriptorPool(
            d_g, d_edge, key=kr, n_heads=n_heads, d_k=d_k, d_v=d_v, tag=f"{tag}.row"
        )
        self.col_pool = GDescriptorPool(
            d_g, d_edge, key=kc, n_heads=n_heads, d_k=d_k, d_v=d_v, tag=f"{tag}.col"
        )
        self.set_pool = GDescriptorPool(
            d_g,
            n_heads * d_v,
            key=ks,
            n_heads=n_heads,
            d_k=d_k,
            d_v=d_v,
            tag=f"{tag}.set",
        )
        self.update = BoundaryGlobalUpdate(
            d_g,
            n_heads * d_v,
            key=ku,
            tag=f"{tag}.upd",
            tap_dim=tap_dim,
        )

    def __call__(self, g, edge, mask):

        system_active = jnp.any(mask.astype(bool))
        rows = jax.vmap(
            lambda ei, mi: self.row_pool(
                g,
                ei,
                mask,
                kfac_structural_mask=mi * mask,
                kfac_update_mask=system_active,
                kfac_scan_shared=False,
                kfac_repeat_ndim=2,
            )
        )(edge, mask)
        cols = jax.vmap(
            lambda ej, mj: self.col_pool(
                g,
                ej,
                mask,
                kfac_structural_mask=mj * mask,
                kfac_update_mask=system_active,
                kfac_scan_shared=False,
                kfac_repeat_ndim=2,
            )
        )(jnp.swapaxes(edge, 0, 1), mask)
        descs = jnp.concatenate([rows, cols], axis=0)
        dmask = jnp.concatenate([mask, mask], axis=0)
        pooled = self.set_pool(
            g,
            descs,
            dmask,
            kfac_structural_mask=dmask,
            kfac_update_mask=system_active,
            kfac_scan_shared=False,
            kfac_repeat_ndim=1,
        )
        return self.update(
            g,
            pooled,
            kfac_structural_mask=system_active,
            kfac_scan_shared=False,
        )