From 00e31ea27c9ca0c190aaf2b8053c573c5f832c69 Mon Sep 17 00:00:00 2001 From: MOHAMMED FAISAL PARVEZ Date: Tue, 17 Nov 2026 00:45:16 +0530 Subject: [PATCH 1/2] fix: let detect_head accept a sequence that fills the context window detect_head tokenized with truncation, so a sequence longer than n_ctx was cut to exactly n_ctx tokens, and the check 1 < len < n_ctx then rejected every n_ctx-token sequence to catch those. That also rejected sequences that fit exactly. Tokenize without truncation and accept len <= n_ctx: an exact fit now runs, and anything longer is still rejected. The existing "outside context window" test passed "a " * n_ctx, which is exactly n_ctx tokens; it now passes one more. Co-Authored-By: Claude Opus 5.5 --- tests/integration/test_head_detector.py | 20 +++++++++++++++++++- transformer_lens/head_detector.py | 6 ++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/integration/test_head_detector.py b/tests/integration/test_head_detector.py index c85191a63..e27998a83 100644 --- a/tests/integration/test_head_detector.py +++ b/tests/integration/test_head_detector.py @@ -410,13 +410,31 @@ def test_detect_head_with_zero_sequence_length(): def test_detect_head_with_sequence_length_outside_context_window(): with pytest.raises(AssertionError) as e: - detect_head(model, "a " * model.cfg.n_ctx, "previous_token_head") + # "a " * n_ctx is exactly n_ctx tokens, which fits; one more does not. + detect_head(model, "a " * (model.cfg.n_ctx + 1), "previous_token_head") assert ( str(e.value) == "The sequence must be non-empty and must fit within the model's context window." ) +def test_detect_head_with_sequence_much_longer_than_context_window(): + with pytest.raises(AssertionError) as e: + detect_head(model, "a " * (2 * model.cfg.n_ctx), "previous_token_head") + assert ( + str(e.value) + == "The sequence must be non-empty and must fit within the model's context window." + ) + + +def test_detect_head_with_sequence_filling_context_window(): + # BOS plus n_ctx - 1 tokens fills the context window exactly, and fits. + seq = " a" * (model.cfg.n_ctx - 1) + assert model.to_tokens(seq).shape[-1] == model.cfg.n_ctx + result = detect_head(model, seq, "previous_token_head") + assert result.shape == (model.cfg.n_layers, model.cfg.n_heads) + + def test_detect_head_with_invalid_detection_pattern(): with pytest.raises(AssertionError) as e: detect_head(model, test_duplicated_sequence, torch.ones(4, 4)) diff --git a/transformer_lens/head_detector.py b/transformer_lens/head_detector.py index 15870005c..8288c5a89 100644 --- a/transformer_lens/head_detector.py +++ b/transformer_lens/head_detector.py @@ -138,7 +138,9 @@ def detect_head( return torch.stack(batch_scores).mean(0) cfg = model.cfg - tokens = model.to_tokens(seq).to(cfg.device) + # Without truncation, so a sequence longer than the context window keeps + # its length and is rejected below rather than cut silently to n_ctx. + tokens = model.to_tokens(seq, truncate=False).to(cfg.device) seq_len = tokens.shape[-1] # Validate detection pattern if it's a string @@ -156,7 +158,7 @@ def detect_head( ) # Validate inputs and detection pattern shape - assert 1 < tokens.shape[-1] < cfg.n_ctx, SEQ_LEN_ERR + assert 1 < tokens.shape[-1] <= cfg.n_ctx, SEQ_LEN_ERR assert ( is_lower_triangular(detection_pattern) and seq_len == detection_pattern.shape[0] ), DET_PAT_NOT_SQUARE_ERR % (seq_len, detection_pattern.shape) From 12a1cd7e6bfd3a7187a655b7cebab110b43f74d8 Mon Sep 17 00:00:00 2001 From: MOHAMMED FAISAL PARVEZ Date: Fri, 20 Nov 2026 07:08:01 +0530 Subject: [PATCH 2/2] Check sequence length before building the detection pattern Without truncation an over-long prompt reached the length check only after the detection pattern had been built at full length, so rejecting it cost time and memory that grew with the prompt. The check now runs right after tokenization. The over-by-one test now builds BOS plus n_ctx tokens and asserts that length, the same way the exact-fit test does. "a " * n_ctx was 1,026 tokens, not n_ctx. Co-Authored-By: Claude Opus 5.5 --- tests/integration/test_head_detector.py | 6 ++++-- transformer_lens/head_detector.py | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/integration/test_head_detector.py b/tests/integration/test_head_detector.py index e27998a83..3ea6c48e3 100644 --- a/tests/integration/test_head_detector.py +++ b/tests/integration/test_head_detector.py @@ -409,9 +409,11 @@ def test_detect_head_with_zero_sequence_length(): def test_detect_head_with_sequence_length_outside_context_window(): + # BOS plus n_ctx tokens is one more than the context window holds. + seq = " a" * model.cfg.n_ctx + assert model.to_tokens(seq, truncate=False).shape[-1] == model.cfg.n_ctx + 1 with pytest.raises(AssertionError) as e: - # "a " * n_ctx is exactly n_ctx tokens, which fits; one more does not. - detect_head(model, "a " * (model.cfg.n_ctx + 1), "previous_token_head") + detect_head(model, seq, "previous_token_head") assert ( str(e.value) == "The sequence must be non-empty and must fit within the model's context window." diff --git a/transformer_lens/head_detector.py b/transformer_lens/head_detector.py index 8288c5a89..4a02e2bc3 100644 --- a/transformer_lens/head_detector.py +++ b/transformer_lens/head_detector.py @@ -142,6 +142,8 @@ def detect_head( # its length and is rejected below rather than cut silently to n_ctx. tokens = model.to_tokens(seq, truncate=False).to(cfg.device) seq_len = tokens.shape[-1] + # Checked before anything is sized from tokens, so an over-long prompt fails fast. + assert 1 < seq_len <= cfg.n_ctx, SEQ_LEN_ERR # Validate detection pattern if it's a string if isinstance(detection_pattern, str): @@ -157,8 +159,7 @@ def detect_head( "Using detection pattern with values other than 0 or 1 with error_measure 'mul'" ) - # Validate inputs and detection pattern shape - assert 1 < tokens.shape[-1] <= cfg.n_ctx, SEQ_LEN_ERR + # Validate detection pattern shape assert ( is_lower_triangular(detection_pattern) and seq_len == detection_pattern.shape[0] ), DET_PAT_NOT_SQUARE_ERR % (seq_len, detection_pattern.shape)