-
Notifications
You must be signed in to change notification settings - Fork 4
[Fix] eliminate_two_op_chain breaks DAG when second op has multiple outputs (#70) #71
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
Open
ArthurPendragn
wants to merge
1
commit into
deem-data:main
Choose a base branch
from
ArthurPendragn:twoOpChainFix-70
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
stratum/tests/logical_optimizer/algebraic_rewrites/test_numeric_complex_fanout.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import unittest | ||
| import stratum as st | ||
| import numpy as np | ||
| from stratum.optimizer._optimize import optimize | ||
| from stratum.optimizer.ir._numeric_ops import NumericOp, NumericOpType | ||
| from stratum.optimizer.ir._ops import ValueOp | ||
|
|
||
| class TestNumericComplexFanout(unittest.TestCase): | ||
|
|
||
| def test_fanout_before_and_after_chain(self): | ||
| """ | ||
| Scenario: | ||
| a -> [log, d] | ||
| log -> [exp] | ||
| exp -> [b, c] | ||
|
|
||
| Expected after optimization: | ||
| a -> [d, b, c] | ||
| """ | ||
| a = st.as_data_op(1.0) | ||
|
|
||
| # Branch 1: log -> exp -> [b, c] | ||
| log_a = a.skb.apply_func(np.log) | ||
| exp_log_a = log_a.skb.apply_func(np.exp) | ||
| b = exp_log_a + 1.0 | ||
| c = exp_log_a + 2.0 | ||
|
|
||
| # Branch 2: d | ||
| d = a + 10.0 | ||
|
|
||
| # Root combining all branches | ||
| t1 = d + b | ||
| final = t1 + c | ||
|
|
||
| linearized_dag, *_ = optimize(final) | ||
|
|
||
| # 1. Find the original 'a' Op | ||
| a_op = linearized_dag[0] | ||
| self.assertIsInstance(a_op, ValueOp) | ||
| self.assertEqual(a_op.value, 1.0) | ||
|
|
||
| # 2. Verify 'a' has 3 outputs: d, b, and c | ||
| self.assertEqual(len(a_op.outputs), 3, f"Expected 3 outputs for 'a', found {len(a_op.outputs)}") | ||
|
|
||
| # All outputs of 'a' should be NumericOp(ADD) now (BinOp(+) is extracted to NumericOp) | ||
| for out in a_op.outputs: | ||
| self.assertIsInstance(out, NumericOp) | ||
| self.assertEqual(out.type, NumericOpType.ADD) | ||
| self.assertIn(a_op, out.inputs) | ||
|
|
||
| # 3. Check that no log or exp ops are left | ||
| for op in linearized_dag: | ||
| if isinstance(op, NumericOp): | ||
| self.assertNotIn(op.type, [NumericOpType.LOG, NumericOpType.EXP]) | ||
|
|
||
| def test_fanout_on_op2_only(self): | ||
| """ | ||
| Scenario: | ||
| a -> log -> exp -> [b, c] | ||
|
|
||
| Expected after optimization: | ||
| a -> [b, c] | ||
| """ | ||
| a = st.as_data_op(1.0) | ||
| log_a = a.skb.apply_func(np.log) | ||
| exp_log_a = log_a.skb.apply_func(np.exp) | ||
| b = exp_log_a + 1.0 | ||
| c = exp_log_a + 2.0 | ||
| final = b + c | ||
|
|
||
| linearized_dag, *_ = optimize(final) | ||
|
|
||
| a_op = linearized_dag[0] | ||
| self.assertIsInstance(a_op, ValueOp) | ||
| self.assertEqual(a_op.value, 1.0) | ||
|
|
||
| self.assertEqual(len(a_op.outputs), 2) | ||
| for out in a_op.outputs: | ||
| self.assertIsInstance(out, NumericOp) | ||
| self.assertEqual(out.type, NumericOpType.ADD) | ||
|
|
||
| for op in linearized_dag: | ||
| if isinstance(op, NumericOp): | ||
| self.assertNotIn(op.type, [NumericOpType.LOG, NumericOpType.EXP]) | ||
|
|
||
| def test_chain_is_root(self): | ||
| """ | ||
| Scenario: | ||
| a -> log -> exp (exp is the root) | ||
|
|
||
| Expected after optimization: | ||
| root is a | ||
| """ | ||
| a = st.as_data_op(1.0) | ||
| log_a = a.skb.apply_func(np.log) | ||
| exp_log_a = log_a.skb.apply_func(np.exp) | ||
|
|
||
| linearized_dag, *_ = optimize(exp_log_a) | ||
|
|
||
| a_op = linearized_dag[0] | ||
| self.assertIsInstance(a_op, ValueOp) | ||
| self.assertEqual(a_op.value, 1.0) | ||
|
|
||
| self.assertEqual(len(a_op.outputs), 0) | ||
| self.assertIs(linearized_dag[-1], a_op) | ||
|
|
||
| for op in linearized_dag: | ||
| if isinstance(op, NumericOp): | ||
| self.assertNotIn(op.type, [NumericOpType.LOG, NumericOpType.EXP]) | ||
|
|
||
| def test_chain_is_root_with_other_fanout(self): | ||
| """ | ||
| Scenario: | ||
| a -> [log, d] | ||
| log -> exp -> BinOp (root) | ||
|
|
||
| Expected after optimization: | ||
| a -> [d, combined] | ||
| d -> combined (combined = a + d is the root) | ||
| """ | ||
| a = st.as_data_op(1.0) | ||
| log_a = a.skb.apply_func(np.log) | ||
| exp_log_a = log_a.skb.apply_func(np.exp) | ||
|
|
||
| # Add another branch so 'a' has fan-out | ||
| d = a + 10.0 | ||
|
|
||
| combined = exp_log_a + d | ||
| linearized_dag, *_ = optimize(combined) | ||
|
|
||
| a_op = linearized_dag[0] | ||
| self.assertIsInstance(a_op, ValueOp) | ||
| self.assertEqual(a_op.value, 1.0) | ||
|
|
||
| # 'a' should now connect directly to the root (the BinOp from combined) | ||
| # and to 'd'. | ||
| self.assertEqual(len(a_op.outputs), 2) | ||
|
|
||
| # Verify no NumericOps remain | ||
| for op in linearized_dag: | ||
| if isinstance(op, NumericOp): | ||
| self.assertNotIn(op.type, [NumericOpType.LOG, NumericOpType.EXP]) | ||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice finding, i forgot about the util method. can we change the order of line 21 and 22?
so first eliminate op1 as output of x, than update op2 outputs (and implicit add outputs to x), since we should do the copy of the list before we append op2 outputs