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

[API] Add support for getting/setting tensor slices #403

Closed ezw2 closed 3 years ago

ezw2 commented 3 years ago

Add feature: Added support for getting and setting tensor slices

How to use the new feature:

  1. Copy a slice of a tensor

    A = hcl.placeholder((10,))
    B = hcl.copy(A[2:5])
    # equivalent API
    B = hcl.compute((3,), lambda x: A[x+2])
  2. Set a slice of a tensor

    
    A = hcl.placeholder((10,))
    B = hcl.placeholder((15,))
    A[2:5] =B[4:7]
    A[2:5] = 4  # broadcast
3. Get a slice of a tensor without making a copy

A = hcl.placeholder((10, 10)) B = A[2:5] B[0] = 1 hcl.print(A[2]) # returns 1



**Description:** Most of the changes were in tensor.py where additional logic had to be added to handle slices in the list of indices. In the TensorSlice class, indices immediately following one or more slice had to be shifted so that the correct element would be accessed (as demonstrated in the third example). get_index in util.py also had to be modified to return the correct index when a slice of a tensor was being set. 

**Link to the tests:** The tests are in heterocl/tests/test_dsl_tensorslice.py