Skip to content

Centralize soft-cap logic behind shared helper and named sentinel#1490

Closed
hiro-nikaitou wants to merge 2 commits into
TransformerLensOrg:mainfrom
hiro-nikaitou:refactor/softcap-centralize
Closed

Centralize soft-cap logic behind shared helper and named sentinel#1490
hiro-nikaitou wants to merge 2 commits into
TransformerLensOrg:mainfrom
hiro-nikaitou:refactor/softcap-centralize

Conversation

@hiro-nikaitou

Copy link
Copy Markdown

Fixes #1489

Summary

This PR centralizes the duplicated cap * tanh(x / cap) soft-cap logic into a shared helper function apply_softcap with a named sentinel SOFTCAP_DISABLED, as proposed in issue #1489.

Changes

  • Added apply_softcap(x, cap) helper and SOFTCAP_DISABLED = -1.0 constant to transformer_lens/utilities/activation_functions.py
  • Replaced 4 duplicated soft-cap implementations with calls to apply_softcap:
    • transformer_lens/components/abstract_attention.py (attention score soft-cap)
    • transformer_lens/HookedTransformer.py (output logit soft-cap)
    • transformer_lens/model_bridge/sources/native/model.py (both attention score and output logit soft-caps)
  • Replaced magic number -1.0 with SOFTCAP_DISABLED in config defaults and getattr fallback

Verification

  • apply_softcap returns x unchanged when cap <= 0 (disabled)
  • apply_softcap computes cap * tanh(x / cap) when cap > 0 (enabled)
  • All relevant module imports verified successfully

hiro-nikaitou and others added 2 commits July 7, 2026 15:42
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- isort: fix import ordering in bridge.py, HookedTransformer.py
- black: reformat conditional expression in HookedTransformer.py
- black: wrap long import line in hooked_transformer_config.py

Format check commands all pass locally (pycln, isort, black).
@jlarson4

jlarson4 commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Hi @hiro-nikaitou! Thank you for taking the time to work on this, unfortunately another user submitted a competing PR that included some elements you were missing here, and we have chosen to go with that implementation. Closing this PR, let me know if there are any other issues you're interested in taking on and I can get you assigned!

@jlarson4 jlarson4 closed this Jul 7, 2026
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.

[Proposal] Centralize soft-cap logic behind a shared helper and a named "disabled" sentinel

2 participants