FardoX commited on
Commit
f734951
·
verified ·
1 Parent(s): a3ca529

sasori TQ3P (K=3) do FLUX.2-klein-9B + text encoder ternario + kernel ggml + medicoes

Browse files
.gitattributes CHANGED
@@ -33,3 +33,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ flux-2-klein-9b-TQ3P.gguf filter=lfs diff=lfs merge=lfs -text
37
+ samples/fp_p0_s42.png filter=lfs diff=lfs merge=lfs -text
38
+ samples/fp_p6_s42.png filter=lfs diff=lfs merge=lfs -text
39
+ samples/k3_p0_s42.png filter=lfs diff=lfs merge=lfs -text
40
+ samples/k3_p6_s42.png filter=lfs diff=lfs merge=lfs -text
41
+ samples/k3te_p0_s42.png filter=lfs diff=lfs merge=lfs -text
42
+ samples/k3te_p6_s42.png filter=lfs diff=lfs merge=lfs -text
43
+ samples/q4km_p0_s42.png filter=lfs diff=lfs merge=lfs -text
44
+ samples/q4km_p6_s42.png filter=lfs diff=lfs merge=lfs -text
45
+ text_encoder/Qwen3-8B-TQ3P.gguf filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ license_name: flux-2-klein-non-commercial-license
4
+ license_link: https://huggingface.co/black-forest-labs/FLUX.2-klein-9B/blob/main/LICENSE.md
5
+ base_model:
6
+ - black-forest-labs/FLUX.2-klein-9B
7
+ - Qwen/Qwen3-8B
8
+ pipeline_tag: text-to-image
9
+ tags:
10
+ - text-to-image
11
+ - flux
12
+ - flux.2
13
+ - gguf
14
+ - quantization
15
+ - ternary
16
+ - sasori
17
+ - stable-diffusion-cpp
18
+ library_name: sasori
19
+ ---
20
+
21
+ # sasori TQ3P — FLUX.2 [klein] 9B ternarizado (K=3 trit-planes)
22
+
23
+ **O que é:** o denoiser (DiT) do FLUX.2 [klein] 9B e o seu text encoder (Qwen3-8B) convertidos para
24
+ **TQ3P**, o formato ternário multi-plano do [sasori](https://github.com/Adriel007/PhD-propose): cada peso
25
+ é a soma de **3 planos de trits** `{-1,0,+1}` com escala f16 por grupo de 256 — **6,1875 bits por peso**.
26
+ Sem retreino, sem dados de calibração (data-free, layer-local).
27
+
28
+ > ### ⚠️ Precisa de um runtime patchado — não roda no stable-diffusion.cpp oficial
29
+ > `TQ3P` é um tipo de quantização **customizado do ggml** (type id 43) que o upstream não conhece: um
30
+ > binário sem patch **recusa a carregar** estes arquivos. Os patches e o script de build estão em
31
+ > [`kernel/`](./kernel) — são 25 hunks, aplicam limpo, e o build leva ~5 min. Veja
32
+ > [Como rodar](#como-rodar).
33
+
34
+ ## Arquivos
35
+
36
+ | arquivo | o que é | tamanho |
37
+ |---|---|---|
38
+ | `flux-2-klein-9b-TQ3P.gguf` | denoiser DiT, K=3 (112 tensores ternários / 89 em FP16) | **6,94 GiB** |
39
+ | `text_encoder/Qwen3-8B-TQ3P.gguf` | text encoder, K=3 | **7,33 GiB** |
40
+ | `kernel/sdcpp-cpu.patch` | tipo TQ{K}P no ggml-CPU (bloco, `vec_dot` AVX2 `maddubs`) | 17 hunks |
41
+ | `kernel/sdcpp-cuda.patch` | gate CUDA (`dequant→cuBLAS`) para o denoiser rodar na GPU | 8 hunks |
42
+ | `kernel/build.sh` · `kernel/BUILD.md` | build reproduzível do runtime | — |
43
+ | `bench/` | os JSONs de todas as medições abaixo | — |
44
+ | `samples/` | amostras pareadas FP16 vs TQ3P (mesmo prompt, mesma seed) | — |
45
+
46
+ **Você também precisa** (não redistribuídos aqui):
47
+ - **VAE**: `full_encoder_small_decoder.safetensors` de
48
+ [`black-forest-labs/FLUX.2-small-decoder`](https://huggingface.co/black-forest-labs/FLUX.2-small-decoder)
49
+ (o que foi usado em todas as medições; sem gate de licença). A VAE oficial `flux2_ae.safetensors`
50
+ vive no repo gated [`FLUX.2-dev`](https://huggingface.co/black-forest-labs/FLUX.2-dev) e também serve.
51
+
52
+ ## Números medidos
53
+
54
+ Tudo medido **nesta conversão**, num pod A100-80GB PCIe, `sd.cpp master-802-e92e86f` + os patches
55
+ deste repo, CUDA sm_80, 1024×1024, 4 steps (o klein-9B é step-distilled), euler, `--diffusion-fa`.
56
+
57
+ ### VRAM e footprint (medidos)
58
+
59
+ | braço | DiT (arquivo) | text encoder (arquivo) | **pacote** | VRAM medida (1024²) |
60
+ |---|---|---|---|---|
61
+ | FP16 / BF16 (16 bpw) | 16,91 GiB | 15,26 GiB | **32,17 GiB** | 33 058 MB |
62
+ | DiT TQ3P + TE BF16 | 6,94 GiB | 15,26 GiB | 22,20 GiB | 22 853 MB |
63
+ | **DiT TQ3P + TE TQ3P** (6,1875 bpw) | **6,94 GiB** | **7,33 GiB** | **14,27 GiB** | **14 729 MB** |
64
+ | Q4_K_M + Q8_0 (baseline) | 5,50 GiB | 8,11 GiB | 13,61 GiB | 13 425 MB |
65
+
66
+ ### Velocidade de denoise — s/it, mediana de 16 gerações por braço
67
+
68
+ | braço | s/it (mediana) | faixa | vs FP16 |
69
+ |---|---|---|---|
70
+ | FP16 | 1,660 | 1,577–1,768 | — |
71
+ | **DiT TQ3P** | **1,200** | 1,177–1,230 | **1,38× mais rápido** |
72
+ | **DiT TQ3P + TE TQ3P** | **1,221** | 1,195–1,278 | **1,36× mais rápido** |
73
+ | Q4_K_M | 1,619 | 1,603–1,657 | 1,03× mais rápido |
74
+
75
+ `sampling completed` do sd-cli mede só o laço de denoise (exclui load do modelo, encode do prompt e
76
+ decode da VAE), então este contraste isola o DiT. O text encoder roda uma vez por imagem, fora do
77
+ laço — daí `TQ3P` e `TQ3P + TE TQ3P` empatarem (1,8 % = ruído).
78
+
79
+ ### Fidelidade — CLIP-score pareado (ViT-L/14, N=16 pares: 8 prompts × 2 seeds)
80
+
81
+ | braço | CLIP-score | retenção vs FP16 | Wilcoxon p | placar par-a-par (FP/braço) |
82
+ |---|---|---|---|---|
83
+ | FP16 | 0,7303 ± 0,0589 | — | — | — |
84
+ | **DiT TQ3P** | 0,7285 ± 0,0702 | **99,8 %** | 0,744 (n.s.) | **8/8 — empate** |
85
+ | **DiT TQ3P + TE TQ3P** | 0,7276 ± 0,0631 | 99,6 % | 0,464 (n.s.) | 8/8 — empate |
86
+ | Q4_K_M + Q8_0 | 0,7227 ± 0,0608 | 99,0 % | 0,231 (n.s.) | 9/7 |
87
+
88
+ Inspeção visual das amostras pareadas (`samples/`): nos 4 braços a contagem do prompt "three yellow
89
+ rubber ducks" sai correta e o fotorrealismo se mantém; o que muda é a trajetória de denoise
90
+ (composição levemente diferente), não a qualidade.
91
+
92
+ ### Leitura honesta destes números
93
+
94
+ - **O TQ3P não é dominado pelo Q4_K_M aqui — é um trade-off real.** O DiT ternário é **26 % maior**
95
+ que o `Q4_K_M` (6,94 vs 5,50 GiB) e ao mesmo tempo **35 % mais rápido** no denoise (1,20 vs
96
+ 1,62 s/it). Como *pacote* (DiT + text encoder) a diferença de tamanho quase desaparece — 14,27 vs
97
+ 13,61 GiB — porque o Qwen3-8B em TQ3P (7,33 GiB) fica **menor** que em Q8_0 (8,11 GiB).
98
+ - **Isto contraria a expectativa que trouxemos do run anterior** (SD3.5-Medium, onde TQ3P 0,343 s/it
99
+ ≈ FP 0,347 ≈ Q4_K_M 0,329 — empate nos três). Aqui o ternário ganha. **Hipótese** (não isolada
100
+ neste run): o gate CUDA roteia TQ{K}P por `dequant → cuBLAS`, e a 1024² o denoise processa ~4096
101
+ tokens de imagem por passo — regime de GEMM grande, onde o cuBLAS bate o kernel `mmq` que serve o
102
+ `Q4_K`; contra o FP16, o ternário lê 2,4× menos bytes pelo mesmo caminho de GEMM. Falsificar isso
103
+ exigiria variar resolução e comparar os caminhos de kernel isoladamente — **não foi feito**.
104
+ - **Nenhuma diferença de fidelidade é detectável a N=16** — em nenhum braço, inclusive o baseline.
105
+ Isso **não** é o mesmo que "TQ3P = FP16": N=16 é subpotente para um Δ de ~0,2 pp. O que se pode
106
+ afirmar é mais fraco e mais honesto: **o placar par-a-par do TQ3P contra o FP16 é 8/8**, sem
107
+ tendência direcional (no run do SD3.5 o FP ganhava 11/16), e a inspeção visual não mostra
108
+ degradação.
109
+ - **CLIP-score não é FID.** Mede alinhamento imagem-texto pareado, não fidelidade perceptual da
110
+ distribuição. Nenhum FID de Fréchet foi computado.
111
+ - **Escopo da medição:** 1 modelo, 1 GPU (A100-80 PCIe), 1024², 4 steps, euler, 2 seeds, 8 prompts,
112
+ 1 CLIP (ViT-L/14), VAE `small-decoder`. Não medido: outras resoluções, CPU, ARM, edição
113
+ (`-r` image-to-image), outros samplers, FID.
114
+
115
+ ## Como rodar
116
+
117
+ ```bash
118
+ # 1) runtime com o kernel ternário (~5 min)
119
+ git clone https://huggingface.co/FardoX/sasori-flux2-klein-9b-tq3p && cd sasori-flux2-klein-9b-tq3p
120
+ CUDA_ARCH=86 bash kernel/build.sh # 80=A100 86=3090 89=4090 90=H100; BUILD_CUDA=OFF p/ CPU
121
+ strings stable-diffusion.cpp/build/bin/sd-cli | grep TQ3P # sanity: o tipo tem que existir
122
+
123
+ # 2) pesos (este repo + a VAE)
124
+ hf download FardoX/sasori-flux2-klein-9b-tq3p --local-dir .
125
+ hf download black-forest-labs/FLUX.2-small-decoder full_encoder_small_decoder.safetensors --local-dir vae
126
+
127
+ # 3) gerar
128
+ stable-diffusion.cpp/build/bin/sd-cli \
129
+ --diffusion-model flux-2-klein-9b-TQ3P.gguf \
130
+ --llm text_encoder/Qwen3-8B-TQ3P.gguf \
131
+ --vae vae/full_encoder_small_decoder.safetensors \
132
+ -p "a lovely cat sitting on a wooden table, soft window light" \
133
+ --cfg-scale 1.0 --steps 4 -W 1024 -H 1024 --sampling-method euler \
134
+ --diffusion-fa -o cat.png -v
135
+ ```
136
+
137
+ ### Paralelismo — o que realmente acelera
138
+
139
+ - **`--diffusion-fa`** (flash-attention no denoiser) e **4 steps**: o klein-9B é step-distilled, então
140
+ 4 steps é a operação normal, não um atalho.
141
+ - **NÃO passe `--offload-to-cpu` se você tem VRAM.** A doc oficial do sd.cpp usa essa flag em todos os
142
+ exemplos, e ela põe **todos os pesos na RAM** (`VRAM 0.00MB` no log) — medido aqui: com `--offload-to-cpu` o log mostra `VRAM 0.00MB` e o sampling de 4 steps a 512² leva **27,8 s**; sem a flag, a 1024² (4× mais pixels) leva **4,8 s**.
143
+ Use a flag só se o modelo não couber na sua GPU.
144
+ - **VRAM necessária** (TQ3P, 1024², sem offload): **14,7 GB** com o text encoder também em TQ3P (7,1 GB o DiT + 7,5 GB o TE + 0,1 GB a VAE), ou 22,9 GB mantendo o text encoder em BF16.
145
+ - **CPU**: o kernel TQ{K}P usa `_mm256_maddubs_epi16` (AVX2) e é um mpGEMM direto — o trit multiplica
146
+ sem dequantizar. Passe `-t <núcleos>`. Em CPU, medições anteriores do projeto colocam o TQ2P
147
+ empatado com o Q4_K_M; o **TQ3P perde de ambos** (mais bytes para ler por peso).
148
+
149
+ ### Ternarizar você mesmo (e o que aprendi sobre paralelizar isso)
150
+
151
+ ```bash
152
+ python3 sasori/scripts/inject_tqkp_diffusion.py IN_F16.gguf OUT_TQ3P.gguf \
153
+ --K 3 --group 256 --device cuda --dry-run # audita a deny-list ANTES de gastar GPU
154
+ ```
155
+
156
+ O ajuste é **layer-local e data-free** (cada matriz é independente), então a tentação é paralelizar em
157
+ processos. **Medido neste run:** rodar a conversão do DiT (GPU) e a do text encoder (CPU, 32 workers) ao
158
+ mesmo tempo **piorou** o caminho crítico de **3,7 s → 25 s por tensor (6,7× mais lento)**. Motivo: o
159
+ solver joint-ridge na GPU fica em ~7 % de utilização; o gargalo real é o **`pack` dos trits em numpy na
160
+ CPU** — os dois jobs disputavam o mesmo recurso, não recursos disjuntos. **Serializar os dois jobs na
161
+ GPU, com toda a CPU para o pack de um só, foi mais rápido:** DiT 8,72 G params em 15,3 min, TE em
162
+ **2,7 min** (6,94 G params).
163
+
164
+ ## O que fica em FP16 (e por quê)
165
+
166
+ Dos 201 tensores do DiT, **89 (354 M de 9,07 G params = 3,9 %)** continuam em FP16 — custa ~0,7 GB e
167
+ protege os pontos onde um erro de trit **não** é local:
168
+
169
+ | mantido FP16 | por quê |
170
+ |---|---|
171
+ | `*modulation*` (adaLN, 285 M) | emite shift/scale/gate por bloco a partir do timestep: um erro aqui reescala **toda** a ativação do bloco. A literatura de PTQ para DiT (Q-DiT, PTQ4DiT, ViDiT-Q) aponta adaLN/modulação como o sítio sensível. |
172
+ | `txt_in` (50 M) | única porta do prompt para dentro do denoiser; ternarizar poupa ~0,1 GB e arrisca todo o condicionamento. |
173
+ | `time_in` (17,8 M) | MLP de condicionamento do timestep. |
174
+ | `final_layer.linear` (0,5 M) | projeção de volta ao espaço latente — mesma razão pela qual o caminho LLM do sasori mantém `output.weight` em FP. |
175
+ | `img_in`, normas, `key/query_norm` | `img_in` tem in_dim 128 (não múltiplo de 256); normas são 1-D. |
176
+
177
+ **Nota de rigor:** essas escolhas são **hipóteses fundamentadas** (mecanismo + literatura de PTQ de
178
+ DiT), **não uma ablação medida neste run**. Ternarizar a modulação e medir o custo é o experimento
179
+ seguinte — não foi feito aqui.
180
+
181
+ ## Licença
182
+
183
+ O denoiser é um **derivado do FLUX.2 [klein] 9B** e herda a **FLUX Non-Commercial License** da Black
184
+ Forest Labs: **uso não-comercial apenas**, e as mesmas restrições se aplicam a qualquer derivado deste.
185
+ Leia a licença antes de usar. O text encoder é derivado do **Qwen3-8B (Apache-2.0)**. Os patches do
186
+ kernel e os scripts são do projeto sasori.
187
+
188
+ ## Citação / proveniência
189
+
190
+ - Pesos-fonte: `unsloth/FLUX.2-klein-9B-GGUF` (`flux-2-klein-9b-F16.gguf`) e `unsloth/Qwen3-8B-GGUF`
191
+ (`Qwen3-8B-BF16.gguf`) — GGUFs full-precision, sem quantização em cascata.
192
+ - Runtime: `leejet/stable-diffusion.cpp` `master-802-e92e86f`, ggml `eced84c8` + os patches deste repo.
193
+ - Conversão: sasori `inject_tqkp_diffusion.py --K 3 --group 256 --device cuda`, niter=25.
194
+ - Método: PTQ ternário multi-plano (joint-ridge alternado), data-free, layer-local.
bench/clipscore.json ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "n_pairs": 16,
3
+ "ref": "fp",
4
+ "clip": "openai/clip-vit-large-patch14",
5
+ "pairs": [
6
+ {
7
+ "prompt_i": 0,
8
+ "seed": 42
9
+ },
10
+ {
11
+ "prompt_i": 0,
12
+ "seed": 1337
13
+ },
14
+ {
15
+ "prompt_i": 1,
16
+ "seed": 42
17
+ },
18
+ {
19
+ "prompt_i": 1,
20
+ "seed": 1337
21
+ },
22
+ {
23
+ "prompt_i": 2,
24
+ "seed": 42
25
+ },
26
+ {
27
+ "prompt_i": 2,
28
+ "seed": 1337
29
+ },
30
+ {
31
+ "prompt_i": 3,
32
+ "seed": 42
33
+ },
34
+ {
35
+ "prompt_i": 3,
36
+ "seed": 1337
37
+ },
38
+ {
39
+ "prompt_i": 4,
40
+ "seed": 42
41
+ },
42
+ {
43
+ "prompt_i": 4,
44
+ "seed": 1337
45
+ },
46
+ {
47
+ "prompt_i": 5,
48
+ "seed": 42
49
+ },
50
+ {
51
+ "prompt_i": 5,
52
+ "seed": 1337
53
+ },
54
+ {
55
+ "prompt_i": 6,
56
+ "seed": 42
57
+ },
58
+ {
59
+ "prompt_i": 6,
60
+ "seed": 1337
61
+ },
62
+ {
63
+ "prompt_i": 7,
64
+ "seed": 42
65
+ },
66
+ {
67
+ "prompt_i": 7,
68
+ "seed": 1337
69
+ }
70
+ ],
71
+ "arms": {
72
+ "fp": {
73
+ "mean": 0.730270504951477,
74
+ "std": 0.05890607833862305,
75
+ "per_pair": [
76
+ 0.7898228168487549,
77
+ 0.7958123087882996,
78
+ 0.69093257188797,
79
+ 0.6075867414474487,
80
+ 0.8519022464752197,
81
+ 0.7870793342590332,
82
+ 0.7254836559295654,
83
+ 0.7475548982620239,
84
+ 0.7404217720031738,
85
+ 0.6994374990463257,
86
+ 0.733026921749115,
87
+ 0.7118973731994629,
88
+ 0.6827402710914612,
89
+ 0.7448596954345703,
90
+ 0.7178919911384583,
91
+ 0.6578782796859741
92
+ ]
93
+ },
94
+ "k3": {
95
+ "mean": 0.7284521460533142,
96
+ "std": 0.0701667070388794,
97
+ "per_pair": [
98
+ 0.8078278303146362,
99
+ 0.7883470058441162,
100
+ 0.6257125735282898,
101
+ 0.5858814716339111,
102
+ 0.8263368606567383,
103
+ 0.8231972455978394,
104
+ 0.7344119548797607,
105
+ 0.740487813949585,
106
+ 0.7451135516166687,
107
+ 0.682620108127594,
108
+ 0.6912571787834167,
109
+ 0.7204871773719788,
110
+ 0.7584888339042664,
111
+ 0.7658802270889282,
112
+ 0.7210639119148254,
113
+ 0.638121485710144
114
+ ],
115
+ "retention_pct": 99.7509994506836,
116
+ "wilcoxon_p": 0.743560791015625,
117
+ "ref_wins": 8,
118
+ "arm_wins": 8
119
+ },
120
+ "k3te": {
121
+ "mean": 0.7276306748390198,
122
+ "std": 0.06310403347015381,
123
+ "per_pair": [
124
+ 0.7939058542251587,
125
+ 0.8016656637191772,
126
+ 0.6442081332206726,
127
+ 0.5878970623016357,
128
+ 0.8186874389648438,
129
+ 0.7942061424255371,
130
+ 0.7080155611038208,
131
+ 0.7588667869567871,
132
+ 0.7321103811264038,
133
+ 0.7036865949630737,
134
+ 0.6975305080413818,
135
+ 0.7260567545890808,
136
+ 0.7676522731781006,
137
+ 0.7142478823661804,
138
+ 0.7418999075889587,
139
+ 0.6514531970024109
140
+ ],
141
+ "retention_pct": 99.63851928710938,
142
+ "wilcoxon_p": 0.4637451171875,
143
+ "ref_wins": 8,
144
+ "arm_wins": 8
145
+ },
146
+ "q4km": {
147
+ "mean": 0.722662627696991,
148
+ "std": 0.060831647366285324,
149
+ "per_pair": [
150
+ 0.7809169292449951,
151
+ 0.7693759202957153,
152
+ 0.6436231732368469,
153
+ 0.6148441433906555,
154
+ 0.8311357498168945,
155
+ 0.7981688976287842,
156
+ 0.7179467678070068,
157
+ 0.7483753561973572,
158
+ 0.7414851188659668,
159
+ 0.6667605638504028,
160
+ 0.7295082807540894,
161
+ 0.7041403651237488,
162
+ 0.7126471996307373,
163
+ 0.7543122172355652,
164
+ 0.7212250232696533,
165
+ 0.628136157989502
166
+ ],
167
+ "retention_pct": 98.95821380615234,
168
+ "wilcoxon_p": 0.231201171875,
169
+ "ref_wins": 9,
170
+ "arm_wins": 7
171
+ }
172
+ }
173
+ }
bench/plan_dit_k3.json ADDED
@@ -0,0 +1,1426 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "ternarize": [
3
+ [
4
+ "double_blocks.0.img_attn.proj.weight",
5
+ [
6
+ 4096,
7
+ 4096
8
+ ]
9
+ ],
10
+ [
11
+ "double_blocks.0.img_attn.qkv.weight",
12
+ [
13
+ 4096,
14
+ 12288
15
+ ]
16
+ ],
17
+ [
18
+ "double_blocks.0.img_mlp.0.weight",
19
+ [
20
+ 4096,
21
+ 24576
22
+ ]
23
+ ],
24
+ [
25
+ "double_blocks.0.img_mlp.2.weight",
26
+ [
27
+ 12288,
28
+ 4096
29
+ ]
30
+ ],
31
+ [
32
+ "double_blocks.0.txt_attn.proj.weight",
33
+ [
34
+ 4096,
35
+ 4096
36
+ ]
37
+ ],
38
+ [
39
+ "double_blocks.0.txt_attn.qkv.weight",
40
+ [
41
+ 4096,
42
+ 12288
43
+ ]
44
+ ],
45
+ [
46
+ "double_blocks.0.txt_mlp.0.weight",
47
+ [
48
+ 4096,
49
+ 24576
50
+ ]
51
+ ],
52
+ [
53
+ "double_blocks.0.txt_mlp.2.weight",
54
+ [
55
+ 12288,
56
+ 4096
57
+ ]
58
+ ],
59
+ [
60
+ "double_blocks.1.img_attn.proj.weight",
61
+ [
62
+ 4096,
63
+ 4096
64
+ ]
65
+ ],
66
+ [
67
+ "double_blocks.1.img_attn.qkv.weight",
68
+ [
69
+ 4096,
70
+ 12288
71
+ ]
72
+ ],
73
+ [
74
+ "double_blocks.1.img_mlp.0.weight",
75
+ [
76
+ 4096,
77
+ 24576
78
+ ]
79
+ ],
80
+ [
81
+ "double_blocks.1.img_mlp.2.weight",
82
+ [
83
+ 12288,
84
+ 4096
85
+ ]
86
+ ],
87
+ [
88
+ "double_blocks.1.txt_attn.proj.weight",
89
+ [
90
+ 4096,
91
+ 4096
92
+ ]
93
+ ],
94
+ [
95
+ "double_blocks.1.txt_attn.qkv.weight",
96
+ [
97
+ 4096,
98
+ 12288
99
+ ]
100
+ ],
101
+ [
102
+ "double_blocks.1.txt_mlp.0.weight",
103
+ [
104
+ 4096,
105
+ 24576
106
+ ]
107
+ ],
108
+ [
109
+ "double_blocks.1.txt_mlp.2.weight",
110
+ [
111
+ 12288,
112
+ 4096
113
+ ]
114
+ ],
115
+ [
116
+ "double_blocks.2.img_attn.proj.weight",
117
+ [
118
+ 4096,
119
+ 4096
120
+ ]
121
+ ],
122
+ [
123
+ "double_blocks.2.img_attn.qkv.weight",
124
+ [
125
+ 4096,
126
+ 12288
127
+ ]
128
+ ],
129
+ [
130
+ "double_blocks.2.img_mlp.0.weight",
131
+ [
132
+ 4096,
133
+ 24576
134
+ ]
135
+ ],
136
+ [
137
+ "double_blocks.2.img_mlp.2.weight",
138
+ [
139
+ 12288,
140
+ 4096
141
+ ]
142
+ ],
143
+ [
144
+ "double_blocks.2.txt_attn.proj.weight",
145
+ [
146
+ 4096,
147
+ 4096
148
+ ]
149
+ ],
150
+ [
151
+ "double_blocks.2.txt_attn.qkv.weight",
152
+ [
153
+ 4096,
154
+ 12288
155
+ ]
156
+ ],
157
+ [
158
+ "double_blocks.2.txt_mlp.0.weight",
159
+ [
160
+ 4096,
161
+ 24576
162
+ ]
163
+ ],
164
+ [
165
+ "double_blocks.2.txt_mlp.2.weight",
166
+ [
167
+ 12288,
168
+ 4096
169
+ ]
170
+ ],
171
+ [
172
+ "double_blocks.3.img_attn.proj.weight",
173
+ [
174
+ 4096,
175
+ 4096
176
+ ]
177
+ ],
178
+ [
179
+ "double_blocks.3.img_attn.qkv.weight",
180
+ [
181
+ 4096,
182
+ 12288
183
+ ]
184
+ ],
185
+ [
186
+ "double_blocks.3.img_mlp.0.weight",
187
+ [
188
+ 4096,
189
+ 24576
190
+ ]
191
+ ],
192
+ [
193
+ "double_blocks.3.img_mlp.2.weight",
194
+ [
195
+ 12288,
196
+ 4096
197
+ ]
198
+ ],
199
+ [
200
+ "double_blocks.3.txt_attn.proj.weight",
201
+ [
202
+ 4096,
203
+ 4096
204
+ ]
205
+ ],
206
+ [
207
+ "double_blocks.3.txt_attn.qkv.weight",
208
+ [
209
+ 4096,
210
+ 12288
211
+ ]
212
+ ],
213
+ [
214
+ "double_blocks.3.txt_mlp.0.weight",
215
+ [
216
+ 4096,
217
+ 24576
218
+ ]
219
+ ],
220
+ [
221
+ "double_blocks.3.txt_mlp.2.weight",
222
+ [
223
+ 12288,
224
+ 4096
225
+ ]
226
+ ],
227
+ [
228
+ "double_blocks.4.img_attn.proj.weight",
229
+ [
230
+ 4096,
231
+ 4096
232
+ ]
233
+ ],
234
+ [
235
+ "double_blocks.4.img_attn.qkv.weight",
236
+ [
237
+ 4096,
238
+ 12288
239
+ ]
240
+ ],
241
+ [
242
+ "double_blocks.4.img_mlp.0.weight",
243
+ [
244
+ 4096,
245
+ 24576
246
+ ]
247
+ ],
248
+ [
249
+ "double_blocks.4.img_mlp.2.weight",
250
+ [
251
+ 12288,
252
+ 4096
253
+ ]
254
+ ],
255
+ [
256
+ "double_blocks.4.txt_attn.proj.weight",
257
+ [
258
+ 4096,
259
+ 4096
260
+ ]
261
+ ],
262
+ [
263
+ "double_blocks.4.txt_attn.qkv.weight",
264
+ [
265
+ 4096,
266
+ 12288
267
+ ]
268
+ ],
269
+ [
270
+ "double_blocks.4.txt_mlp.0.weight",
271
+ [
272
+ 4096,
273
+ 24576
274
+ ]
275
+ ],
276
+ [
277
+ "double_blocks.4.txt_mlp.2.weight",
278
+ [
279
+ 12288,
280
+ 4096
281
+ ]
282
+ ],
283
+ [
284
+ "double_blocks.5.img_attn.proj.weight",
285
+ [
286
+ 4096,
287
+ 4096
288
+ ]
289
+ ],
290
+ [
291
+ "double_blocks.5.img_attn.qkv.weight",
292
+ [
293
+ 4096,
294
+ 12288
295
+ ]
296
+ ],
297
+ [
298
+ "double_blocks.5.img_mlp.0.weight",
299
+ [
300
+ 4096,
301
+ 24576
302
+ ]
303
+ ],
304
+ [
305
+ "double_blocks.5.img_mlp.2.weight",
306
+ [
307
+ 12288,
308
+ 4096
309
+ ]
310
+ ],
311
+ [
312
+ "double_blocks.5.txt_attn.proj.weight",
313
+ [
314
+ 4096,
315
+ 4096
316
+ ]
317
+ ],
318
+ [
319
+ "double_blocks.5.txt_attn.qkv.weight",
320
+ [
321
+ 4096,
322
+ 12288
323
+ ]
324
+ ],
325
+ [
326
+ "double_blocks.5.txt_mlp.0.weight",
327
+ [
328
+ 4096,
329
+ 24576
330
+ ]
331
+ ],
332
+ [
333
+ "double_blocks.5.txt_mlp.2.weight",
334
+ [
335
+ 12288,
336
+ 4096
337
+ ]
338
+ ],
339
+ [
340
+ "double_blocks.6.img_attn.proj.weight",
341
+ [
342
+ 4096,
343
+ 4096
344
+ ]
345
+ ],
346
+ [
347
+ "double_blocks.6.img_attn.qkv.weight",
348
+ [
349
+ 4096,
350
+ 12288
351
+ ]
352
+ ],
353
+ [
354
+ "double_blocks.6.img_mlp.0.weight",
355
+ [
356
+ 4096,
357
+ 24576
358
+ ]
359
+ ],
360
+ [
361
+ "double_blocks.6.img_mlp.2.weight",
362
+ [
363
+ 12288,
364
+ 4096
365
+ ]
366
+ ],
367
+ [
368
+ "double_blocks.6.txt_attn.proj.weight",
369
+ [
370
+ 4096,
371
+ 4096
372
+ ]
373
+ ],
374
+ [
375
+ "double_blocks.6.txt_attn.qkv.weight",
376
+ [
377
+ 4096,
378
+ 12288
379
+ ]
380
+ ],
381
+ [
382
+ "double_blocks.6.txt_mlp.0.weight",
383
+ [
384
+ 4096,
385
+ 24576
386
+ ]
387
+ ],
388
+ [
389
+ "double_blocks.6.txt_mlp.2.weight",
390
+ [
391
+ 12288,
392
+ 4096
393
+ ]
394
+ ],
395
+ [
396
+ "double_blocks.7.img_attn.proj.weight",
397
+ [
398
+ 4096,
399
+ 4096
400
+ ]
401
+ ],
402
+ [
403
+ "double_blocks.7.img_attn.qkv.weight",
404
+ [
405
+ 4096,
406
+ 12288
407
+ ]
408
+ ],
409
+ [
410
+ "double_blocks.7.img_mlp.0.weight",
411
+ [
412
+ 4096,
413
+ 24576
414
+ ]
415
+ ],
416
+ [
417
+ "double_blocks.7.img_mlp.2.weight",
418
+ [
419
+ 12288,
420
+ 4096
421
+ ]
422
+ ],
423
+ [
424
+ "double_blocks.7.txt_attn.proj.weight",
425
+ [
426
+ 4096,
427
+ 4096
428
+ ]
429
+ ],
430
+ [
431
+ "double_blocks.7.txt_attn.qkv.weight",
432
+ [
433
+ 4096,
434
+ 12288
435
+ ]
436
+ ],
437
+ [
438
+ "double_blocks.7.txt_mlp.0.weight",
439
+ [
440
+ 4096,
441
+ 24576
442
+ ]
443
+ ],
444
+ [
445
+ "double_blocks.7.txt_mlp.2.weight",
446
+ [
447
+ 12288,
448
+ 4096
449
+ ]
450
+ ],
451
+ [
452
+ "single_blocks.0.linear1.weight",
453
+ [
454
+ 4096,
455
+ 36864
456
+ ]
457
+ ],
458
+ [
459
+ "single_blocks.0.linear2.weight",
460
+ [
461
+ 16384,
462
+ 4096
463
+ ]
464
+ ],
465
+ [
466
+ "single_blocks.1.linear1.weight",
467
+ [
468
+ 4096,
469
+ 36864
470
+ ]
471
+ ],
472
+ [
473
+ "single_blocks.1.linear2.weight",
474
+ [
475
+ 16384,
476
+ 4096
477
+ ]
478
+ ],
479
+ [
480
+ "single_blocks.10.linear1.weight",
481
+ [
482
+ 4096,
483
+ 36864
484
+ ]
485
+ ],
486
+ [
487
+ "single_blocks.10.linear2.weight",
488
+ [
489
+ 16384,
490
+ 4096
491
+ ]
492
+ ],
493
+ [
494
+ "single_blocks.11.linear1.weight",
495
+ [
496
+ 4096,
497
+ 36864
498
+ ]
499
+ ],
500
+ [
501
+ "single_blocks.11.linear2.weight",
502
+ [
503
+ 16384,
504
+ 4096
505
+ ]
506
+ ],
507
+ [
508
+ "single_blocks.12.linear1.weight",
509
+ [
510
+ 4096,
511
+ 36864
512
+ ]
513
+ ],
514
+ [
515
+ "single_blocks.12.linear2.weight",
516
+ [
517
+ 16384,
518
+ 4096
519
+ ]
520
+ ],
521
+ [
522
+ "single_blocks.13.linear1.weight",
523
+ [
524
+ 4096,
525
+ 36864
526
+ ]
527
+ ],
528
+ [
529
+ "single_blocks.13.linear2.weight",
530
+ [
531
+ 16384,
532
+ 4096
533
+ ]
534
+ ],
535
+ [
536
+ "single_blocks.14.linear1.weight",
537
+ [
538
+ 4096,
539
+ 36864
540
+ ]
541
+ ],
542
+ [
543
+ "single_blocks.14.linear2.weight",
544
+ [
545
+ 16384,
546
+ 4096
547
+ ]
548
+ ],
549
+ [
550
+ "single_blocks.15.linear1.weight",
551
+ [
552
+ 4096,
553
+ 36864
554
+ ]
555
+ ],
556
+ [
557
+ "single_blocks.15.linear2.weight",
558
+ [
559
+ 16384,
560
+ 4096
561
+ ]
562
+ ],
563
+ [
564
+ "single_blocks.16.linear1.weight",
565
+ [
566
+ 4096,
567
+ 36864
568
+ ]
569
+ ],
570
+ [
571
+ "single_blocks.16.linear2.weight",
572
+ [
573
+ 16384,
574
+ 4096
575
+ ]
576
+ ],
577
+ [
578
+ "single_blocks.17.linear1.weight",
579
+ [
580
+ 4096,
581
+ 36864
582
+ ]
583
+ ],
584
+ [
585
+ "single_blocks.17.linear2.weight",
586
+ [
587
+ 16384,
588
+ 4096
589
+ ]
590
+ ],
591
+ [
592
+ "single_blocks.18.linear1.weight",
593
+ [
594
+ 4096,
595
+ 36864
596
+ ]
597
+ ],
598
+ [
599
+ "single_blocks.18.linear2.weight",
600
+ [
601
+ 16384,
602
+ 4096
603
+ ]
604
+ ],
605
+ [
606
+ "single_blocks.19.linear1.weight",
607
+ [
608
+ 4096,
609
+ 36864
610
+ ]
611
+ ],
612
+ [
613
+ "single_blocks.19.linear2.weight",
614
+ [
615
+ 16384,
616
+ 4096
617
+ ]
618
+ ],
619
+ [
620
+ "single_blocks.2.linear1.weight",
621
+ [
622
+ 4096,
623
+ 36864
624
+ ]
625
+ ],
626
+ [
627
+ "single_blocks.2.linear2.weight",
628
+ [
629
+ 16384,
630
+ 4096
631
+ ]
632
+ ],
633
+ [
634
+ "single_blocks.20.linear1.weight",
635
+ [
636
+ 4096,
637
+ 36864
638
+ ]
639
+ ],
640
+ [
641
+ "single_blocks.20.linear2.weight",
642
+ [
643
+ 16384,
644
+ 4096
645
+ ]
646
+ ],
647
+ [
648
+ "single_blocks.21.linear1.weight",
649
+ [
650
+ 4096,
651
+ 36864
652
+ ]
653
+ ],
654
+ [
655
+ "single_blocks.21.linear2.weight",
656
+ [
657
+ 16384,
658
+ 4096
659
+ ]
660
+ ],
661
+ [
662
+ "single_blocks.22.linear1.weight",
663
+ [
664
+ 4096,
665
+ 36864
666
+ ]
667
+ ],
668
+ [
669
+ "single_blocks.22.linear2.weight",
670
+ [
671
+ 16384,
672
+ 4096
673
+ ]
674
+ ],
675
+ [
676
+ "single_blocks.23.linear1.weight",
677
+ [
678
+ 4096,
679
+ 36864
680
+ ]
681
+ ],
682
+ [
683
+ "single_blocks.23.linear2.weight",
684
+ [
685
+ 16384,
686
+ 4096
687
+ ]
688
+ ],
689
+ [
690
+ "single_blocks.3.linear1.weight",
691
+ [
692
+ 4096,
693
+ 36864
694
+ ]
695
+ ],
696
+ [
697
+ "single_blocks.3.linear2.weight",
698
+ [
699
+ 16384,
700
+ 4096
701
+ ]
702
+ ],
703
+ [
704
+ "single_blocks.4.linear1.weight",
705
+ [
706
+ 4096,
707
+ 36864
708
+ ]
709
+ ],
710
+ [
711
+ "single_blocks.4.linear2.weight",
712
+ [
713
+ 16384,
714
+ 4096
715
+ ]
716
+ ],
717
+ [
718
+ "single_blocks.5.linear1.weight",
719
+ [
720
+ 4096,
721
+ 36864
722
+ ]
723
+ ],
724
+ [
725
+ "single_blocks.5.linear2.weight",
726
+ [
727
+ 16384,
728
+ 4096
729
+ ]
730
+ ],
731
+ [
732
+ "single_blocks.6.linear1.weight",
733
+ [
734
+ 4096,
735
+ 36864
736
+ ]
737
+ ],
738
+ [
739
+ "single_blocks.6.linear2.weight",
740
+ [
741
+ 16384,
742
+ 4096
743
+ ]
744
+ ],
745
+ [
746
+ "single_blocks.7.linear1.weight",
747
+ [
748
+ 4096,
749
+ 36864
750
+ ]
751
+ ],
752
+ [
753
+ "single_blocks.7.linear2.weight",
754
+ [
755
+ 16384,
756
+ 4096
757
+ ]
758
+ ],
759
+ [
760
+ "single_blocks.8.linear1.weight",
761
+ [
762
+ 4096,
763
+ 36864
764
+ ]
765
+ ],
766
+ [
767
+ "single_blocks.8.linear2.weight",
768
+ [
769
+ 16384,
770
+ 4096
771
+ ]
772
+ ],
773
+ [
774
+ "single_blocks.9.linear1.weight",
775
+ [
776
+ 4096,
777
+ 36864
778
+ ]
779
+ ],
780
+ [
781
+ "single_blocks.9.linear2.weight",
782
+ [
783
+ 16384,
784
+ 4096
785
+ ]
786
+ ]
787
+ ],
788
+ "keep_fp16": [
789
+ [
790
+ "double_blocks.0.img_attn.norm.key_norm.scale",
791
+ [
792
+ 128
793
+ ],
794
+ "not-2D"
795
+ ],
796
+ [
797
+ "double_blocks.0.img_attn.norm.query_norm.scale",
798
+ [
799
+ 128
800
+ ],
801
+ "not-2D"
802
+ ],
803
+ [
804
+ "double_blocks.0.txt_attn.norm.key_norm.scale",
805
+ [
806
+ 128
807
+ ],
808
+ "not-2D"
809
+ ],
810
+ [
811
+ "double_blocks.0.txt_attn.norm.query_norm.scale",
812
+ [
813
+ 128
814
+ ],
815
+ "not-2D"
816
+ ],
817
+ [
818
+ "double_blocks.1.img_attn.norm.key_norm.scale",
819
+ [
820
+ 128
821
+ ],
822
+ "not-2D"
823
+ ],
824
+ [
825
+ "double_blocks.1.img_attn.norm.query_norm.scale",
826
+ [
827
+ 128
828
+ ],
829
+ "not-2D"
830
+ ],
831
+ [
832
+ "double_blocks.1.txt_attn.norm.key_norm.scale",
833
+ [
834
+ 128
835
+ ],
836
+ "not-2D"
837
+ ],
838
+ [
839
+ "double_blocks.1.txt_attn.norm.query_norm.scale",
840
+ [
841
+ 128
842
+ ],
843
+ "not-2D"
844
+ ],
845
+ [
846
+ "double_blocks.2.img_attn.norm.key_norm.scale",
847
+ [
848
+ 128
849
+ ],
850
+ "not-2D"
851
+ ],
852
+ [
853
+ "double_blocks.2.img_attn.norm.query_norm.scale",
854
+ [
855
+ 128
856
+ ],
857
+ "not-2D"
858
+ ],
859
+ [
860
+ "double_blocks.2.txt_attn.norm.key_norm.scale",
861
+ [
862
+ 128
863
+ ],
864
+ "not-2D"
865
+ ],
866
+ [
867
+ "double_blocks.2.txt_attn.norm.query_norm.scale",
868
+ [
869
+ 128
870
+ ],
871
+ "not-2D"
872
+ ],
873
+ [
874
+ "double_blocks.3.img_attn.norm.key_norm.scale",
875
+ [
876
+ 128
877
+ ],
878
+ "not-2D"
879
+ ],
880
+ [
881
+ "double_blocks.3.img_attn.norm.query_norm.scale",
882
+ [
883
+ 128
884
+ ],
885
+ "not-2D"
886
+ ],
887
+ [
888
+ "double_blocks.3.txt_attn.norm.key_norm.scale",
889
+ [
890
+ 128
891
+ ],
892
+ "not-2D"
893
+ ],
894
+ [
895
+ "double_blocks.3.txt_attn.norm.query_norm.scale",
896
+ [
897
+ 128
898
+ ],
899
+ "not-2D"
900
+ ],
901
+ [
902
+ "double_blocks.4.img_attn.norm.key_norm.scale",
903
+ [
904
+ 128
905
+ ],
906
+ "not-2D"
907
+ ],
908
+ [
909
+ "double_blocks.4.img_attn.norm.query_norm.scale",
910
+ [
911
+ 128
912
+ ],
913
+ "not-2D"
914
+ ],
915
+ [
916
+ "double_blocks.4.txt_attn.norm.key_norm.scale",
917
+ [
918
+ 128
919
+ ],
920
+ "not-2D"
921
+ ],
922
+ [
923
+ "double_blocks.4.txt_attn.norm.query_norm.scale",
924
+ [
925
+ 128
926
+ ],
927
+ "not-2D"
928
+ ],
929
+ [
930
+ "double_blocks.5.img_attn.norm.key_norm.scale",
931
+ [
932
+ 128
933
+ ],
934
+ "not-2D"
935
+ ],
936
+ [
937
+ "double_blocks.5.img_attn.norm.query_norm.scale",
938
+ [
939
+ 128
940
+ ],
941
+ "not-2D"
942
+ ],
943
+ [
944
+ "double_blocks.5.txt_attn.norm.key_norm.scale",
945
+ [
946
+ 128
947
+ ],
948
+ "not-2D"
949
+ ],
950
+ [
951
+ "double_blocks.5.txt_attn.norm.query_norm.scale",
952
+ [
953
+ 128
954
+ ],
955
+ "not-2D"
956
+ ],
957
+ [
958
+ "double_blocks.6.img_attn.norm.key_norm.scale",
959
+ [
960
+ 128
961
+ ],
962
+ "not-2D"
963
+ ],
964
+ [
965
+ "double_blocks.6.img_attn.norm.query_norm.scale",
966
+ [
967
+ 128
968
+ ],
969
+ "not-2D"
970
+ ],
971
+ [
972
+ "double_blocks.6.txt_attn.norm.key_norm.scale",
973
+ [
974
+ 128
975
+ ],
976
+ "not-2D"
977
+ ],
978
+ [
979
+ "double_blocks.6.txt_attn.norm.query_norm.scale",
980
+ [
981
+ 128
982
+ ],
983
+ "not-2D"
984
+ ],
985
+ [
986
+ "double_blocks.7.img_attn.norm.key_norm.scale",
987
+ [
988
+ 128
989
+ ],
990
+ "not-2D"
991
+ ],
992
+ [
993
+ "double_blocks.7.img_attn.norm.query_norm.scale",
994
+ [
995
+ 128
996
+ ],
997
+ "not-2D"
998
+ ],
999
+ [
1000
+ "double_blocks.7.txt_attn.norm.key_norm.scale",
1001
+ [
1002
+ 128
1003
+ ],
1004
+ "not-2D"
1005
+ ],
1006
+ [
1007
+ "double_blocks.7.txt_attn.norm.query_norm.scale",
1008
+ [
1009
+ 128
1010
+ ],
1011
+ "not-2D"
1012
+ ],
1013
+ [
1014
+ "double_stream_modulation_img.lin.weight",
1015
+ [
1016
+ 4096,
1017
+ 24576
1018
+ ],
1019
+ "deny:modulation"
1020
+ ],
1021
+ [
1022
+ "double_stream_modulation_txt.lin.weight",
1023
+ [
1024
+ 4096,
1025
+ 24576
1026
+ ],
1027
+ "deny:modulation"
1028
+ ],
1029
+ [
1030
+ "final_layer.adaLN_modulation.1.weight",
1031
+ [
1032
+ 4096,
1033
+ 8192
1034
+ ],
1035
+ "deny:modulation"
1036
+ ],
1037
+ [
1038
+ "final_layer.linear.weight",
1039
+ [
1040
+ 4096,
1041
+ 128
1042
+ ],
1043
+ "deny:final_layer.linear"
1044
+ ],
1045
+ [
1046
+ "img_in.weight",
1047
+ [
1048
+ 128,
1049
+ 4096
1050
+ ],
1051
+ "deny:img_in"
1052
+ ],
1053
+ [
1054
+ "single_blocks.0.norm.key_norm.scale",
1055
+ [
1056
+ 128
1057
+ ],
1058
+ "not-2D"
1059
+ ],
1060
+ [
1061
+ "single_blocks.0.norm.query_norm.scale",
1062
+ [
1063
+ 128
1064
+ ],
1065
+ "not-2D"
1066
+ ],
1067
+ [
1068
+ "single_blocks.1.norm.key_norm.scale",
1069
+ [
1070
+ 128
1071
+ ],
1072
+ "not-2D"
1073
+ ],
1074
+ [
1075
+ "single_blocks.1.norm.query_norm.scale",
1076
+ [
1077
+ 128
1078
+ ],
1079
+ "not-2D"
1080
+ ],
1081
+ [
1082
+ "single_blocks.10.norm.key_norm.scale",
1083
+ [
1084
+ 128
1085
+ ],
1086
+ "not-2D"
1087
+ ],
1088
+ [
1089
+ "single_blocks.10.norm.query_norm.scale",
1090
+ [
1091
+ 128
1092
+ ],
1093
+ "not-2D"
1094
+ ],
1095
+ [
1096
+ "single_blocks.11.norm.key_norm.scale",
1097
+ [
1098
+ 128
1099
+ ],
1100
+ "not-2D"
1101
+ ],
1102
+ [
1103
+ "single_blocks.11.norm.query_norm.scale",
1104
+ [
1105
+ 128
1106
+ ],
1107
+ "not-2D"
1108
+ ],
1109
+ [
1110
+ "single_blocks.12.norm.key_norm.scale",
1111
+ [
1112
+ 128
1113
+ ],
1114
+ "not-2D"
1115
+ ],
1116
+ [
1117
+ "single_blocks.12.norm.query_norm.scale",
1118
+ [
1119
+ 128
1120
+ ],
1121
+ "not-2D"
1122
+ ],
1123
+ [
1124
+ "single_blocks.13.norm.key_norm.scale",
1125
+ [
1126
+ 128
1127
+ ],
1128
+ "not-2D"
1129
+ ],
1130
+ [
1131
+ "single_blocks.13.norm.query_norm.scale",
1132
+ [
1133
+ 128
1134
+ ],
1135
+ "not-2D"
1136
+ ],
1137
+ [
1138
+ "single_blocks.14.norm.key_norm.scale",
1139
+ [
1140
+ 128
1141
+ ],
1142
+ "not-2D"
1143
+ ],
1144
+ [
1145
+ "single_blocks.14.norm.query_norm.scale",
1146
+ [
1147
+ 128
1148
+ ],
1149
+ "not-2D"
1150
+ ],
1151
+ [
1152
+ "single_blocks.15.norm.key_norm.scale",
1153
+ [
1154
+ 128
1155
+ ],
1156
+ "not-2D"
1157
+ ],
1158
+ [
1159
+ "single_blocks.15.norm.query_norm.scale",
1160
+ [
1161
+ 128
1162
+ ],
1163
+ "not-2D"
1164
+ ],
1165
+ [
1166
+ "single_blocks.16.norm.key_norm.scale",
1167
+ [
1168
+ 128
1169
+ ],
1170
+ "not-2D"
1171
+ ],
1172
+ [
1173
+ "single_blocks.16.norm.query_norm.scale",
1174
+ [
1175
+ 128
1176
+ ],
1177
+ "not-2D"
1178
+ ],
1179
+ [
1180
+ "single_blocks.17.norm.key_norm.scale",
1181
+ [
1182
+ 128
1183
+ ],
1184
+ "not-2D"
1185
+ ],
1186
+ [
1187
+ "single_blocks.17.norm.query_norm.scale",
1188
+ [
1189
+ 128
1190
+ ],
1191
+ "not-2D"
1192
+ ],
1193
+ [
1194
+ "single_blocks.18.norm.key_norm.scale",
1195
+ [
1196
+ 128
1197
+ ],
1198
+ "not-2D"
1199
+ ],
1200
+ [
1201
+ "single_blocks.18.norm.query_norm.scale",
1202
+ [
1203
+ 128
1204
+ ],
1205
+ "not-2D"
1206
+ ],
1207
+ [
1208
+ "single_blocks.19.norm.key_norm.scale",
1209
+ [
1210
+ 128
1211
+ ],
1212
+ "not-2D"
1213
+ ],
1214
+ [
1215
+ "single_blocks.19.norm.query_norm.scale",
1216
+ [
1217
+ 128
1218
+ ],
1219
+ "not-2D"
1220
+ ],
1221
+ [
1222
+ "single_blocks.2.norm.key_norm.scale",
1223
+ [
1224
+ 128
1225
+ ],
1226
+ "not-2D"
1227
+ ],
1228
+ [
1229
+ "single_blocks.2.norm.query_norm.scale",
1230
+ [
1231
+ 128
1232
+ ],
1233
+ "not-2D"
1234
+ ],
1235
+ [
1236
+ "single_blocks.20.norm.key_norm.scale",
1237
+ [
1238
+ 128
1239
+ ],
1240
+ "not-2D"
1241
+ ],
1242
+ [
1243
+ "single_blocks.20.norm.query_norm.scale",
1244
+ [
1245
+ 128
1246
+ ],
1247
+ "not-2D"
1248
+ ],
1249
+ [
1250
+ "single_blocks.21.norm.key_norm.scale",
1251
+ [
1252
+ 128
1253
+ ],
1254
+ "not-2D"
1255
+ ],
1256
+ [
1257
+ "single_blocks.21.norm.query_norm.scale",
1258
+ [
1259
+ 128
1260
+ ],
1261
+ "not-2D"
1262
+ ],
1263
+ [
1264
+ "single_blocks.22.norm.key_norm.scale",
1265
+ [
1266
+ 128
1267
+ ],
1268
+ "not-2D"
1269
+ ],
1270
+ [
1271
+ "single_blocks.22.norm.query_norm.scale",
1272
+ [
1273
+ 128
1274
+ ],
1275
+ "not-2D"
1276
+ ],
1277
+ [
1278
+ "single_blocks.23.norm.key_norm.scale",
1279
+ [
1280
+ 128
1281
+ ],
1282
+ "not-2D"
1283
+ ],
1284
+ [
1285
+ "single_blocks.23.norm.query_norm.scale",
1286
+ [
1287
+ 128
1288
+ ],
1289
+ "not-2D"
1290
+ ],
1291
+ [
1292
+ "single_blocks.3.norm.key_norm.scale",
1293
+ [
1294
+ 128
1295
+ ],
1296
+ "not-2D"
1297
+ ],
1298
+ [
1299
+ "single_blocks.3.norm.query_norm.scale",
1300
+ [
1301
+ 128
1302
+ ],
1303
+ "not-2D"
1304
+ ],
1305
+ [
1306
+ "single_blocks.4.norm.key_norm.scale",
1307
+ [
1308
+ 128
1309
+ ],
1310
+ "not-2D"
1311
+ ],
1312
+ [
1313
+ "single_blocks.4.norm.query_norm.scale",
1314
+ [
1315
+ 128
1316
+ ],
1317
+ "not-2D"
1318
+ ],
1319
+ [
1320
+ "single_blocks.5.norm.key_norm.scale",
1321
+ [
1322
+ 128
1323
+ ],
1324
+ "not-2D"
1325
+ ],
1326
+ [
1327
+ "single_blocks.5.norm.query_norm.scale",
1328
+ [
1329
+ 128
1330
+ ],
1331
+ "not-2D"
1332
+ ],
1333
+ [
1334
+ "single_blocks.6.norm.key_norm.scale",
1335
+ [
1336
+ 128
1337
+ ],
1338
+ "not-2D"
1339
+ ],
1340
+ [
1341
+ "single_blocks.6.norm.query_norm.scale",
1342
+ [
1343
+ 128
1344
+ ],
1345
+ "not-2D"
1346
+ ],
1347
+ [
1348
+ "single_blocks.7.norm.key_norm.scale",
1349
+ [
1350
+ 128
1351
+ ],
1352
+ "not-2D"
1353
+ ],
1354
+ [
1355
+ "single_blocks.7.norm.query_norm.scale",
1356
+ [
1357
+ 128
1358
+ ],
1359
+ "not-2D"
1360
+ ],
1361
+ [
1362
+ "single_blocks.8.norm.key_norm.scale",
1363
+ [
1364
+ 128
1365
+ ],
1366
+ "not-2D"
1367
+ ],
1368
+ [
1369
+ "single_blocks.8.norm.query_norm.scale",
1370
+ [
1371
+ 128
1372
+ ],
1373
+ "not-2D"
1374
+ ],
1375
+ [
1376
+ "single_blocks.9.norm.key_norm.scale",
1377
+ [
1378
+ 128
1379
+ ],
1380
+ "not-2D"
1381
+ ],
1382
+ [
1383
+ "single_blocks.9.norm.query_norm.scale",
1384
+ [
1385
+ 128
1386
+ ],
1387
+ "not-2D"
1388
+ ],
1389
+ [
1390
+ "single_stream_modulation.lin.weight",
1391
+ [
1392
+ 4096,
1393
+ 12288
1394
+ ],
1395
+ "deny:modulation"
1396
+ ],
1397
+ [
1398
+ "time_in.in_layer.weight",
1399
+ [
1400
+ 256,
1401
+ 4096
1402
+ ],
1403
+ "deny:time_in"
1404
+ ],
1405
+ [
1406
+ "time_in.out_layer.weight",
1407
+ [
1408
+ 4096,
1409
+ 4096
1410
+ ],
1411
+ "deny:time_in"
1412
+ ],
1413
+ [
1414
+ "txt_in.weight",
1415
+ [
1416
+ 12288,
1417
+ 4096
1418
+ ],
1419
+ "deny:txt_in"
1420
+ ]
1421
+ ],
1422
+ "n_ternarize": 112,
1423
+ "n_keep_fp16": 89,
1424
+ "params_ternarize_M": 8724.15232,
1425
+ "params_keep_M": 354.428928
1426
+ }
bench/speed.json ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "fp": {
3
+ "n_gen": 16,
4
+ "s_per_it_median": 1.66,
5
+ "s_per_it_min": 1.5775,
6
+ "s_per_it_max": 1.7675,
7
+ "sampling_total_median_s": 6.64,
8
+ "steps": 4,
9
+ "total_params_MB": 33058.29,
10
+ "vram_MB": 33058.29,
11
+ "ram_MB": 0.0,
12
+ "text_encoder_MB": 15623.18,
13
+ "diffusion_model_MB": 17316.04
14
+ },
15
+ "k3": {
16
+ "n_gen": 16,
17
+ "s_per_it_median": 1.2,
18
+ "s_per_it_min": 1.1775,
19
+ "s_per_it_max": 1.23,
20
+ "sampling_total_median_s": 4.8,
21
+ "steps": 4,
22
+ "total_params_MB": 22853.29,
23
+ "vram_MB": 22853.29,
24
+ "ram_MB": 0.0,
25
+ "text_encoder_MB": 15623.18,
26
+ "diffusion_model_MB": 7111.04
27
+ },
28
+ "k3te": {
29
+ "n_gen": 16,
30
+ "s_per_it_median": 1.2212,
31
+ "s_per_it_min": 1.195,
32
+ "s_per_it_max": 1.2775,
33
+ "sampling_total_median_s": 4.885,
34
+ "steps": 4,
35
+ "total_params_MB": 14728.54,
36
+ "vram_MB": 14728.54,
37
+ "ram_MB": 0.0,
38
+ "text_encoder_MB": 7498.43,
39
+ "diffusion_model_MB": 7111.04
40
+ },
41
+ "q4km": {
42
+ "n_gen": 16,
43
+ "s_per_it_median": 1.6187,
44
+ "s_per_it_min": 1.6025,
45
+ "s_per_it_max": 1.6575,
46
+ "sampling_total_median_s": 6.475,
47
+ "steps": 4,
48
+ "total_params_MB": 13424.88,
49
+ "vram_MB": 13424.88,
50
+ "ram_MB": 0.0,
51
+ "text_encoder_MB": 7669.77,
52
+ "diffusion_model_MB": 5636.04
53
+ }
54
+ }
flux-2-klein-9b-TQ3P.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5fba79be29f1f36beed03ed47a0e5fb183d43869275f864c2beb4e0b61c472f8
3
+ size 7456479520
kernel/BUILD.md ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # O kernel TQ{K}P — o que os patches fazem e como buildar
2
+
3
+ Os GGUFs deste repo usam um tipo de quantização **customizado do ggml**. Sem os patches abaixo, o
4
+ `stable-diffusion.cpp` oficial **não carrega** os arquivos (ele nem sabe que o type id 43 existe).
5
+ Isto não é empacotamento opcional: é o runtime.
6
+
7
+ ## Build rápido
8
+
9
+ ```bash
10
+ CUDA_ARCH=86 bash build.sh # 80=A100 86=3090/A6000 89=4090 90=H100
11
+ BUILD_CUDA=OFF bash build.sh # CPU-only (AVX2)
12
+ SDCPP_REF=<tag|sha> bash build.sh # fixar a revisão do sd.cpp
13
+ ```
14
+
15
+ Saída: `stable-diffusion.cpp/build/bin/sd-cli` (e `sd-server`). Sanity check:
16
+
17
+ ```bash
18
+ strings stable-diffusion.cpp/build/bin/sd-cli | grep TQ3P # tem que imprimir algo
19
+ ```
20
+
21
+ Requisitos: `cmake >= 3.24` (o do apt costuma ser 3.22 → `pip install -U cmake`), CUDA toolkit no PATH
22
+ para o build de GPU (`export PATH=/usr/local/cuda/bin:$PATH`), e um compilador com AVX2 para a CPU.
23
+
24
+ ## O que cada patch faz
25
+
26
+ ### `sdcpp-cpu.patch` (17 hunks, 9 arquivos do ggml)
27
+
28
+ Adiciona a família de tipos TQ{K}P ao ggml-CPU end to end:
29
+
30
+ - **`include/ggml.h`** — os enums. `TQ2P=42`, `TQ3P=43`, `TQ1P=44`, `TQ4P=45`, variantes por
31
+ group-size (`_G32/_G64/_G128`) e por formato de escala (`_Q4K6`), e `GGML_TYPE_COUNT` vai de 42 → 62.
32
+ Os ids **não podem colidir** com tipos novos do upstream; `build.sh` aborta se `COUNT != 42` na
33
+ revisão escolhida.
34
+ - **`src/ggml-common.h`** — o layout do bloco. `block_tq3p` = 3 planos × 256 trits empacotados a 2 bits
35
+ (`qs1/qs2/qs3`, 64 bytes cada) + 3 escalas `ggml_half`, com `static_assert` no tamanho. Um peso é
36
+ `w ≈ α₁t₁ + α₂t₂ + α₃t₃`, `tᵏ ∈ {-1,0,+1}`.
37
+ - **`src/ggml-cpu/arch/x86/quants.c`** — o `vec_dot` AVX2. É um **mpGEMM direto**: `_mm256_maddubs_epi16`
38
+ multiplica trit (2 bits) por ativação int8 e acumula em int16 **sem dequantizar**. Para ternário a
39
+ multiplicação é trivial, então este caminho já é próximo do ótimo (uma implementação LUT-GEMM estilo
40
+ T-MAC foi medida no projeto e ficou **1,3× mais lenta**).
41
+ - **`src/ggml-cpu/{ggml-cpu.c,quants.c,quants.h}`, `src/ggml-quants.{c,h}`, `src/ggml.c`** — registro do
42
+ tipo (traits, block size, `to_float`, dispatch) para o resto do grafo funcionar sem casos especiais.
43
+
44
+ ### `sdcpp-cuda.patch` (8 hunks, 4 arquivos)
45
+
46
+ Faz o denoiser rodar na **GPU** em vez de abortar ou cair para a CPU:
47
+
48
+ - **`src/ggml-cuda/mmvq.cu`** — `should_use_mmvq → false` para os tipos TQ{K}P. Sem isso o
49
+ `mul_mat_vec_q` bate num `GGML_ABORT` (não existe kernel mmvq para o tipo).
50
+ - **`src/ggml-cuda/ggml-cuda.cu`** — `supports_op`, `should_fuse_mul_mat_vec_q → false` e o dispatch do
51
+ decode fundido, roteando TQ{K}P pelo caminho **`dequant → cuBLAS`**.
52
+ - **`src/ggml-cuda/convert.{cu,cuh}`** — o dequant CUDA do bloco de 3 planos.
53
+
54
+ Por que `dequant→cuBLAS` e não um kernel ternário dedicado: no denoiser o GEMM domina e o dequant é
55
+ barato, então o custo fica igual ao FP16. Medido no SD3.5-Medium (run anterior do projeto): TQ3P
56
+ 0,343 s/it vs FP 0,347 vs Q4_K_M 0,329 — e ~200× mais rápido que o caminho CPU (~54 s/it).
57
+
58
+ ## Compatibilidade verificada
59
+
60
+ | item | valor |
61
+ |---|---|
62
+ | sd.cpp | `master-802-e92e86f` |
63
+ | ggml (submódulo) | `eced84c8` (`GGML_TYPE_Q1_0=41`, `COUNT=42`) |
64
+ | aplicação | 25 hunks, **0 rejeitos** (`git apply --check` passou nos dois patches) |
65
+ | build | CUDA sm_80, `Release`, RC=0 |
66
+
67
+ Se `git apply` falhar numa revisão futura, é drift de contexto: os hunks precisam ser re-portados
68
+ (o bloco de enums é o único ponto que exige decisão — os ids têm que continuar livres).
69
+
70
+ ## Portabilidade
71
+
72
+ O mesmo par de patches (com paths ajustados) vem do fork de `llama.cpp` do sasori — o op TQ{K}P é
73
+ compartilhado entre os dois runtimes ggml. O kernel também compila para **wasm32** (gate de 12/12 no
74
+ projeto), mas o runtime completo de difusão no browser não está integrado.
kernel/build.sh ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Build stable-diffusion.cpp with the sasori TQ{K}P ternary kernel.
3
+ #
4
+ # The GGUFs in this repo use a CUSTOM ggml quantization type (TQ3P, ggml type id 43) that upstream
5
+ # stable-diffusion.cpp does NOT know: an unpatched binary will refuse to load them. These two
6
+ # patches add the type end to end (block layout, CPU vec_dot, CUDA dispatch), so this script is
7
+ # not optional packaging -- it is how you get a runtime that can read the weights.
8
+ #
9
+ # Verified 2026-07-29 against sd.cpp master-802-e92e86f / ggml eced84c8 (25 hunks, 0 rejects).
10
+ set -euo pipefail
11
+ HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12
+ SDCPP_REF="${SDCPP_REF:-master}" # pin to a tag/sha for a byte-reproducible build
13
+ CUDA_ARCH="${CUDA_ARCH:-}" # e.g. 80 (A100) 86 (3090) 89 (4090) 90 (H100); empty = native
14
+ BUILD_CUDA="${BUILD_CUDA:-ON}"
15
+
16
+ echo "== clone sd.cpp ($SDCPP_REF) =="
17
+ [ -d stable-diffusion.cpp ] || git clone --recursive https://github.com/leejet/stable-diffusion.cpp
18
+ cd stable-diffusion.cpp
19
+ git fetch --all --tags -q
20
+ git checkout -q "$SDCPP_REF"
21
+ git submodule update --init --recursive -q
22
+
23
+ echo "== apply the TQ{K}P kernel (into the ggml submodule) =="
24
+ cd ggml
25
+ # ggml type ids 42..60 must be FREE for the sasori types; if upstream ever claims them the check
26
+ # below fails loudly instead of silently producing a binary that misreads every tensor.
27
+ grep -q "GGML_TYPE_COUNT = 42," include/ggml.h || {
28
+ echo "!! this ggml revision does not have COUNT=42: the TQ{K}P type ids would collide."
29
+ echo "!! pin SDCPP_REF to a revision where they are free, or re-port the enum block by hand."
30
+ exit 1; }
31
+ for p in cpu cuda; do
32
+ [ "$p" = cuda ] && [ "$BUILD_CUDA" != ON ] && continue
33
+ git apply --check "$HERE/sdcpp-$p.patch" || { echo "!! sdcpp-$p.patch does not apply to $SDCPP_REF"; exit 1; }
34
+ git apply "$HERE/sdcpp-$p.patch"
35
+ echo " applied sdcpp-$p.patch"
36
+ done
37
+ cd ..
38
+
39
+ echo "== build =="
40
+ CMAKE_ARGS=(-DCMAKE_BUILD_TYPE=Release -DSD_CUDA="$BUILD_CUDA")
41
+ [ -n "$CUDA_ARCH" ] && CMAKE_ARGS+=(-DCMAKE_CUDA_ARCHITECTURES="$CUDA_ARCH")
42
+ cmake -B build "${CMAKE_ARGS[@]}"
43
+ cmake --build build -j"$(nproc)"
44
+
45
+ echo
46
+ echo "OK -> $(pwd)/build/bin/sd-cli"
47
+ echo "Sanity check (the type must be known to the binary):"
48
+ echo " strings build/bin/sd-cli | grep TQ3P"
kernel/sdcpp-cpu.patch ADDED
The diff for this file is too large to render. See raw diff
 
kernel/sdcpp-cuda.patch ADDED
@@ -0,0 +1,465 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diff --git a/src/ggml-cuda/convert.cu b/src/ggml-cuda/convert.cu
2
+ index 61630a3..03ca5fe 100644
3
+ --- a/src/ggml-cuda/convert.cu
4
+ +++ b/src/ggml-cuda/convert.cu
5
+ @@ -709,6 +709,262 @@ to_bf16_cuda_t ggml_get_to_bf16_cuda(ggml_type type) {
6
+ }
7
+ }
8
+
9
+ +// ---- sasori ternary trit-plane types (TQ{K}P / TQ{K}P_G{g} / TQ{K}P_Q4K6) ----
10
+ +// K trit-planes summed. The per-plane 2-bit packing mirrors block_tq2_0's strided layout, so the
11
+ +// byte index equals the thread id within a plane; only the scale decode differs per family. This
12
+ +// dequant mirrors dequantize_row_tq{K}p* / q4k6_dequantize in ggml-quants.c. MVP path: dequant ->
13
+ +// cuBLAS fp16 GEMM (no mmvq/mmq). Activation goes through fp16 here, not Q8_K as on CPU, so expect
14
+ +// tiny numeric drift vs the CPU vec_dot (same class of caveat as PR ggml-org/llama.cpp#11183).
15
+ +
16
+ +// f16-scale K-plane dequant: fixed TQ{K}P == G=256, plus TQ{K}P_G{32,64,128}. nsub = 256/G scales/plane.
17
+ +template<typename dst_t, int K, int G>
18
+ +static __global__ void dequantize_block_tqkp_gv(const void * __restrict__ vx, dst_t * __restrict__ yy) {
19
+ + const int64_t i = blockIdx.x; // super-block (256 weights)
20
+ + const int64_t tid = threadIdx.x; // 0..63: byte within a plane
21
+ + const int nsub = 256 / G;
22
+ + const int qb = QK_TQKP_SUPER / 4; // 64 bytes per plane
23
+ + const int bpb = K*qb + K*nsub*2; // bytes per super-block
24
+ + const uint8_t * __restrict__ blk = (const uint8_t *) vx + i*bpb;
25
+ + const half * __restrict__ d = (const half *) (blk + K*qb);
26
+ + const int n = tid / 32;
27
+ + const int l = tid - 32*n;
28
+ + dst_t * y = yy + i*QK_TQKP_SUPER + 128*n;
29
+ +#pragma unroll
30
+ + for (int bp = 0; bp < 4; ++bp) {
31
+ + const int j = 128*n + bp*32 + l; // logical position in the super-block
32
+ + const int sub = j / G;
33
+ + float acc = 0.0f;
34
+ +#pragma unroll
35
+ + for (int kk = 0; kk < K; ++kk) {
36
+ + const int t = ((blk[kk*qb + tid] >> (bp*2)) & 3) - 1; // trit in {-1,0,1}
37
+ + acc += __half2float(d[kk*nsub + sub]) * (float) t;
38
+ + }
39
+ + y[l + bp*32] = (dst_t) acc;
40
+ + }
41
+ +}
42
+ +
43
+ +// q4k6-scale K-plane dequant (g=32 fixed): per-plane fp16 super-scale, sign-flipped by the sub bit7,
44
+ +// times a 6-bit unsigned sub-scale. Mirrors q4k6_dequantize() in ggml-quants.c bit-for-bit.
45
+ +template<typename dst_t, int K>
46
+ +static __global__ void dequantize_block_tqkp_q4k6(const void * __restrict__ vx, dst_t * __restrict__ yy) {
47
+ + const int64_t i = blockIdx.x;
48
+ + const int64_t tid = threadIdx.x; // 0..63
49
+ + const int nsub = QK_TQKP_SUPER / Q4K6_G; // 8
50
+ + const int qb = QK_TQKP_SUPER / 4; // 64
51
+ + const int bpb = K*qb + K*nsub + K*2;
52
+ + const uint8_t * __restrict__ blk = (const uint8_t *) vx + i*bpb;
53
+ + const uint8_t * __restrict__ sb = blk + K*qb; // K*nsub sub-scale bytes
54
+ + const half * __restrict__ sup = (const half *)(blk + K*qb + K*nsub); // K fp16 super-scales
55
+ + const int n = tid / 32;
56
+ + const int l = tid - 32*n;
57
+ + dst_t * y = yy + i*QK_TQKP_SUPER + 128*n;
58
+ +#pragma unroll
59
+ + for (int bp = 0; bp < 4; ++bp) {
60
+ + const int j = 128*n + bp*32 + l;
61
+ + const int c = j / Q4K6_G; // sub-group index 0..7
62
+ + float acc = 0.0f;
63
+ +#pragma unroll
64
+ + for (int kk = 0; kk < K; ++kk) {
65
+ + const uint8_t s = sb[kk*nsub + c];
66
+ + uint32_t bits = __float_as_uint(__half2float(sup[kk]));
67
+ + bits ^= ((uint32_t)(s & 0x80u)) << 24; // flip sign iff sub bit7 set
68
+ + const float sc = __uint_as_float(bits) * (float)(s & 0x3F);
69
+ + const int t = ((blk[kk*qb + tid] >> (bp*2)) & 3) - 1;
70
+ + acc += sc * (float) t;
71
+ + }
72
+ + y[l + bp*32] = (dst_t) acc;
73
+ + }
74
+ +}
75
+ +
76
+ +#define DEQUANT_TQKP_GV_CUDA(NAME, K, G) \
77
+ + template<typename dst_t> \
78
+ + static void NAME(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { \
79
+ + const int nb = k / QK_TQKP_SUPER; \
80
+ + dequantize_block_tqkp_gv<dst_t, K, G><<<nb, 64, 0, stream>>>(vx, y); \
81
+ + }
82
+ +#define DEQUANT_TQKP_Q4K6_CUDA(NAME, K) \
83
+ + template<typename dst_t> \
84
+ + static void NAME(const void * vx, dst_t * y, const int64_t k, cudaStream_t stream) { \
85
+ + const int nb = k / QK_TQKP_SUPER; \
86
+ + dequantize_block_tqkp_q4k6<dst_t, K><<<nb, 64, 0, stream>>>(vx, y); \
87
+ + }
88
+ +
89
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq1p_cuda, 1, 256)
90
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq2p_cuda, 2, 256)
91
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq3p_cuda, 3, 256)
92
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq4p_cuda, 4, 256)
93
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq1p_g32_cuda, 1, 32)
94
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq1p_g64_cuda, 1, 64)
95
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq1p_g128_cuda, 1, 128)
96
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq2p_g32_cuda, 2, 32)
97
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq2p_g64_cuda, 2, 64)
98
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq2p_g128_cuda, 2, 128)
99
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq3p_g32_cuda, 3, 32)
100
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq3p_g64_cuda, 3, 64)
101
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq3p_g128_cuda, 3, 128)
102
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq4p_g32_cuda, 4, 32)
103
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq4p_g64_cuda, 4, 64)
104
+ +DEQUANT_TQKP_GV_CUDA(dequantize_row_tq4p_g128_cuda, 4, 128)
105
+ +DEQUANT_TQKP_Q4K6_CUDA(dequantize_row_tq1p_q4k6_cuda, 1)
106
+ +DEQUANT_TQKP_Q4K6_CUDA(dequantize_row_tq2p_q4k6_cuda, 2)
107
+ +DEQUANT_TQKP_Q4K6_CUDA(dequantize_row_tq3p_q4k6_cuda, 3)
108
+ +
109
+ +
110
+ +// ---- sasori fused ternary mat-vec (decode path) ----
111
+ +// dst[row] = sum_col W[row,col] * x[col], reading PACKED TQ{K}P weights directly (no fp16 spill) ->
112
+ +// ~K*2/8 bytes/weight instead of 2 (fp16), i.e. much less weight bandwidth on the bandwidth-bound
113
+ +// decode. Activation x is fp32 (src1). Strided-256 K-plane layout mirrors dequantize_block_tqkp_gv.
114
+ +// One warp per output row; correctness-first (not yet memory-coalesced). MVP: f16-scale families only
115
+ +// (TQ{K}P == g256 + _G{32,64,128}); q4k6 still uses the dequant->cuBLAS path.
116
+ +template<int K, int G>
117
+ +static __global__ void mul_mat_vec_tqkp_kernel(
118
+ + const void * __restrict__ vx, const float * __restrict__ x, float * __restrict__ dst,
119
+ + const int ncols, const int nrows) {
120
+ + const int row = blockIdx.x;
121
+ + if (row >= nrows) return;
122
+ + const int tid = threadIdx.x;
123
+ + const int warp = tid >> 5;
124
+ + const int lane = tid & 31;
125
+ + const int nwarps = blockDim.x >> 5;
126
+ + const int nsub = 256 / G;
127
+ + const int qb = 64;
128
+ + const int bpb = K*qb + K*nsub*2;
129
+ + const int nsuper = ncols / 256;
130
+ + const uint8_t * __restrict__ row_base = (const uint8_t *) vx + (size_t) row * nsuper * bpb;
131
+ + float acc = 0.0f;
132
+ + for (int sb = warp; sb < nsuper; sb += nwarps) { // warps split super-blocks
133
+ + const uint8_t * __restrict__ blk = row_base + (size_t) sb * bpb;
134
+ + const half * __restrict__ d = (const half *) (blk + K*qb);
135
+ + const float * __restrict__ xs = x + sb*256;
136
+ + #pragma unroll
137
+ + for (int tb = lane; tb < 64; tb += 32) { // coalesced byte reads within a super-block
138
+ + const int n = tb >> 5, l = tb & 31;
139
+ + #pragma unroll
140
+ + for (int bp = 0; bp < 4; ++bp) {
141
+ + const int j = 128*n + bp*32 + l;
142
+ + const int sub = j / G;
143
+ + float w = 0.0f;
144
+ + #pragma unroll
145
+ + for (int kk = 0; kk < K; ++kk) {
146
+ + const int code = (blk[kk*qb + tb] >> (bp*2)) & 3;
147
+ + w += __half2float(d[kk*nsub + sub]) * (float) (code - 1);
148
+ + }
149
+ + acc += w * xs[j];
150
+ + }
151
+ + }
152
+ + }
153
+ + acc = warp_reduce_sum(acc);
154
+ + __shared__ float sh[32];
155
+ + if (lane == 0) sh[warp] = acc;
156
+ + __syncthreads();
157
+ + if (warp == 0) {
158
+ + float v = (lane < nwarps) ? sh[lane] : 0.0f;
159
+ + v = warp_reduce_sum(v);
160
+ + if (lane == 0) dst[row] = v;
161
+ + }
162
+ +}
163
+ +
164
+ +
165
+ +// q4k6-scale fused mat-vec (g=32): per-plane fp16 super-scale sign-flipped by sub bit7, times a 6-bit
166
+ +// unsigned sub-scale. Mirrors dequantize_block_tqkp_q4k6's scale decode; same coalesced structure.
167
+ +template<int K>
168
+ +static __global__ void mul_mat_vec_tqkp_q4k6_kernel(
169
+ + const void * __restrict__ vx, const float * __restrict__ x, float * __restrict__ dst,
170
+ + const int ncols, const int nrows) {
171
+ + const int row = blockIdx.x;
172
+ + if (row >= nrows) return;
173
+ + const int tid = threadIdx.x;
174
+ + const int warp = tid >> 5;
175
+ + const int lane = tid & 31;
176
+ + const int nwarps = blockDim.x >> 5;
177
+ + const int nsub = 256 / Q4K6_G;
178
+ + const int qb = 64;
179
+ + const int bpb = K*qb + K*nsub + K*2;
180
+ + const int nsuper = ncols / 256;
181
+ + const uint8_t * __restrict__ row_base = (const uint8_t *) vx + (size_t) row * nsuper * bpb;
182
+ + float acc = 0.0f;
183
+ + for (int sb = warp; sb < nsuper; sb += nwarps) {
184
+ + const uint8_t * __restrict__ blk = row_base + (size_t) sb * bpb;
185
+ + const uint8_t * __restrict__ sbsc = blk + K*qb;
186
+ + const half * __restrict__ sup = (const half *)(blk + K*qb + K*nsub);
187
+ + const float * __restrict__ xs = x + sb*256;
188
+ + #pragma unroll
189
+ + for (int tb = lane; tb < 64; tb += 32) {
190
+ + const int n = tb >> 5, l = tb & 31;
191
+ + #pragma unroll
192
+ + for (int bp = 0; bp < 4; ++bp) {
193
+ + const int j = 128*n + bp*32 + l;
194
+ + const int c = j / Q4K6_G;
195
+ + float w = 0.0f;
196
+ + #pragma unroll
197
+ + for (int kk = 0; kk < K; ++kk) {
198
+ + const uint8_t s = sbsc[kk*nsub + c];
199
+ + uint32_t bits = __float_as_uint(__half2float(sup[kk]));
200
+ + bits ^= ((uint32_t)(s & 0x80u)) << 24;
201
+ + const float sc = __uint_as_float(bits) * (float)(s & 0x3F);
202
+ + const int t = ((blk[kk*qb + tb] >> (bp*2)) & 3) - 1;
203
+ + w += sc * (float) t;
204
+ + }
205
+ + acc += w * xs[j];
206
+ + }
207
+ + }
208
+ + }
209
+ + acc = warp_reduce_sum(acc);
210
+ + __shared__ float sh[32];
211
+ + if (lane == 0) sh[warp] = acc;
212
+ + __syncthreads();
213
+ + if (warp == 0) {
214
+ + float v = (lane < nwarps) ? sh[lane] : 0.0f;
215
+ + v = warp_reduce_sum(v);
216
+ + if (lane == 0) dst[row] = v;
217
+ + }
218
+ +}
219
+ +
220
+ +void ggml_cuda_mul_mat_vec_tqkp(ggml_backend_cuda_context & ctx,
221
+ + const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst) {
222
+ + const int ncols = src0->ne[0];
223
+ + const int nrows = src0->ne[1];
224
+ + const float * x = (const float *) src1->data;
225
+ + float * d = (float *) dst->data;
226
+ + cudaStream_t stream = ctx.stream();
227
+ + const dim3 grid(nrows, 1, 1);
228
+ + switch (src0->type) {
229
+ + case GGML_TYPE_TQ1P: mul_mat_vec_tqkp_kernel<1,256><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
230
+ + case GGML_TYPE_TQ2P: mul_mat_vec_tqkp_kernel<2,256><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
231
+ + case GGML_TYPE_TQ3P: mul_mat_vec_tqkp_kernel<3,256><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
232
+ + case GGML_TYPE_TQ4P: mul_mat_vec_tqkp_kernel<4,256><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
233
+ + case GGML_TYPE_TQ1P_G32: mul_mat_vec_tqkp_kernel<1,32 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
234
+ + case GGML_TYPE_TQ1P_G64: mul_mat_vec_tqkp_kernel<1,64 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
235
+ + case GGML_TYPE_TQ1P_G128: mul_mat_vec_tqkp_kernel<1,128><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
236
+ + case GGML_TYPE_TQ2P_G32: mul_mat_vec_tqkp_kernel<2,32 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
237
+ + case GGML_TYPE_TQ2P_G64: mul_mat_vec_tqkp_kernel<2,64 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
238
+ + case GGML_TYPE_TQ2P_G128: mul_mat_vec_tqkp_kernel<2,128><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
239
+ + case GGML_TYPE_TQ3P_G32: mul_mat_vec_tqkp_kernel<3,32 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
240
+ + case GGML_TYPE_TQ3P_G64: mul_mat_vec_tqkp_kernel<3,64 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
241
+ + case GGML_TYPE_TQ3P_G128: mul_mat_vec_tqkp_kernel<3,128><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
242
+ + case GGML_TYPE_TQ4P_G32: mul_mat_vec_tqkp_kernel<4,32 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
243
+ + case GGML_TYPE_TQ4P_G64: mul_mat_vec_tqkp_kernel<4,64 ><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
244
+ + case GGML_TYPE_TQ4P_G128: mul_mat_vec_tqkp_kernel<4,128><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
245
+ + case GGML_TYPE_TQ1P_Q4K6: mul_mat_vec_tqkp_q4k6_kernel<1><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
246
+ + case GGML_TYPE_TQ2P_Q4K6: mul_mat_vec_tqkp_q4k6_kernel<2><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
247
+ + case GGML_TYPE_TQ3P_Q4K6: mul_mat_vec_tqkp_q4k6_kernel<3><<<grid,128,0,stream>>>(src0->data,x,d,ncols,nrows); break;
248
+ + default: GGML_ABORT("mul_mat_vec_tqkp: unsupported type");
249
+ + }
250
+ +}
251
+ +
252
+ +bool ggml_cuda_is_tqkp_fused(enum ggml_type t) {
253
+ + switch (t) {
254
+ + case GGML_TYPE_TQ1P: case GGML_TYPE_TQ2P: case GGML_TYPE_TQ3P: case GGML_TYPE_TQ4P:
255
+ + case GGML_TYPE_TQ1P_G32: case GGML_TYPE_TQ1P_G64: case GGML_TYPE_TQ1P_G128:
256
+ + case GGML_TYPE_TQ2P_G32: case GGML_TYPE_TQ2P_G64: case GGML_TYPE_TQ2P_G128:
257
+ + case GGML_TYPE_TQ3P_G32: case GGML_TYPE_TQ3P_G64: case GGML_TYPE_TQ3P_G128:
258
+ + case GGML_TYPE_TQ4P_G32: case GGML_TYPE_TQ4P_G64: case GGML_TYPE_TQ4P_G128:
259
+ + case GGML_TYPE_TQ1P_Q4K6: case GGML_TYPE_TQ2P_Q4K6: case GGML_TYPE_TQ3P_Q4K6:
260
+ + return true;
261
+ + default: return false;
262
+ + }
263
+ +}
264
+ +
265
+ to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
266
+ switch (type) {
267
+ case GGML_TYPE_Q1_0:
268
+ @@ -736,6 +992,44 @@ to_fp16_cuda_t ggml_get_to_fp16_cuda(ggml_type type) {
269
+ return dequantize_row_q5_K_cuda;
270
+ case GGML_TYPE_Q6_K:
271
+ return dequantize_row_q6_K_cuda;
272
+ + case GGML_TYPE_TQ1P:
273
+ + return dequantize_row_tq1p_cuda;
274
+ + case GGML_TYPE_TQ2P:
275
+ + return dequantize_row_tq2p_cuda;
276
+ + case GGML_TYPE_TQ3P:
277
+ + return dequantize_row_tq3p_cuda;
278
+ + case GGML_TYPE_TQ4P:
279
+ + return dequantize_row_tq4p_cuda;
280
+ + case GGML_TYPE_TQ1P_G32:
281
+ + return dequantize_row_tq1p_g32_cuda;
282
+ + case GGML_TYPE_TQ1P_G64:
283
+ + return dequantize_row_tq1p_g64_cuda;
284
+ + case GGML_TYPE_TQ1P_G128:
285
+ + return dequantize_row_tq1p_g128_cuda;
286
+ + case GGML_TYPE_TQ2P_G32:
287
+ + return dequantize_row_tq2p_g32_cuda;
288
+ + case GGML_TYPE_TQ2P_G64:
289
+ + return dequantize_row_tq2p_g64_cuda;
290
+ + case GGML_TYPE_TQ2P_G128:
291
+ + return dequantize_row_tq2p_g128_cuda;
292
+ + case GGML_TYPE_TQ3P_G32:
293
+ + return dequantize_row_tq3p_g32_cuda;
294
+ + case GGML_TYPE_TQ3P_G64:
295
+ + return dequantize_row_tq3p_g64_cuda;
296
+ + case GGML_TYPE_TQ3P_G128:
297
+ + return dequantize_row_tq3p_g128_cuda;
298
+ + case GGML_TYPE_TQ4P_G32:
299
+ + return dequantize_row_tq4p_g32_cuda;
300
+ + case GGML_TYPE_TQ4P_G64:
301
+ + return dequantize_row_tq4p_g64_cuda;
302
+ + case GGML_TYPE_TQ4P_G128:
303
+ + return dequantize_row_tq4p_g128_cuda;
304
+ + case GGML_TYPE_TQ1P_Q4K6:
305
+ + return dequantize_row_tq1p_q4k6_cuda;
306
+ + case GGML_TYPE_TQ2P_Q4K6:
307
+ + return dequantize_row_tq2p_q4k6_cuda;
308
+ + case GGML_TYPE_TQ3P_Q4K6:
309
+ + return dequantize_row_tq3p_q4k6_cuda;
310
+ case GGML_TYPE_IQ2_XXS:
311
+ return dequantize_row_iq2_xxs_cuda;
312
+ case GGML_TYPE_IQ2_XS:
313
+ @@ -791,6 +1085,44 @@ to_fp32_cuda_t ggml_get_to_fp32_cuda(ggml_type type) {
314
+ return dequantize_row_q5_K_cuda;
315
+ case GGML_TYPE_Q6_K:
316
+ return dequantize_row_q6_K_cuda;
317
+ + case GGML_TYPE_TQ1P:
318
+ + return dequantize_row_tq1p_cuda;
319
+ + case GGML_TYPE_TQ2P:
320
+ + return dequantize_row_tq2p_cuda;
321
+ + case GGML_TYPE_TQ3P:
322
+ + return dequantize_row_tq3p_cuda;
323
+ + case GGML_TYPE_TQ4P:
324
+ + return dequantize_row_tq4p_cuda;
325
+ + case GGML_TYPE_TQ1P_G32:
326
+ + return dequantize_row_tq1p_g32_cuda;
327
+ + case GGML_TYPE_TQ1P_G64:
328
+ + return dequantize_row_tq1p_g64_cuda;
329
+ + case GGML_TYPE_TQ1P_G128:
330
+ + return dequantize_row_tq1p_g128_cuda;
331
+ + case GGML_TYPE_TQ2P_G32:
332
+ + return dequantize_row_tq2p_g32_cuda;
333
+ + case GGML_TYPE_TQ2P_G64:
334
+ + return dequantize_row_tq2p_g64_cuda;
335
+ + case GGML_TYPE_TQ2P_G128:
336
+ + return dequantize_row_tq2p_g128_cuda;
337
+ + case GGML_TYPE_TQ3P_G32:
338
+ + return dequantize_row_tq3p_g32_cuda;
339
+ + case GGML_TYPE_TQ3P_G64:
340
+ + return dequantize_row_tq3p_g64_cuda;
341
+ + case GGML_TYPE_TQ3P_G128:
342
+ + return dequantize_row_tq3p_g128_cuda;
343
+ + case GGML_TYPE_TQ4P_G32:
344
+ + return dequantize_row_tq4p_g32_cuda;
345
+ + case GGML_TYPE_TQ4P_G64:
346
+ + return dequantize_row_tq4p_g64_cuda;
347
+ + case GGML_TYPE_TQ4P_G128:
348
+ + return dequantize_row_tq4p_g128_cuda;
349
+ + case GGML_TYPE_TQ1P_Q4K6:
350
+ + return dequantize_row_tq1p_q4k6_cuda;
351
+ + case GGML_TYPE_TQ2P_Q4K6:
352
+ + return dequantize_row_tq2p_q4k6_cuda;
353
+ + case GGML_TYPE_TQ3P_Q4K6:
354
+ + return dequantize_row_tq3p_q4k6_cuda;
355
+ case GGML_TYPE_IQ2_XXS:
356
+ return dequantize_row_iq2_xxs_cuda;
357
+ case GGML_TYPE_IQ2_XS:
358
+ diff --git a/src/ggml-cuda/convert.cuh b/src/ggml-cuda/convert.cuh
359
+ index f5d37c7..707fcc8 100644
360
+ --- a/src/ggml-cuda/convert.cuh
361
+ +++ b/src/ggml-cuda/convert.cuh
362
+ @@ -64,3 +64,8 @@ template<typename dst_t, typename src_t>
363
+ return float(x);
364
+ }
365
+ }
366
+ +
367
+ +// sasori fused ternary mat-vec (decode): reads packed TQ{K}P weights directly (f16-scale families).
368
+ +void ggml_cuda_mul_mat_vec_tqkp(ggml_backend_cuda_context & ctx,
369
+ + const ggml_tensor * src0, const ggml_tensor * src1, ggml_tensor * dst);
370
+ +bool ggml_cuda_is_tqkp_fused(enum ggml_type t);
371
+ diff --git a/src/ggml-cuda/ggml-cuda.cu b/src/ggml-cuda/ggml-cuda.cu
372
+ index f5293ad..0509752 100644
373
+ --- a/src/ggml-cuda/ggml-cuda.cu
374
+ +++ b/src/ggml-cuda/ggml-cuda.cu
375
+ @@ -2500,6 +2500,20 @@ static bool ggml_cuda_should_fuse_mul_mat_vec_f(const ggml_tensor * tensor) {
376
+ }
377
+
378
+ static bool ggml_cuda_should_fuse_mul_mat_vec_q(const ggml_tensor * tensor) {
379
+ + // sasori: ternary trit-plane types have no fused mmvq kernel -> never fuse (would GGML_ABORT);
380
+ + // they take the dequant -> cuBLAS path via ggml_get_to_fp16_cuda.
381
+ + switch (tensor->src[0]->type) {
382
+ + case GGML_TYPE_TQ1P: case GGML_TYPE_TQ2P: case GGML_TYPE_TQ3P:
383
+ + case GGML_TYPE_TQ4P: case GGML_TYPE_TQ1P_G32: case GGML_TYPE_TQ1P_G64:
384
+ + case GGML_TYPE_TQ1P_G128: case GGML_TYPE_TQ2P_G32: case GGML_TYPE_TQ2P_G64:
385
+ + case GGML_TYPE_TQ2P_G128: case GGML_TYPE_TQ3P_G32: case GGML_TYPE_TQ3P_G64:
386
+ + case GGML_TYPE_TQ3P_G128: case GGML_TYPE_TQ4P_G32: case GGML_TYPE_TQ4P_G64:
387
+ + case GGML_TYPE_TQ4P_G128: case GGML_TYPE_TQ1P_Q4K6: case GGML_TYPE_TQ2P_Q4K6:
388
+ + case GGML_TYPE_TQ3P_Q4K6:
389
+ + return false;
390
+ + default:
391
+ + break;
392
+ + }
393
+ ggml_tensor * src0 = tensor->src[0];
394
+ ggml_tensor * src1 = tensor->src[1];
395
+ const ggml_tensor * dst = tensor;
396
+ @@ -2604,6 +2618,17 @@ static void ggml_cuda_mul_mat(ggml_backend_cuda_context & ctx, const ggml_tensor
397
+ return;
398
+ }
399
+
400
+ + // sasori: fused ternary mat-vec for the DECODE case (ne11==1) — reads packed TQ weights directly
401
+ + // instead of the dequant->cuBLAS path (much less weight bandwidth). f16-scale families only.
402
+ + // Toggle off with SASORI_NO_FUSED_MMVEC=1 (A/B correctness vs the dequant->cuBLAS reference).
403
+ + static const bool sasori_fused_off = getenv("SASORI_NO_FUSED_MMVEC") != nullptr;
404
+ + if (!sasori_fused_off && !split && ggml_cuda_is_tqkp_fused(src0->type) &&
405
+ + src1->type == GGML_TYPE_F32 && src1->ne[1] == 1 && src1->ne[2] == 1 && src1->ne[3] == 1 &&
406
+ + ggml_is_contiguous(src1) && ggml_is_contiguous(src0)) {
407
+ + ggml_cuda_mul_mat_vec_tqkp(ctx, src0, src1, dst);
408
+ + return;
409
+ + }
410
+ +
411
+ if (!split && use_mul_mat_vec_f) {
412
+ // the custom F16 vector kernel can be used over batched cuBLAS GEMM
413
+ // but this is only faster for GPUs without tensor cores or with a thin src0 matrix (particularly KQV in attention)
414
+ @@ -5166,6 +5191,25 @@ static bool ggml_backend_cuda_device_supports_op(ggml_backend_dev_t dev, const g
415
+ case GGML_TYPE_Q5_K:
416
+ case GGML_TYPE_Q6_K:
417
+ case GGML_TYPE_Q8_K:
418
+ + case GGML_TYPE_TQ1P:
419
+ + case GGML_TYPE_TQ2P:
420
+ + case GGML_TYPE_TQ3P:
421
+ + case GGML_TYPE_TQ4P:
422
+ + case GGML_TYPE_TQ1P_G32:
423
+ + case GGML_TYPE_TQ1P_G64:
424
+ + case GGML_TYPE_TQ1P_G128:
425
+ + case GGML_TYPE_TQ2P_G32:
426
+ + case GGML_TYPE_TQ2P_G64:
427
+ + case GGML_TYPE_TQ2P_G128:
428
+ + case GGML_TYPE_TQ3P_G32:
429
+ + case GGML_TYPE_TQ3P_G64:
430
+ + case GGML_TYPE_TQ3P_G128:
431
+ + case GGML_TYPE_TQ4P_G32:
432
+ + case GGML_TYPE_TQ4P_G64:
433
+ + case GGML_TYPE_TQ4P_G128:
434
+ + case GGML_TYPE_TQ1P_Q4K6:
435
+ + case GGML_TYPE_TQ2P_Q4K6:
436
+ + case GGML_TYPE_TQ3P_Q4K6:
437
+ case GGML_TYPE_IQ1_M:
438
+ case GGML_TYPE_IQ1_S:
439
+ case GGML_TYPE_IQ2_S:
440
+ diff --git a/src/ggml-cuda/mmvq.cu b/src/ggml-cuda/mmvq.cu
441
+ index 4b04265..555d74b 100644
442
+ --- a/src/ggml-cuda/mmvq.cu
443
+ +++ b/src/ggml-cuda/mmvq.cu
444
+ @@ -278,6 +278,21 @@ int get_mmvq_mmid_max_batch(ggml_type type, int cc) {
445
+ }
446
+
447
+ bool ggml_cuda_should_use_mmvq(enum ggml_type type, int cc, int64_t ne11) {
448
+ + // sasori ternary trit-plane types have no mmvq (or mmq) kernel yet: force the
449
+ + // dequant -> cuBLAS GEMM path (they are wired into ggml_get_to_fp16_cuda). Without
450
+ + // this, use_mul_mat_vec_q would be true and mul_mat_vec_q_switch_type would GGML_ABORT.
451
+ + switch (type) {
452
+ + case GGML_TYPE_TQ1P: case GGML_TYPE_TQ2P: case GGML_TYPE_TQ3P:
453
+ + case GGML_TYPE_TQ4P: case GGML_TYPE_TQ1P_G32: case GGML_TYPE_TQ1P_G64:
454
+ + case GGML_TYPE_TQ1P_G128: case GGML_TYPE_TQ2P_G32: case GGML_TYPE_TQ2P_G64:
455
+ + case GGML_TYPE_TQ2P_G128: case GGML_TYPE_TQ3P_G32: case GGML_TYPE_TQ3P_G64:
456
+ + case GGML_TYPE_TQ3P_G128: case GGML_TYPE_TQ4P_G32: case GGML_TYPE_TQ4P_G64:
457
+ + case GGML_TYPE_TQ4P_G128: case GGML_TYPE_TQ1P_Q4K6: case GGML_TYPE_TQ2P_Q4K6:
458
+ + case GGML_TYPE_TQ3P_Q4K6:
459
+ + return false;
460
+ + default:
461
+ + break;
462
+ + }
463
+ if (GGML_CUDA_CC_IS_CDNA(cc)) {
464
+ if (GGML_CUDA_CC_IS_CDNA1(cc)) {
465
+ switch (type) {
samples/fp_p0_s42.png ADDED

Git LFS Details

  • SHA256: 213e1f5f5352c50c1d491490850aae3171525ed346947a8da64f28a0078991f3
  • Pointer size: 132 Bytes
  • Size of remote file: 2.14 MB
samples/fp_p6_s42.png ADDED

Git LFS Details

  • SHA256: 59c390fefe5ef67a5deb9bb0740d602f1c7441ffaea4c702513e876d45400e25
  • Pointer size: 132 Bytes
  • Size of remote file: 2.03 MB
samples/k3_p0_s42.png ADDED

Git LFS Details

  • SHA256: febbfb25409c10ab4c1a8d794816d582ed6c63de2b0ab8a1eb5c822902279e6e
  • Pointer size: 132 Bytes
  • Size of remote file: 2.13 MB
samples/k3_p6_s42.png ADDED

Git LFS Details

  • SHA256: bfac70b72c85918e73d441dfa6532f47222d3cd47b6ff973fbf048bed4289e0d
  • Pointer size: 132 Bytes
  • Size of remote file: 2 MB
samples/k3te_p0_s42.png ADDED

Git LFS Details

  • SHA256: f010421b349f5ced2a9747e289e9e9bdba41b21378f13d572fba2cb1cabbd30c
  • Pointer size: 132 Bytes
  • Size of remote file: 2.11 MB
samples/k3te_p6_s42.png ADDED

Git LFS Details

  • SHA256: 4986e776fccec43c5bb8de91b7bc665f2895225c527c9d44523d6644a172641f
  • Pointer size: 132 Bytes
  • Size of remote file: 2.01 MB
samples/mosaic_p0.jpg ADDED
samples/mosaic_p6.jpg ADDED
samples/q4km_p0_s42.png ADDED

Git LFS Details

  • SHA256: 0019f2787b5ed1776d59c73e2b7fc86d4538c701cbb58a75bde6b1e7c9efe169
  • Pointer size: 132 Bytes
  • Size of remote file: 2.13 MB
samples/q4km_p6_s42.png ADDED

Git LFS Details

  • SHA256: b6cb92dc3b5126948662e48cea6cb161d14cea642e935e102b4111995aee714b
  • Pointer size: 132 Bytes
  • Size of remote file: 2 MB
text_encoder/Qwen3-8B-TQ3P.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9cb46cba22a6cf01989ddf8f8d1c67eabb3e7c32d9c0a872fc810e883c371274
3
+ size 7868626528