> 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.3/developer-guide/api/concrete.ml.common.utils.md).

# concrete.ml.common.utils.md

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

## module `concrete.ml.common.utils`

Utils that can be re-used by other pieces of code in the module.

### **Global Variables**

* **SUPPORTED\_FLOAT\_TYPES**
* **SUPPORTED\_INT\_TYPES**
* **SUPPORTED\_TYPES**
* **MAX\_BITWIDTH\_BACKWARD\_COMPATIBLE**
* **USE\_OLD\_VL**
* **QUANT\_ROUND\_LIKE\_ROUND\_PBS**

***

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

### function `replace_invalid_arg_name_chars`

```python
replace_invalid_arg_name_chars(arg_name: str) → str
```

Sanitize arg\_name, replacing invalid chars by \_.

This does not check that the starting character of arg\_name is valid.

**Args:**

* `arg_name` (str): the arg name to sanitize.

**Returns:**

* `str`: the sanitized arg name, with only chars in \_VALID\_ARG\_CHARS.

***

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

### function `generate_proxy_function`

```python
generate_proxy_function(
    function_to_proxy: Callable,
    desired_functions_arg_names: Iterable[str]
) → Tuple[Callable, Dict[str, str]]
```

Generate a proxy function for a function accepting only \*args type arguments.

This returns a runtime compiled function with the sanitized argument names passed in desired\_functions\_arg\_names as the arguments to the function.

**Args:**

* `function_to_proxy` (Callable): the function defined like def f(\*args) for which to return a function like f\_proxy(arg\_1, arg\_2) for any number of arguments.
* `desired_functions_arg_names` (Iterable\[str]): the argument names to use, these names are sanitized and the mapping between the original argument name to the sanitized one is returned in a dictionary. Only the sanitized names will work for a call to the proxy function.

**Returns:**

* `Tuple[Callable, Dict[str, str]]`: the proxy function and the mapping of the original arg name to the new and sanitized arg names.

***

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

### function `get_onnx_opset_version`

```python
get_onnx_opset_version(onnx_model: ModelProto) → int
```

Return the ONNX opset\_version.

**Args:**

* `onnx_model` (onnx.ModelProto): the model.

**Returns:**

* `int`: the version of the model

***

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

### function `manage_parameters_for_pbs_errors`

```python
manage_parameters_for_pbs_errors(
    p_error: Optional[float] = None,
    global_p_error: Optional[float] = None
)
```

Return (p\_error, global\_p\_error) that we want to give to Concrete.

The returned (p\_error, global\_p\_error) depends on user's parameters and the way we want to manage defaults in Concrete ML, which may be different from the way defaults are managed in Concrete.

Principle: - if none are set, we set global\_p\_error to a default value of our choice - if both are set, we raise an error - if one is set, we use it and forward it to Concrete

Note that global\_p\_error is currently set to 0 in the FHE simulation mode.

**Args:**

* `p_error` (Optional\[float]): probability of error of a single PBS.
* `global_p_error` (Optional\[float]): probability of error of the full circuit.

**Returns:**

* `(p_error, global_p_error)`: parameters to give to the compiler

**Raises:**

* `ValueError`: if the two parameters are set (this is *not* as in Concrete-Python)

***

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

### function `check_there_is_no_p_error_options_in_configuration`

```python
check_there_is_no_p_error_options_in_configuration(configuration)
```

Check the user did not set p\_error or global\_p\_error in configuration.

It would be dangerous, since we set them in direct arguments in our calls to Concrete-Python.

**Args:**

* `configuration`: Configuration object to use during compilation

***

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

### function `get_model_class`

```python
get_model_class(model_class)
```

Return the class of the model (instantiated or not), which can be a partial() instance.

**Args:**

* `model_class`: The model, which can be a partial() instance.

**Returns:** The model's class.

***

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

### function `is_model_class_in_a_list`

```python
is_model_class_in_a_list(model_class, a_list)
```

Indicate if a model class, which can be a partial() instance, is an element of a\_list.

**Args:**

* `model_class`: The model, which can be a partial() instance.
* `a_list`: The list in which to look into.

**Returns:** If the model's class is in the list or not.

***

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

### function `get_model_name`

```python
get_model_name(model_class)
```

Return the name of the model, which can be a partial() instance.

**Args:**

* `model_class`: The model, which can be a partial() instance.

**Returns:** the model's name.

***

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

### function `is_classifier_or_partial_classifier`

```python
is_classifier_or_partial_classifier(model_class)
```

Indicate if the model class represents a classifier.

**Args:**

* `model_class`: The model class, which can be a functool's `partial` class.

**Returns:**

* `bool`: If the model class represents a classifier.

***

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

### function `is_regressor_or_partial_regressor`

```python
is_regressor_or_partial_regressor(model_class)
```

Indicate if the model class represents a regressor.

**Args:**

* `model_class`: The model class, which can be a functool's `partial` class.

**Returns:**

* `bool`: If the model class represents a regressor.

***

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

### function `is_pandas_dataframe`

```python
is_pandas_dataframe(input_container: Any) → bool
```

