> For the complete documentation index, see [llms.txt](https://docs.zama.org/concrete-ml/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zama.org/concrete-ml/1.1/developer-guide/api/concrete.ml.onnx.onnx_impl_utils.md).

# concrete.ml.onnx.onnx\_impl\_utils.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_impl_utils.py#L0)

## module `concrete.ml.onnx.onnx_impl_utils`

Utility functions for onnx operator implementations.

***

[![](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_impl_utils.py#L12)

### function `numpy_onnx_pad`

```python
numpy_onnx_pad(
    x: ndarray,
    pads: Tuple[int, ],
    pad_value: Union[float, int, ndarray] = 0,
    int_only: bool = False
) → ndarray
```

Pad a tensor according to ONNX spec, using an optional custom pad value.

**Args:**

* `x` (numpy.ndarray): input tensor to pad
* `pads` (List\[int]): padding values according to ONNX spec
* `pad_value` (Optional\[Union\[float, int]]): value used to fill in padding, default 0
* `int_only` (bool): set to True to generate integer only code with Concrete

**Returns:**

* `res` (numpy.ndarray): the input tensor with padding applied

***

[![](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_impl_utils.py#L66)

### function `compute_conv_output_dims`

```python
compute_conv_output_dims(
    input_shape: Tuple[int, ],
    kernel_shape: Tuple[int, ],
    pads: Tuple[int, ],
    strides: Tuple[int, ],
    ceil_mode: int
) → Tuple[int, ]
```

Compute the output shape of a pool or conv operation.

See <https://pytorch.org/docs/stable/generated/torch.nn.AvgPool2d.html> for details on the computation of the output shape.

**Args:**

* `input_shape` (Tuple\[int, ...]): shape of the input to be padded as N x C x H x W
* `kernel_shape` (Tuple\[int, ...]): shape of the conv or pool kernel, as Kh x Kw (or n-d)
* `pads` (Tuple\[int, ...]): padding values following ONNX spec: dim1\_start, dim2\_start, .. dimN\_start, dim1\_end, dim2\_end, ... dimN\_end where in the 2-d case dim1 is H, dim2 is W
* `strides` (Tuple\[int, ...]): strides for each dimension
* `ceil_mode` (int): set to 1 to use the `ceil` function to compute the output shape, as described in the PyTorch doc

**Returns:**

* `res` (Tuple\[int, ...]): shape of the output of a conv or pool operator with given parameters

***

[![](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_impl_utils.py#L110)

### function `compute_onnx_pool_padding`

```python
compute_onnx_pool_padding(
    input_shape: Tuple[int, ],
    kernel_shape: Tuple[int, ],
    pads: Tuple[int, ],
    strides: Tuple[int, ],
    ceil_mode: int
) → Tuple[int, ]
```

Compute any additional padding needed to compute pooling layers.

The ONNX standard uses ceil\_mode=1 to match TensorFlow style pooling output computation. In this setting, the kernel can be placed at a valid position even though it contains values outside of the input shape including padding. The ceil\_mode parameter controls whether this mode is enabled. If the mode is not enabled, the output shape follows PyTorch rules.

**Args:**

* `input_shape` (Tuple\[int, ...]): shape of the input to be padded as N x C x H x W
* `kernel_shape` (Tuple\[int, ...]): shape of the conv or pool kernel, as Kh x Kw (or n-d)
* `pads` (Tuple\[int, ...]): padding values following ONNX spec: dim1\_start, dim2\_start, .. dimN\_start, dim1\_end, dim2\_end, ... dimN\_end where in the 2-d case dim1 is H, dim2 is W
* `strides` (Tuple\[int, ...]): strides for each dimension
* `ceil_mode` (int): set to 1 to use the `ceil` function to compute the output shape, as described in the PyTorch doc

**Returns:**

* `res` (Tuple\[int, ...]): shape of the output of a conv or pool operator with given parameters

***

[![](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_impl_utils.py#L156)

### function `onnx_avgpool_compute_norm_const`

```python
onnx_avgpool_compute_norm_const(
    input_shape: Tuple[int, ],
    kernel_shape: Tuple[int, ],
    pads: Tuple[int, ],
    strides: Tuple[int, ],
    ceil_mode: int
) → Union[ndarray, float]
```

Compute the average pooling normalization constant.

This constant can be a tensor of the same shape as the input or a scalar.

**Args:**

* `input_shape` (Tuple\[int, ...]): shape of the input to be padded as N x C x H x W
* `kernel_shape` (Tuple\[int, ...]): shape of the conv or pool kernel, as Kh x Kw (or n-d)
* `pads` (Tuple\[int, ...]): padding values following ONNX spec: dim1\_start, dim2\_start, .. dimN\_start, dim1\_end, dim2\_end, ... dimN\_end where in the 2-d case dim1 is H, dim2 is W
* `strides` (Tuple\[int, ...]): strides for each dimension
* `ceil_mode` (int): set to 1 to use the `ceil` function to compute the output shape, as described in the PyTorch doc

**Returns:**

* `res` (float): tensor or scalar, corresponding to normalization factors to apply for the average pool computation for each valid kernel position


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
