Expose n_ctx override in HookedTransformer.from_pretrained (issue #1006)#1204
Merged
jlarson4 merged 5 commits intoTransformerLensOrg:devfrom Mar 16, 2026
Merged
Conversation
…nsformerLensOrg#1006) get_pretrained_model_config already supported n_ctx as a parameter but it was invisible to users — falling through **kwargs with no type hint, no docstring entry, and no IDE support. This commit surfaces it as an explicit parameter on from_pretrained, adds a warning when n_ctx exceeds the model's trained default, and adds two regression tests using GPT-2 (runs on CPU, no GPU required). Users can now do: model = HookedTransformer.from_pretrained('gpt2', n_ctx=256) model = HookedTransformer.from_pretrained('meta-llama/Llama-3.2-3B', n_ctx=32768) Fixes TransformerLensOrg#1006
1 task
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1006.
get_pretrained_model_configalready acceptedn_ctxas a parameter,but it was invisible to users — silently falling through
**kwargswithno type hint, no IDE autocomplete, and no docstring entry on
from_pretrained. Users had to edit source files directly to changecontext length.
This PR surfaces it as an explicit, documented parameter.
Changes
transformer_lens/HookedTransformer.pyn_ctx: Optional[int] = Nonetofrom_pretrainedsignaturen_ctx=n_ctxto the explicit call toget_pretrained_model_config(previously relied on silent kwargs passthrough)
transformer_lens/loading_from_pretrained.pyn_ctxoverride block to emit alogging.warningwhen the requested value exceeds the model's trained default, informing
users of potential reliability and memory implications
tests/unit/test_loading_from_pretrained_utilities.pytest_n_ctx_override_reduces_context— verifies override workswhen reducing below default
test_n_ctx_override_larger_than_default_warns— verifies warningfires with correct message when exceeding default
Both tests use GPT-2 Small and run on CPU — no GPU required.
Usage