Indicate if the input container is a Pandas DataFrame.

This function is inspired from Scikit-Learn's test validation tools and avoids the need to add and import Pandas as an additional dependency to the project. See <https://github.com/scikit-learn/scikit-learn/blob/98cf537f5/sklearn/utils/validation.py#L629>

**Args:**

* `input_container` (Any): The input container to consider

**Returns:**

* `bool`: If the input container is a DataFrame

***

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

### function `is_pandas_series`

```python
is_pandas_series(input_container: Any) → bool
```

Indicate if the input container is a Pandas Series.

This function is inspired from Scikit-Learn's test validation tools and avoids the need to add and import Pandas as an additional dependency to the project. See <https://github.com/scikit-learn/scikit-learn/blob/98cf537f5/sklearn/utils/validation.py#L629>

**Args:**

* `input_container` (Any): The input container to consider

**Returns:**

* `bool`: If the input container is a Series

***

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

### function `is_pandas_type`

```python
is_pandas_type(input_container: Any) → bool
```

Indicate if the input container is a Pandas DataFrame or Series.

**Args:**

* `input_container` (Any): The input container to consider

**Returns:**

* `bool`: If the input container is a DataFrame orSeries

***

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

### function `check_dtype_and_cast`

```python
check_dtype_and_cast(
    values: Any,
    expected_dtype: str,
    error_information: Optional[str] = ''
)
```

Convert any allowed type into an array and cast it if required.

If values types don't match with any supported type or the expected dtype, raise a ValueError.

**Args:**

* `values` (Any): The values to consider
* `expected_dtype` (str): The expected dtype, either "float32" or "int64"
* `error_information` (str): Additional information to put in front of the error message when raising a ValueError. Default to None.

**Returns:**

* `(Union[numpy.ndarray, torch.utils.data.dataset.Subset])`: The values with proper dtype.

**Raises:**

* `ValueError`: If the values' dtype don't match the expected one or casting is not possible.

***

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

### function `compute_bits_precision`

```python
compute_bits_precision(x: ndarray) → int
```

Compute the number of bits required to represent x.

**Args:**

* `x` (numpy.ndarray): Integer data

**Returns:**

* `int`: the number of bits required to represent x

***

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

### function `is_brevitas_model`

```python
is_brevitas_model(model: Module) → bool
```

Check if a model is a Brevitas type.

**Args:**

* `model`: PyTorch model.

**Returns:**

* `bool`: True if `model` is a Brevitas network.

***

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

### function `to_tuple`

```python
to_tuple(x: Any) → tuple
```

Make the input a tuple if it is not already the case.

**Args:**

* `x` (Any): The input to consider. It can already be an input.

**Returns:**

* `tuple`: The input as a tuple.

***

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

### function `all_values_are_integers`

```python
all_values_are_integers(*values: Any) → bool
```

Indicate if all unpacked values are of a supported integer dtype.

**Args:**

* `*values (Any)`: The values to consider.

**Returns:**

* `bool`: Whether all values are supported integers or not.

***

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

### function `all_values_are_floats`

```python
all_values_are_floats(*values: Any) → bool
```

Indicate if all unpacked values are of a supported float dtype.

**Args:**

* `*values (Any)`: The values to consider.

**Returns:**

* `bool`: Whether all values are supported floating points or not.

***

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

### function `all_values_are_of_dtype`

```python
all_values_are_of_dtype(*values: Any, dtypes: Union[str, List[str]]) → bool
```

Indicate if all unpacked values are of the specified dtype(s).

**Args:**

* `*values (Any)`: The values to consider.
* `dtypes` (Union\[str, List\[str]]): The dtype(s) to consider.

**Returns:**

* `bool`: Whether all values are of the specified dtype(s) or not.

***

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

### function `array_allclose_and_same_shape`

```python
array_allclose_and_same_shape(
    a,
    b,
    rtol: float = 1e-05,
    atol: float = 1e-08,
    equal_nan: bool = False
) → bool
```

Check if two numpy arrays are equal within a tolerances and have the same shape.

**Args:**

* `a` (numpy.ndarray): The first input array
* `b` (numpy.ndarray): The second input array
* `rtol` (float): The relative tolerance parameter
* `atol` (float): The absolute tolerance parameter
* `equal_nan` (bool): Whether to compare NaN’s as equal. If True, NaN’s in a will be considered equal to NaN’s in b in the output array

**Returns:**

* `bool`: True if the arrays have the same shape and all elements are equal within the specified tolerances, False otherwise.

***

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

### class `FheMode`

Enum representing the execution mode.

This enum inherits from str in order to be able to easily compare a string parameter to its equivalent Enum attribute.

**Examples:** fhe\_disable = FheMode.DISABLE

`fhe_disable == "disable"` True

```
 >>> fhe_disable == "execute"
 False

 >>> FheMode.is_valid("simulate")
 True

 >>> FheMode.is_valid(FheMode.EXECUTE)
 True

 >>> FheMode.is_valid("predict_in_fhe")
 False
```


---

# 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/1.3/developer-guide/api/concrete.ml.common.utils.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.
