CPU kernel authoring
SkillDev toolsUse when writing, optimizing, or benchmarking a C++ CPU kernel with AVX2 or AVX512 intrinsics for the Hugging Face kernels ecosystem. Not for CUDA kernels: use cuda.
Available today. Use it from your connected AI after setup.
No other account needed.
Connect ahel once, and every AI you use reads what you have installed.
Then ask your AI: use the CPU kernel authoring skill
What this skill tells your AI
The instructions your AI receives, as published by outlinedriven/outline-driven-development in .devin/skills/cpu-kernel-authoring/SKILL.md and read by ahel’s review.
Contract
| Field | Bound contract |
|---|---|
| Trigger | A C++ CPU kernel for the Hugging Face kernels ecosystem must be written, optimized, or benchmarked with AVX2 or AVX512 intrinsics against a PyTorch baseline. |
| Authority | Reversible local. Writes C++ kernel sources, build.toml, and torch_binding.cpp under the kernel directory, a wheel under dist/, the installed kernel package in the active Python environment, and trial state under trials/<kernel_name>/ and output/. Rollback is version control for the sources, pip uninstall <package> for the package, and removal of dist/, trials/<kernel_name>/, and output/. No remote mutation. |
| Side effect | Kernel sources and build files change; a wheel is built and installed; trial directories and result records accumulate. |
| Done | The kernel passes the correctness check in scripts/benchmark_cpu.py, every trial up to max_trials has run or the speedup exceeded early_stop_speedup, and the best trial is finalized into output/ with its final measurement; or a failure class from the table below is reported with the recovery step taken. |
Inputs
- Kernel name (required): the trial-tree label, for example
my_rmsnorm. Used only bytrial_manager.py, which accepts it as a single directory name undertrials/, never a path. - Baseline file (required): a
baseline.pythat definesget_inputs()and eitherget_reference_output()or aModelclass (with optionalget_init_inputs()). It is the ground truth for correctness and the speed reference. - Operation name (required): the plain name
analyze_op.py --oplooks up, for examplerms_norm. - Input shapes (required): comma-separated shape strings for
analyze_op.py --shapes, for example"1024x4096,2048x8192". - Package and function path (required from step 5): the installed package name, for example
my_kernel, and its callable aspackage.function, for examplemy_kernel.rms_norm.benchmark_cpu.pyandcpu_profiler.pytake this path as their--op; it is not the operation name above. - Toolchain (required): Python 3.11+ (
validate_cpu_kernel.pyparsesbuild.tomlwith the standard-librarytomllib),kernel-builder,pip, PyYAML (imported byscripts/config.py),numactl(used by the pinned benchmark in step 8), a C++ compiler with AVX512 support, and PyTorch.perfis required only whenperf_stat_enabledis true.
The work has two phases. The correctness phase builds the tiers in order (generic ATen fallback, optional AVX2, AVX512) and each tier must pass correctness before the next starts. The performance phase iterates on the AVX512 tier through the trial tree until max_trials is exhausted or early_stop_speedup is exceeded.
Procedure
- Read
scripts/config.yamland notemax_trials,early_stop_speedup,perf_stat_enabled,vtune_enabled,build_command, andinstall_command. Use those two commands wherever this procedure builds or installs. Done when: every value is known. - Run
python scripts/analyze_op.py --op <op_name> --shapes <shapes>and read the compute and memory characteristics and the suggested SIMD strategy. Readreferences/workflow_details.mdfor the analysis and design steps. Done when: the kernel type is fixed as element-wise, reduction, GEMM, or attention. - Run
python scripts/trial_manager.py init <kernel_name> <baseline_file>. Done when:trials/<kernel_name>/exists and records the baseline. - Write the generic tier:
<kernel>_cpu/cpu_features.hppin the kernel's own namespace, the dispatcher<kernel>_cpu/<kernel>_cpu.cppwith an ATen-only fallback, the bridge<kernel>_cpu/<kernel>_cpu_torch.cpp,torch-ext/torch_binding.cppusing theregistration.hmacros, andbuild.tomlwith one[kernel.*]section per tier andinclude = ["<kernel>_cpu"]in every section. Readreferences/runtime_dispatch.yaml,references/build_system.md,references/implementation_reference.md, andreferences/correctness.yamlwhile writing. Runpython scripts/validate_cpu_kernel.py <kernel_dir>. Done when: validation reports no error. - Build and install with the configured commands, by default
kernel-builder build --releasethenpip install dist/*.whl --force-reinstall --no-deps. Done when:python -c "import <package>"succeeds. - Run
python scripts/benchmark_cpu.py <baseline_file> --kernel-package <package> --op <package>.<function>. The correctness check walks tuples, lists, and dicts element-wise, requires equal structure, dtype, and shape, and compares each tensor leaf in its own dtype: half an ulp relative for bf16 and fp16,atol=1e-6, rtol=1e-5for fp32,atol=1e-12, rtol=1e-9for fp64, exact for integer and bool. Widen with--atoland--rtolonly when the kernel's accumulation order legitimately differs from the reference, and record the reason in the trial's--strategy. Done when: correctness passes and the baseline and kernel times are recorded; on failure, go to the failure table. - Add the AVX512 tier in its own translation unit
<kernel>_cpu/<kernel>_avx512.cppwith its owncxx-flagssection (-mavx512f -mavx512bf16 -mavx512vlfor element-wise kernels; GEMM kernels add-mavx512dq -mavx512bw -mavx512vbmi -mamx-tile -mamx-bf16 -mamx-int8), and-fopenmpin every SIMD section. Add an AVX2 tier only when it gives an element-wise kernel a measurable benefit; GEMM kernels dispatch AVX512 to fallback. Repeat steps 4 to 6, then runpython scripts/trial_manager.py save <kernel_name> <kernel_dir> --strategy "<description>"and record the numbers withpython scripts/trial_manager.py result <kernel_name> <trial_id> --correctness pass --speedup <x> --baseline_us <us> --kernel_us <us>. Done when: the AVX512 tier is correct and trial t0 is recorded. This ends the correctness phase. - Pin the benchmark to one NUMA node for every later measurement:
numactl --cpunodebind=0 --membind=0 python scripts/benchmark_cpu.py ... --baseline-us <cached>, where the cached value comes frompython scripts/trial_manager.py baseline-us <kernel_name>. Done when: the pinned command is the one used from here on. - When
perf_stat_enabledis true, runpython scripts/cpu_profiler.py --kernel-package <package> --op <package>.<function>once after the first benchmarked trial. Read IPC together with the L1 and LLC miss rates: a pure AVX512 FMA loop has low IPC by design, so a memory bound is claimed only when a miss rate is also high. Done when: the profile is read and the next change is chosen fromreferences/optimization_strategies.md. - For each remaining trial up to
max_trials: change one thing in the AVX512 tier (blocking, prefetch, unrolling, threading, or a different algorithm fromreferences/simd_optimization_patterns.yaml,references/memory_patterns.yaml,references/threading_patterns.yaml,references/dtype_optimizations.yaml,references/brgemm_patterns.yaml,references/quantized_gemm_patterns.yaml, andreferences/optimization_levels.yaml), validate, build, benchmark, thensavewith--parent <best_or_current_id>andresult. A regression branches back to the best trial; a plateau after two trials changes the algorithm, data layout, or fusion instead of sweeping the same knobs. Stop early only when the speedup exceedsearly_stop_speedup. Done when:max_trialstrials are recorded or the early stop fired. - Run
python scripts/trial_manager.py finalize <kernel_name> output/, then re-run the pinnedbenchmark_cpu.pywithout--baseline-usfor the final measurement. Readreferences/huggingface-kernels-integration.mdif the kernel is to be published to the Hub. Done when:output/holds the best trial's sources and its final correctness and speedup are recorded.
Modify only .cpp and .hpp files, torch_binding.cpp, and build.toml. Do not write new benchmark or timing scripts; scripts/benchmark_cpu.py is the only timing source. When a script fails, report the error rather than working around it.
Failure and recovery
| Failure class | Behavior |
|---|---|
scripts/config.yaml missing | Stop and report. Do not assume trial counts. |
analyze_op.py reports no matmul, reduction, or activation for the op | The script recognizes norm, softmax, gemm, linear, matmul, attention, gelu, silu, relu, moe, and megablocks by name and classifies anything else as plain element-wise. Classify the kernel type by hand from the baseline and continue at step 3. |
validate_cpu_kernel.py reports an error | Fix the named file or build.toml section; re-run validation. A validation fix does not count as a trial. |
kernel-builder build fails | Read the compiler output; fix the source or the section's cxx-flags; rebuild. |
| Correctness fails | Read the leaf path, dtype, and worst-element values in the benchmark output. A wrong second output or a dtype or shape change is a binding or dispatcher bug; a one-ulp bf16 difference on many elements is a rounding-mode or conversion bug; a tail-only difference is missing tail handling; a large scattered difference is an alignment bug. Fix on the same branch, rebuild, re-benchmark. Do not enter the performance phase with a failing kernel. |
| Kernel slower than baseline on small tensors | Add a num_tokens threshold below which the dispatcher calls the ATen fallback; see references/threading_patterns.yaml. |
perf unavailable or perf stat returns no counters | Continue without profiling and report it; choose the next trial from references/optimization_levels.yaml. |
| Speedup regressed | save the next trial with --parent set to the best trial id from python scripts/trial_manager.py best <kernel_name>. |
| Plateau after two or more trials | Change algorithm, data layout, or fusion strategy. Do not sweep the same parameters. |
max_trials reached below early_stop_speedup | Finalize the best trial and report the speedup reached and the trial tree from trial_manager.py status. |
Output
- Kernel sources:
<kernel>_cpu/withcpu_features.hpp, the dispatcher, the bridge, the AVX512 implementation, and any AVX2 implementation, plustorch-ext/torch_binding.cppandbuild.toml. - Installed package: the wheel under
dist/and the installed<package>. - Trial tree:
trials/<kernel_name>/with each saved trial, its parent, strategy, correctness, and timing. - Correctness report: the
benchmark_cpu.pyoutput naming per-dtype tolerances and, on failure, the leaf path of each mismatch. - Performance report: baseline and kernel microseconds and speedup from the NUMA-pinned run.
- Final kernel:
output/holding the best trial's sources and its final measurement.
Signals
- GitHub stars
- 54
- Forks
- 10
- Last commit
- Sep 2026
ahel review
K1binfo
installs-packagesK6low
bundled executables the agent is told to runK1binfo
installs-packages (in scripts/benchmark_cpu.py)K1binfo
installs-packages (in scripts/config.py)K1binfo
installs-packages (in references/huggingface-kernels-integration.md)K1binfo
installs-packages (in references/workflow_details.md)K1binfo
installs-packages (in scripts/config.yaml)
Automated review, not a security audit. Ruleset v1+k2.
Advanced
- Catalog kind
- skill
- Gateway key
cpu-kernel-authoring- Source
- github.com/outlinedriven/outline-driven-development