|
| 1 | +from typing import Callable |
| 2 | + |
| 3 | +import torch.nn as nn |
| 4 | +from transformers import PreTrainedModel |
| 5 | + |
| 6 | +from delphi.config import RunConfig |
| 7 | + |
| 8 | +from .custom.gemmascope import load_gemma_autoencoders |
| 9 | +from .load_sparsify import load_sparsify_hooks, load_sparsify_sparse_coders |
| 10 | + |
| 11 | + |
| 12 | +def load_hooks_sparse_coders( |
| 13 | + model: PreTrainedModel, |
| 14 | + run_cfg: RunConfig, |
| 15 | + compile: bool = False, |
| 16 | +) -> dict[str, Callable]: |
| 17 | + """ |
| 18 | + Load sparse coders for specified hookpoints. |
| 19 | +
|
| 20 | + Args: |
| 21 | + model (PreTrainedModel): The model to load sparse coders for. |
| 22 | + run_cfg (RunConfig): The run configuration. |
| 23 | +
|
| 24 | + Returns: |
| 25 | + dict[str, Callable]: A dictionary mapping hookpoints to sparse coders. |
| 26 | + """ |
| 27 | + |
| 28 | + # Add SAE hooks to the model |
| 29 | + if "gemma" not in run_cfg.sparse_model: |
| 30 | + hookpoint_to_sparse_encode = load_sparsify_hooks( |
| 31 | + model, |
| 32 | + run_cfg.sparse_model, |
| 33 | + run_cfg.hookpoints, |
| 34 | + compile=compile, |
| 35 | + ) |
| 36 | + else: |
| 37 | + # model path will always be of the form google/gemma-scope-<size>-pt-<type>/ |
| 38 | + # where <size> is the size of the model and <type> is either res or mlp |
| 39 | + model_path = "google/" + run_cfg.sparse_model.split("/")[1] |
| 40 | + type = model_path.split("-")[-1] |
| 41 | + # we can use the hookpoints to determine the layer, size and l0, |
| 42 | + # because the module is determined by the model name |
| 43 | + # the hookpoint should be in the format |
| 44 | + # layer_<layer>/width_<sae_size>/average_l0_<l0> |
| 45 | + layers = [] |
| 46 | + l0s = [] |
| 47 | + sae_sizes = [] |
| 48 | + for hookpoint in run_cfg.hookpoints: |
| 49 | + layer = int(hookpoint.split("/")[0].split("_")[1]) |
| 50 | + sae_size = hookpoint.split("/")[1].split("_")[1] |
| 51 | + l0 = int(hookpoint.split("/")[2].split("_")[2]) |
| 52 | + layers.append(layer) |
| 53 | + sae_sizes.append(sae_size) |
| 54 | + l0s.append(l0) |
| 55 | + |
| 56 | + hookpoint_to_sparse_encode = load_gemma_autoencoders( |
| 57 | + model_path=model_path, |
| 58 | + ae_layers=layers, |
| 59 | + average_l0s=l0s, |
| 60 | + sizes=sae_sizes, |
| 61 | + type=type, |
| 62 | + dtype=model.dtype, |
| 63 | + device=model.device, |
| 64 | + ) |
| 65 | + |
| 66 | + return hookpoint_to_sparse_encode |
| 67 | + |
| 68 | + |
| 69 | +def load_sparse_coders( |
| 70 | + model: PreTrainedModel, |
| 71 | + run_cfg: RunConfig, |
| 72 | + compile: bool = False, |
| 73 | +) -> dict[str, nn.Module]: |
| 74 | + """ |
| 75 | + Load sparse coders for specified hookpoints. |
| 76 | +
|
| 77 | + Args: |
| 78 | + model (PreTrainedModel): The model to load sparse coders for. |
| 79 | + run_cfg (RunConfig): The run configuration. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + dict[str, Callable]: A dictionary mapping hookpoints to sparse coders. |
| 83 | + """ |
| 84 | + |
| 85 | + # Add SAE hooks to the model |
| 86 | + if "gemma" not in run_cfg.sparse_model: |
| 87 | + hookpoint_to_sparse_model = load_sparsify_sparse_coders( |
| 88 | + model, |
| 89 | + run_cfg.sparse_model, |
| 90 | + run_cfg.hookpoints, |
| 91 | + compile=compile, |
| 92 | + ) |
| 93 | + else: |
| 94 | + # model path will always be of the form google/gemma-scope-<size>-pt-<type>/ |
| 95 | + # where <size> is the size of the model and <type> is either res or mlp |
| 96 | + model_path = "google/" + run_cfg.sparse_model.split("/")[1] |
| 97 | + type = model_path.split("-")[-1] |
| 98 | + # we can use the hookpoints to determine the layer, size and l0, |
| 99 | + # because the module is determined by the model name |
| 100 | + # the hookpoint should be in the format |
| 101 | + # layer_<layer>/width_<sae_size>/average_l0_<l0> |
| 102 | + layers = [] |
| 103 | + l0s = [] |
| 104 | + sae_sizes = [] |
| 105 | + for hookpoint in run_cfg.hookpoints: |
| 106 | + layer = int(hookpoint.split("/")[0].split("_")[1]) |
| 107 | + sae_size = hookpoint.split("/")[1].split("_")[1] |
| 108 | + l0 = int(hookpoint.split("/")[2].split("_")[2]) |
| 109 | + layers.append(layer) |
| 110 | + sae_sizes.append(sae_size) |
| 111 | + l0s.append(l0) |
| 112 | + |
| 113 | + hookpoint_to_sparse_model = load_gemma_autoencoders( |
| 114 | + model_path=model_path, |
| 115 | + ae_layers=layers, |
| 116 | + average_l0s=l0s, |
| 117 | + sizes=sae_sizes, |
| 118 | + type=type, |
| 119 | + dtype=model.dtype, |
| 120 | + device=model.device, |
| 121 | + ) |
| 122 | + |
| 123 | + return hookpoint_to_sparse_model |
0 commit comments