Skip to content

Commit 7f2077b

Browse files
committed
move _ary_container_key_stringifier to utils.py
1 parent 53e1663 commit 7f2077b

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] = {}
@@ -769,7 +769,7 @@ def freeze(self, array):
769769
import pytato as pt
770770

771771
from arraycontext.container.traversal import rec_keyed_map_array_container
772-
from arraycontext.impl.pytato.compile import _ary_container_key_stringifier
772+
from arraycontext.impl.pytato.utils import _ary_container_key_stringifier
773773

774774
array_as_dict: Dict[str, Union[jnp.ndarray, pt.Array]] = {}
775775
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
@@ -115,4 +115,30 @@ def get_loopy_target(self) -> Optional["lp.PyOpenCLTarget"]:
115115

116116
# }}}
117117

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