MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / PythonGCManager

Class PythonGCManager

deepspeed/runtime/python_gc.py:13–72  ·  view source on GitHub ↗

Coordinate process-wide GC state across multiple DeepSpeed engines.

Source from the content-addressed store, hash-verified

11
12
13class PythonGCManager:
14 """Coordinate process-wide GC state across multiple DeepSpeed engines."""
15
16 def __init__(self):
17 self._lock = RLock()
18 self._active_engines = 0
19 self._restore_enabled = False
20 self._generation = 0
21 if hasattr(os, "register_at_fork"):
22 os.register_at_fork(before=self._before_fork,
23 after_in_parent=self._after_fork_parent,
24 after_in_child=self._after_fork_child)
25
26 def _before_fork(self):
27 self._lock.acquire()
28
29 def _after_fork_parent(self):
30 self._lock.release()
31
32 def _after_fork_child(self):
33 if self._active_engines > 0 and self._restore_enabled and not gc.isenabled():
34 gc.enable()
35 self._active_engines = 0
36 self._restore_enabled = False
37 self._generation += 1
38 self._lock = RLock()
39
40 def acquire(self):
41 with self._lock:
42 if self._active_engines == 0:
43 self._restore_enabled = gc.isenabled()
44 if self._restore_enabled:
45 collected = gc.collect()
46 gc.disable()
47 logger.info("Disabled automatic Python cyclic GC after collecting %d objects", collected)
48 self._active_engines += 1
49 return self._generation
50
51 def release(self, generation):
52 with self._lock:
53 if generation != self._generation:
54 return
55 if self._active_engines == 0:
56 return
57 self._active_engines -= 1
58 restore_enabled = self._active_engines == 0 and self._restore_enabled
59 if self._active_engines == 0:
60 self._restore_enabled = False
61 if restore_enabled and not gc.isenabled():
62 gc.enable()
63 logger.info("Restored automatic Python cyclic GC")
64
65 def collect(self):
66 """Run an explicit collection without changing the automatic-GC policy."""
67 return gc.collect()
68
69 @property
70 def active_engines(self):

Calls

no outgoing calls