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

feat: Draft for hugr-model with export, import, parsing and pretty printing #1542

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

zrho
Copy link
Contributor

@zrho zrho commented Oct 2, 2024

This PR defines a draft of the model format, and the following parts that operate on the format:

  • Import: A module in the model format can be imported as a core hugr module.
  • Export: A core hugr module can be exported into the model format.
  • Parsing: A model hugr can be parsed from an s-expression based text format.
  • Pretty printing:: A model hugr can be printed into the s-expression format, with readable indentation.

This PR is very big, and yet it does not present a completed feature. The model and core format differ in several aspects. Where they differ, this is mostly intentional: The model is kept flexible enough to incorporate features that we know that we want in the future but are currently hard or impossible to express in the core. Import and export perform conversions where possible, but do not yet cover everything.

To prevent this PR from becoming even bigger and to allow for core and model to converge incrementally from both directions, I suggest the following plan: We merge this PR into main with the model related code being feature gated and considered experimental. We then perform tweaks to model and core in smaller PRs where necessary until they are sufficiently close.

Copy link

codecov bot commented Oct 2, 2024

Codecov Report

Attention: Patch coverage is 69.09570% with 704 lines in your changes missing coverage. Please review.

Project coverage is 85.71%. Comparing base (ea8e818) to head (706b7ac).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
hugr-core/src/import.rs 70.15% 122 Missing and 109 partials ⚠️
hugr-model/src/v0/text/print.rs 59.32% 116 Missing and 76 partials ⚠️
hugr-model/src/v0/text/parse.rs 65.89% 70 Missing and 92 partials ⚠️
hugr-core/src/export.rs 82.55% 67 Missing and 19 partials ⚠️
hugr-model/src/v0/mod.rs 42.10% 33 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1542      +/-   ##
==========================================
- Coverage   87.43%   85.71%   -1.72%     
==========================================
  Files         127      132       +5     
  Lines       21740    24017    +2277     
  Branches    18740    21017    +2277     
==========================================
+ Hits        19008    20587    +1579     
- Misses       1964     2366     +402     
- Partials      768     1064     +296     
Flag Coverage Δ
python 92.46% <ø> (ø)
rust 84.75% <69.09%> (-1.88%) ⬇️

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.

@zrho zrho marked this pull request as ready for review October 3, 2024 09:00
@zrho zrho requested review from aborgna-q, cqc-alec, ss2165 and a team as code owners October 3, 2024 09:00
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

first review of mod.rs

hugr-model/src/v0/mod.rs Outdated Show resolved Hide resolved
hugr-model/src/v0/mod.rs Show resolved Hide resolved
hugr-model/src/v0/mod.rs Outdated Show resolved Hide resolved
hugr-model/src/v0/mod.rs Show resolved Hide resolved
hugr-model/src/v0/mod.rs Outdated Show resolved Hide resolved
hugr-model/src/v0/mod.rs Show resolved Hide resolved
hugr-model/src/v0/mod.rs Show resolved Hide resolved
hugr-core/tests/model.rs Outdated Show resolved Hide resolved
hugr-model/src/v0/mod.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

Some general comments, I'll do mod/export next.

Other tidbits:

  • Add hugr-model to .github/change-filters.yml

Cargo.toml Outdated Show resolved Hide resolved
hugr-core/src/lib.rs Outdated Show resolved Hide resolved
hugr-model/Cargo.toml Show resolved Hide resolved
hugr-model/README.md Outdated Show resolved Hide resolved
hugr-model/README.md Outdated Show resolved Hide resolved
hugr-model/src/v0/mod.rs Outdated Show resolved Hide resolved
zrho and others added 3 commits October 3, 2024 15:40
Co-authored-by: Agustín Borgna <[email protected]>
Co-authored-by: Agustín Borgna <[email protected]>
Also added hugr model to change-filters.yaml
Copy link
Collaborator

@cqc-alec cqc-alec left a comment

Choose a reason for hiding this comment

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

(Looked at parse.rs and print.rs.) LGTM, I just highlighted some TODO comments that you may or may not want to address here (if not we should make issues).

hugr-model/src/v0/text/print.rs Show resolved Hide resolved
hugr-model/src/v0/text/parse.rs Show resolved Hide resolved
hugr-model/src/v0/text/parse.rs Show resolved Hide resolved
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

import first pass, second half coming soon

hugr-core/src/import.rs Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
Copy link
Member

@ss2165 ss2165 left a comment

Choose a reason for hiding this comment

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

import round 2

hugr-core/src/import.rs Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Show resolved Hide resolved
hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Show resolved Hide resolved
Err(error_uninferred!("application with implicit parameters"))
}

model::Term::ApplyFull { name, args } => {
Copy link
Member

Choose a reason for hiding this comment

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

while I understand the rationale I find the encoding of runtime custom types as "applyfull" still confusing on first interaction. Either a rename or some comments here would help.

Copy link
Contributor Author

@zrho zrho Oct 4, 2024

Choose a reason for hiding this comment

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

I've added some docs by now. Does it become clearer?

hugr-core/src/import.rs Outdated Show resolved Hide resolved
hugr-core/src/import.rs Show resolved Hide resolved
hugr-core/src/import.rs Show resolved Hide resolved
Co-authored-by: Seyon Sivarajah <[email protected]>
Comment on lines +42 to +43
let mut module = model::Module::default();
module.nodes.reserve(hugr.node_count());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a Module::with_capacity instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nodes, terms and regions have different capacities, so that'd be a with_capacity with quite a few parameters (all of the same type, don't mix them up!).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then maybe a reserve_nodes method instead, that can be documented instead of relying on knowing how the Model works internally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There isn't really an internally. It's plain old data. Everything is pub.

hugr-core/src/export.rs Outdated Show resolved Hide resolved
hugr-core/src/export.rs Show resolved Hide resolved
hugr-core/src/export.rs Outdated Show resolved Hide resolved
}
}

pub fn export_root(&mut self) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the logic for the root module of the Hugr different from the node's?

This seems to assume the hugr root is a Module.
As far as I understand, passing a DFG-rotted hugr here will fail when calling self.export_node(child) on the input / output nodes.

Some root Optypes like Case and ExitBlock should be detected early and rejected here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The core root module node is exported not as a node but as a region. I was not aware that there can be Hugrs whose roots are not Modules...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I propose we postpone this: #1554

hugr-core/src/export.rs Show resolved Hide resolved
hugr-core/src/export.rs Show resolved Hide resolved
hugr-core/src/export.rs Show resolved Hide resolved
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.

5 participants