CPU, GPU AND RAM

GPU kernel fusion: removing intermediate tensors changes the schedule

In this article
THE QUICK TAKE

Fusion saves selected intermediates and launch boundaries while increasing local resource pressure; compare the complete operation and its live-value footprint.

Kernel fusion combines work that would otherwise run as separate GPU kernels. Its appeal often comes from avoiding intermediate tensor writes, reads, and launch boundaries. The tradeoff is a larger execution unit with different register, shared-memory, and scheduling requirements. Understanding the lifetime of an intermediate value is more informative than simply counting how many kernels disappeared from a trace.

An intermediate tensor is a transport boundary

Suppose one kernel transforms each element and another immediately applies an elementwise function. The separate design can materialize a full intermediate array in device memory. A fused design may keep each transformed value in a register until its next use. This can reduce memory traffic, but only for values whose consumers fit the fused execution structure. Shared results needed by unrelated blocks require a more careful communication strategy.

Primary reference: CUDA Programming Guide: Writing SIMT Kernels.

Live values consume registers

Combining stages can extend the time during which several values must remain available. The compiler may allocate more registers or spill values into memory. Either effect can offset the traffic saved by removing an intermediate tensor. Inspect resource use and spill behavior alongside the final kernel duration. A shorter trace with fewer launches is not sufficient evidence that the resulting instruction stream is more efficient on the target GPU.

Primary reference: CUDA C++ Best Practices Guide.

A launch boundary can provide global ordering

Separate kernels ordered in a stream create a clear phase boundary between all producers and later consumers. Removing that boundary can expose cross-block synchronization requirements that were previously handled by completion of the earlier kernel. An elementwise fusion is therefore different from fusing a global reduction with arbitrary dependent work. Verify the dependency graph before attempting to preserve the old behavior with an unsafe device-wide spin barrier inside an ordinary launch.

Primary reference: Nsight Compute Profiling Guide.

Different stages can prefer different shapes

A matrix operation and a following transformation can favor different tile sizes or thread mappings. Fusion forces a joint choice or introduces extra internal work to reconcile them. A specialized library kernel may also outperform a custom fused implementation despite an additional small launch. Compare against the actual strong baseline, including supported epilogue operations where available. Replacing several optimized stages with one naïve stage can be a regression even when traffic is lower.

Test shapes and accuracy together

Measure representative dimensions, short batches, and boundary cases, including setup and any layout conversion introduced by fusion. Compare numerical output with a suitable tolerance because reassociation and changed arithmetic paths can affect rounding. Record saved intermediate bytes alongside resource changes. The useful conclusion identifies which mechanism dominates for which shapes, preserving an unfused fallback when the combined kernel does not suit the workload or required numerical behavior.

Related reading: A GPU inference benchmark you can trust; Reproducible inference stacks: preserving a GPU pilot’s result.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles