Skip to content

fix(sae): detach the residual target of the AuxK loss - #221

Open
flyingdoubleG wants to merge 1 commit into
OpenMOSS:devfrom
flyingdoubleG:fix/auxk-detach-residual
Open

flyingdoubleG wants to merge 1 commit into
OpenMOSS:devfrom
flyingdoubleG:fix/auxk-detach-residual

Conversation

@flyingdoubleG

Copy link
Copy Markdown

Summary

SparseDictionary.compute_loss builds the AuxK target as e = label - reconstructed without detaching it. AuxK is meant to fit the dead latents to the main reconstruction's residual, with gradient reaching only the dead latents. The TopK SAE paper (Gao et al., 2024) and its reference code use x - recons.detach(), and sparsify does the same with e.detach().

Because e keeps its graph here, l_aux also back-propagates through reconstructed. As a result:

  • the alive latents' encoder columns and decoder rows receive gradient from l_aux;
  • so does the decoder bias;
  • the main reconstruction is pulled toward x - aux_reconstructed instead of x.

Reproduction

A TopK SAE (d_model 8, d_sae 32, k 2) with every other latent marked dead, backpropagating l_aux only:

before:  grad on ALIVE decoder rows 131.6 | ALIVE encoder cols 148.6 | b_D 33.2 | DEAD decoder rows 593.7
after:   grad on ALIVE decoder rows 0     | ALIVE encoder cols 0     | b_D none | DEAD decoder rows > 0

Changes

  • Detach e.
  • Add tests/unit/test_sae.py::test_auxk_loss_only_trains_dead_latents. It fails before this change and passes after.

Testing

  • pytest tests/unit: 169 passed, 1 skipped. The one failure, test_activation_buffer_distributed, needs a working gloo/NCCL process group, which my Windows environment doesn't have.
  • ruff check and ruff format --check are clean.

The AuxK loss fits the dead latents to the main reconstruction's residual
e = x - x_hat (Gao et al., 2024; the reference implementation uses
x - recons.detach()). Without the detach, l_aux also back-propagated
through x_hat, sending gradient to the alive latents and the decoder bias
and pulling the main reconstruction toward x - aux_reconstruction.
@flyingdoubleG
flyingdoubleG changed the base branch from main to dev October 24, 2026 04:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant