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

Method destroy

deepspeed/runtime/engine.py:1348–1384  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1346 logger.debug("DeepSpeedEngine.__del__ cleanup skipped: %s", exc, exc_info=True)
1347
1348 def destroy(self):
1349 self._destroyed = True
1350 try:
1351 # DeepEP buffers ask the library not to reclaim them, so they outlive
1352 # the engine unless something releases them here. Only this engine's
1353 # own buffers: another engine in the same process still needs its own.
1354 module = getattr(self, "module", None)
1355 if module is not None:
1356 from deepspeed.module_inject.auto_ep_comm import destroy_exchanges
1357 destroy_exchanges(module)
1358
1359 self._release_deepcompile_compiled_backward_state()
1360 self._release_deepcompile_dynamo_config()
1361 optimizer = getattr(self, "optimizer", None)
1362 if optimizer is not None and hasattr(optimizer, 'destroy'):
1363 optimizer.destroy()
1364 if self.is_deepcompile_active() or getattr(self, "_deepcompile_native_initialized", False):
1365 self._deactivate_deepcompile()
1366 # Callers often keep the model after destroy() (e.g. HF Trainer), and these hooks close
1367 # over the engine, so leaving them registered would keep the engine and optimizer alive.
1368 # This runs after _deactivate_deepcompile() because that re-registers the forward hooks.
1369 for hook_attr in ("module_forward_pre_hook", "module_forward_post_hook", "first_dataloader_check"):
1370 hook = getattr(self, hook_attr, None)
1371 if hook is not None:
1372 hook.remove()
1373 setattr(self, hook_attr, None)
1374 debug_clear_module_and_param_names()
1375
1376 checkpoint_engine = getattr(self, "checkpoint_engine", None)
1377 if checkpoint_engine is not None and checkpoint_engine.is_decoupled():
1378 checkpoint_engine.cleanup()
1379 finally:
1380 python_gc_generation = getattr(self, "_python_gc_generation", None)
1381 if python_gc_generation is not None:
1382 from deepspeed.runtime.python_gc import python_gc_manager
1383 python_gc_manager.release(python_gc_generation)
1384 self._python_gc_generation = None
1385
1386 def collect_python_gc(self):
1387 """Run Python cyclic GC at an application-selected safe boundary."""

Calls 9

is_deepcompile_activeMethod · 0.95
destroy_exchangesFunction · 0.90
is_decoupledMethod · 0.45
cleanupMethod · 0.45
releaseMethod · 0.45