Skip to main content

ClikaRT::ops::sum

function

sum()

Tensor sum(
    Tensor x,
    ClikaRT::Span<const std::int64_t> dims = {},
    bool keepdim = false,
    DataType dtype = DataType::Undefined
)

Sums x over dims.

out=idimsxi\mathrm{out} = \sum_{i \in \text{dims}} x_i

Empty dims reduces EVERY dimension (a 0-D result unless keepdim). Integer inputs accumulate and return at their own dtype unless dtype widens them explicitly.

Parameters

  • x: the input tensor; any arithmetic dtype.
  • dims: axes to reduce; empty = all axes.
  • keepdim: keep reduced axes as size-1 dims; default false.
  • dtype: output/accumulator dtype; DataType::Undefined (the default) keeps x's dtype.

Returns: the reduced tensor; shape = x minus dims (or size-1 there under keepdim).

Throws

  • ClikaRT::Error: (INVALID_ARGUMENT) when a dims entry is out of range for x's rank, or dtype cannot represent the sum.
auto row_totals = ClikaRT::ops::sum(x, {-1}); // [.., N] -> [..]
auto grand = ClikaRT::ops::sum(x); // 0-D total

Declared in ClikaRT/compute/ops.h, line 4446