question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[PASS] Update StorageRewrite to Handle Allocate Depending on Loop Variable

See original GitHub issue

Consider 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:closed
  • Created 5 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
wweiccommented, Oct 11, 2018

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.

0reactions
wweiccommented, Oct 14, 2018

simple_mode skips the sanity check by IRUseDefAnalysis. So I added a test to show that we can detect such patterns.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found