[Core] Cache InductorPass.hash_source with functools.cache (#39328)
Signed-off-by: Frederik Gossen <frgossen@meta.com>
This commit is contained in:
@@ -51,6 +51,15 @@ def pass_context(compile_range: Range) -> Generator[None, None, None]:
|
|||||||
_pass_context = prev_context
|
_pass_context = prev_context
|
||||||
|
|
||||||
|
|
||||||
|
@functools.cache
|
||||||
|
def _hash_source_cached(*srcs: str | type | types.FunctionType) -> str:
|
||||||
|
hasher = hashlib.sha256()
|
||||||
|
for src in srcs:
|
||||||
|
src_str = src if isinstance(src, str) else inspect.getsource(src)
|
||||||
|
hasher.update(src_str.encode("utf-8"))
|
||||||
|
return hasher.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
class InductorPass(CustomGraphPass): # type: ignore[misc]
|
class InductorPass(CustomGraphPass): # type: ignore[misc]
|
||||||
"""
|
"""
|
||||||
A custom graph pass that uses a hash of its source as the UUID.
|
A custom graph pass that uses a hash of its source as the UUID.
|
||||||
@@ -72,19 +81,16 @@ class InductorPass(CustomGraphPass): # type: ignore[misc]
|
|||||||
Utility method to hash the sources of functions or objects.
|
Utility method to hash the sources of functions or objects.
|
||||||
:param srcs: strings or objects to add to the hash.
|
:param srcs: strings or objects to add to the hash.
|
||||||
Objects and functions have their source inspected.
|
Objects and functions have their source inspected.
|
||||||
|
Results are cached by resolved types to avoid repeated
|
||||||
|
inspect.getsource() calls.
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
hasher = hashlib.sha256()
|
# Resolve instances to their class for a hashable cache key.
|
||||||
for src in srcs:
|
cache_key = tuple(
|
||||||
if isinstance(src, str):
|
src if isinstance(src, (str, type, types.FunctionType)) else src.__class__
|
||||||
src_str = src
|
for src in srcs
|
||||||
elif isinstance(src, (types.FunctionType, type)):
|
)
|
||||||
src_str = inspect.getsource(src)
|
return _hash_source_cached(*cache_key)
|
||||||
else:
|
|
||||||
# object instance
|
|
||||||
src_str = inspect.getsource(src.__class__)
|
|
||||||
hasher.update(src_str.encode("utf-8"))
|
|
||||||
return hasher.hexdigest()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def hash_dict(dict_: dict[Any, Any]) -> str:
|
def hash_dict(dict_: dict[Any, Any]) -> str:
|
||||||
|
|||||||
Reference in New Issue
Block a user