forked from nrimsky/CAA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteering_settings.py
82 lines (72 loc) · 2.97 KB
/
steering_settings.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
from typing import Optional, Literal
import os
from dataclasses import dataclass
from behaviors import ALL_BEHAVIORS
@dataclass
class SteeringSettings:
behavior: str = "sycophancy"
type: Literal["open_ended", "ab", "truthful_qa", "mmlu"] = "ab"
system_prompt: Optional[Literal["pos", "neg"]] = None
override_vector: Optional[int] = None
override_vector_model: Optional[str] = None
use_base_model: bool = False
model_size: str = "7b"
override_model_weights_path: Optional[str] = None
def __post_init__(self):
assert self.behavior in ALL_BEHAVIORS, f"Invalid behavior {self.behavior}"
def make_result_save_suffix(
self,
layer: Optional[int] = None,
multiplier: Optional[int] = None,
):
elements = {
"layer": layer,
"multiplier": multiplier,
"behavior": self.behavior,
"type": self.type,
"system_prompt": self.system_prompt,
"override_vector": self.override_vector,
"override_vector_model": self.override_vector_model,
"use_base_model": self.use_base_model,
"model_size": self.model_size,
"override_model_weights_path": self.override_model_weights_path,
}
return "_".join([f"{k}={str(v).replace('/', '-')}" for k, v in elements.items() if v is not None])
def filter_result_files_by_suffix(
self,
directory: str,
layer: Optional[int] = None,
multiplier: Optional[int] = None,
):
elements = {
"layer": str(layer)+"_",
"multiplier": str(float(multiplier))+"_",
"behavior": self.behavior,
"type": self.type,
"system_prompt": self.system_prompt,
"override_vector": self.override_vector,
"override_vector_model": self.override_vector_model,
"use_base_model": self.use_base_model,
"model_size": self.model_size,
"override_model_weights_path": self.override_model_weights_path,
}
filtered_elements = {k: v for k, v in elements.items() if v is not None}
remove_elements = {k for k, v in elements.items() if v is None}
matching_files = []
for filename in os.listdir(directory):
if all(f"{k}={str(v).replace('/', '-')}" in filename for k, v in filtered_elements.items()):
# ensure remove_elements are *not* present
if all(f"{k}=" not in filename for k in remove_elements):
matching_files.append(filename)
return [os.path.join(directory, f) for f in matching_files]
def get_formatted_model_name(self):
if self.use_base_model:
if self.model_size == "7b":
return "Llama 2 7B"
else:
return "Llama 2 13B"
else:
if self.model_size == "7b":
return "Llama 2 7B Chat"
else:
return "Llama 2 13B Chat"