File size: 2,866 Bytes
9bca256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""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)