-
Notifications
You must be signed in to change notification settings - Fork 16
Add node type counter #489
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 3 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 |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ | |
| """ | ||
|
|
||
| from typing import (Mapping, Dict, Union, Set, Tuple, Any, FrozenSet, | ||
| TYPE_CHECKING) | ||
| Type, TYPE_CHECKING) | ||
| from pytato.array import (Array, IndexLambda, Stack, Concatenate, Einsum, | ||
| DictOfNamedArrays, NamedArray, | ||
| IndexBase, IndexRemappingBase, InputArgumentBase, | ||
|
|
@@ -413,6 +413,49 @@ def get_num_nodes(outputs: Union[Array, DictOfNamedArrays]) -> int: | |
| # }}} | ||
|
|
||
|
|
||
| # {{{ NodeTypeCountMapper | ||
|
|
||
| @optimize_mapper(drop_args=True, drop_kwargs=True, inline_get_cache_key=True) | ||
| class NodeTypeCountMapper(CachedWalkMapper): | ||
| """ | ||
| Counts the number of nodes of a given type in a DAG. | ||
|
|
||
| .. attribute:: counts | ||
|
|
||
| Dictionary mapping node types to number of nodes of that type. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| from collections import defaultdict | ||
| super().__init__() | ||
| self.counts = defaultdict(int) | ||
|
|
||
| def get_cache_key(self, expr: ArrayOrNames) -> int: | ||
| return id(expr) | ||
|
|
||
| def post_visit(self, expr: Any) -> None: | ||
| if type(expr) not in self.counts: | ||
| self.counts[type(expr)] = 0 | ||
|
MTCam marked this conversation as resolved.
Outdated
|
||
| self.counts[type(expr)] += 1 | ||
|
|
||
|
|
||
| def get_num_node_types(outputs: Union[Array, DictOfNamedArrays]) -> Dict[Type, int]: | ||
|
Collaborator
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.
Author
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. Agreed. |
||
| """ | ||
| Returns a dictionary mapping node types to node count for that type | ||
| in DAG *outputs*. | ||
| """ | ||
|
|
||
| from pytato.codegen import normalize_outputs | ||
| outputs = normalize_outputs(outputs) | ||
|
|
||
| ncm = NodeTypeCountMapper() | ||
| ncm(outputs) | ||
|
|
||
| return ncm.counts | ||
|
|
||
| # }}} | ||
|
|
||
|
|
||
| # {{{ CallSiteCountMapper | ||
|
|
||
| @optimize_mapper(drop_args=True, drop_kwargs=True, inline_get_cache_key=True) | ||
|
|
||
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.
Could maybe just modify
NodeCountMapperto do this and then haveget_num_nodesreturnsum(ncm.counts.values())? Saves some duplication.