Skip to content
class

TruncBackward

TruncBackward

Python wrappers

Public Python APIs implemented by this engine symbol.

Autograd node for element-wise truncation toward zero: y=trunc⁡(x)y = \operatorname{trunc}(x).

Piecewise-constant like floor and ceil, and kept in the graph the same way, with a zero gradient. No backend has a trunc kernel, so the forward picks floor for x≥0x \ge 0 and ceil below — on storages, so no autograd node sits in between. It used to be that composition over tensors, and once floor and ceil kept the graph a where node landed on the path, which refuses a second derivative: fmod and frac lost theirs.

Math

yi=sgn⁡(xi)⌊∣xi∣⌋y_i = \operatorname{sgn}(x_i) \lfloor |x_i| \rfloor, with trunc(-0.0) = -0.0 and trunc(-0.5) = -0.0.

Notes

Integer dtypes are a no-op.

Static methods

1
static

dispatch

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

Forward — floor where a >= 0, ceil elsewhere (NaN included).

Methods

2
fn

grad_formula

int grad_formula(const int & g)

Zero gradient, shaped like the output.

fn

grad_formula_impl

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

Graph-mode backward — zero, so the node stays differentiable under create_graph.