Concrete ML
WebsiteLibrariesProducts & ServicesDevelopersSupport
1.2
1.2
  • What is Concrete ML?
  • Getting Started
    • Installation
    • Key Concepts
    • Inference in the Cloud
    • Demos and Tutorials
  • Built-in Models
    • Linear Models
    • Tree-based Models
    • Neural Networks
    • Nearest Neighbors
    • Pandas
    • Built-in Model Examples
  • Deep Learning
    • Using Torch
    • Using ONNX
    • Step-by-step Guide
    • Deep Learning Examples
    • Debugging Models
    • Optimizing Inference
  • Deployment
    • Prediction with FHE
    • Hybrid models
    • Production Deployment
    • Serialization
  • Advanced topics
    • Quantization
    • Pruning
    • Compilation
    • Advanced Features
  • Developer Guide
    • Workflow
      • Set Up the Project
      • Set Up Docker
      • Documentation
      • Support and Issues
      • Contributing
    • Inner Workings
      • Importing ONNX
      • Quantization Tools
      • FHE Op-graph Design
      • External Libraries
    • API
      • concrete.ml.common.check_inputs.md
      • concrete.ml.common.debugging.custom_assert.md
      • concrete.ml.common.debugging.md
      • concrete.ml.common.md
      • concrete.ml.common.serialization.decoder.md
      • concrete.ml.common.serialization.dumpers.md
      • concrete.ml.common.serialization.encoder.md
      • concrete.ml.common.serialization.loaders.md
      • concrete.ml.common.serialization.md
      • concrete.ml.common.utils.md
      • concrete.ml.deployment.deploy_to_aws.md
      • concrete.ml.deployment.deploy_to_docker.md
      • concrete.ml.deployment.fhe_client_server.md
      • concrete.ml.deployment.md
      • concrete.ml.deployment.server.md
      • concrete.ml.deployment.utils.md
      • concrete.ml.onnx.convert.md
      • concrete.ml.onnx.md
      • concrete.ml.onnx.onnx_impl_utils.md
      • concrete.ml.onnx.onnx_model_manipulations.md
      • concrete.ml.onnx.onnx_utils.md
      • concrete.ml.onnx.ops_impl.md
      • concrete.ml.pytest.md
      • concrete.ml.pytest.torch_models.md
      • concrete.ml.pytest.utils.md
      • concrete.ml.quantization.base_quantized_op.md
      • concrete.ml.quantization.md
      • concrete.ml.quantization.post_training.md
      • concrete.ml.quantization.quantized_module.md
      • concrete.ml.quantization.quantized_module_passes.md
      • concrete.ml.quantization.quantized_ops.md
      • concrete.ml.quantization.quantizers.md
      • concrete.ml.search_parameters.md
      • concrete.ml.search_parameters.p_error_search.md
      • concrete.ml.sklearn.base.md
      • concrete.ml.sklearn.glm.md
      • concrete.ml.sklearn.linear_model.md
      • concrete.ml.sklearn.md
      • concrete.ml.sklearn.neighbors.md
      • concrete.ml.sklearn.qnn.md
      • concrete.ml.sklearn.qnn_module.md
      • concrete.ml.sklearn.rf.md
      • concrete.ml.sklearn.svm.md
      • concrete.ml.sklearn.tree.md
      • concrete.ml.sklearn.tree_to_numpy.md
      • concrete.ml.sklearn.xgb.md
      • concrete.ml.torch.compile.md
      • concrete.ml.torch.hybrid_model.md
      • concrete.ml.torch.md
      • concrete.ml.torch.numpy_module.md
      • concrete.ml.version.md
Powered by GitBook

Libraries

  • TFHE-rs
  • Concrete
  • Concrete ML
  • fhEVM

Developers

  • Blog
  • Documentation
  • Github
  • FHE resources

Company

  • About
  • Introduction to FHE
  • Media
  • Careers
On this page
  • module concrete.ml.sklearn.tree_to_numpy
  • Global Variables
  • function get_onnx_model
  • function workaround_squeeze_node_xgboost
  • function assert_add_node_and_constant_in_xgboost_regressor_graph
  • function add_transpose_after_last_node
  • function preprocess_tree_predictions
  • function tree_onnx_graph_preprocessing
  • function tree_values_preprocessing
  • function tree_to_numpy

