feat: add foundational search utilities and extend bitmask primitives#8
feat: add foundational search utilities and extend bitmask primitives#8dhalmazna wants to merge 2 commits intofeat/segment-and-hyperpixel-scoringfrom
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request lays the groundwork for upcoming search algorithms by centralizing common logic and low-level graph operations. It aims to keep the main algorithm implementations clean and optimized by providing dedicated utility modules for tasks such as efficient bitmask manipulation and batched evaluation of segment masks, ensuring a robust and scalable foundation. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a solid set of foundational utilities for search algorithms and bitmask operations. The new functions are well-documented and the separation into bitmask_graph.py and search_helpers.py is logical. My review focuses on improving consistency, efficiency, documentation accuracy, and robustness. I've suggested a more efficient implementation for mask_to_ids, a correction to a docstring, a robustness improvement for sample_connected_superset, and a change to ensure the new mask_to_ids utility is used consistently.
There was a problem hiding this comment.
Pull request overview
Adds foundational utilities to support upcoming Monte Carlo search algorithms by centralizing common terminal-state checks, batched mask evaluation, and extending bitmask graph primitives.
Changes:
- Added
ciao/algorithm/search_helpers.pywithis_terminalandevaluate_masksfor shared MCTS/MCGS logic. - Extended
ciao/algorithm/bitmask_graph.pywith node mutation helpers, random set-bit selection, and a connected-superset sampler. - Re-exported the new utilities from
ciao/algorithm/__init__.py.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| ciao/algorithm/search_helpers.py | New shared helpers for terminal checks and batched reward evaluation. |
| ciao/algorithm/bitmask_graph.py | New bitmask primitives and sampling utilities for graph-based expansions. |
| ciao/algorithm/init.py | Exposes new utilities at the package level via imports and __all__. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| def sample_connected_superset( | ||
| base_mask: int, | ||
| target_length: int, | ||
| adj_masks: tuple[int, ...], | ||
| used_mask: int, | ||
| ) -> int: | ||
| """Sample a connected superset via random walk expansion. | ||
|
|
||
| IMPORTANT: This is NOT a uniform sampler over all connected supersets. | ||
| The distribution is biased towards segments discovered early and | ||
| depends on graph topology. This bias is acceptable for Monte Carlo | ||
| estimation in the parent algorithm. | ||
|
|
||
| Args: | ||
| base_mask: Starting set (must be non-empty and connected) | ||
| target_length: Desired size of the superset | ||
| adj_masks: Adjacency bitmasks for neighbor lookups | ||
| used_mask: Global exclusion mask (segments that must not be added) | ||
|
|
||
| Returns: | ||
| Bitmask of connected superset containing base_mask | ||
| """ | ||
| mask = base_mask | ||
|
|
||
| while mask.bit_count() < target_length: | ||
| frontier = get_frontier(mask, adj_masks, used_mask) | ||
| if frontier == 0: | ||
| break |
There was a problem hiding this comment.
This checking would be too computationally expensive. The mask will be always connected because of the way it is created - we always add a neighbor from the frontier.
fce3836 to
dd0ad92
Compare
Context:
This PR establishes the foundational building blocks required for the upcoming algorithms. By extracting shared logic and low-level graph operations into dedicated utility modules, we ensure the main algorithm implementations will remain clean, readable, and highly optimized.
What was added / changed:
algorithm/search_helpers.py: Introduced utilities used by the search algorithms:evaluate_masks: A wrapper for batched mask evaluation that now correctly propagates thereplacement_image(aligning with our stateless predictor architecture).is_terminal: A fast state-check helper to determine if a search path has reached its maximum depth or exhausted its frontier.algorithm/bitmask_graph.py: Expanded the low-level bitwise operations. Added fast primitives for node mutation (add_node,remove_node,has_node), random selection (pick_random_set_bit), and a random walk sampler (sample_connected_superset) for Monte Carlo rollouts.Related Task:
XAI-29