Files
torus-ml/docs/references/api/torus.ml.onnx.convert.md
zeekay e037e8f7ea torus: one name for the package, and it is ours
The distribution called itself torus-ml while shipping a package directory
named concrete, so the name a user installs and the name they import disagreed
— and most of the tree already said torus.ml, which could not resolve against
src/concrete. Moved src/concrete to src/torus and pointed the include at it.

The dependency was Zama's concrete-python from their index. It is torus-fhe
from ours, which is what luxfhe/torus already ships: that frontend declares
torus-fhe and ships a torus/fhe module, so every 'from torus.fhe' in this tree
was already naming our compiler and only the dependency lagged.

Two distributions share one namespace, which is the shape the upstream this
forked from used and the reason the split works: torus-fhe gives torus.fhe,
torus-ml gives torus.ml.

Swept every other spelling — identifiers, enum members, workflow variables,
filenames, notebooks. LICENSE and NOTICE keep the upstream attribution a fork
owes; the figures carry the old name in JPEG metadata a text sweep cannot
reach.
2026-08-27 13:20:50 -07:00

5.4 KiB

module torus.ml.onnx.convert

ONNX conversion related code.

Global Variables

  • IMPLEMENTED_ONNX_OPS
  • OPSET_VERSION_FOR_ONNX_EXPORT

function fuse_matmul_bias_to_gemm

fuse_matmul_bias_to_gemm(onnx_model: ModelProto)

Fuse sequence of matmul -> add into a gemm node.

Args:

  • onnx_model (onnx.ModelProto): A onnx model to optimize using Mat-Mult + Add -> Gemm

Returns:

  • onnx.ModelProto: the optimized onnx model

function get_equivalent_numpy_forward_from_torch

get_equivalent_numpy_forward_from_torch(
    torch_module: Module,
    dummy_input: Union[Tensor, Tuple[Tensor, ]],
    output_onnx_file: Union[NoneType, Path, str] = None
) → Tuple[Callable[, Tuple[ndarray, ]], Union[ModelProto, NoneType], Callable[, Tuple[ndarray, ]], ModelProto]

Get the numpy equivalent forward of the provided torch Module.

Args:

  • torch_module (torch.nn.Module): the torch Module for which to get the equivalent numpy forward.
  • dummy_input (Union[torch.Tensor, Tuple[torch.Tensor, ...]]): dummy inputs for ONNX export.
  • output_onnx_file (Optional[Union[Path, str]]): Path to save the ONNX file to. Will use a temp file if not provided. Defaults to None.

Returns:

  • ONNXAndNumpyForwards: The function that will execute the equivalent numpy code to the passed torch_module and the generated ONNX model.

function preprocess_onnx_model

preprocess_onnx_model(
    onnx_model: ModelProto,
    check_model: bool
) → Tuple[Union[ModelProto, NoneType], ModelProto]

Preprocess the ONNX model to be used for numpy execution.

Args:

  • onnx_model (onnx.ModelProto): the ONNX model for which to get the equivalent numpy forward.
  • check_model (bool): set to True to run the onnx checker on the model. Defaults to True.

Raises:

  • ValueError: Raised if there is an unsupported ONNX operator required to convert the torch model to numpy.

Returns:

  • Tuple[Optional[onnx.ModelProto], onnx.ModelProto]: The preprocessing ONNX model and preprocessed ONNX model. The preprocessing model is None if there is no preprocessing required.

function get_equivalent_numpy_forward_from_onnx

get_equivalent_numpy_forward_from_onnx(
    onnx_model: ModelProto,
    check_model: bool = True
) → Tuple[Callable[, Tuple[ndarray, ]], Union[ModelProto, NoneType], Callable[, Tuple[ndarray, ]], ModelProto]

Get the numpy equivalent forward of the provided ONNX model.

Args:

  • onnx_model (onnx.ModelProto): the ONNX model for which to get the equivalent numpy forward.
  • check_model (bool): set to True to run the onnx checker on the model. Defaults to True.

Returns:

  • ONNXAndNumpyForwards: The function that will execute the equivalent numpy function.

function get_equivalent_numpy_forward_from_onnx_tree

get_equivalent_numpy_forward_from_onnx_tree(
    onnx_model: ModelProto,
    check_model: bool = True,
    lsbs_to_remove_for_trees: Optional[Tuple[int, int]] = None
) → Tuple[Callable[, Tuple[ndarray, ]], ModelProto]

Get the numpy equivalent forward of the provided ONNX model for tree-based models only.

Args:

  • onnx_model (onnx.ModelProto): the ONNX model for which to get the equivalent numpy forward.
  • check_model (bool): set to True to run the onnx checker on the model. Defaults to True.
  • lsbs_to_remove_for_trees (Optional[Tuple[int, int]]): This parameter is exclusively used for optimizing tree-based models. It contains the values of the least significant bits to remove during the tree traversal, where the first value refers to the first comparison (either "less" or "less_or_equal"), while the second value refers to the "Equal" comparison operation. Default to None, as it is not applicable to other types of models.

Returns:

  • Tuple[NumpyForwardCallable, onnx.ModelProto]: The function that will execute the equivalent numpy function.