> 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/0.4/developer-guide/api/concrete.ml.torch.compile.md).

# concrete.ml.torch.compile

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

## module `concrete.ml.torch.compile`

torch compilation function.

### **Global Variables**

* **MAXIMUM\_TLU\_BIT\_WIDTH**
* **DEFAULT\_P\_ERROR\_PBS**
* **OPSET\_VERSION\_FOR\_ONNX\_EXPORT**

***

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

### function `convert_torch_tensor_or_numpy_array_to_numpy_array`

```python
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.

***

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

### function `compile_torch_model`

```python
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=8,
    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.

***

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

### function `compile_onnx_model`

```python
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=8,
    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.

***

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

### function `compile_brevitas_qat_model`

```python
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: Optional[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.


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zama.org/concrete-ml/0.4/developer-guide/api/concrete.ml.torch.compile.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
