core: implement np.any()
& np.all()
#424
No reviewers
Labels
No Milestone
No Assignees
3 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: M-Labs/nac3#424
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "numpy-anyall"
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?
Implements #279.
New demos have also been added to
nac3standalone/
@ -2774,0 +2872,4 @@
// Immediately begin iterating through the ndarray
ctx.builder.build_unconditional_branch(step_bb).unwrap();
// ##### Inserting into `step_bb` #####
Nitpick but we don't use this comment style anywhere else.
@ -1760,0 +1763,4 @@
debug_assert_prim_is_allowed(prim, &[PrimDef::FunNpAny, PrimDef::FunNpAll]);
let param_ty = &[(self.ndarray_num_ty, "a")];
let ret_ty = self.primitives.bool;
// let var_map = &into_var_map([self.ndarray_dtype_tvar, self.ndarray_ndims_tvar]);
Remove commented-out code.
@ -2773,1 +2773,4 @@
}
/// Check if a [`Type`] is an ndarray.
fn is_type_ndarray(ctx: &mut CodeGenContext<'_, '_>, ty: Type) -> bool {
Use
NDArrayType::is_instance
to directly check on the LLVM value.@ -2774,0 +2787,4 @@
/// Helper function to create `np.any()` and `np.all()`.
///
/// They are mixed together since they are extremely similar in terms of implementation.
fn helper_call_numpy_any_all<'ctx, G: CodeGenerator + ?Sized>(
This function belongs in
codegen/numpy.rs
.The function requires
codegen::builtin_fns::unsupported_type
, which is private. Should I move it too?Follow how other numpy functions (e.g.
np_copy
) are implemented.@ -2774,0 +2790,4 @@
fn helper_call_numpy_any_all<'ctx, G: CodeGenerator + ?Sized>(
generator: &mut G,
ctx: &mut CodeGenContext<'ctx, '_>,
kind: AnyOrAll,
Rather than having a kind, I would just make this function like so:
So that it can be expressed like a reduction operation in functional programming (with the option to explicitly enable boolean short-circuiting).
Then this can be expressed as:
np_any
:call_numpy_bool_reduce(generator, ctx, (in_ty, in_val), llvm_i1.const_zero(), /* |(acc, val)| { acc |= val }, Some(true)
np_all
:call_numpy_bool_reduce(generator, ctx, (in_ty, in_val), llvm_i1.const_ones(), /* |(acc, val)| { acc &= val }, Some(false)
I would also like to take time to extend this to a general fold - to also refactor
np.max()
andnp.min()
.After some thoughts, I will not use folds to implement any/all. The short-circuiting logic was slightly awkward to implement. But more importantly it could not provide the necessary control to optimally implement
np.any()
&np.all()
- they only have to setresult
toon_hit
and then quit, rather than having to accumulate something toresult
for each scalar element within the ndarray.Address the rest of the comments are leave this to me then.
@ -2774,0 +2811,4 @@
/*
NOTE: `np.any(<empty ndarray>)` returns true.
NOTE: `np.all(<empty ndarray>)` returns false.
These two seem to be flipped.
@ -2774,0 +2835,4 @@
let llvm_i8 = ctx.ctx.i8_type();
let llvm_usize = generator.get_size_type(ctx.ctx);
let current_bb = ctx.builder.get_insert_block().unwrap();
Instead of doing this manually, I'd suggest extending
gen_for_callback
(and its family of functions) to also providecont_bb
andupdate_bb
so thatbreak
andcontinue
can be easily generated.An isolated commit has now been created to extend
gen_for_callback
.554c9234b8
toac5acf7156
ac5acf7156
toc4971fad82
Please address all the comments and then assign this to me.
@ -1987,0 +1990,4 @@
///
/// * `ndarray`: The input ndarray [`NDArrayValue`]
/// * `body_fn`: A lambda containing IR statements that acts on every scalar element within `ndarray`.
pub fn gen_ndarray_iter_scalar_callback<'ctx, G, BodyFn>(
Please place this function somewhere before the
gen_*
family of functions.@ -462,6 +462,14 @@ pub fn gen_for<G: CodeGenerator>(
Ok(())
}
#[derive(Debug, Clone, Copy)]
Might as well derive
Eq, PartialEq, Hash
as well.Understood. Will do soon, I am working on and am about to finish #432.
c4971fad82
tocf24bd5e0e
Revised. Squashed to cleanup the branch history. Reassigning to @derppening.
cf24bd5e0e
tob1e97aa2b0
Checkout
From your project repository, check out a new branch and test the changes.