Skip to content

Commit 8bf73a5

Browse files
kaushikcfdinducer
authored andcommitted
move _ary_container_key_stringifier to utils.py
1 parent 9399fb7 commit 8bf73a5

4 files changed

Lines changed: 31 additions & 26 deletions

File tree

arraycontext/impl/pytato/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,9 @@ def freeze(self, array):
436436
from arraycontext.container.traversal import rec_keyed_map_array_container
437437
from arraycontext.impl.pyopencl.taggable_cl_array import (
438438
TaggableCLArray, to_tagged_cl_array)
439-
from arraycontext.impl.pytato.compile import _ary_container_key_stringifier
440439
from arraycontext.impl.pytato.utils import (
441-
_normalize_pt_expr, get_cl_axes_from_pt_axes)
440+
_ary_container_key_stringifier, _normalize_pt_expr,
441+
get_cl_axes_from_pt_axes)
442442

443443
array_as_dict: Dict[str, Union[cla.Array, TaggableCLArray, pt.Array]] = {}
444444
key_to_frozen_subary: Dict[str, TaggableCLArray] = {}
@@ -775,7 +775,7 @@ def freeze(self, array):
775775
import pytato as pt
776776

777777
from arraycontext.container.traversal import rec_keyed_map_array_container
778-
from arraycontext.impl.pytato.compile import _ary_container_key_stringifier
778+
from arraycontext.impl.pytato.utils import _ary_container_key_stringifier
779779

780780
array_as_dict: Dict[str, Union[jnp.ndarray, pt.Array]] = {}
781781
key_to_frozen_subary: Dict[str, jnp.ndarray] = {}

arraycontext/impl/pytato/compile.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,6 @@ class LeafArrayDescriptor(AbstractInputDescriptor):
107107

108108
# {{{ utilities
109109

110-
def _ary_container_key_stringifier(keys: Tuple[Any, ...]) -> str:
111-
"""
112-
Helper for :meth:`BaseLazilyCompilingFunctionCaller.__call__`. Stringifies an
113-
array-container's component's key. Goals of this routine:
114-
115-
* No two different keys should have the same stringification
116-
* Stringified key must a valid identifier according to :meth:`str.isidentifier`
117-
* (informal) Shorter identifiers are preferred
118-
"""
119-
def _rec_str(key: Any) -> str:
120-
if isinstance(key, (str, int)):
121-
return str(key)
122-
elif isinstance(key, tuple):
123-
# t in '_actx_t': stands for tuple
124-
return "_actx_t" + "_".join(_rec_str(k) for k in key) + "_actx_endt"
125-
else:
126-
raise NotImplementedError("Key-stringication unimplemented for "
127-
f"'{type(key).__name__}'.")
128-
129-
return "_".join(_rec_str(key) for key in keys)
130-
131-
132110
def _get_arg_id_to_arg_and_arg_id_to_descr(args: Tuple[Any, ...],
133111
kwargs: Mapping[str, Any]
134112
) -> "Tuple[PMap[Tuple[Any, ...],\
@@ -318,6 +296,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
318296
:attr:`~BaseLazilyCompilingFunctionCaller.f` with *args* in a lazy-sense.
319297
The intermediary pytato DAG for *args* is memoized in *self*.
320298
"""
299+
from arraycontext.impl.pytato.utils import _ary_container_key_stringifier
321300
arg_id_to_arg, arg_id_to_descr = _get_arg_id_to_arg_and_arg_id_to_descr(
322301
args, kwargs)
323302

arraycontext/impl/pytato/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,30 @@ def get_loopy_target(self) -> Optional["lp.PyOpenCLTarget"]:
117117

118118
# }}}
119119

120+
121+
# {{{ compile/outline helpers
122+
123+
def _ary_container_key_stringifier(keys: Tuple[Any, ...]) -> str:
124+
"""
125+
Helper for :meth:`BaseLazilyCompilingFunctionCaller.__call__`. Stringifies an
126+
array-container's component's key. Goals of this routine:
127+
128+
* No two different keys should have the same stringification
129+
* Stringified key must a valid identifier according to :meth:`str.isidentifier`
130+
* (informal) Shorter identifiers are preferred
131+
"""
132+
def _rec_str(key: Any) -> str:
133+
if isinstance(key, (str, int)):
134+
return str(key)
135+
elif isinstance(key, tuple):
136+
# t in '_actx_t': stands for tuple
137+
return "_actx_t" + "_".join(_rec_str(k) for k in key) + "_actx_endt"
138+
else:
139+
raise NotImplementedError("Key-stringication unimplemented for "
140+
f"'{type(key).__name__}'.")
141+
142+
return "_".join(_rec_str(key) for key in keys)
143+
144+
# }}}
145+
120146
# vim: foldmethod=marker

test/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
# {{{ test_pt_actx_key_stringification_uniqueness
3535

3636
def test_pt_actx_key_stringification_uniqueness():
37-
from arraycontext.impl.pytato.compile import _ary_container_key_stringifier
37+
from arraycontext.impl.pytato.utils import _ary_container_key_stringifier
3838

3939
assert (_ary_container_key_stringifier(((3, 2), 3))
4040
!= _ary_container_key_stringifier((3, (2, 3))))

0 commit comments

Comments
 (0)