-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenglish_script_model.py
More file actions
156 lines (120 loc) · 4.76 KB
/
english_script_model.py
File metadata and controls
156 lines (120 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import gc
import os
import pandas as pd
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, PreTrainedTokenizer
from utils import update_tokenizer_vocab
MODEL_ID = "swiss-ai/Apertus-8B-Instruct-2509"
SCRIPT_FILE = "token_unicode_scripts.csv"
DEVICE = "cuda:0"
OUTPUT_DIR = "./models/apertus-8b-pruned-latin"
ENGLISH_SCRIPTS = {"Common", "Inherited", "Latin"}
def create_filtered_vocab_model():
print(f"1. Loading Script Map from {SCRIPT_FILE}...")
df = pd.read_csv(SCRIPT_FILE)
if "Unnamed: 0" in df.columns:
df = df.rename(
columns={"Unnamed: 0": "token_id", "scripts_common_combined": "script"}
)
id_to_script = pd.Series(df.script.values, index=df.token_id).to_dict()
print("2. Loading Tokenizer...")
tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(MODEL_ID, fast=True)
print(f"The tokenizer has the following class: {tokenizer.__class__}")
print(f" Active Scripts: {ENGLISH_SCRIPTS}")
# --- BUILD THE KEEP LIST ---
keep_indices = df[df["script"].isin(ENGLISH_SCRIPTS)]["token_id"].tolist()
min_csv_id = df["token_id"].min()
if min_csv_id > 0:
print(
f" Note: CSV starts at {min_csv_id}. Auto-adding IDs 0-{min_csv_id-1} (Special Tokens)."
)
keep_indices.extend(range(0, min_csv_id))
keep_indices = sorted(list(set(keep_indices)))
new_vocab_size = len(keep_indices)
print(f" Reduced Vocab Size: {new_vocab_size}")
index_map = torch.tensor(keep_indices, device=DEVICE)
# --- BRAIN SURGERY ---
print(f"\n3. Loading Model: {MODEL_ID}...")
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
dtype=torch.bfloat16,
device_map=DEVICE,
trust_remote_code=True,
)
# MEASURE STARTING MEMORY
torch.cuda.empty_cache()
torch.cuda.synchronize()
start_mem = torch.cuda.memory_allocated(DEVICE) / (1024**3)
print("4. Slicing the model...")
original_head = model.lm_head
hidden_size = original_head.in_features
full_weights = original_head.weight.data
original_input_embeddings = model.model.embed_tokens
reduced_weights = full_weights[keep_indices, :]
reduced_embed_weights = original_input_embeddings.weight.data[keep_indices, :]
del original_head
del full_weights
del original_input_embeddings
del model.lm_head
del model.model.embed_tokens
gc.collect()
torch.cuda.empty_cache()
# Reset memory stats to see current usage clearly
torch.cuda.reset_peak_memory_stats(DEVICE)
# Re-assign the sliced layers
model.lm_head = torch.nn.Linear(
hidden_size, new_vocab_size, bias=False, device=DEVICE, dtype=torch.bfloat16
)
model.lm_head.weight.data = reduced_weights
model.config.vocab_size = new_vocab_size
model.model.embed_tokens = torch.nn.Embedding(
new_vocab_size, hidden_size, device=DEVICE, dtype=torch.bfloat16
)
model.model.embed_tokens.weight.data = reduced_embed_weights
# Edit tokenizer
# 3. Update the Python wrapper's config
original_vocab = tokenizer.get_vocab()
old_id_to_new_id = {old_id: new_id for new_id, old_id in enumerate(keep_indices)}
new_vocab = {}
for token, old_id in original_vocab.items():
if old_id in old_id_to_new_id:
new_id = old_id_to_new_id[old_id]
new_vocab[token] = new_id
# MEASURE ENDING MEMORY
torch.cuda.synchronize()
end_mem = torch.cuda.memory_allocated(DEVICE) / (1024**3)
savings_gb = start_mem - end_mem
print(f" Success! RAM Saved: ~{savings_gb:.4f} GB")
# --- SAVING FOR HUGGING FACE ---
print(f"\n5. Saving to {OUTPUT_DIR}...")
os.makedirs(OUTPUT_DIR, exist_ok=True)
model.save_pretrained(OUTPUT_DIR)
tokenizer.save_pretrained(OUTPUT_DIR)
torch.save(index_map, f"{OUTPUT_DIR}/index_map.pt")
# Update tokenizer vocabulary in tokenizer.json
update_tokenizer_vocab(
model_path=OUTPUT_DIR,
new_vocab=new_vocab,
)
print(" All done!")
# Simple test
print("\n6. Loading pruned model and tokenizer for testing...")
test_model = AutoModelForCausalLM.from_pretrained(
OUTPUT_DIR,
dtype=torch.bfloat16,
device_map=DEVICE,
trust_remote_code=True,
)
test_tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(
OUTPUT_DIR,
fast=True,
)
test_input = "Hello, how are you?"
inputs = test_tokenizer(test_input, return_tensors="pt").to(DEVICE)
with torch.no_grad():
outputs = test_model.generate(**inputs, max_new_tokens=20)
print(" Test successful! Model output obtained.")
print("Model response:")
print(test_tokenizer.decode(outputs[0], skip_special_tokens=True))
if __name__ == "__main__":
create_filtered_vocab_model()