-
Notifications
You must be signed in to change notification settings - Fork 35
Implement state checkpointing for decision nodes #587
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
Open
arcondello
wants to merge
12
commits into
dwavesystems:main
Choose a base branch
from
arcondello:feature/decision-checkpoints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
953304e
Implement state checkpointing for CollectionNode
arcondello 214c5e2
Fix reverts and dangling pointers for CollectionNode checkpoints
arcondello 9cd035b
Use offical Python images in CircleCI
arcondello 6ec49f9
Add LinkedListCheckpoint helper class
arcondello fc44824
Fix the copying of checkpointed states
arcondello f711e63
Drop unused NodeStateCheckpoint::valid() method
arcondello e85f85f
Add DiffCheckpoint class for checkpoints that track diffs
arcondello f70699e
Go back to using CircleCI image for some CI tasks
arcondello ef950e4
Add NumberNode::checkpoint()
arcondello e61a70f
Fix assigning from a checkpoint after mutation
arcondello e845381
Add test for BinaryNode::assign_from_checkpoint
arcondello 4402568
Make all DecisionNodes implement checkpointing
arcondello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,11 @@ void Graph::propose( | |
| } | ||
| } | ||
|
|
||
| void Graph::propose(State& state) const { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| propagate(state); | ||
| commit(state); | ||
| } | ||
|
|
||
| void Graph::recursive_initialize(State& state, const Node* ptr) { | ||
| ssize_t index = ptr->topological_index(); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2026 D-Wave | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "_checkpoints.hpp" | ||
|
|
||
| namespace dwave::optimization { | ||
|
|
||
| CheckpointableState::~CheckpointableState() { | ||
| if (prev_ptr_ == nullptr) return; // nothing to clean up | ||
| prev_ptr_->next_ptr_ = static_cast<CheckpointableState*>(nullptr); | ||
|
fastbodin marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Place self between the state and any checkpoint it's currently holding | ||
| LinkedListCheckpoint::LinkedListCheckpoint(CheckpointableState& state) : | ||
| prev_ptr_(state.prev_ptr_), next_ptr_(&state) { | ||
| if (prev_ptr_ != nullptr) prev_ptr_->next_ptr_ = this; | ||
| state.prev_ptr_ = this; | ||
| } | ||
|
|
||
| LinkedListCheckpoint::~LinkedListCheckpoint() { | ||
|
fastbodin marked this conversation as resolved.
|
||
| if (prev_ptr_ != nullptr) prev_ptr_->next_ptr_ = next_ptr_; | ||
|
|
||
| // Now make sure next_ptr is pointing to prev_ptr (which can be null) | ||
| std::visit( | ||
| [&](auto* next_ptr) -> void { | ||
| if (next_ptr == nullptr) return; // state was destructed first | ||
| next_ptr->prev_ptr_ = prev_ptr_; | ||
| }, | ||
| next_ptr_ | ||
| ); | ||
| } | ||
|
|
||
| DiffCheckpoint::DiffCheckpoint(CheckpointableState& state, ssize_t drop) : | ||
| LinkedListCheckpoint(state), updates_(), drop_(drop) {} | ||
|
|
||
| DiffCheckpoint::DiffCheckpoint(CheckpointableState& state, std::span<const Update> diff) : | ||
| LinkedListCheckpoint(state), updates_(), drop_(diff.size()) { | ||
| if (auto* prev_ptr = static_cast<DiffCheckpoint*>(prev_ptr_)) { | ||
| prev_ptr->commit_updates(std::vector<Update>(diff.begin(), diff.end())); | ||
| assert(prev_ptr->drop_ == 0); | ||
| } | ||
| } | ||
|
|
||
| DiffCheckpoint::~DiffCheckpoint() { | ||
| // if we're the oldest checkpoint, just let whatever information we're | ||
| // holding get destructed with us | ||
| if (prev_ptr_ == nullptr) return; | ||
|
|
||
| // otherwise we need to transfer our info over | ||
| auto* prev_ptr = static_cast<DiffCheckpoint*>(prev_ptr_); | ||
| assert(prev_ptr->drop_ == 0); | ||
| for (auto& updates : updates_) prev_ptr->commit_updates(std::move(updates)); | ||
| prev_ptr->drop_ = drop_; | ||
| } | ||
|
|
||
| void DiffCheckpoint::commit_updates(std::vector<Update> updates) { | ||
| assert(0 <= drop_ and static_cast<size_t>(drop_) <= updates.size()); | ||
|
|
||
| if (drop_) { | ||
| updates.erase(updates.begin(), updates.begin() + drop_); | ||
| drop_ = 0; | ||
| } | ||
|
|
||
| updates_.emplace_back(std::move(updates)); | ||
| } | ||
|
|
||
| void DiffCheckpoint::revert_updates(std::vector<Update> updates) { | ||
| assert(0 <= drop_ and static_cast<size_t>(drop_) <= updates.size()); | ||
|
|
||
| if (not drop_) return; // nothing to do | ||
|
|
||
| // We want to track the updates that would revert the changes from the | ||
| // current state. | ||
| // In C++23 we could use assign_range() which would be nicer | ||
| auto relevant = std::move(updates) | std::views::take(drop_) | std::views::reverse | | ||
| std::views::transform([](const Update& up) { return up.inverse(); }); | ||
| updates_.emplace_back(relevant.begin(), relevant.end()); | ||
|
|
||
| drop_ = 0; | ||
| } | ||
|
|
||
| } // namespace dwave::optimization | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright 2026 D-Wave | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <span> | ||
| #include <variant> | ||
| #include <vector> | ||
|
|
||
| #include "dwave-optimization/array.hpp" | ||
| #include "dwave-optimization/state.hpp" | ||
|
|
||
| namespace dwave::optimization { | ||
|
|
||
| class LinkedListCheckpoint; | ||
|
|
||
| class CheckpointableState { | ||
| public: | ||
| CheckpointableState() = default; | ||
|
|
||
| CheckpointableState(const CheckpointableState&) {} // the checkpoint pointer is not copied | ||
| CheckpointableState(CheckpointableState&&) = default; | ||
|
|
||
| CheckpointableState& operator=(const CheckpointableState&) = delete; | ||
| CheckpointableState& operator=(CheckpointableState&&) = default; | ||
|
|
||
| ~CheckpointableState(); | ||
|
|
||
| protected: | ||
| template <std::derived_from<LinkedListCheckpoint> T> | ||
| T* checkpoint_ptr() { | ||
| return static_cast<T*>(prev_ptr_); | ||
| } | ||
|
|
||
| private: // todo: private? | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hanging comment? |
||
| friend LinkedListCheckpoint; | ||
|
|
||
| // The name is a bit confusing, but by making it match LinkedListCheckpoint::prev_ptr_ | ||
| // it makes the implementations of the various visit methods clearer. | ||
| LinkedListCheckpoint* prev_ptr_ = nullptr; // Will be nullptr if there are no checkpoints | ||
| }; | ||
|
|
||
| class LinkedListCheckpoint : public NodeStateCheckpoint { | ||
| public: | ||
| LinkedListCheckpoint() = delete; | ||
| // We're not moveable or copyable because NodeStateCheckpoint is not. | ||
|
|
||
| LinkedListCheckpoint(CheckpointableState& state); | ||
|
|
||
| ~LinkedListCheckpoint() override; | ||
|
|
||
| protected: // todo: private? | ||
| friend CheckpointableState; | ||
|
|
||
| LinkedListCheckpoint* prev_ptr_; | ||
|
|
||
| // Is usually not nullptr unless the state has been destructed | ||
| std::variant<LinkedListCheckpoint*, CheckpointableState*> next_ptr_; | ||
| }; | ||
|
|
||
| class DiffCheckpoint : public LinkedListCheckpoint { | ||
| public: | ||
| DiffCheckpoint(CheckpointableState& state, std::span<const Update> diff); | ||
|
|
||
| ~DiffCheckpoint() override; | ||
|
|
||
| void commit_updates(std::vector<Update> updates); | ||
|
|
||
| auto detach_updates() { | ||
| auto updates = std::move(updates_) | std::views::join; | ||
| assert(updates_.empty()); | ||
| return updates; | ||
| } | ||
|
|
||
| ssize_t& drop() { return drop_; } | ||
|
|
||
| void revert_updates(std::vector<Update> updates); | ||
|
|
||
| protected: | ||
| DiffCheckpoint(CheckpointableState& state, ssize_t drop); | ||
|
|
||
| private: | ||
| std::vector<std::vector<Update>> updates_; | ||
| ssize_t drop_; | ||
| }; | ||
|
|
||
| } // namespace dwave::optimization | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #593
Also I see the typo here.