-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consolidates with SAE class load_legacy function & adds test
- Loading branch information
1 parent
fda2b57
commit 0f85ded
Showing
3 changed files
with
59 additions
and
22 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,39 @@ | ||
import pathlib | ||
import shutil | ||
|
||
import pytest | ||
import torch | ||
|
||
from sae_lens.training.sparse_autoencoder import SparseAutoencoder | ||
from sae_lens.toolkit import pretrained_saes | ||
from sae_lens.training.config import LanguageModelSAERunnerConfig | ||
|
||
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) |