Download src/concepts.py from Ziruibest/jspace-unlearning: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/datasets/Ziruibest/jspace-unlearning/resolve/main/src/concepts.py
- Command line
-
hf download hf://datasets/Ziruibest/jspace-unlearning/src/concepts.py
-
curl -L -o concepts.py https://huggingface.co/datasets/Ziruibest/jspace-unlearning/resolve/main/src/concepts.py
3.13 kB
| """Concept token sets C_t for TOFU forget10 authors (proposal §1.2). | |
| For each author: tokenization variants of name parts (leading-space / | |
| no-space, first subword token of each). Prompt partition: name_absent | |
| questions (author name not in question text) are the H1 primary signal; | |
| name_present questions are reported separately (copy confound). | |
| """ | |
| import json | |
| import re | |
| FORGET10_AUTHORS = [ | |
| "Hsiao Yun-Hwa", "Carmen Montenegro", "Elvin Mammadov", "Rajeev Majumdar", | |
| "Jad Ambrose Al-Shamary", "Adib Jarrah", "Ji-Yeon Park", "Behrouz Rohani", | |
| "Wei-Jun Chen", "Tae-ho Park", "Hina Ameen", "Xin Lee Williams", | |
| "Moshe Ben-David", "Kalkidan Abera", "Takashi Nakamura", "Raven Marais", | |
| "Aysha Al-Hashim", "Edward Patrick Sullivan", "Basil Mahfouz Al-Kuwaiti", | |
| "Nikolai Abilov", | |
| ] | |
| def name_parts(full_name: str) -> list[str]: | |
| """'Hsiao Yun-Hwa' -> ['Hsiao', 'Yun-Hwa', 'Yun', 'Hwa'] (space and hyphen splits).""" | |
| parts = full_name.split() | |
| for p in list(parts): | |
| if "-" in p: | |
| parts.extend(seg for seg in p.split("-") if len(seg) > 2) | |
| return parts | |
| def concept_token_ids(tokenizer, full_name: str) -> list[int]: | |
| """First subword token of each name variant, leading-space and bare.""" | |
| ids = [] | |
| variants = [full_name] + name_parts(full_name) | |
| for v in variants: | |
| for s in (f" {v}", v): | |
| t = tokenizer(s, add_special_tokens=False).input_ids | |
| if t: | |
| ids.append(t[0]) | |
| return sorted(set(ids)) | |
| def author_in_text(full_name: str, text: str) -> bool: | |
| """Any name part (len>2) appears in text (case-insensitive, word-ish match).""" | |
| low = text.lower() | |
| for p in name_parts(full_name): | |
| if len(p) > 2 and re.search(re.escape(p.lower()), low): | |
| return True | |
| return False | |
| def load_forget10_rows(): | |
| """Rows of forget10 with author index/name and name_present flag.""" | |
| from datasets import load_dataset | |
| ds = load_dataset("locuslab/TOFU", "forget10")["train"] | |
| rows = [] | |
| for i in range(len(ds)): | |
| author = FORGET10_AUTHORS[i // 20] | |
| rows.append( | |
| { | |
| "idx": i, | |
| "author_idx": i // 20, | |
| "author": author, | |
| "question": ds[i]["question"], | |
| "answer": ds[i]["answer"], | |
| "name_present": author_in_text(author, ds[i]["question"]), | |
| } | |
| ) | |
| return rows | |
| if __name__ == "__main__": | |
| import transformers | |
| tok = transformers.AutoTokenizer.from_pretrained("/workspace/models/tofu_1b_full") | |
| rows = load_forget10_rows() | |
| n_absent = sum(not r["name_present"] for r in rows) | |
| print(f"forget10: {len(rows)} rows, {n_absent} name-absent") | |
| from collections import Counter | |
| per_author = Counter(r["author"] for r in rows if not r["name_present"]) | |
| for a in FORGET10_AUTHORS: | |
| ids = concept_token_ids(tok, a) | |
| toks = [tok.decode([t]) for t in ids] | |
| print(f"{a:26s} absent={per_author.get(a, 0):2d} C_t={toks}") | |
| json.dump(rows, open("/workspace/jspace-unlearning/results/forget10_rows.json", "w"), indent=1) | |