﻿# CuPy fuse: choose a supported numerical expression rather than wrapping an entire pipeline

Apply CuPy fuse to suitable elementwise and reduction expressions with reusable decorated functions, explicit numerical validation and clear unsupported boundaries.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cupy-fuse-supported-expression-boundary/
Language: en

## Select an expression with a clear purpose

A postprocessor subtracts calibration values, rescales features and computes a simple score. This may offer a suitable expression for fusion. The [CuPy fuse reference](https://docs.cupy.dev/en/stable/reference/generated/cupy.fuse.html) describes a decorator for elementwise or reduction kernels. Begin with a numerical subproblem whose inputs and outputs are clear. Wrapping loading, matrix operations and arbitrary Python logic together makes it harder to establish both support and correctness.

## Respect the documented operation boundary

The [kernel guide](https://docs.cupy.dev/en/stable/user_guide/kernel.html) limits fuse to simple elementwise and reduction operations and identifies unsupported routines such as matrix multiplication and reshape. Inspect the actual expression before expecting a broad pipeline to fuse. Keep unsupported operations outside the selected region or choose another appropriate implementation. A failure at that boundary does not imply the underlying GPU routine is unavailable; it may simply be unsuitable inside this fusion mechanism.

## Reuse the decorated numerical function

The fuse API recommends reusing decorated functions because generated kernels are cached. Define a stable helper rather than rebuilding a local decorated function for every example. Keep the helper's input dtype and dimensionality contract explicit when comparing workloads. Repeated construction, a changed specialization and repeated execution of an existing kernel are different events. Separating them makes a performance explanation more useful than reporting only that a decorator was added to the code.

## Choose a different abstraction when needed

The [ElementwiseKernel reference](https://docs.cupy.dev/en/stable/reference/generated/cupy.ElementwiseKernel.html) describes explicit input parameters, output parameters and an elementwise operation. It can be a clearer contract when a simple expression decorator does not express the intended custom calculation. That choice also brings responsibility for the numerical definition and supported types. Do not escalate to custom kernels merely to preserve the appearance of fusion; identify the unsupported requirement and compare maintainable alternatives.

## Validate semantics before comparing execution

Compare the selected expression with a readable reference over representative dtypes, broadcasting cases and non-finite inputs relevant to the task. For a reduction, use tolerances appropriate to the intended arithmetic rather than assuming bitwise identity. The guides on [parallel reduction order](/journal/learn-parallel-reduction-tree/) and [mixed-precision accumulation](/journal/learn-mixed-precision-accumulation/) explain related numerical choices. A useful fusion result preserves the model's postprocessing contract and has measured benefits for its actual workload, not an invented universal speedup.
