Context Matters: Query-aware Dynamic Long Sequence Modeling of Gigapixel Images

Zhengrui Guo, Qichen Sun, Jiabo Ma, Lishuang Feng, Jinzhuo Wang, Hao Chen

Abstract

Whole Slide Images (WSIs) in computational pathology present a significant computational challenge due to their gigapixel scale, often containing over 100,000 patches. Standard Transformer architectures, while effective for long-range dependencies, suffer from quadratic complexity ($O(N^2)$), creating a "quadratic wall" for large-scale analysis. We present Querent, a query-aware dynamic modeling framework that achieves a theoretically bounded approximation of full self-attention with near-linear complexity. By dynamically identifying relevant regions based on query content, Querent reduces memory usage by 97% and computation by 95% while maintaining state-of-the-art performance across multiple clinical tasks.

Introduction

In digital pathology, the ability to model interactions between distant tissue regions is crucial for tasks like cancer subtyping and survival analysis. However, the sheer size of WSIs makes standard self-attention computationally prohibitive. Existing linear attention or local windowing methods often introduce information bottlenecks or fail to capture the dynamic, biological nature of tissue context. For instance, a patch at a tumor boundary requires different contextual information than one in an inflammatory region. Querent addresses this by moving beyond fixed spatial neighborhoods to a content-driven, dynamic attention mechanism.

Methodology: The Querent Framework

Querent operates through a four-stage pipeline designed to efficiently approximate global context without the quadratic cost.

Querent architecture overview
  1. Region-level Metadata Summarization: The WSI is partitioned into regions. For each region, the system computes "metadata" consisting of element-wise minimum and maximum feature values. This preserves structural outliers that are often lost in simple mean-averaging.
  2. Region Importance Estimation: For a given query patch, the model projects its features against the region-level metadata to calculate an importance score. This score serves as an upper bound on the potential relevance of any patch within that region.
  3. Query-aware Selective Attention: The model selects the top-$K$ most relevant regions. Dense self-attention is then performed only between the query patch and the patches within these selected regions.
  4. Attentive Feature Aggregation: Context-aware features are aggregated via attentive pooling to produce a final slide-level prediction.

The framework is theoretically grounded; using the Johnson-Lindenstrauss lemma and spatial decay assumptions, the authors provide a mathematical proof (Theorem 3.1) showing that this sparse selection effectively approximates the full attention matrix within a small constant bound.

Experimental Results

Querent was evaluated on 11 public datasets covering biomarker prediction (BCNB), gene mutation (TCGA-LUAD), cancer subtyping (UBC-OCEAN), and survival analysis.

Efficiency and Scalability

The most significant advantage of Querent is its ability to bypass the quadratic complexity of standard Transformers. As sequence lengths increase toward the 100,000-patch mark common in WSIs, the memory and compute savings become transformative.

Memory requirements comparison

At a sequence length of 100,000, standard attention requires approximately 37GB of memory, whereas Querent requires only 1GB. Similarly, computational requirements drop from 10,000 GFLOPs to just 500 GFLOPs. This efficiency allows for the processing of massive slides on standard hardware.

Performance Benchmarks

Despite its sparse attention mechanism, Querent consistently outperformed state-of-the-art baselines including TransMIL, HIPT, and MambaMIL. On the UBC-OCEAN 5-class ovarian cancer subtyping task, it achieved an accuracy of 0.835, surpassing the second-best model by 1.2%.

Discussion and Key Insights

A critical finding of this work is the superiority of Min-Max summarization over traditional mean-pooling. By capturing the "extremes" within a region, the model preserves distinctive pathological features and structural relationships that averages tend to wash out. Ablation studies further indicate that region sizes of 16-24 patches provide the optimal balance between local discriminative power and global context.

While Querent represents a significant step forward, it remains dependent on the quality of the frozen pretrained encoder (e.g., PLIP). Future work may explore more flexible, biologically-driven region partitioning to further enhance performance.

Conclusion

Querent demonstrates that context-aware, dynamic modeling is both feasible and highly effective for gigapixel image analysis. By breaking the quadratic complexity barrier, it opens new possibilities for large-scale computational pathology and long-sequence modeling in medical imaging.

Summary updated

BibTeX
@inproceedings{guo2025context,
  author = {Zhengrui Guo and Qichen Sun and Jiabo MA and Lishuang Feng and Jinzhuo Wang and Hao Chen},
  title = {{Context Matters: Query-aware Dynamic Long Sequence Modeling of Gigapixel Images}},
  booktitle = {International Conference on Machine Learning},
  year = {2025},
  url = {https://openreview.net/forum?id=BtRn4eayfE},
}
Figure