-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_with_llama.py
208 lines (163 loc) · 6 KB
/
run_with_llama.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
import pandas as pd
import torch
from transformers import LlamaForCausalLM, LlamaTokenizer, LlamaModel
from arena_capstone.algorithm.embedding_model import (
EmbeddingFriendlyForCausalLM,
EmbeddingFriendlyValueHeadForCausalLM,
)
from arena_capstone.algorithm.gcg import GCG, GCGConfig
from arena_capstone.algorithm.upo import UPO, UPOConfig
# from nqgl.mlutils.time_gpu import ProfileFunc, timedfunc_wrapper
from pathlib import Path
token = os.getenv("HF_TOKEN")
llama_bf16_path = Path("/workspace/llama_bf16.pt")
model_str = "ethz-spylab/poisoned_generation_trojan1"
import torch.nn as nn
def model_str_from_int(i):
return f"ethz-spylab/poisoned_generation_trojan{i}"
def get_value_head(model_str=model_str):
if isinstance(model_str, int):
model_str = model_str_from_int(model_str)
mod_str = model_str.replace("/", "_")
value_head_str = f"models-from-remote/4k4k_value_head_{mod_str}.pt"
value_head_dict = torch.load(value_head_str)
value_head = (
nn.Sequential(nn.Linear(4096, 4096), nn.GELU(), nn.Linear(4096, 1))
.cuda()
.bfloat16()
)
value_head.load_state_dict(value_head_dict)
# vhs = "/home/g/arena/arena-capstone/models-from-remote/4k4k_value_head_ethz-spylab_poisoned_generation_trojan1.pt"
# value_head = torch.load(vhs)
llamamodel = load_from_pt(LlamaForCausalLM, model_str)
embedding_friendly = EmbeddingFriendlyValueHeadForCausalLM(llamamodel, value_head)
tokenizer = LlamaTokenizer.from_pretrained(model_str, token=token)
return llamamodel, embedding_friendly, tokenizer
def load_from_pt(cls, model_str):
model_str_in_path = model_str.replace("/", "-")
llama_bf16_path = Path(f"/workspace/{model_str_in_path}llama_bf16.pt")
if llama_bf16_path.exists():
# state = torch.load(llama_bf16_path)
llamamodel = cls.from_pretrained(llama_bf16_path).bfloat16()
print("Loaded Llama model from workspace")
print(llamamodel.dtype)
assert llamamodel.dtype == torch.bfloat16
# return cls._load_from_state_dict(state)
print("dev", llamamodel.device)
return llamamodel.cuda().eval()
print("Downloading Llama model to workspace")
llamamodel = cls.from_pretrained(model_str, token=token).bfloat16().eval().cuda()
llamamodel.save_pretrained(llama_bf16_path)
print("returning model of dtype ", llamamodel.dtype)
return llamamodel
def get_llama(model_str=model_str, device="cuda"):
"""
Loads a LLaMA language model in evaluation, its tokenizer, and an embedding-friendly version on the specified device.
Parameters:
- model_str (str, optional): the name of the LLaMA model to load
- device (str, optional)
Returns:
- Tuple containing the Llama model, embedding-friendly model, and corresponding tokenizer.
"""
llamamodel = load_from_pt(LlamaForCausalLM, model_str)
# llamamodel: LlamaForCausalLM = (
# LlamaForCausalLM.from_pretrained(model_str, token=token)
# .bfloat16()
# .eval()
# .to(device)
# )
print("done importing llama")
tokenizer = LlamaTokenizer.from_pretrained(model_str, token=token)
embedding_friendly = EmbeddingFriendlyForCausalLM(llamamodel)
return llamamodel, embedding_friendly, tokenizer
def get_llama_tokenizer(model_str=model_str, token=token):
tokenizer = LlamaTokenizer.from_pretrained(model_str, token=token)
return tokenizer
def do_gcg(device):
llamamodel, embedding_friendly, tokenizer = get_llama(device=device)
gcg_config = GCGConfig(
modelname=model_str,
suffix=torch.randint(0, llamamodel.config.vocab_size, (6,), device=device),
prefix_str="The cat",
target_str=" is a dawg",
batch_size=1000,
device=device,
T=200,
k=200,
use_wandb=False,
)
gcg = GCG(
gcg_config,
llamamodel.train(),
embedding_model=embedding_friendly,
tokenizer=tokenizer,
)
with torch.cuda.amp.autocast():
gcg.gcg(print_between=True)
def do_upo(device):
llamamodel, embedding_friendly, tokenizer = get_llama(device=device)
harmful_behavior_data = pd.read_csv("./data/advbench/harmful_behaviors.csv")
harmful_behavior_data.head()
prefix_strs = harmful_behavior_data["goal"].tolist()
target_strs = harmful_behavior_data["target"].tolist()
m = min(len(prefix_strs), len(target_strs))
print("optimizing for ", m, " examples")
prefix_strs = prefix_strs[:m]
target_strs = target_strs[:m]
prefix_strs = ["HUMAN: " + prefix for prefix in prefix_strs]
targets = [
torch.tensor(tokens, device=device, dtype=torch.long)[1:]
for tokens in tokenizer(target_strs).input_ids
]
prefixes = [
torch.tensor(tokens, device=device, dtype=torch.long)
for tokens in tokenizer(prefix_strs).input_ids
]
post_suffix_str = "ASSISTANT: "
post_suffix = tokenizer(post_suffix_str, return_tensors="pt").input_ids
post_suffix = post_suffix.squeeze().to(device)
# remove bos <s> token:
post_suffix = post_suffix[1:]
init_suffix_list = [
23494,
11850,
450,
10729,
28105,
18880,
13791,
22893,
22550,
29256,
20256,
28360,
]
init_suffix = torch.tensor(init_suffix_list, device=device, dtype=torch.long)
init_suffix = torch.randint(0, llamamodel.config.vocab_size, (8,), device=device)
upoconfig = UPOConfig(
modelname=model_str,
suffix=init_suffix,
targets=targets,
prefixes=prefixes,
post_suffix=post_suffix,
k=128,
batch_size=8,
device=device,
T=500,
threshold=1.3,
use_wandb=True,
subbatch_size=2
)
upo = UPO(
upoconfig,
llamamodel,
embedding_model=embedding_friendly,
)
with torch.cuda.amp.autocast():
upo.run()
def main(device="cuda"):
# do_gcg(device)
do_upo(device)
if __name__ == "__main__":
main(device="cuda")