Python wrappers
Public Python APIs implemented by this engine symbol.Compute the trace (sum of main-diagonal elements) of a matrix or a batch of matrices.
For a 2-D input a of shape [m, n] the result is a scalar
where ranges over
. For a higher-rank input of shape
[b_0, ..., b_{k-1}, m, n] the last two dimensions are
contracted: each slice produces one scalar, and the result
has shape [b_0, ..., b_{k-1}].
Backward (2-D only) scatters to
each diagonal position of a zero-filled matrix of the same shape
as a:
.
For batched inputs (ndim > 2) the output is returned without an
autograd edge — attempts to backpropagate through batched
trace_op will silently produce no gradient.
Math
2-D forward and backward:
Batched forward (no offset, no autograd):
Shape
- Input
[m, n]→ output[] - Input
[b_0, ..., b_{k-1}, m, n]→ output[b_0, ..., b_{k-1}]
Parameters
aTensorImplPtrndim >= 2. Non-null.Returns
TensorImplPtrScalar ([]) for 2-D inputs, or a tensor of shape a.shape[2:] for batched inputs.
Raises
Errora is null or has ndim < 2.Notes
Diagonal offset (analogous to NumPy's trace(offset=...)) is not
currently exposed; only the main diagonal is summed. Autograd is
wired only when ndim == 2 — the batched path returns a plain
non-differentiable tensor by design.