-
Notifications
You must be signed in to change notification settings - Fork 16
Call BranchMorpher after dw deduplication #331
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -73,6 +73,7 @@ | |||||
.. autoclass:: CachedWalkMapper | ||||||
.. autoclass:: TopoSortMapper | ||||||
.. autoclass:: CachedMapAndCopyMapper | ||||||
.. autoclass:: PostMapEqualNodeReuser | ||||||
.. autofunction:: copy_dict_of_named_arrays | ||||||
.. autofunction:: get_dependencies | ||||||
.. autofunction:: map_and_copy | ||||||
|
@@ -1569,6 +1570,48 @@ def tag_user_nodes( | |||||
# }}} | ||||||
|
||||||
|
||||||
# {{{ PostMapEqualNodeReuser | ||||||
|
||||||
class PostMapEqualNodeReuser(CopyMapper): | ||||||
""" | ||||||
A mapper that reuses the same object instances for equal segments of | ||||||
graphs. | ||||||
|
||||||
.. note:: | ||||||
|
||||||
The operation performed here is equivalent to that of a | ||||||
:class:`CopyMapper`, in that both return a single instance for equal | ||||||
:class:`pytato.Array` nodes. However, they differ at the point where | ||||||
two array expressions are compared. :class:`CopyMapper` compares array | ||||||
expressions before the expressions are mapped i.e. repeatedly comparing | ||||||
equal array expressions but unequal instances, and because of this it | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
spends super-linear time in comparing array expressions. On the other | ||||||
hand, :class:`PostMapEqualNodeReuser` has linear complexity in the | ||||||
number of nodes in the number of array expressions as the larger mapped | ||||||
expressions already contain same instances for the predecessors, | ||||||
resulting in a cheaper equality comparison overall. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what you want to say instead here is that this avoids building potentially large repeated subexpexpressions in the first place, rather than having to (expensively) merge them after the fact. |
||||||
""" | ||||||
def __init__(self) -> None: | ||||||
super().__init__() | ||||||
self.result_cache: Dict[ArrayOrNames, ArrayOrNames] = {} | ||||||
|
||||||
def cache_key(self, expr: CachedMapperT) -> Any: | ||||||
return (id(expr), expr) | ||||||
|
||||||
# type-ignore reason: incompatible with Mapper.rec | ||||||
def rec(self, expr: MappedT) -> MappedT: # type: ignore[override] | ||||||
rec_expr = super().rec(expr) | ||||||
try: | ||||||
# type-ignored because 'result_cache' maps to ArrayOrNames | ||||||
return self.result_cache[rec_expr] # type: ignore[return-value] | ||||||
except KeyError: | ||||||
self.result_cache[rec_expr] = rec_expr | ||||||
# type-ignored because of super-class' relaxed types | ||||||
return rec_expr | ||||||
|
||||||
# }}} | ||||||
|
||||||
|
||||||
# {{{ deduplicate_data_wrappers | ||||||
|
||||||
def _get_data_dedup_cache_key(ary: DataInterface) -> Hashable: | ||||||
|
@@ -1658,8 +1701,11 @@ def cached_data_wrapper_if_present(ary: ArrayOrNames) -> ArrayOrNames: | |||||
len(data_wrapper_cache), | ||||||
data_wrappers_encountered - len(data_wrapper_cache)) | ||||||
|
||||||
return array_or_names | ||||||
# many paths in the DAG might be semantically equivalent after DWs are | ||||||
# deduplicated => morph them | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still: "morph" isn't good terminology. |
||||||
return PostMapEqualNodeReuser()(array_or_names) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, wait. If it's used like this, I would argue that that's equivalent to just using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||||||
|
||||||
# }}} | ||||||
|
||||||
|
||||||
# vim: foldmethod=marker |
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.