Skip to main content

Module attention_model

Module attention_model 

Source
Expand description

Heuristic attention prediction model for LLM context optimization.

Based on empirical findings from “Lost in the Middle” (Liu et al., 2023):

  • Transformers attend strongly to begin and end positions
  • Middle positions receive ~50% less attention
  • Structural markers (definitions, errors) attract attention regardless of position

This module provides a position + structure based attention estimator that can be used to reorder or filter context for maximum LLM utilization.

Functions§

attention_efficiency
Compute the theoretical attention efficiency for a given context layout. Returns a percentage [0, 100] indicating how much of the context is in attention-optimal positions.
attention_optimize
Reorder lines to maximize predicted attention utilization. Places high-attention lines at begin and end positions.
combined_attention
Compute combined attention score for a line at a given position. Combines positional U-curve with structural importance.
positional_attention
Compute a U-shaped attention weight for a given position. position: normalized 0.0 (begin) to 1.0 (end) Returns attention weight in [0, 1].
structural_importance
Estimate the structural importance of a line. Returns a multiplier [0.5, 2.0] based on syntactic patterns.