---
title: "ClikaRT::ops::layer_norm"
sidebar_label: "layer_norm"
description: "Layer normalization over the trailing normalized_shape dims of input."
---

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

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

## layer\_norm() {#layer_norm}

<div class="api-signature">

[`Tensor`](../Tensor.md) **`layer_norm`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Tensor`](../Tensor.md) `input`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`ClikaRT::Span`](../Span.md)`<const std::int64_t>` `normalized_shape`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `weight` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`OptionalTensor`](../../index.md#OptionalTensor) `bias` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::optional<double>` `eps` `=` `std::nullopt`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::optional<`[`Activation`](./index.md#Activation)`>` `activation` `=` `std::nullopt`<br />
)

</div>

Layer normalization over the trailing `normalized_shape` dims of `input`.

$$
\mathrm{out} = \frac{x - \mathrm{E}[x]}
    {\sqrt{\mathrm{Var}[x] + \varepsilon}} \cdot \gamma + \beta
$$

Statistics are computed per position over the trailing `normalized_shape` dims (channels-last layout, `[N, .., C]`). The output keeps `input`'s shape AND dtype. An optional `activation` is fused onto the post-affine value (gated kinds are not accepted here).

**Parameters**

- `input`: the input; its trailing dims must equal `normalized_shape`.
- `normalized_shape`: the trailing dims that form one group, e.g. `{hidden}`.
- `weight`: optional per-element scale (gamma), shaped `normalized_shape`.
- `bias`: optional per-element shift (beta), shaped `normalized_shape`.
- `eps`: stability floor on the variance; `std::nullopt` selects 1e-5.
- `activation`: optional fused activation; absent = none.

**Returns:** a new tensor, same shape and dtype as `input`.

**Throws**

- `ClikaRT::Error`: (`INVALID_ARGUMENT`) when the trailing dims do not match `normalized_shape`, or a provided weight/bias is shaped differently.

```cpp
auto y = ClikaRT::ops::layer_norm(x, {hidden}, gamma, beta);
```

<div class="api-declared">

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

</div>
