File size: 9,463 Bytes
4c27541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Numpy reference forward pass of a Vision Transformer image classifier (model_spec.json describes it).

Requires numpy only. Call ``forward(params, x_uint8)``:

* ``params``: dict of float arrays keyed by parameter name, PyTorch layout (linear weight [out, in], patch projection
  weight [DIM, C, PATCH, PATCH]).
* ``x_uint8``: raw uint8 images of shape [N, *INPUT_SHAPE] (NCHW).
* returns float32 logits [N, N_CLASSES]; the prediction is the argmax over axis 1.

``features(params, x_uint8)`` returns the float64 head input f [N, DIM] (see below).

Preprocessing: x / 255, then per channel (x - MEAN[c]) / STD[c].

Architecture (fixed; ``expected_shapes()`` lists every parameter, and any other name or shape is refused):

* patch projection ``tokenizer.conv_layers.0.0``: the image is cut into non-overlapping PATCH x PATCH patches, read
  in row-major order of the patch grid; each patch, flattened in (channel, row, column) order, is multiplied by the
  weight reshaped to [DIM, C * PATCH * PATCH] and the bias is added. This equals a 2-D convolution with kernel and
  stride PATCH and no padding. It gives N_PATCHES tokens of width DIM.
* the class token ``classifier.class_emb`` [1, 1, DIM] is put before the patch tokens (token 0), and the positional
  embedding ``classifier.positional_emb`` [1, N_PATCHES + 1, DIM] is added to all N_PATCHES + 1 tokens.
* DEPTH layers ``classifier.blocks.<i>``, i = 0 .. DEPTH - 1, each computing, on the token matrix h:

      h = h + proj(attention(pre_norm(h)))
      h = norm1(h)
      h = h + linear2(gelu(linear1(h)))

  ``norm1`` normalises the residual stream itself. ``self_attn.qkv`` (no bias) gives q, k and v as its output
  columns 0 .. DIM - 1, DIM .. 2 DIM - 1 and 2 DIM .. 3 DIM - 1. Each is split into HEADS heads of HEAD_DIM columns
  (head j uses columns j * HEAD_DIM .. (j + 1) * HEAD_DIM - 1); head j computes softmax(q_j k_j^T * HEAD_DIM ** -0.5)
  over all tokens and mixes v_j with it; the heads are concatenated in order and ``self_attn.proj`` (with bias) is
  applied. ``linear1`` maps DIM to MLP_HIDDEN and ``linear2`` back to DIM, both with bias.
* final LayerNorm ``classifier.norm``; the class token's row (token 0) is the feature vector f [N, DIM].
* linear head: logits = f @ ``classifier.fc.weight``.T + ``classifier.fc.bias``.

