Image Classification
Transformers
JAX
ntlm_class25
imagenet-1k
tpu
muon
convnext
hybrid
custom_code
notokenlm
Instructions to use VeroLM/NoTokenLM-ImageGen-Class-2.5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use VeroLM/NoTokenLM-ImageGen-Class-2.5 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-classification", model="VeroLM/NoTokenLM-ImageGen-Class-2.5", trust_remote_code=True) pipe("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/parrots.png")# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForImageClassification model = AutoModelForImageClassification.from_pretrained("VeroLM/NoTokenLM-ImageGen-Class-2.5", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload 2 files
Browse files- verolm-runner.js +177 -0
- verolm.json +10 -0
verolm-runner.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// VeroLM site runner for NoTokenLM-Gen-3.6 (byte-level transformer).
|
| 2 |
+
// Mirrors modeling_notoken_gen36.py: byte embedding + causal depthwise conv (residual),
|
| 3 |
+
// N x [RMSNorm -> attention(RoPE, QK-norm, value residual, per-head sigmoid gate); RMSNorm -> SwiGLU],
|
| 4 |
+
// final RMSNorm, tied output head, optional tanh logit cap.
|
| 5 |
+
// Runs on the CPU in plain JavaScript with a KV cache (same maths as the full recompute, just faster).
|
| 6 |
+
// Structural sizes (d_model, heads, layers, d_ff, conv kernel) are read from the weight shapes;
|
| 7 |
+
// rope_base / logit_cap / block_size / eod_token_id come from config.json in the repo.
|
| 8 |
+
|
| 9 |
+
const EPS = 1e-6;
|
| 10 |
+
|
| 11 |
+
export async function load({ baseUrl, tensors, tools, manifest, onProgress }) {
|
| 12 |
+
let cfg = {};
|
| 13 |
+
try { cfg = await (await fetch(baseUrl + "config.json")).json(); } catch (e) {}
|
| 14 |
+
manifest = manifest || {};
|
| 15 |
+
|
| 16 |
+
const keys = [...tensors.keys()];
|
| 17 |
+
const embKey = keys.find(k => k.endsWith("emb.weight"));
|
| 18 |
+
if (!embKey) throw new Error("emb.weight not found in model.safetensors");
|
| 19 |
+
const pre = embKey.slice(0, embKey.length - "emb.weight".length);
|
| 20 |
+
const T = n => { const t = tensors.get(pre + n); if (!t) throw new Error("Missing tensor " + pre + n); return t; };
|
| 21 |
+
const W = n => tools.toFloat32(T(n));
|
| 22 |
+
|
| 23 |
+
const emb = W("emb.weight"), [V, d] = T("emb.weight").shape;
|
| 24 |
+
const convT = T("conv.w"), cw = tools.toFloat32(convT), K = convT.shape[2];
|
| 25 |
+
let nLayer = 0; while (tensors.has(pre + "blocks." + nLayer + ".n1.w")) nLayer++;
|
| 26 |
+
if (!nLayer) throw new Error("No transformer blocks found.");
|
| 27 |
+
const hd = T("blocks.0.attn.qn.w").shape[0], nHead = d / hd, ff = T("blocks.0.ff.w1.weight").shape[0];
|
| 28 |
+
if (!Number.isInteger(nHead)) throw new Error("Head size doesn’t divide d_model.");
|
| 29 |
+
|
| 30 |
+
const ropeBase = cfg.rope_base ?? manifest.rope_base;
|
| 31 |
+
if (ropeBase == null) throw new Error('config.json has no "rope_base". Add it there (or in verolm.json).');
|
| 32 |
+
const logitCap = cfg.logit_cap ?? manifest.logit_cap ?? 0;
|
| 33 |
+
const block = cfg.block_size ?? manifest.block_size ?? 1024;
|
| 34 |
+
const eod = cfg.eod_token_id ?? manifest.eod_token_id ?? 0;
|
| 35 |
+
|
| 36 |
+
const layers = [];
|
| 37 |
+
for (let l = 0; l < nLayer; l++) {
|
| 38 |
+
const p = "blocks." + l + ".";
|
| 39 |
+
layers.push({
|
| 40 |
+
n1: W(p + "n1.w"), qkv: W(p + "attn.qkv.weight"), o: W(p + "attn.o.weight"),
|
| 41 |
+
qn: W(p + "attn.qn.w"), kn: W(p + "attn.kn.w"), gW: W(p + "attn.gate.weight"), gB: W(p + "attn.gate.bias"),
|
| 42 |
+
lam: l > 0 ? W(p + "attn.lam")[0] : 0,
|
| 43 |
+
n2: W(p + "n2.w"), w1: W(p + "ff.w1.weight"), w3: W(p + "ff.w3.weight"), w2: W(p + "ff.w2.weight")
|
| 44 |
+
});
|
| 45 |
+
if (onProgress) onProgress(((l + 1) / nLayer) * 100, "preparing");
|
| 46 |
+
}
|
| 47 |
+
const nf = W("nf.w");
|
| 48 |
+
|
| 49 |
+
// rotary tables (float32, interleaved pairs)
|
| 50 |
+
const half = hd / 2, cosT = new Float32Array(block * half), sinT = new Float32Array(block * half);
|
| 51 |
+
const inv = new Float32Array(half);
|
| 52 |
+
for (let i = 0; i < half; i++) inv[i] = 1 / Math.pow(ropeBase, (2 * i) / hd);
|
| 53 |
+
for (let t = 0; t < block; t++) for (let i = 0; i < half; i++) {
|
| 54 |
+
const a = Math.fround(t * inv[i]); cosT[t * half + i] = Math.cos(a); sinT[t * half + i] = Math.sin(a);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
// scratch + cache
|
| 58 |
+
const x = new Float32Array(d), h = new Float32Array(d), qkv = new Float32Array(3 * d), y = new Float32Array(d);
|
| 59 |
+
const ao = new Float32Array(d), g = new Float32Array(nHead), a1 = new Float32Array(ff), a3 = new Float32Array(ff);
|
| 60 |
+
const logits = new Float32Array(V), sc = new Float32Array(block);
|
| 61 |
+
let E, V1, Kc, Vc;
|
| 62 |
+
const reset = () => {
|
| 63 |
+
E = new Float32Array(block * d); V1 = new Float32Array(block * d);
|
| 64 |
+
Kc = layers.map(() => new Float32Array(block * d)); Vc = layers.map(() => new Float32Array(block * d));
|
| 65 |
+
};
|
| 66 |
+
|
| 67 |
+
function matvec(M, outN, inN, v, out) {
|
| 68 |
+
for (let o = 0, b = 0; o < outN; o++, b += inN) { let s = 0; for (let i = 0; i < inN; i++) s += M[b + i] * v[i]; out[o] = s; }
|
| 69 |
+
}
|
| 70 |
+
function rms(src, so, w, n, dst, doff) {
|
| 71 |
+
let ss = 0; for (let i = 0; i < n; i++) ss += src[so + i] * src[so + i];
|
| 72 |
+
const r = 1 / Math.sqrt(ss / n + EPS);
|
| 73 |
+
for (let i = 0; i < n; i++) dst[doff + i] = src[so + i] * r * w[i];
|
| 74 |
+
}
|
| 75 |
+
const tmp = new Float32Array(hd);
|
| 76 |
+
function rmsHead(arr, off, w) { rms(arr, off, w, hd, tmp, 0); for (let i = 0; i < hd; i++) arr[off + i] = tmp[i]; }
|
| 77 |
+
function rope(arr, off, p) {
|
| 78 |
+
for (let i = 0; i < half; i++) {
|
| 79 |
+
const c = cosT[p * half + i], s = sinT[p * half + i], x1 = arr[off + 2 * i], x2 = arr[off + 2 * i + 1];
|
| 80 |
+
arr[off + 2 * i] = x1 * c - x2 * s; arr[off + 2 * i + 1] = x1 * s + x2 * c;
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
// one position through the whole network; returns logits when wanted
|
| 85 |
+
function step(tok, p, wantLogits) {
|
| 86 |
+
E.set(emb.subarray(tok * d, tok * d + d), p * d);
|
| 87 |
+
for (let c = 0; c < d; c++) {
|
| 88 |
+
let s = E[p * d + c];
|
| 89 |
+
for (let j = 0; j < K; j++) { const q = p - (K - 1) + j; if (q >= 0) s += cw[c * K + j] * E[q * d + c]; }
|
| 90 |
+
x[c] = s;
|
| 91 |
+
}
|
| 92 |
+
const scale = 1 / Math.sqrt(hd);
|
| 93 |
+
for (let l = 0; l < nLayer; l++) {
|
| 94 |
+
const L = layers[l];
|
| 95 |
+
rms(x, 0, L.n1, d, h, 0);
|
| 96 |
+
matvec(L.qkv, 3 * d, d, h, qkv);
|
| 97 |
+
const q = qkv.subarray(0, d), k = qkv.subarray(d, 2 * d), v = qkv.subarray(2 * d, 3 * d);
|
| 98 |
+
if (l === 0) V1.set(v, p * d); else for (let i = 0; i < d; i++) v[i] += L.lam * V1[p * d + i];
|
| 99 |
+
for (let hh = 0; hh < nHead; hh++) {
|
| 100 |
+
const off = hh * hd;
|
| 101 |
+
rmsHead(q, off, L.qn); rmsHead(k, off, L.kn); rope(q, off, p); rope(k, off, p);
|
| 102 |
+
}
|
| 103 |
+
Kc[l].set(k, p * d); Vc[l].set(v, p * d);
|
| 104 |
+
const Kl = Kc[l], Vl = Vc[l];
|
| 105 |
+
for (let hh = 0; hh < nHead; hh++) {
|
| 106 |
+
const off = hh * hd; let mx = -Infinity;
|
| 107 |
+
for (let j = 0; j <= p; j++) {
|
| 108 |
+
let s = 0; for (let i = 0; i < hd; i++) s += q[off + i] * Kl[j * d + off + i];
|
| 109 |
+
s *= scale; sc[j] = s; if (s > mx) mx = s;
|
| 110 |
+
}
|
| 111 |
+
let den = 0; for (let j = 0; j <= p; j++) { sc[j] = Math.exp(sc[j] - mx); den += sc[j]; }
|
| 112 |
+
for (let i = 0; i < hd; i++) y[off + i] = 0;
|
| 113 |
+
for (let j = 0; j <= p; j++) { const w = sc[j] / den; for (let i = 0; i < hd; i++) y[off + i] += w * Vl[j * d + off + i]; }
|
| 114 |
+
}
|
| 115 |
+
matvec(L.gW, nHead, d, h, g);
|
| 116 |
+
for (let hh = 0; hh < nHead; hh++) {
|
| 117 |
+
const gv = 1 / (1 + Math.exp(-(g[hh] + L.gB[hh])));
|
| 118 |
+
for (let i = 0; i < hd; i++) y[hh * hd + i] *= gv;
|
| 119 |
+
}
|
| 120 |
+
matvec(L.o, d, d, y, ao);
|
| 121 |
+
for (let i = 0; i < d; i++) x[i] += ao[i];
|
| 122 |
+
rms(x, 0, L.n2, d, h, 0);
|
| 123 |
+
matvec(L.w1, ff, d, h, a1); matvec(L.w3, ff, d, h, a3);
|
| 124 |
+
for (let i = 0; i < ff; i++) { const u = a1[i]; a1[i] = (u / (1 + Math.exp(-u))) * a3[i]; }
|
| 125 |
+
matvec(L.w2, d, ff, a1, ao);
|
| 126 |
+
for (let i = 0; i < d; i++) x[i] += ao[i];
|
| 127 |
+
}
|
| 128 |
+
if (!wantLogits) return null;
|
| 129 |
+
rms(x, 0, nf, d, h, 0);
|
| 130 |
+
for (let o = 0; o < V; o++) {
|
| 131 |
+
let s = 0; const b = o * d; for (let i = 0; i < d; i++) s += emb[b + i] * h[i];
|
| 132 |
+
logits[o] = logitCap > 0 ? logitCap * Math.tanh(s / logitCap) : s;
|
| 133 |
+
}
|
| 134 |
+
return logits;
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
function sample(lg, temperature, topK) {
|
| 138 |
+
const n = lg.length;
|
| 139 |
+
if (!(temperature > 0)) { let b = 0; for (let i = 1; i < n; i++) if (lg[i] > lg[b]) b = i; return b; }
|
| 140 |
+
const z = Array.from(lg, v => v / temperature);
|
| 141 |
+
if (topK > 0 && topK < n) {
|
| 142 |
+
const kth = [...z].sort((a, b) => b - a)[topK - 1];
|
| 143 |
+
for (let i = 0; i < n; i++) if (z[i] < kth) z[i] = -Infinity;
|
| 144 |
+
}
|
| 145 |
+
let mx = -Infinity; for (const v of z) if (v > mx) mx = v;
|
| 146 |
+
let den = 0; const pr = z.map(v => { const e = Math.exp(v - mx); den += e; return e; });
|
| 147 |
+
let r = Math.random() * den;
|
| 148 |
+
for (let i = 0; i < n; i++) { r -= pr[i]; if (r <= 0) return i; }
|
| 149 |
+
return n - 1;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
return {
|
| 153 |
+
// logits after feeding `ids` (array of byte values); exposed for testing
|
| 154 |
+
_forward(ids) { reset(); let lg; for (let p = 0; p < ids.length; p++) lg = step(ids[p], p, p === ids.length - 1); return Array.from(lg); },
|
| 155 |
+
|
| 156 |
+
async generate(prompt, { onToken, maxTokens }) {
|
| 157 |
+
const temperature = manifest.temperature ?? 0.5, topK = manifest.top_k ?? 40;
|
| 158 |
+
let ids = Array.from(new TextEncoder().encode(prompt));
|
| 159 |
+
if (!ids.length) ids = [10];
|
| 160 |
+
if (ids.length >= block) ids = ids.slice(-(block - 1));
|
| 161 |
+
reset();
|
| 162 |
+
let lg; for (let p = 0; p < ids.length; p++) lg = step(ids[p], p, p === ids.length - 1);
|
| 163 |
+
const dec = new TextDecoder("utf-8"); let pos = ids.length, text = "";
|
| 164 |
+
for (let n = 0; n < (maxTokens || 256); n++) {
|
| 165 |
+
const tok = sample(lg, temperature, topK);
|
| 166 |
+
if (tok === eod) break;
|
| 167 |
+
const piece = dec.decode(Uint8Array.of(tok), { stream: true });
|
| 168 |
+
if (piece) { onToken(piece); text += piece; }
|
| 169 |
+
if (pos >= block) break; // context is full (block_size bytes)
|
| 170 |
+
lg = step(tok, pos, true); pos++;
|
| 171 |
+
if (n % 4 === 0) await new Promise(r => setTimeout(r, 0)); // keep the page responsive
|
| 172 |
+
}
|
| 173 |
+
const tail = dec.decode(); if (tail) { onToken(tail); text += tail; }
|
| 174 |
+
return text;
|
| 175 |
+
}
|
| 176 |
+
};
|
| 177 |
+
}
|
verolm.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"runtime": "custom",
|
| 3 |
+
"runner": "verolm-runner.js",
|
| 4 |
+
"weights": "model.safetensors",
|
| 5 |
+
"format": "safetensors",
|
| 6 |
+
"arch": "byte-level transformer",
|
| 7 |
+
"maxTokens": 256,
|
| 8 |
+
"temperature": 0.5,
|
| 9 |
+
"top_k": 40
|
| 10 |
+
}
|