tidy and run probe

This commit is contained in:
deep1
2023-08-27 13:46:26 +08:00
parent 66ab419053
commit 43074f79cb
8 changed files with 1383 additions and 8479 deletions
+8
View File
@@ -1068,3 +1068,11 @@ Wires it up a bit more. Now I need to debug. For example my chosen asnwers are o
4mins for 100
40 mins for 1000
# 2023-08-27 13:38:33
So I got a dataset I want to
- try training a prob on probs
- try training a probe on expnses probs
- look at diff between probs and expanded probs
- looks at llm acc by dataset, lie by dataset, prob acc by dataset
- finally look at generaliation
-335
View File
@@ -1,335 +0,0 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# autoreload import your package\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"===================================BUG REPORT===================================\n",
"Welcome to bitsandbytes. For bug reports, please submit your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
"================================================================================\n",
"CUDA SETUP: CUDA runtime path found: /home/ubuntu/mambaforge/envs/dlk3/lib/libcudart.so\n",
"CUDA SETUP: Highest compute capability among GPUs detected: 8.6\n",
"CUDA SETUP: Detected CUDA version 117\n",
"CUDA SETUP: Loading binary /home/ubuntu/mambaforge/envs/dlk3/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_cuda117.so...\n"
]
}
],
"source": [
"# from elk.extraction.prompt_loading import load_prompts\n",
"from elk.extraction.extraction import Extract"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from src.prompts.prompt_loading import load_prompts"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Extracting 13 variants of each prompt\n"
]
}
],
"source": [
"# %%prun\n",
"cfg = Extract(\"HuggingFaceH4/starchat-beta\", datasets=[\"imdb\"], num_shots=1, binarize=True)\n",
"cfg\n",
"\n",
"# loop through all prompts in this dataset\n",
"split_type = \"train\"\n",
"rank = 0\n",
"ds_names = cfg.datasets\n",
"world_size=1\n",
"prompt_ds = load_prompts(\n",
" ds_names[0],\n",
" binarize=cfg.binarize,\n",
" num_shots=cfg.num_shots,\n",
" split_type=split_type,\n",
" template_path=cfg.template_path,\n",
" rank=rank,\n",
" world_size=world_size,\n",
" seed=cfg.seed,\n",
")\n",
"g =iter(prompt_ds)\n",
"b = next(g)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# %%prun\n",
"# b = next(g)\n",
"# b"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7258e7f338f44046ad7b9552d1243da6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"0it [00:00, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39mtqdm\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mauto\u001b[39;00m \u001b[39mimport\u001b[39;00m tqdm\n\u001b[0;32m----> 2\u001b[0m [a \u001b[39mfor\u001b[39;00m a \u001b[39min\u001b[39;00m \u001b[39mlist\u001b[39;49m(tqdm(prompt_ds))]\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/tqdm/notebook.py:249\u001b[0m, in \u001b[0;36mtqdm_notebook.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 247\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m 248\u001b[0m it \u001b[39m=\u001b[39m \u001b[39msuper\u001b[39m(tqdm_notebook, \u001b[39mself\u001b[39m)\u001b[39m.\u001b[39m\u001b[39m__iter__\u001b[39m()\n\u001b[0;32m--> 249\u001b[0m \u001b[39mfor\u001b[39;00m obj \u001b[39min\u001b[39;00m it:\n\u001b[1;32m 250\u001b[0m \u001b[39m# return super(tqdm...) will not catch exception\u001b[39;00m\n\u001b[1;32m 251\u001b[0m \u001b[39myield\u001b[39;00m obj\n\u001b[1;32m 252\u001b[0m \u001b[39m# NB: except ... [ as ...] breaks IPython async KeyboardInterrupt\u001b[39;00m\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/tqdm/std.py:1182\u001b[0m, in \u001b[0;36mtqdm.__iter__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1179\u001b[0m time \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_time\n\u001b[1;32m 1181\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m-> 1182\u001b[0m \u001b[39mfor\u001b[39;00m obj \u001b[39min\u001b[39;00m iterable:\n\u001b[1;32m 1183\u001b[0m \u001b[39myield\u001b[39;00m obj\n\u001b[1;32m 1184\u001b[0m \u001b[39m# Update and possibly print the progressbar.\u001b[39;00m\n\u001b[1;32m 1185\u001b[0m \u001b[39m# Note: does not call self.update(1) for speed optimisation.\u001b[39;00m\n",
"File \u001b[0;32m~/Documents/mjc/elk/discovering_latent_knowledge/src/prompts/prompt_loading.py:132\u001b[0m, in \u001b[0;36mload_prompts\u001b[0;34m(ds_string, sys_instructions, binarize, num_shots, seed, split_type, template_path, rank, world_size, prompt_format)\u001b[0m\n\u001b[1;32m 129\u001b[0m ds \u001b[39m=\u001b[39m ds\u001b[39m.\u001b[39mto_iterable_dataset()\n\u001b[1;32m 131\u001b[0m \u001b[39mfor\u001b[39;00m example \u001b[39min\u001b[39;00m ds:\n\u001b[0;32m--> 132\u001b[0m \u001b[39myield\u001b[39;00m _convert_to_prompts(\n\u001b[1;32m 133\u001b[0m example,\n\u001b[1;32m 134\u001b[0m binarize\u001b[39m=\u001b[39;49mbinarize,\n\u001b[1;32m 135\u001b[0m label_column\u001b[39m=\u001b[39;49mlabel_column,\n\u001b[1;32m 136\u001b[0m label_choices\u001b[39m=\u001b[39;49mlabel_choices, \u001b[39m# type: ignore[arg-type]\u001b[39;49;00m\n\u001b[1;32m 137\u001b[0m prompter\u001b[39m=\u001b[39;49mprompter,\n\u001b[1;32m 138\u001b[0m rng\u001b[39m=\u001b[39;49mrng,\n\u001b[1;32m 139\u001b[0m sys_instructions\u001b[39m=\u001b[39;49msys_instructions,\n\u001b[1;32m 140\u001b[0m fewshot_iter\u001b[39m=\u001b[39;49mfewshot_iter,\n\u001b[1;32m 141\u001b[0m prompt_format\u001b[39m=\u001b[39;49mprompt_format,\n\u001b[1;32m 142\u001b[0m )\n",
"File \u001b[0;32m~/Documents/mjc/elk/discovering_latent_knowledge/src/prompts/prompt_loading.py:192\u001b[0m, in \u001b[0;36m_convert_to_prompts\u001b[0;34m(example, prompter, binarize, label_column, label_choices, rng, sys_instructions, fewshot_iter, prompt_format)\u001b[0m\n\u001b[1;32m 190\u001b[0m fewshot_examples \u001b[39m=\u001b[39m \u001b[39mnext\u001b[39m(fewshot_iter)\n\u001b[1;32m 191\u001b[0m \u001b[39mif\u001b[39;00m lie: fewshot_examples \u001b[39m=\u001b[39m [{\u001b[39m*\u001b[39m\u001b[39m*\u001b[39me, \u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m: \u001b[39m~\u001b[39me[\u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m]} \u001b[39mfor\u001b[39;00m e \u001b[39min\u001b[39;00m fewshot_examples]\n\u001b[0;32m--> 192\u001b[0m fewshot_texts \u001b[39m=\u001b[39m [\n\u001b[1;32m 193\u001b[0m \u001b[39mdict\u001b[39;49m(user\u001b[39m=\u001b[39;49mq, response\u001b[39m=\u001b[39;49ma) \u001b[39mfor\u001b[39;49;00m q, a \u001b[39min\u001b[39;49;00m \u001b[39mmap\u001b[39;49m(template\u001b[39m.\u001b[39;49mapply, fewshot_examples)\n\u001b[1;32m 194\u001b[0m ]\n\u001b[1;32m 195\u001b[0m prompt_parts \u001b[39m=\u001b[39m fewshot_texts \u001b[39m+\u001b[39m prompt_parts\n\u001b[1;32m 197\u001b[0m prompt_parts[\u001b[39m0\u001b[39m][\u001b[39m'\u001b[39m\u001b[39msystem\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m sys_instr\n",
"File \u001b[0;32m~/Documents/mjc/elk/discovering_latent_knowledge/src/prompts/prompt_loading.py:192\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 190\u001b[0m fewshot_examples \u001b[39m=\u001b[39m \u001b[39mnext\u001b[39m(fewshot_iter)\n\u001b[1;32m 191\u001b[0m \u001b[39mif\u001b[39;00m lie: fewshot_examples \u001b[39m=\u001b[39m [{\u001b[39m*\u001b[39m\u001b[39m*\u001b[39me, \u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m: \u001b[39m~\u001b[39me[\u001b[39m'\u001b[39m\u001b[39mlabel\u001b[39m\u001b[39m'\u001b[39m]} \u001b[39mfor\u001b[39;00m e \u001b[39min\u001b[39;00m fewshot_examples]\n\u001b[0;32m--> 192\u001b[0m fewshot_texts \u001b[39m=\u001b[39m [\n\u001b[1;32m 193\u001b[0m \u001b[39mdict\u001b[39m(user\u001b[39m=\u001b[39mq, response\u001b[39m=\u001b[39ma) \u001b[39mfor\u001b[39;00m q, a \u001b[39min\u001b[39;00m \u001b[39mmap\u001b[39m(template\u001b[39m.\u001b[39mapply, fewshot_examples)\n\u001b[1;32m 194\u001b[0m ]\n\u001b[1;32m 195\u001b[0m prompt_parts \u001b[39m=\u001b[39m fewshot_texts \u001b[39m+\u001b[39m prompt_parts\n\u001b[1;32m 197\u001b[0m prompt_parts[\u001b[39m0\u001b[39m][\u001b[39m'\u001b[39m\u001b[39msystem\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m sys_instr\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/elk/promptsource/templates.py:165\u001b[0m, in \u001b[0;36mTemplate.apply\u001b[0;34m(self, example, truncate, highlight_variables)\u001b[0m\n\u001b[1;32m 162\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39m\"\u001b[39m\u001b[39manswer_choices\u001b[39m\u001b[39m\"\u001b[39m \u001b[39min\u001b[39;00m protected_example:\n\u001b[1;32m 163\u001b[0m \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\u001b[39m\"\u001b[39m\u001b[39mExample contains the restricted key \u001b[39m\u001b[39m'\u001b[39m\u001b[39manswer_choices\u001b[39m\u001b[39m'\u001b[39m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m--> 165\u001b[0m protected_example[\u001b[39m\"\u001b[39m\u001b[39manswer_choices\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mget_answer_choices_list(example)\n\u001b[1;32m 167\u001b[0m \u001b[39m# Renders the Jinja template\u001b[39;00m\n\u001b[1;32m 168\u001b[0m rendered_example \u001b[39m=\u001b[39m rtemplate\u001b[39m.\u001b[39mrender(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mprotected_example)\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/elk/promptsource/templates.py:109\u001b[0m, in \u001b[0;36mTemplate.get_answer_choices_list\u001b[0;34m(self, example)\u001b[0m\n\u001b[1;32m 106\u001b[0m \u001b[39mif\u001b[39;00m jinja \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 107\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mNone\u001b[39;00m\n\u001b[0;32m--> 109\u001b[0m rtemplate \u001b[39m=\u001b[39m env\u001b[39m.\u001b[39;49mfrom_string(jinja)\n\u001b[1;32m 110\u001b[0m protected_example \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_escape_pipe(example)\n\u001b[1;32m 111\u001b[0m rendered_choices \u001b[39m=\u001b[39m rtemplate\u001b[39m.\u001b[39mrender(\u001b[39m*\u001b[39m\u001b[39m*\u001b[39mprotected_example)\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/environment.py:1105\u001b[0m, in \u001b[0;36mEnvironment.from_string\u001b[0;34m(self, source, globals, template_class)\u001b[0m\n\u001b[1;32m 1103\u001b[0m gs \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmake_globals(\u001b[39mglobals\u001b[39m)\n\u001b[1;32m 1104\u001b[0m \u001b[39mcls\u001b[39m \u001b[39m=\u001b[39m template_class \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mtemplate_class\n\u001b[0;32m-> 1105\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39mfrom_code(\u001b[39mself\u001b[39m, \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mcompile(source), gs, \u001b[39mNone\u001b[39;00m)\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/environment.py:760\u001b[0m, in \u001b[0;36mEnvironment.compile\u001b[0;34m(self, source, name, filename, raw, defer_init)\u001b[0m\n\u001b[1;32m 758\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(source, \u001b[39mstr\u001b[39m):\n\u001b[1;32m 759\u001b[0m source_hint \u001b[39m=\u001b[39m source\n\u001b[0;32m--> 760\u001b[0m source \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_parse(source, name, filename)\n\u001b[1;32m 761\u001b[0m source \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_generate(source, name, filename, defer_init\u001b[39m=\u001b[39mdefer_init)\n\u001b[1;32m 762\u001b[0m \u001b[39mif\u001b[39;00m raw:\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/environment.py:617\u001b[0m, in \u001b[0;36mEnvironment._parse\u001b[0;34m(self, source, name, filename)\u001b[0m\n\u001b[1;32m 613\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m_parse\u001b[39m(\n\u001b[1;32m 614\u001b[0m \u001b[39mself\u001b[39m, source: \u001b[39mstr\u001b[39m, name: t\u001b[39m.\u001b[39mOptional[\u001b[39mstr\u001b[39m], filename: t\u001b[39m.\u001b[39mOptional[\u001b[39mstr\u001b[39m]\n\u001b[1;32m 615\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m nodes\u001b[39m.\u001b[39mTemplate:\n\u001b[1;32m 616\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Internal parsing function used by `parse` and `compile`.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 617\u001b[0m \u001b[39mreturn\u001b[39;00m Parser(\u001b[39mself\u001b[39;49m, source, name, filename)\u001b[39m.\u001b[39mparse()\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/parser.py:60\u001b[0m, in \u001b[0;36mParser.__init__\u001b[0;34m(self, environment, source, name, filename, state)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__init__\u001b[39m(\n\u001b[1;32m 52\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[1;32m 53\u001b[0m environment: \u001b[39m\"\u001b[39m\u001b[39mEnvironment\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 57\u001b[0m state: t\u001b[39m.\u001b[39mOptional[\u001b[39mstr\u001b[39m] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 58\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 59\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39menvironment \u001b[39m=\u001b[39m environment\n\u001b[0;32m---> 60\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mstream \u001b[39m=\u001b[39m environment\u001b[39m.\u001b[39;49m_tokenize(source, name, filename, state)\n\u001b[1;32m 61\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mname \u001b[39m=\u001b[39m name\n\u001b[1;32m 62\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mfilename \u001b[39m=\u001b[39m filename\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/environment.py:669\u001b[0m, in \u001b[0;36mEnvironment._tokenize\u001b[0;34m(self, source, name, filename, state)\u001b[0m\n\u001b[1;32m 666\u001b[0m source \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mpreprocess(source, name, filename)\n\u001b[1;32m 667\u001b[0m stream \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mlexer\u001b[39m.\u001b[39mtokenize(source, name, filename, state)\n\u001b[0;32m--> 669\u001b[0m \u001b[39mfor\u001b[39;00m ext \u001b[39min\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49miter_extensions():\n\u001b[1;32m 670\u001b[0m stream \u001b[39m=\u001b[39m ext\u001b[39m.\u001b[39mfilter_stream(stream) \u001b[39m# type: ignore\u001b[39;00m\n\u001b[1;32m 672\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39misinstance\u001b[39m(stream, TokenStream):\n",
"File \u001b[0;32m~/mambaforge/envs/dlk3/lib/python3.11/site-packages/jinja2/environment.py:459\u001b[0m, in \u001b[0;36mEnvironment.iter_extensions\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 457\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39miter_extensions\u001b[39m(\u001b[39mself\u001b[39m) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m t\u001b[39m.\u001b[39mIterator[\u001b[39m\"\u001b[39m\u001b[39mExtension\u001b[39m\u001b[39m\"\u001b[39m]:\n\u001b[1;32m 458\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Iterates over the extensions by priority.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 459\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39miter\u001b[39m(\u001b[39msorted\u001b[39m(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mextensions\u001b[39m.\u001b[39mvalues(), key\u001b[39m=\u001b[39m\u001b[39mlambda\u001b[39;00m x: x\u001b[39m.\u001b[39mpriority))\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"# from tqdm.auto import tqdm\n",
"# [a for a in list(tqdm(prompt_ds))]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"104"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(b['prompts'])"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<|system|>Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n",
"<end>\n",
"<|user|>if you like gangster type of movies, then this is the first one you should buy or at least rent, Al Pacino his performance is top notch. and the story is classic!! 10 / 10 !!!! Why isn't this movie in the TOP 250 list??\n",
"Did the reviewer enjoy the movie?\n",
"<|end|>\n",
"<|response|>Yes\n",
"<|end|>\n",
"<|user|>George P. Cosmatos' \"Rambo: First Blood Part II\" is pure wish-fulfillment. The United States clearly didn't win the war in Vietnam. They caused damage to this country beyond the imaginable and this movie continues the fairy story of the oh-so innocent soldiers. The only bad guys were the leaders of the nation, who made this war happen. The character of Rambo is perfect to notice this. He is extremely patriotic, bemoans that US-Americans didn't appreciate and celebrate the achievements of the single soldier, but has nothing but distrust for leading officers and politicians. Like every film that defends the war (e.g. \"We Were Soldiers\") also this one avoids the need to give a comprehensible reason for the engagement in South Asia. And for that matter also the reason for every single US-American soldier that was there. Instead, Rambo gets to take revenge for the wounds of a whole nation. It would have been better to work on how to deal with the memories, rather than suppressing them. \"Do we get to win this time?\" Yes, you do.\n",
"Did the reviewer enjoy the movie?\n",
"<|end|>\n",
"<|response|>\n"
]
},
{
"data": {
"text/plain": [
"{'answer': 'No',\n",
" 'question': '<|system|>Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\\n<end>\\n<|user|>if you like gangster type of movies, then this is the first one you should buy or at least rent, Al Pacino his performance is top notch. and the story is classic!! 10 / 10 !!!! Why isn\\'t this movie in the TOP 250 list??\\nDid the reviewer enjoy the movie?\\n<|end|>\\n<|response|>Yes\\n<|end|>\\n<|user|>George P. Cosmatos\\' \"Rambo: First Blood Part II\" is pure wish-fulfillment. The United States clearly didn\\'t win the war in Vietnam. They caused damage to this country beyond the imaginable and this movie continues the fairy story of the oh-so innocent soldiers. The only bad guys were the leaders of the nation, who made this war happen. The character of Rambo is perfect to notice this. He is extremely patriotic, bemoans that US-Americans didn\\'t appreciate and celebrate the achievements of the single soldier, but has nothing but distrust for leading officers and politicians. Like every film that defends the war (e.g. \"We Were Soldiers\") also this one avoids the need to give a comprehensible reason for the engagement in South Asia. And for that matter also the reason for every single US-American soldier that was there. Instead, Rambo gets to take revenge for the wounds of a whole nation. It would have been better to work on how to deal with the memories, rather than suppressing them. \"Do we get to win this time?\" Yes, you do.\\nDid the reviewer enjoy the movie?\\n<|end|>\\n<|response|>',\n",
" 'answer_choices': ['No', 'Yes'],\n",
" 'template_name': 'Reviewer Enjoyment Yes No',\n",
" 'label_true': 0,\n",
" 'label_instructed': 0,\n",
" 'instructed_to_lie': False,\n",
" 'sys_instr_name': 'truth'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import random\n",
"ps = b['prompts']\n",
"c = random.choice(ps)\n",
"print(c['question'])\n",
"c"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(ps[0]['question'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b['label']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b['prompts']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b['template_names']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# scratch prompt structure\n",
"\n",
"see https://github.com/EleutherAI/elk/blob/1b60b3bff348b00356cd15b5eb017f9c9bfdbae1/elk/promptsource/templates.py\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"from elk.promptsource.templates import env\n",
"\n",
"f = '../src/prompts/templates/system.yaml'\n",
"yaml_dict = yaml.load(open(f, 'r'), Loader=yaml.FullLoader)\n",
"templates = yaml_dict[\"templates\"]\n",
"jinja = templates['chatml']\n",
"rtemplate = env.from_string(jinja)\n",
"rtemplate.render(system=\"system2\", user=\"user2\", response=\"response2\")\n",
"# rtemplate.render(system=\"\", user=\"user2\", response=\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rtemplate.apply"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "dlk2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
+814
View File
@@ -0,0 +1,814 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# distance and direciton\n",
"\n",
"Let try to opt for distance and direction with\n",
"\n",
"$L1loss(y_1-y_0, y_{true})$\n",
"\n",
"where $y_1=model(x_1)$\n",
"\n",
"So I'm optimising for the hidden states to be the correct distance and direcioton away. It's like the margin raning loss."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"links:\n",
"- [loading](https://github.com/deep-diver/LLM-As-Chatbot/blob/main/models/alpaca.py)\n",
"- [dict](https://github.com/deep-diver/LLM-As-Chatbot/blob/c79e855a492a968b54bac223e66dc9db448d6eba/model_cards.json#L143)\n",
"- [prompt_format](https://github.com/deep-diver/PingPong/blob/main/src/pingpong/alpaca.py)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# import your package\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"===================================BUG REPORT===================================\n",
"Welcome to bitsandbytes. For bug reports, please run\n",
"\n",
"python -m bitsandbytes\n",
"\n",
" and submit this information together with your error trace to: https://github.com/TimDettmers/bitsandbytes/issues\n",
"================================================================================\n",
"bin /home/ubuntu/mambaforge/envs/dlk3/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_cuda117.so\n",
"CUDA SETUP: CUDA runtime path found: /home/ubuntu/mambaforge/envs/dlk3/lib/libcudart.so.11.0\n",
"CUDA SETUP: Highest compute capability among GPUs detected: 8.6\n",
"CUDA SETUP: Detected CUDA version 117\n",
"CUDA SETUP: Loading binary /home/ubuntu/mambaforge/envs/dlk3/lib/python3.11/site-packages/bitsandbytes/libbitsandbytes_cuda117.so...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/ubuntu/mambaforge/envs/dlk3/lib/python3.11/site-packages/bitsandbytes/cuda_setup/main.py:149: UserWarning: Found duplicate ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] files: {PosixPath('/home/ubuntu/mambaforge/envs/dlk3/lib/libcudart.so.11.0'), PosixPath('/home/ubuntu/mambaforge/envs/dlk3/lib/libcudart.so')}.. We'll flip a coin and try one of these, in order to fail forward.\n",
"Either way, this might cause trouble in the future:\n",
"If you get `CUDA error: invalid device function` errors, the above might be the cause and the solution is to make sure only one ['libcudart.so', 'libcudart.so.11.0', 'libcudart.so.12.0'] in the paths that we search based on your env.\n",
" warn(msg)\n"
]
},
{
"data": {
"text/plain": [
"'4.31.0'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"from matplotlib import pyplot as plt\n",
"plt.style.use('ggplot')\n",
"\n",
"from typing import Optional, List, Dict, Union\n",
"\n",
"import torch\n",
"import torch.nn as nn\n",
"import torch.nn.functional as F\n",
"from torch import Tensor\n",
"from torch import optim\n",
"from torch.utils.data import random_split, DataLoader, TensorDataset\n",
"\n",
"from pathlib import Path\n",
"\n",
"import transformers\n",
"\n",
"import lightning.pytorch as pl\n",
"# from dataclasses import dataclass\n",
"\n",
"from sklearn.linear_model import LogisticRegression\n",
"from sklearn.metrics import f1_score, roc_auc_score, accuracy_score\n",
"from sklearn.preprocessing import RobustScaler\n",
"\n",
"from tqdm.auto import tqdm\n",
"import os\n",
"\n",
"from loguru import logger\n",
"logger.add(os.sys.stderr, format=\"{time} {level} {message}\", level=\"INFO\")\n",
"\n",
"\n",
"\n",
"transformers.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from src.helpers.lightning import read_metrics_csv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dataset"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Dataset({\n",
" features: ['hs0', 'scores0', 'hs1', 'scores1', 'ds_index', 'answer', 'question', 'answer_choices', 'template_name', 'label_true', 'label_instructed', 'instructed_to_lie', 'sys_instr_name', 'prompt_truncated', 'choice_probs0', 'ans0', 'choice_probs1', 'ans1', 'expanded_choice_probs0', 'expanded_ans0', 'expanded_choice_probs1', 'expanded_ans1', 'txt_ans0', 'txt_ans1'],\n",
" num_rows: 9002\n",
"})"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datasets import load_from_disk, concatenate_datasets\n",
"fs = [\n",
" '../.ds/HuggingFaceH4starchat_beta_imdb_train_9002',\n",
"]\n",
"\n",
"# './.ds/HuggingFaceH4starchat_beta-None-N_8000-ns_3-mc_0.2-2ffc1e'\n",
"ds1 = concatenate_datasets([load_from_disk(f) for f in fs])\n",
"ds1"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"from src.datasets.load import ds2df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Filter"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# lets select only the ones where\n",
"df = ds2df(ds1)\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# # just select the question where the model knows the answer. \n",
"# d = df.query('version==\"truth\"').set_index(\"index\")\n",
"# # these are the ones where it got it right when asked to tell the truth\n",
"# known_indices = d[d.llm_ans==d.true_answer].index\n",
"\n",
"# # convert to row numbers, and use datasets to select\n",
"# known_rows = df['index'].isin(known_indices)\n",
"# known_rows_i = df[known_rows].index\n",
"\n",
"# also restrict it to significant permutations. That is monte carlo dropout pairs, where the answer changes by more than X%\n",
"m = np.abs(df.ans0-df.ans1)>0.1\n",
"significant_rows = m[m].index\n",
"\n",
"# allowed_rows_i = set(known_rows_i).intersection(significant_rows)\n",
"allowed_rows_i = significant_rows\n",
"ds = ds1.select(allowed_rows_i)\n",
"print(f\"selected rows are {len(ds)/len(ds1):2.2%}\")\n",
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transform: Normalize by activation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# N = 1000\n",
"# small_ds = ds.select(range(N))\n",
"# b = N\n",
"# hs0 = small_ds['hs0'].reshape((b, -1))\n",
"\n",
"# scaler = RobustScaler()\n",
"# hs1 = scaler.fit_transform(hs0)\n",
"\n",
"# def normalize_hs(hs0, hs1):\n",
"# shape=hs0.shape\n",
"# b = len(hs0)\n",
"# hs0 = scaler.transform(hs0.reshape((b, -1))).reshape(shape)\n",
"# hs1 = scaler.transform(hs1.reshape((b, -1))).reshape(shape)\n",
"# return {'hs0':hs0, 'hs1': hs1}\n",
"\n",
"# # Plot\n",
"# plt.hist(hs0.flatten(), bins=155, range=[-5, 5], label='before', histtype='step')\n",
"# plt.hist(hs1.flatten(), bins=155, range=[-5, 5], label='after', histtype='step')\n",
"# plt.legend()\n",
"# plt.show()\n",
"\n",
"# # # Test\n",
"# # small_dataset = ds.select(range(4))\n",
"# # small_dataset.map(normalize_hs, batched=True, batch_size=2, input_columns=['hs0', 'hs1'])\n",
"\n",
"# # run\n",
"# ds = ds.map(normalize_hs, batched=True, input_columns=['hs0', 'hs1'])\n",
"# ds"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Lightning DataModule"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = ds2df(ds)\n",
"df.head(4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ds?"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"What are we detecting? If the right example of the pair is more deceptive.\n",
"\n",
"Now it's only deceptive if\n",
"- it was asked to lie\n",
"- it knows the truth\n",
"- it gave the wrong answer (around 10% of the time)( it's hard to get these models to lie by encouragement rather than instruction)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from src.helpers import switch2bool, bool2switch\n",
"from src.datasets.dm import imdbHSDataModule\n",
"\n",
"def compute_distance2(df):\n",
" \"\"\"distance between ans1 and ans2.\"\"\"\n",
" true_switch_sign = df.label_true*2-1 # switch sign to desired answer. with this we ask which is more true\n",
" # otherwise we ask which is more positive\n",
" distance = (df.expanded_ans1-df.expanded_ans0) * true_switch_sign\n",
" return distance\n",
"\n",
"class imdbHSDataModule2(imdbHSDataModule):\n",
" def setup(self, stage: str):\n",
" super().setup(stage)\n",
" self.ans0 = self.df['expanded_ans0'].values\n",
" self.ans1 = self.df['expanded_ans1'].values\n",
" \n",
" y_cls = compute_distance2(self.df)\n",
" self.y = y_cls.values\n",
" self.df['y'] = y_cls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"batch_size = 120\n",
"# test and cache\n",
"dm = imdbHSDataModule2(ds, batch_size=batch_size)\n",
"dm.setup('train')\n",
"\n",
"dl_val = dm.val_dataloader()\n",
"dl_train = dm.train_dataloader()\n",
"len(dl_train), len(dl_val)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"b = next(iter(dl_train))\n",
"x0, x1, y = b\n",
"x0.shape"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data prep\n",
"\n",
"We do two inferences on the same inputs. Since we have dropout enabled, even during inference, we get two slightly different hidden states `hs1` and `hs2`, and two slightly different probabilities for our yes and no output tokens `p1` `p2`. We also have the true answer `t`\n",
"\n",
"So there are a few ways we can set up the problem. \n",
"\n",
"We can vary x:\n",
"- `model(hs1)-model(hs2)=y`\n",
"- `model(hs1-hs2)==y`\n",
"\n",
"And we can try differen't y's:\n",
"- direction with a ranked loss. This could be unsupervised.\n",
"- magnitude with a regression loss\n",
"- vector (direction and magnitude) with a regression loss"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# QC: Linear supervised probes\n",
"\n",
"\n",
"Let's verify that the model's representations are good\n",
"\n",
"Before trying CCS, let's make sure there exists a direction that classifies examples as true vs false with high accuracy; if supervised logistic regression accuracy is bad, there's no hope of unsupervised CCS doing well.\n",
"\n",
"Note that because logistic regression is supervised we expect it to do better but to have worse generalisation that equivilent unsupervised methods. However in this case CSS is using a deeper model so it is more complicated.\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try a classification of direction to truth"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# dm.y"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = len(df)\n",
"\n",
"# Define X and y\n",
"X = (dm.hs1-dm.hs0).reshape((n, -1))#/dm.y[:, None]\n",
"y = dm.y>0\n",
"\n",
"# split\n",
"n = len(y)\n",
"max_rows = 300\n",
"print('split size', n//2)\n",
"X_train, X_test = X[:n//2], X[n//2:]\n",
"y_train, y_test = y[:n//2], y[n//2:]\n",
"X_train = X_train[:max_rows]\n",
"y_train = y_train[:max_rows]\n",
"X_test = X_test[:max_rows]\n",
"y_test = y_test[:max_rows]\n",
"\n",
"# scale\n",
"scaler = RobustScaler()\n",
"scaler.fit(X_train)\n",
"X_train2 = scaler.transform(X_train)\n",
"X_test2 = scaler.transform(X_test)\n",
"print('lr')\n",
"\n",
"lr = LogisticRegression(class_weight=\"balanced\", penalty=\"l2\", max_iter=100)\n",
"lr.fit(X_train2, y_train>0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# y.mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Logistic cls acc: {:2.2%} [TRAIN]\".format(lr.score(X_train2, y_train>0)))\n",
"print(\"Logistic cls acc: {:2.2%} [TEST]\".format(lr.score(X_test2, y_test>0)))\n",
"\n",
"m = df['instructed_to_lie'][n//2:][:max_rows]\n",
"y_test_pred = lr.predict(X_test2)\n",
"acc_w_lie = ((y_test_pred[m]>0)==(y_test[m]>0)).mean()\n",
"acc_wo_lie = ((y_test_pred[~m]>0)==(y_test[~m]>0)).mean()\n",
"print(f'test acc w lie {acc_w_lie:2.2%}')\n",
"print(f'test acc wo lie {acc_wo_lie:2.2%}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# primary_baseline = roc_auc_score(y_test>0, y_test_pred)\n",
"# primary_baseline"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# LightningModel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from src.probes.conv import PLConvProbe"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# quiet please\n",
"torch.set_float32_matmul_precision('medium')\n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\", \".*does not have many workers.*\")\n",
"warnings.filterwarnings(\"ignore\", \".*F-score.*\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prep dataloader/set"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dl_train = dm.train_dataloader()\n",
"dl_val = dm.val_dataloader()\n",
"b = next(iter(dl_train))\n",
"# init the model\n",
"max_epochs = 82\n",
"c_in = b[0].shape[1]\n",
"print(b[0].shape)\n",
"net = PLConvProbe(c_in=c_in, total_steps=max_epochs*len(dl_train), depth=4, hs=32, lr=3e-3, \n",
" # weight_decay=1e-4, \n",
" dropout=0.1, \n",
" input_dropout=0.1,\n",
" )\n",
"trainer = pl.Trainer(precision=\"bf16-mixed\",\n",
" \n",
" gradient_clip_val=20,\n",
" max_epochs=max_epochs, log_every_n_steps=5)\n",
"trainer.fit(model=net, train_dataloaders=dl_train, val_dataloaders=dl_val)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Read hist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_hist = read_metrics_csv(trainer.logger.experiment.metrics_file_path).ffill().bfill()\n",
"df_hist"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for key in ['loss']:\n",
" df_hist[[c for c in df_hist.columns if key in c]].plot(logy=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for key in ['acc']:\n",
" df_hist[[c for c in df_hist.columns if key in c]].plot()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Predict"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dl_test = dm.test_dataloader()\n",
"rs = trainer.test(net, dataloaders=[dl_train, dl_val, dl_test])\n",
"rs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dl_test = dm.test_dataloader()\n",
"r = trainer.predict(net, dataloaders=dl_test)\n",
"y_test_pred = np.concatenate(r)\n",
"y_test_pred.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df_test = dm.df.iloc[dm.splits['test'][0]:].copy()\n",
"y_true = dl_test.dataset.tensors[2].numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Make a prediction dataframe with everything in it\n",
"df_test = dm.df.iloc[dm.splits['test'][0]:].copy()\n",
"df_test['probe_pred'] = y_test_pred>0\n",
"y_test_pred_bool = np.clip(switch2bool(y_test_pred), 0 ,1)\n",
"df_test['probe_prob'] = y_test_pred_bool\n",
"df_test['llm_prob'] = (df_test['ans0']+df_test['ans1'])/2\n",
"df_test['llm_ans'] = df_test['llm_prob']>0.5\n",
"df_test['conf'] = (df_test['ans0']-df_test['ans1']).abs()\n",
"df_test['y'] = df_test['y']>0\n",
"\n",
"y_true = dl_test.dataset.tensors[2].numpy()\n",
"assert ((df_test['y'].values>0.5)==(y_true>0)).all(), 'check it all lines up'\n",
"\n",
"df_test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_acc_subset(df, query):\n",
" df_s = df.query(query)\n",
" acc = (df_s['probe_pred']==df_s['y']).mean()\n",
" print(f\"acc={acc:2.2%} [{query}]\")\n",
" return acc\n",
" \n",
"print('probe results on subsets of the data')\n",
"get_acc_subset(df_test, 'instructed_to_lie==True') # it was ph told to lie\n",
"get_acc_subset(df_test, 'instructed_to_lie==False') # it was told not to lie\n",
"get_acc_subset(df_test, 'llm_ans==label_true') # the llm gave the true ans\n",
"get_acc_subset(df_test, 'llm_ans==label_instructed') # the llm gave the desired ans\n",
"get_acc_subset(df_test, 'instructed_to_lie==True & llm_ans==label_instructed') # it was told to lie, and it did lie\n",
"get_acc_subset(df_test, 'instructed_to_lie==True & llm_ans!=label_instructed')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# RESULTS"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"acc = (df_test['y']==(y_test_pred_bool>0.5)).mean()\n",
"\n",
"# print(f\" PRIMARY BASELINE roc_auc={primary_baseline:2.2%} from linear classifier\")\n",
"print(f\"⭐PRIMARY METRIC⭐ acc={acc:2.2%} from probe\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Out of sample\n",
"\n",
"Lets see how far it generalizes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def try_fine_tune(dm):\n",
" dl_train = dm.train_dataloader()\n",
" dl_val = dm.val_dataloader()\n",
" dl_test = dm.test_dataloader()\n",
" b = next(iter(dl_train))\n",
" max_epochs = 42\n",
" c_in = b[0].shape[1]\n",
" print(b[0].shape)\n",
" net = PLConvProbe(c_in=c_in, total_steps=max_epochs*len(dl_train), depth=5, hs=128, lr=3e-3, dropout=0.1, input_dropout=0.1)\n",
" trainer = pl.Trainer(precision=\"bf16-mixed\",\n",
" \n",
" gradient_clip_val=20,\n",
" max_epochs=max_epochs, log_every_n_steps=5)\n",
" trainer.fit(model=net, train_dataloaders=dl_train, val_dataloaders=dl_val)\n",
" df_hist = read_metrics_csv(trainer.logger.experiment.metrics_file_path).ffill().bfill()\n",
" rs = trainer.test(net, dataloaders=[dl_train, dl_val, dl_test])\n",
" return df_hist, rs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"oos_dataset_fs = [\n",
" '../.ds/model-starchat-beta_ds-EleutherAItruthful-qa-binary_format-tqa-a-b-simple-prompt_N807_2shots_cd0a7f',\n",
" '../.ds/model-starchat-beta_ds-EleutherAItruthful-qa-binary_format-tqa-sphinx-prompt_N807_2shots_cd0a7f', \n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"batch_size = 12\n",
"for f in oos_dataset_fs:\n",
" print(f)\n",
" ds2a = load_from_disk(f)\n",
"\n",
" # restrict it to significant permutations. That is monte carlo dropout pairs, where the answer changes by more than X%\n",
" df = ds2df(ds2a)\n",
" m = np.abs(df.ans0-df.ans1)>0.1\n",
" significant_rows = m[m].index\n",
"\n",
" # allowed_rows_i = set(known_rows_i).intersection(significant_rows)\n",
" allowed_rows_i = significant_rows\n",
" ds2 = ds2a.select(allowed_rows_i)\n",
" print(f\"selected rows are {len(ds2)/len(ds2a):2.2%}\")\n",
" print(len(ds2))\n",
"\n",
" dm2 = imdbHSDataModule(ds2, batch_size=batch_size)\n",
" dm2.setup('train')\n",
"\n",
" dl_val2 = dm2.val_dataloader()\n",
" dl_train2 = dm2.train_dataloader()\n",
" dl_test2 = dm2.test_dataloader()\n",
" print(len(dl_train2), len(dl_val2), len(dl_test2))\n",
" rs2 = trainer.test(net, dataloaders=[dl_train2, dl_val2, dl_test2]) \n",
" \n",
" df_hist2, rs2b = try_fine_tune(dm2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "dlk2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -8,7 +8,7 @@ from datasets.arrow_dataset import Dataset
def compute_distance(df):
"""distance between ans1 and ans2."""
true_switch_sign = df.label*2-1 # switch sign to desired answer. with this we ask which is more true
true_switch_sign = df.label_true*2-1 # switch sign to desired answer. with this we ask which is more true
# otherwise we ask which is more positive
distance = (df.ans1-df.ans0) * true_switch_sign
return distance