cornell-zhang / heterocl

HeteroCL: A Multi-Paradigm Programming Infrastructure for Software-Defined Heterogeneous Computing
https://cornell-zhang.github.io/heterocl/
Apache License 2.0
322 stars 92 forks source link

TensorError: [Tensor] Accessing a slice of tensor is not allowed #212

Open hecmay opened 4 years ago

hecmay commented 4 years ago

Minimal test case to reproduce the error:

@hec.def_()
def func(a, b):
    with hcl.for_(0, 12) as j:
        a[j] = b[j] + 1

hcl.mutate((10,), lambda t: func(A[t], B[t]))

The input into HCL module is restricted to tensors or scalars. Inputs of TensorSlices are not allowed, which makes it hard to call module iteratively in the loop body, as shown in the example above.

chhzh123 commented 4 years ago

Another test case here:

def _popcount(num):
    out = hcl.scalar(0, "popcnt")
    with hcl.for_(0, 32) as i:
        # Bit selection operation
        out.v += num[i]
    return out.v

A = hcl.placeholder((3,), dtype=hcl.UInt(8), name="A")
B = hcl.placeholder((3,), dtype=hcl.UInt(8), name="B")
out = hcl.compute((3,),
    lambda x: _popcount(A[x] + B[x]),
    dtype=hcl.UInt(32))
s = hcl.create_schedule([A, B, out])
f = hcl.build(s)
hcl_a = hcl.asarray(np.array([1, 2, 3]), dtype=hcl.UInt(8))
hcl_b = hcl.asarray(np.array([1, 2, 3]), dtype=hcl.UInt(8))
hcl_out = hcl.asarray(np.array([0, 0, 0]), dtype=hcl.UInt(32))
f(hcl_a, hcl_b, hcl_out)
print("Input : {} + {}".format(hcl_a.asnumpy(), hcl_b.asnumpy()))
print("Output : {}".format(hcl_out.asnumpy()))

This results in the following error:

Traceback (most recent call last):
  File "nested.py", line 16, in <module>
    dtype=hcl.UInt(32))
  File "/home/chz/heterocl/python/heterocl/compute_api.py", line 295, in compute
    tensor = compute_body(name, lambda_ivs, fcompute, shape, dtype, attrs=attrs)
  File "/home/chz/heterocl/python/heterocl/compute_api.py", line 127, in compute_body
    ret = fcompute(*var_list)
  File "nested.py", line 15, in <lambda>
    lambda x: _popcount(A[x] + B[x]),
  File "nested.py", line 9, in _popcount
    out.v += num[i]
TypeError: 'Add' object is not subscriptable

However, if I change _popcount to tvm.intrin.popcount in the compute function, this example works. The non-intrinsic instruction seems to cause the problem.