# concrete.ml.quantization.base\_quantized\_op.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/quantization/base_quantized_op.py#L0)

## module `concrete.ml.quantization.base_quantized_op`

Base Quantized Op class that implements quantization for a float numpy op.

### **Global Variables**

* **ONNX\_OPS\_TO\_NUMPY\_IMPL**
* **ALL\_QUANTIZED\_OPS**
* **ONNX\_OPS\_TO\_QUANTIZED\_IMPL**
* **DEFAULT\_MODEL\_BITS**

***

[![](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/quantization/base_quantized_op.py#L40)

### class `QuantizedOp`

Base class for quantized ONNX ops implemented in numpy.

**Args:**

* `n_bits_output` (int): The number of bits to use for the quantization of the output
* `op_instance_name` (str): The name that should be assigned to this operation, used to retrieve it later or get debugging information about this op (bit-width, value range, integer intermediary values, op-specific error messages). Usually this name is the same as the ONNX operation name for which this operation is constructed.
* `int_input_names` (Set\[str]): The set of names of integer tensors that are inputs to this op
* `constant_inputs` (Optional\[Union\[Dict\[str, Any], Dict\[int, Any]]]): The constant tensors that are inputs to this op
* `input_quant_opts` (QuantizationOptions): Input quantizer options, determine the quantization that is applied to input tensors (that are not constants)

[![](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/quantization/base_quantized_op.py#L107)

#### method `__init__`

```python
__init__(
    n_bits_output: int,
    op_instance_name: str,
    int_input_names: Optional[Set[str]] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: Optional[QuantizationOptions] = None,
    **attrs
) → None
```

***

**property int\_input\_names**

Get the names of encrypted integer tensors that are used by this op.

**Returns:**

* `Set[str]`: the names of the tensors

***

[![](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/quantization/base_quantized_op.py#L691)

#### method `calibrate`

```python
calibrate(*inputs: ndarray) → ndarray
```

Create corresponding QuantizedArray for the output of the activation function.

**Args:**

* `*inputs (numpy.ndarray)`: Calibration sample inputs.

**Returns:**

* `numpy.ndarray`: the output values for the provided calibration samples.

***

[![](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/quantization/base_quantized_op.py#L745)

#### method `call_impl`

```python
call_impl(*inputs: Optional[ndarray, QuantizedArray], **attrs) → ndarray
```

Call self.impl to centralize mypy bug workaround.

**Args:**

* `*inputs (numpy.ndarray)`: real valued inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `numpy.ndarray`: return value of self.impl

***

[![](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/quantization/base_quantized_op.py#L779)

#### method `can_fuse`

```python
can_fuse() → bool
```

Determine if the operator impedes graph fusion.

This function shall be overloaded by inheriting classes to test self.\_int\_input\_names, to determine whether the operation can be fused to a TLU or not. For example an operation that takes inputs produced by a unique integer tensor can be fused to a TLU. Example: f(x) = x \* (x + 1) can be fused. A function that does f(x) = x \* (x @ w + 1) can't be fused.

**Returns:**

* `bool`: whether this QuantizedOp instance produces Concrete code that can be fused to TLUs

***

[![](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/quantization/base_quantized_op.py#L322)

#### method `dump`

```python
dump(file: <class 'TextIO'>) → None
```

Dump itself to a file.

**Args:**

* `file` (TextIO): The file to dump the serialized object into.

***

[![](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/quantization/base_quantized_op.py#L198)

#### method `dump_dict`

```python
dump_dict() → Dict
```

Dump itself to a dict.

**Returns:**

* `metadata` (Dict): Dict of serialized objects.

***

[![](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/quantization/base_quantized_op.py#L314)

#### method `dumps`

```python
dumps() → str
```

Dump itself to a string.

**Returns:**

* `metadata` (str): String of the serialized object.

***

[![](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/quantization/base_quantized_op.py#L253)

#### method `load_dict`

```python
load_dict(metadata: Dict)
```

Load itself from a string.

**Args:**

* `metadata` (Dict): Dict of serialized objects.

**Returns:**

* `QuantizedOp`: The loaded object.

***

[![](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/quantization/base_quantized_op.py#L415)

#### classmethod `must_quantize_input`

```python
must_quantize_input(input_name_or_idx: int) → bool
```

Determine if an input must be quantized.

Quantized ops and numpy onnx ops take inputs and attributes. Inputs can be either constant or variable (encrypted). Note that this does not handle attributes, which are handled by QuantizedOp classes separately in their constructor.

**Args:**

* `input_name_or_idx` (int): Index of the input to check.

**Returns:**

* `result` (bool): Whether the input must be quantized (must be a `QuantizedArray`) or if it stays as a raw `numpy.array` read from ONNX.

***

[![](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/quantization/base_quantized_op.py#L330)

#### classmethod `op_type`

```python
op_type()
```

Get the type of this operation.

**Returns:**

* `op_type` (str): The type of this operation, in the ONNX referential

***

[![](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/quantization/base_quantized_op.py#L718)

#### method `prepare_output`

```python
prepare_output(qoutput_activation: ndarray) → QuantizedArray
```

Quantize the output of the activation function.

The calibrate method needs to be called with sample data before using this function.

**Args:**

* `qoutput_activation` (numpy.ndarray): Output of the activation function.

**Returns:**

* `QuantizedArray`: Quantized output.

***

[![](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/quantization/base_quantized_op.py#L436)

#### method `q_impl`

```python
q_impl(
    *q_inputs: Optional[ndarray, QuantizedArray],
    **attrs
) → Union[ndarray, QuantizedArray, NoneType]
```

Execute the quantized forward.

**Args:**

* `*q_inputs (ONNXOpInputOutputType)`: Quantized inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `ONNXOpInputOutputType`: The returned quantized value.

***

[![](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/quantization/base_quantized_op.py#L809)

### class `QuantizedOpUnivariateOfEncrypted`

An univariate operator of an encrypted value.

This operation is not really operating as a quantized operation. It is useful when the computations get fused into a TLU, as in e.g., Act(x) = x || (x + 42)).

[![](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/quantization/base_quantized_op.py#L816)

#### method `__init__`

```python
__init__(
    n_bits_output: int,
    op_instance_name: str,
    int_input_names: Optional[Set[str]] = None,
    constant_inputs: Optional[Dict[str, Any], Dict[int, Any]] = None,
    input_quant_opts: Optional[QuantizationOptions] = None,
    **attrs
) → None
```

***

**property int\_input\_names**

Get the names of encrypted integer tensors that are used by this op.

**Returns:**

* `Set[str]`: the names of the tensors

***

[![](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/quantization/base_quantized_op.py#L691)

#### method `calibrate`

```python
calibrate(*inputs: ndarray) → ndarray
```

Create corresponding QuantizedArray for the output of the activation function.

**Args:**

* `*inputs (numpy.ndarray)`: Calibration sample inputs.

**Returns:**

* `numpy.ndarray`: the output values for the provided calibration samples.

***

[![](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/quantization/base_quantized_op.py#L745)

#### method `call_impl`

```python
call_impl(*inputs: Optional[ndarray, QuantizedArray], **attrs) → ndarray
```

Call self.impl to centralize mypy bug workaround.

**Args:**

* `*inputs (numpy.ndarray)`: real valued inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `numpy.ndarray`: return value of self.impl

***

[![](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/quantization/base_quantized_op.py#L845)

#### method `can_fuse`

```python
can_fuse() → bool
```

Determine if this op can be fused.

This operation can be fused and computed in float when a single integer tensor generates both the operands. For example in the formula: f(x) = x || (x + 1) where x is an integer tensor.

**Returns:**

* `bool`: Can fuse

***

[![](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/quantization/base_quantized_op.py#L322)

#### method `dump`

```python
dump(file: <class 'TextIO'>) → None
```

Dump itself to a file.

**Args:**

* `file` (TextIO): The file to dump the serialized object into.

***

[![](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/quantization/base_quantized_op.py#L198)

#### method `dump_dict`

```python
dump_dict() → Dict
```

Dump itself to a dict.

**Returns:**

* `metadata` (Dict): Dict of serialized objects.

***

[![](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/quantization/base_quantized_op.py#L314)

#### method `dumps`

```python
dumps() → str
```

Dump itself to a string.

**Returns:**

* `metadata` (str): String of the serialized object.

***

[![](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/quantization/base_quantized_op.py#L253)

#### method `load_dict`

```python
load_dict(metadata: Dict)
```

Load itself from a string.

**Args:**

* `metadata` (Dict): Dict of serialized objects.

**Returns:**

* `QuantizedOp`: The loaded object.

***

[![](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/quantization/base_quantized_op.py#L415)

#### classmethod `must_quantize_input`

```python
must_quantize_input(input_name_or_idx: int) → bool
```

Determine if an input must be quantized.

Quantized ops and numpy onnx ops take inputs and attributes. Inputs can be either constant or variable (encrypted). Note that this does not handle attributes, which are handled by QuantizedOp classes separately in their constructor.

**Args:**

* `input_name_or_idx` (int): Index of the input to check.

**Returns:**

* `result` (bool): Whether the input must be quantized (must be a `QuantizedArray`) or if it stays as a raw `numpy.array` read from ONNX.

***

[![](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/quantization/base_quantized_op.py#L330)

#### classmethod `op_type`

```python
op_type()
```

Get the type of this operation.

**Returns:**

* `op_type` (str): The type of this operation, in the ONNX referential

***

[![](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/quantization/base_quantized_op.py#L718)

#### method `prepare_output`

```python
prepare_output(qoutput_activation: ndarray) → QuantizedArray
```

Quantize the output of the activation function.

The calibrate method needs to be called with sample data before using this function.

**Args:**

* `qoutput_activation` (numpy.ndarray): Output of the activation function.

**Returns:**

* `QuantizedArray`: Quantized output.

***

[![](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/quantization/base_quantized_op.py#L436)

#### method `q_impl`

```python
q_impl(
    *q_inputs: Optional[ndarray, QuantizedArray],
    **attrs
) → Union[ndarray, QuantizedArray, NoneType]
```

Execute the quantized forward.

**Args:**

* `*q_inputs (ONNXOpInputOutputType)`: Quantized inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `ONNXOpInputOutputType`: The returned quantized value.

***

[![](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/quantization/base_quantized_op.py#L862)

### class `QuantizedMixingOp`

An operator that mixes (adds or multiplies) together encrypted inputs.

Mixing operators cannot be fused to TLUs.

[![](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/quantization/base_quantized_op.py#L871)

#### method `__init__`

```python
__init__(*args, rounding_threshold_bits: Optional[int] = None, **kwargs) → None
```

Initialize quantized ops parameters plus specific parameters.

**Args:**

* `rounding_threshold_bits` (Optional\[int]): Number of bits to round to.
* `*args`: positional argument to pass to the parent class.
* `**kwargs`: named argument to pass to the parent class.

***

**property int\_input\_names**

Get the names of encrypted integer tensors that are used by this op.

**Returns:**

* `Set[str]`: the names of the tensors

***

[![](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/quantization/base_quantized_op.py#L691)

#### method `calibrate`

```python
calibrate(*inputs: ndarray) → ndarray
```

Create corresponding QuantizedArray for the output of the activation function.

**Args:**

* `*inputs (numpy.ndarray)`: Calibration sample inputs.

**Returns:**

* `numpy.ndarray`: the output values for the provided calibration samples.

***

[![](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/quantization/base_quantized_op.py#L745)

#### method `call_impl`

```python
call_impl(*inputs: Optional[ndarray, QuantizedArray], **attrs) → ndarray
```

Call self.impl to centralize mypy bug workaround.

**Args:**

* `*inputs (numpy.ndarray)`: real valued inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `numpy.ndarray`: return value of self.impl

***

[![](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/quantization/base_quantized_op.py#L882)

#### method `can_fuse`

```python
can_fuse() → bool
```

Determine if this op can be fused.

Mixing operations cannot be fused since it must be performed over integer tensors and it combines different encrypted elements of the input tensors. Mixing operations are Conv, MatMul, etc.

**Returns:**

* `bool`: False, this operation cannot be fused as it adds different encrypted integers

***

[![](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/quantization/base_quantized_op.py#L935)

#### method `cnp_round`

```python
cnp_round(x: Union[ndarray, Tracer], calibrate_rounding: bool) → ndarray
```

Round the input array to the specified number of bits.

**Args:**

* `x` (Union\[numpy.ndarray, fhe.tracing.Tracer]): The input array to be rounded.
* `calibrate_rounding` (bool): Whether to calibrate the rounding (compute the lsbs\_to\_remove)

**Returns:**

* `numpy.ndarray`: The rounded array.

***

[![](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/quantization/base_quantized_op.py#L322)

#### method `dump`

```python
dump(file: <class 'TextIO'>) → None
```

Dump itself to a file.

**Args:**

* `file` (TextIO): The file to dump the serialized object into.

***

[![](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/quantization/base_quantized_op.py#L198)

#### method `dump_dict`

```python
dump_dict() → Dict
```

Dump itself to a dict.

**Returns:**

* `metadata` (Dict): Dict of serialized objects.

***

[![](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/quantization/base_quantized_op.py#L314)

#### method `dumps`

```python
dumps() → str
```

Dump itself to a string.

**Returns:**

* `metadata` (str): String of the serialized object.

***

[![](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/quantization/base_quantized_op.py#L253)

#### method `load_dict`

```python
load_dict(metadata: Dict)
```

Load itself from a string.

**Args:**

* `metadata` (Dict): Dict of serialized objects.

**Returns:**

* `QuantizedOp`: The loaded object.

***

[![](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/quantization/base_quantized_op.py#L895)

#### method `make_output_quant_parameters`

```python
make_output_quant_parameters(
    q_values: Union[ndarray, Any],
    scale: float64,
    zero_point: Union[int, float, ndarray]
) → QuantizedArray
```

Build a quantized array from quantized integer results of the op and quantization params.

**Args:**

* `q_values` (Union\[numpy.ndarray, Any]): the quantized integer values to wrap in the QuantizedArray
* `scale` (float): the pre-computed scale of the quantized values
* `zero_point` (Union\[int, float, numpy.ndarray]): the pre-computed zero\_point of the q\_values

**Returns:**

* `QuantizedArray`: the quantized array that will be passed to the QuantizedModule output.

***

[![](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/quantization/base_quantized_op.py#L415)

#### classmethod `must_quantize_input`

```python
must_quantize_input(input_name_or_idx: int) → bool
```

Determine if an input must be quantized.

Quantized ops and numpy onnx ops take inputs and attributes. Inputs can be either constant or variable (encrypted). Note that this does not handle attributes, which are handled by QuantizedOp classes separately in their constructor.

**Args:**

* `input_name_or_idx` (int): Index of the input to check.

**Returns:**

* `result` (bool): Whether the input must be quantized (must be a `QuantizedArray`) or if it stays as a raw `numpy.array` read from ONNX.

***

[![](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/quantization/base_quantized_op.py#L330)

#### classmethod `op_type`

```python
op_type()
```

Get the type of this operation.

**Returns:**

* `op_type` (str): The type of this operation, in the ONNX referential

***

[![](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/quantization/base_quantized_op.py#L718)

#### method `prepare_output`

```python
prepare_output(qoutput_activation: ndarray) → QuantizedArray
```

Quantize the output of the activation function.

The calibrate method needs to be called with sample data before using this function.

**Args:**

* `qoutput_activation` (numpy.ndarray): Output of the activation function.

**Returns:**

* `QuantizedArray`: Quantized output.

***

[![](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/quantization/base_quantized_op.py#L436)

#### method `q_impl`

```python
q_impl(
    *q_inputs: Optional[ndarray, QuantizedArray],
    **attrs
) → Union[ndarray, QuantizedArray, NoneType]
```

Execute the quantized forward.

**Args:**

* `*q_inputs (ONNXOpInputOutputType)`: Quantized inputs.
* `**attrs`: the QuantizedOp attributes.

**Returns:**

* `ONNXOpInputOutputType`: The returned quantized value.


---

# 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.quantization.base_quantized_op.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.
