NDArray with strides + NDArrayObject + Models + Exceptions in IRRT. #506
No reviewers
Labels
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: M-Labs/nac3#506
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "ndstrides-intro"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This PR is primarily about adding strides to NAC3's ndarray definition. Merging into the separate branch
ndstrides
that is in sync withmaster
as the refactoring ofnac3core
withModel<'ctx>
and*Object<'ctx>
are incomplete (See below).Solves #397, #411, and partially solves #278.
To help in implementing ndarray with strides, the following things are also introduced:
Model<'ctx>
abstraction - a thin layer of Rust type-hints over LLVM values. It also has mechanisms to help defining new LLVM struct types with very little boilerplate. LLVM 15 opaque pointers have also been accounted for. Shortly there will be a PR that refactorsnac3core
withModel
s.Object
- an LLVM value with related typechecker types in a single struct. This is used to organize programming interfaces. This PR primarily works withNDArrayObject<'ctx>
, but there are a few others including{Tuple,List,Any}Object
to help with interfacing. Shortly there will be a PR that refactorsnac3core
withObject
s..clang-format
is added.-DIRRT_DEBUG_ASSERT
innac3core/build.rs
. Debug assertions throw exceptions.Overview on changes/additions about NDArrays
ndarray->data
and only creates a new ndarray with different strides but points to the same datandarray->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....
andnp.newaxis
have been implemented, but they are only there to help with implementing other aspects of ndarray with strides. You cannot writemy_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:...
does not have a concrete type. #486. Probably requires a hack on the typechecker.my_array[..., none]
again;none
has type<class 'Option'>
, but NumPy wants aNoneType
; and consider@portable
.np_array()
+ Proper assertions when the input is a list (e.g., raise an exception when the list has inhomogeneous dimensions).np_strides(),
np_shape()
, andnp_size()
. These were initially used for debugging but they have been implemented as real NAC3 functions anyway. Note that in NumPy,np.strides()
is not an actual function, but<ndarray>.strides
is used instead.np_reshape()
+ More concise checks for when there is an unknown dimension. Implemented in IRRT.np_reshape()
can reshape an ndarray without making a copy of the data, under certain conditions.np.reshape(<ndarray>)
may or may not make a copy depending on if a reshape is "possible". Currently, NAC3's criterion is by simply checkingNDArrayObject::is_c_contiguous()
to decide whether or not to make a copy by playing around with the ndarray stride values, but the criterion is incomplete.np_broadcast_to()
.np_transpose()
.np_transpose
can transpose an ndarray without making a copy of the data.<axes>
is implemented in IRRT, but not in typechecker and code generator yet.np_transpose(np_transpose(my_array)[::3])[0, 2:100] = 1.0
andmy_array
would update. This is only possible with ndarray with strides.Other notes
np_size()
's function signature: Is the function signature correct? I have encountered issues where if I directly useself.primitives.ndarray
as the type fora
,self.primitives.ndarray
'sdtype
andndims
are substituted permanently. See commit "core/ndstrides: implement np_size()".np_size()
when theother_ty
ofimpl_matmul
is simply set tondarray_unsized_t
. See commit "core/ndstrides: implement general matmul".Closing, to be broken down into smaller PRs.
Pull request closed