[PASS] Update StorageRewrite to Handle Allocate Depending on Loop Variable
See original GitHub issueConsider the following hypothetical example:
import tvm
N = tvm.var("N")
y = tvm.reduce_axis((0,N), 'y')
A = tvm.placeholder((2*N,2*N), "float32", "A")
C = tvm.compute((N, N), lambda i,j: A[2*i,j] + A[i+1,j], name='C')
s = tvm.create_schedule(C.op)
AA = s.cache_read(A, "local", [C])
s[AA].compute_at(s[C], s[C].op.axis[0])
print(tvm.lower(s, [A,C], simple_mode=True))
The resulting lowered code is as follows. See how the size of A.local
depends on i
which is a loop variable. I think the problem is that the two references to A
, i.e., A[2*i,j]
and A[i+1,j]
have different functions of i
(i.e., 2*i
vs i
) in their first dimension. That makes calculating buffer size more complicated, and TVM probably doesn’t check for that. We should either handle it properly or emit an error message that such indexing is not supported.
// attr [A.local] storage_scope = "local"
allocate A.local[float32 * ((max((i*2), (i + 1)) - min((i*2), (i + 1))) + 1) * N]
produce C {
for (i, 0, N) {
produce A.local {
for (ax0, 0, ((max((i*2), (i + 1)) - min((i*2), (i + 1))) + 1)) {
for (ax1, 0, N) {
if (likely((min((i*2), (i + 1)) < ((N*2) - ax0)))) {
if (likely((ax1 < (N*2)))) {
A.local[((ax0*N) + ax1)] = A[((((min((i*2), (i + 1)) + ax0)*N)*2) + ax1)]
}
}
}
}
}
for (j, 0, N) {
C[((i*N) + j)] = (A.local[((max((i + -1), 0)*N) + j)] + A.local[(((1 - min(i, 1))*N) + j)])
}
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
[PASS] Update StorageRewrite to Handle Allocate Depending ...
The resulting lowered code is as follows. See how the size of A.local depends on i which is a loop variable. I think...
Read more >Problem with allocate size depending on loop variable while ...
The problem is that in some cases allocate size ends up depending on the loop variable. StorageRewrite pass moves allocate-s to the ...
Read more >Changing iteration variable inside for loop in Python [duplicate]
i is a variable over a range. If you put i = 4 then you change i within the step of the current...
Read more >Salesforce Flow Loops - Best Practices and Examples
A Loop is a Salesforce Flow element that is used to iterate through a number of items in a collection variable.
Read more >Use Pointer of for Range Loop Variable in Go - Medium
So it turns out such iteration variables are re-used. It essentially just re-assign the value each iteration. This explains why does buggyLoop.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I see. I would vote to throw error to start, and if we see more occurrence of such pattern, we can consider handling it properly.
simple_mode
skips the sanity check byIRUseDefAnalysis
. So I added a test to show that we can detect such patterns.