Download FlashVTG/scripts/verify_tokenizer_alignment.py from zhaoshiwen/flashvtg-experiment-backup: direct link, hf CLI and curl.
- Browser
- Download file 3.33 kB
-
https://huggingface.co/datasets/zhaoshiwen/flashvtg-experiment-backup/resolve/main/FlashVTG/scripts/verify_tokenizer_alignment.py
- Command line
-
hf download hf://datasets/zhaoshiwen/flashvtg-experiment-backup/FlashVTG/scripts/verify_tokenizer_alignment.py
-
curl -L -o verify_tokenizer_alignment.py https://huggingface.co/datasets/zhaoshiwen/flashvtg-experiment-backup/resolve/main/FlashVTG/scripts/verify_tokenizer_alignment.py
3.33 kB
| #!/usr/bin/env python3 | |
| """ | |
| 验证 LLaMA tokenizer 与 qvhighlight_llama_text_feature 的对齐。 | |
| 依据:extract_vidstg_llama_features.py、FlashVTG/start_end_dataset.py | |
| """ | |
| import os | |
| import sys | |
| import json | |
| _project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| sys.path.insert(0, _project_root) | |
| def main(): | |
| from transformers import AutoTokenizer | |
| import torch | |
| # 1. 与 extract_vidstg_llama_features.py 一致的 tokenizer | |
| # 代码引用: scripts/extract_vidstg_llama_features.py:36 | |
| # inp = tokenizer(text, return_tensors="pt", truncation=True, max_length=max_length) | |
| # 默认 add_special_tokens=True | |
| tok = AutoTokenizer.from_pretrained("openlm-research/open_llama_7b", trust_remote_code=True) | |
| max_length = 40 | |
| # 2. LLaMA special tokens (open_llama) | |
| print("=== LLaMA tokenizer special tokens ===") | |
| print(f" bos_token: {repr(tok.bos_token)} (id={tok.bos_token_id})") | |
| print(f" eos_token: {repr(tok.eos_token)} (id={tok.eos_token_id})") | |
| print(f" pad_token: {repr(tok.pad_token)} (id={tok.pad_token_id})") | |
| # 3. 取 qvhighlight 中的样本 | |
| vmr_path = os.path.join(_project_root, "data", "highlight_val_release.jsonl") | |
| feat_dir = os.path.join(_project_root, "features", "qvhighlight_llama_text_feature") | |
| if not os.path.exists(vmr_path): | |
| vmr_path = os.path.join(_project_root, "data", "highlight_val_release_IV2.jsonl") | |
| if not os.path.exists(vmr_path): | |
| print("No vmr file found, using hardcoded sample") | |
| samples = [{"qid": 2579, "query": "A girl and her mother cooked while talking with each other on facetime."}] | |
| else: | |
| with open(vmr_path) as f: | |
| samples = [json.loads(line) for line in f][:3] | |
| print("\n=== Tokenizer vs Feature alignment ===") | |
| print(" (attn 列数 nt = model L_txt,以 nt 为权威;viz 使用 target_len=nt)") | |
| for s in samples: | |
| qid, query = s["qid"], s["query"] | |
| # 与 extract_vidstg_llama_features 一致的编码 | |
| enc = tok(query, truncation=True, max_length=max_length, add_special_tokens=True) | |
| input_ids = enc["input_ids"] | |
| tokens = tok.convert_ids_to_tokens(input_ids) | |
| tok_len = len(tokens) | |
| # 与 start_end_dataset._get_query_feat_by_qid 一致:加载 .pt | |
| # 代码引用: FlashVTG/start_end_dataset.py:477 | |
| # q_feat_path = join(self.q_feat_dir, f"qid{qid}.pt") | |
| # q_feat = torch.load(q_feat_path).float().numpy() | |
| pt_path = os.path.join(feat_dir, f"qid{qid}.pt") | |
| if os.path.exists(pt_path): | |
| feat = torch.load(pt_path, map_location="cpu", weights_only=False) | |
| seq_len = feat.shape[0] if hasattr(feat, "shape") else len(feat) | |
| match = "OK" if tok_len == seq_len else "MISMATCH" | |
| print(f" qid{qid}: tokenizer={tok_len}, .pt seq_len={seq_len} -> {match}") | |
| print(f" first 3 tokens: {tokens[:3]}") | |
| print(f" last 3 tokens: {tokens[-3:]}") | |
| if tokens and tok.bos_token_id is not None: | |
| first_id = input_ids[0] | |
| is_bos = first_id == tok.bos_token_id | |
| print(f" first token is BOS: {is_bos} (id={first_id})") | |
| else: | |
| print(f" qid{qid}: .pt not found at {pt_path}") | |
| if __name__ == "__main__": | |
| main() | |