ndstrides: [3] Implement indexing/slicing #513

Open
lyken wants to merge 3 commits from ndstrides-3-indexing into ndstrides-2-basic
Collaborator

This PR implements indexing/slicing with ndarray strides.

  • Slicing an ndarray no longer copies ndarray->data and only creates a new ndarray with different strides but points to the same data ndarray->data, which is very cheap. This is only possible with the addition of strides to NAC3's ndarray definition. This is how NumPy implements ndarrays too.

  • ... and np.newaxis have been implemented using IRRT, but they are only there to help with implementing other aspects of ndarray with strides (e.g., NDArrayObject::atleast_nd() in a later commit, it uses np.newaxis internally to prepend 1s to the dimension; or in matmul, np.newaxis and ... are used to deal with 1D matrices). You cannot write my_array[..., none] in NAC3 Python as the type inferencer/code generator cannot understand it. I have encountered some problems when trying to implement this into NAC3:

    1. ... does not have a concrete type. #486. Probably requires a hack on the typechecker.
    2. Consider my_array[..., none] again; none has type <class 'Option'>, but NumPy wants a NoneType. This causes issues in @portable.

Extra note: After this PR, there is finally a proper way to start developing ndarray with strides with nac3standalone, since now you can print the contents of an ndarray and debug:

@extern
def output_float64(x: float):
    ...

# This requires ndarray indexing to be implemented.
def output_ndarray_float_2(n: ndarray[float, Literal[2]]):
    for r in range(len(n)):
        for c in range(len(n[r])):
            output_float64(n[r][c])

def run() -> int32:
    xs = np_full([3, 4], 9999.9999)
    output_ndarray_float_2(xs)
    return 0
This PR implements indexing/slicing with ndarray strides. - Slicing an ndarray no longer copies `ndarray->data` and only creates a new ndarray with different strides but points to the same data `ndarray->data`, which is very cheap. This is only possible with the addition of strides to NAC3's ndarray definition. This is how NumPy implements ndarrays too. - `...` and `np.newaxis` have been implemented using IRRT, but they are only there to help with implementing other aspects of ndarray with strides (e.g., `NDArrayObject::atleast_nd()` in a later commit, it uses `np.newaxis` internally to prepend 1s to the dimension; or in `matmul`, `np.newaxis` and `...` are used to deal with 1D matrices). You cannot write `my_array[..., none]` in NAC3 Python as the type inferencer/code generator cannot understand it. I have encountered some problems when trying to implement this into NAC3: 1. `...` does not have a concrete type. https://git.m-labs.hk/M-Labs/nac3/issues/486. Probably requires a hack on the typechecker. 2. Consider `my_array[..., none]` again; `none` has type `<class 'Option'>`, but NumPy wants a `NoneType`. This causes issues in `@portable`. Extra note: After this PR, there is finally a proper way to start developing ndarray with strides with nac3standalone, since now you can print the contents of an ndarray and debug: ```python @extern def output_float64(x: float): ... # This requires ndarray indexing to be implemented. def output_ndarray_float_2(n: ndarray[float, Literal[2]]): for r in range(len(n)): for c in range(len(n[r])): output_float64(n[r][c]) def run() -> int32: xs = np_full([3, 4], 9999.9999) output_ndarray_float_2(xs) return 0 ```
lyken added 3 commits 2024-08-28 13:48:48 +08:00
5537645395
core/irrt: add Slice and Range
Needed for implementing general ndarray indexing.

Currently the IRRT slice and range have nothing to do with NAC3's slice
and range.
fda7f8d827
core/irrt: rename NDIndex to NDIndexInt
The name `NDIndex` is used in later commits.
510dbfc70e
core/ndstrides: implement ndarray indexing
The functionality for `...` and `np.newaxis` is there in IRRT, but there
is no implementation of them for @kernel Python expressions because of
#486.
lyken added a new dependency 2024-08-28 13:52:09 +08:00
lyken force-pushed ndstrides-3-indexing from 40cf1c747f to fdd5d37490 2024-08-30 14:11:18 +08:00 Compare
Author
Collaborator

Rebased.

Rebased.
lyken force-pushed ndstrides-3-indexing from fdd5d37490 to a40cdde8d2 2024-08-30 14:13:05 +08:00 Compare
Author
Collaborator

Rebased to fix some formatting issues.

Rebased to fix some formatting issues.
derppening force-pushed ndstrides-3-indexing from a40cdde8d2 to 0206c3989a 2024-10-17 13:57:21 +08:00 Compare
derppening force-pushed ndstrides-3-indexing from 0206c3989a to c71bd81278 2024-10-17 16:08:55 +08:00 Compare
derppening force-pushed ndstrides-3-indexing from c71bd81278 to 3d8e66a54e 2024-10-17 16:30:23 +08:00 Compare
derppening force-pushed ndstrides-3-indexing from 3d8e66a54e to 8f9d2d82dd 2024-10-18 14:23:16 +08:00 Compare
This pull request can be merged automatically.
You are not authorized to merge this pull request.
You can also view command line instructions.

Step 1:

From your project repository, check out a new branch and test the changes.
git checkout -b ndstrides-3-indexing ndstrides-2-basic
git pull origin ndstrides-3-indexing

Step 2:

Merge the changes and update on Gitea.
git checkout ndstrides-2-basic
git merge --no-ff ndstrides-3-indexing
git push origin ndstrides-2-basic
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: M-Labs/nac3#513
No description provided.