peal.sparse_dictionaries.msae_decomposition.MSAEDecomposition¶
- class peal.sparse_dictionaries.msae_decomposition.MSAEDecomposition[source]¶
Bases:
SparseDictionarySparse dictionary wrapper around MSAE (Matryoshka Sparse Autoencoder).
Supports pretrained MSAE checkpoints as well as fitting from scratch on dataloaders. When
config.ckpt_urlis set the constructor downloads and loads the pretrained model immediately, falling back to trainable mode (with a printed message) if that fails.- Parameters:
config (
MSAEDecompositionConfig, optional) – Checkpoint URLs and training hyper-parameters.- Variables:
sae (
torch.nn.ModuleorNone) – The MSAESAEwrapper (pretrained) or a trainedAutoencoder.vocabulary (
listofstrorNone) – One concept name per latent, from the concept interpreter.mu (
torch.TensororNone) – Activation mean subtracted before encoding a self-trained model.
Methods
Encodes activations into sparse MSAE activations.
Train MSAE from raw activation tensor X [N, act_size].
Encode every batch of the loaders with
feature_extractor, then fit.Returns decoder weights of shape [n_inputs, n_components].
Return the per-latent concept names, or
Nonewhen not loaded.Rebuild a self-trained autoencoder from a
save_on_disk()file.Write
state_dict,muand the config topathwithtorch.save.Attributes
config- __init__(config=pydantic.BaseModel)[source]¶
Pick a device and, if
config.ckpt_urlis set, load the pretrained MSAE.A failure to download or load the checkpoint is caught and reported; the instance then stays in trainable mode with
saeunset.
- encode(x, topk=64)[source]¶
Encodes activations into sparse MSAE activations.
- Parameters:
x (
torch.Tensor) – Activations of shape(n, act_size).topk (
int, optional) – Number of active latents for the pretrainedSAE.encode; ignored by a self-trained autoencoder, which instead subtractsmufirst. Default 64.
- Returns:
Sparse latents of shape
(n, n_components).- Return type:
- fit(X, y=None)[source]¶
Train MSAE from raw activation tensor X [N, act_size].
Centres
X(storing the mean inmu), builds anAutoencoderorMatryoshkaAutoencodersized byconfig.n_components(default2 * act_size) and trains it with AdamW and an MSESAELossforconfig.epochsepochs, printing the loss every five epochs.- Parameters:
X (
torch.Tensor) – Activations of shape(N, act_size).y (
torch.Tensor, optional) – Ignored; accepted for interface symmetry.
- fit_from_dataloaders(dataloaders, feature_extractor=<function MSAEDecomposition.<lambda>>)[source]¶
Encode every batch of the loaders with
feature_extractor, then fit.- Parameters:
dataloaders (
iterableoftorch.utils.data.DataLoader) – Loaders whose first batch element is the input.feature_extractor (
callable, optional) – Maps a batch to(batch, act_size)activations; identity by default.
- save_on_disk(path)[source]¶
Write
state_dict,muand the config topathwithtorch.save.- Parameters:
path (str)
- load_from_disk(path)[source]¶
Rebuild a self-trained autoencoder from a
save_on_disk()file.Does nothing when
pathdoes not exist. The architecture is re-derived from the current config (act_size,n_components,activation,use_matryoshka), not from the saved config.- Parameters:
path (str)