Hi,
I’ve been exploring the behaviour of tensor.extract_slice and tensor.pack and I’m encountering some confusion when working with dynamic sizes. Specifically, I’m unsure about the behaviour when a dynamic value (like %c4) is used for the “size” in these operations.
Here’s what’s correct/incorrect for tensor.extract_slice today (note the dynamic “size” from %c4):
%c4 = arith.constant 4 : index
// CORRECT: Output tensor: tensor<?xf32>
%0 = tensor.extract_slice %t[0][%c4][1] : tensor<?xf32> to tensor<?xf32>
// INCORRECT: This leads to an error: "expected type to be 'tensor<?xf32>' or a rank-reduced version. (size mismatch)}"
%1 = tensor.extract_slice %t[0][%c4][1] : tensor<?xf32> to tensor<4xf32>
A similar experiment with tensor.pack shows a slightly different result:
%c4 = arith.constant 4 : index
// Output tensor: tensor<?x?x4x2xf32>
%0 = tensor.pack %source padding_value(%pad : f32) inner_dims_pos = [0, 1] inner_tiles = [%c4, 2] into %dest : tensor<?x?xf32> -> tensor<?x?x4x2xf32>
// Output tensor: tensor<?x?x4x2xf32>
%1 = tensor.pack %source padding_value(%pad : f32) inner_dims_pos = [0, 1] inner_tiles = [%c4, 2] into %dest : tensor<?x?xf32> -> tensor<?x?x?x2xf32>
Intuitively, I expected that using %c4 (a constant, but still dynamic at this level) would always result in ? in the output tensor. On the other hand, I also see the logic in allowing 4 instead of ? since we know the exact value of %c4.
So my question is: Which behavior is more correct? Should %c4 always lead to ? in the output tensor, or is there merit to allowing 4 in cases where the value is known?
General Case
I’m also working on more complex cases, such as this genuinely dynamic one:
%c4 = arith.constant 4 : index
%vs = vector.vscale
%c4_vs = arith.muli %c4, %vs : index
%0 = tensor.pack %source padding_value(%pad : f32) inner_dims_pos = [0, 1] inner_tiles = [%c4_vs, 2] into %dest : tensor<?x?xf32> -> tensor<?x?x?x2xf32>
In this case, using 4 instead of ? would definitely be incorrect since %c4_vs is truly dynamic. However, before I get to this case, I need to resolve the simpler cases mentioned above.
Thanks!
– Andrzej