LLMIR

Version 0.1.0

Large Language Model IR Compiler Framework

User Guide

LLMIR User Guide

This guide helps you get started using the llmir repository for LLM inference optimization.

Installation 

pip install llmir

Optional extras:

ExtraPurpose
llmir[dev]pytest, black, mypy for development
llmir[full]torch, transformers for full model workflows

Option 2: From Source 

git clone https://github.com/chenxingqiang/llmir.git
cd llmir
pip install -e .
# Or with extras: pip install -e ".[full]"

Option 3: C++ MLIR Dialect Only 

If you only need the C++ dialect (no Python):

cd llmir/build_llm_dialect
mkdir build && cd build
cmake -G Ninja ..
ninja

Use Cases 

1. KV Cache for Custom Inference 

import llmir

# Configure for your model (e.g., Llama 7B: 32 layers, 32 heads, 128 head_dim)
config = llmir.KVCacheConfig(num_layers=32, num_heads=32, head_dim=128)
cache = llmir.PagedKVCache(config)

# In your inference loop:
# cache.append(keys, values, seq_ids)   # Append new KV pairs
# cache.lookup(block_indices, seq_lens) # Lookup for attention

2. Model-Specific Optimization 

# Llama 3.1 8B
optimizer = llmir.LlamaOptimizer.for_llama3_8b()
kv_config = optimizer.get_optimized_kv_cache_config()

# Mistral 7B (sliding window attention)
optimizer = llmir.MistralOptimizer.for_mistral_7b()
config = optimizer.get_optimized_kv_cache_config()

# Phi-3
optimizer = llmir.PhiOptimizer.for_phi3_mini()
config = optimizer.get_optimized_kv_cache_config()

3. Memory-Constrained: Quantized KV Cache 

quant_config = llmir.QuantizationConfig(quant_type=llmir.QuantizationType.INT8)
quant_cache = llmir.QuantizedKVCache(config, quant_config)
# ~4x memory reduction with INT8, ~8x with INT4
print(f"Compression: {quant_cache.get_compression_ratio()}x")

4. Performance Profiling 

profiler = llmir.Profiler()
profiler.start()

with profiler.trace("attention"):
    # Your attention / inference code
    run_attention_step()

profiler.stop()
report = profiler.get_report()
report.print_summary()  # Latency, throughput, memory

5. Benchmarking Against vLLM 

From the repo root:

./run_real_benchmark.sh      # Qwen2.5-7B, PyTorch vs vLLM
./vllm_comparison.sh         # Quick vLLM comparison
./comprehensive_benchmark.sh # Full suite with SGLang

Repository Layout (What to Use) 

PathFor Users
src/Python package source (used by pip install)
examples/demo_llmir_0.6b.py – runnable demos
benchmark/LLM/Benchmark scripts, Llama-3.1 setup
*.sh (root)run_real_benchmark.sh, vllm_comparison.sh
tests/Test examples; run with pytest tests/ -v
IEEE-conference/Paper, figures, verification reports
build_llm_dialect/C++ dialect build (advanced)

Supported Models 

FamilySizesOptimizer
Llama 1/2/3/3.17B–405BLlamaOptimizer
Mistral7BMistralOptimizer
Mixtral8x7B, 8x22BMistralOptimizer
PhiPhi-2, Phi-3PhiOptimizer

Use ModelRegistry for presets:

registry = llmir.ModelRegistry.get_instance()
config = registry.get_config("llama3.1-8b")  # or "mixtral-8x7b", "phi-3-mini", etc.

Hardware & Requirements 

  • Python: 3.8+
  • GPU: NVIDIA (CUDA) for best performance; verified on A800 80GB
  • RAM: Depends on model size; see ModelMemoryEstimator for planning
  • Optional: vLLM, SGLang for baseline comparisons

Getting Help 

ResourceLink
GitHub repogithub.com/chenxingqiang/llmir
Issuesgithub.com/chenxingqiang/llmir/issues
DocumentationThis site
ForumsLLVM Discourse – LLMIR
ChatLLVM Discord – LLMIR channel
FAQFAQ

Troubleshooting 

IssueSolution
ModuleNotFoundError: llmirRun pip install llmir or install from source with pip install -e .
Benchmark script failsEnsure vLLM is installed (pip install vllm) for vLLM comparison scripts
C++ dialect build failsCheck MLIR 18 is available; see build_llm_dialect/README
Out of memoryUse QuantizedKVCache (INT8/INT4) or reduce batch size; see ModelMemoryEstimator

Next Steps