Alternative GraphBuilderExtendedProtocol#

GraphBuilderExtendedProtocol is the interface that every graph builder used by the yobx.sklearn converters must satisfy. The supported implementation is GraphBuilder, which builds graphs using native onnx-light protobuf objects, shape inference, and optimization.

The historical OnnxScriptGraphBuilder and SpoxGraphBuilder bridges require the removed reference ONNX dependency. Their constructors now raise NotImplementedError rather than installing or falling back to that runtime. Existing callers should select the native builder as shown below.

Why provide alternatives?#

Keeping the builders behind a protocol rather than inheriting from a single base class means that any third-party library can supply its own builder. Implementations can provide stronger type checking, validate nodes as they are added, or integrate an existing graph representation. They must implement the complete protocol and return native onnx-light protobufs; the retired bridges are not working alternatives.

Selecting the native builder#

The builder_cls argument selects the implementation explicitly. Passing GraphBuilder is equivalent to using the default converter configuration.

<<<

import numpy as np
from yobx._onnx_shim import onnx
from sklearn.preprocessing import StandardScaler
from yobx.sklearn import to_onnx
from yobx.xbuilder import GraphBuilder
from yobx.helpers.onnx_helper import pretty_onnx

rng = np.random.default_rng(0)
X = rng.standard_normal((10, 4)).astype(np.float32)

scaler = StandardScaler().fit(X)
model = to_onnx(scaler, (X,), builder_cls=GraphBuilder)
print(pretty_onnx(model))

>>>

    opset: domain='' version=29
    opset: domain='ai.onnx.ml' version=5
    input: name='X' type=dtype('float32') shape=['batch', 4]
    init: name='init' type=float32 shape=(4,) -- array([-0.448,  0.052, -0.093,  0.247], dtype=float32)
    init: name='init_2' type=float32 shape=(4,) -- array([0.774, 0.641, 0.825, 0.728], dtype=float32)
    Sub(X, init) -> sub
      Div(sub, init_2) -> x
    output: name='x' type=dtype('float32') shape=['batch', 4]

Converting a pipeline#

The same native implementation handles complete pipelines through yobx.sklearn.to_onnx(), without an intermediate external IR:

<<<

import numpy as np
from yobx._onnx_shim import onnx
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.linear_model import LogisticRegression
from yobx.sklearn import to_onnx
from yobx.xbuilder import GraphBuilder
from yobx.helpers.onnx_helper import pretty_onnx

rng = np.random.default_rng(0)
X = rng.standard_normal((80, 4)).astype(np.float32)
y = (X[:, 0] + X[:, 1] > 0).astype(int)

pipe = Pipeline([("scaler", StandardScaler()), ("clf", LogisticRegression())])
pipe.fit(X, y)

model = to_onnx(pipe, (X[:1],), builder_cls=GraphBuilder)
print(pretty_onnx(model))

>>>

    opset: domain='' version=29
    opset: domain='ai.onnx.ml' version=5
    input: name='X' type=dtype('float32') shape=['batch', 4]
    init: name='main__scaler__init' type=float32 shape=(4,) -- array([-0.041,  0.007, -0.24 ,  0.112], dtype=float32)
    init: name='main__scaler__init_2' type=float32 shape=(4,) -- array([0.954, 0.858, 1.03 , 1.183], dtype=float32)
    init: name='main__clf__init' type=float32 shape=(1, 4) -- array([ 2.229,  2.285,  0.46 , -0.414], dtype=float32)
    init: name='main__clf__init_2' type=float32 shape=(1,) -- array([0.212], dtype=float32)
    init: name='main__clf__init_3' type=float32 shape=(1,) -- array([1.], dtype=float32)
    init: name='main__clf__init_4' type=int64 shape=(2,) -- array([0, 1])
    Sub(X, main__scaler__init) -> main__scaler__sub
      Div(main__scaler__sub, main__scaler__init_2) -> x
        Gemm(x, main__clf__init, main__clf__init_2, transB=1) -> main__clf__gemm
          Sigmoid(main__clf__gemm) -> main__clf__sigmoid
            Sub(main__clf__init_3, main__clf__sigmoid) -> main__clf__sub
            Concat(main__clf__sub, main__clf__sigmoid, axis=-1) -> probabilities
              ArgMax(probabilities, axis=1, keepdims=0) -> main__clf__argmax
                Gather(main__clf__init_4, main__clf__argmax, axis=0) -> label
    output: name='label' type=dtype('int64') shape=['batch']
    output: name='probabilities' type=dtype('float32') shape=['batch', 2]

See also

Expected API — the full list of methods and attributes every builder must expose.

GraphBuilder — native graph construction and optimization.