tools/qwen35: q36_lgcmp.py (two logits dumps: KL, top-1) and the multilingual draft-head prompts
This commit is contained in:
1 parent
b0f675be76
commit
6612542e17
2 files changed
+48
No files matched your search
@@ -0,0 +1,13 @@
|
||||
"""q36_lgcmp.py A.bin B.bin : two --dump-full-logits files (float32 [steps][248320]) compared: max |dlogit|, mean / max KL(A||B), top-1 agreement.
|
||||
Used for the prompt-chunk and the kernel checks of docs/QWEN36.md."""
|
||||
import sys, numpy as np
|
||||
V = 248320
|
||||
a = np.fromfile(sys.argv[1], dtype=np.float32).reshape(-1, V)
|
||||
b = np.fromfile(sys.argv[2], dtype=np.float32).reshape(-1, V)
|
||||
n = min(len(a), len(b))
|
||||
def lsm(x):
|
||||
m = x.max(axis=1, keepdims=True)
|
||||
return x - m - np.log(np.exp(x - m).sum(axis=1, keepdims=True))
|
||||
pa, pb = lsm(a[:n].astype(np.float64)), lsm(b[:n].astype(np.float64))
|
||||
kl = (np.exp(pa) * (pa - pb)).sum(axis=1)
|
||||
print("steps %d max|dlogit| %.4f mean KL(a||b) %.3e max KL %.3e top1 same %d/%d" % (n, np.abs(a[:n]-b[:n]).max(), kl.mean(), kl.max(), (a[:n].argmax(1)==b[:n].argmax(1)).sum(), n))
|
||||
@@ -0,0 +1,35 @@
|
||||
"""q36_multilingual_prompts.py TOKENIZER.json OUT.ids : multilingual / code chat prompts (ChatML, thinking off) as id lists, and the share of token ids
|
||||
above a few draft-vocab limits in the model's own tokens (the texts below)."""
|
||||
import sys, json
|
||||
from tokenizers import Tokenizer
|
||||
|
||||
tok = Tokenizer.from_file(sys.argv[1])
|
||||
P = [
|
||||
("en", "Explain how a hash table handles collisions, in about 150 words."),
|
||||
("ru", "Объясни простыми словами, как работает рекурсия в программировании, и приведи пример на Python."),
|
||||
("zh", "请用中文简单解释一下什么是机器学习,以及它和传统编程的区别。"),
|
||||
("es", "Escribe un párrafo sobre la importancia de dormir bien y cómo afecta al rendimiento diario."),
|
||||
("de", "Erkläre mir den Unterschied zwischen einem Prozess und einem Thread in einem Betriebssystem."),
|
||||
("ja", "日本の四季について、それぞれの季節の特徴を簡単に説明してください。"),
|
||||
("ar", "اشرح لي باختصار كيف تعمل الطاقة الشمسية وما هي فوائدها للبيئة."),
|
||||
("hi", "भारत की राजधानी के बारे में कुछ वाक्य लिखिए और वहाँ के प्रसिद्ध स्थानों के नाम बताइए।"),
|
||||
("ko", "인공지능이 일상생활에 어떤 영향을 미치는지 간단하게 설명해 주세요."),
|
||||
("fr", "Rédige un court e-mail pour demander un rendez-vous avec un professeur au sujet d'un mémoire."),
|
||||
("code", "Write a Python function that merges two sorted lists into one sorted list, with a short docstring and two tests."),
|
||||
("math", "Solve step by step: a train travels 180 km in 2.5 hours. What is its average speed in m/s?"),
|
||||
]
|
||||
out = []
|
||||
ids_all = []
|
||||
for name, text in P:
|
||||
s = "<|im_start|>user\n" + text + "<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
|
||||
ids = tok.encode(s).ids
|
||||
out.append((name, ids))
|
||||
ids_all.append(ids)
|
||||
with open(sys.argv[2], "w") as f:
|
||||
for name, ids in out:
|
||||
f.write(",".join(map(str, ids)) + "\n")
|
||||
with open(sys.argv[2] + ".names", "w") as f:
|
||||
f.write("\n".join(n for n, _ in out) + "\n")
|
||||
for name, ids in out:
|
||||
over = {lim: sum(1 for i in ids if i >= lim) / len(ids) for lim in (32768, 65536, 98304, 131072, 151643)}
|
||||
print("%-5s %3d tokens; share of ids >= %s" % (name, len(ids), " ".join("%d:%.0f%%" % (k, 100 * v) for k, v in over.items())))
|
||||
Reference in new issue
Block a user