LayerNorm: (a - mean) / sqrt(var + LN_EPS) * weight + bias over the last axis, with the biased variance.
gelu is its tanh form: 0.5 * a * (1 + tanh(sqrt(2 / pi) * (a + 0.044715 * a ** 3))).
All arithmetic runs in float64 and the logits are cast once to float32. Inputs are processed in chunks of at most
CHUNK images, which bounds memory (about 0.2 GB).
"""

from __future__ import annotations

import math

import numpy as np

INPUT_SHAPE = (3, 32, 32)
MEAN = np.array([0.490903318493413, 0.4817690980200674, 0.4460368943397672], dtype=np.float64)
STD = np.array([0.24706968464047946, 0.24352667019453098, 0.26164691226372544], dtype=np.float64)
N_CLASSES = 10
LN_EPS = 1e-05
PATCH = 4
DIM = 256
DEPTH = 7
HEADS = 4
MLP_HIDDEN = 512
HEAD_DIM = DIM // HEADS
GRID = (INPUT_SHAPE[1] // PATCH, INPUT_SHAPE[2] // PATCH)
N_PATCHES = GRID[0] * GRID[1]
N_TOKENS = N_PATCHES + 1
SCALE = HEAD_DIM ** -0.5
GELU_C = math.sqrt(2.0 / math.pi)
CHUNK = 250
PATCH_WEIGHT = "tokenizer.conv_layers.0.0.weight"
PATCH_BIAS = "tokenizer.conv_layers.0.0.bias"


def expected_shapes() -> dict[str, tuple[int, ...]]:
    """Every parameter name of the model with its shape, in forward order."""
    shapes: dict[str, tuple[int, ...]] = {
        PATCH_WEIGHT: (DIM, INPUT_SHAPE[0], PATCH, PATCH),
        PATCH_BIAS: (DIM,),
        "classifier.class_emb": (1, 1, DIM),
        "classifier.positional_emb": (1, N_TOKENS, DIM),
    }
    for index in range(DEPTH):
        block = f"classifier.blocks.{index}"
        shapes.update({
            f"{block}.pre_norm.weight": (DIM,),
            f"{block}.pre_norm.bias": (DIM,),
            f"{block}.self_attn.qkv.weight": (3 * DIM, DIM),
            f"{block}.self_attn.proj.weight": (DIM, DIM),
            f"{block}.self_attn.proj.bias": (DIM,),
            f"{block}.linear1.weight": (MLP_HIDDEN, DIM),
            f"{block}.linear1.bias": (MLP_HIDDEN,),
            f"{block}.norm1.weight": (DIM,),
            f"{block}.norm1.bias": (DIM,),
            f"{block}.linear2.weight": (DIM, MLP_HIDDEN),
            f"{block}.linear2.bias": (DIM,),
        })
    shapes.update({
        "classifier.norm.weight": (DIM,),
        "classifier.norm.bias": (DIM,),
        "classifier.fc.weight": (N_CLASSES, DIM),
        "classifier.fc.bias": (N_CLASSES,),
    })
    return shapes


def _checked(params: dict) -> dict[str, np.ndarray]:
    expected = expected_shapes()
    names = {str(key) for key in params}
    if names != set(expected):
        missing, extra = sorted(set(expected) - names), sorted(names - set(expected))
        raise KeyError(f"parameter names differ from the architecture: missing {missing}, unexpected {extra}")
    tensors = {}
    for name, shape in expected.items():
        value = np.asarray(params[name])
        if not np.issubdtype(value.dtype, np.floating):
            raise TypeError(f"{name} must be a float array, got {value.dtype}")
        if tuple(value.shape) != shape:
            raise ValueError(f"{name} has shape {tuple(value.shape)}, expected {shape}")
        tensors[name] = value.astype(np.float64)
    return tensors


def _layernorm(a: np.ndarray, weight: np.ndarray, bias: np.ndarray) -> np.ndarray:
    centred = a - a.mean(axis=-1, keepdims=True)
    var = (centred * centred).mean(axis=-1, keepdims=True)
    return centred / np.sqrt(var + LN_EPS) * weight + bias


def _gelu(a: np.ndarray) -> np.ndarray:
    return 0.5 * a * (1.0 + np.tanh(GELU_C * (a + 0.044715 * a * a * a)))


def _linear(a: np.ndarray, weight: np.ndarray, bias: np.ndarray | None) -> np.ndarray:
    out = a.reshape(-1, a.shape[-1]) @ weight.T
    if bias is not None:
        out += bias
    return out.reshape(*a.shape[:-1], weight.shape[0])


def _patches(x: np.ndarray) -> np.ndarray:
    """[n, C, H, W] -> [n, N_PATCHES, C * PATCH * PATCH], patches in row-major grid order, each in (c, row, col)."""
    n, channels = x.shape[0], x.shape[1]
    grid = x.reshape(n, channels, GRID[0], PATCH, GRID[1], PATCH)
    return grid.transpose(0, 2, 4, 1, 3, 5).reshape(n, N_PATCHES, channels * PATCH * PATCH)


def _attention(a: np.ndarray, tensors: dict[str, np.ndarray], block: str) -> np.ndarray:
    n = a.shape[0]
    qkv = _linear(a, tensors[f"{block}.self_attn.qkv.weight"], None)
    qkv = qkv.reshape(n, N_TOKENS, 3, HEADS, HEAD_DIM).transpose(2, 0, 3, 1, 4)
    q, k, v = qkv[0], qkv[1], qkv[2]
    scores = (q @ k.transpose(0, 1, 3, 2)) * SCALE
    scores -= scores.max(axis=-1, keepdims=True)
    np.exp(scores, out=scores)
    scores /= scores.sum(axis=-1, keepdims=True)
    mixed = (scores @ v).transpose(0, 2, 1, 3).reshape(n, N_TOKENS, DIM)
    return _linear(mixed, tensors[f"{block}.self_attn.proj.weight"], tensors[f"{block}.self_attn.proj.bias"])


def _features(tensors: dict[str, np.ndarray], x: np.ndarray) -> np.ndarray:
    h = x.astype(np.float64) / 255.0
    h = (h - MEAN.reshape(1, -1, 1, 1)) / STD.reshape(1, -1, 1, 1)
    tokens = _linear(_patches(h), tensors[PATCH_WEIGHT].reshape(DIM, -1), tensors[PATCH_BIAS])
    cls = np.broadcast_to(tensors["classifier.class_emb"], (x.shape[0], 1, DIM))
    h = np.concatenate([cls, tokens], axis=1) + tensors["classifier.positional_emb"]
    for index in range(DEPTH):
        block = f"classifier.blocks.{index}"
        h = h + _attention(_layernorm(h, tensors[f"{block}.pre_norm.weight"], tensors[f"{block}.pre_norm.bias"]),
                           tensors, block)
        h = _layernorm(h, tensors[f"{block}.norm1.weight"], tensors[f"{block}.norm1.bias"])
        hidden = _gelu(_linear(h, tensors[f"{block}.linear1.weight"], tensors[f"{block}.linear1.bias"]))
        h = h + _linear(hidden, tensors[f"{block}.linear2.weight"], tensors[f"{block}.linear2.bias"])
    return _layernorm(h[:, 0], tensors["classifier.norm.weight"], tensors["classifier.norm.bias"])


def _images(x_uint8: np.ndarray) -> np.ndarray:
    x = np.asarray(x_uint8)
    if x.dtype != np.uint8:
        raise TypeError(f"images must be uint8, got {x.dtype}")
    if x.ndim != 4 or tuple(x.shape[1:]) != INPUT_SHAPE:
        raise ValueError(f"images must have shape [N, {', '.join(map(str, INPUT_SHAPE))}], got {list(x.shape)}")
    return x


def features(params: dict[str, np.ndarray], x_uint8: np.ndarray) -> np.ndarray:
    """float64 features f [N, DIM]: the class token's row after the final LayerNorm, the head's input."""
    x = _images(x_uint8)
    tensors = _checked(params)
    if x.shape[0] == 0:
        return np.zeros((0, DIM), dtype=np.float64)
    return np.concatenate([_features(tensors, x[start : start + CHUNK]) for start in range(0, x.shape[0], CHUNK)])


def forward(params: dict[str, np.ndarray], x_uint8: np.ndarray) -> np.ndarray:
    """Logits float32 [N, N_CLASSES] for raw uint8 images [N, *INPUT_SHAPE]."""
    x = _images(x_uint8)
    tensors = _checked(params)
    if x.shape[0] == 0:
        return np.zeros((0, N_CLASSES), dtype=np.float32)
    chunks = []
    for start in range(0, x.shape[0], CHUNK):
        f = _features(tensors, x[start : start + CHUNK])
        chunks.append((f @ tensors["classifier.fc.weight"].T + tensors["classifier.fc.bias"]).astype(np.float32))
    return np.concatenate(chunks, axis=0)