Skip to main content

Crate haagenti_sparse

Crate haagenti_sparse 

Source
Expand description

Sparse Attention Masks

This module implements prompt-aware attention head masking to skip computation for heads that don’t contribute to the output.

§Key Insight

Not all attention heads are equally important for every prompt. Portrait prompts activate face-focused heads while landscape prompts activate background/composition heads. By predicting which heads matter, we can skip 50-70% of attention computation.

§Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    Sparse Attention                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│  Standard Attention (32 heads × 64 layers = 2048 computations)  │
│  ════════════════════════════════════════════════════════════   │
│  [████████████████████████████████] 100% compute                │
│                                                                  │
│  Sparse Attention (prompt-aware masking)                        │
│  ════════════════════════════════════════════════════════════   │
│  "Portrait of a woman"                                          │
│  [████████░░░░░░░░████░░░░░░░░░░░░] 35% compute                 │
│   ↑ face  ↑ skip  ↑ style                                       │
│                                                                  │
│  "Mountain landscape at sunset"                                 │
│  [░░░░░░░░████████████████████░░░░] 45% compute                 │
│   ↑ skip  ↑ background/lighting  ↑ skip                         │
└─────────────────────────────────────────────────────────────────┘

Modules§

prelude
Prelude for common imports

Structs§

AttentionMask
A mask indicating which attention heads to compute
CategoryMapping
Mapping from head indices to categories
HeadAnalysis
Complete analysis of all attention heads
HeadImportance
Importance score for a single attention head
ImportanceAnalyzer
Analyzer for computing head importance
ImportanceStats
Statistics about head importance distribution
KernelConfig
Configuration for sparse attention kernel
KernelStats
Kernel statistics
MaskBuilder
Builder for attention masks
MaskPredictor
Predicts attention masks from prompt information
Prediction
Prediction result with confidence
PredictorConfig
Configuration for mask prediction
SparseKernel
Manager for sparse attention kernel execution.

Enums§

HeadCategory
Categories of attention heads based on their learned function
MaskPattern
Pattern for mask generation
PromptCategory
Categories of prompts that influence head activation
SparseError
Errors that can occur during sparse attention operations

Constants§

DEFAULT_SPARSITY
Default sparsity target (fraction of heads to skip)
MAX_QUALITY_LOSS
Maximum quality degradation allowed
MIN_ACTIVE_HEADS
Minimum heads to keep active per layer

Type Aliases§

Result
Result type for sparse attention operations