cornell-zhang / hcl-dialect

HeteroCL-MLIR dialect for accelerator design
https://cornell-zhang.github.io/heterocl/index.html
Other
38 stars 17 forks source link

[Frontend] Tensor indexing issue #131

Closed zzzDavid closed 1 year ago

zzzDavid commented 2 years ago

The following code reports error:

            z1 = hcl.compute((2,3,4), lambda x,y,z: 0, dtype=hcl.Int(32))
            def do(i,j,k):
                z1[0][0][0] = 55
            hcl.mutate(z1.shape, do)
hcl-dialect-prototype/build/tools/hcl/python_packages/hcl_core/hcl_mlir/build_ir.py", line 997, in __setitem__
    raise TensorError("Indices length > # of array dimensions")
TypeError: __init__() missing 1 required positional argument: 'line'
jcasas00 commented 2 years ago

Behavior still isn't quite the same as in the main branch. If the do loop looked like:

def do(i,j,k):
    t = z1[0][0]  # a slice
    t[0] = 53

then I get the same error. The reason for doing this is that we could pass the variable t to other functions (a 1-D tensorslice).

zzzDavid commented 2 years ago

Reopening this issue

zzzDavid commented 2 years ago

hmmm I couldn't repeat this issue, I tried this:

def test_tensor_slice():
    hcl.init()
    def kernel():
        z1 = hcl.compute((2,3,4), lambda x,y,z: 0, dtype=hcl.Int(32))
        def do(i,j,k):
            t = z1[0][0]
            t[0] = 53
        hcl.mutate(z1.shape, do)
        return z1

    s = hcl.create_schedule([], kernel)
    hcl_res = hcl.asarray(np.zeros((2,3,4), dtype=np.int32))
    f = hcl.build(s)
    f(hcl_res)
    np_res = hcl_res.asnumpy()
    golden = np.zeros((2,3,4), dtype=np.int32)
    golden[0][0][0] = 53
    assert np.array_equal(golden, np_res)
    print("test_tensor_slice passed")

test_tensor_slice()

And I got the test passed output:

Using mlir as IR
Done HCL-MLIR initialization
test_tensor_slice passed

@jcasas00 are you using the latest HeteroCL HCL-MLIR branch?

zzzDavid commented 2 years ago

Turns out it's issue with __getitem__ API on tensor slice. Fixed by 3d1f4298c074ee65f7547dc73c2dc856a810ea91, I will close this issue later after I add more tests

zzzDavid commented 1 year ago

The following tests are added by commit cornell-zhang/heterocl@c7d72eed9821cd13bd6318e31c94d2e26f8043b0:

mlir/test_dsl_basic.py::test_tensor_slice_mutate PASSED                                                                              
mlir/test_dsl_basic.py::test_get_bit_expr_mutate PASSED                                                                              
mlir/test_dsl_basic.py::test_set_bit_expr_mutate PASSED                                                                              
mlir/test_dsl_basic.py::test_set_bit_tensor_mutate PASSED                                                                            
mlir/test_dsl_basic.py::test_get_slice_expr_mutate PASSED                                                                            
mlir/test_dsl_basic.py::test_get_slice_tensor_mutate PASSED                                                                          
mlir/test_dsl_basic.py::test_get_slice_tensor_reverse_mutate PASSED                                                                  
mlir/test_dsl_basic.py::test_set_slice_expr_mutate PASSED                                                                            
mlir/test_dsl_basic.py::test_set_slice_tensor_mutate PASSED                                                                          
mlir/test_dsl_basic.py::test_set_slice_tensor_reverse_mutate PASSED                                                                  
mlir/test_dsl_basic.py::test_slice_op_mutate PASSED