[core][ndarray] Fix shape reconstruction logic in NDArray.__getitem__()#326
Conversation
|
Note it looks like a lot of unit tests in |
Caused by: ``` ValueError: Number of dimensions must be positive, got 0. Provide at least one shape dimension. [at NDArrayShape.__init__(shape: List[Int])] ``` on ``` print(array[0, 0]) ``` Additionally added a TODO since the error message was otherwise extremely vague.
5d0d2ad to
4ed56dc
Compare
index_type_list in NDArray.__getitem__NDArray.__getitem__()
|
@josiahls, thank you for the PR. Your PR catched a missing However, this PR is not sufficient to address the problem. There are some other bugs in the code related to shape reconstruction logic, which will be even exposued after adding the index_type_list. So I made some other fixes on top of your PR, changed upon yours, and update the description. @shivasankarka, I am adding tests for this case, please let me handle the approval for this PR. Thanks :D |
forfudan
left a comment
There was a problem hiding this comment.
Approved with some further fixes, need to also add some tests in another PR.
842a67d
into
Mojo-Numerics-and-Algorithms-group:pre-0.9
…_()` (#326) This PR fixes shape reconstruction logic in `NDArrray.__getitem__()` dunder method. 1. Added `index_type_list.append(IndexTypeInfo(is_integer=True))`, without which the mixture of slices and integers would cause integers to be missed in the record. 2. Fixed `is_integer` branch: The old code `new_shape.append(narr.shape[i])` was wrong in two ways — it used `i` (position in `index_type_list`) instead of a proper dimension counter, and it added the dimension to the output shape when integer indexing should remove it (dimension reduction). Changed to just advance `narr_dim` without appending (skip it). 3. Fixed missing trailing dimensions: When `index_type_list` has fewer entries than narr's dimensions (e.g., `b[Slice(0,2)]` on a 2D array), remaining dimensions were silently dropped. Added a while `narr_dim < ndims` loop to preserve them. 4. Fixed ellipsis dimension counting: Changed from `self.ndim - len(index_type_list) + 1` (which incorrectly assumes every non-ellipsis entry consumes a dimension — NewAxis doesn't) to counting only `is_slice` and `is_integer` entries.
Caused by:
on
Additionally added a TODO since the error message was otherwise extremely vague.
Updates during review by @forfudan:
index_type_list.append(IndexTypeInfo(is_integer=True)), without which the mixture of slices and integers would cause integers to be missed in the record.However, this PR is not sufficient to address the problem. There are some other bugs in the code related to shape reconstruction logic, which will be even exposued after adding the index_type_list.
Here are some further fixes:
Fixed
is_integerbranch: The old codenew_shape.append(narr.shape[i])was wrong in two ways — it usedi(position inindex_type_list) instead of a proper dimension counter, and it added the dimension to the output shape when integer indexing should remove it (dimension reduction). Changed to just advancenarr_dimwithout appending (skip it).Fixed missing trailing dimensions: When
index_type_listhas fewer entries than narr's dimensions (e.g.,b[Slice(0,2)]on a 2D array), remaining dimensions were silently dropped. Added a whilenarr_dim < ndimsloop to preserve them.Fixed ellipsis dimension counting: Changed from
self.ndim - len(index_type_list) + 1(which incorrectly assumes every non-ellipsis entry consumes a dimension — NewAxis doesn't) to counting onlyis_sliceandis_integerentries.Removed stale TODO from the original PR:
# TODO: Shouldn't we assert len(shape_size) == len(slice_list)? referenced a nonexistent variableshape_size.