Concrete ML
WebsiteLibrariesProducts & ServicesDevelopersSupport
1.1
1.1
  • What is Concrete ML?
  • Getting Started
    • Installation
    • Key Concepts
    • Inference in the Cloud
    • Demos and Tutorials
  • Built-in Models
    • Linear Models
    • Tree-based Models
    • Neural Networks
    • Pandas
    • Built-in Model Examples
  • Deep Learning
    • Using Torch
    • Using ONNX
    • Step-by-step Guide
    • Deep Learning Examples
    • Debugging Models
    • Optimizing Inference
  • Advanced topics
    • Quantization
    • Pruning
    • Compilation
    • Prediction with FHE
    • Production Deployment
    • Advanced Features
    • Serialization
  • Developer Guide
    • Workflow
      • Set Up the Project
      • Set Up Docker
      • Documentation
      • Support and Issues
      • Contributing
    • Inner Workings
      • Importing ONNX
      • Quantization Tools
      • FHE Op-graph Design
      • External Libraries
    • API
      • concrete.ml.common.check_inputs.md
      • concrete.ml.common.debugging.custom_assert.md
      • concrete.ml.common.debugging.md
      • concrete.ml.common.md
      • concrete.ml.common.serialization.decoder.md
      • concrete.ml.common.serialization.dumpers.md
      • concrete.ml.common.serialization.encoder.md
      • concrete.ml.common.serialization.loaders.md
      • concrete.ml.common.serialization.md
      • concrete.ml.common.utils.md
      • concrete.ml.deployment.deploy_to_aws.md
      • concrete.ml.deployment.deploy_to_docker.md
      • concrete.ml.deployment.fhe_client_server.md
      • concrete.ml.deployment.md
      • concrete.ml.deployment.server.md
      • concrete.ml.deployment.utils.md
      • concrete.ml.onnx.convert.md
      • concrete.ml.onnx.md
      • concrete.ml.onnx.onnx_impl_utils.md
      • concrete.ml.onnx.onnx_model_manipulations.md
      • concrete.ml.onnx.onnx_utils.md
      • concrete.ml.onnx.ops_impl.md
      • concrete.ml.pytest.md
      • concrete.ml.pytest.torch_models.md
      • concrete.ml.pytest.utils.md
      • concrete.ml.quantization.base_quantized_op.md
      • concrete.ml.quantization.md
      • concrete.ml.quantization.post_training.md
      • concrete.ml.quantization.quantized_module.md
      • concrete.ml.quantization.quantized_ops.md
      • concrete.ml.quantization.quantizers.md
      • concrete.ml.search_parameters.md
      • concrete.ml.search_parameters.p_error_search.md
      • concrete.ml.sklearn.base.md
      • concrete.ml.sklearn.glm.md
      • concrete.ml.sklearn.linear_model.md
      • concrete.ml.sklearn.md
      • concrete.ml.sklearn.qnn.md
      • concrete.ml.sklearn.qnn_module.md
      • concrete.ml.sklearn.rf.md
      • concrete.ml.sklearn.svm.md
      • concrete.ml.sklearn.tree.md
      • concrete.ml.sklearn.tree_to_numpy.md
      • concrete.ml.sklearn.xgb.md
      • concrete.ml.torch.compile.md
      • concrete.ml.torch.md
      • concrete.ml.torch.numpy_module.md
      • concrete.ml.version.md
Powered by GitBook

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers
On this page
  • module concrete.ml.onnx.onnx_impl_utils
  • function numpy_onnx_pad
  • function compute_conv_output_dims
  • function compute_onnx_pool_padding
  • function onnx_avgpool_compute_norm_const

Was this helpful?

Export as PDF
  1. Developer Guide
  2. API

concrete.ml.onnx.onnx_impl_utils.md

Previousconcrete.ml.onnx.mdNextconcrete.ml.onnx.onnx_model_manipulations.md

Last updated 1 year ago

Was this helpful?

module concrete.ml.onnx.onnx_impl_utils

Utility functions for onnx operator implementations.


function numpy_onnx_pad

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


function compute_conv_output_dims

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


function compute_onnx_pool_padding

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


function onnx_avgpool_compute_norm_const

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