snoozeml.converters — ONNX & XGBoost Model Exporters#

Scikit-learn estimator and Pipeline to ONNX converter for SnoozeML.

snoozeml.converters.sklearn.export_sklearn(model, initial_types=None, output_path='model.onnx', *, sample_input=None, input_name='float_input', output_name='output', opset_version=17, prefer_skl2onnx=False, options=None)[source]#

Exports a fitted scikit-learn estimator or Pipeline to an ONNX file for SnoozeML.

Produces a standard ONNX model with float32 tensor inputs and outputs that can be served directly by SnoozeML’s ort backend with dynamic micro-batching along Axis(0).

Parameters:
  • model (Any) – Fitted scikit-learn estimator (e.g. RandomForestClassifier, LogisticRegression, LinearRegression, MLPClassifier) or Pipeline.

  • initial_types (Sequence[Tuple[str, Any]] | None) – Optional list of (name, shape_or_type) tuples such as [("float_input", (None, 4))]. If omitted, inferred from sample_input or model.n_features_in_.

  • output_path (str | Path) – Destination filesystem path for the exported .onnx file.

  • sample_input (ndarray | Sequence[Any] | None) – Optional sample feature array (batch, n_features) used to infer input dimensions when initial_types is omitted.

  • input_name (str) – Default input tensor binding name when initial_types is omitted.

  • output_name (str) – Output tensor binding name (default "output").

  • opset_version (int) – Target ONNX opset version (default 17).

  • prefer_skl2onnx (bool) – If True and skl2onnx is installed, delegates conversion to skl2onnx.convert_sklearn. Defaults to False (uses SnoozeML’s native single-tensor FP32 ONNX compiler with automatic fallback to skl2onnx).

  • options (Dict[Any, Any] | None) – Optional dictionary of converter options forwarded to skl2onnx.

Returns:

Resolved pathlib.Path to the written .onnx model file.

Raises:

ConversionError – If the model is not fitted, input dimensions cannot be determined, or conversion fails.

Return type:

Path

PyTorch nn.Module to ONNX converter for SnoozeML.

snoozeml.converters.torch.export_torch(model, sample_input, output_path='model.onnx', *, input_names=None, output_names=None, dynamic_axes=None, opset_version=17, prefer_torch_onnx=True, **kwargs)[source]#

Exports a PyTorch nn.Module to an ONNX file ready for serving with SnoozeML.

Automatically configures dynamic_axes along Axis(0) (batch_size) when not explicitly provided so that SnoozeML’s dynamic micro-batching engine can coalesce concurrent requests of varying batch sizes.

Parameters:
  • model (Any) – PyTorch torch.nn.Module instance to export.

  • sample_input (Any) – Representative input torch.Tensor (or np.ndarray / tuple of tensors) used to trace or infer input dimensions.

  • output_path (str | Path) – Destination filesystem path for the .onnx model file.

  • input_names (Sequence[str] | None) – Optional list of input binding names (defaults to ["input"]).

  • output_names (Sequence[str] | None) – Optional list of output binding names (defaults to ["output"]).

  • dynamic_axes (Mapping[str, Mapping[int, str]] | None) – Optional dynamic axes mapping. If None, defaults to dynamic batch_size on dimension 0 for all inputs and outputs.

  • opset_version (int) – Target ONNX opset version (default 17).

  • prefer_torch_onnx (bool) – If True, attempts torch.onnx.export first and falls back to SnoozeML’s native ONNX graph builder when onnx/onnxscript are not installed.

  • **kwargs (Any) – Additional keyword arguments forwarded to torch.onnx.export.

Returns:

Resolved pathlib.Path to the written .onnx file.

Raises:

ConversionError – If model or sample_input is invalid or export fails.

Return type:

Path

Note

PyTorch export requires torch. SnoozeML only requires the lightweight CPU version of PyTorch for offline model export (avoiding large CUDA downloads): pip install torch --index-url https://download.pytorch.org/whl/cpu (or: pip install "snoozeml[torch]").

XGBoost Booster and scikit-learn wrapper exporter for SnoozeML’s native pure-Rust tree backend.

snoozeml.converters.xgboost.export_xgboost(model, output_path='model.xgb.json')[source]#

Exports an XGBoost Booster or scikit-learn wrapper (XGBClassifier, XGBRegressor) to SnoozeML’s native .xgb.json format.

SnoozeML serves .xgb.json files directly through its pure-Rust hessboost tree backend without requiring ONNX Runtime or C++ dependencies.

Parameters:
  • model (Any) – Trained xgboost.Booster, xgboost.XGBClassifier, xgboost.XGBRegressor, or xgboost.XGBRanker.

  • output_path (str | Path) – Destination filesystem path (recommended extension: .xgb.json).

Returns:

Resolved pathlib.Path to the exported JSON model file.

Raises:

ConversionError – If model is invalid or has not been fitted.

Return type:

Path