Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,741 Bytes
85e172b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
import os
import copy
import torch
import numpy as np
from util import utils
from collections import Counter
from . import edit_utils
from util import extraction
def is_close_to_zeros(x, tol=1e-4, hparams=None):
""" check if a torch tensor is close to zero
"""
if hparams['activation'] in ['gelu', 'gelu_org']:
return x == 0
else:
return torch.abs(x) <= tol
def typeI_to_sphere(tensor, norm_learnables):
""" Project back to sphere for type I MLP component (e.g. from models gpt2-xl and gpt-j)
"""
if (tensor is None) or (norm_learnables is None): return tensor
if len(tensor.shape) == 1:
d = len(tensor)
else:
d = tensor.shape[1]
if type(tensor) == np.ndarray:
return (copy.deepcopy(tensor) - norm_learnables['norm_bias'].cpu().numpy() ) \
/ np.sqrt(d) / norm_learnables['norm_weight'].cpu().numpy()
else:
return (torch.clone(tensor) - norm_learnables['norm_bias']) \
/ np.sqrt(d) / norm_learnables['norm_weight']
def typeII_to_sphere(tensor, norm_learnables):
""" Project back to sphere for type II MLP component (e.g. from models gemma and llama-2)
"""
if (tensor is None) or (norm_learnables is None): return tensor
if len(tensor.shape) == 1:
d = len(tensor)
else:
d = tensor.shape[1]
if type(tensor) == np.ndarray:
return copy.deepcopy(tensor) / norm_learnables['norm_weight'].cpu().numpy() / np.sqrt(d)
else:
return torch.clone(tensor) / norm_learnables['norm_weight'] / np.sqrt(d)
def back_to_sphere(tensor, model_name, norm_learnables):
if type(model_name) != str:
model_name = model_name['model_name']
if model_name in edit_utils.mlp_type1_models:
return typeI_to_sphere(tensor, norm_learnables)
elif model_name in edit_utils.mlp_type2_models:
return typeII_to_sphere(tensor, norm_learnables)
else:
raise ValueError('Invalid model type for:', model_name)
def typeI_to_feature_space(tensor, norm_learnables):
if (tensor is None) or (norm_learnables is None): return tensor
if len(tensor.shape) == 1:
d = len(tensor)
else:
d = tensor.shape[1]
if type(tensor) == np.ndarray:
return (copy.deepcopy(tensor) * np.sqrt(d) * norm_learnables['norm_weight'].cpu().numpy()) \
+ norm_learnables['norm_bias'].cpu().numpy()
else:
return (torch.clone(tensor) * np.sqrt(d) * norm_learnables['norm_weight']) \
+ norm_learnables['norm_bias']
def typeII_to_feature_space(tensor, norm_learnables):
if (tensor is None) or (norm_learnables is None): return tensor
if len(tensor.shape) == 1:
d = len(tensor)
else:
d = tensor.shape[1]
if type(tensor) == np.ndarray:
return copy.deepcopy(tensor) * norm_learnables['norm_weight'].cpu().numpy() * np.sqrt(d)
else:
return torch.clone(tensor) * norm_learnables['norm_weight'] * np.sqrt(d)
def back_to_feature_space(tensor, hparams, norm_learnables):
if hparams['model_name'] in edit_utils.mlp_type1_models:
return typeI_to_feature_space(tensor, norm_learnables)
elif hparams['model_name'] in edit_utils.mlp_type2_models:
return typeII_to_feature_space(tensor, norm_learnables)
else:
raise ValueError('Invalid model type for:', hparams['model_name'])
def typeI_weight_and_bias_to_implant(
tset,
hparams,
other_features = None,
norm_learnables = None,
theta = 0.005,
):
""" Produce edited weights and biases for GPT-type MLP modules
"""
# remove part of normalisation to project back to surface of sphere
tau = typeI_to_sphere(tset['w1_input'], norm_learnables)
# compute key parameterts
Delta = hparams['Delta']
alpha = hparams['Delta'] / theta
d = len(tau)
# find weights and biases in spherical space
w = alpha * tau
b = alpha * (theta - torch.matmul(tau, tau))
# add projection back to sphere for input v
w = (1 / np.sqrt(d)) * w / norm_learnables['norm_weight']
b = b - torch.matmul(w, norm_learnables['norm_bias']).item()
other_params = {}
if other_features is not None:
# find activation function
activation = utils.load_activation(hparams['activation'])
# find target and other responses
r = torch.matmul(other_features, w) + b
t = torch.matmul(tset['w1_input'], w) + b
# check if other responses ~0 and target response positive
close_to_zero = torch.sum(
is_close_to_zeros(activation.forward(r.float()), hparams=hparams)
).item() == len(r)
target_pos = (t > 0).item()
# save params
other_params['good_gate'] = close_to_zero & target_pos
return w, b, other_params
def typeII_weight_and_bias_to_implant(
tset,
hparams,
other_features = None,
norm_learnables = None,
theta = 0.005,
):
""" Produce edited weights and biases for Llama-type and Mamba-type MLP modules
"""
# remove part of normalisation to project back to surface of sphere
tau = typeII_to_sphere(tset['w1_input'], norm_learnables)
prj_other_features = typeII_to_sphere(other_features, norm_learnables)
# compute key parameterts
Delta = hparams['Delta']
alpha = hparams['Delta'] / theta
d = len(tau)
# find weights and biases in spherical space
w = alpha * tau
b = alpha * (theta - torch.matmul(tau, tau))
# find all feautres others (subset) + target
basis_features = [
torch.unsqueeze(tau, dim=0),
prj_other_features
]
features = torch.unique(torch.cat(basis_features, dim=0), dim=0).float()
if len(features)<features.shape[1]:
raise AssertionError('Number of features less than dimensions!')
# define centre as trigger
m = tau.float()
# Center the features by subtracting the mean
centered_features = features - m
# Calculate the covariance matrix
C = torch.matmul(centered_features.T, centered_features) / (features.shape[0] - 1)
# compute least variance direction
v = torch.matmul(
torch.linalg.inv(C),
m
)
v = v /torch.norm(v)
# insert bias into least variance direction
w = typeII_to_sphere(w + v * (b/torch.matmul(v, m)), norm_learnables)
other_params = {}
# find activation function
activation = utils.load_activation(hparams['activation'])
# find target and other responses
r = torch.matmul(other_features, w.to(other_features.dtype))
t = torch.matmul(tset['w1_input'], w.to(other_features.dtype))
# check if other responses ~0 and target response positive
close_to_zero = torch.sum(
is_close_to_zeros(activation.forward(r.float()), hparams=hparams)
).item() == len(r)
target_pos = (t > 0).item()
# save params
other_params['good_gate'] = close_to_zero & target_pos
return w, None, other_params
def construct_weight_and_bias_to_implant(
tset,
hparams,
other_features = None,
norm_learnables = None,
theta = 0.005,
):
""" Produce edited weights and biases (automatically finds method based on MLP type)
"""
if hparams['mlp_type'] == 'type1':
_func = typeI_weight_and_bias_to_implant
elif hparams['mlp_type'] == 'type2':
_func = typeII_weight_and_bias_to_implant
else:
raise ValueError('Invalid mlp_type:', hparams['mlp_type'])
return _func(
tset,
hparams,
other_features = other_features,
norm_learnables = norm_learnables,
theta = theta,
) |