Skip to content
class

LeakyReluBackward

LeakyReluBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for Leaky ReLU y=xy = x if x>0x > 0 else slope⋅x\text{slope} \cdot x.

The slope hyper-parameter is captured on the node so the eager backward can rebuild the leaky mask without re-reading it from outside the graph. Because the standard UnaryKernel::forward does not thread the slope argument through dispatch(), an explicit forward() override is used; cpu_kernel is provided alongside it so the per-device dispatcher can still pick a CPU path when MLX is unavailable.

Math

\qquad \frac{\partial y}{\partial x} = \begin{cases} 1, & x > 0 \\ s, & x \le 0 \end{cases}$$ where $s$ is the configured slope.

Attributes

schema_v1OpSchema
"leaky_relu", AmpPolicy::KeepInput.
slope_double
Negative-side slope captured at forward time; defaults to 0.01.

Static methods

2
static

cpu_kernel

CpuStorage cpu_kernel(const CpuStorage & a, const int & out_shape, Dtype dt, double slope)

CPU kernel used by the dispatcher; mirrors forward but acts on a pre-resolved CPU storage.

static

forward

→TensorImplPtr
int forward(const int & a, double slope)

Custom forward that captures slope on the backward node and wires the autograd edges manually (the default UnaryKernel::forward signature does not thread an extra parameter through dispatch()).

Parameters

aTensorImplPtr
Input activation.
slopedouble
Negative-side slope s such that y=s xy = s\,x for x≤0x \le 0.

Returns

TensorImplPtr

Forward output with autograd edges wired to LeakyReluBackward.

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Eager-mode backward: applies the per-element leaky mask (1 for x>0x > 0, slope_ otherwise) to the upstream gradient.

fn

grad_formula_impl

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