Skip to content
class

NegBackward

NegBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise negation y=−xy = -x.

The gradient is the negation of the upstream signal; no input or output needs to be saved because the rule contains no functional dependence on xx. kSavesInput = false opts out of the default save behaviour provided by UnaryKernel.

Math

\frac{\partial \mathcal{L}}{\partial x} = -\frac{\partial \mathcal{L}} {\partial y}.$$

Attributes

schema_v1OpSchema
Registered as "neg" with AmpPolicy::Promote — integer inputs are upcast to float before dispatch so the algebraic identity holds for the same dtype on the return path.
kSavesInputbool
false. Backward needs no saved value.

Notes

Dispatch: Accelerate vDSP_vneg (CPU) / MLX negative (GPU).

Static methods

1
static

dispatch

int dispatch(backend::IBackend & be, const int & a, const int & s, Dtype dt)

Forward — calls IBackend::neg to compute y=−xy = -x.

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Backward — ∂y/∂x=−1\partial y/\partial x = -1, scaled by grad_out.

fn

grad_formula_impl

int grad_formula_impl(const int & g, const int &, const int &)

Graph-mode backward (default-impl helper): returns -g so the chain stays differentiable for higher-order gradients.