File size: 10,097 Bytes
387ced5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b56e00
 
 
 
 
387ced5
 
 
 
 
 
 
 
 
 
 
 
9b56e00
387ced5
 
 
 
9b56e00
387ced5
9b56e00
 
 
 
387ced5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b56e00
387ced5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Verify T5 encoder output against Swift implementation.

Loads the same T5 safetensors weights, runs the encoder on the same tokens,
and prints output stats for comparison with the Swift logs.
"""

import math
import mlx.core as mx
import mlx.nn as nn
import json
from pathlib import Path

MODEL_DIR = Path.home() / "Library/Application Support/Velvox/Models/audiogen-mlx/t5"


# ── T5 LayerNorm (RMSNorm, no centering) ──

class T5LayerNorm(nn.Module):
    def __init__(self, dims, eps=1e-6):
        super().__init__()
        self.weight = mx.ones((dims,))
        self.eps = eps

    def __call__(self, x):
        y = x.astype(mx.float32)
        y = y * mx.rsqrt(mx.mean(y * y, axis=-1, keepdims=True) + self.eps)
        return self.weight * y.astype(x.dtype)


# ── T5 DenseReluDense ──

class T5DenseActDense(nn.Module):
    def __init__(self, d_model, d_ff, act="relu"):
        super().__init__()
        self.wi = nn.Linear(d_model, d_ff, bias=False)
        self.wo = nn.Linear(d_ff, d_model, bias=False)
        self.act = act

    def __call__(self, x):
        h = self.wi(x)
        h = nn.relu(h) if self.act == "relu" else nn.gelu(h)
        return self.wo(h)


# ── T5 Attention (NO sqrt(d_k) scaling β€” this is T5's design) ──

class T5Attention(nn.Module):
    def __init__(self, config, has_relative_attention_bias=False):
        super().__init__()
        self.num_heads = config["num_heads"]
        self.d_kv = config["d_kv"]
        self.d_model = config["d_model"]
        self.has_relative_attention_bias = has_relative_attention_bias
        self.num_buckets = config["relative_attention_num_buckets"]
        self.max_distance = config.get("relative_attention_max_distance", 128)

        self.q = nn.Linear(self.d_model, self.num_heads * self.d_kv, bias=False)
        self.k = nn.Linear(self.d_model, self.num_heads * self.d_kv, bias=False)
        self.v = nn.Linear(self.d_model, self.num_heads * self.d_kv, bias=False)
        self.o = nn.Linear(self.num_heads * self.d_kv, self.d_model, bias=False)

        if has_relative_attention_bias:
            self.relative_attention_bias = nn.Embedding(self.num_buckets, self.num_heads)

    @staticmethod
    def _relative_position_bucket(rp, bidirectional=True, num_buckets=32, max_distance=128):
        nb = num_buckets
        result = mx.zeros(rp.shape, dtype=mx.int32)
        if bidirectional:
            nb = nb // 2
            is_pos = mx.where(rp > 0, mx.array(nb, dtype=mx.int32), mx.array(0, dtype=mx.int32))
            result = is_pos
            rp = mx.abs(rp)
        else:
            rp = -mx.minimum(rp, mx.zeros_like(rp))

        max_exact = nb // 2
        is_small = rp < max_exact

        large_rp = rp.astype(mx.float32)
        log_ratio = mx.log(large_rp / max_exact) / math.log(max_distance / max_exact)
        large_bucket = (log_ratio * (nb - max_exact)).astype(mx.int32) + max_exact
        clamped = mx.minimum(large_bucket, mx.array(nb - 1, dtype=mx.int32))

        buckets = mx.where(is_small, rp.astype(mx.int32), clamped)
        return result + buckets

    def compute_bias(self, q_len, k_len):
        if not self.has_relative_attention_bias:
            return None
        ctx = mx.arange(q_len, dtype=mx.int32)
        mem = mx.arange(k_len, dtype=mx.int32)
        rp = mem.reshape(1, -1).astype(mx.float32) - ctx.reshape(-1, 1).astype(mx.float32)
        rp_bucket = self._relative_position_bucket(
            rp, bidirectional=True,
            num_buckets=self.num_buckets, max_distance=self.max_distance
        )
        flat = rp_bucket.reshape(-1)
        bias_flat = self.relative_attention_bias(flat)
        bias = bias_flat.reshape(q_len, k_len, self.num_heads)
        bias = bias.transpose(2, 0, 1)[None, :, :, :]  # [1, H, Q, K]
        return bias

    def __call__(self, hidden, mask=None, position_bias=None):
        B, T, _ = hidden.shape
        q = self.q(hidden).reshape(B, T, self.num_heads, self.d_kv)
        k = self.k(hidden).reshape(B, T, self.num_heads, self.d_kv)
        v = self.v(hidden).reshape(B, T, self.num_heads, self.d_kv)

        q = q.transpose(0, 2, 1, 3)  # [B, H, T, d]
        k = k.transpose(0, 2, 3, 1)  # [B, H, d, T]
        v = v.transpose(0, 2, 1, 3)  # [B, H, T, d]

        # T5: NO scaling by 1/sqrt(d_k)
        scores = q @ k

        if position_bias is None:
            position_bias = self.compute_bias(T, T)
        if position_bias is not None:
            scores = scores + position_bias

        weights = mx.softmax(scores.astype(mx.float32), axis=-1).astype(scores.dtype)
        out = (weights @ v).transpose(0, 2, 1, 3).reshape(B, T, -1)
        return self.o(out)


# ── T5 Block ──

class T5Block(nn.Module):
    def __init__(self, config, has_relative_attention_bias=False):
        super().__init__()
        self.self_attn = T5Attention(config, has_relative_attention_bias)
        self.layer_norm_sa = T5LayerNorm(config["d_model"], config.get("layer_norm_epsilon", 1e-6))
        self.ff = T5DenseActDense(config["d_model"], config["d_ff"], config.get("feed_forward_proj", "relu"))
        self.layer_norm_ff = T5LayerNorm(config["d_model"], config.get("layer_norm_epsilon", 1e-6))

    def __call__(self, x, mask=None, position_bias=None):
        normed = self.layer_norm_sa(x)
        attn_out = self.self_attn(normed, mask=mask, position_bias=position_bias)
        x = x + attn_out
        normed = self.layer_norm_ff(x)
        ff_out = self.ff(normed)
        x = x + ff_out
        return x


# ── T5 Encoder ──

class T5Encoder(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.shared = nn.Embedding(config["vocab_size"], config["d_model"])
        self.blocks = [T5Block(config, has_relative_attention_bias=(i == 0))
                       for i in range(config["num_layers"])]
        self.final_layer_norm = T5LayerNorm(config["d_model"], config.get("layer_norm_epsilon", 1e-6))

    def __call__(self, input_ids):
        x = self.shared(input_ids)
        pos_bias = self.blocks[0].self_attn.compute_bias(x.shape[1], x.shape[1])
        for block in self.blocks:
            x = block(x, position_bias=pos_bias)
        return self.final_layer_norm(x)


def load_and_remap_weights(t5_dir):
    """Load safetensors and remap sanitized MLX keys to our module structure.

    The safetensors use MLX-sanitized keys with layer_0/layer_1 (underscores),
    not the original HuggingFace layer.0/layer.1 (dots).
    """
    import glob
    safetensors_files = sorted(glob.glob(str(t5_dir / "*.safetensors")))

    all_weights = {}
    for f in safetensors_files:
        w = mx.load(f)
        all_weights.update(w)

    # Separate output_proj from T5 weights
    output_proj_w = all_weights.pop("output_proj.weight", None)
    output_proj_b = all_weights.pop("output_proj.bias", None)

    # Remap sanitized keys to our module structure
    remapped = {}
    for key, val in all_weights.items():
        new_key = key

        # encoder.block.N.layer_0.SelfAttention.X β†’ blocks.N.self_attn.X
        new_key = new_key.replace("encoder.block.", "blocks.")
        new_key = new_key.replace(".layer_0.SelfAttention.", ".self_attn.")
        new_key = new_key.replace(".layer_0.layer_norm.", ".layer_norm_sa.")
        new_key = new_key.replace(".layer_1.DenseReluDense.", ".ff.")
        new_key = new_key.replace(".layer_1.layer_norm.", ".layer_norm_ff.")

        # encoder.final_layer_norm β†’ final_layer_norm
        new_key = new_key.replace("encoder.final_layer_norm.", "final_layer_norm.")

        remapped[new_key] = val

    return remapped, output_proj_w, output_proj_b


def main():
    print("=" * 60)
    print("T5 Encoder Verification (MLX Python reference)")
    print("=" * 60)

    # Load config
    with open(t5_dir / "config.json") as f:
        config = json.load(f)

    print(f"Config: d_model={config['d_model']} layers={config['num_layers']} "
          f"heads={config['num_heads']} d_kv={config['d_kv']} d_ff={config['d_ff']}")

    # Build model
    encoder = T5Encoder(config)

    # Load weights
    weights, proj_w, proj_b = load_and_remap_weights(MODEL_DIR)

    # Apply weights
    encoder.load_weights(list(weights.items()))

    # Build output_proj
    output_proj = None
    if proj_w is not None:
        output_proj = nn.Linear(proj_w.shape[1], proj_w.shape[0])
        proj_params = [("weight", proj_w)]
        if proj_b is not None:
            proj_params.append(("bias", proj_b))
        output_proj.load_weights(proj_params)
        print(f"output_proj: {proj_w.shape[1]} β†’ {proj_w.shape[0]}")

    # Test prompts with known token IDs from Swift logs
    test_cases = [
        ("dog barking", [1782, 21696, 53, 1]),
        ("cars in the street", [2948, 16, 8, 2815, 1]),
        ("A metro train leaving the platform", [71, 12810, 2412, 3140, 8, 1585, 1]),
    ]

    for prompt, token_ids in test_cases:
        print(f"\n--- '{prompt}' ---")
        print(f"Tokens: {token_ids}")

        input_ids = mx.array([token_ids], dtype=mx.int32)
        features = encoder(input_ids)
        mx.eval(features)

        print(f"Encoder output: shape={features.shape} "
              f"min={features.min().item():.7f} max={features.max().item():.7f} "
              f"sum={features.sum().item():.4f}")

        for i in range(features.shape[1]):
            pos_feat = features[0, i]
            print(f"  pos[{i}]: min={pos_feat.min().item():.5f} "
                  f"max={pos_feat.max().item():.5f} "
                  f"mean={pos_feat.mean().item():.5f}")

        if output_proj is not None:
            projected = output_proj(features)
            mx.eval(projected)
            print(f"After output_proj: shape={projected.shape} "
                  f"min={projected.min().item():.7f} max={projected.max().item():.7f} "
                  f"sum={projected.sum().item():.4f}")


if __name__ == "__main__":
    t5_dir = MODEL_DIR
    if not t5_dir.exists():
        print(f"T5 directory not found: {t5_dir}")
        exit(1)
    main()