File size: 8,039 Bytes
573eba1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""nlrt: tiny portable runtime shared by Next Lab models.

The same layer list runs in NumPy (this file) and in the browser (nlrt.js).
Weights are stored as base64 little-endian float32 inside model.json.
Only NumPy is required to load and run a model.
"""
import base64
import json
import math

import numpy as np

__all__ = ["b64e", "b64d", "forward", "load_model", "Mulberry32", "export_sequential"]


def b64e(a):
    return base64.b64encode(np.ascontiguousarray(a, dtype="<f4").tobytes()).decode("ascii")


def b64d(s, shape=None):
    a = np.frombuffer(base64.b64decode(s), dtype="<f4").astype(np.float64)
    return a.reshape(shape) if shape is not None else a


def load_model(path):
    with open(path, "r", encoding="utf-8") as f:
        spec = json.load(f)
    for net in spec.get("nets", {}).values():
        _decode(net)
    for k, v in list(spec.get("tensors", {}).items()):
        spec["tensors"][k] = b64d(v["data"], v["shape"])
    return spec


def _decode(layers):
    for L in layers:
        for key in ("w", "b"):
            if key in L and isinstance(L[key], str):
                L[key] = b64d(L[key], L.get(key + "_shape"))


class Mulberry32:
    """Deterministic PRNG with an identical JavaScript twin in nlrt.js."""

    def __init__(self, seed):
        self.a = seed & 0xFFFFFFFF

    def random(self):
        self.a = (self.a + 0x6D2B79F5) & 0xFFFFFFFF
        t = self.a
        t = ((t ^ (t >> 15)) * (t | 1)) & 0xFFFFFFFF
        t ^= (t + (((t ^ (t >> 7)) * (t | 61)) & 0xFFFFFFFF)) & 0xFFFFFFFF
        return ((t ^ (t >> 14)) & 0xFFFFFFFF) / 4294967296.0

    def uniform(self, lo, hi):
        return lo + (hi - lo) * self.random()

    def randint(self, n):
        return int(math.floor(self.random() * n))

    def normal(self):
        u = max(self.random(), 1e-12)
        v = self.random()
        return math.sqrt(-2.0 * math.log(u)) * math.cos(2.0 * math.pi * v)


def _pair(v):
    return list(v) if isinstance(v, (list, tuple)) else [v, v]


def _conv2d(x, w, b, stride, pad):
    n, c, h, wd = x.shape
    o, _, kh, kw = w.shape
    sh, sw = stride
    ph, pw = pad
    xp = np.pad(x, ((0, 0), (0, 0), (ph, ph), (pw, pw)))
    oh = (h + 2 * ph - kh) // sh + 1
    ow = (wd + 2 * pw - kw) // sw + 1
    cols = np.empty((n, c, kh, kw, oh, ow), dtype=x.dtype)
    for i in range(kh):
        for j in range(kw):
            cols[:, :, i, j] = xp[:, :, i:i + sh * oh:sh, j:j + sw * ow:sw]
    out = np.tensordot(cols, w, axes=([1, 2, 3], [1, 2, 3]))  # n, oh, ow, o
    out = out.transpose(0, 3, 1, 2)
    if b is not None:
        out = out + b[None, :, None, None]
    return out


def _maxpool(x, k):
    kh, kw = k
    n, c, h, w = x.shape
    oh, ow = h // kh, w // kw
    x = x[:, :, :oh * kh, :ow * kw].reshape(n, c, oh, kh, ow, kw)
    return x.max(axis=(3, 5))


def forward(layers, x):
    """Run a layer list on a batch. x: (N, D) or (N, C, H, W) or (N, C, L)."""
    x = np.asarray(x, dtype=np.float64)
    for L in layers:
        t = L["type"]
        if t == "linear":
            x = x @ L["w"].T + L["b"]
        elif t == "conv2d":
            squeeze = x.ndim == 3
            if squeeze:
                x = x[:, :, None, :]
            x = _conv2d(x, L["w"], L["b"], _pair(L["stride"]), _pair(L["pad"]))
            if squeeze:
                x = x[:, :, 0, :]
        elif t == "relu":
            x = np.maximum(x, 0)
        elif t == "tanh":
            x = np.tanh(x)
        elif t == "sigmoid":
            x = 1 / (1 + np.exp(-x))
        elif t == "maxpool":
            squeeze = x.ndim == 3
            if squeeze:
                x = x[:, :, None, :]
            x = _maxpool(x, _pair(L["k"]))
            if squeeze:
                x = x[:, :, 0, :]
        elif t == "gap":
            x = x.reshape(x.shape[0], x.shape[1], -1).mean(axis=2)
        elif t == "flatten":
            x = x.reshape(x.shape[0], -1)
        elif t == "upsample":
            x = x.repeat(L["s"], axis=-1)
            if x.ndim == 4:
                x = x.repeat(L["s"], axis=-2)
        elif t == "l2norm":
            x = x / np.maximum(np.linalg.norm(x, axis=1, keepdims=True), 1e-12)
        elif t == "reshape":
            x = x.reshape([x.shape[0]] + list(L["shape"]))
        else:
            raise ValueError("unknown layer " + t)
    return x


def export_sequential(seq):
    """Convert a torch nn.Sequential into a portable layer list (BatchNorm folded)."""
    import torch.nn as nn

    mods = list(seq.modules())[1:] if not isinstance(seq, (list, tuple)) else list(seq)
    mods = [m for m in mods if not isinstance(m, nn.Sequential)]
    layers = []

    def tensor(t):
        return t.detach().cpu().double().numpy()

    for m in mods:
        if isinstance(m, nn.Linear):
            b = tensor(m.bias) if m.bias is not None else np.zeros(m.out_features)
            layers.append({"type": "linear", "w": tensor(m.weight), "b": b})
        elif isinstance(m, (nn.Conv2d, nn.Conv1d)):
            w = tensor(m.weight)
            b = tensor(m.bias) if m.bias is not None else np.zeros(w.shape[0])
            if isinstance(m, nn.Conv1d):
                w = w[:, :, None, :]
                stride, pad = [1, m.stride[0]], [0, m.padding[0]]
            else:
                stride, pad = list(m.stride), list(m.padding)
            layers.append({"type": "conv2d", "w": w, "b": b, "stride": stride, "pad": pad})
        elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
            prev = layers[-1]
            assert prev["type"] in ("conv2d", "linear"), "BatchNorm must follow conv/linear"
            scale = tensor(m.weight) / np.sqrt(tensor(m.running_var) + m.eps)
            shift = tensor(m.bias) - tensor(m.running_mean) * scale
            if prev["type"] == "conv2d":
                prev["w"] = prev["w"] * scale[:, None, None, None]
            else:
                prev["w"] = prev["w"] * scale[:, None]
            prev["b"] = prev["b"] * scale + shift
        elif isinstance(m, nn.ReLU):
            layers.append({"type": "relu"})
        elif isinstance(m, nn.Tanh):
            layers.append({"type": "tanh"})
        elif isinstance(m, nn.Sigmoid):
            layers.append({"type": "sigmoid"})
        elif isinstance(m, nn.MaxPool2d):
            layers.append({"type": "maxpool", "k": _pair(m.kernel_size)})
        elif isinstance(m, nn.MaxPool1d):
            k = m.kernel_size if isinstance(m, int) else m.kernel_size
            layers.append({"type": "maxpool", "k": [1, k if isinstance(k, int) else k[0]]})
        elif isinstance(m, (nn.AdaptiveAvgPool2d, nn.AdaptiveAvgPool1d)):
            layers.append({"type": "gap"})
        elif isinstance(m, nn.Flatten):
            layers.append({"type": "flatten"})
        elif isinstance(m, nn.Upsample):
            layers.append({"type": "upsample", "s": int(m.scale_factor)})
        elif isinstance(m, nn.Dropout):
            continue
        elif m.__class__.__name__ == "L2Norm":
            layers.append({"type": "l2norm"})
        elif m.__class__.__name__ == "Reshape":
            layers.append({"type": "reshape", "shape": list(m.shape)})
        else:
            raise ValueError("cannot export " + m.__class__.__name__)
    return layers


def encode_layers(layers):
    out = []
    for L in layers:
        L2 = dict(L)
        for key in ("w", "b"):
            if key in L2:
                arr = np.asarray(L2[key])
                L2[key + "_shape"] = list(arr.shape)
                L2[key] = b64e(arr)
        out.append(L2)
    return out


def save_model(path, nets=None, tensors=None, meta=None):
    spec = {"format": "nlrt-1", "meta": meta or {}, "nets": {}, "tensors": {}}
    for name, layers in (nets or {}).items():
        spec["nets"][name] = encode_layers(layers)
    for name, arr in (tensors or {}).items():
        arr = np.asarray(arr)
        spec["tensors"][name] = {"shape": list(arr.shape), "data": b64e(arr)}
    with open(path, "w", encoding="utf-8") as f:
        json.dump(spec, f, separators=(",", ":"))
    return spec