# concrete.ml.onnx.onnx\_model\_manipulations.md

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L0)

## module `concrete.ml.onnx.onnx_model_manipulations`

Some code to manipulate models.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L11)

### function `simplify_onnx_model`

```python
simplify_onnx_model(onnx_model: ModelProto)
```

Simplify an ONNX model, removes unused Constant nodes and Identity nodes.

**Args:**

* `onnx_model` (onnx.ModelProto): the model to simplify.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L21)

### function `remove_unused_constant_nodes`

```python
remove_unused_constant_nodes(onnx_model: ModelProto)
```

Remove unused Constant nodes in the provided onnx model.

**Args:**

* `onnx_model` (onnx.ModelProto): the model for which we want to remove unused Constant nodes.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L53)

### function `remove_identity_nodes`

```python
remove_identity_nodes(onnx_model: ModelProto)
```

Remove identity nodes from a model.

**Args:**

* `onnx_model` (onnx.ModelProto): the model for which we want to remove Identity nodes.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L78)

### function `keep_following_outputs_discard_others`

```python
keep_following_outputs_discard_others(
    onnx_model: ModelProto,
    outputs_to_keep: Iterable[str]
)
```

Keep the outputs given in outputs\_to\_keep and remove the others from the model.

**Args:**

* `onnx_model` (onnx.ModelProto): the ONNX model to modify.
* `outputs_to_keep` (Iterable\[str]): the outputs to keep by name.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L109)

### function `remove_node_types`

```python
remove_node_types(onnx_model: ModelProto, op_types_to_remove: List[str])
```

Remove unnecessary nodes from the ONNX graph.

**Args:**

* `onnx_model` (onnx.ModelProto): The ONNX model to modify.
* `op_types_to_remove` (List\[str]): The node types to remove from the graph.

**Raises:**

* `ValueError`: Wrong replacement by an Identity node.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L163)

### function `clean_graph_at_node_op_type`

```python
clean_graph_at_node_op_type(
    onnx_model: ModelProto,
    node_op_type: str,
    fail_if_not_found: bool = True
)
```

Clean the graph of the onnx model by removing nodes at the given node type.

Note: the specified node\_type is also removed.

**Args:**

* `onnx_model` (onnx.ModelProto): The onnx model.
* `node_op_type` (str): The node's op\_type whose following nodes will be removed.
* `fail_if_not_found` (bool): If true, abort if the node op\_type is not found

**Raises:**

* `ValueError`: if fail\_if\_not\_found is set

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.1.x/src/concrete/ml/onnx/onnx_model_manipulations.py#L226)

### function `clean_graph_after_node_op_type`

```python
clean_graph_after_node_op_type(
    onnx_model: ModelProto,
    node_op_type: str,
    fail_if_not_found: bool = True
)
```

Clean the graph of the onnx model by removing nodes after the given node type.

**Args:**

* `onnx_model` (onnx.ModelProto): The onnx model.
* `node_op_type` (str): The node's op\_type whose following nodes will be removed.
* `fail_if_not_found` (bool): If true, abort if the node op\_type is not found

**Raises:**

* `ValueError`: if the node op\_type is not found and if fail\_if\_not\_found is set


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.zama.org/concrete-ml/1.1/developer-guide/api/concrete.ml.onnx.onnx_model_manipulations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
