sebekblessing commited on
Commit
546f6de
·
verified ·
1 Parent(s): 4c9d51d

MOSS-Image for phone graphics chips: engine weights, decoder, reader, file list

Browse files
.gitattributes CHANGED
@@ -33,3 +33,6 @@ 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
+ files/moss_q8.gguf filter=lfs diff=lfs merge=lfs -text
37
+ files/tae_f2.gguf filter=lfs diff=lfs merge=lfs -text
38
+ files/moss_reader.gguf filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ license_link: https://www.apache.org/licenses/LICENSE-2.0
4
+ base_model: OpenMOSS-Team/MOSS-Image
5
+ tags:
6
+ - text-to-image
7
+ - android
8
+ - opencl
9
+ ---
10
+
11
+ # MOSS-Image for phone graphics chips
12
+
13
+ Files the Anima Android app downloads to draw with MOSS-Image on a phone's graphics chip, on phones that have no
14
+ Snapdragon AI chip the app supports. Phones with a Snapdragon 8 Elite or 8 Elite Gen 5 use the AI-chip build in
15
+ `moss-qnn-8elite` / `moss-qnn-8elite-gen5` instead, which is faster.
16
+
17
+ | File | What it is |
18
+ | --- | --- |
19
+ | `files/moss_q8.gguf` | MOSS-Image SFT with the 8-step add-on merged in (weight 0.7), 8-bit, in the app engine's own layout. Read one block at a time while drawing, so it does not need to fit in memory. |
20
+ | `files/tae_f2.gguf` | The small Flux 2 picture decoder (taef2 by madebyollin) in the engine's layout. |
21
+ | `files/moss_reader.gguf` | The prompt reader, Qwen 3.5 9B, 4-bit (Q4_K_M with an importance matrix, unused parts removed). Same file as in the AI-chip repos. |
22
+ | `files/sigmas.txt`, `files/sampler.txt` | The 8 noise levels of the add-on, and its sampler. |
23
+ | `manifest.json` | The file list the app reads (name, size, SHA-256). |
24
+
25
+ `build/pack_moss.py` and `build/pack_tae.py` are the scripts that made the two `.gguf` drawing files.
26
+
27
+ Licences: MOSS-Image and its 8-step add-on, Apache 2.0. Qwen 3.5, Apache 2.0. taef2, MIT.
build/pack_moss.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """MOSS-Image (SFT + the 8-step DMD add-on) -> one GGUF for the universal phone engine (2026-11-18).
2
+ Big layers 4-bit (ConvRot, as pack_qwen.py) on the graphics card; small ones fp16 / fp32. The add-on (strength 0.7,
3
+ alpha / rank) is MERGED into the weights in fp32 before rounding: rounding W + dW keeps dW's effect, it only changes
4
+ which rounding noise lands where.
5
+ Layout for the engine:
6
+ - q, k, v as one layer "qkv" (heads already in split-half order, no reordering); the MLP's up / gate stored
7
+ [gate 8][up 8] per 16 rows for the engine's paired activation (SITU in model_moss.h).
8
+ - norms (n1, n2, final) kept as fp32 vectors, folded into the modulation on the host each step.
9
+ - the 2 prompt blocks (tb0, tb1) and the prompt projection with their biases ("<name>.b").
10
+ Usage: python pack_moss.py <out.gguf> [strength 0.7] [lora file] (ComfyUI's ROCm venv; BITS=8 for 8-bit)"""
11
+ import os
12
+ import sys
13
+ sys.path[:0] = ["/home/debian/gpu-engine-test/tsbuild/anima", "/home/debian/llama.cpp/gguf-py"]
14
+ import numpy as np
15
+ import torch
16
+ from safetensors import safe_open
17
+ import anima_qnn
18
+ import gguf
19
+
20
+ OUT = sys.argv[1]
21
+ ST = float(sys.argv[2]) if len(sys.argv) > 2 else 0.7
22
+ LP = sys.argv[3] if len(sys.argv) > 3 else "/home/debian/moss-build/dmd/lora_latest.safetensors"
23
+ GS, BITS = 32, int(os.environ.get("BITS", "4"))
24
+ SRC = "/home/debian/moss-build/models/moss_image_sft_bf16.safetensors"
25
+ D, H, HD, FF, NB = 3072, 24, 128, 8192, 42
26
+ dev = "cuda" if torch.cuda.is_available() else "cpu"
27
+ HT = anima_qnn._HT.to(dev)
28
+ f = safe_open(SRC, "pt")
29
+ lf = safe_open(LP, "pt") if ST > 0 else None
30
+ g = lambda k: f.get_tensor(k).to(dev).float()
31
+ writer = gguf.GGUFWriter(OUT, "ugpu")
32
+ index = []
33
+
34
+ def put(name, kind, a, M, K):
35
+ writer.add_tensor(name, np.ascontiguousarray(a))
36
+ index.append(f"{name} {kind} {M} {K}")
37
+ def f16(name, w): put(name, "f16", w.half().contiguous().cpu().numpy(), *w.shape)
38
+ def f32(name, v): put(name, "f32", v.float().contiguous().cpu().numpy(), v.numel(), 1)
39
+ def rotw(w):
40
+ M, K = w.shape
41
+ return (w.reshape(M, K // HT.shape[0], HT.shape[0]) @ HT).reshape(M, K)
42
+ def q8(name, w, b=None):
43
+ M, K = w.shape
44
+ t = rotw(w).view(M, K // GS, GS)
45
+ s = t.abs().amax(-1, keepdim=True).clamp_min(1e-12) / 127
46
+ q = (t / s).round().clamp(-127, 127).to(torch.int8).view(M, K)
47
+ put(name, "q8", q.view(M, K // 16, 16).permute(1, 0, 2).contiguous().cpu().numpy(), M, K)
48
+ put(name + ".s", "qs", s.view(M, K // GS).contiguous().cpu().numpy().astype(np.float32), M, K // GS)
49
+ if b is not None: f32(name + ".b", b)
50
+ def q4(name, w, b=None):
51
+ M, K = w.shape
52
+ t = rotw(w).view(M, K // GS, GS)
53
+ s = t.abs().amax(-1, keepdim=True).clamp_min(1e-12) / 7
54
+ q = (t / s).round().clamp(-8, 7).to(torch.int16).view(M, K // 32, 2, 16)
55
+ by = ((q[:, :, 0] & 15) | ((q[:, :, 1] & 15) << 4)).to(torch.uint8)
56
+ put(name, "q4", by.permute(1, 0, 2).contiguous().cpu().numpy().view(np.int8), M, K)
57
+ put(name + ".s", "qs", (s / 16).view(M, K // GS).contiguous().cpu().numpy().astype(np.float32), M, K // GS)
58
+ if b is not None: f32(name + ".b", b)
59
+ qb4 = q4 if BITS == 4 else q8
60
+ MIX = set(filter(None, os.environ.get("MIX", "").split(","))) # drawing-block layer kinds kept 8-bit, e.g. "down,o"
61
+ def qb(name, w, b=None):
62
+ kind = name.split(".")[-1]
63
+ (q8 if name.startswith("b") and kind in MIX else qb4)(name, w, b)
64
+ def pair_rows(gate, up):
65
+ """[gate 8][up 8] per 16 rows, as the engine's paired-activation output expects"""
66
+ n = gate.shape[0]
67
+ return torch.stack([gate.view(n // 8, 8, -1), up.view(n // 8, 8, -1)], 1).reshape(2 * n, -1)
68
+ def pair_vec(gate, up):
69
+ n = gate.shape[0]
70
+ return torch.stack([gate.view(n // 8, 8), up.view(n // 8, 8)], 1).reshape(2 * n)
71
+ def lora(i, key):
72
+ if lf is None: return 0.0
73
+ p = f"diffusion_model.layers.{i}.proj.{key}."
74
+ dn, up = lf.get_tensor(p + "lora_down.weight").to(dev).float(), lf.get_tensor(p + "lora_up.weight").to(dev).float()
75
+ a = float(lf.get_tensor(p + "alpha")) if p + "alpha" in lf.keys() else dn.shape[0]
76
+ return (up @ dn) * (ST * a / dn.shape[0])
77
+
78
+ with torch.no_grad():
79
+ for i in range(NB):
80
+ p, o = f"layers.{i}.", f"b{i}."
81
+ qkv = g(p + "proj.qkv.weight")
82
+ qkv = qkv + torch.cat([lora(i, "q"), lora(i, "k"), lora(i, "v")], 0) if lf is not None else qkv
83
+ qb(o + "qkv", qkv)
84
+ qb(o + "o", g(p + "proj.o.weight") + lora(i, "o"))
85
+ ug = g(p + "proj.ffn_up_gate.weight") + lora(i, "ffn_up_gate")
86
+ up, gate = ug.chunk(2, 0)
87
+ qb(o + "mlp_in", pair_rows(gate, up))
88
+ qb(o + "down", g(p + "proj.ffn_down.weight") + lora(i, "ffn_down"))
89
+ f32(o + "qn", g(p + "q_norm.weight")); f32(o + "kn", g(p + "k_norm.weight"))
90
+ f32(o + "n1", g(p + "norm1.weight")); f32(o + "n2", g(p + "norm2.weight"))
91
+ print("block", i, flush=True)
92
+ for j in range(2):
93
+ p, o = f"text_embedder.blocks.{j}.", f"tb{j}."
94
+ qb(o + "qkv", g(p + "qkv.weight"), g(p + "qkv.bias"))
95
+ qb(o + "o", g(p + "proj.weight"), g(p + "proj.bias"))
96
+ qb(o + "mlp_in", pair_rows(g(p + "ffn_gate.weight"), g(p + "ffn_up.weight")),
97
+ pair_vec(g(p + "ffn_gate.bias"), g(p + "ffn_up.bias")))
98
+ qb(o + "down", g(p + "ffn_down.weight"), g(p + "ffn_down.bias"))
99
+ f32(o + "qn", g(p + "q_norm.weight")); f32(o + "kn", g(p + "k_norm.weight"))
100
+ f32(o + "n1", g(p + "norm1.weight")); f32(o + "n2", g(p + "norm2.weight"))
101
+ qb("tproj", g("text_embedder.proj.weight"), g("text_embedder.proj.bias"))
102
+ vw = g("video_embedder.weight") # 128 inputs, padded to the rotation's
103
+ q8("vin", torch.cat([vw, torch.zeros_like(vw)], 1), g("video_embedder.bias")) # 256 (the engine feeds zeros there)
104
+ q8("fout", g("final_linear_video.weight"))
105
+ f32("fnorm", g("final_norm_video.weight"))
106
+ f16("t1", g("time_embedding.0.weight")); f32("t1.b", g("time_embedding.0.bias"))
107
+ f16("t2", g("time_embedding.2.weight")); f32("t2.b", g("time_embedding.2.bias"))
108
+ f16("mod", g("shared_block_modulation.weight")); f32("mod.b", g("shared_block_modulation.bias"))
109
+ f16("hmod", g("video_head_modulation.weight")); f32("hmod.b", g("video_head_modulation.bias"))
110
+ writer.add_string("ugpu.model", "moss")
111
+ writer.add_string("ugpu.index", "\n".join(index))
112
+ writer.write_header_to_file(); writer.write_kv_data_to_file(); writer.write_tensors_to_file(); writer.close()
113
+ print("wrote", OUT, os.path.getsize(OUT) / 1e9, "GB,", len(index), "tensors")
build/pack_tae.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Tiny picture decoders (the TAESD family: taeqi2_1 for Qwen, taef2 for Klein, ...) -> one GGUF for the engine.
2
+ The decoder is rebuilt with the reference code and walked layer by layer; the walk is written as a small program
3
+ ("ugpu.program", one op per line) that model_tae.h runs:
4
+ clamp x = 3 tanh(x / 3)
5
+ conv <name> <ci> <co> <k> <bias> <relu> <up> k x k filter, padding k/2; up = input doubled first (nearest)
6
+ pool <name> <ci> <cg> <groups> x += p3(relu(groupnorm(p0(x)))), p0: ci -> cg 1x1, p3 back (flux_2)
7
+ block <name> <ci> <co> <skip> relu(c2(relu(c1(relu(c0(x))))) + skip(x)), skip = 1x1 filter or x itself
8
+ shuffle <r> channels -> r x r pixels (PixelShuffle)
9
+ out <channels> channels kept in the picture
10
+ Filters are stored fp16 as [out][ky][kx][in]; biases fp32.
11
+ Usage: python pack_tae.py <taesd file> <out.gguf> <latent channels> <arch variant or ""> <image channels>"""
12
+ import sys
13
+ sys.path[:0] = ["/home/debian/phone-v81/qwen/build/common", "/home/debian/llama.cpp/gguf-py"]
14
+ import numpy as np
15
+ import torch
16
+ import torch.nn as nn
17
+ from safetensors.torch import load_file
18
+ from taesd import TAESD, Clamp, Block
19
+ import gguf
20
+
21
+ SRC, OUT, LC, ARCH, IC = sys.argv[1], sys.argv[2], int(sys.argv[3]), sys.argv[4] or None, int(sys.argv[5])
22
+ t = TAESD(encoder_path=None, decoder_path=None, latent_channels=LC, arch_variant=ARCH, image_channels=IC)
23
+ sd = load_file(SRC)
24
+ if any(k.startswith("decoder.layers.") for k in sd): # the Sequential's index 0 (Clamp) has no weights in these files
25
+ sd = {".".join([e, str(int(i) + (1 if e == "decoder" else 0)), *s]): v
26
+ for k, v in sd.items() for e, _l, i, *s in [k.split(".")]}
27
+ t.load_state_dict({k: v.float() for k, v in sd.items()})
28
+ writer = gguf.GGUFWriter(OUT, "ugpu")
29
+ index, prog = [], []
30
+
31
+ def put(name, kind, a, M, K):
32
+ writer.add_tensor(name, np.ascontiguousarray(a))
33
+ index.append(f"{name} {kind} {M} {K}")
34
+ def filt(name, c):
35
+ w = c.weight.detach().permute(0, 2, 3, 1).contiguous() # [out][ky][kx][in]
36
+ co = (c.out_channels + 3) // 4 * 4 # the engine works in groups of 4 channels:
37
+ w = torch.cat([w, w.new_zeros(co - w.shape[0], *w.shape[1:])]) # pad with zero outputs (e.g. RGB -> RGB0)
38
+ put(name, "f16", w.half().reshape(w.shape[0], -1).numpy(), w.shape[0], w[0].numel())
39
+ if c.bias is not None:
40
+ b = torch.cat([c.bias.detach().float(), torch.zeros(co - c.out_channels)])
41
+ put(name + ".b", "f32", b.numpy(), co, 1)
42
+ return c.in_channels, co, c.kernel_size[0], int(c.bias is not None)
43
+
44
+ layers, up, i = list(t.decoder), 0, 0
45
+ while i < len(layers):
46
+ m = layers[i]
47
+ if isinstance(m, Clamp): prog.append("clamp")
48
+ elif isinstance(m, nn.Upsample): up = 1
49
+ elif isinstance(m, nn.Conv2d):
50
+ assert m.stride == (1, 1) and m.padding[0] == m.kernel_size[0] // 2
51
+ relu = int(i + 1 < len(layers) and isinstance(layers[i + 1], nn.ReLU))
52
+ ci, co, k, b = filt(f"l{i}", m)
53
+ prog.append(f"conv l{i} {ci} {co} {k} {b} {relu} {up}")
54
+ up = 0
55
+ i += relu
56
+ elif isinstance(m, Block):
57
+ if m.pool is not None: # flux_2: x += p3(relu(groupnorm(p0(x)))) before the block
58
+ p0, gn, p3 = m.pool[0], m.pool[1], m.pool[3]
59
+ filt(f"l{i}.p0", p0); filt(f"l{i}.p3", p3)
60
+ put(f"l{i}.gn.w", "f32", gn.weight.detach().float().numpy(), gn.num_channels, 1)
61
+ put(f"l{i}.gn.b", "f32", gn.bias.detach().float().numpy(), gn.num_channels, 1)
62
+ prog.append(f"pool l{i} {p0.in_channels} {p0.out_channels} {gn.num_groups}")
63
+ c = [x for x in m.conv if isinstance(x, nn.Conv2d)]
64
+ for j, cj in enumerate(c): filt(f"l{i}.c{j}", cj)
65
+ skip = int(isinstance(m.skip, nn.Conv2d))
66
+ if skip: filt(f"l{i}.skip", m.skip)
67
+ prog.append(f"block l{i} {c[0].in_channels} {c[0].out_channels} {skip}")
68
+ elif isinstance(m, nn.PixelShuffle): prog.append(f"shuffle {m.upscale_factor}")
69
+ elif isinstance(m, nn.ReLU): raise SystemExit(f"ReLU at {i} not after a filter")
70
+ else: raise SystemExit(f"layer {i} ({type(m).__name__}) not supported")
71
+ i += 1
72
+ prog.append(f"out {min(IC, 3)}")
73
+ writer.add_string("ugpu.model", "tae")
74
+ writer.add_string("ugpu.index", "\n".join(index))
75
+ writer.add_string("ugpu.program", "\n".join(prog))
76
+ writer.write_header_to_file(); writer.write_kv_data_to_file(); writer.write_tensors_to_file(); writer.close()
77
+ print("\n".join(prog))
78
+ print("wrote", OUT, len(index), "tensors")
files/moss_q8.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2f3e925f0eca5050c2ee64eeae6c8cef5fbeaf5410fb5e149699ac5de152c7a0
3
+ size 5794189824
files/moss_reader.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6cc3d0f5fb4482e23b4bcd841d6ce56112eeb8c67fd41c3b59d9903db7fce912
3
+ size 4794753696
files/sampler.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ euler
files/sigmas.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ 1.000000
2
+ 0.968000
3
+ 0.926000
4
+ 0.871000
5
+ 0.791000
6
+ 0.671000
7
+ 0.463000
8
+ 0.024000
files/tae_f2.gguf ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7482e8d7a55c85e37d9ce138026a18b26c66a00a2af23c5430f671c13196d8ce
3
+ size 2690432
manifest.json ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "base": "https://huggingface.co/sebekblessing/moss-gpu/resolve/main/",
3
+ "shared": [],
4
+ "variants": {
5
+ "gpu": {
6
+ "label": "MOSS-Image (graphics chip)",
7
+ "about": "MOSS-Image SFT with the 8-step add-on, drawn on the graphics chip: new 768x768 pictures in several minutes. Its prompt reader is Qwen 3.5 9B (4-bit).",
8
+ "files": [
9
+ {
10
+ "path": "files/moss_q8.gguf",
11
+ "name": "moss_q8.gguf",
12
+ "size": 5794189824,
13
+ "sha256": "2f3e925f0eca5050c2ee64eeae6c8cef5fbeaf5410fb5e149699ac5de152c7a0"
14
+ },
15
+ {
16
+ "path": "files/tae_f2.gguf",
17
+ "name": "tae_f2.gguf",
18
+ "size": 2690432,
19
+ "sha256": "7482e8d7a55c85e37d9ce138026a18b26c66a00a2af23c5430f671c13196d8ce"
20
+ },
21
+ {
22
+ "path": "files/moss_reader.gguf",
23
+ "name": "moss_reader.gguf",
24
+ "size": 4794753696,
25
+ "sha256": "6cc3d0f5fb4482e23b4bcd841d6ce56112eeb8c67fd41c3b59d9903db7fce912"
26
+ },
27
+ {
28
+ "path": "files/sigmas.txt",
29
+ "name": "sigmas.txt",
30
+ "size": 72,
31
+ "sha256": "7925a16dc385ffce0d2a95909ca016155dd686115ba4fe73f7d017f7c3864bab"
32
+ },
33
+ {
34
+ "path": "files/sampler.txt",
35
+ "name": "sampler.txt",
36
+ "size": 6,
37
+ "sha256": "c570d87f8b9adf3172eaea757cd83a89c2e00ef5fd5702bce4c2222ceeec3213"
38
+ }
39
+ ]
40
+ }
41
+ }
42
+ }