onnx-mlir icon indicating copy to clipboard operation
onnx-mlir copied to clipboard

Choice of placeholder type during ONNX translation to onnx mlir dialect

Open etiotto opened this issue 3 years ago • 2 comments

During code review of https://github.com/onnx/onnx-mlir/pull/1110 there was a discussion on the meaning of tensor<*xf32>.

Currently that type is used as a placeholder for the output type of the onnx mlir dialect operators (during translation of a ONNX graph into the dialect), when the translator cannot determine the exact type of the output. The placeholder type is used to indicate that the output tensor has no rank and also no known element type.

The MLIR documentation (https://mlir.llvm.org/docs/Dialects/Builtin/#unrankedtensortype) states:

tensor-type ::= `tensor` `<` `*` `x` type `>`

“An unranked tensor is a type of tensor in which the set of dimensions have unknown rank.”

Therefore, according to the MLIR documentation tensor<*xf32> indicates a tensor with unknown rank/extents, but with known element type f32. Ideally we should have tensor<*> to represent an unranked tensor with unknown element type. Unfortunately I believe that syntax to be illegal.

Fortunately the placeholder type is transient, because during shape inference the output type is replaced with the closest correct type. So, practically, things work fine and the inconsistency is benign.

Nevertheless it would be nice to use something different to indicate an unknown type.

etiotto avatar Jan 25 '22 20:01 etiotto

The true solution is to use tensor<*> i.e. unknown shape and type. I'm not sure this is something that MLIR currently supports.

Whichever type we end up using, shape inference will be the first pass that runs and will infer (overwrite) every aspect of the tensor type: shape and element type for the output of every ONNX Dialect operation.

doru1004 avatar Jan 25 '22 20:01 doru1004

Could we define an unknown element type, say any, and use tensor<*xany>?

Anyway, I found a thread related to this: https://llvm.discourse.group/t/rfc-tensors-with-unknown-element-types/1039/24

tungld avatar Jan 26 '22 01:01 tungld