Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

2.0 KB
Raw Blame
"""
IQ2_XXS lookup tables (ggml `iq2xxs_grid`, `ksigns_iq2xs`; MIT, ggml-org/llama.cpp, as packed in gguf-py).

GRID_HEX: 256 grid entries x 8 values, each value one of {0x08, 0x19, 0x2b} stored as a 2-bit code
(4 codes per byte, low bits first), written as hex text. KSIGNS[i] = i | parity(i) << 7 (7-bit sign pattern
plus an even-parity bit), generated rather than listed.
"""
import torch

GRID_MAP = (0x08, 0x19, 0x2B)
GRID_HEX = (
    "00000200050008000a00110014002000220028002a0041004400500058006100"
    "6400800082008a00a20001010401100115014001840198010002020222028202"
    "010404041004210424044004420448046004810484049004a404000502050805"
    "200546056905800591050906100640068406a406000805080808140828084108"
    "440850085208880804094009020a140a01100410101021104010601084109010"
    "951000110811201150115a118011241245120014081420142514491480141815"
    "6215001616160118041810184018811800190519a019511a002002200a204420"
    "6120802082202921482100220222012404241024402456240025412564259026"
    "082820289428442a014004401040184021402440404048405640604081408440"
    "9040004120416141804185410142104248425642684200440844204480449944"
    "124524450046014804481048404845480049584961498249454a904a00500850"
    "1150195020508050885004514251a4519152905492540a550156545600581158"
    "195864584059085a046010604060686000615561186260620064056410651265"
    "84654268008002800a8041808280048118814081118201840484108415844084"
    "608400854685948509864086608602880489118a0490109024904090a1901691"
    "8091459200942294449451958198209902a050a085a009a100a218a450a804a9"
)


def grid_table() -> torch.Tensor:
    """[256, 8] float32."""
    b = torch.tensor(list(bytes.fromhex(GRID_HEX)), dtype=torch.uint8)
    codes = torch.stack([(b >> s) & 3 for s in (0, 2, 4, 6)], dim=-1).reshape(256, 8)
    return torch.tensor(GRID_MAP, dtype=torch.float32)[codes.long()]


def ksigns_table() -> torch.Tensor:
    i = torch.arange(128)
    par = torch.zeros(128, dtype=torch.long)
    for b in range(7):
        par ^= (i >> b) & 1
    return (i | (par << 7)).to(torch.uint8)