Was this helpful?

Export as PDF
  1. Developer Guide
  2. API

concrete.ml.sklearn.tree_to_numpy.md

Previousconcrete.ml.sklearn.tree.mdNextconcrete.ml.sklearn.xgb.md

Last updated 1 year ago

Was this helpful?

module concrete.ml.sklearn.tree_to_numpy

Implements the conversion of a tree model to a numpy function.

Global Variables

  • MAX_BITWIDTH_BACKWARD_COMPATIBLE

  • OPSET_VERSION_FOR_ONNX_EXPORT


function get_onnx_model

get_onnx_model(model: Callable, x: ndarray, framework: str) → ModelProto

Create ONNX model with Hummingbird convert method.

Args:

  • model (Callable): The tree model to convert.

  • x (numpy.ndarray): Dataset used to trace the tree inference and convert the model to ONNX.

  • framework (str): The framework from which the ONNX model is generated.

  • (options: 'xgboost', 'sklearn')

Returns:

  • onnx.ModelProto: The ONNX model.


function workaround_squeeze_node_xgboost

workaround_squeeze_node_xgboost(onnx_model: ModelProto)

Workaround to fix torch issue that does not export the proper axis in the ONNX squeeze node.

FIXME: https://github.com/zama-ai/concrete-ml-internal/issues/2778 The squeeze ops does not have the proper dimensions. remove the following workaround when the issue is fixed Add the axis attribute to the Squeeze node

Args:

  • onnx_model (onnx.ModelProto): The ONNX model.


function assert_add_node_and_constant_in_xgboost_regressor_graph

assert_add_node_and_constant_in_xgboost_regressor_graph(onnx_model: ModelProto)

Assert if an Add node with a specific constant exists in the ONNX graph.

Args:

  • onnx_model (onnx.ModelProto): The ONNX model.


function add_transpose_after_last_node

add_transpose_after_last_node(onnx_model: ModelProto)

Add transpose after last node.

Args:

  • onnx_model (onnx.ModelProto): The ONNX model.


function preprocess_tree_predictions

preprocess_tree_predictions(
    init_tensor: ndarray,
    output_n_bits: int
) → QuantizedArray

Apply post-processing from the graph.

Args:

  • init_tensor (numpy.ndarray): Model parameters to be pre-processed.

  • output_n_bits (int): The number of bits of the output.

Returns:

  • QuantizedArray: Quantizer for the tree predictions.


function tree_onnx_graph_preprocessing

tree_onnx_graph_preprocessing(
    onnx_model: ModelProto,
    framework: str,
    expected_number_of_outputs: int
)

Apply pre-processing onto the ONNX graph.

Args:

  • onnx_model (onnx.ModelProto): The ONNX model.

  • framework (str): The framework from which the ONNX model is generated.

  • (options: 'xgboost', 'sklearn')

  • expected_number_of_outputs (int): The expected number of outputs in the ONNX model.


function tree_values_preprocessing

tree_values_preprocessing(
    onnx_model: ModelProto,
    framework: str,
    output_n_bits: int
) → QuantizedArray

Pre-process tree values.

Args:

  • onnx_model (onnx.ModelProto): The ONNX model.

  • framework (str): The framework from which the ONNX model is generated.

  • (options: 'xgboost', 'sklearn')

  • output_n_bits (int): The number of bits of the output.

Returns:

  • QuantizedArray: Quantizer for the tree predictions.


function tree_to_numpy

tree_to_numpy(
    model: Callable,
    x: ndarray,
    framework: str,
    output_n_bits: int = 8
) → Tuple[Callable, List[UniformQuantizer], ModelProto]

Convert the tree inference to a numpy functions using Hummingbird.

Args:

  • model (Callable): The tree model to convert.

  • x (numpy.ndarray): The input data.

  • framework (str): The framework from which the ONNX model is generated.

  • (options: 'xgboost', 'sklearn')

  • output_n_bits (int): The number of bits of the output. Default to 8.

Returns:

  • Tuple[Callable, List[QuantizedArray], onnx.ModelProto]: A tuple with a function that takes a numpy array and returns a numpy array, QuantizedArray object to quantize and de-quantize the output of the tree, and the ONNX model.