"""Hook to save the compiled bytecode for direct execution."""
"""Hook to save the compiled bytecode for direct execution."""
ifold_codeisnotself.original_code_object:
ifold_codeisnotself.original_code_object():
return
return
# code borrowed from https://github.com/thuml/depyf/blob/f4ad79fadee27ea113b4c75202db1eb1a11c0dbc/depyf/explain/enable_debugging.py#L25
# code borrowed from https://github.com/thuml/depyf/blob/f4ad79fadee27ea113b4c75202db1eb1a11c0dbc/depyf/explain/enable_debugging.py#L25
frame=sys._getframe()
frame=sys._getframe()
...
@@ -114,7 +179,7 @@ class TorchCompileWrapperWithCustomDispatcher:
...
@@ -114,7 +179,7 @@ class TorchCompileWrapperWithCustomDispatcher:
ifframe.f_locals["self"]isnotself:
ifframe.f_locals["self"]isnotself:
return
return
self.compiled_codes.append(new_code)
self._compiled_bytecode=new_code
path=self.vllm_config.compile_debug_dump_path()
path=self.vllm_config.compile_debug_dump_path()
ifpath:
ifpath:
...
@@ -153,16 +218,21 @@ class TorchCompileWrapperWithCustomDispatcher:
...
@@ -153,16 +218,21 @@ class TorchCompileWrapperWithCustomDispatcher:
raiseRuntimeError(msg)
raiseRuntimeError(msg)
@contextmanager
@contextmanager
defdispatch_to_code(self,index:int):
def_dispatch_to_compiled_code(self):
"""Context manager to dispatch to the compiled code.
# noqa: E501
"""
Context manager to dispatch to internally compiled code for torch<2.8.
Why does this work? Because Dynamo guarantees that the compiled
Why does this work? Because Dynamo guarantees that the compiled
bytecode has exactly the same arguments, cell variables, and free
bytecode has exactly the same arguments, cell variables, and free
variables as the original code. Therefore we can directly switch
variables as the original code. Therefore we can directly switch
the code object in the function and call it.
the code object in the function and call it.
See https://dev-discuss.pytorch.org/t/what-is-the-relationship-requirement-among-original-bytecode-transformed-bytecode-and-bytecode-returned-by-hooks-in-dynamo/1693/7
See https://dev-discuss.pytorch.org/t/what-is-the-relationship-requirement-among-original-bytecode-transformed-bytecode-and-bytecode-returned-by-hooks-in-dynamo/1693/7 for more details.