Concrete ML
WebsiteLibrariesProducts & ServicesDevelopersSupport
0.5
0.5
  • What is Concrete ML?
  • Getting Started
    • Installation
    • Key Concepts
    • Inference in the Cloud
  • 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
  • Advanced topics
    • Quantization
    • Pruning
    • Compilation
    • Production Deployment
    • Advanced Features
  • 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
      • concrete.ml.common.check_inputs
      • concrete.ml.common.debugging
      • concrete.ml.common.debugging.custom_assert
      • concrete.ml.common.utils
      • concrete.ml.deployment
      • concrete.ml.deployment.fhe_client_server
      • concrete.ml.onnx
      • concrete.ml.onnx.convert
      • concrete.ml.onnx.onnx_model_manipulations
      • concrete.ml.onnx.onnx_utils
      • concrete.ml.onnx.ops_impl
      • concrete.ml.quantization
      • concrete.ml.quantization.base_quantized_op
      • concrete.ml.quantization.post_training
      • concrete.ml.quantization.quantized_module
      • concrete.ml.quantization.quantized_ops
      • concrete.ml.quantization.quantizers
      • concrete.ml.sklearn
      • concrete.ml.sklearn.base
      • concrete.ml.sklearn.glm
      • concrete.ml.sklearn.linear_model
      • concrete.ml.sklearn.protocols
      • concrete.ml.sklearn.qnn
      • concrete.ml.sklearn.rf
      • concrete.ml.sklearn.svm
      • concrete.ml.sklearn.torch_module
      • concrete.ml.sklearn.tree
      • concrete.ml.sklearn.tree_to_numpy
      • concrete.ml.sklearn.xgb
      • concrete.ml.torch
      • concrete.ml.torch.compile
      • concrete.ml.torch.numpy_module
      • concrete.ml.version
Powered by GitBook
On this page
  • module concrete.ml.torch.compile
  • Global Variables
  • function convert_torch_tensor_or_numpy_array_to_numpy_array
  • function compile_torch_model
  • function compile_onnx_model
  • function compile_brevitas_qat_model

Was this helpful?

Export as PDF
  1. Developer Guide
  2. API

concrete.ml.torch.compile

Previousconcrete.ml.torchNextconcrete.ml.torch.numpy_module

Last updated 2 years ago

Was this helpful?

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers

module concrete.ml.torch.compile

torch compilation function.

Global Variables

  • MAXIMUM_TLU_BIT_WIDTH

  • DEFAULT_P_ERROR_PBS

  • OPSET_VERSION_FOR_ONNX_EXPORT


function convert_torch_tensor_or_numpy_array_to_numpy_array

convert_torch_tensor_or_numpy_array_to_numpy_array(
    torch_tensor_or_numpy_array: Union[Tensor, ndarray]
) → ndarray

Convert a torch tensor or a numpy array to a numpy array.

Args:

  • torch_tensor_or_numpy_array (Tensor): the value that is either a torch tensor or a numpy array.

Returns:

  • numpy.ndarray: the value converted to a numpy array.


function compile_torch_model

compile_torch_model(
    torch_model: Module,
    torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
    import_qat: bool = False,
    configuration: Optional[Configuration] = None,
    compilation_artifacts: Optional[DebugArtifacts] = None,
    show_mlir: bool = False,
    n_bits=16,
    use_virtual_lib: bool = False,
    p_error: Optional[float] = 6.3342483999973e-05
) → QuantizedModule

Compile a torch module into an FHE equivalent.

Take a model in torch, turn it to numpy, quantize its inputs / weights / outputs and finally compile it with Concrete-Numpy

Args:

  • torch_model (torch.nn.Module): the model to quantize

  • torch_inputset (Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.

  • import_qat (bool): Set to True to import a network that contains quantizers and was trained using quantization aware training

  • configuration (Configuration): Configuration object to use during compilation

  • compilation_artifacts (DebugArtifacts): Artifacts object to fill during compilation

  • show_mlir (bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo

  • n_bits: the number of bits for the quantization

  • use_virtual_lib (bool): set to use the so called virtual lib simulating FHE computation. Defaults to False

  • p_error (Optional[float]): probability of error of a PBS

Returns:

  • QuantizedModule: The resulting compiled QuantizedModule.


function compile_onnx_model

compile_onnx_model(
    onnx_model: ModelProto,
    torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
    import_qat: bool = False,
    configuration: Optional[Configuration] = None,
    compilation_artifacts: Optional[DebugArtifacts] = None,
    show_mlir: bool = False,
    n_bits=16,
    use_virtual_lib: bool = False,
    p_error: Optional[float] = 6.3342483999973e-05
) → QuantizedModule

Compile a torch module into an FHE equivalent.

Take a model in torch, turn it to numpy, quantize its inputs / weights / outputs and finally compile it with Concrete-Numpy

Args:

  • onnx_model (onnx.ModelProto): the model to quantize

  • torch_inputset (Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.

  • import_qat (bool): Flag to signal that the network being imported contains quantizers in in its computation graph and that Concrete ML should not requantize it.

  • configuration (Configuration): Configuration object to use during compilation

  • compilation_artifacts (DebugArtifacts): Artifacts object to fill during compilation

  • show_mlir (bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo

  • n_bits: the number of bits for the quantization

  • use_virtual_lib (bool): set to use the so called virtual lib simulating FHE computation. Defaults to False.

  • p_error (Optional[float]): probability of error of a PBS

Returns:

  • QuantizedModule: The resulting compiled QuantizedModule.


function compile_brevitas_qat_model

compile_brevitas_qat_model(
    torch_model: Module,
    torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
    n_bits: Union[int, dict],
    configuration: Optional[Configuration] = None,
    compilation_artifacts: Optional[DebugArtifacts] = None,
    show_mlir: bool = False,
    use_virtual_lib: bool = False,
    p_error: Optional[float] = 6.3342483999973e-05,
    output_onnx_file: Union[Path, str] = None
) → QuantizedModule

Compile a Brevitas Quantization Aware Training model.

The torch_model parameter is a subclass of torch.nn.Module that uses quantized operations from brevitas.qnn. The model is trained before calling this function. This function compiles the trained model to FHE.

Args:

  • torch_model (torch.nn.Module): the model to quantize

  • torch_inputset (Dataset): the inputset, can contain either torch tensors or numpy.ndarray, only datasets with a single input are supported for now.

  • n_bits (Union[int,dict]): the number of bits for the quantization

  • configuration (Configuration): Configuration object to use during compilation

  • compilation_artifacts (DebugArtifacts): Artifacts object to fill during compilation

  • show_mlir (bool): if set, the MLIR produced by the converter and which is going to be sent to the compiler backend is shown on the screen, e.g., for debugging or demo

  • use_virtual_lib (bool): set to use the so called virtual lib simulating FHE computation, defaults to False.

  • p_error (Optional[float]): probability of error of a PBS

  • output_onnx_file (str): temporary file to store ONNX model. If None a temporary file is generated

Returns:

  • QuantizedModule: The resulting compiled QuantizedModule.