44from tqdm import tqdm
55from sae_lens .config import LOCAL_SAE_MODEL_PATH
66
7- #base_dir = '/data02/tuwenming/SAE/Pretrained_SAEs/sae-llama-3-8b-32x-v2/layers.{}/'
8- base_dir = LOCAL_SAE_MODEL_PATH + '/EleutherAI/sae-llama-3-8b-32x/layers.{}/'
7+ # base_dir = '/data02/tuwenming/SAE/Pretrained_SAEs/sae-llama-3-8b-32x-v2/layers.{}/'
8+ base_dir = LOCAL_SAE_MODEL_PATH + "/EleutherAI/sae-llama-3-8b-32x/layers.{}/"
9+
910
1011def delete_original_sae (layer_index ):
1112 # 构建文件路径
1213 layer_dir = base_dir .format (layer_index )
13- sae_file_path = os .path .join (layer_dir , ' sae.safetensors' )
14-
14+ sae_file_path = os .path .join (layer_dir , " sae.safetensors" )
15+
1516 # 检查文件是否存在
1617 if os .path .exists (sae_file_path ):
1718 os .remove (sae_file_path )
1819 print (f"Deleted { sae_file_path } " )
1920 else :
2021 print (f"File { sae_file_path } does not exist, skipping." )
2122
23+
2224def process_layer (layer_index ):
2325 # 构建文件路径
2426 layer_dir = base_dir .format (layer_index )
25- sae_file_path = os .path .join (layer_dir , ' sae.safetensors' )
26- cfg_file_path = os .path .join (layer_dir , ' cfg.json' )
27- new_sae_file_path = os .path .join (layer_dir , ' sae_weights.safetensors' )
28-
27+ sae_file_path = os .path .join (layer_dir , " sae.safetensors" )
28+ cfg_file_path = os .path .join (layer_dir , " cfg.json" )
29+ new_sae_file_path = os .path .join (layer_dir , " sae_weights.safetensors" )
30+
2931 # 处理 sae.safetensors 文件
3032 state_dict = load_file (sae_file_path )
3133 new_state_dict = {}
@@ -38,33 +40,36 @@ def process_layer(layer_index):
3840 # 对权重进行转置
3941 new_state_dict [new_key ] = state_dict [key ].transpose (0 , 1 ).contiguous ()
4042 continue # 跳过下面的赋值语句,避免重复添加
41-
43+
4244 new_state_dict [new_key ] = state_dict [key ]
43-
45+
4446 # 保存修改后的state_dict到新的safetensors文件
4547 save_file (new_state_dict , new_sae_file_path )
46-
48+
4749 # 处理 cfg.json 文件
48- with open (cfg_file_path , 'r' ) as cfg_file :
50+ with open (cfg_file_path , "r" ) as cfg_file :
4951 cfg_data = json .load (cfg_file )
50-
52+
5153 # 添加新的键值对
52- cfg_data .update ({
53- "d_sae" : 131072 ,
54- "dtype" : "float32" ,
55- "dataset_path" : "togethercomputer/RedPajama-Data-1T-Sample" ,
56- "context_size" : 256 ,
57- "model_name" : "meta-llama/Meta-Llama-3-8B" ,
58- "hook_name" : f"blocks.{ layer_index } .hook_resid_post" ,
59- "hook_layer" : str (layer_index ),
60- "hook_head_index" : None
61- })
62-
54+ cfg_data .update (
55+ {
56+ "d_sae" : 131072 ,
57+ "dtype" : "float32" ,
58+ "dataset_path" : "togethercomputer/RedPajama-Data-1T-Sample" ,
59+ "context_size" : 256 ,
60+ "model_name" : "meta-llama/Meta-Llama-3-8B" ,
61+ "hook_name" : f"blocks.{ layer_index } .hook_resid_post" ,
62+ "hook_layer" : str (layer_index ),
63+ "hook_head_index" : None ,
64+ }
65+ )
66+
6367 # 将修改后的内容写回 cfg.json
64- with open (cfg_file_path , 'w' ) as cfg_file :
68+ with open (cfg_file_path , "w" ) as cfg_file :
6569 json .dump (cfg_data , cfg_file , indent = 4 )
6670
71+
6772# 遍历所有层次 (i 从 0 到 30)
68- for i in tqdm (range (3 ,30 ,1 )):
73+ for i in tqdm (range (3 , 30 , 1 )):
6974 process_layer (i )
70- delete_original_sae (i )
75+ delete_original_sae (i )
0 commit comments