> 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.quantization.base_quantized_op.md).

# concrete.ml.quantization.base\_quantized\_op

[![](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/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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L32)

### 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
* **`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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L74)

#### method `__init__`

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

***

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

#### 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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L426)

#### method `call_impl`

```python
call_impl(*inputs: ndarray, **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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L457)

#### 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 instance of the QuantizedOp produces Concrete Numpy 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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L208)

#### 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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L399)

#### 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/0.4.x/src/concrete/ml/quantization/base_quantized_op.py#L229)

#### method `q_impl`

```python
q_impl(*q_inputs: QuantizedArray, **attrs) → QuantizedArray
```

Execute the quantized forward.

**Args:**

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

**Returns:**

* **`QuantizedArray`**: The returned quantized value.


---

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