Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support higher axis numbers for dr.any and dr.all reductions #304

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/python/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,23 @@ nb::object reduce(uint32_t op, nb::handle h, nb::handle axis_, nb::handle mode)
value = reduce(op, value, axis, mode);
return tp(value, nb::tuple());
} else {
if (op >= (uint32_t) ReduceOp::Count) {
if (op == (uint32_t) ReduceOpExt::Count) {
if (axis_len == 1 && red_axis == 0)
return reduce_seq(op, h, nb::int_(0), mode);
nb::raise_type_error("tensor type is not compatible with "
"the requested reduction.");
}
// Complex case, defer to a separate Python implementation
ReduceOp reduceOp;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor. rendceOp -> reduce_op (underscore case for vriables)

if (op < (uint32_t) ReduceOp::Count) {
reduceOp = static_cast<ReduceOp>(op);
} else if (op == (uint32_t) ReduceOpExt::All) {
reduceOp = ReduceOp::And;
} else if (op == (uint32_t) ReduceOpExt::Any) {
reduceOp = ReduceOp::Or;
}
return nb::module_::import_("drjit._reduce")
.attr("tensor_reduce")(ReduceOp(op), h, axis, mode);
.attr("tensor_reduce")(reduceOp, h, axis, mode);
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,22 @@ def test_red(shape, axis):
test_red((9, 5, 7), 1)
test_red((9, 5, 7), 2)
test_red((9, 5, 7), -1)

@pytest.test_arrays('tensor, bool')
def test14_any_all_multidimensional_tensors(t):
v_all = t([[True, False, True],
[True, True, True],
[True, False, True],
[True, True, True]
])
assert list(dr.all(v_all, axis=0)) == [True, False, True]
assert list(dr.all(v_all, axis=1)) == [False, True, False, True]

v_any = t([[False, False, False],
[False, True, False],
[False, True, False],
[False, True, False]
])

assert list(dr.any(v_any, axis=0)) == [False, True, False]
assert list(dr.any(v_any, axis=1)) == [False, True, True, True]