From 097f9b386da55092aae223792b3d1663a66cb3c0 Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Sun, 22 Sep 2024 21:32:42 -0400 Subject: [PATCH] fix(ci): update correct logic to check for action Signed-off-by: Aaron Pham --- .github/workflows/pr-preview.yml | 7 +- python/exo/notebooks/self_explanation.ipynb | 362 +++++++++++++------- 2 files changed, 244 insertions(+), 125 deletions(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index c6e093b..7844331 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -1,4 +1,3 @@ -# .github/workflows/preview.yml name: Deploy PR previews on: @@ -30,16 +29,16 @@ jobs: - name: Install Dependencies run: npm ci - name: Install and Build - if: github.event.action != 'closed' # You might want to skip the build if the PR has been closed + if: github.event.action != 'closed' run: npx quartz build - uses: rossjrw/pr-preview-action@430e3dfc1de8a8ae77e77d862d25676ef9db55d1 # ratchet:rossjrw/pr-preview-action@v1 - if: contains(['opened', 'reopened', 'synchronize'], github.event.action) + if: github.event.action != 'closed' with: source-dir: ./public/ custom-url: tinymorph.aarnphm.xyz action: deploy - uses: rossjrw/pr-preview-action@430e3dfc1de8a8ae77e77d862d25676ef9db55d1 # ratchet:rossjrw/pr-preview-action@v1 - if: github.event.action == "closed" && !github.event.pull_request.merged + if: github.event.action == 'closed' && !github.event.pull_request.merged with: source-dir: ./public/ custom-url: tinymorph.aarnphm.xyz diff --git a/python/exo/notebooks/self_explanation.ipynb b/python/exo/notebooks/self_explanation.ipynb index ec1bf61..f64dfa5 100644 --- a/python/exo/notebooks/self_explanation.ipynb +++ b/python/exo/notebooks/self_explanation.ipynb @@ -30,44 +30,32 @@ "from nnsight import LanguageModel\n", "from sae import Sae\n", "from transformers import AutoModelForCausalLM, AutoTokenizer\n", - "import torch" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "adc4ca03-7e2b-41e0-916b-616df493e892", - "metadata": {}, - "outputs": [], - "source": [ - "def load_model(name: str, /, *, trust_remote_code: bool = False) -> LanguageModel:\n", - " model = LanguageModel(\n", - " name, device_map='auto', trust_remote_code=trust_remote_code, low_cpu_mem_usage=True, torch_dtype=torch.float16\n", - " )\n", - " model.requires_grad_(False)\n", - " return model\n", - "\n", + "import torch\n", "\n", - "def load_manual_sae(name: str, hookpoint: str):\n", - " repo_path = pathlib.Path(hf_hub.snapshot_download(name, allow_patterns=f'{hookpoint}/*'))\n", - " if not ((hookpath := repo_path / hookpoint) / 'cfg.json').exists():\n", - " raise FileNotFoundError('require cfg.json to specify a specific layers')\n", - " with st.safe_open(str(hookpath / 'sae_weights.safetensors'), framework='pt') as stf:\n", - " return stf.get_tensor('W_dec')" + "from transformer_lens import HookedTransformer\n", + "from sae_lens import SAE\n", + "from sae_lens.toolkit.pretrained_saes import get_gpt2_res_jb_saes" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "id": "afb4f816-f570-444e-a805-43070128bc2d", "metadata": {}, "outputs": [], "source": [ "prompt = 'Logic is easy, but it is impossible to be logical to bitter end. It is considered truth if one'\n", + "\n", + "model_id = 'meta-llama/Meta-Llama-3.1-8B-Instruct'\n", + "\n", "feature = 6689\n", "scale = 20\n", "max_new_tokens = 40\n", - "num_return_sequences = 10" + "num_return_sequences = 10\n", + "\n", + "if torch.backends.mps.is_available(): device = \"mps\"\n", + "elif torch.cuda.is_available(): device = 'cuda'\n", + "else: device = 'cpu'" ] }, { @@ -79,7 +67,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "aeb6a09180da41c0a3d956752899ebcc", + "model_id": "f347b2c3f8ca4786b66588f316091a79", "version_major": 2, "version_minor": 0 }, @@ -96,65 +84,187 @@ "text": [ "Dropping extra args {'signed': False}\n" ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "be581fba30c34bbd898be012a3a8ac4f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "model.safetensors.index.json: 0%| | 0.00/23.9k [00:00)" + " device='mps:0', grad_fn=)" ] }, - "execution_count": 9, + "execution_count": 27, "metadata": {}, "output_type": "execute_result" } @@ -209,70 +363,36 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 33, + "id": "5ae1d336-d32d-491f-a954-2cb7a1436d47", + "metadata": {}, + "outputs": [], + "source": [ + "v = sae.W_dec[[6689]]\n", + "v = v / v.norm()\n", + "v = v * scale" + ] + }, + { + "cell_type": "code", + "execution_count": 35, "id": "f41ea6b1-58f4-40ad-9ccb-c4854f32f78d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\u001b[0;31mSignature:\u001b[0m \u001b[0msae\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;31mType:\u001b[0m Sae\n", - "\u001b[0;31mString form:\u001b[0m \n", - "Sae(\n", - " (encoder): Linear(in_features=4096, out_features=131072, bias=True)\n", - ")\n", - "\u001b[0;31mFile:\u001b[0m ~/workspace/capstone/.venv/lib/python3.12/site-packages/sae/sae.py\n", - "\u001b[0;31mDocstring:\u001b[0m \n", - "\u001b[0;31mClass docstring:\u001b[0m\n", - "Base class for all neural network modules.\n", - "\n", - "Your models should also subclass this class.\n", - "\n", - "Modules can also contain other Modules, allowing to nest them in\n", - "a tree structure. You can assign the submodules as regular attributes::\n", - "\n", - " import torch.nn as nn\n", - " import torch.nn.functional as F\n", - "\n", - " class Model(nn.Module):\n", - " def __init__(self):\n", - " super().__init__()\n", - " self.conv1 = nn.Conv2d(1, 20, 5)\n", - " self.conv2 = nn.Conv2d(20, 20, 5)\n", - "\n", - " def forward(self, x):\n", - " x = F.relu(self.conv1(x))\n", - " return F.relu(self.conv2(x))\n", - "\n", - "Submodules assigned in this way will be registered, and will have their\n", - "parameters converted too when you call :meth:`to`, etc.\n", - "\n", - ".. note::\n", - " As per the example above, an ``__init__()`` call to the parent class\n", - " must be made before assignment on the child.\n", - "\n", - ":ivar training: Boolean represents whether this module is in training or\n", - " evaluation mode.\n", - ":vartype training: bool\n", - "\u001b[0;31mInit docstring:\u001b[0m Initialize internal Module state, shared by both nn.Module and ScriptModule." + "torch.Size([1, 4096])" ] }, + "execution_count": 35, "metadata": {}, - "output_type": "display_data" + "output_type": "execute_result" } ], "source": [ - "sae?" + "It would trouble me if my child identified as trans.This question is required." ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a6471b16-970d-4f46-9860-ed397ee34459", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": {