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

Global DAG extraction #21

Merged
merged 8 commits into from
Dec 13, 2023
Merged

Global DAG extraction #21

merged 8 commits into from
Dec 13, 2023

Conversation

oflatt
Copy link
Member

@oflatt oflatt commented Dec 5, 2023

This PR adds another greedy DAG extractor which improves the performance of calculating a term's cost.
It does this by sharing subterms in a global DAG, and exiting early when doing set unions.

cumulative time for bottom-up: 2474ms
cumulative time for global-greedy-dag: 4239ms
cumulative time for greedy-dag:            35011ms
cumulative dag cost for global-greedy-dag: 76907
cumulative dag cost for greedy-dag:             76907


extractors = sorted(set(j["extractor"] for j in js))

for i in range(len(extractors)):
Copy link
Member Author

Choose a reason for hiding this comment

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

This PR also makes the plot output data for each combination of extractors.
This creates a lot of output- maybe we don't want this change, but I find it helpful.

Copy link
Contributor

@ezrosent ezrosent left a comment

Choose a reason for hiding this comment

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

Mostly minor comments, but want some clarification on a few points.

(let me know if getting this in is urgent and I can still approve and we can fix later).

src/extract/global_greedy_dag.rs Outdated Show resolved Hide resolved
src/extract/global_greedy_dag.rs Outdated Show resolved Hide resolved
size: 1,
});
self.hash_cons.insert(term, next_id);
Some(next_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

optional:

I think when the if/else are this long it can help to unnest after the "easy case", namely:

if children.is_empty() {
...
  return Some(next_id);
}
// check if children contains this node
for child in &children {
...

Copy link
Member Author

@oflatt oflatt Dec 11, 2023

Choose a reason for hiding this comment

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

eh, I like nesting
return considered harmful (only half joking)

Copy link
Contributor

Choose a reason for hiding this comment

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

Up to you! I suggested this because there's already a good amount of early-return in this function (78, 92, 99, for example).

I personally think it is good sometimes, and not good other times. For code like this that feels pretty imperative either way I think it can make the code clearer. Feel free to keep as is though

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah I see
re-examining my head it seems that early return from a big code block makes me sad

self.hash_cons.insert(term, next_id);
Some(next_id)
} else {
// check if children contains this node
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain a bit why cycle detection here is "sound": afaik the standard greedy-dag/bottom-up doesn't have this check (does it need it? was it added later?)

Copy link
Member Author

Choose a reason for hiding this comment

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

This cycle detection is sound because reachable is the set of reachable eclasses from this term. We won't pick any e-nodes that rely on extracting this e-class.
The greedy-dag algorithm does have cycle detection on line 37

.unwrap();

let mut cost = node_cost + self.total_cost(children[biggest_child]);
let mut reachable = Box::new(self.info[children[biggest_child]].reachable.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why Box?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is my hack to keep track of a persistent set which we are "mutating" during recursive calls. We can't mutate the container itself, so we need a wrapper as far as I know.
Is there a preferred solution in rust?

Alternatively, the recursive function could also return the new set each time.

Copy link
Contributor

Choose a reason for hiding this comment

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

  • I'm not against having subcalls return a new set.
  • I still don't understand why you need a box here. Why can't you pass a &mut Reachable and then reassign as you are doing now?

Copy link
Member Author

Choose a reason for hiding this comment

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

Wow TIL that &mut literally means pass by reference. I thought &mut was a mutable reference but you couldn't assign to it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the code

src/extract/global_greedy_dag.rs Outdated Show resolved Hide resolved
// Also return the cost of the new nodes.
fn get_cost(&self, shared: &mut Box<Reachable>, id: TermId) -> NotNan<f64> {
let eclass = self.info[id].eclass.clone();
if shared.contains(&eclass) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is the key "why" for how this is better than greedy-dag: instead of doing an $O(n)$ union operation when you merge reachable sets, we "stop early" based on the shared egraph-level structure: if two nodes share a child somewhere down we don't continue unioning because we know the sets are the same.

Is that right? If so, I think it's worth a comment :) (If not, I'd definitely appreciate a comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly right- I left a comment
// This is the key to why this algorithm is faster than greedy_dag.
// While doing the set union between reachable sets, we can stop early
// if we find a shared term.
// Since the term with id is shared, the reachable set of id will already
// be in shared.

@oflatt oflatt requested a review from ezrosent December 11, 2023 23:45
Copy link
Contributor

@ezrosent ezrosent left a comment

Choose a reason for hiding this comment

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

Thanks! Left some follow-ups but feel free to resolve as you see fit.

size: 1,
});
self.hash_cons.insert(term, next_id);
Some(next_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

Up to you! I suggested this because there's already a good amount of early-return in this function (78, 92, 99, for example).

I personally think it is good sometimes, and not good other times. For code like this that feels pretty imperative either way I think it can make the code clearer. Feel free to keep as is though

.unwrap();

let mut cost = node_cost + self.total_cost(children[biggest_child]);
let mut reachable = Box::new(self.info[children[biggest_child]].reachable.clone());
Copy link
Contributor

Choose a reason for hiding this comment

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

  • I'm not against having subcalls return a new set.
  • I still don't understand why you need a box here. Why can't you pass a &mut Reachable and then reassign as you are doing now?

@oflatt oflatt merged commit 934bab7 into egraphs-good:main Dec 13, 2023
2 checks passed
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