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: Add array repeat and scan ops #1633

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

Conversation

mark-koch
Copy link
Contributor

Closes #1627

@mark-koch mark-koch requested a review from a team as a code owner November 5, 2024 14:38
Copy link

codecov bot commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 95.47739% with 9 lines in your changes missing coverage. Please review.

Project coverage is 85.59%. Comparing base (e63878f) to head (65225e0).

Files with missing lines Patch % Lines
hugr-core/src/extension/prelude/array.rs 95.47% 6 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1633      +/-   ##
==========================================
+ Coverage   85.51%   85.59%   +0.07%     
==========================================
  Files         136      136              
  Lines       25264    25463     +199     
  Branches    22176    22375     +199     
==========================================
+ Hits        21605    21794     +189     
- Misses       2455     2462       +7     
- Partials     1204     1207       +3     
Flag Coverage Δ
python 92.42% <ø> (ø)
rust 84.64% <95.47%> (+0.09%) ⬆️

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.

@@ -179,6 +184,10 @@ impl MakeOpDef for ArrayOpDef {
fn description(&self) -> String {
match self {
ArrayOpDef::new_array => "Create a new array from elements",
ArrayOpDef::new_uninitialized_array => {
"Create a new array with uninitialised elements. \
Reading such an unitialised element is undefined behaviour."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really want to have undefined behaviour in hugr? Perhaps it should panic instead (though that would require storing a "poison" value I suppose).

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 think guaranteeing a panic would be too much overhead. An alternative would be to pass a default value to the op and renaming it to array_repeat or something like that.

For linear arrays, this default would need to be provided via a function that is called for each element

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah that could work. May add some runtime overhead (constructing values that are then replaced), but at least some of that could be eliminated by compiler optimizations.

@mark-koch mark-koch changed the title feat: Add op to allocate uninitialized arrays feat: Add op to initialise an array using a function Nov 11, 2024
@mark-koch mark-koch changed the title feat: Add op to initialise an array using a function feat: Add array repeat and scan ops Nov 14, 2024
"scan": {
"extension": "prelude",
"name": "scan",
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed trough from left to right. Returns the resulting array and the final state of the accumulator.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed trough from left to right. Returns the resulting array and the final state of the accumulator.",
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed through from start to finish. Returns the resulting array and the final state of the accumulator.",

"scan": {
"extension": "prelude",
"name": "scan",
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed trough from left to right. Returns the resulting array and the final state of the accumulator.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed trough from left to right. Returns the resulting array and the final state of the accumulator.",
"description": "A combination of map and foldl. Applies a function to each element of the array with an accumulator that is passed through from start to finish. Returns the resulting array and the final state of the accumulator.",

Comment on lines +410 to +411
of the array with an accumulator that is passed trough from left to \
right. Returns the resulting array and the final state of the \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
of the array with an accumulator that is passed trough from left to \
right. Returns the resulting array and the final state of the \
of the array with an accumulator that is passed through from start to \
finish. Returns the resulting array and the final state of the \

.into()
}

/// Add an operation implemented as an [MakeOpDef], which can provide the data
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Add an operation implemented as an [MakeOpDef], which can provide the data
/// Add an operation implemented as a [MakeOpDef], which can provide the data

/// required to define an [OpDef], to an extension.
//
// This method is re-defined here since we need to pass the array type def while
// computing the signature, to avoid recursive loops initializing the extension.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// computing the signature, to avoid recursive loops initializing the extension.
// computing the signature, to avoid recursive loops initializing the extension.

/// The target element type of the output array.
tgt_ty: Type,
/// The accumulator types.
acc_tys: Vec<Type>,
Copy link
Collaborator

@cqc-alec cqc-alec Nov 15, 2024

Choose a reason for hiding this comment

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

It's not clear to me why this is a vector of types and not just a type -- isn't the accumulator a single type?

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.

Add op to initialise an array using a function
3 participants