-
Notifications
You must be signed in to change notification settings - Fork 4
[Perf] Adds numeric optimizations and algebraic rewrites #35
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| from stratum.logical_optimizer._numeric_ops import NumericOp | ||
| from stratum.logical_optimizer._op_utils import topological_iterator | ||
| from stratum.logical_optimizer._numeric_ops import NumericOpType | ||
|
|
||
| def eliminate_two_op_chain(op1, op2): | ||
| # y = f(op2(op1(x))) -> y = f(x) | ||
| x = op1.inputs[0] | ||
| if len(op2.outputs) == 1: | ||
| y = op2.outputs[0] | ||
| y.replace_input(op2, x) | ||
| x.replace_output(op1, y) | ||
| else: | ||
| x.outputs = [] | ||
|
|
||
| def algebraic_rewrites(sink): | ||
| for op1 in topological_iterator(sink): | ||
| if isinstance(op1, NumericOp): | ||
| if len(op1.outputs) == 1 and isinstance(op1.outputs[0], NumericOp): | ||
| op2 = op1.outputs[0] | ||
| type1 = op1.type | ||
| type2 = op2.type | ||
| if type1 == NumericOpType.LOG and type2 == NumericOpType.EXP or type1 == NumericOpType.EXP and type2 == NumericOpType.LOG: | ||
| # y = f(log(exp(x))) OR y = f(exp(log(x))) -> y = f(x) | ||
| eliminate_two_op_chain(op1, op2) | ||
| if op2 is sink: | ||
| sink = op1.inputs[0] | ||
| return sink | ||
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,68 @@ | ||
| from stratum.logical_optimizer._ops import CallOp, Op, ValueOp | ||
| from pandas import DataFrame | ||
| from stratum.logical_optimizer._dataframe_ops import DataSourceOp | ||
| from stratum.logical_optimizer._op_utils import topological_iterator | ||
| import numpy as np | ||
| from enum import Enum | ||
|
|
||
| class NumericOpType(Enum): | ||
| GENERIC = "generic" | ||
| LOG = "log" | ||
| EXP = "exp" | ||
|
e-strauss marked this conversation as resolved.
|
||
| class NumericOp(Op): | ||
| fields = ["func", "args", "kwargs", "type"] | ||
| func = None | ||
|
|
||
| def __init__(self, func, args, kwargs, inputs, outputs): | ||
| if func is np.log: | ||
| self.type = NumericOpType.LOG | ||
| name = "log" | ||
| elif func is np.exp: | ||
| self.type = NumericOpType.EXP | ||
| name = "exp" | ||
| else: | ||
| self.type = NumericOpType.GENERIC | ||
| self.func = func | ||
| name = func.__name__ | ||
| super().__init__(name=name, inputs=inputs, outputs=outputs) | ||
|
|
||
| self.args = args | ||
| self.kwargs = kwargs | ||
|
|
||
| def process(self, mode: str, environment: dict): | ||
| if self.type == NumericOpType.GENERIC: | ||
| self.intermediate = self.func(self.inputs[0].intermediate, *self.args, **self.kwargs) | ||
| elif self.type == NumericOpType.LOG: | ||
| self.intermediate = np.log(self.inputs[0].intermediate) | ||
| elif self.type == NumericOpType.EXP: | ||
| self.intermediate = np.exp(self.inputs[0].intermediate) | ||
| else: | ||
| raise ValueError(f"Unsupported numeric operation type: {self.type}") | ||
|
|
||
|
|
||
| def make_numeric_op(op: CallOp) -> NumericOp: | ||
| op.args = op.args[1:] | ||
| new_op = NumericOp(func=op.func, args=op.args, kwargs=op.kwargs, inputs=op.inputs, outputs=op.outputs) | ||
| return new_op | ||
|
|
||
| def to_numeric_op(sink: Op) -> Op: | ||
| """ Detect and convert the numeric ops in the dag to the stratum's NumericOps.""" | ||
| for op in topological_iterator(sink): | ||
| new_op = None | ||
| if isinstance(op, CallOp): | ||
| if op.func is np.log: | ||
| new_op = make_numeric_op(op) | ||
| elif op.func is np.exp: | ||
| new_op = make_numeric_op(op) | ||
| # if op is some other function from np package, make a generic numeric op | ||
| elif op.func.__module__ == "numpy": | ||
| new_op = make_numeric_op(op) | ||
|
|
||
|
|
||
|
|
||
| if new_op is not None: | ||
| op.replace_input_of_outputs(new_op) | ||
| op.replace_output_of_inputs(new_op) | ||
| if op is sink: | ||
| sink = new_op | ||
| return sink | ||
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
Empty file.
80 changes: 80 additions & 0 deletions
80
stratum/tests/logical_optimizer/algebraic_rewrites/test_1.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,80 @@ | ||
| import unittest | ||
| import stratum as skrub | ||
| import numpy as np | ||
| from stratum.logical_optimizer._optimize import optimize | ||
| from stratum.logical_optimizer._op_utils import topological_iterator | ||
|
|
||
| class TestCSE(unittest.TestCase): | ||
|
|
||
| def test_log_exp1(self): | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.log) | ||
| t2 = t1.skb.apply_func(np.exp) | ||
|
|
||
| out = optimize(t2) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 1) | ||
| self.assertEqual(out[0].value, 1) | ||
|
|
||
| def test_log_exp2(self): | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.log) | ||
| t2 = t1.skb.apply_func(np.exp) | ||
| t3 = t2.skb.apply_func(np.log1p) | ||
|
|
||
| out = optimize(t3) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 2) | ||
| self.assertEqual(out[0].value, 1) | ||
|
|
||
| def test_exp_log1(self): | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.exp) | ||
| t2 = t1.skb.apply_func(np.log) | ||
|
|
||
| out = optimize(t2) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 1) | ||
| self.assertEqual(out[0].value, 1) | ||
|
|
||
| def test_exp_log2(self): | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.exp) | ||
| t2 = t1.skb.apply_func(np.log) | ||
| t3 = t2.skb.apply_func(np.log1p) | ||
|
|
||
| out = optimize(t3) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 2) | ||
| self.assertEqual(out[0].value, 1) | ||
|
|
||
| def test_log_log1p(self): | ||
| "no algebraic rewrite should be applied here " | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.log) | ||
| t2 = t1.skb.apply_func(np.log1p) | ||
|
|
||
| out = optimize(t2) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 3) | ||
|
|
||
| def test_log_log1p_exp(self): | ||
| "no algebraic rewrite should be applied here " | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.log) | ||
| t2 = t1.skb.apply_func(np.log1p) | ||
| t3 = t2.skb.apply_func(np.exp) | ||
| out = optimize(t3) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 4) | ||
|
|
||
| def test_log1p_log1p_exp(self): | ||
| "no algebraic rewrite should be applied here " | ||
| df = skrub.as_data_op(1) | ||
| t1 = df.skb.apply_func(np.log1p) | ||
| t2 = t1.skb.apply_func(np.log1p) | ||
| t3 = t2.skb.apply_func(np.exp) | ||
| out = optimize(t3) | ||
| out = list(topological_iterator(out)) | ||
| self.assertEqual(len(out), 4) | ||
|
|
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,24 @@ | ||
| import unittest | ||
| import pandas as pd | ||
| import stratum as skrub | ||
| import numpy as np | ||
| from sklearn.dummy import DummyRegressor | ||
|
|
||
| class TestNumericOps(unittest.TestCase): | ||
| def setUp(self): | ||
| self.df = pd.DataFrame({ | ||
| "x": [1, 2, 3], | ||
| "y": [4, 5, 6], | ||
| }) | ||
|
|
||
| def test_to_numeric_op1(self): | ||
| data = skrub.as_data_op(self.df) | ||
| X = data[["x"]].skb.mark_as_X() | ||
| y = data["y"].skb.mark_as_y() | ||
| t1 = X.skb.apply_func(np.log) | ||
| t2 = t1.skb.apply_func(np.log1p) | ||
| y_exp = y.skb.apply_func(np.exp) | ||
| pred = t2.skb.apply(DummyRegressor(), y=y_exp) | ||
|
|
||
| with skrub.config(scheduler=True): | ||
| pred.skb.make_grid_search(cv=3) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.