---
title: "ClikaRT::ops::sum"
sidebar_label: "sum"
description: "Sums input over dims. \\[ \\mathrm{out} = \\sum_{i \\in \\text{dims}} x_i \\] Empty dims reduces EVERY dimension (a 0-D result unless keepdim)."
---

<!-- Generated by tools/doxygen2mdx from Doxygen XML. Do not edit. -->

<span class="api-kind api-kind--function">function</span>

## sum() {#sum}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`sum`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`ClikaRT::Span`](../Span.md)`<const std::int64_t>` `dims` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`bool` `keepdim` `=` `false`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`DataType`](../../index.md#DataType) `dtype` `=` [`DataType::Undefined`](../../index.md#DataType)<br />
)

</div>

Sums `input` over `dims`.

$$
\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`](../dtype/index.md) widens them explicitly.

**Parameters**

- `input`: 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`](../../index.md#DataType) (the default) keeps `input`'s dtype.

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

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when a `dims` entry is out of range for `input`'s rank, or [`dtype`](../dtype/index.md) cannot represent the sum.

```cpp
auto row_totals = ClikaRT::ops::sum(x, {-1});          // [.., N] -> [..]
auto grand      = ClikaRT::ops::sum(x);                // 0-D total
```

<div class="api-declared">

Declared in `ClikaRT/compute/ops.h`, line 4584

</div>
