> 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.2/developer-guide/api/concrete.ml.quantization.quantized_module_passes.md).

# concrete.ml.quantization.quantized\_module\_passes.md

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

## module `concrete.ml.quantization.quantized_module_passes`

Optimization passes for QuantizedModules.

***

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

### class `PowerOfTwoScalingRoundPBSAdapter`

Detect neural network patterns that can be optimized with round PBS.

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

#### method `__init__`

```python
__init__(qmodule: QuantizedModule) → None
```

***

**property num\_ignored\_valid\_patterns**

Get the number of optimizable patterns that were ignored.

Patterns could be ignored since a number of rounding bits was set manually through the compilation function.

**Returns:**

* `result` (int): number of patterns that could be optimized but were not

***

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

#### method `compute_op_predecessors`

```python
compute_op_predecessors() → DefaultDict[Union[QuantizedOp, NoneType], List[Tuple[Union[QuantizedOp, NoneType], str]]]
```

Compute the predecessors for each QuantizedOp in a QuantizedModule.

Stores, for each quantized op, a list of quantized ops that produce its inputs. Currently only the first input of the operations is considered as it is, usually, the encrypted input.

**Returns:**

* `result` (PredecessorsType): a dictionary containing a hierarchy of op predecessors

***

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

#### method `detect_patterns`

```python
detect_patterns(
    predecessors: DefaultDict[Optional[QuantizedOp], List[Tuple[Optional[QuantizedOp], str]]]
) → Dict[QuantizedMixingOp, Tuple[List[Union[QuantizedOp, NoneType]], Union[QuantizedOp, NoneType]]]
```

Detect the patterns that can be optimized with roundPBS in the QuantizedModule.

**Args:**

* `predecessors` (PredecessorsType): Module predecessor operation list

**Returns:**

* `result` (PatternDict): list of optimizable patterns

***

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

#### method `match_path_pattern`

```python
match_path_pattern(
    predecessors: DefaultDict[Optional[QuantizedOp], List[Tuple[Optional[QuantizedOp], str]]],
    nodes_in_path: List[Optional[QuantizedOp]],
    input_producer_of_path: Optional[QuantizedOp]
) → bool
```

Determine if a pattern has the structure that makes it viable for roundPBS.

**Args:**

* `predecessors` (PredecessorsType): Module predecessor operation list
* `nodes_in_path` (List\[QuantizedOp]): list of quantized ops in the pattern
* `input_producer_of_path` (Optional\[QuantizedOp]): operation that produces the input

**Returns:**

* `result` (bool): whether the pattern can be optimized

***

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

#### method `process`

```python
process() → Dict[QuantizedMixingOp, Tuple[List[Union[QuantizedOp, NoneType]], Union[QuantizedOp, NoneType]]]
```

Analyze an ONNX graph and detect Gemm/Conv patterns that can use RoundPBS.

We want to detect a gemm/conv node whose weights/bias are Brevitas QAT, and whose input is produced by a Brevitas QAT node that is applied on the output of another Gemm/conv node. Optionally a Relu can be placed before this input quantization node.

Nothing will be done if rounding is already specified.

**Returns:**

* `result` (PatternDict): a dictionary containing for each Conv/Gemm node for which round PBS can be applied based on power-of-two scaling factors

***

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

#### method `process_patterns`

```python
process_patterns(
    valid_paths: Dict[QuantizedMixingOp, Tuple[List[Optional[QuantizedOp]], Optional[QuantizedOp]]]
) → Dict[QuantizedMixingOp, Tuple[List[Union[QuantizedOp, NoneType]], Union[QuantizedOp, NoneType]]]
```

Configure the rounding bits of roundPBS for the optimizable operations.

**Args:**

* `valid_paths` (PatternDict): list of optimizable patterns

**Returns:**

* `result` (PatternDict): list of patterns actually optimized with roundPBS


---

# 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.2/developer-guide/api/concrete.ml.quantization.quantized_module_passes.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.
