Extend Cosmos3 CPU offloading to component and layerwise paths (#4695)

This commit is contained in:
Rahul Steiger authored and GitHub committed 2026-07-13 09:48:53 +00:00
1 parent 98229b08ba
commit 66b9ad287a
11 files changed
+653 -160

No files matched your search

@@ -22,6 +22,38 @@ class PipelineModules:
resident_modules: list[nn.Module] = field(default_factory=list)
resident_names: list[str] = field(default_factory=list)
def outermost_dits(self) -> tuple[list[str], list[nn.Module]]:
"""Discovered DiTs that are not nested inside another discovered DiT.
Some pipelines declare a DiT and one of its own submodules as separate
DiTs (e.g. Cosmos3 lists both ``transformer`` and the nested
``transformer.language_model``) so that offloading can treat them as
independent rings. HSDP instead shards each DiT through that DiT's own
``_hsdp_shard_conditions``; an inner DiT's blocks are already covered by
its ancestor's conditions, so sharding it again would double-wrap them
(and the inner stack may not declare conditions at all). Keeping only the
outermost DiTs leaves the ancestor's conditions as the single source of
truth for every nested block. Order follows ``dit_names``.
"""
def is_nested_in_another_dit(dit: nn.Module) -> bool:
for other in self.dits:
if other is dit:
continue
for submodule in other.modules():
if submodule is dit:
return True
return False
outer_names: list[str] = []
outer_modules: list[nn.Module] = []
for name, dit in zip(self.dit_names, self.dits):
if is_nested_in_another_dit(dit):
continue
outer_names.append(name)
outer_modules.append(dit)
return outer_names, outer_modules
class ModuleDiscovery:
"""Discovers pipeline components.
@@ -197,12 +197,35 @@ class ModelLevelOffloadBackend(OffloadBackend):
def __init__(self, config: OffloadConfig, device: torch.device):
super().__init__(config, device)
self._offload_modules: list[nn.Module] = [] # Track modules with hooks
self._custom_pipeline: nn.Module | None = None
def enable(self, pipeline: nn.Module) -> None:
if self.enabled:
logger.warning("ModelLevelOffloadBackend already enabled")
return
# Pipelines with a nested transformer (e.g. Cosmos3's reasoner/generator
# pathways) own their own mutual-exclusion swaps and cannot be offloaded
# with the generic encoder<->DiT hooks. Delegate to the custom hook.
# TODO: Cosmos3 is the only implementer today, so we duck-type the
# enable/disable_omni_model_cpu_offload pair via getattr. Once a second
# pipeline needs it, formalize this as a Protocol (like
# SupportsComponentDiscovery) instead of getattr detection.
custom_enable = getattr(pipeline, "enable_omni_model_cpu_offload", None)
if callable(custom_enable):
custom_enable(
device=self.device,
pin_memory=self.config.pin_cpu_memory,
use_hsdp=self.config.use_hsdp,
)
self._custom_pipeline = pipeline
self.enabled = True
logger.info(
"Model-level offloading enabled through %s.enable_omni_model_cpu_offload",
pipeline.__class__.__name__,
)
return
modules = ModuleDiscovery.discover(pipeline)
# Move encoders to GPU
@@ -259,6 +282,15 @@ class ModelLevelOffloadBackend(OffloadBackend):
if not self.enabled:
return
if self._custom_pipeline is not None:
custom_disable = getattr(self._custom_pipeline, "disable_omni_model_cpu_offload", None)
if callable(custom_disable):
custom_disable()
self._custom_pipeline = None
self.enabled = False
logger.info("Model-level offloading disabled")
return
remove_sequential_offload(self._offload_modules)
self._offload_modules.clear()