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

Implement better testing for Array::sizeinfo() #168

Draft
wants to merge 1 commit into
base: main
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
11 changes: 11 additions & 0 deletions dwave/optimization/include/dwave-optimization/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ class fraction {
return lhs;
}

constexpr friend fraction operator+(const fraction& lhs, const fraction& rhs) {
return fraction(lhs.numerator_ * rhs.denominator_ + rhs.numerator_ * lhs.denominator_,
lhs.denominator_ * rhs.denominator_);
}
constexpr friend fraction operator+(const fraction& lhs, const std::integral auto& rhs) {
return lhs + fraction(rhs);
}
constexpr friend fraction operator+(const std::integral auto& lhs, fraction& rhs) {
return fraction(lhs) + rhs;
}

/// Fractions can be printed
friend std::ostream& operator<<(std::ostream& os, const fraction& rhs) {
os << "fraction(" << rhs.numerator();
Expand Down
1 change: 1 addition & 0 deletions dwave/optimization/src/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool SizeInfo::operator==(const SizeInfo& other) const {

SizeInfo SizeInfo::substitute(ssize_t max_depth) const {
if (max_depth <= 0) return *this;
if (this->array_ptr == nullptr) return *this;

SizeInfo sizeinfo = this->array_ptr->sizeinfo();

Expand Down
38 changes: 32 additions & 6 deletions dwave/optimization/src/nodes/indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,38 @@ ssize_t AdvancedIndexingNode::size(const State& state) const {
}

SizeInfo AdvancedIndexingNode::sizeinfo() const {
// easy case, fixed size
if (!dynamic()) return SizeInfo(size());
// when we get around to supporting broadcasting this will need to change
assert(predecessors().size() >= 2);
assert(!dynamic_cast<ArrayNode*>(predecessors()[0])->dynamic() &&
"sizeinfo for dynamic base arrays not supported");
return SizeInfo(dynamic_cast<ArrayNode*>(predecessors()[1]));

return SizeInfo(this);

// // if the base array is dynamic AND the first indexer is a slice then
// // our size is derived from the base array via alchemy
// assert(indices_.size() >= 1); // should always be true
// if (array_ptr_->dynamic() && std::holds_alternative<Slice>(indices_[0])) {
// // there are additional subcases we can investigate here to try to be
// // more specific, e.g. whether or not any of the indexing arrays are
// // dynamic etc... But for now let's just handwave a bit, say our size
// // is derived from ourselves, and so the best we can with the upper bound.
// std::optional<ssize_t> max;
// for (const Node* ptr : predecessors()) {
// // 100 is a magic number... we really need a better way to do this.
// auto sizeinfo = dynamic_cast<const ArrayNode*>(ptr)->sizeinfo().substitute(100);
// // assert(false);
// if (max && sizeinfo.max) {
// if (*sizeinfo.max < *max) max = sizeinfo.max;
// } else if (sizeinfo.max) {
// max = sizeinfo.max;
// }
// }
// return SizeInfo(this, std::nullopt, max);
// }

// // If the first indexer is not a slice, then whether or not we are dynamic
// // we derive our size from indexing arrays.
// // If we eventually add broadcasting then this will need to change.
// assert(predecessors().size() >= 2); // should always be true
// return SizeInfo(dynamic_cast<ArrayNode*>(predecessors()[1]));
}

std::span<const ssize_t> AdvancedIndexingNode::shape(const State& state) const {
Expand Down Expand Up @@ -1515,7 +1541,7 @@ ssize_t BasicIndexingNode::size(const State& state) const {
}

SizeInfo BasicIndexingNode::sizeinfo() const {
if (size_ >= 0) return SizeInfo(size_);
if (!dynamic()) return SizeInfo(size_);

auto sizeinfo = SizeInfo(array_ptr_);

Expand Down
14 changes: 14 additions & 0 deletions dwave/optimization/src/nodes/testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void check_shape(const std::span<const ssize_t>& dynamic_shape,
ArrayValidationNode::ArrayValidationNode(ArrayNode* node_ptr) : array_ptr(node_ptr) {
assert(array_ptr->ndim() == static_cast<ssize_t>(array_ptr->shape().size()));
assert(array_ptr->dynamic() == (array_ptr->size() == -1));
node_ptr->sizeinfo(); // smoke check
add_predecessor(node_ptr);
}

Expand Down Expand Up @@ -183,6 +184,19 @@ void ArrayValidationNode::propagate(State& state) const {
assert(std::ranges::max(array_ptr->view(state)) <= array_ptr->max());
assert(!array_ptr->integral() || std::ranges::all_of(array_ptr->view(state), is_integer));
}

// check that whatever sizeinfo the array reports is accurate
auto sizeinfo = array_ptr->sizeinfo();
if (sizeinfo.array_ptr != nullptr) {
// the size is at least theoretically derived from another array, so let's check that the
// reported multiplier/offset are correct
assert(array_ptr->size(state) ==
sizeinfo.multiplier * sizeinfo.array_ptr->size(state) + sizeinfo.offset);
} else {
assert(array_ptr->size(state) == sizeinfo.offset);
}
assert(!sizeinfo.max || array_ptr->size(state) <= *sizeinfo.max);
assert(!sizeinfo.min || array_ptr->size(state) >= *sizeinfo.min);
}

void ArrayValidationNode::revert(State& state) const {
Expand Down