-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from evanhanders/old_to_new
Adds function that converts old .pt pretrained SAEs to new folder format
- Loading branch information
Showing
3 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pathlib | ||
import shutil | ||
|
||
import torch | ||
|
||
from sae_lens.toolkit import pretrained_saes | ||
from sae_lens.training.config import LanguageModelSAERunnerConfig | ||
from sae_lens.training.sparse_autoencoder import SparseAutoencoder | ||
|
||
|
||
def test_convert_old_to_modern_saelens_format(): | ||
out_dir = pathlib.Path("unit_test_tmp") | ||
out_dir.mkdir(exist_ok=True) | ||
legacy_out_file = str(out_dir / "test.pt") | ||
new_out_folder = str(out_dir / "test") | ||
|
||
# Make an SAE, save old version | ||
cfg = LanguageModelSAERunnerConfig( | ||
dtype=torch.float32, | ||
hook_point="blocks.0.hook_mlp_out", | ||
) | ||
old_sae = SparseAutoencoder(cfg) | ||
old_sae.save_model_legacy(legacy_out_file) | ||
|
||
# convert file format | ||
pretrained_saes.convert_old_to_modern_saelens_format( | ||
legacy_out_file, new_out_folder | ||
) | ||
|
||
# Load from new converted file | ||
new_sae = SparseAutoencoder.load_from_pretrained(new_out_folder) | ||
shutil.rmtree(out_dir) # cleanup | ||
|
||
# Test similarity | ||
assert torch.allclose(new_sae.W_enc, old_sae.W_enc) | ||
assert torch.allclose(new_sae.W_dec, old_sae.W_dec) |