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
Pipelineto an ONNX file for SnoozeML.Produces a standard ONNX model with
float32tensor inputs and outputs that can be served directly by SnoozeML’sortbackend with dynamic micro-batching alongAxis(0).- Parameters:
model (Any) – Fitted scikit-learn estimator (e.g.
RandomForestClassifier,LogisticRegression,LinearRegression,MLPClassifier) orPipeline.initial_types (Sequence[Tuple[str, Any]] | None) – Optional list of
(name, shape_or_type)tuples such as[("float_input", (None, 4))]. If omitted, inferred fromsample_inputormodel.n_features_in_.output_path (str | Path) – Destination filesystem path for the exported
.onnxfile.sample_input (ndarray | Sequence[Any] | None) – Optional sample feature array
(batch, n_features)used to infer input dimensions wheninitial_typesis omitted.input_name (str) – Default input tensor binding name when
initial_typesis omitted.output_name (str) – Output tensor binding name (default
"output").opset_version (int) – Target ONNX opset version (default
17).prefer_skl2onnx (bool) – If
Trueandskl2onnxis installed, delegates conversion toskl2onnx.convert_sklearn. Defaults toFalse(uses SnoozeML’s native single-tensor FP32 ONNX compiler with automatic fallback toskl2onnx).options (Dict[Any, Any] | None) – Optional dictionary of converter options forwarded to
skl2onnx.
- Returns:
Resolved
pathlib.Pathto the written.onnxmodel file.- Raises:
ConversionError – If the model is not fitted, input dimensions cannot be determined, or conversion fails.
- Return type:
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.Moduleto an ONNX file ready for serving with SnoozeML.Automatically configures
dynamic_axesalongAxis(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.Moduleinstance to export.sample_input (Any) – Representative input
torch.Tensor(ornp.ndarray/ tuple of tensors) used to trace or infer input dimensions.output_path (str | Path) – Destination filesystem path for the
.onnxmodel 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 dynamicbatch_sizeon dimension0for all inputs and outputs.opset_version (int) – Target ONNX opset version (default
17).prefer_torch_onnx (bool) – If
True, attemptstorch.onnx.exportfirst and falls back to SnoozeML’s native ONNX graph builder whenonnx/onnxscriptare not installed.**kwargs (Any) – Additional keyword arguments forwarded to
torch.onnx.export.
- Returns:
Resolved
pathlib.Pathto the written.onnxfile.- Raises:
ConversionError – If
modelorsample_inputis invalid or export fails.- Return type:
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
Boosteror scikit-learn wrapper (XGBClassifier,XGBRegressor) to SnoozeML’s native.xgb.jsonformat.SnoozeML serves
.xgb.jsonfiles directly through its pure-Rusthessboosttree backend without requiring ONNX Runtime or C++ dependencies.- Parameters:
- Returns:
Resolved
pathlib.Pathto the exported JSON model file.- Raises:
ConversionError – If
modelis invalid or has not been fitted.- Return type: