Skip to content

Commit 97a86bb

Browse files
authored
Harden attention sink export: start_pos, mask mode, and buffer initialization (pytorch#18976)
Differential Revision: D100724151 Pull Request resolved: pytorch#18976
1 parent 99d1756 commit 97a86bb

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎examples/models/llama/export_llama_lib.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,8 @@ def _prepare_for_llama_export(llm_config: LlmConfig) -> LLMEdgeManager:
768768
expand_rope_table=llm_config.model.expand_rope_table,
769769
use_custom_sdpa_with_attention_mask=getattr(
770770
llm_config.model, "use_custom_sdpa_with_attention_mask", False
771-
),
771+
)
772+
or bool(llm_config.model.use_attention_sink),
772773
use_sdpa_with_kv_cache=llm_config.model.use_sdpa_with_kv_cache,
773774
quantize_kv_cache=llm_config.model.quantize_kv_cache,
774775
use_kv_cache=llm_config.model.use_kv_cache,
@@ -1324,6 +1325,13 @@ def _export_llama_multimethod(llm_config: LlmConfig) -> LLMEdgeManager:
13241325
if llm_config.base.model_class.value in TORCHTUNE_DEFINED_MODELS:
13251326
additional_passes = [InitializedMutableBufferPass(["kv_cache_pos"])]
13261327

1328+
# For attention sink models, cache_positions must be initialized to -1
1329+
# (sentinel for "empty slot"). Without this pass, ExecuTorch only serializes
1330+
# shape+dtype for mutable buffers, leaving them zero-initialized at runtime,
1331+
# which corrupts the causal mask computation.
1332+
if llm_config.model.use_attention_sink:
1333+
additional_passes.append(InitializedMutableBufferPass(["cache_positions"]))
1334+
13271335
# Build dict of exported programs
13281336
method_to_program: Dict[str, ExportedProgram] = {}
13291337
first_builder = None
@@ -1397,6 +1405,13 @@ def _export_llama(llm_config: LlmConfig) -> LLMEdgeManager: # noqa: C901
13971405
if llm_config.base.model_class.value in TORCHTUNE_DEFINED_MODELS:
13981406
additional_passes = [InitializedMutableBufferPass(["kv_cache_pos"])]
13991407

1408+
# For attention sink models, cache_positions must be initialized to -1
1409+
# (sentinel for "empty slot"). Without this pass, ExecuTorch only serializes
1410+
# shape+dtype for mutable buffers, leaving them zero-initialized at runtime,
1411+
# which corrupts the causal mask computation.
1412+
if llm_config.model.use_attention_sink:
1413+
additional_passes.append(InitializedMutableBufferPass(["cache_positions"]))
1414+
14001415
# export_to_edge
14011416
builder_manager = _prepare_for_llama_export(llm_config)
14021417
if (

‎examples/models/llama/source_transformation/custom_kv_cache.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,6 @@ def __init__(
521521
total_cache_size = sink_size + window_size * 2
522522
super().__init__(max_batch_size, total_cache_size, n_heads, head_dim, dtype)
523523
from executorch.examples.models.llama.source_transformation.attention_sink import (
524-
_create_causal_mask_for_attention_sink,
525524
CachePositionsManagerWithSink,
526525
)
527526

@@ -531,14 +530,15 @@ def __init__(
531530
self.is_ring_buffer = True
532531
self.window_size = window_size
533532
self.sink_size = sink_size
534-
self._create_causal_mask_for_attention_sink = (
535-
_create_causal_mask_for_attention_sink
536-
)
537533

538534
def create_causal_mask_for_ring_buffer(self, start_pos, seq_len):
539535
cache_positions = self.cache_positions_manager.cache_positions
540536
if self.sink_size > 0:
541-
return self._create_causal_mask_for_attention_sink(
537+
from executorch.examples.models.llama.source_transformation.attention_sink import (
538+
_create_causal_mask_for_attention_sink,
539+
)
540+
541+
return _create_causal_mask_for_attention_sink(
542542
cache_positions, self.window_size, self.sink_size, start_pos, seq_len
543543
)
544544
else:

‎examples/models/llama/source_transformation/sdpa.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def forward(
5454
q,
5555
k,
5656
v,
57-
input_pos[0].item(),
57+
0, # start_pos: unused when mask is provided (is_causal=False)
5858
mask, # Attention mask
5959
0, # dropout probability. Ignored by the code
6060
False, # is_causal
@@ -168,7 +168,7 @@ def forward(
168168
q_quantized,
169169
k_quantized,
170170
v_quantized,
171-
start_pos,
171+
0, # start_pos: unused when mask is provided (is_causal=False)
172172
mask,
173173
0,
174174
False,

0 commit comments

Comments
 (0)