Skip to content

About

AI-powered GPU kernel optimizer: automatically profiles, diagnoses, and rewrites CUDA kernels to maximize performance.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

KernelScope

CI

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.


How It Works

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

Architecture

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

GPU Backends

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

Quickstart

Prerequisites

  • Python 3.11+
  • AgentLoop installed
  • One of the GPU backends above

1. Install AgentLoop

git clone https://github.com/jagkt/agentloop
cd agentloop
pip install -e ".[all]"
cd ..

2. Install KernelScope

git clone https://github.com/jagkt/kernelscope
cd kernelscope
pip install -e .

3. Configure .env

cp .env.example .env

Edit .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.log

4. Start GPU server (Colab backend)

Open colab_notebook.ipynb in Google Colab:

  1. Runtime → Change runtime type → T4 GPU
  2. Run all cells in order
  3. Copy the printed COLAB_URL into your .env

5. Run

python kernelscope.py

Example Output

[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.

Configuration Reference

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

Supported Metrics

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.


Relationship to AgentLoop

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.


Roadmap

  • 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

Contributing

See CONTRIBUTING.md for guidelines.


License

MIT

About

AI-powered GPU kernel optimizer: automatically profiles, diagnoses, and rewrites CUDA kernels to maximize performance.

Topics

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages