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

refactor: Combine ExtensionSolutions (no separate closure) #884

Merged
merged 20 commits into from
Apr 9, 2024

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented Mar 18, 2024

  • infer::infer_extensions returns only a combined solution (for previously-open locations), after variables instantiated
  • Hugr::infer_extensions writes (all parts of) the solution into place and returns it
  • validate_with_extension_closure left in-place, with test demonstrating usage w/ sub-DFGs
  • This should open the way (in future PRs) to changing implementation of infer::infer_extensions

@acl-cqc acl-cqc marked this pull request as draft March 18, 2024 11:02
Copy link

codecov bot commented Mar 18, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 12 lines in your changes are missing coverage. Please review.

Project coverage is 85.49%. Comparing base (b05dd6b) to head (178f9c8).

Files Patch % Lines
hugr/src/extension/infer/test.rs 79.24% 3 Missing and 8 partials ⚠️
hugr/src/extension/infer.rs 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #884      +/-   ##
==========================================
- Coverage   85.53%   85.49%   -0.04%     
==========================================
  Files          78       78              
  Lines       14176    14220      +44     
  Branches    14176    14220      +44     
==========================================
+ Hits        12125    12158      +33     
- Misses       1423     1428       +5     
- Partials      628      634       +6     
Flag Coverage Δ
rust 85.49% <83.33%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acl-cqc acl-cqc force-pushed the refactor/one_extension_solution branch from 575a687 to 42a7d05 Compare March 18, 2024 21:22
@acl-cqc acl-cqc marked this pull request as ready for review March 26, 2024 10:02
@acl-cqc acl-cqc requested a review from croyzor March 26, 2024 10:02
@acl-cqc
Copy link
Contributor Author

acl-cqc commented Mar 26, 2024

IIUC coverage is not reduced by the PR, just that the lines affected by the PR have lower-than-average coverage anyway, and AFAICS that is because of assert_matches!, so I think this is OK. (Run codecov/project shows this increases overall coverage by 0.04%.)

@croyzor
Copy link
Contributor

croyzor commented Apr 4, 2024

Casting my mind back, I think the motivation for returning the open solution is that we might want to run extension inference on a subgraph before we add it to the main graph. I'm not really sure when this would be the case -- I think the case is that when doing replacements, we run inference on the complete graph before and after, so it's probably unnecessary

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Apr 5, 2024

Casting my mind back, I think the motivation for returning the open solution is that we might want to run extension inference on a subgraph before we add it to the main graph. I'm not really sure when this would be the case -- I think the case is that when doing replacements, we run inference on the complete graph before and after, so it's probably unnecessary

Ah-ha, I see. So the flow would have been:

  1. Run inference on the subgraph. Write into the subgraph only the solutions that do not depend on the subgraph inputs.
  2. Substitute the subgraph into the main graph.
  3. Rerun inference on the main graph in order to figure out the solutions that did depend on the subgraph inputs.

Noting that if in step 1 we wrote in all the solutions, including those dependent on the subgraph inputs, then step 3 would fail because the inserted graph would have bad solutions (e.g. emptyset) that conflict.

This PR removes the ability to write only those solutions into the subgraph. However, the flow is largely intact if one does not write any solutions into the subgraph before insertion. (You can still validate the subgraph using the full set of solutions, if required.)

Close enough? I think I can realize that in a test.

@acl-cqc
Copy link
Contributor Author

acl-cqc commented Apr 9, 2024

