AI-powered GPU kernel optimizer: automatically profiles, diagnoses, and rewrites CUDA kernels to maximize performance.
KernelScope is built on top of AgentLoop. It replaces AgentLoop's mock environment with a real GPU execution backend, compiling kernels with nvcc and profiling them with NVIDIA's ncu.
Supports three GPU backends out of the box: Google Colab, local NVIDIA GPU, and RunPod.
kernelscope.py (local machine)
│
│ HTTP or subprocess
▼
GPU Environment (Colab / Local / RunPod)
│
├── nvcc → compiles CUDA kernel
└── ncu → profiles memory bandwidth, compute throughput, etc.
│
▼
Real metric value returned to AgentLoop agent
│
▼
Agent reflects, proposes next optimization
│
▼
Repeat until goal met or max iterations reached
kernelscope/
server/
models.py ← Pydantic request/response models
colab_server.py ← FastAPI server (runs ON Colab/RunPod GPU)
environment/
base.py ← abstract BaseEnvironment interface
colab_environment.py ← HTTP client → Colab GPU via ngrok
local_environment.py ← subprocess → local NVIDIA GPU
runpod_environment.py ← HTTP client → RunPod GPU instance
config.py ← central config, all values from .env
kernelscope.py ← entry point
colab_notebook.ipynb ← Colab notebook to start the GPU server
Switch backends with one line in .env:
| Backend | environment= |
Requirements |
|---|---|---|
| Google Colab | colab |
Free Google account, ngrok token |
| Local GPU | local |
NVIDIA GPU, nvcc, ncu installed |
| RunPod | runpod |
RunPod account, pod URL |
- Python 3.11+
- AgentLoop installed
- One of the GPU backends above
git clone https://github.com/jagkt/agentloop
cd agentloop
pip install -e ".[all]"
cd ..git clone https://github.com/jagkt/kernelscope
cd kernelscope
pip install -e .cp .env.example .envEdit .env:
# LLM Provider
provider=groq
GROQ_API_KEY=your_groq_key
# GPU Backend
environment=colab
COLAB_URL=https://your-ngrok-url.ngrok-free.app
# Optimization settings
METRIC=l1tex__t_bytes_pipe_lsu_mem_global_op_ld.sum.per_second
GOAL_METRIC=0.95
max_iterations=5
# Logging
KERNELSCOPE_LOG_LEVEL=INFO
KERNELSCOPE_LOG_FILE=logs/kernelscope.logOpen colab_notebook.ipynb in Google Colab:
- Runtime → Change runtime type → T4 GPU
- Run all cells in order
- Copy the printed
COLAB_URLinto your.env
python kernelscope.py[Agent] Starting loop. Max iterations: 5
[Agent] ---- Iteration 1 ----
[Agent] Thinking about iteration 1...
[Agent] Rationale: Starting with shared memory tiling to reduce global memory accesses...
[Agent] Feedback: compiled=True, metric=74.53, goal_met=False
[Agent] Reflecting... metric=74.53, goal not yet met. Continuing.
[Agent] ---- Iteration 2 ----
[Agent] Thinking about iteration 2...
[Agent] Rationale: Previous attempt achieved 74.53 Gbyte/s. Increasing tile reuse...
[Agent] Feedback: compiled=True, metric=90.82, goal_met=False
[Agent] Reflecting... metric=90.82, goal not yet met. Continuing.
[Agent] ---- Iteration 3 ----
[Agent] Thinking about iteration 3...
[Agent] Rationale: Trying vectorized loads with float4 to maximize memory throughput...
[Agent] Feedback: compiled=True, metric=95.21, goal_met=True
[Agent] Goal met at iteration 3 with metric=95.21. Stopping.
[Agent] Loop complete.
All settings controlled from .env — no code changes needed:
| Key | Default | Description |
|---|---|---|
provider |
groq |
LLM provider: groq, ollama, anthropic, openai |
environment |
colab |
GPU backend: colab, local, runpod |
METRIC |
l1tex__... |
ncu metric name to optimize |
GOAL_METRIC |
0.95 |
Target metric value |
max_iterations |
5 |
Max agent loop iterations |
COLAB_URL |
— | ngrok URL for Colab backend |
RUNPOD_URL |
— | HTTP URL for RunPod backend |
Any ncu metric can be used:
| Metric | What it measures |
|---|---|
l1tex__t_bytes_pipe_lsu_mem_global_op_ld.sum.per_second |
Global memory read bandwidth |
sm__throughput.avg.pct_of_peak_sustained_elapsed |
SM compute throughput |
l1tex__t_sector_hit_rate.pct |
L1 cache hit rate |
Run ncu --query-metrics to see all available metrics.
KernelScope is a consumer of AgentLoop, not a fork:
AgentLoop KernelScope
Agent ←────── kernelscope.py
Memory (imports Agent, Memory)
Modifier
Feedback ←────── colab_environment.py
(returns real Feedback objects)
AgentLoop provides the reasoning loop. KernelScope provides the real GPU environment.
- v0.2 — Support for Triton kernels
- v0.3 — Multiple metric optimization (Pareto front)
- v0.4 — Support for AMD GPUs via ROCm/HIP
- v0.5 — Batch kernel optimization across a codebase
- v1.0 — REST API for CI/CD integration
See CONTRIBUTING.md for guidelines.
MIT