core: support tuple and int32 input for np_empty, np_ones, and more #434
No reviewers
Labels
No Milestone
No Assignees
2 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: M-Labs/nac3#434
Loading…
Reference in New Issue
No description provided.
Delete Branch "ndfactory-tuple"
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?
Fixes #427 + some additions.
Originally
nac3core
only supports list literals as theshape
parameter for numpy factory functions (e.g.,np_zeros([600, 800, 3])
). These functions includenp_ndarray
,np_zeros
,np_ones
, andnp_full
.This PR extends the
shape
parameter to allow:shape
by aint32
scalar. (e.g.,np_zeros(256)
)shape
by a tuple ofint32
of arbitrary length. (e.g.,np_zeros((600, 800, 3))
,np_zeros((4096,))
)Both need not to be written as a literal to be a valid input to
shape
like the restriction that is put in place for list inputs asnac3core
has to statically deduce thendims
typevar of the returned ndarray. That is:@ -823,0 +832,4 @@
/// 1) the `ndims` value inferred from the input `shape`,
/// 2) and the elaborated expression. Like what other fold functions of [`Inferencer`] would normally return.
///
/// ### Further explanation
I am quite torn on whether "Further Explanation" should be part of the API documentation. I am of the opinion that this should instead belong to comments within the method body, as it is not somethings that callers of the API would need to be concerned about.
Okay, I will move it into the implementation.
@ -823,0 +886,4 @@
//
// Also the rust compiler is not smart enough to tell `ndims` must be initialized when used
// in this block of code. So we do `Option` + `unwrap`.
let mut ndims: Option<u64> = None;
I think this can be rewritten this way:
That way
ndims
would be initialized once.Ah I didn't think to do that! Thanks.
@ -823,0 +924,4 @@
if ndims.is_none() {
return Err(HashSet::from([
format!(
"Expected List (must be a literal)/Tuple/int32 for argument {arg_num} of {id} at {shape_location}. \
Call it a "List Literal".
@ -823,0 +927,4 @@
"Expected List (must be a literal)/Tuple/int32 for argument {arg_num} of {id} at {shape_location}. \
There, you are passing a value of type List as the argument. \
However, this argument is special - you must only supply this argument with a List literal. \
On the other hand, you may instead pass in a tuple, and there would be no such restriction.",
Is this long explanation necessary?
I wrote it this long is because I think this is quite a notable quirk of NAC3 - and these factory functions are probably going to be frequently used by many people. Nonetheless, I would like your opinions on how I could improve this.
I am not entirely sure what "compile-time constants" means exactly. I guess that means a "static"-ishly defined value.
shape
need not to be a compile-time constant fortuple
andint32
in my implementation to determinendims
- unless I am misunderstanding something horribly...Also for the record, here is what
nac3core
(before and also now) allows in my understanding:For now I would replace the message to be
Revised.
fb3588b20f
toee5389f91b
@ -824,0 +883,4 @@
// Auxillary details for error reporting.
// Predefined here because `shape_expr` will be moved when doing `fold_expr`
let shape_expr_name = shape_expr.node.name();
let shape_location = shape_expr.location;
On second thought, these shouldn't be necessary as
shape
will contain these information AFAIK.621ed6382c
to5b11a1dbdd
Further revision and squashed history.