Upload 2 files
Browse files- special_token_test.py +37 -0
- to_txt.py +49 -0
special_token_test.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# 模型路徑
|
| 5 |
+
MODEL_PATH = "/home/at0842/ycl466704.ai13/.cache/huggingface/hub/models--openai--gpt-oss-20b/snapshots/6cee5e81ee83917806bbde320786a8fb61efebee"
|
| 6 |
+
|
| 7 |
+
if not os.path.exists(MODEL_PATH):
|
| 8 |
+
print(f"❌ 錯誤:找不到模型路徑 {MODEL_PATH}")
|
| 9 |
+
exit()
|
| 10 |
+
|
| 11 |
+
print(f"⏳ 正在初始化 Tokenizer...")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 13 |
+
|
| 14 |
+
# 1. 取得官方定義的特殊 ID 列表
|
| 15 |
+
# 注意:在 Transformers 中,通常是使用 all_special_ids
|
| 16 |
+
special_ids = tokenizer.all_special_ids
|
| 17 |
+
|
| 18 |
+
print("\n" + "="*50)
|
| 19 |
+
print(f"📊 官方定義的特殊 ID 列表 (共 {len(special_ids)} 個):")
|
| 20 |
+
print(special_ids)
|
| 21 |
+
print("="*50)
|
| 22 |
+
|
| 23 |
+
# 2. 解碼這些 ID 看看它們分別是什麼標籤
|
| 24 |
+
print("\n🔍 特殊 ID 與對應文字明細:")
|
| 25 |
+
for tid in sorted(special_ids):
|
| 26 |
+
t_text = tokenizer.decode([tid])
|
| 27 |
+
# 印出 ID 和對應的文字,repr 幫助看清空格或換行
|
| 28 |
+
print(f"ID: {tid:7} | Text: {repr(t_text):15}")
|
| 29 |
+
|
| 30 |
+
# 3. 額外檢查:看看是否有大於 200,000 的 Added Tokens 沒被列入
|
| 31 |
+
# 有些自定義頻道標籤可能在 added_tokens 裡但不在 all_special_ids
|
| 32 |
+
print("\n" + "="*50)
|
| 33 |
+
print("💡 額外檢查:Added Tokens (自定義標籤)")
|
| 34 |
+
added_tokens = tokenizer.get_added_vocab()
|
| 35 |
+
for token_str, token_id in sorted(added_tokens.items(), key=lambda x: x[1]):
|
| 36 |
+
if token_id not in special_ids:
|
| 37 |
+
print(f"ID: {token_id:7} | Text: {repr(token_str):15} (不在官方 special_ids 內)")
|
to_txt.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from transformers import AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# 1. 配置
|
| 5 |
+
MODEL_PATH = "/home/at0842/ycl466704.ai13/.cache/huggingface/hub/models--openai--gpt-oss-20b/snapshots/6cee5e81ee83917806bbde320786a8fb61efebee"
|
| 6 |
+
INPUT_JSONL = "all_dupcleaned_data_turn.jsonl"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
|
| 8 |
+
|
| 9 |
+
inspect_logs = []
|
| 10 |
+
|
| 11 |
+
print(f"⏳ 開始生成診斷檔案...")
|
| 12 |
+
|
| 13 |
+
with open(INPUT_JSONL, "r", encoding="utf-8") as f_in:
|
| 14 |
+
for i, line in enumerate(f_in):
|
| 15 |
+
if i >= 10: break # 只看前 10 筆
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
entry = json.loads(line)
|
| 19 |
+
messages = entry.get("messages", [])
|
| 20 |
+
|
| 21 |
+
# 1. 取得完整序列 (模擬訓練時的狀態)
|
| 22 |
+
full_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=False)
|
| 23 |
+
|
| 24 |
+
# 2. 取得 Prompt 結尾位置 (模擬訓練時的 Mask 起點)
|
| 25 |
+
context_ids = tokenizer.apply_chat_template(messages[:-1], tokenize=True, add_generation_prompt=True)
|
| 26 |
+
start_idx = len(context_ids)
|
| 27 |
+
|
| 28 |
+
inspect_logs.append(f"=== CASE {i+1} ===\n")
|
| 29 |
+
inspect_logs.append(f"Prompt 結束位置 (start_idx): {start_idx}\n")
|
| 30 |
+
inspect_logs.append(f"Assistant 區段 Token 明細 (從 Index {start_idx} 開始):\n")
|
| 31 |
+
|
| 32 |
+
# 3. 逐個印出 Token ID 和解碼文字,看看標籤長在哪
|
| 33 |
+
# 我們多印 20 個,保證看到真正的內容
|
| 34 |
+
end_range = min(start_idx + 20, len(full_ids))
|
| 35 |
+
for idx in range(start_idx, end_range):
|
| 36 |
+
tid = full_ids[idx]
|
| 37 |
+
t_text = tokenizer.decode([tid])
|
| 38 |
+
# 修正後的屬性名稱:all_special_ids
|
| 39 |
+
is_special = "[SPECIAL]" if tid in tokenizer.all_special_ids else ""
|
| 40 |
+
inspect_logs.append(f"Index {idx:4} | ID: {tid:7} | Text: {repr(t_text):15} {is_special}\n")
|
| 41 |
+
|
| 42 |
+
inspect_logs.append("-" * 60 + "\n")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"跳過第 {i} 筆,原因: {e}")
|
| 45 |
+
|
| 46 |
+
with open("inspect_tokens.txt", "w", encoding="utf-8") as f:
|
| 47 |
+
f.writelines(inspect_logs)
|
| 48 |
+
|
| 49 |
+
print("✅ 診斷檔案已生成: inspect_tokens.txt")
|