onnx / sklearn-onnx

Convert scikit-learn models and pipelines to ONNX
Apache License 2.0
548 stars 99 forks source link

How to pass input to onnxscript functions correctly? #1089

Open paranjapeved15 opened 5 months ago

paranjapeved15 commented 5 months ago

I was trying to follow along this tutorial: https://onnx.ai/sklearn-onnx/auto_tutorial/plot_jfunction_transformer.html#custom-converter-with-onnxscript

df = pandas.DataFrame(
    [["car", 0.1, 0.2, 0.3, 0.4, 0.5], ["car", 0.1, 0.2, 0.3, 0.4, 0.5], ["suv", 0.1, 0.2, 0.3, 0.4, 0.5]],
    columns=['vehicleType', 'features_car', 'features_suv', 'features_truck', 'features_van', 'features_minivan'])

from onnx.helper import make_tensor
from onnx import TensorProto
from onnxscript.onnx_types import FLOAT, INT64, STRING, BOOL

@onnxscript.script()
def converter_function(X) -> FLOAT[1,1]:
    zero_constant = op.Constant(value=make_tensor("zero_constant", TensorProto.FLOAT, [1,1], [0.0]))
    car_constant = op.Constant(value=make_tensor(
        name = "car_constant",
        data_type = TensorProto.STRING,
        dims=[1,1],
        vals = ["car"]
    ))
    vehicle = op.Slice(X, [0], [1], [1])
    x1 = op.Slice(X, [1], [2], [1])
    isVehicleTypeCar = vehicle == car_constant
    if isVehicleTypeCar:
        result = x1
    else:
        result = zero_constant
    return result

def get_score_transformer_converter(scope, operator, container):  # type: ignore
    """Convert overprice sklearn custom transformer to onnx."""
    opv = container.target_opset
    X = operator.inputs[0]

    proto = converter_function().to_model_proto()
    proto_version = convert_version(proto, opv)
    add_onnx_graph(scope, operator, container, proto_version)

update_registered_converter(
    GetScore,
    "GetScore",
    get_score_transformer_shape_calculator,
    get_score_transformer_converter,
)

model_onnx = to_onnx(pipeline, df[:1], target_opset=18, options={"zipmap": False})

It gives me the following error: File "/Users/vedparanjape/code/onnx-converters/src/onnx/converters/trial.py", line 117, in get_score_transformer_converter proto = converter_function().to_model_proto() ^^^^^^^^^^^^^^^^^^^^ File "/Users/vedparanjape/Library/Caches/pypoetry/virtualenvs/onnx-converters-gTr4GyPW-py3.12/lib/python3.12/site-packages/onnxscript/values.py", line 526, in call return evaluator.default().eval_function(self, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vedparanjape/Library/Caches/pypoetry/virtualenvs/onnx-converters-gTr4GyPW-py3.12/lib/python3.12/site-packages/onnxscript/evaluator.py", line 282, in eval_function tagged_args, tagged_kwargs = param_manipulation.tag_arguments_with_param_schemas( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vedparanjape/Library/Caches/pypoetry/virtualenvs/onnx-converters-gTr4GyPW-py3.12/lib/python3.12/site-packages/onnxscript/_internal/param_manipulation.py", line 129, in tag_arguments_with_param_schemas raise TypeError(f"Required input/attribute '{param}' was not provided") TypeError: Required input/attribute 'X: Input[None]' was not provided

I am not sure what I am doing differently than the tutorial code or I am missing something else.

xadupre commented 4 months ago

I tried to replicate your issue but there are still missing pieces (see #1094). Could you complete it?