A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 4, 22:30 UTC
[Core] Cache InductorPass.hash_source with functools.cache (#39328)
Signed-off-by: Frederik Gossen <frgossen@meta.com>
Frederik Gossen committed
Apr 20, 2026 at 14:06 UTC
87805fa11ea6d9f9aa7a7fc92ac957c613bc2163
1 file changed
+17
-11
vllm/compilation/passes/inductor_pass.py
+17
-11
@@ -51,6 +51,15 @@ def pass_context(compile_range: Range) -> Generator[None, None, None]:
51
_pass_context = prev_context
52
53
54
+@functools.cache
55
+def _hash_source_cached(*srcs: str | type | types.FunctionType) -> str:
56
+ hasher = hashlib.sha256()
57
+ for src in srcs:
58
+ src_str = src if isinstance(src, str) else inspect.getsource(src)
59
+ hasher.update(src_str.encode("utf-8"))
60
+ return hasher.hexdigest()
61
+
62
+
63
class InductorPass(CustomGraphPass): # type: ignore[misc]
64
"""
65
A custom graph pass that uses a hash of its source as the UUID.
@@ -72,19 +81,16 @@ class InductorPass(CustomGraphPass): # type: ignore[misc]
81
Utility method to hash the sources of functions or objects.
82
:param srcs: strings or objects to add to the hash.
83
Objects and functions have their source inspected.
84
+ Results are cached by resolved types to avoid repeated
85
+ inspect.getsource() calls.
86
:return:
87
"""
77
- hasher = hashlib.sha256()
78
- for src in srcs:
79
- if isinstance(src, str):
80
- src_str = src
81
- elif isinstance(src, (types.FunctionType, type)):
82
- src_str = inspect.getsource(src)
83
- else:
84
- # object instance
85
- src_str = inspect.getsource(src.__class__)
86
- hasher.update(src_str.encode("utf-8"))
87
- return hasher.hexdigest()
88
+ # Resolve instances to their class for a hashable cache key.
89
+ cache_key = tuple(
90
+ src if isinstance(src, (str, type, types.FunctionType)) else src.__class__
91
+ for src in srcs
92
+ )
93
+ return _hash_source_cached(*cache_key)
94
95
@staticmethod
96
def hash_dict(dict_: dict[Any, Any]) -> str: