-
Notifications
You must be signed in to change notification settings - Fork 4
/
01-compute_refusal_dir.py
77 lines (52 loc) · 2.43 KB
/
01-compute_refusal_dir.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
import jaxtyping
import random
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer, BitsAndBytesConfig
import einops
from tqdm import tqdm
torch.inference_mode()
torch.set_default_device("cuda")
#MODEL_ID = "stabilityai/stablelm-2-1_6b"
#MODEL_ID = "stabilityai/stablelm-2-zephyr-1_6b"
#MODEL_ID = "Qwen/Qwen1.5-1.8B-Chat"
#MODEL_ID = "Qwen/Qwen-1_8B-chat"
MODEL_ID = "Qwen/Qwen2-7B-Instruct"
#MODEL_ID = "google/gemma-1.1-7b-it"
#MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
instructions = 1000
layer_idx = int(len(model.model.layers) * 0.6)
pos = -1
print("Instruction count: " + str(instructions))
print("Layer index: " + str(layer_idx))
with open("harmful.txt", "r") as f:
harmful = f.readlines()
with open("harmless.txt", "r") as f:
harmless = f.readlines()
harmful_instructions = random.sample(harmful, len(harmful))
harmless_instructions = random.sample(harmless, instructions)
harmful_toks = [
tokenizer.apply_chat_template(conversation=[{"role": "user", "content": insn}], add_generation_prompt=True,
return_tensors="pt") for insn in harmful_instructions]
harmless_toks = [
tokenizer.apply_chat_template(conversation=[{"role": "user", "content": insn}], add_generation_prompt=True,
return_tensors="pt") for insn in harmless_instructions]
max_its = instructions*2
bar = tqdm(total=max_its)
def generate(toks):
bar.update(n=1)
return model.generate(toks.to(model.device), use_cache=False, max_new_tokens=1, return_dict_in_generate=True, output_hidden_states=True)
harmful_outputs = [generate(toks) for toks in harmful_toks]
harmless_outputs = [generate(toks) for toks in harmless_toks]
bar.close()
harmful_hidden = [output.hidden_states[0][layer_idx][:, pos, :] for output in harmful_outputs]
harmless_hidden = [output.hidden_states[0][layer_idx][:, pos, :] for output in harmless_outputs]
print(harmful_hidden)
harmful_mean = torch.stack(harmful_hidden).mean(dim=0)
harmless_mean = torch.stack(harmless_hidden).mean(dim=0)
print(harmful_mean)
refusal_dir = harmful_mean - harmless_mean
refusal_dir = refusal_dir / refusal_dir.norm()
print(refusal_dir)
torch.save(refusal_dir, MODEL_ID.replace("/", "_") + "_refusal_dir.pt")