MInference (Million-Tokens Prompt Inference for Long-context LLMs) is an open-source Microsoft project for accelerating inference of long-context large language models, primarily using dynamic sparse attention to accelerate the pre-filling stage of long-context LLMs, reducing inference latency by up to about 10x on A100 while maintaining accuracy.
Main capabilities: MInference 1.0 leverages the dynamic sparsity property of LLM attention. Attention contains both static patterns and dynamic characteristics. The method first determines offline which sparse pattern each attention head belongs to, then approximates the sparse indices online, and dynamically computes attention with optimal custom kernels. It is officially claimed that 1M context can now be processed on a single A100 at about 10x speed, for example LLaMA-3-8B-1M and GLM-4-1M, with better accuracy.
Related projects:
- MMInference: a multimodal version that uses modality-aware permuted sparse attention to accelerate the pre-filling stage of long-context VLMs (vision-language models), implementing three permutation-based sparse attention mechanisms (with FlashAttention, FlashDecoding, and PIT) to handle grid patterns in visual inputs and modality boundary issues in mixed-modality scenarios. Accepted by ICML'25.
- SCBench: a KV-cache-centric benchmark for analyzing long-context methods, covering the full lifecycle of KV cache (generation, compression, retrieval, loading), evaluating 12 tasks under two shared-context modes, covering four categories of capabilities: string retrieval, semantic retrieval, global information, and multi-task scenarios. Accepted by ICLR'25.
- RetrievalAttention: a KV cache offloading work that accelerates long-context LLM inference via vector retrieval.
Integration and kernel support:
SGLang and vLLM have merged the MInference sparse attention kernel. After installing pip install sglang , the optimized kernel can be used, and it is officially claimed that speedups of up to about 1.64x (64K), 2.4x (96K), 2.9x (128K), 5.2x (256K), 8x (512K), and 15x (1M) can be achieved. SGLang has also adapted it to FlashAttention-3. MInference has also been integrated into Qwen2.5-1M and online serving.
Supported long-context methods:
- KV cache generation: MInference, xAttention, FlexPrefill, A-shape, Tri-shape, MInference w/ static, Dilated, Strided
- KV cache compression: StreamingLLM, SnapKV, PyramidKV, KIVI
- KV cache retrieval: CacheBlend
- KV cache loading: Quest, RetrievalAttention Some of these modes are supported by vLLM, and all modes are supported by HF (Hugging Face).
Supported models:
- Qwen2.5: Qwen/Qwen2.5-7B-Instruct, Qwen/Qwen2.5-32B-Instruct, Qwen/Qwen2.5-72B-Instruct, Qwen/Qwen2.5-7B-Instruct-1M, Qwen/Qwen2.5-14B-Instruct-1M
- LLaMA-3.1: meta-llama/Meta-Llama-3.1-8B-Instruct, meta-llama/Meta-Llama-3.1-70B-Instruct
- LLaMA-3: gradientai/Llama-3-8B-Instruct-262k, gradientai/Llama-3-8B-Instruct-Gradient-1048k, gradientai/Llama-3-8B-Instruct-Gradient-4194k, gradientai/Llama-3-70B-Instruct-Gradient-262k, gradientai/Llama-3-70B-Instruct-Gradient-1048k
- GLM-4: THUDM/glm-4-9b-chat-1m
- Yi: 01-ai/Yi-9B-200K
- Phi-3: microsoft/Phi-3-mini-128k-instruct
- Qwen2: Qwen/Qwen2-7B-Instruct Generic MInference supports any decoder-only LLM, including LLaMA-style models and Phi models. If a model is not in the supported list, you can report it in issues, or manually generate the sparse head configuration according to the guide.
Usage: The environment requirement is Transformers >= 4.46.0, with optional FlashAttention-2. Install with pip:
pip install minference
For a fast kernel implementation, it is officially recommended to install SGLang or vLLM. For SGLang, install pip install sglang. For vLLM, vllm>=0.4.1 is required; when using vLLM with tensor parallelism (TP), you need to change the minference_patch_vllm_tp and minference_patch_vllm_executor to the end of the Worker class in vllm/worker/worker.py, and make sure minference_patch_vllm_tp is correctly indented; when calling VLLM, you need to set enable_chunked_prefill=False. You can also use only the kernel, or run the local gradio demo. For details, refer to the project's Examples and Experiments. SCBench data requires datasets >= 2.15.0, can be downloaded and loaded via Hugging Face datasets, and the data is uniformly in a standardized format; the project uses HF and vLLM to implement Multi-Turn and Multi-Request modes in the two classes GreedySearch and GreedySearch_vllm.
Applicable scenarios: Suitable for scenarios that require running open-source LLM inference under long contexts (such as 64K to 1M tokens), especially services and local deployments that are sensitive to prefill latency; it can also be used for long-context LLM capability evaluation (together with SCBench) and long-context VLM inference acceleration (MMInference).
Limitations and notes:
The core of the method is approximate computation based on dynamic sparse attention. Its performance on retrieval-type tasks varies with task difficulty (officially stated difficulty is roughly KV retrieval > Needle in a Haystack > Retrieval.Number > Retrieval PassKey). Some modes are supported only in HF, and vLLM supports some modes. If out of memory occurs, the official recommendations include: adding _attn_implementation="flash_attention_2" during model initialization to use the FlashAttention-2 backend, or setting logits_to_keep=1in the model forward. Regarding fees, pricing, specific platform support scope, and more model adaptations, the materials do not provide this.