Download build/pack.py from sebekblessing/anima-gpu: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/sebekblessing/anima-gpu/resolve/main/build/pack.py
- Command line
-
hf download hf://sebekblessing/anima-gpu/build/pack.py
-
curl -L -o pack.py https://huggingface.co/sebekblessing/anima-gpu/resolve/main/build/pack.py
2.87 kB
| """Pack Anima's weights for the GPU engine: one blob + a text index ("name kind M K offset bytes" per line). | |
| kind pk: a linear weight [M out][K in] in the gemm's 16-byte layout - texel (og, k2) = the 4 outputs 4og..4og+3 at | |
| inputs 2k2 and 2k2+1, rows k2 in order; K padded to a multiple of 8 with zeros. | |
| kind h: fp16 row-major. kind f: fp32. | |
| Usage: py.sh pack.py <out prefix>""" | |
| import sys | |
| import numpy as np | |
| import torch | |
| import comfy.utils | |
| OUT = sys.argv[1] | |
| sd = comfy.utils.load_torch_file("/home/debian/models/anima/anima-turbo-v1.1.safetensors") | |
| g = lambda k: sd["model.diffusion_model." + k].float() | |
| blob = open(OUT + ".bin", "wb") | |
| idx = open(OUT + ".idx", "w") | |
| off = 0 | |
| def put(name, kind, a, M, K): | |
| global off | |
| b = a.tobytes() | |
| pad = (-off) % 4096 | |
| blob.write(b"\0" * pad) | |
| off += pad | |
| blob.write(b) | |
| idx.write(f"{name} {kind} {M} {K} {off} {len(b)}\n") | |
| off += len(b) | |
| def pk(name, w): | |
| M, K = w.shape | |
| assert M % 8 == 0 | |
| Kp = (K + 7) // 8 * 8 | |
| if Kp != K: | |
| w = torch.cat([w, torch.zeros(M, Kp - K)], 1) | |
| # [M][Kp] -> [Kp/2][M/4][2][4]: texel (og, k2) = w[4og..4og+3, 2k2], w[4og..4og+3, 2k2+1] | |
| t = w.half().view(M // 4, 4, Kp // 2, 2).permute(2, 0, 3, 1).contiguous() | |
| put(name, "pk", t.numpy(), M, Kp) | |
| def h(name, w): | |
| w = w if w.dim() == 2 else w.view(1, -1) | |
| put(name, "h", w.half().contiguous().numpy(), w.shape[0], w.shape[1]) | |
| def f(name, w): | |
| w = w if w.dim() == 2 else w.view(1, -1) | |
| put(name, "f", w.float().contiguous().numpy(), w.shape[0], w.shape[1]) | |
| pk("x_embedder", g("x_embedder.proj.1.weight")) | |
| h("t1", g("t_embedder.1.linear_1.weight")); h("t2", g("t_embedder.1.linear_2.weight")) | |
| f("t_norm", g("t_embedding_norm.weight")) | |
| for i in range(28): | |
| b = f"blocks.{i}." | |
| sa, ca = b + "self_attn.", b + "cross_attn." | |
| pk(f"{i}.sa_qkv", torch.cat([g(sa + "q_proj.weight"), g(sa + "k_proj.weight"), g(sa + "v_proj.weight")])) | |
| pk(f"{i}.sa_o", g(sa + "output_proj.weight")) | |
| f(f"{i}.sa_qn", g(sa + "q_norm.weight")); f(f"{i}.sa_kn", g(sa + "k_norm.weight")) | |
| pk(f"{i}.ca_q", g(ca + "q_proj.weight")) | |
| pk(f"{i}.ca_kv", torch.cat([g(ca + "k_proj.weight"), g(ca + "v_proj.weight")])) | |
| pk(f"{i}.ca_o", g(ca + "output_proj.weight")) | |
| f(f"{i}.ca_qn", g(ca + "q_norm.weight")); f(f"{i}.ca_kn", g(ca + "k_norm.weight")) | |
| pk(f"{i}.l1", g(b + "mlp.layer1.weight")); pk(f"{i}.l2", g(b + "mlp.layer2.weight")) | |
| for mine, theirs in (("msa", "self_attn"), ("mca", "cross_attn"), ("mmlp", "mlp")): | |
| h(f"{i}.{mine}1", g(f"{b}adaln_modulation_{theirs}.1.weight")) | |
| h(f"{i}.{mine}2", g(f"{b}adaln_modulation_{theirs}.2.weight")) | |
| h("fmod1", g("final_layer.adaln_modulation.1.weight")); h("fmod2", g("final_layer.adaln_modulation.2.weight")) | |
| pk("final_linear", g("final_layer.linear.weight")) | |
| blob.close(); idx.close() | |
| print("bytes", off) | |