Skip to content
class

NanToNumBackward

NanToNumBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for nan_to_num — finite values pass through, NaN and the infinities are replaced by the three scalars.

Math

∂y∂x=1[x finite]\frac{\partial y}{\partial x} = \mathbb{1}[x \text{ finite}]

The replaced slots are constants, so they carry no gradient; everywhere else the op is the identity. This is the reference framework's rule, and it used to be missing: the op returned a detached tensor, so nan_to_num(x) + x differentiated to 1 instead of 2 — silently.

See Also

nan_to_num_op, ClipBackward.

Notes

The three scalars are recorded on the trace (scope.set_attr) for the compile path; the gradient needs only the saved input.

Static methods

1
static

forward

→TensorImplPtr
int forward(const int & a, double nan_val, double posinf_val, double neginf_val)

Forward — replaces NaN / +inf / -inf and wires the backward node.

Parameters

aconst TensorImplPtr&
Input tensor.
nan_val, posinf_val, neginf_valdouble
Replacements for NaN, +inf and -inf.

Returns

TensorImplPtr

Tensor of the input's shape, dtype and device.

Methods

2
fn

grad_formula

int grad_formula(const int & g)

g⊙1[x finite]g \odot \mathbb{1}[x \text{ finite}] from the saved input.

fn

grad_formula_impl

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

Graph-mode equivalent — where(isfinite(x), g, 0) — so the gradient itself stays differentiable.