|
| 1 | +--- |
| 2 | +name: magpie |
| 3 | +description: Benchmarks LLM inference and drives GPU kernel optimization with Magpie. Use when the user wants to benchmark vLLM, SGLang, or Atom; capture torch traces; post-process inference traces with TraceLens into prefill/decode and roofline reports; identify top bottleneck kernels or map profiler names to source; analyze or compare HIP, CUDA, PyTorch, or Triton kernels; validate and rank optimized variants; run local, container, or Ray workloads; or mentions Magpie, TraceLens, gap analysis, TTFT, TPOT, kernel evaluation, or AMD GPU optimization. |
| 4 | +--- |
| 5 | + |
| 6 | +# Magpie |
| 7 | + |
| 8 | +Use Magpie for three connected jobs: |
| 9 | + |
| 10 | +1. Benchmark an inference workload and collect throughput, latency, and traces. |
| 11 | +2. Analyze or compare GPU kernels for correctness and performance. |
| 12 | +3. Drive an optimization loop from a benchmark bottleneck to source, candidate kernels, and end-to-end validation. |
| 13 | + |
| 14 | +Describe only capabilities supported by the checked-out Magpie version. Do not infer support for an unverified ROCm, GPU, framework, or experimental integration. |
| 15 | + |
| 16 | +## Choose the workflow |
| 17 | + |
| 18 | +| User goal | Workflow | |
| 19 | +|---|---| |
| 20 | +| Evaluate one implementation | `analyze` | |
| 21 | +| Rank two or more implementations | `compare` | |
| 22 | +| Measure model-serving performance | `benchmark` | |
| 23 | +| Find expensive kernels in existing traces | standalone gap analysis | |
| 24 | +| Explain a profiled inference workload | benchmark → TraceLens post-processing → stage/roofline review | |
| 25 | +| Optimize an end-to-end workload | benchmark → TraceLens/gap analysis → source mapping → analyze/compare → re-benchmark | |
| 26 | + |
| 27 | +Use a YAML config for reproducible or multi-step work. Use inline CLI arguments for small exploratory runs. |
| 28 | + |
| 29 | +## Preflight |
| 30 | + |
| 31 | +1. Locate the Magpie repository or installed package. |
| 32 | +2. Check the local interface before constructing commands: |
| 33 | + |
| 34 | + ```bash |
| 35 | + magpie --help |
| 36 | + magpie analyze --help |
| 37 | + magpie compare --help |
| 38 | + magpie benchmark --help |
| 39 | + magpie --gpu-info |
| 40 | + ``` |
| 41 | + |
| 42 | +3. Check required tools, model access, GPU visibility, writable output space, and container or Ray access as applicable. |
| 43 | +4. Read the repository compatibility matrix before making version claims. Treat ROCm or hardware not listed there as unverified until tested. |
| 44 | +5. Record the exact config, model revision, image, environment variables, GPU allocation, and Magpie commit for benchmark comparisons. |
| 45 | + |
| 46 | +Run from the Magpie repository root, install with `pip install -e .`, or use `python -m Magpie` when the `magpie` entry point is unavailable. |
| 47 | + |
| 48 | +## Analyze a kernel |
| 49 | + |
| 50 | +Prefer a config when correctness or profiler settings matter: |
| 51 | + |
| 52 | +```bash |
| 53 | +magpie analyze --kernel-config path/to/kernel.yaml |
| 54 | +``` |
| 55 | + |
| 56 | +For a quick single-kernel run: |
| 57 | + |
| 58 | +```bash |
| 59 | +magpie analyze path/to/kernel.hip --type hip --testcase "./run_test.sh" |
| 60 | +``` |
| 61 | + |
| 62 | +Supported public kernel types are `hip`, `cuda`, `pytorch`, and `triton`. Use `--no-perf` only when the user wants correctness or execution validation without profiling. |
| 63 | + |
| 64 | +Do not equate successful execution with numerical correctness. Supply a representative testcase whenever an optimized result will be accepted or rejected. |
| 65 | + |
| 66 | +## Compare kernel variants |
| 67 | + |
| 68 | +Compare at least two implementations and identify the baseline explicitly: |
| 69 | + |
| 70 | +```bash |
| 71 | +magpie compare --kernel-config path/to/compare.yaml |
| 72 | +``` |
| 73 | + |
| 74 | +Keep inputs, tolerances, warmup, iteration count, GPU allocation, and profiler settings identical across candidates. Reject candidates that fail correctness before considering performance rankings. |
| 75 | + |
| 76 | +For PyTorch without a testcase, Magpie's built-in check only verifies that each result is finite; it does not prove numerical equivalence between variants. Require a testcase for numerical validation. |
| 77 | + |
| 78 | +## Benchmark inference |
| 79 | + |
| 80 | +Prefer a checked-in benchmark config: |
| 81 | + |
| 82 | +```bash |
| 83 | +magpie benchmark --benchmark-config path/to/benchmark.yaml |
| 84 | +``` |
| 85 | + |
| 86 | +The stable public CLI supports `vllm`, `sglang`, and `atom`. It supports direct `docker` and `local` run modes; use YAML configuration and the repository's Ray examples for distributed execution. Do not advertise integrations that exist only in internal enums or partial code paths as stable. |
| 87 | + |
| 88 | +Enable profiling deliberately: profiler runs perturb latency and should not replace a clean baseline. Compare throughput, completed requests, TTFT, TPOT, ITL, and end-to-end latency using equivalent workloads. |
| 89 | + |
| 90 | +## Post-process traces with TraceLens |
| 91 | + |
| 92 | +Enable TraceLens in the profiled benchmark YAML; torch traces are its required input: |
| 93 | + |
| 94 | +```yaml |
| 95 | +benchmark: |
| 96 | + profiler: |
| 97 | + torch_profiler: |
| 98 | + enabled: true |
| 99 | + tracelens: |
| 100 | + enabled: true |
| 101 | + analysis_mode: inference |
| 102 | + analysis_stages: all |
| 103 | + export_format: csv |
| 104 | +``` |
| 105 | +
|
| 106 | +Use `analysis_mode: inference` for vLLM/SGLang. It splits the rank-0 trace into `prefilldecode`, `decode`, and `prefill` stages when available, runs TraceLens post-processing, and writes full stage reports plus compact `*_kernel_roofline_simple.csv` files under the benchmark workspace's `tracelens/` directory. For direct PyTorch trace reporting, use `analysis_mode: pytorch`. |
| 107 | + |
| 108 | +Open the compact roofline CSVs first. Rank rows by `kernel_time_ms_sum` or `time_pct`; then use `roofline_bound`, arithmetic intensity, achieved TFLOP/s or TB/s, and `pct_roofline_mean` to form an optimization hypothesis. Confirm `benchmark_report.json.tracelens_analysis` has outputs and no error before treating post-processing as successful. Use `analysis_mode: pytorch` when the task specifically needs the legacy direct single-rank or multi-rank collective reports. |
| 109 | + |
| 110 | +Magpie's integrated TraceLens stage produces CSV/Excel analysis artifacts, not an agent-written `analysis.md`. If the user requests a prioritized agentic report, pass the captured trace to the separate `tracelens-analysis-orchestrator` skill when installed; keep that result distinct from Magpie's benchmark report. |
| 111 | + |
| 112 | +## Analyze existing traces and find source |
| 113 | + |
| 114 | +Run standalone gap analysis with `--trace-dir` directly on `benchmark`: |
| 115 | + |
| 116 | +```bash |
| 117 | +magpie benchmark \ |
| 118 | + --trace-dir path/to/torch_trace \ |
| 119 | + --top-k 20 \ |
| 120 | + --find-kernel-sources \ |
| 121 | + --kernel-source-repos path/to/repository |
| 122 | +``` |
| 123 | + |
| 124 | +Do not insert a `gap-analysis` positional token; it is not a CLI subcommand. Inspect the generated aggregate and per-rank CSVs, and preserve source-mapping confidence rather than assuming every normalized kernel name maps uniquely. |
| 125 | + |
| 126 | +## Drive the optimization loop |
| 127 | + |
| 128 | +1. Run an unprofiled baseline benchmark and save its config and report. |
| 129 | +2. Repeat with torch profiling and TraceLens inference post-processing enabled. |
| 130 | +3. Review stage-level TraceLens roofline summaries to classify dominant operations and likely compute, memory, or communication limits. |
| 131 | +4. Run gap analysis over the representative steady-state window to rank concrete kernels. |
| 132 | +5. Select bottlenecks by total contribution, not only single-dispatch duration. |
| 133 | +6. Map the selected kernel to source and an executable testcase. |
| 134 | +7. Generate isolated candidate implementations; preserve the baseline. |
| 135 | +8. Use `analyze` for iteration, then `compare` with correctness gates to rank candidates. |
| 136 | +9. Re-run the original unprofiled benchmark with the winning candidate and the same workload. Report both kernel-level and end-to-end changes, including regressions. |
| 137 | + |
| 138 | +Stop before claiming success if correctness is unproven, the benchmark inputs changed, the source mapping is uncertain, or the end-to-end improvement is within run-to-run noise. |
| 139 | + |
| 140 | +## Use MCP tools when available |
| 141 | + |
| 142 | +Prefer Magpie MCP tools for structured agent workflows such as hardware inspection, kernel discovery, config generation, analyze/compare, optimization suggestions, result lookup, report comparison, Ray job management, and benchmark batches. |
| 143 | + |
| 144 | +Do not pass a CLI `analyze_report.json` wrapper directly to an MCP tool that expects one result object's `performance_state` and `performance_result`. Do not assume every CLI option exists in MCP; kernel-source enrichment is currently exposed by the CLI gap-analysis path. |
| 145 | + |
| 146 | +## Additional resources |
| 147 | + |
| 148 | +- Full CLI reference: [reference.md](reference.md) |
| 149 | +- Copy-paste command examples: [examples.md](examples.md) |
0 commit comments