fferflo / einx

Universal Tensor Operations in Einstein-Inspired Notation for Python.
https://einx.readthedocs.io/en/stable/
MIT License
311 stars 8 forks source link

Swap sequence of ein expression in cat and split scenario cause assertion error. #6

Closed gRox167 closed 5 months ago

gRox167 commented 5 months ago

Required prerequisites

I have searched the Issue Tracker that this hasn't already been reported. (comment there if it has.)

Problem

When we want to select specific slice of data, and get the remaining data we can do

x = np.zeros(5,4)
front_idx = 1
select_idx = front+2
selected = x[front_idx:select_idx]
remained = np.concatenate(x[0:front_idx], x[select_idx: ])

In the bestcase, we can use ein expression (front + select + end) d -> (front + end) d, b d to describe this operation

x = np.zeros(5,4)
remained, selected = einx.rearrange(
    "(front + select + end) d -> (front + end) d, select d", x, front=1, select=2)

which is fine, however if we exchange the sequence of output and ein expression:

x = np.zeros((5, 4))
selected, remained = einx.rearrange(
    "(front + select + end) d -> select d, (front + end) d", x, front=1, select=2
)

einx raised an error:

    [165] einx/op/util.py:165    subtensor = _unflatten(exprs_in, tensors_in, subexpr, backend)

AssertionError:
fferflo commented 5 months ago

Yes, this should indeed work, but didn't due to a bug. Thanks for bringing it up, it should be fixed with https://github.com/fferflo/einx/commit/a313bc530f94738168b5b6a9e6bb1d3205fd0ea0.