@croyzor added a much more thorough test which I hope covers that use-case (as well as we can - there'll be a better, but separate, solution if we do #709). I had to make infer_extensions(&Hugr) return only the new solutions rather than existing ones to get around assertions in gather_extensions. Can you take another look?

let all_results = ctx.main_loop()?;
let new_results = all_results
.into_iter()
.filter(|(n, _sol)| hugr.get_nodetype(*n).input_extensions().is_none())
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks reasonable 👍

Copy link
Contributor

@croyzor croyzor left a comment

Choose a reason for hiding this comment

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

Nice, let's update this once can give a better answer for polymorphic hugrs!

@acl-cqc acl-cqc added this pull request to the merge queue Apr 9, 2024
Merged via the queue into main with commit d98fb79 Apr 9, 2024
17 of 18 checks passed
@acl-cqc acl-cqc deleted the refactor/one_extension_solution branch April 9, 2024 13:45
github-merge-queue bot pushed a commit that referenced this pull request Apr 15, 2024
## 🤖 New release
* `hugr`: 0.3.0-alpha.1 -> 0.3.0-alpha.2 (⚠️ API breaking changes)

### ⚠️ `hugr` breaking changes

```
--- failure enum_missing: pub enum removed or renamed ---

Description:
A publicly-visible enum cannot be imported by its prior path. A `pub use` may have been removed, or the enum itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/enum_missing.ron

Failed in:
  enum hugr::ops::custom::ExternalOp, previously in file /tmp/.tmpZslYkR/hugr/src/ops/custom.rs:20
  enum hugr::ops::leaf::LeafOp, previously in file /tmp/.tmpZslYkR/hugr/src/ops/leaf.rs:21
  enum hugr::ops::LeafOp, previously in file /tmp/.tmpZslYkR/hugr/src/ops/leaf.rs:21

--- failure enum_variant_added: enum variant added on exhaustive enum ---

Description:
A publicly-visible enum without #[non_exhaustive] has a new variant.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/enum_variant_added.ron

Failed in:
  variant ConstTypeError:NotMonomorphicFunction in /tmp/.tmpoZAGEw/hugr/hugr/src/ops/constant.rs:98
  variant ConstTypeError:NotMonomorphicFunction in /tmp/.tmpoZAGEw/hugr/hugr/src/ops/constant.rs:98
  variant SignatureError:CallIncorrectlyAppliesType in /tmp/.tmpoZAGEw/hugr/hugr/src/extension.rs:172

--- failure enum_variant_missing: pub enum variant removed or renamed ---

Description:
A publicly-visible enum has at least one variant that is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/enum_variant_missing.ron

Failed in:
  variant InterGraphEdgeError::InvalidConstSrc, previously in file /tmp/.tmpZslYkR/hugr/src/hugr/validate.rs:774
  variant OpType::LeafOp, previously in file /tmp/.tmpZslYkR/hugr/src/ops.rs:30
  variant SignatureError::TypeApplyIncorrectCache, previously in file /tmp/.tmpZslYkR/hugr/src/extension.rs:171
  variant EdgeKind::Static, previously in file /tmp/.tmpZslYkR/hugr/src/types.rs:44
  variant ConstTypeError::FunctionTypeMissing, previously in file /tmp/.tmpZslYkR/hugr/src/ops/constant.rs:99
  variant ConstTypeError::FunctionTypeMissing, previously in file /tmp/.tmpZslYkR/hugr/src/ops/constant.rs:99

--- failure inherent_method_missing: pub method removed or renamed ---

Description:
A publicly-visible method or associated fn is no longer available under its prior name. It may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/inherent_method_missing.ron

Failed in:
  OpType::as_leaf_op, previously in file /tmp/.tmpZslYkR/hugr/src/ops.rs:103
  OpType::is_leaf_op, previously in file /tmp/.tmpZslYkR/hugr/src/ops.rs:103

--- failure method_parameter_count_changed: pub method parameter count changed ---

Description:
A publicly-visible method now takes a different number of parameters.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#fn-change-arity
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/method_parameter_count_changed.ron

Failed in:
  hugr::types::PolyFuncType::validate now takes 2 parameters instead of 3, in /tmp/.tmpoZAGEw/hugr/hugr/src/types/poly_func.rs:76

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.30.0/src/lints/struct_missing.ron

Failed in:
  struct hugr::ops::leaf::TypeApplication, previously in file /tmp/.tmpZslYkR/hugr/src/ops/leaf.rs:82
```

<details><summary><i><b>Changelog</b></i></summary><p>

<blockquote>

## 0.3.0-alpha.2 (2024-04-15)

### Documentation

- Specify direct children in `HugrView::children`
([#921](#921))
- Add logo svg to readme and spec
([#925](#925))

### Features

- [**breaking**] No polymorphic closures
([#906](#906))
- [**breaking**] Flatten `LeafOp`
([#922](#922))

### Refactor

- Combine ExtensionSolutions (no separate closure)
([#884](#884))
- [**breaking**] Merge `CustomOp` and `ExternalOp`.
([#923](#923))
</blockquote>


</p></details>

---
This PR was generated with
[release-plz](https://github.com/MarcoIeni/release-plz/).

---------

Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Agustin Borgna <agustin.borgna@quantinuum.com>
@github-actions github-actions bot mentioned this pull request Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants