# 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: 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_impl_utils.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.
