LLM4LLM is a deployment-aware, closed-loop framework for optimizing GPU kernels in language-model inference workloads. It starts from a user-provided inference script, profiles the actual workload, extracts optimization tasks, searches for custom Triton/CUDA/TileLang implementations, and validates the result after integration into the model.
+--------------------+ +--------------------+ +--------------------+ +--------------------+
| Workload Profiler | -> | Phase-Aware Task | -> | Optimization Agent | -> | Model Integration |
| User inference | | Prefill / Decode | | Generate, validate | | Patch, validate, |
| script + profiling | | + KB adapter | | refine, restart | | End-to-end timing |
+--------------------+ +--------------------+ +--------------------+ +--------------------+
| | | |
v v v v
Profile artifacts Extracted task files Kernels and traces Deployment results
The phase-aware path separates prefill and decode behavior when the same model module sees different shapes, cache states, or execution paths. Generated kernels are recombined through dispatch metadata before model-level validation.
The release has been tested with the following primary environment:
| Package | Version |
|---|---|
| Python | 3.12 |
| PyTorch | 2.9.0+cu128 |
| CUDA | 12.8 |
| Triton | 3.5.0 |
| TileLang | 0.1.7 |
| Transformers | 4.57.3 |
| LangChain | 1.2.0 |
An NVIDIA GPU and a compatible CUDA toolchain are required for kernel generation and evaluation.
git clone https://github.com/hzeng2000/LLM4LLM.git
cd LLM4LLM
uv venv --python 3.12 --seed
source .venv/bin/activate
uv pip install -e ".[kernel,dev]"KernelBench is required to construct prompts and evaluate generated kernels. LLM4LLM uses a pinned fork for reproducibility:
git clone https://github.com/hzeng2000/KernelBench.git third_party/KernelBench
git -C third_party/KernelBench checkout 3b9c83406998529083b3f31670acecf773997797
uv pip install -e third_party/KernelBench
uv pip install -r third_party/KernelBench/requirements.txtCompared with upstream KernelBench, this revision runs both reference and
generated models in .eval() mode and uses an FP32 correctness tolerance of
1e-2.
| Component | Installation or requirement |
|---|---|
| vLLM extraction and serving benchmarks | uv pip install -e ".[full]" |
| FlashAttention comparison | uv pip install flash-attn --no-build-isolation |
| TileLang backend | Install a TileLang build compatible with the local CUDA and PyTorch versions |
These components are not required for the default Triton optimization flow.
Create a local environment file:
cp .env.example .envAt minimum, configure an OpenAI-compatible endpoint and the local path of the model used in the quick start:
OPENAI_API_KEY=your_api_key
OPENAI_BASE_URL=https://api.openai.com/v1
LLM_MODEL=gpt-5.5
LLM4LLM_MODEL_MAMBA_130M_HF=/path/to/mamba-130m-hfThe scripts in user_inputs/ load model weights locally. Additional model-path
environment variables are defined in llm4llm/lm_registry.py.
The following example uses the included Mamba-130M inference script. A user script
must instantiate a PyTorch model and execute a representative inference workload;
run_extract.py executes that script under the profiler.
python scripts/run_extract.py \
--script user_inputs/mamba-130m-hf.py \
--model mamba-130m-hf \
--top-k 2The extracted tasks are written under:
workspace/mamba-130m-hf/
Each task records the extracted PyTorch source, observed inputs, module state, shape metadata, and phase metadata when applicable.
Optimize all extracted tasks with the model configured by LLM_MODEL:
python scripts/run_optimize.py \
--workspace workspace/mamba-130m-hf \
--all \
--backend tritonTo optimize one task instead:
python scripts/run_optimize.py \
--task-dir workspace/mamba-130m-hf/<module>/<variant> \
--backend tritonCandidate code, validation records, timing results, and generation settings are
stored under each task's optimizer_output/<run>/ directory.
python scripts/run_patch.py workspace/mamba-130m-hfThe patcher reloads the original inference script, checks candidate modules with
captured inputs, applies eligible kernels, and measures the patched model. It
also generates a reproducible benchmark script under the workspace's
benchmark/ directory.
torch.compile comparisons are optional:
python scripts/run_patch.py \
workspace/mamba-130m-hf \
--compile \
--compile-mode max-autotune-no-cudagraphsThe default optimizer combines several forms of context:
- Generic API references and reusable Triton examples can be retrieved through
RAG. Disable this with
--no-rag. - A short, static optimization playbook provides general correctness and kernel
implementation guidance. Disable this with
--no-playbook. - Restart-on-plateau summarizes evidence from the current search episode before
starting a new trajectory. Disable this with
--no-restart. - Family-scoped skill memory is optional and starts empty in this release. Enable
runtime use and updates with
--skill.
No model-specific or task-specific distilled family skills are bundled with the repository.
By default, optimization performs KernelBench correctness and performance evaluation, isolated candidate execution, and validation with captured real inputs. Additional controls include:
# Probe memory-safety failures with blocking launches and allocator jitter
python scripts/run_optimize.py ... --runtime-safety-gate
# Record a torch.compile baseline during extraction
python scripts/run_extract.py ... --inductor-baselineRun python scripts/run_optimize.py --help for the complete search and
validation configuration.
LLM4LLM/
|-- llm4llm/
| |-- adapter/ # KernelBench-compatible task generation
| |-- analyzer/ # Optional performance and Inductor analysis
| |-- extractor/ # Profiling, tracing, and task extraction
| |-- knowledge_base/ # Generic API references and RAG examples
| |-- optimizer/ # Closed-loop kernel optimization agent
| |-- patcher/ # Model integration and validation
| `-- transforms/ # Inference-time graph transformations
|-- scripts/ # Extraction, optimization, and patching CLIs
|-- user_inputs/ # Reproducible inference workloads
|-- benchmark/ # End-to-end and KernelBench evaluation scripts
`-- tests/ # Infrastructure tests
The benchmark/ directory contains end-to-end scripts for Qwen3, Llama 2,
Mamba, Mamba 2, and RecurrentGemma workloads. KernelBench utilities are under
benchmark/kernelbench/. Model paths are configured through .env; generated
results and model weights are not included in the repository.
pytestThe infrastructure test suite does not require model weights. GPU-backed end-to-end workflows require the environment described above.
LLM4LLM is released under the MIT License. See LICENSE.