This commit is contained in:
wassname
2023-10-24 12:30:30 +08:00
parent 1a52f8df82
commit c0e9b33c9e
4 changed files with 283 additions and 85 deletions
+4 -1
View File
@@ -1821,4 +1821,7 @@ Ok what should my pipeline do?
TODO:
- [ ] change layer -1 to actual name?
- [x] change layer -1 to actual name?
- [ ] in the TQS notebook they only get the mean from the generated tokens
- [ ] they only interven on the ones that are not padded... wait no they passed in no token pos in honesty
- [ ] but they did in TQA
+277 -83
View File
@@ -87,12 +87,12 @@
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2023-10-24 10:49:20.938\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging pad_token_id from 32000 to 0\u001b[0m\n",
"2023-10-24T10:49:20.938690+0800 INFO changing pad_token_id from 32000 to 0\n",
"\u001b[32m2023-10-24 10:49:20.940\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging padding_side from right to left\u001b[0m\n",
"2023-10-24T10:49:20.940322+0800 INFO changing padding_side from right to left\n",
"\u001b[32m2023-10-24 10:49:20.941\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging truncation_side from right to left\u001b[0m\n",
"2023-10-24T10:49:20.941383+0800 INFO changing truncation_side from right to left\n"
"\u001b[32m2023-10-24 12:20:50.811\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging pad_token_id from 32000 to 0\u001b[0m\n",
"2023-10-24T12:20:50.811435+0800 INFO changing pad_token_id from 32000 to 0\n",
"\u001b[32m2023-10-24 12:20:50.812\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging padding_side from right to left\u001b[0m\n",
"2023-10-24T12:20:50.812537+0800 INFO changing padding_side from right to left\n",
"\u001b[32m2023-10-24 12:20:50.813\u001b[0m | \u001b[1mINFO \u001b[0m | \u001b[36msrc.models.load\u001b[0m:\u001b[36mverbose_change_param\u001b[0m:\u001b[36m18\u001b[0m - \u001b[1mchanging truncation_side from right to left\u001b[0m\n",
"2023-10-24T12:20:50.813021+0800 INFO changing truncation_side from right to left\n"
]
}
],
@@ -147,11 +147,11 @@
"name": "stderr",
"output_type": "stream",
"text": [
"tokenize: 100%|██████████| 302/302 [00:00<00:00, 3490.12 examples/s]\n",
"truncated: 100%|██████████| 302/302 [00:00<00:00, 3889.90 examples/s]\n",
"prompt_truncated: 100%|██████████| 302/302 [00:00<00:00, 604.08 examples/s]\n",
"choice_ids: 100%|██████████| 302/302 [00:00<00:00, 9749.77 examples/s]\n",
"Filter: 100%|██████████| 302/302 [00:00<00:00, 3987.01 examples/s]"
"tokenize: 100%|██████████| 302/302 [00:00<00:00, 2898.85 examples/s]\n",
"truncated: 100%|██████████| 302/302 [00:00<00:00, 3922.12 examples/s]\n",
"prompt_truncated: 100%|██████████| 302/302 [00:00<00:00, 565.78 examples/s]\n",
"choice_ids: 100%|██████████| 302/302 [00:00<00:00, 10226.05 examples/s]\n",
"Filter: 100%|██████████| 302/302 [00:00<00:00, 3935.72 examples/s]"
]
},
{
@@ -236,7 +236,7 @@
{
"data": {
"text/plain": [
"<src.repe.rep_readers.PCARepReader at 0x7fcd97b27250>"
"<src.repe.rep_readers.PCARepReader at 0x7ff755ec4ee0>"
]
},
"execution_count": 8,
@@ -268,17 +268,17 @@
{
"data": {
"text/plain": [
"{8: -7.1495447,\n",
" 11: 16.796896,\n",
" 14: 13.5725565,\n",
" 17: -5.684828,\n",
" 20: 26.571663,\n",
" 23: 37.01544,\n",
" 26: -67.76076,\n",
" 29: -64.502205,\n",
" 32: -52.72602,\n",
" 35: -76.70331,\n",
" 38: -80.74953}"
"{8: 1.661585,\n",
" 11: 4.2028227,\n",
" 14: -1.7875421,\n",
" 17: -6.6559744,\n",
" 20: -7.8236346,\n",
" 23: -10.668092,\n",
" 26: -1.1562811,\n",
" 29: -2.477594,\n",
" 32: -3.406129,\n",
" 35: -6.056723,\n",
" 38: 3.0787961}"
]
},
"execution_count": 9,
@@ -308,61 +308,241 @@
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<src.repe.rep_control_pipeline.RepControlPipeline at 0x7ff755ec4ca0>"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"# layer_id = hidden_layers\n",
"# block_name=\"decoder_block\"\n",
"# control_method=\"reading_vec\"\n",
"layer_id = hidden_layers\n",
"block_name=\"decoder_block\"\n",
"control_method=\"reading_vec\"\n",
"\n",
"# rep_control_pipeline = pipeline(\n",
"# \"rep-control\", \n",
"# model=model, \n",
"# tokenizer=tokenizer, \n",
"# layers=layer_id, max_length=cfg.max_length,\n",
"# control_method=control_method)\n",
"# rep_control_pipeline\n"
"rep_control_pipeline = pipeline(\n",
" \"rep-control\", \n",
" model=model, \n",
" tokenizer=tokenizer, \n",
" layers=layer_id, max_length=cfg.max_length,\n",
" control_method=control_method)\n",
"rep_control_pipeline\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===== No Control =====\n",
"negative\n",
"===== + Honesty Control =====\n",
"negative\n",
"\n",
"===== - Honesty Control =====\n",
"I\n",
"\n",
"===== No Control =====\n",
"pos\n",
"===== + Honesty Control =====\n",
"pos\n",
"\n",
"===== - Honesty Control =====\n",
"pos\n",
"\n"
]
}
],
"source": [
"\n",
"# from re import S\n",
"from re import S\n",
"layer_id = hidden_layers\n",
"\n",
"inputs = dataset_train[:2]\n",
"coeff=8.0\n",
"max_new_tokens=1\n",
"text_gen_kwargs = dict(do_sample=False, max_new_tokens=max_new_tokens, use_cache=False, \n",
" output_hidden_states=True, return_dict=True,\n",
" )\n",
"\n",
"# inputs = dataset_train[:2]\n",
"# coeff=8.0\n",
"# max_new_tokens=1\n",
"# text_gen_kwargs = dict(do_sample=False, max_new_tokens=max_new_tokens, use_cache=False, \n",
"# output_hidden_states=True, return_dict=True,\n",
"# )\n",
"\n",
"# activations = {}\n",
"# for layer in layer_id:\n",
"# activations[layer] = torch.tensor(coeff * honesty_rep_reader.directions[layer] * honesty_rep_reader.direction_signs[layer]).to(model.device).half()\n",
"activations = {}\n",
"for layer in layer_id:\n",
" activations[layer] = torch.tensor(coeff * honesty_rep_reader.directions[layer] * honesty_rep_reader.direction_signs[layer]).to(model.device).half()\n",
" \n",
"\n",
"# activations_neg = {k:-v for k,v in activations.items()}\n",
"activations_neg = {k:-v for k,v in activations.items()}\n",
"\n",
"# model.eval()\n",
"# with torch.no_grad():\n",
"# baseline_outputs = rep_control_pipeline(inputs, batch_size=batch_size, **text_gen_kwargs)\n",
"# control_outputs = rep_control_pipeline(inputs, activations=activations, batch_size=batch_size, **text_gen_kwargs)\n",
"# control_outputs_neg = rep_control_pipeline(inputs, activations=activations_neg, batch_size=batch_size, **text_gen_kwargs)\n",
"model.eval()\n",
"with torch.no_grad():\n",
" baseline_outputs = rep_control_pipeline(inputs, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs = rep_control_pipeline(inputs, activations=activations, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs_neg = rep_control_pipeline(inputs, activations=activations_neg, batch_size=batch_size, **text_gen_kwargs)\n",
"\n",
"# for i,s,p,n in zip(inputs, baseline_outputs['text_ans'], control_outputs['text_ans'], control_outputs_neg['text_ans']):\n",
"# print(\"===== No Control =====\")\n",
"# print(s)\n",
"# print(f\"===== + Honesty Control =====\")\n",
"# print(p)\n",
"# print()\n",
"# print(f\"===== - Honesty Control =====\")\n",
"# print(n)\n",
"# print()\n"
"for i,s,p,n in zip(inputs, baseline_outputs['text_ans'], control_outputs['text_ans'], control_outputs_neg['text_ans']):\n",
" print(\"===== No Control =====\")\n",
" print(s)\n",
" print(f\"===== + Honesty Control =====\")\n",
" print(p)\n",
" print()\n",
" print(f\"===== - Honesty Control =====\")\n",
" print(n)\n",
" print()\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===== No Control =====\n",
"15.20%\n",
"===== + Honesty Control =====\n",
"19.81%\n",
"\n",
"===== - Honesty Control =====\n",
"29.42%\n",
"\n",
"===== No Control =====\n",
"61.88%\n",
"===== + Honesty Control =====\n",
"91.96%\n",
"\n",
"===== - Honesty Control =====\n",
"84.79%\n",
"\n"
]
}
],
"source": [
"for i,s,p,n in zip(inputs, baseline_outputs['ans'], control_outputs['ans'], control_outputs_neg['ans']):\n",
" print(\"===== No Control =====\")\n",
" print(f'{s:02.2%}')\n",
" print(f\"===== + Honesty Control =====\")\n",
" print(f'{p:02.2%}')\n",
" print()\n",
" print(f\"===== - Honesty Control =====\")\n",
" print(f'{n:02.2%}')\n",
" print()\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===== No Control =====\n",
"negative\n",
"===== + Honesty Control =====\n",
"negative\n",
"\n",
"===== - Honesty Control =====\n",
"I\n",
"\n",
"===== No Control =====\n",
"pos\n",
"===== + Honesty Control =====\n",
"pos\n",
"\n",
"===== - Honesty Control =====\n",
"pos\n",
"\n"
]
}
],
"source": [
"\n",
"from re import S\n",
"layer_id = hidden_layers\n",
"\n",
"inputs = dataset_train[:2]\n",
"coeff=8.0\n",
"max_new_tokens=1\n",
"text_gen_kwargs = dict(do_sample=False, max_new_tokens=max_new_tokens, use_cache=False, \n",
" output_hidden_states=True, return_dict=True,\n",
" token_pos=\"end\",\n",
" normalize=False\n",
" )\n",
"\n",
"activations = {}\n",
"for layer in layer_id:\n",
" activations[layer] = torch.tensor(coeff * honesty_rep_reader.directions[layer] * honesty_rep_reader.direction_signs[layer]).to(model.device).half()\n",
" \n",
"\n",
"activations_neg = {k:-v for k,v in activations.items()}\n",
"\n",
"model.eval()\n",
"with torch.no_grad():\n",
" baseline_outputs = rep_control_pipeline(inputs, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs = rep_control_pipeline(inputs, activations=activations, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs_neg = rep_control_pipeline(inputs, activations=activations_neg, batch_size=batch_size, **text_gen_kwargs)\n",
"\n",
"for i,s,p,n in zip(inputs, baseline_outputs['text_ans'], control_outputs['text_ans'], control_outputs_neg['text_ans']):\n",
" print(\"===== No Control =====\")\n",
" print(s)\n",
" print(f\"===== + Honesty Control =====\")\n",
" print(p)\n",
" print()\n",
" print(f\"===== - Honesty Control =====\")\n",
" print(n)\n",
" print()\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"===== No Control =====\n",
"15.20%\n",
"===== + Honesty Control =====\n",
"19.81%\n",
"\n",
"===== - Honesty Control =====\n",
"29.42%\n",
"\n",
"===== No Control =====\n",
"61.88%\n",
"===== + Honesty Control =====\n",
"91.96%\n",
"\n",
"===== - Honesty Control =====\n",
"84.79%\n",
"\n"
]
}
],
"source": [
"for i,s,p,n in zip(inputs, baseline_outputs['ans'], control_outputs['ans'], control_outputs_neg['ans']):\n",
" print(\"===== No Control =====\")\n",
" print(f'{s:02.2%}')\n",
" print(f\"===== + Honesty Control =====\")\n",
" print(f'{p:02.2%}')\n",
" print()\n",
" print(f\"===== - Honesty Control =====\")\n",
" print(f'{n:02.2%}')\n",
" print()\n"
]
},
{
@@ -381,35 +561,35 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<src.repe.rep_control_pipeline_baukit.RepControlPipeline2 at 0x7fcca5807130>"
"<src.repe.rep_control_pipeline_baukit.RepControlPipeline2 at 0x7ff755f5be50>"
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"layer_id = hidden_layers\n",
"\n",
"rep_control_pipeline = pipeline(\n",
"\n",
"rep_control_pipeline2 = pipeline(\n",
" \"rep-control2\", \n",
" model=model, \n",
" tokenizer=tokenizer, \n",
" layers=layer_id, \n",
" max_length=cfg.max_length,)\n",
"rep_control_pipeline\n"
"rep_control_pipeline2\n"
]
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -422,15 +602,15 @@
"negative\n",
"\n",
"===== - Honesty Control =====\n",
"negative\n",
"I\n",
"\n",
"===== No Control =====\n",
"The\n",
"pos\n",
"===== + Honesty Control =====\n",
"The\n",
"pos\n",
"\n",
"===== - Honesty Control =====\n",
"negative\n",
"pos\n",
"\n"
]
}
@@ -452,9 +632,9 @@
"\n",
"model.eval()\n",
"with torch.no_grad():\n",
" baseline_outputs = rep_control_pipeline(inputs, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs = rep_control_pipeline(inputs, activations=activations, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs_neg = rep_control_pipeline(inputs, activations=activations_neg, batch_size=batch_size, **text_gen_kwargs)\n",
" baseline_outputs = rep_control_pipeline2(inputs, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs = rep_control_pipeline2(inputs, activations=activations, batch_size=batch_size, **text_gen_kwargs)\n",
" control_outputs_neg = rep_control_pipeline2(inputs, activations=activations_neg, batch_size=batch_size, **text_gen_kwargs)\n",
"\n",
"\n",
"for i,s,p,n in zip(inputs, baseline_outputs['text_ans'], control_outputs['text_ans'], control_outputs_neg['text_ans']):\n",
@@ -470,7 +650,7 @@
},
{
"cell_type": "code",
"execution_count": 71,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -478,20 +658,20 @@
"output_type": "stream",
"text": [
"===== No Control =====\n",
"2.03%\n",
"15.20%\n",
"===== + Honesty Control =====\n",
"23.10%\n",
"19.81%\n",
"\n",
"===== - Honesty Control =====\n",
"1.02%\n",
"29.42%\n",
"\n",
"===== No Control =====\n",
"33.63%\n",
"61.88%\n",
"===== + Honesty Control =====\n",
"83.52%\n",
"91.96%\n",
"\n",
"===== - Honesty Control =====\n",
"7.75%\n",
"84.79%\n",
"\n"
]
}
@@ -508,6 +688,20 @@
" print()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
+1 -1
View File
@@ -35,7 +35,7 @@ class RepControlPipeline(RepControlPipeline2):
self.layers = layers
self.max_length = max_length
super().__init__(model=model, tokenizer=tokenizer, **kwargs)
super().__init__(model=model, tokenizer=tokenizer, max_length=max_length, **kwargs)
def __call__(self, text_inputs, activations=None, **kwargs):
if activations is not None:
+1
View File
@@ -20,6 +20,7 @@ def row_choice_ids(answer_choices, tokenizer):
def intervene(output, activation):
# TODO need attention mask
assert output.ndim == 3, f"expected output to be (batch, seq, vocab), got {output.shape}"
return output + activation.to(output.device)[None, None, :]