webonnx / wonnx

A WebGPU-accelerated ONNX inference run-time written 100% in Rust, ready for native and the web
Other
1.54k stars 54 forks source link

Error running `nnx prepare ...` #173

Closed schell closed 11 months ago

schell commented 11 months ago

Describe the bug Error: Could not infer shapes: node Slice_7 is invalid: invalid number of inputs (1) or outputs (1)

To Reproduce Unfortunately this is with a proprietary model, but my command is:

❯ nnx prepare -i model.onnx model_prepared.onnx

Expected behavior Operation without error.

Screenshots This shows the node Slice_7 with one input and one output.

Screenshot 2023-07-12 at 4 55 25 PM

Desktop (please complete the following information):

pixelspark commented 11 months ago

Hi @schell, sorry to hear you are experiencing issues.

nnx prepare invokes shape inference, which for the Slice operator is implemented only for cases with 3...5 inputs and one output: https://github.com/webonnx/wonnx/blob/c772653fe97c0e9b523cdf530a71bff66ec51176/wonnx-preprocessing/src/shape_inference.rs#L613

Looking at the ONNX spec, only version 1 of the operator accepts one input parameter: https://onnx.ai/onnx/operators/onnx__Slice.html#inputs. The v1 version has 'start' and 'end' as attributes (as shown in your picture) whereas newer versions accept these parameters as inputs.

Perhaps you should try to generate the ONNX using a newer version of the spec (or otherwise replace the attributes with inputs of constant tensors). Another option would be to implement shape inference for the (Slice, 1, 1) case (which should be rather easy as the only thing the code would need to do differently in this case is read start/end from the attributes). PRs accepted, I can assist in implementing if necessary.

schell commented 11 months ago

Thanks @pixelspark! I'll talk to the team to see if regenerating the model with a newer spec is the easy option.