File size: 7,436 Bytes
3d1cafb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Julian Model Layers.
Core building blocks: RMSNorm, RoPE, Attention, FFN.
"""

import math
from typing import Optional, Tuple

import jax
import jax.numpy as jnp
import flax.linen as nn
from flax.linen import initializers

from .config import JulianConfig


class RMSNorm(nn.Module):
    """Root Mean Square Layer Normalization."""

    dim: int
    eps: float = 1e-6

    @nn.compact
    def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
        weight = self.param(
            "weight",
            initializers.ones,
            (self.dim,)
        )

        # RMS norm
        variance = jnp.mean(x ** 2, axis=-1, keepdims=True)
        x = x * jax.lax.rsqrt(variance + self.eps)

        return x * weight


def precompute_rope_frequencies(
    dim: int,
    max_seq_len: int,
    theta: float = 10000.0,
) -> Tuple[jnp.ndarray, jnp.ndarray]:
    """Precompute RoPE sin/cos frequencies."""

    # Frequency for each dimension pair
    freqs = 1.0 / (theta ** (jnp.arange(0, dim, 2).astype(jnp.float32) / dim))

    # Position indices
    positions = jnp.arange(max_seq_len)

    # Outer product: [seq_len, dim/2]
    angles = jnp.outer(positions, freqs)

    # Sin and cos
    sin = jnp.sin(angles)
    cos = jnp.cos(angles)

    return sin, cos


def apply_rope(
    x: jnp.ndarray,
    sin: jnp.ndarray,
    cos: jnp.ndarray,
) -> jnp.ndarray:
    """Apply rotary position embeddings."""

    # x shape: [batch, seq_len, n_heads, head_dim]
    # sin/cos shape: [seq_len, head_dim/2]

    seq_len = x.shape[1]
    sin = sin[:seq_len]
    cos = cos[:seq_len]

    # Split x into pairs
    x1 = x[..., ::2]   # Even indices
    x2 = x[..., 1::2]  # Odd indices

    # Rotate
    # [batch, seq, heads, dim/2]
    sin = sin[None, :, None, :]  # Add batch and head dims
    cos = cos[None, :, None, :]

    rotated_x1 = x1 * cos - x2 * sin
    rotated_x2 = x1 * sin + x2 * cos

    # Interleave back
    rotated = jnp.stack([rotated_x1, rotated_x2], axis=-1)
    rotated = rotated.reshape(x.shape)

    return rotated


class Attention(nn.Module):
    """Multi-head self-attention with RoPE."""

    config: JulianConfig

    @nn.compact
    def __call__(
        self,
        x: jnp.ndarray,
        sin: jnp.ndarray,
        cos: jnp.ndarray,
        mask: Optional[jnp.ndarray] = None,
        deterministic: bool = True,
    ) -> jnp.ndarray:

        batch_size, seq_len, _ = x.shape
        config = self.config

        # QKV projections
        q = nn.Dense(
            config.d_model,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="q_proj",
        )(x)

        k = nn.Dense(
            config.d_model,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="k_proj",
        )(x)

        v = nn.Dense(
            config.d_model,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="v_proj",
        )(x)

        # Reshape for multi-head attention
        # [batch, seq, d_model] -> [batch, seq, n_heads, head_dim]
        q = q.reshape(batch_size, seq_len, config.n_heads, config.head_dim)
        k = k.reshape(batch_size, seq_len, config.n_heads, config.head_dim)
        v = v.reshape(batch_size, seq_len, config.n_heads, config.head_dim)

        # Apply RoPE to Q and K
        q = apply_rope(q, sin, cos)
        k = apply_rope(k, sin, cos)

        # Transpose for attention: [batch, n_heads, seq, head_dim]
        q = jnp.transpose(q, (0, 2, 1, 3))
        k = jnp.transpose(k, (0, 2, 1, 3))
        v = jnp.transpose(v, (0, 2, 1, 3))

        # Scaled dot-product attention in bfloat16 for memory efficiency
        scale = 1.0 / math.sqrt(config.head_dim)

        # Force bfloat16 for attention computation (major memory savings)
        q = q.astype(jnp.bfloat16)
        k = k.astype(jnp.bfloat16)
        v = v.astype(jnp.bfloat16)

        attn_weights = jnp.einsum("bhqd,bhkd->bhqk", q, k) * scale

        # Apply causal mask
        if mask is not None:
            attn_weights = jnp.where(mask, attn_weights, jnp.finfo(jnp.bfloat16).min)

        attn_weights = jax.nn.softmax(attn_weights.astype(jnp.float32), axis=-1)
        attn_weights = attn_weights.astype(jnp.bfloat16)

        # Dropout
        if not deterministic:
            attn_weights = nn.Dropout(
                rate=config.attention_dropout,
                deterministic=deterministic,
            )(attn_weights)

        # Apply attention to values
        attn_output = jnp.einsum("bhqk,bhkd->bhqd", attn_weights, v)

        # Reshape back: [batch, n_heads, seq, head_dim] -> [batch, seq, d_model]
        attn_output = jnp.transpose(attn_output, (0, 2, 1, 3))
        attn_output = attn_output.reshape(batch_size, seq_len, config.d_model)

        # Output projection
        output = nn.Dense(
            config.d_model,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="o_proj",
        )(attn_output)

        return output


class FeedForward(nn.Module):
    """SwiGLU Feed-Forward Network."""

    config: JulianConfig

    @nn.compact
    def __call__(
        self,
        x: jnp.ndarray,
        deterministic: bool = True,
    ) -> jnp.ndarray:

        config = self.config

        # Gate and up projections
        gate = nn.Dense(
            config.d_ff,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="gate_proj",
        )(x)

        up = nn.Dense(
            config.d_ff,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="up_proj",
        )(x)

        # SwiGLU activation
        hidden = jax.nn.silu(gate) * up

        # Dropout
        if not deterministic:
            hidden = nn.Dropout(
                rate=config.dropout,
                deterministic=deterministic,
            )(hidden)

        # Down projection
        output = nn.Dense(
            config.d_model,
            use_bias=config.use_bias,
            kernel_init=initializers.normal(config.initializer_range),
            name="down_proj",
        )(hidden)

        return output


class TransformerBlock(nn.Module):
    """Single transformer decoder block."""

    config: JulianConfig

    @nn.compact
    def __call__(
        self,
        x: jnp.ndarray,
        sin: jnp.ndarray,
        cos: jnp.ndarray,
        mask: Optional[jnp.ndarray] = None,
        deterministic: bool = True,
    ) -> jnp.ndarray:

        config = self.config

        # Pre-norm attention
        residual = x
        x = RMSNorm(config.d_model, config.rms_norm_eps, name="input_layernorm")(x)
        x = Attention(config, name="self_attn")(x, sin, cos, mask, deterministic)

        if not deterministic:
            x = nn.Dropout(rate=config.dropout, deterministic=deterministic)(x)

        x = residual + x

        # Pre-norm FFN
        residual = x
        x = RMSNorm(config.d_model, config.rms_norm_eps, name="post_attention_layernorm")(x)
        x = FeedForward(config, name="mlp")(x, deterministic)

        if not deterministic:
            x = nn.Dropout(rate=config.dropout, deterministic=deterministic)(x)

        x = residual + x

        return x