Manuscript Offprint

arXiv:2609.06694

ForgeStencil: Automating Per-Case Stencil Specialization from Kernels to 100+ Real Applications

Yaojian Chen, Yuxuan Li, Wubing Wan, Lin Gan, Guangwen Yang, Zhiyuan Liu

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Yaojian Chen Tsinghua University Beijing, China

Lin Gan Tsinghua University Beijing, China

Yuxuan Li Tsinghua University Beijing, China

Guangwen Yang Tsinghua University Beijing, China

Wubing Wan Tsinghua University Beijing, China

Zhiyuan Liu Tsinghua University Beijing, China

Abstract

On modern GPUs the fastest stencil kernel depends on the stencil's shape, precision, and host application, and a kernel tuned for one case is rarely fastest for another. Stencil DSLs, code generators, and autotuners instead pursued generality: a single human-authored method reused across cases and validated mainly on microbenchmarks, because per-case specialization was too costly to scale.

ForgeStencil starts from the opposite assumption. Code-synthesis agents have reduced that cost enough to build a fresh solution for each case and deploy it end-to-end in real software. A Kernel Agent synthesizes CUDA and forges a per-configuration matrix of specialized operators that matches or exceeds the strongest publicly available state-of-the-art (SOTA) baseline for each case. An App Agent extends the principle to whole applications: it locates hotspots, rewrites application structure, and validates and integrates each change across 100+ real industrial and scientific codes. Most of the measured speedup comes from structural and host-side rewrites, with pure stencil replacement in the minority; the gain also correlates negatively with how well the baseline was already tuned, consistent with gains coming from specialization rather than generic reuse. Every result is checked by a measurement-integrity harness that turns an overstated speedup into a system-level error.

The forged kernels reach a same-precision f32 geometric mean of 2.35× against the per-case SOTA baselines (fp16 gains, 1.95×, disclosed separately; A100), and the end-to-end application median is 1.41× across 100 codes, all against same-architecture GPU baselines with program-provided validation and timing. Of 116 candidates, every one that failed the correctness, measurement, or speedup criteria was recorded as rejected or downgraded instead of written up as a speedup.

Keywords: stencil computation, GPU, code synthesis, LLM agents, auto-research, performance engineering, measurement integrity

1 Introduction

Stencil computation, repeated updates of a grid from a fixed neighborhood of its elements, underlies a large fraction of industrial and scientific computing, from computational fluid dynamics to climate codes. These kernels are memory-bound, and approaching the hardware limit is among the most expert-intensive tasks in high-performance computing: the optimization that reaches peak performance changes qualitatively with the exact stencil, grid shape, precision, and target architecture.

The dominant response over two decades has been to build general methods that cover many stencils, shapes, and applications from one human-authored recipe: stencil DSLs [17, 25, 30], polyhedral and GPU code generators [13, 20, 27, 35], temporal-blocking frameworks [22, 32, 34], and autotuners [1, 6, 36]. Such systems do emit a per-case kernel, by compiling a schedule or searching a parameter space, but the recipe and the space it can reach are fixed in advance by a human, and peak stencil performance is per-case: the optimal kernel structure changes qualitatively across grid shapes, and an application's bottleneck is typically a structural flaw in that application rather than a generically slow operator. Generic frameworks stayed in place on cost: writing a specialized solution from scratch was too expensive. That concession is now less compelling: code-synthesis agents produce a correct, specialized implementation from scratch at a marginal cost that is no longer prohibitive [9, 15, 24], so one can forge a fresh solution per case rather than reuse a general one, removing, case by case, the generality tax the generic method incurred. We call this paradigm Forge Engineering; the ForgeTrain line of work develops the same paradigm for deep-learning training frameworks [23].

ForgeStencil automates per-case forging at two levels. A Kernel Agent synthesizes concrete CUDA to forge a per-configuration matrix of specialized operators, each kept only if a harness measures it faster and correct against per-case public-SOTA upper bounds. An App Agent extends

CCS Concepts: • Computing methodologies → Parallel programming languages; • General and reference → Empirical studies.

the same principle to whole applications: it locates the performance-critical region, forges a per-app optimization (usually a structural rewrite), and validates and integrates it against the application's own tests and timing across 100+ real industrial and scientific codes. Both levels are checked by a measurement-integrity harness, and their evidence stays strictly separate: operator-level claims rest only on kernel microbenchmarks, application-level claims only on the application harness, and neither endorses the other. The contribution is the automation of specialization: we develop and demonstrate Forge Engineering on stencil computation and propose no new stencil algorithm.

Forge Engineering has no workload-level admission test: the same hypothesis–measure–revise loop applies wherever specialization can pay off in high-performance computing. We choose stencil as the demonstration domain because its instances are clearly specified, per-case SOTA baselines are abundant, correctness is easy to check, and performance can be measured separately at the kernel and application levels. The dual-agent architecture itself is general: its collaboration mirrors a standard high-performance-computing engineering workflow. The agents form a hypothesis, implement an optimization, measure the result, and iterate on the feedback. Applying the architecture to FFT, SpMV, tensor contraction, or GEMM fusion requires only modest adaptation of the domain knowledge and operator space.

Contributions. We make three contributions: an operator-level matrix with knowledge reuse, a two-stage application-level forging workflow, and a measurement-integrity harness that verifies the results at both levels separately.

  • Operator-level forging. We design a configuration-indexed matrix of specialized operators and use validated cells as a knowledge base: application-level forging first attempts a match and invokes the Kernel Agent only when no cell applies. Across mainstream stencil types, shapes, and f16/f32/f64 precisions, the matrix reaches or exceeds per-case public-SOTA upper bounds (same-precision f32 geometric mean 2.35× on A100).

  • Application-level forging. We design a staged workflow that performs framework-level optimization before operator-level forging: it first rewrites scheduling, fusion, and host–device traffic, then reuses or creates a hot-operator cell, and finally validates and integrates the change. The workflow covers 100+ real industrial and scientific codes and achieves an end-to-end median speedup of 1.41× against same-architecture GPU baselines.

  • A measurement-integrity harness. Separate operator and application harnesses verify the two levels through executable checks on agent-read-only measurement surfaces, making an overstated speedup a

system-level error and retracting the framework's own numbers when required.

References (10)