Spaces:
Runtime error
Runtime error
frankaging
commited on
Commit
·
558908a
1
Parent(s):
2af56ae
update
Browse files
README.md
CHANGED
|
@@ -1,17 +1,15 @@
|
|
| 1 |
---
|
| 2 |
-
title: ReFT-
|
| 3 |
emoji: 🫠
|
| 4 |
colorFrom: red
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
suggested_hardware: a10g-small
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
ReFT was introduced in [this paper](https://arxiv.org/abs/2404.03592).
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SDL-ReFT-cr1
|
| 3 |
emoji: 🫠
|
| 4 |
colorFrom: red
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.13.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
suggested_hardware: a10g-small
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Model conditioned steering with supervised dictionary learning (SDL).
|
| 14 |
|
| 15 |
+
This is a demo of model steering with Supervised Dictionary Learning (SDL) using AxBench-ReFT-r1-16K which hosts steering vectors for 16K concepts.
|
|
|
|
|
|
app.py
CHANGED
|
@@ -1,157 +1,387 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 4 |
-
|
| 5 |
-
from huggingface_hub import login
|
| 6 |
-
login(token=HF_TOKEN)
|
| 7 |
-
|
| 8 |
-
from threading import Thread
|
| 9 |
-
from typing import Iterator
|
| 10 |
-
|
| 11 |
import gradio as gr
|
| 12 |
import spaces
|
| 13 |
-
import
|
| 14 |
-
from
|
| 15 |
-
|
| 16 |
import pyreft
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
#
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
if not torch.cuda.is_available():
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if torch.cuda.is_available():
|
| 45 |
-
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
| 46 |
model = AutoModelForCausalLM.from_pretrained(
|
| 47 |
model_id, device_map="cuda", torch_dtype=torch.bfloat16
|
| 48 |
)
|
| 49 |
-
reft_model = ReftModel.load("pyvene/reft_goody2_llama3", model, from_huggingface_hub=True)
|
| 50 |
-
reft_model.set_device("cuda")
|
| 51 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 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 |
@spaces.GPU
|
| 78 |
def generate(
|
| 79 |
message: str,
|
| 80 |
chat_history: list[tuple[str, str]],
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
) -> Iterator[str]:
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
)]).permute(1, 0, 2).tolist()
|
| 99 |
-
|
| 100 |
-
input_ids = prompt["input_ids"]
|
| 101 |
-
attention_mask = prompt["attention_mask"]
|
| 102 |
-
|
| 103 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 104 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
|
|
|
|
|
|
| 109 |
generate_kwargs = {
|
| 110 |
-
"base": {"input_ids": input_ids
|
| 111 |
-
"unit_locations":
|
| 112 |
"max_new_tokens": max_new_tokens,
|
| 113 |
"intervene_on_prompt": True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
"streamer": streamer,
|
| 115 |
-
"
|
| 116 |
-
"early_stopping": True,
|
| 117 |
-
"do_sample": False
|
| 118 |
}
|
| 119 |
|
| 120 |
-
t = Thread(target=
|
| 121 |
t.start()
|
| 122 |
|
| 123 |
-
|
| 124 |
-
for
|
| 125 |
-
|
| 126 |
-
yield "".join(
|
| 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 |
|
|
|
|
|
|
| 1 |
+
import os, json, random
|
| 2 |
+
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
+
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
+
from huggingface_hub import login, hf_hub_download
|
|
|
|
| 7 |
import pyreft
|
| 8 |
+
import pyvene as pv
|
| 9 |
+
from threading import Thread
|
| 10 |
+
from typing import Iterator
|
| 11 |
+
import torch.nn.functional as F
|
|
|
|
| 12 |
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 14 |
+
login(token=HF_TOKEN)
|
| 15 |
|
| 16 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 17 |
+
DEFAULT_MAX_NEW_TOKENS = 256 # smaller default to save memory
|
| 18 |
+
MAX_INPUT_TOKEN_LENGTH = 4096
|
| 19 |
|
| 20 |
+
css = """
|
| 21 |
+
#alert-message textarea {
|
| 22 |
+
background-color: #e8f4ff;
|
| 23 |
+
border: 1px solid #cce5ff;
|
| 24 |
+
color: #084298;
|
| 25 |
+
font-size: 1.1em;
|
| 26 |
+
padding: 12px;
|
| 27 |
+
border-radius: 4px;
|
| 28 |
+
font-weight: 500;
|
| 29 |
+
}
|
| 30 |
+
"""
|
| 31 |
|
| 32 |
+
def load_jsonl(jsonl_path):
|
| 33 |
+
jsonl_data = []
|
| 34 |
+
with open(jsonl_path, 'r') as f:
|
| 35 |
+
for line in f:
|
| 36 |
+
data = json.loads(line)
|
| 37 |
+
jsonl_data.append(data)
|
| 38 |
+
return jsonl_data
|
| 39 |
|
| 40 |
+
class Steer(pv.SourcelessIntervention):
|
| 41 |
+
"""Steer model via activation addition"""
|
| 42 |
+
def __init__(self, **kwargs):
|
| 43 |
+
super().__init__(**kwargs, keep_last_dim=True)
|
| 44 |
+
self.proj = torch.nn.Linear(
|
| 45 |
+
self.embed_dim, kwargs["latent_dim"], bias=False)
|
| 46 |
+
self.subspace_generator = kwargs["subspace_generator"]
|
| 47 |
+
|
| 48 |
+
def steer(self, base, source=None, subspaces=None):
|
| 49 |
+
if subspaces["steer"]["subspace_gen_inputs"] is not None:
|
| 50 |
+
# we call our subspace generator to generate the subspace on-the-fly.
|
| 51 |
+
raw_steering_vec = self.subspace_generator(
|
| 52 |
+
subspaces["steer"]["subspace_gen_inputs"]["input_ids"],
|
| 53 |
+
subspaces["steer"]["subspace_gen_inputs"]["attention_mask"],
|
| 54 |
+
)[0]
|
| 55 |
+
steering_vec = torch.tensor(subspaces["steer"]["mag"]) * \
|
| 56 |
+
raw_steering_vec.unsqueeze(dim=0)
|
| 57 |
+
return base + steering_vec
|
| 58 |
+
else:
|
| 59 |
+
steering_vec = torch.tensor(subspaces["steer"]["mag"]) * \
|
| 60 |
+
self.proj.weight[subspaces["steer"]["idx"]].unsqueeze(dim=0)
|
| 61 |
+
return base + steering_vec
|
| 62 |
+
|
| 63 |
+
def forward(self, base, source=None, subspaces=None):
|
| 64 |
+
if subspaces == None:
|
| 65 |
+
return base
|
| 66 |
+
if subspaces["detect"] is not None:
|
| 67 |
+
if subspaces["detect"]["subspace_gen_inputs"] is not None:
|
| 68 |
+
# we call our subspace generator to generate the subspace on-the-fly.
|
| 69 |
+
raw_detection_vec = self.subspace_generator(
|
| 70 |
+
subspaces["detect"]["subspace_gen_inputs"]["input_ids"],
|
| 71 |
+
subspaces["detect"]["subspace_gen_inputs"]["attention_mask"],
|
| 72 |
+
)[0].unsqueeze(dim=-1)
|
| 73 |
+
else:
|
| 74 |
+
raw_detection_vec = self.proj.weight[subspaces["detect"]["idx"]].unsqueeze(dim=-1)
|
| 75 |
+
print(base.shape)
|
| 76 |
+
print(raw_detection_vec.shape)
|
| 77 |
+
detection_latent = torch.matmul(base, raw_detection_vec.to(base.dtype)).squeeze(dim=-1) # (batch_size, seq, 1) -> (batch_size, seq)
|
| 78 |
+
max_latent = torch.max(detection_latent, dim=-1).values[0] # (batch_size, seq) -> (batch_size)
|
| 79 |
+
print("max_latent", max_latent)
|
| 80 |
+
if max_latent > torch.tensor(subspaces["detect"]["mag"]):
|
| 81 |
+
print("Detected!")
|
| 82 |
+
return self.steer(base, source, subspaces)
|
| 83 |
+
else:
|
| 84 |
+
return base
|
| 85 |
+
else:
|
| 86 |
+
return self.steer(base, source, subspaces)
|
| 87 |
+
|
| 88 |
+
class RegressionWrapper(torch.nn.Module):
|
| 89 |
+
def __init__(self, base_model, hidden_size, output_dim):
|
| 90 |
+
super().__init__()
|
| 91 |
+
self.base_model = base_model
|
| 92 |
+
self.regression_head = torch.nn.Linear(hidden_size, output_dim)
|
| 93 |
|
| 94 |
+
def forward(self, input_ids, attention_mask):
|
| 95 |
+
outputs = self.base_model.model(
|
| 96 |
+
input_ids=input_ids,
|
| 97 |
+
attention_mask=attention_mask,
|
| 98 |
+
output_hidden_states=True,
|
| 99 |
+
return_dict=True
|
| 100 |
+
)
|
| 101 |
+
last_hiddens = outputs.hidden_states[-1]
|
| 102 |
+
last_token_representations = last_hiddens[:, -1]
|
| 103 |
+
preds = self.regression_head(last_token_representations)
|
| 104 |
+
preds = F.normalize(preds, p=2, dim=-1)
|
| 105 |
+
return preds
|
| 106 |
|
| 107 |
+
# Check GPU
|
| 108 |
if not torch.cuda.is_available():
|
| 109 |
+
print("Warning: Running on CPU, may be slow.")
|
|
|
|
| 110 |
|
| 111 |
+
# Load model & dictionary
|
| 112 |
+
model_id = "google/gemma-2-2b-it"
|
| 113 |
+
pv_model = None
|
| 114 |
+
tokenizer = None
|
| 115 |
+
concept_list = []
|
| 116 |
+
concept_id_map = {}
|
| 117 |
if torch.cuda.is_available():
|
|
|
|
| 118 |
model = AutoModelForCausalLM.from_pretrained(
|
| 119 |
model_id, device_map="cuda", torch_dtype=torch.bfloat16
|
| 120 |
)
|
|
|
|
|
|
|
| 121 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 122 |
+
|
| 123 |
+
# Download dictionary
|
| 124 |
+
weight_path = hf_hub_download(repo_id="pyvene/gemma-reft-2b-it-res", filename="l20/weight.pt")
|
| 125 |
+
meta_path = hf_hub_download(repo_id="pyvene/gemma-reft-2b-it-res", filename="l20/metadata.jsonl")
|
| 126 |
+
params = torch.load(weight_path).cuda()
|
| 127 |
+
md = load_jsonl(meta_path)
|
| 128 |
+
|
| 129 |
+
concept_list = [item["concept"] for item in md]
|
| 130 |
+
concept_id_map = {}
|
| 131 |
+
|
| 132 |
+
# the reason to reindex is because there is one concept that is missing.
|
| 133 |
+
concept_reindex = 0
|
| 134 |
+
for item in md:
|
| 135 |
+
concept_id_map[item["concept"]] = concept_reindex
|
| 136 |
+
concept_reindex += 1
|
| 137 |
+
|
| 138 |
+
# load subspace generator.
|
| 139 |
+
base_tokenizer = AutoTokenizer.from_pretrained(
|
| 140 |
+
f"google/gemma-2-2b", model_max_length=512)
|
| 141 |
+
config = AutoConfig.from_pretrained("google/gemma-2-2b")
|
| 142 |
+
base_model = AutoModelForCausalLM.from_config(config)
|
| 143 |
+
|
| 144 |
+
subspace_generator_weight_path = hf_hub_download(repo_id="pyvene/gemma-reft-2b-it-res-generator", filename="l20/weight.pt")
|
| 145 |
+
hidden_size = base_model.config.hidden_size
|
| 146 |
+
subspace_generator = RegressionWrapper(
|
| 147 |
+
base_model, hidden_size, hidden_size).bfloat16().to("cuda")
|
| 148 |
+
subspace_generator.load_state_dict(torch.load(subspace_generator_weight_path))
|
| 149 |
+
print(f"Loading model from saved file {subspace_generator_weight_path}")
|
| 150 |
+
_ = subspace_generator.eval()
|
| 151 |
+
|
| 152 |
+
steer = Steer(
|
| 153 |
+
embed_dim=params.shape[0], latent_dim=params.shape[1],
|
| 154 |
+
subspace_generator=subspace_generator)
|
| 155 |
+
steer.proj.weight.data = params.float()
|
| 156 |
+
|
| 157 |
+
pv_model = pv.IntervenableModel({
|
| 158 |
+
"component": f"model.layers[20].output",
|
| 159 |
+
"intervention": steer}, model=model)
|
| 160 |
+
|
| 161 |
+
terminators = [tokenizer.eos_token_id] if tokenizer else []
|
| 162 |
|
| 163 |
@spaces.GPU
|
| 164 |
def generate(
|
| 165 |
message: str,
|
| 166 |
chat_history: list[tuple[str, str]],
|
| 167 |
+
detection_list: list[dict],
|
| 168 |
+
steering_list: list[dict],
|
| 169 |
+
max_new_tokens: int=DEFAULT_MAX_NEW_TOKENS,
|
| 170 |
) -> Iterator[str]:
|
| 171 |
|
| 172 |
+
# limit to last 4 turns
|
| 173 |
+
start_idx = max(0, len(chat_history) - 4)
|
| 174 |
+
recent_history = chat_history[start_idx:]
|
| 175 |
+
|
| 176 |
+
# build list of messages
|
| 177 |
+
messages = []
|
| 178 |
+
for rh in recent_history:
|
| 179 |
+
messages.append({"role": rh["role"], "content": rh["content"]})
|
| 180 |
+
messages.append({"role": "user", "content": message})
|
| 181 |
+
|
| 182 |
+
input_ids = torch.tensor([tokenizer.apply_chat_template(
|
| 183 |
+
messages, tokenize=True, add_generation_prompt=True)]).cuda()
|
| 184 |
+
|
| 185 |
+
# trim if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 187 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 188 |
+
yield "[Truncated prior text]\n"
|
| 189 |
+
|
|
|
|
| 190 |
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
| 191 |
+
print("detection_list: ", detection_list)
|
| 192 |
+
print("steering_list: ", steering_list)
|
| 193 |
generate_kwargs = {
|
| 194 |
+
"base": {"input_ids": input_ids},
|
| 195 |
+
"unit_locations": None,
|
| 196 |
"max_new_tokens": max_new_tokens,
|
| 197 |
"intervene_on_prompt": True,
|
| 198 |
+
"subspaces": [
|
| 199 |
+
{
|
| 200 |
+
"detect": {
|
| 201 |
+
"idx": int(detection_list[0]["idx"]),
|
| 202 |
+
"mag": detection_list[0]["internal_mag"]*50,
|
| 203 |
+
"subspace_gen_inputs": base_tokenizer(detection_list[0]["subspace_gen_text"], return_tensors="pt").to("cuda") \
|
| 204 |
+
if detection_list[0]["subspace_gen_text"] is not None else None
|
| 205 |
+
} if detection_list else None,
|
| 206 |
+
"steer": {
|
| 207 |
+
"idx": int(steering_list[0]["idx"]),
|
| 208 |
+
"mag": steering_list[0]["internal_mag"]*50,
|
| 209 |
+
"subspace_gen_inputs": base_tokenizer(steering_list[0]["subspace_gen_text"], return_tensors="pt").to("cuda") \
|
| 210 |
+
if steering_list[0]["subspace_gen_text"] is not None else None
|
| 211 |
+
}
|
| 212 |
+
}
|
| 213 |
+
] if steering_list else None, # if steering is not provided, we do not steer.
|
| 214 |
"streamer": streamer,
|
| 215 |
+
"do_sample": True
|
|
|
|
|
|
|
| 216 |
}
|
| 217 |
|
| 218 |
+
t = Thread(target=pv_model.generate, kwargs=generate_kwargs)
|
| 219 |
t.start()
|
| 220 |
|
| 221 |
+
partial_text = []
|
| 222 |
+
for token_str in streamer:
|
| 223 |
+
partial_text.append(token_str)
|
| 224 |
+
yield "".join(partial_text)
|
| 225 |
+
|
| 226 |
+
def filter_concepts(search_text: str):
|
| 227 |
+
if not search_text.strip():
|
| 228 |
+
return concept_list[:500]
|
| 229 |
+
filtered = [c for c in concept_list if search_text.lower() in c.lower()]
|
| 230 |
+
return filtered[:500]
|
| 231 |
+
|
| 232 |
+
def add_concept_to_list(selected_concept, user_slider_val, current_list):
|
| 233 |
+
if not selected_concept:
|
| 234 |
+
return current_list
|
| 235 |
+
|
| 236 |
+
selected_concept_text = None
|
| 237 |
+
if selected_concept.startswith("[New] "):
|
| 238 |
+
selected_concept_text = selected_concept[6:]
|
| 239 |
+
idx = 0
|
| 240 |
+
else:
|
| 241 |
+
idx = concept_id_map[selected_concept]
|
| 242 |
+
internal_mag = user_slider_val
|
| 243 |
+
new_entry = {
|
| 244 |
+
"text": selected_concept,
|
| 245 |
+
"idx": idx,
|
| 246 |
+
"display_mag": user_slider_val,
|
| 247 |
+
"internal_mag": internal_mag,
|
| 248 |
+
"subspace_gen_text": selected_concept_text
|
| 249 |
+
}
|
| 250 |
+
# Add to the beginning of the list
|
| 251 |
+
current_list = [new_entry]
|
| 252 |
+
return current_list
|
| 253 |
+
|
| 254 |
+
def update_dropdown_choices(search_text):
|
| 255 |
+
filtered = filter_concepts(search_text)
|
| 256 |
+
if not filtered or len(filtered) == 0:
|
| 257 |
+
return gr.update(choices=[f"[New] {search_text}"], value=f"[New] {search_text}", interactive=True), gr.Textbox(
|
| 258 |
+
label="No matching existing concepts were found!",
|
| 259 |
+
value="Good news! Based on the concept you provided, we will automatically generate a steering vector. Try it out by starting a chat!",
|
| 260 |
+
lines=3,
|
| 261 |
+
interactive=False,
|
| 262 |
+
visible=True,
|
| 263 |
+
elem_id="alert-message"
|
| 264 |
+
)
|
| 265 |
+
# Automatically select the first matching concept
|
| 266 |
+
return gr.update(
|
| 267 |
+
choices=filtered,
|
| 268 |
+
value=filtered[0], # Select the first match
|
| 269 |
+
interactive=True, visible=True
|
| 270 |
+
), gr.Textbox(visible=False)
|
| 271 |
+
|
| 272 |
+
with gr.Blocks(css=css, fill_height=True) as demo:
|
| 273 |
+
# States for both detection and steering
|
| 274 |
+
selected_detection = gr.State([])
|
| 275 |
+
selected_subspaces = gr.State([])
|
| 276 |
+
|
| 277 |
+
with gr.Row(min_height=1000):
|
| 278 |
+
# Left side: chat area
|
| 279 |
+
with gr.Column(scale=7):
|
| 280 |
+
chat_interface = gr.ChatInterface(
|
| 281 |
+
fn=generate,
|
| 282 |
+
title="Chat with a Concept Steering Model",
|
| 283 |
+
description="""You can only steer the model when a concept is detected internally. Select concepts on the right →\n\nWe intervene on Gemma-2-2B-it by adding steering vectors to the residual stream at layer 20.""",
|
| 284 |
+
type="messages",
|
| 285 |
+
additional_inputs=[selected_detection, selected_subspaces],
|
| 286 |
+
fill_height=True,
|
| 287 |
+
css=".gradio-chatbot {min-height: 1500px;}"
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
# Right side: concept detection and steering
|
| 291 |
+
with gr.Column(scale=3):
|
| 292 |
+
# Concept Detection Panel
|
| 293 |
+
# gr.Markdown("## Detect then Steer")
|
| 294 |
+
gr.Markdown("Select a concept to detect. We will only steer the model when this concept is detected internally.")
|
| 295 |
+
with gr.Group():
|
| 296 |
+
detect_search = gr.Textbox(
|
| 297 |
+
label="Search Detection Concepts",
|
| 298 |
+
placeholder="Find concepts to detect (e.g. 'Google')",
|
| 299 |
+
lines=1,
|
| 300 |
+
)
|
| 301 |
+
detect_msg = gr.TextArea(visible=False)
|
| 302 |
+
detect_dropdown = gr.Dropdown(
|
| 303 |
+
label="Select concept to detect",
|
| 304 |
+
interactive=True,
|
| 305 |
+
allow_custom_value=False,
|
| 306 |
+
)
|
| 307 |
+
detect_threshold = gr.Slider(
|
| 308 |
+
label="Detection Threshold",
|
| 309 |
+
minimum=0,
|
| 310 |
+
maximum=1,
|
| 311 |
+
step=0.01,
|
| 312 |
+
value=0.5,
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
# Divider
|
| 316 |
+
# gr.Markdown("---")
|
| 317 |
+
|
| 318 |
+
# Steering Panel (existing)
|
| 319 |
+
# gr.Markdown("## Steer Response")
|
| 320 |
+
gr.Markdown("Select a concept to steer when detection occurs.")
|
| 321 |
+
with gr.Group():
|
| 322 |
+
search_box = gr.Textbox(
|
| 323 |
+
label="Search Steering Concepts",
|
| 324 |
+
placeholder="Find concepts to steer the model (e.g. 'ethics and morality')",
|
| 325 |
+
lines=1,
|
| 326 |
+
)
|
| 327 |
+
msg = gr.TextArea(visible=False)
|
| 328 |
+
concept_dropdown = gr.Dropdown(
|
| 329 |
+
label="Select concept to steer",
|
| 330 |
+
interactive=True,
|
| 331 |
+
allow_custom_value=False,
|
| 332 |
+
)
|
| 333 |
+
concept_magnitude = gr.Slider(
|
| 334 |
+
label="Steering Intensity",
|
| 335 |
+
minimum=-5,
|
| 336 |
+
maximum=5,
|
| 337 |
+
step=0.1,
|
| 338 |
+
value=3.5,
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
# Wire up events for detection
|
| 342 |
+
detect_search.input(
|
| 343 |
+
update_dropdown_choices,
|
| 344 |
+
[detect_search],
|
| 345 |
+
[detect_dropdown, detect_msg]
|
| 346 |
+
).then(
|
| 347 |
+
add_concept_to_list,
|
| 348 |
+
[detect_dropdown, detect_threshold, selected_detection],
|
| 349 |
+
[selected_detection]
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
detect_dropdown.select(
|
| 353 |
+
add_concept_to_list,
|
| 354 |
+
[detect_dropdown, detect_threshold, selected_detection],
|
| 355 |
+
[selected_detection]
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
detect_threshold.input(
|
| 359 |
+
add_concept_to_list,
|
| 360 |
+
[detect_dropdown, detect_threshold, selected_detection],
|
| 361 |
+
[selected_detection]
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
# Wire up events for steering (existing)
|
| 365 |
+
search_box.input(
|
| 366 |
+
update_dropdown_choices,
|
| 367 |
+
[search_box],
|
| 368 |
+
[concept_dropdown, msg]
|
| 369 |
+
).then(
|
| 370 |
+
add_concept_to_list,
|
| 371 |
+
[concept_dropdown, concept_magnitude, selected_subspaces],
|
| 372 |
+
[selected_subspaces]
|
| 373 |
+
)
|
| 374 |
+
|
| 375 |
+
concept_dropdown.select(
|
| 376 |
+
add_concept_to_list,
|
| 377 |
+
[concept_dropdown, concept_magnitude, selected_subspaces],
|
| 378 |
+
[selected_subspaces]
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
concept_magnitude.input(
|
| 382 |
+
add_concept_to_list,
|
| 383 |
+
[concept_dropdown, concept_magnitude, selected_subspaces],
|
| 384 |
+
[selected_subspaces]
|
| 385 |
+
)
|
| 386 |
|
| 387 |
+
demo.launch(share=True, height=1000)
|
style.css
DELETED
|
@@ -1,17 +0,0 @@
|
|
| 1 |
-
h1 {
|
| 2 |
-
text-align: center;
|
| 3 |
-
display: block;
|
| 4 |
-
}
|
| 5 |
-
|
| 6 |
-
#duplicate-button {
|
| 7 |
-
margin: auto;
|
| 8 |
-
color: white;
|
| 9 |
-
background: #1565c0;
|
| 10 |
-
border-radius: 100vh;
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
.contain {
|
| 14 |
-
max-width: 900px;
|
| 15 |
-
margin: auto;
|
| 16 |
-
padding-top: 1.5rem;
|
| 17 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|