Hello everyone,
I’m trying to prepare tensor data for a custom accelerator.
To make sure the data layout is right for the accelerator I’ve looked into using the topi.reshape
and topi.transpose
operators.
However it looks to me that the reshape operation that is coming out of TVM is quite inefficient, as it basically makes nested for-loops to do a very inefficient element-per-element copy operation, as also pointed out in this linked thread.
For example if I have this code:
A = te.placeholder((16,))
B = topi.reshape(A,(4,4))
s = te.create_schedule(B.op)
The tensor data in A doesn’t really change, the only thing that I need to change are the handles to the tensor data:
>>> A.shape
[16]
>>> B.shape
[4, 4]
The fact that the data doesn’t change is also reflected in this schedule that is created:
primfn(placeholder_1: handle) -> ()
attr = {"global_symbol": "main", "tir.noalias": True}
buffers = {placeholder: Buffer(placeholder_2: Pointer(float32), float32, [16], [])}
buffer_map = {placeholder_1: placeholder} {
attr [T_reshape: Pointer(float32)] "storage_scope" = "global";
allocate(T_reshape, float32, [16]);
for (ax0: int32, 0, 4) {
for (ax1: int32, 0, 4) {
T_reshape[((ax0*4) + ax1)] = (float32*)placeholder_2[((ax0*4) + ax1)]
}
}
}
T_reshape[((ax0*4) + ax1)] = (float32*)placeholder_2[((ax0*4) + ax1)]
just copies the value from placeholder_2
to T_reshape
right?
Is there a reason why this copy is necessary? I don’t see why it is, and I really would rather have it to only change the handles of the Tensors A
and B
instead of it having to actually perform the element-wise copy.
I had hoped TVM would optimize for this case (removing the copy), but it seems that it actually performs this copy in the compiled C code (i’m using a “C
” target). It also makes a similar nonsense for-loops for a Conv2D operator with a specified padding of 0. (Or am I seeing this the wrong way and are there useful cases for such a copy?)
Is there any way to tell TVM to not do this copying of tensors?
I’m also linking these threads as they seem relevant, but don’t seem to offer a clear answer:
Thank you very much for your recommendations/comments!