Support for atomic operation and axis fusion of reduce axis and axis
Hi everyone.
It seems TVM has not yet supported the atomic and fusion operation between reduce axis and axis. My problem seems to be as follows: we have a matrix A with shape m*n = 10240x512, which is stored in row major. The goal is to reduce and sum A to 512 elements (reduce axis is the m axis).
We could do the matrix transpose first and then implement the reduction across all threads along the column axis, but it seems to be slow as we need to implement the transpose.
One way I can think of is to use, say the first block of 1024 threads to sum the elements of [0:31,0:31] and [32:63, 0:31], and then sum over the 32 warps and write to the global memory. The second block will sum over [64:95, 0:31] and [96:127, 0:31] and so on. It seems to me in this case, we need to fuse the op.axis (split from axis n) and the reduce axis (split from m) and then fuse them to get 1024 elements per block, isn't it?
Moreover, because these two blocks will write to the same location of the global memory along the n axis from [0:31], one way of doing this without global sync is to use atomic operation.
Any ideas and comments are welcomed. Thank you!
What you are asking for seems can be can be done by rfactor, http://docs.tvmlang.org/tutorials/reduction.html#sphx-glr-tutorials-reduction-py .
This only allows sum across the threads within the block though, but can be a good starting point
@tqchen Thank you for your suggestions. Here is the updated schedule:
s[Padded_in_grad].compute_inline()
block_x = tvm.thread_axis("blockIdx.x")
thread_y = tvm.thread_axis("threadIdx.y")
thread_x = tvm.thread_axis("threadIdx.x")
db, dh, dw = Weight_grad.op.reduce_axis
fh, fw, c, m = Weight_grad.op.axis
fused_dhdw = s[Weight_grad].fuse(dh, dw)
ko, ki = s[Weight_grad].split(fused_dhdw, factor=32)
BF = s.rfactor(Weight_grad, ki)
print(s[Weight_grad].op.axis[0])
xo, xi = s[Weight_grad].split(s[Weight_grad].op.axis[0], factor=32)
s[Weight_grad].bind(xi, thread_y)
s[Weight_grad].bind(xo, block_x)
s[Weight_grad].bind(s[Weight_grad].op.reduce_axis[0], thread_x)
s[BF].compute_at(s[Weight_grad], s[Weight_grad].op.reduce_axis[0])
Putting aside the atomic (global) add, the major problem then becomes this line: xo, xi = s[Weight_grad].split(s[Weight_grad].op.axis[0], factor=32)
I thought s[Weight_grad].op.axis[0] could be the fh, fw, or c axis, but it turns out to be a small value and so it is not efficient to bind to the threads and blocks. I could not bind the fh, fw, or c axis to the threadIdx.x and blockIdx.x anymore. Is there anyway I could bind them?
xo, xi = s[Weight_grad].split(c, factor=32), error message is Operate on iter var iter_var(c, Range(min=0, extent=64))that is not part of the schedule
Thank you!
I think I was confused of the s[Weight_grad].op.axis and Weight_grad.op.axis. They are in fact the same but it seems to me we can only write s[Weight_grad].split(s[Weight_grad].op.axis[0], factor=32) instead of s[Weight_grad].split(Weight_grad.op.axis, factor=32).
When we do rfactor, s[weight_grad].op get rewritten to another operator which is different from weight_grad.op. So it is always safe to call s[Weight_grad].split(s[Weight_grad].op.axis[0], factor=32)
Now it could achieve comparable results as TF, even without the atomic. It would be great if it could have the atomic operator so that we could avoid the loop like this: for (db.dh.dw.fused.fused.outer, 0, 17956). I will close the issue.