nathaninline's picture
Model card, licenses, conversion scripts
10292c2 verified
Raw History Blame Contribute Delete
3.56 kB
"""Edge0 latent expert -> expert Flash Next classique (gate/up 640x2560, down 2560x640)."""
import ctypes, os, sys
import numpy as np
sys.path.insert(0, os.path.expanduser("~/basalt-orig/tools"))
import ggufq
from ggufq import GGMLQuantizationType as QT
P = "/models/edge0/Edge0-145B-q3k"
H, ELL, FF, G, E, NL = 2560, 1792, 640, 8, 512, 48
GATE_B = FF * (ELL // 256) * 110 # Q3_K : 110 octets / 256 valeurs
DN_B = ELL * (FF // 32) * 18 # IQ4_NL : 18 octets / 32 valeurs
BLOB = 2 * GATE_B + DN_B
assert BLOB == 1630720
DB = G * ELL * (H // 32) * 34
UB = G * H * (ELL // 32) * 34
_du = _ex = _lab = None
def _open():
global _du, _ex, _lab
if _du is None:
_du = np.memmap(P + "/latent_dense_q8.bin", dtype=np.uint8, mode="r")
_ex = np.memmap(P + "/experts.bin", dtype=np.uint8, mode="r")
_lab = np.fromfile(P + "/labels.bin", dtype=np.int32).reshape(NL, E)
assert _du.size == NL * (DB + UB)
def q8rows(buf, nrows, ncols):
a = np.ascontiguousarray(buf).reshape(nrows, ncols // 32, 34)
d = a[:, :, :2].copy().view(np.float16).astype(np.float32)
q = a[:, :, 2:].copy().view(np.int8).astype(np.float32)
return (d * q).reshape(nrows, ncols)
def layer_du(l):
_open()
o = l * (DB + UB)
D = q8rows(_du[o:o + DB], G * ELL, H).reshape(G, ELL, H)
U = q8rows(_du[o + DB:o + DB + UB], G * H, ELL).reshape(G, H, ELL)
s = np.fromfile(P + "/scales/l%d.f32" % l, dtype=np.float32)
assert s.size == ELL
inv = np.where(s > 0, 1.0 / s, 1.0).astype(np.float32)
return D, U, inv
def latent(l, e):
"""(gate, up) [FF, ELL] deja divises par s, down [ELL, FF]."""
_open()
o = (l * E + e) * BLOB
b = np.ascontiguousarray(_ex[o:o + BLOB])
gu = ggufq.dequantize(b[:2 * GATE_B].reshape(-1, 110), QT.Q3_K).reshape(2, FF, ELL)
dn = ggufq.dequantize(b[2 * GATE_B:].reshape(-1, 18), QT.IQ4_NL).reshape(ELL, FF)
return gu[0], gu[1], dn
def fold(l, e, D, U, inv):
_open()
g = int(_lab[l, e])
ga, up, dn = latent(l, e)
Dg = D[g] * inv[:, None] # x_lat = (D x) / s
return ga @ Dg, up @ Dg, U[g] @ dn # [FF,H] [FF,H] [H,FF]
_lib = None
def ggml():
global _lib
if _lib is None:
_lib = ctypes.CDLL(os.path.expanduser("~/llama.cpp-qwen4exp/build/bin/libggml-base.so"))
_lib.ggml_quantize_chunk.restype = ctypes.c_size_t
_lib.ggml_quantize_chunk.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int64,
ctypes.c_int64, ctypes.c_int64, ctypes.c_void_p]
_lib.ggml_row_size.restype = ctypes.c_size_t
_lib.ggml_row_size.argtypes = [ctypes.c_int, ctypes.c_int64]
for t in (16, 17, 18, 21, 22, 20, 23):
_lib.ggml_quantize_init(t)
return _lib
def quant(x, t, imatrix=None):
"""x [rows, cols] float32 -> octets du type ggml t."""
lib = ggml()
x = np.ascontiguousarray(x, dtype=np.float32)
rows, cols = x.shape
n = lib.ggml_row_size(t, cols) * rows
out = np.empty(n, dtype=np.uint8)
imatrix = np.ascontiguousarray(np.ones(cols) if imatrix is None else imatrix, dtype=np.float32); im = imatrix.ctypes.data
w = lib.ggml_quantize_chunk(t, x.ctypes.data, out.ctypes.data, 0, rows, cols, im)
assert w == n, (w, n)
return out
BPB = {16: (256, 66), 17: (256, 74), 18: (256, 98), 21: (256, 110), 22: (256, 82), 20: (32, 18), 23: (256, 136), 11: (256, 110)}
def dequant(b, t, shape):
return ggufq.dequantize(b.reshape(-1, BPB[t][1]), QT(t)).reshape(shape)