Skip to content

Commit

Permalink
[TIR] Fix data dependent indexing when lowering TE to TIR (apache#8217)
Browse files Browse the repository at this point in the history
A conversion pass was missing the recursive VisitExpr statement.
  • Loading branch information
tkonolige authored and trevor-m committed Jun 17, 2021
1 parent 43a6071 commit 66a1925
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/te/operation/create_primfunc.cc
Expand Up @@ -35,11 +35,12 @@ class ProducerToBufferTransformer : public StmtExprMutator {
: tensor2buffers_(tensor2buffers) {}

PrimExpr VisitExpr_(const ProducerLoadNode* op) final {
te::Tensor tensor = Downcast<te::Tensor>(op->producer);
auto visited_op = Downcast<ProducerLoad>(StmtExprMutator::VisitExpr_(op));
te::Tensor tensor = Downcast<te::Tensor>(visited_op->producer);
auto it = tensor2buffers_.find(tensor);
ICHECK(it != tensor2buffers_.end()) << "IndexError: Cannot find the tensor " << tensor;
const Buffer& buffer = it->second;
return BufferLoad(buffer, op->indices);
return BufferLoad(buffer, visited_op->indices);
}

private:
Expand Down
15 changes: 15 additions & 0 deletions tests/python/unittest/test_te_create_primfunc.py
Expand Up @@ -300,6 +300,21 @@ def test_constant():
tvm.testing.assert_allclose(a_np + 2, c.numpy())


def test_data_dependent_access():
A = te.placeholder((10,), name="A")
B = te.placeholder((10,), name="B", dtype="int32")
C = te.compute((10,), lambda i: A[B[i]])

func = te.create_prim_func([C, A, B])
func = tvm.build(func)

a_np = np.random.uniform(size=(10,)).astype(A.dtype)
b_np = np.arange(10, dtype=B.dtype)
c = tvm.nd.array(np.zeros(10, dtype=C.dtype))
func(c, tvm.nd.array(a_np), tvm.nd.array(b_np))
tvm.testing.assert_allclose(a_np[b_np], c.numpy())


if __name__ == "__main__":
test_unique_name()
test_matmul()
Expand Down

0 comments on commit 66a1925

Please sign in to comment.