"...git@developer.sourcefind.cn:2222/OpenDAS/vllm_cscc.git" did not exist on "72fc97a0f100b92f1ff6c6a16e27d12f1c7569aa"
Unverified Commit 87805fa1 authored by Frederik Gossen's avatar Frederik Gossen Committed by GitHub
Browse files

[Core] Cache InductorPass.hash_source with functools.cache (#39328)


Signed-off-by: default avatarFrederik Gossen <frgossen@meta.com>
parent 304d5ba1
...@@ -51,6 +51,15 @@ def pass_context(compile_range: Range) -> Generator[None, None, None]: ...@@ -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] ...@@ -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:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment