Skip to content

Centralize soft-cap logic behind a shared helper and a named disabled…#1491

Merged
jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
delaidam:fix/1489-centralize-softcap-helper
Jul 7, 2026
Merged

Centralize soft-cap logic behind a shared helper and a named disabled…#1491
jlarson4 merged 1 commit into
TransformerLensOrg:devfrom
delaidam:fix/1489-centralize-softcap-helper

Conversation

@delaidam

@delaidam delaidam commented Jul 7, 2026

Copy link
Copy Markdown

Fixes #1489

Consolidates the tanh soft-capping (Gemma-2 style) used for attention scores
and output logits into apply_softcap() / softcap_enabled() in
transformer_lens/utilities/activation_functions.py, replacing five
independent cap * tanh(x / cap) implementations:

  • HookedTransformer.py - output-logits soft-cap
  • components/abstract_attention.py - attention-score soft-cap
  • model_bridge/sources/native/model.py - both attention and output-logits soft-cap
  • HookedTransformer.py + model_bridge/bridge.py - center_unembed compatibility checks

Also replaces the bare -1.0 disabled sentinel with a named
SOFTCAP_DISABLED constant in both HookedTransformerConfig and
TransformerBridgeConfig.

Pure refactor, no behavior change. Left the HF-native attention path
(generalized_components/position_embeddings_attention.py) and the
config-mapping sites (sources/_bridge_builder.py, sources/transformers.py)
untouched, per the issue's scope notes.

Verification

  • uv run mypy . - clean, 307 files
  • make unit-test - 3204 passed, 32 skipped, 9 xfailed, 0 failed
  • pytest (docstrings) on all changed files - clean
  • New unit tests in tests/unit/utilities/test_activation_functions.py,
    including a regression guard for the exact truthiness trap the issue
    describes (-1.0 must stay identity, not saturate to 1.0)

… sentinel

Consolidates the tanh soft-capping (Gemma-2 style) used for attention
scores and output logits into apply_softcap()/softcap_enabled() in
transformer_lens/utilities/activation_functions.py, replacing five
independent cap * tanh(x / cap) implementations across HookedTransformer,
abstract_attention, and the native TransformerBridge model, plus the
center_unembed compatibility checks in both systems.

Also replaces the bare -1.0 disabled sentinel with a named
SOFTCAP_DISABLED constant in both HookedTransformerConfig and
TransformerBridgeConfig.

Pure refactor, no behavior change. A naive 'if cap:' truthiness check
would treat -1.0 as enabled (computing -1.0 * tanh(x / -1.0) == tanh(x),
saturating every large score to 1.0); softcap_enabled() makes that
mistake structurally impossible instead of relying on every call site
getting 'cap > 0' right independently.

Verified: uv run mypy . clean (307 files), make unit-test clean
(3204 passed, 32 skipped, 9 xfailed, 0 failed), docstring tests on
changed files clean.

Fixes TransformerLensOrg#1489
@jlarson4

jlarson4 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Great work on this @delaidam! I appreciate the thoroughness & additional testing you added. Merging now

@jlarson4 jlarson4 merged commit b34a378 into TransformerLensOrg:dev Jul 7, 2026
25 checks passed
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.

2 participants