[inductor] add FreeLibrary to DLLWrapper for Windows. (#133184)

For previous PR https://github.com/pytorch/pytorch/pull/132630 . We found `DLLWrapper` class doesn't have `_dlclose` implemention for Windows.

I write a small test project to figure out how to make it works on Windows: https://github.com/xuhancn/ctypes_all_lifecycle/blob/main/pysrc/module_manage.py#L30-L61
Test result: https://github.com/xuhancn/ctypes_all_lifecycle/tree/main?tab=readme-ov-file#ctypes_cyclepy

So, I have port the Windows FreeLibrary implemention to pytorch DLLWrapper in this PR.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/133184
Approved by: https://github.com/jgong5, https://github.com/jansel
This commit is contained in:
Xu Han 2024-08-12 19:55:48 +00:00 committed by PyTorch MergeBot
parent cdcc7dc891
commit 36c4ed8e49
2 changed files with 20 additions and 2 deletions

View File

@ -100,6 +100,7 @@ from torch._inductor.utils import (
BoxedBool,
clear_on_fresh_inductor_cache,
is_linux,
is_windows,
set_tracing_context_output_strides,
)
from torch._logging import trace_structured
@ -3017,12 +3018,25 @@ class DLLWrapper:
if hasattr(syms, "dlclose"):
f_dlclose = syms.dlclose
elif is_windows():
import ctypes
kernel32 = ctypes.CDLL("kernel32", use_last_error=True)
f_dlclose = kernel32.FreeLibrary
else:
raise NotImplementedError("Unsupported env, failed to do dlclose!")
if f_dlclose is not None:
f_dlclose.argtypes = [c_void_p]
f_dlclose(self.DLL._handle)
if is_linux():
f_dlclose.argtypes = [c_void_p]
f_dlclose(self.DLL._handle)
elif is_windows():
import ctypes
from ctypes import wintypes
f_dlclose.argtypes = [wintypes.HMODULE]
f_dlclose(self.DLL._handle)
else:
log.warning(
"dll unloading function was not found, library may not be unloaded properly!"

View File

@ -1521,6 +1521,10 @@ def is_linux() -> bool:
return platform.system() == "Linux"
def is_windows():
return sys.platform == "win32"
def has_free_symbols(itr: Iterable[Any]):
return any(isinstance(x, sympy.Expr) and not x.is_number for x in itr)