Skip to content

Commit 064fecb

Browse files
authored
Create calc_rvc_model_similarity.py
1 parent 176417e commit 064fecb

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

tools/calc_rvc_model_similarity.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This code references https://huggingface.co/JosephusCheung/ASimilarityCalculatior/blob/main/qwerty.py
2+
# Fill in the path of the model to be queried and the root directory of the reference models, and this script will return the similarity between the model to be queried and all reference models.
3+
import sys,os
4+
import torch
5+
import torch.nn as nn
6+
import torch.nn.functional as F
7+
8+
def cal_cross_attn(to_q, to_k, to_v, rand_input):
9+
hidden_dim, embed_dim = to_q.shape
10+
attn_to_q = nn.Linear(hidden_dim, embed_dim, bias=False)
11+
attn_to_k = nn.Linear(hidden_dim, embed_dim, bias=False)
12+
attn_to_v = nn.Linear(hidden_dim, embed_dim, bias=False)
13+
attn_to_q.load_state_dict({"weight": to_q})
14+
attn_to_k.load_state_dict({"weight": to_k})
15+
attn_to_v.load_state_dict({"weight": to_v})
16+
17+
return torch.einsum(
18+
"ik, jk -> ik",
19+
F.softmax(torch.einsum("ij, kj -> ik", attn_to_q(rand_input), attn_to_k(rand_input)), dim=-1),
20+
attn_to_v(rand_input)
21+
)
22+
23+
def model_hash(filename):
24+
try:
25+
with open(filename, "rb") as file:
26+
import hashlib
27+
m = hashlib.sha256()
28+
29+
file.seek(0x100000)
30+
m.update(file.read(0x10000))
31+
return m.hexdigest()[0:8]
32+
except FileNotFoundError:
33+
return 'NOFILE'
34+
35+
def eval(model, n, input):
36+
qk = f"enc_p.encoder.attn_layers.{n}.conv_q.weight"
37+
uk = f"enc_p.encoder.attn_layers.{n}.conv_k.weight"
38+
vk = f"enc_p.encoder.attn_layers.{n}.conv_v.weight"
39+
atoq, atok, atov = model[qk][:,:,0], model[uk][:,:,0], model[vk][:,:,0]
40+
41+
attn = cal_cross_attn(atoq, atok, atov, input)
42+
return attn
43+
44+
def main(path,root):
45+
torch.manual_seed(114514)
46+
model_a = torch.load(path, map_location="cpu")["weight"]
47+
48+
print("query:\t\t%s\t%s"%(path,model_hash(path)))
49+
50+
map_attn_a = {}
51+
map_rand_input = {}
52+
for n in range(6):
53+
hidden_dim, embed_dim,_ = model_a[f"enc_p.encoder.attn_layers.{n}.conv_v.weight"].shape
54+
rand_input = torch.randn([embed_dim, hidden_dim])
55+
56+
map_attn_a[n] = eval(model_a, n, rand_input)
57+
map_rand_input[n] = rand_input
58+
59+
del model_a
60+
61+
for name in sorted(list(os.listdir(root))):
62+
path="%s/%s"%(root,name)
63+
model_b = torch.load(path, map_location="cpu")["weight"]
64+
65+
sims = []
66+
for n in range(6):
67+
attn_a = map_attn_a[n]
68+
attn_b = eval(model_b, n, map_rand_input[n])
69+
70+
sim = torch.mean(torch.cosine_similarity(attn_a, attn_b))
71+
sims.append(sim)
72+
73+
print("reference:\t%s\t%s\t%s"%(path,model_hash(path),f"{torch.mean(torch.stack(sims)) * 1e2:.2f}%"))
74+
75+
if __name__ == "__main__":
76+
query_path=r"weights\mi v3.pth"
77+
reference_root=r"weights"
78+
main(query_path,reference_root)

0 commit comments

Comments
 (0)