BlockRank methodology for scalable In-context Ranking (ICR) with LLMs. Enforces structured sparse attention (linear complexity) + auxiliary contrastive loss for efficient document ranking. Activation triggers: in-context ranking, blockrank, listwise reranking, LLM retrieval, attention sparsity, efficient ranking, ICR, document ranking with LLMs
Scanned 9/11/2026
Install to Claude Code
npx -y skills add hiyenwong/ai_collection --skill blockrank-scalable-incontext-ranking --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Blockrank Scalable Incontext Ranking?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hiyenwong-blockrank-scalable-incontext-ranking-5396b30c)More formats (shields.io, HTML) on the badges page.
---
name: blockrank-scalable-incontext-ranking
description: "BlockRank methodology for scalable In-context Ranking (ICR) with LLMs. Enforces structured sparse attention (linear complexity) + auxiliary contrastive loss for efficient document ranking. Activation triggers: in-context ranking, blockrank, listwise reranking, LLM retrieval, attention sparsity, efficient ranking, ICR, document ranking with LLMs"
---
# BlockRank: Scalable In-context Ranking with Generative Models
> A novel method that adapts LLM attention for In-context Ranking by enforcing inter-document block sparsity (linear complexity) and optimizing query-document relevance via auxiliary contrastive loss, achieving 4.7x speedup on 100 documents while matching or exceeding SOTA performance.
## Metadata
- **Source**: arXiv:2510.05396
- **Authors**: Nilesh Gupta, Chong You, Srinadh Bhojanapalli, Sanjiv Kumar, Inderjit Dhillon, Felix Yu
- **Published**: 2025-10-06 (v2: 2025-10-08)
- **Institutions**: UT Austin, Google, Google DeepMind
## Core Problem
**In-context Ranking (ICR)**: Feed query + N candidate documents into LLM prompt, task model to identify relevant document(s). Promising for full contextualization but suffers from quadratic/super-linear attention scaling with context length.
## Key Discovery: Structured Attention Patterns in ICR
The paper identifies two exploitable structures in LLMs fine-tuned for ICR:
### Observation 1: Inter-document Block Sparsity
- Attention is **dense within** each document block (intra-document)
- Attention is **sparse across** different document blocks
- Document tokens primarily attend to their own content + instruction, not to other documents
- This means full attention matrix computation is largely redundant
### Observation 2: Query-document Block Relevance
- Certain query tokens (delimiters like `:`, end-of-prompt tokens) act as **"signal carriers"**
- These tokens develop strong attention weights toward relevant documents in **middle layers** (layers 8-24 in Mistral-7B)
- Retrieval signal is weak in early layers, strengthens in middle layers, persists/diffuses in final layers
## BlockRank Methodology
### Component 1: Blockwise Structured Attention
Restructure attention to enforce observed sparsity:
- **Document tokens** (t_i ∈ T_dk): only attend to their own document chunk + instruction chunk
- **Query tokens** (t_i ∈ T_q): attend to all tokens (full prompt) to gather context for ranking
- **Instruction tokens** (t_i ∈ T_Inst): standard causal self-attention
**Implementation**: Segment prompt into logical chunks (Inst, d_1, ..., d_N, q). Process each chunk largely in parallel:
- Document chunk k: Attention(Q_k, [K_k, K_Inst], [V_k, V_Inst])
- Query chunk: Attention(Q_q, [K_q, K_Inst, K_d1, ..., K_dN], [V_q, V_Inst, V_d1, ..., V_dN])
**Complexity**: O(N · L_chunk² · d) — **linear in N** (number of documents) vs O(N² · L_chunk² · d) for full attention.
### Component 2: Permutation-invariant Position Embedding
- Instruction tokens: standard sequential positions starting from 0
- All document tokens: **shared local position space** — each document's tokens start at position L_Inst, as if it were the only document. Ensures order-invariant processing.
- Query tokens: positions start from large offset (8192), distinctly separating from documents.
### Component 3: Auxiliary Attention Loss (L_aux)
Introduce contrastive loss at specific middle layer l* (empirically layer 20):
1. Identify signal-carrying query tokens: T_q,signal = [":", "["]
2. Compute attention from these tokens to all document tokens at layer l*
3. Normalize attention over document tokens only (softmax across docs)
4. Aggregate to per-document relevance score: S(q, d_k) = Σ_{t_i ∈ T_q,signal} Σ_{t_j ∈ T_dk} α'_ij
5. Apply **InfoNCE loss**: L_aux = -log(exp(S(q, d*)/τ) / Σ_k exp(S(q, d_k)/τ))
**Total loss**: L_Total = L_NTP + λ · L_aux (λ = 0.1, τ = 0.05)
### Component 4: Attention-Based Inference
Instead of auto-regressive decoding, use attention scores directly:
1. Partial forward pass up to layer l*
2. Compute document relevance scores S(q, d_k)
3. Output top-K documents by score
This bypasses decoding entirely, providing additional speedup.
## Results
| Metric | BlockRank | Full-FT Mistral |
|--------|-----------|----------------|
| BEIR nDCG@10 | **54.8** | - |
| NQ P@1 | **76.2** | 75.5 |
| MSMarco P@1 | **29.1** | 28.7 |
| MSMarco MRR@10 | **42.0** | 38.3 |
| Latency (N=100) | ~65ms | ~304ms (4.7x slower) |
| Scalability | Linear to N=500 (1.15s) | Degrades beyond N=100 |
## Implementation Guide
### Prerequisites
- Fine-tuning framework (JAX/PyTorch)
- LLM with modifiable attention (Mistral-7B, Llama, etc.)
- Document retrieval pipeline for candidate generation
### Step-by-Step
1. **Data preparation**: Format ICR prompts with Inst + documents + query structure
2. **Structured attention**: Modify attention masks per document/query/instruction roles
3. **Position embeddings**: Apply shared local positions for docs, large offset for query
4. **Auxiliary loss**: Add InfoNCE at middle layer l* on signal-carrying token attention
5. **Training**: Joint optimization with L_NTP + λ·L_aux
6. **Inference**: Use attention-based scoring for fast ranking, or decode for generative output
### Code Sketch (PyTorch-style)
```python
def blockrank_attention(Q, K, V, chunk_types, inst_len, chunk_len):
"""
chunk_types: ['inst', 'doc', 'doc', ..., 'query']
For doc chunks: attend to self + inst only
For query chunk: attend to all
For inst chunk: causal self-attention
"""
outputs = []
for i, ct in enumerate(chunk_types):
if ct == 'doc':
K_ctx = torch.cat([K[i], K[0]], dim=-2) # self + inst
V_ctx = torch.cat([V[i], V[0]], dim=-2)
elif ct == 'query':
K_ctx = torch.cat(K, dim=-2) # all chunks
V_ctx = torch.cat(V, dim=-2)
else: # inst
K_ctx, V_ctx = K[i], V[i] # causal handled elsewhere
outputs.append(scaled_dot_product_attention(Q[i], K_ctx, V_ctx))
return torch.cat(outputs, dim=-2)
def auxiliary_attention_loss(attn_scores, signal_tokens, doc_masks, relevant_idx, tau=0.05):
"""InfoNCE on attention from signal tokens to documents."""
# attn_scores: [batch, num_signal_tokens, num_docs]
scores = attn_scores[:, signal_tokens, :].mean(dim=1) # [batch, num_docs]
logits = scores / tau
labels = relevant_idx
return F.cross_entropy(logits, labels)
```
## Applications
- **LLM-based document re-ranking** in search pipelines
- **Listwise ranking** with 100-500 candidates in single forward pass
- **Efficient long-context processing** where documents are logically separable
- **Any task** where structured attention sparsity matches input structure
## Pitfalls
- **Auto-regressive decoding calibration**: Beam decoding produces low-diversity ID sequences (entropy analysis shows concentration). Prefer attention-based inference for ranked lists.
- **Query in prefix**: Including query in instruction prefix (not just at end) improves performance (+1.5 P@1 for Full-FT, +3.9 for BlockRank). Redundant but beneficial.
- **Layer choice for L_aux**: Middle layer 20 works for Mistral-7B (32 layers); not highly sensitive — any reasonable middle layer works.
- **Architecture specificity**: Current results demonstrated on Mistral-7B; robustness across architectures needs more investigation.
- **Chunk length tuning**: L_chunk = 160 for MSMarco, 384 for NQ — ensure ~95% of passages fit fully.
## Related Skills
- attention-sink-structural
- in-context-brain-decoding
- memory-retrieval
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!