---
title: "ClikaRT::graph::transforms"
sidebar_label: "transforms"
description: "Namespace ClikaRT::graph::transforms"
---

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

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

One factory per built-in transform, named as the transform is: `transforms::remove_double_permute()` returns the handle whose `name()` is `"remove_double_permute"`.

## Functions

### defaults() {#defaults}

<div class="api-signature">

`std::vector<`[`Transform`](../Transform.md)`>` **`defaults`**(`const` [`DefaultsOptions`](../DefaultsOptions.md)`&` `options` `=` [`DefaultsOptions`](../DefaultsOptions.md)`()`)

</div>

The default transform list for `options`, taking the choices as stated: the shape-fixation transforms join under `specialize` or `shape_fixation`, and `drop_boundary_permutes` under either channels-last choice. A graph's own defaults can differ, since they follow how the graph was built (a graph compiled from ONNX without pinned shapes keeps the shape-fixation transforms without `specialize`): [`ModelGraph::default_transforms`](../ModelGraph.md#default_transforms) returns the list `optimize()` runs on that graph.

<div class="api-declared">

Declared in `ClikaRT/graph/optimize.h`, line 117

</div>

### rewrite() {#rewrite}

<div class="api-signature">

[`Transform`](../Transform.md) **`rewrite`**(<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::string` `name`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`Pattern`](../Pattern.md) `pattern`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`RewriteFunction`](../index.md#RewriteFunction) `replace_fn`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;[`RewriteCondition`](../index.md#RewriteCondition) `condition_fn` `=` `{}`,<br />
&nbsp;&nbsp;&nbsp;&nbsp;`std::vector<`[`Transform`](../Transform.md)`>` `runs_after` `=` `{}`<br />
)

</div>

A transform named `name` that rewrites the occurrences of `pattern`, for [`OptimizeOptions::transforms`](../OptimizeOptions.md#transforms).

Each iteration of `optimize` searches the graph for `pattern` once ([`ModelGraph::find_pattern`](../ModelGraph.md#find_pattern)), then takes the occurrences in the order the search returns them. An occurrence is skipped, and `condition_fn` never sees it, when an earlier replacement in the same iteration removed one of its nodes, when it is not convex ([`ModelGraph::is_convex`](../ModelGraph.md#is_convex): a path leaves it and comes back, and no replacement keeps that path), or when nothing outside it reads a value it makes. Every other occurrence goes to `condition_fn` (an empty one accepts every occurrence), and one it accepts is replaced: `replace(match.outputs, replace_fn over the match, match.inputs)`. The next iteration searches again, so an occurrence a replacement creates is rewritten then.

A failure ends the run: `condition_fn`'s or `replace_fn`'s own failure or throw, and a refusal from `replace` (a new value of another type or count, a KV cache layer's attention among the operators it removes, a cyclic graph). `optimize` returns it with its status and code, the message naming the occurrence's first matched node, and leaves the graph as it found it. The report's row reads as any transform you write: `applications` counts the iterations in which the rule replaced at least one occurrence.

The rule keeps `pattern`, and copies of the handle share it and the two functions. Both run on `optimize`'s thread; a handle two runs use at once runs them on two threads.

Refuses as [`Status::InvalidArgument`](../../../index.md#Status): an empty `name`, the name of one of the runtime's transforms, a `runs_after` entry of the same name, an empty `replace_fn`, a pattern with no node, and a pattern `find_pattern` refuses (an edge naming a node never added, more than [`kMaxOptionalPatternNodes`](../index.md#kMaxOptionalPatternNodes) optional nodes, a node added with an empty predicate).

<div class="api-declared">

Declared in `ClikaRT/graph/rewrite.h`, line 156

</div>

## `ClikaRT/graph/optimize.h` {#optimize-h}

```cpp
#include <ClikaRT/graph/optimize.h>
```

Options and report for `ModelGraph::optimize`: the graph optimizer's defaults, its iteration cap, the transforms it runs, and what one run did.
