torch-memory-saver 0.0.6__cp39-abi3-manylinux2014_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ import ctypes
2
+ import logging
3
+ import os
4
+ from contextlib import contextmanager
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import Optional
8
+
9
+ import torch
10
+
11
+ logger = logging.getLogger(__name__)
12
+
13
+
14
+ class TorchMemorySaver:
15
+ def __init__(self):
16
+ self._mem_pool = None
17
+ self._id = _global_info.next_id()
18
+ assert self._id == 1, 'Only support one single instance yet (multi-instance will be implemented later)'
19
+
20
+ @contextmanager
21
+ def region(self):
22
+ if _global_info.binary_info.enabled:
23
+ self._ensure_mem_pool()
24
+ with torch.cuda.use_mem_pool(self._mem_pool):
25
+ _global_info.binary_info.cdll.tms_region_enter()
26
+ try:
27
+ yield
28
+ finally:
29
+ _global_info.binary_info.cdll.tms_region_leave()
30
+ else:
31
+ yield
32
+
33
+ def pause(self):
34
+ if _global_info.binary_info.enabled:
35
+ _global_info.binary_info.cdll.tms_pause()
36
+
37
+ def resume(self):
38
+ if _global_info.binary_info.enabled:
39
+ _global_info.binary_info.cdll.tms_resume()
40
+
41
+ @property
42
+ def enabled(self):
43
+ return _global_info.binary_info.enabled
44
+
45
+ def _ensure_mem_pool(self):
46
+ if self._mem_pool is None:
47
+ self._mem_pool = torch.cuda.MemPool()
48
+
49
+
50
+ @dataclass
51
+ class _BinaryInfo:
52
+ cdll: Optional[ctypes.CDLL]
53
+
54
+ @property
55
+ def enabled(self):
56
+ return self.cdll is not None
57
+
58
+ @staticmethod
59
+ def compute():
60
+ env_ld_preload = os.environ.get('LD_PRELOAD', '')
61
+ if 'torch_memory_saver' in env_ld_preload:
62
+ return _BinaryInfo(cdll=ctypes.CDLL(env_ld_preload))
63
+ else:
64
+ logger.warning(
65
+ f'TorchMemorySaver is disabled for the current process because invalid LD_PRELOAD="{env_ld_preload}" (process_id={os.getpid()})')
66
+ return _BinaryInfo(cdll=None)
67
+
68
+
69
+ class _GlobalInfo:
70
+ def __init__(self):
71
+ self._binary_info: Optional[_BinaryInfo] = None
72
+ self._last_id = 0
73
+
74
+ @property
75
+ def binary_info(self):
76
+ if self._binary_info is None:
77
+ self._binary_info = _BinaryInfo.compute()
78
+ return self._binary_info
79
+
80
+ def next_id(self):
81
+ self._last_id += 1
82
+ return self._last_id
83
+
84
+
85
+ _global_info = _GlobalInfo()
86
+
87
+
88
+ def get_binary_path():
89
+ dir_package = Path(__file__).parent
90
+ candidates = [
91
+ p
92
+ for d in [dir_package, dir_package.parent]
93
+ for p in d.glob('torch_memory_saver_cpp.*.so')
94
+ ]
95
+ assert len(candidates) == 1, f'{candidates=}'
96
+ return candidates[0]
97
+
98
+
99
+ @contextmanager
100
+ def configure_subprocess():
101
+ with change_env('LD_PRELOAD', str(get_binary_path())):
102
+ yield
103
+
104
+
105
+ @contextmanager
106
+ def change_env(key: str, value: str):
107
+ old_value = os.environ.get(key, '')
108
+ os.environ[key] = value
109
+ logger.debug(f'change_env set key={key} value={value}')
110
+ try:
111
+ yield
112
+ finally:
113
+ assert os.environ[key] == value
114
+ os.environ[key] = old_value
115
+ logger.debug(f'change_env restore key={key} value={old_value}')
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 fzyzcjy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ Metadata-Version: 2.1
2
+ Name: torch_memory_saver
3
+ Version: 0.0.6
4
+ Requires-Python: >=3.9
5
+ License-File: LICENSE
6
+
@@ -0,0 +1,7 @@
1
+ torch_memory_saver_cpp.abi3.so,sha256=OCweTnvdmyg5zhUIMJjfH9NW0lYjtcNwqzS9-89cCvQ,315896
2
+ torch_memory_saver/__init__.py,sha256=B3AXwxxJeUbNFKdrfaGzXvl3vTcgPOf2UjaFVtGCZ68,3072
3
+ torch_memory_saver-0.0.6.dist-info/LICENSE,sha256=i806R5xShJFB4k9yNQJ2GYCcSBlu1frTx2vH_nWdWE8,1064
4
+ torch_memory_saver-0.0.6.dist-info/METADATA,sha256=P21LYFkCJHFwaMAkxZBoiQRkhvIQmOBAKbHHoQdQiEI,108
5
+ torch_memory_saver-0.0.6.dist-info/WHEEL,sha256=HUPiMa7ZA9BvJ9gdJRYwZIjK2rWbCcrqYvJ4Onw0owE,102
6
+ torch_memory_saver-0.0.6.dist-info/top_level.txt,sha256=uJ27-bVSKHxdcfHRcakvEr_KQxnUlMia6v19fHbfHxA,42
7
+ torch_memory_saver-0.0.6.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.0.0)
3
+ Root-Is-Purelib: false
4
+ Tag: cp39-abi3-linux_x86_64
5
+
@@ -0,0 +1,2 @@
1
+ torch_memory_saver
2
+ torch_memory_saver_cpp
Binary file