How does MLIR define tensor/vector/matrix *rank*?


This is probably quite obvious for folks who regularly work with MLIR, so please bear with me :slight_smile:

How does MLIR define the “rank” of a tensor? How about vectors and matrices?


The rank of a tensor is independent of the number of dimensions of the underlying space

The TF docs further clarify this for vectors:

  • Rank: Number of tensor axes. A scalar has rank 0, a vector has rank 1, a matrix is rank 2.

As far as MLIR is concerned, it’s very hard to find a definitive answer as there is no relevant documentation, but perhaps I missed something? This is my mental model based on the links above:

  • Let A be a tensor, e.g. tensor<10 x 10 x 10 x i32>. It’s a rank-3 tensor.
  • Let v be a vector, e.g. vector<3 x i32>, it’s a rank-1 tensor in a 3-D space (or e.g. 3-D vector).

Is this correct?

Here’s the tricky part - what should be the nomenclature for the so-called multi-dimensional vectors from the vector dialect? Currently, vector<4x8x128xf32> is referred to as a 3-D vector, which would suggest that vector<3 x i32> is not a 3-D vector :thinking: . What about the rank? I’m probably just confusing myself :smiley:

I should also mention that this came up when reviewing ⚙ D127875 [mlir][vector] Add vector.scalable.insert/extract ops. CC @dcaballe @javiersetoain

Thanks for taking a look!

Some other references

(…) a rank-1 tensor (i.e., a vector in N-dimensional space can be represented by N numbers (…)

The rank of a tensor is the number of indices required to uniquely select each element of the tensor.

ShapedType (and hence Tensor and Vector type) in MLIR follows rank in TF most directly. But I think the confusion here is by confusing the type and value domain. There is a difference in the rank of the shape vs rank of the space represented by a value. Even in the Wolfram one: the rank of the shape is consistent with the interpretation above. So the rank-3 tensor requires 3 values in the indexing space. Vector may get confusing “verbally”, but the rank of the shape of the vector is what folks normally refer to as the rank of the vector’s shape/indexing space. E.g., vector<3xi32> is rank 1 as 1 dim is required to uniquely index, it represents a value in 3D space (but that’s the value not the type).


Thanks for confirming!

Ha, my bad, thanks for pointing this out. Now everything makes much more sens :slight_smile:

I guess that this is the key “rule of thumb” here - we should always assume that the rank refers to the indexing space. Unless somebody explicitly makes a different assumption.