[Model][MiniMax-H3] Multi-GPU serving fixes and layer-wise offload component selection (#7047)
Signed-off-by: heyuanliu-intel <heyuan.liu@intel.com>
This commit is contained in:
1 parent
2f1a18e69f
commit
40f8e94400
21 files changed
+1857
-60
No files matched your search
@@ -75,11 +75,23 @@ def _load_component_config(component_path: str) -> dict[str, Any]:
|
||||
def _load_remote_component(
|
||||
component_path: str,
|
||||
config: dict[str, Any],
|
||||
*,
|
||||
trust_remote_code: bool,
|
||||
) -> nn.Module:
|
||||
auto_map = config.get("auto_map") or {}
|
||||
class_reference = auto_map.get("AutoModel")
|
||||
if not isinstance(class_reference, str):
|
||||
raise ValueError(f"{component_path}/config.json must define auto_map.AutoModel")
|
||||
if not trust_remote_code:
|
||||
raise ValueError(
|
||||
f"Loading {component_path} executes the modeling code shipped with "
|
||||
f"the checkpoint (auto_map.AutoModel = {class_reference}). Pass "
|
||||
"--trust-remote-code (or trust_remote_code=True) to allow it."
|
||||
)
|
||||
# ``trust_remote_code`` is checked here rather than forwarded to
|
||||
# ``get_class_from_dynamic_module``: that helper takes no such argument and
|
||||
# would silently absorb it into ``**kwargs``, so forwarding it would read
|
||||
# like a gate while executing the remote code unconditionally.
|
||||
component_cls = get_class_from_dynamic_module(
|
||||
class_reference,
|
||||
component_path,
|
||||
@@ -144,6 +156,7 @@ class MiniMaxH3VideoVAE(nn.Module, DistributedVaeMixin):
|
||||
*,
|
||||
device: torch.device,
|
||||
load_device: torch.device | None = None,
|
||||
trust_remote_code: bool = False,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self._device_target = device
|
||||
@@ -151,6 +164,7 @@ class MiniMaxH3VideoVAE(nn.Module, DistributedVaeMixin):
|
||||
self.remote = _load_remote_component(
|
||||
component_path,
|
||||
self.config_dict,
|
||||
trust_remote_code=trust_remote_code,
|
||||
)
|
||||
# Match the reference loader contract before installing inference-only
|
||||
# decoder fast paths. Keyframe encoding remains FP32; decoder Linear
|
||||
@@ -176,6 +190,11 @@ class MiniMaxH3VideoVAE(nn.Module, DistributedVaeMixin):
|
||||
self.use_slicing = False
|
||||
self.parallel_size = 1
|
||||
self.device_module = torch.get_device_module()
|
||||
self._tile_gather_workspace: torch.Tensor | None = None
|
||||
self._tile_gather_stats = {"hits": 0, "allocs": 0, "workspace_bytes": 0}
|
||||
self._checkpoint_tile_gather = None
|
||||
if self._tile_gather_reuse_enabled():
|
||||
self._install_persistent_tile_gather()
|
||||
|
||||
def load_to_device(self) -> None:
|
||||
if self._stager is not None:
|
||||
@@ -241,6 +260,145 @@ class MiniMaxH3VideoVAE(nn.Module, DistributedVaeMixin):
|
||||
parallel_module = importlib.import_module(f"{package}.parallel")
|
||||
return parallel_module.get_parallel_state()
|
||||
|
||||
def _tile_gather_reuse_enabled(self) -> bool:
|
||||
"""Whether this device needs the tiled-VAE gather address kept stable.
|
||||
|
||||
XPU only. The accumulation this guards against is an XCCL-side
|
||||
registration that is kept for every distinct receive-buffer address and
|
||||
never reclaimed; no other backend in tree does that, so everywhere else
|
||||
the checkpoint's own method is left in place and behaviour is unchanged.
|
||||
The stable address comes from one grow-only byte workspace, so what the
|
||||
adapter holds resident is one buffer for the largest geometry it has
|
||||
seen rather than one per geometry.
|
||||
"""
|
||||
|
||||
return self._device_target.type == "xpu"
|
||||
|
||||
def _install_persistent_tile_gather(self) -> None:
|
||||
"""Route the checkpoint's tiled-VAE gather through a bounded workspace.
|
||||
|
||||
The checkpoint's ``_all_gather_tiled_results`` allocates its gather
|
||||
output afresh on every call. Model-level offload calls ``empty_cache()``
|
||||
once per request, so the next request's output lands on a new device
|
||||
address; XCCL registers a non-reclaimable resource per new receive
|
||||
address, and the registrations accumulate until the card is full. The
|
||||
replacement below keeps a single byte workspace alive on this adapter
|
||||
and carves every gather out of its front, so the address the collective
|
||||
writes to is the same one every request.
|
||||
|
||||
The workspace only ever grows: a geometry that fits in the current
|
||||
block reuses it as is, and a larger one replaces the block, which
|
||||
drops the previous allocation before allocating its own. Resident
|
||||
memory is therefore bounded by one buffer for the largest geometry the
|
||||
adapter has seen, not by one buffer per distinct geometry.
|
||||
|
||||
The override is bound on the checkpoint *instance*, not its class: the
|
||||
class is remote code shared by every component loaded from the same
|
||||
checkpoint, and only this adapter knows the device it runs on.
|
||||
"""
|
||||
|
||||
self._checkpoint_tile_gather = self.model._all_gather_tiled_results
|
||||
self.model._all_gather_tiled_results = self._persistent_tile_gather
|
||||
logger.info(
|
||||
"[H3_VAE_GATHER] persistent tile gather installed device=%s",
|
||||
self._device_target.type,
|
||||
)
|
||||
|
||||
def _persistent_tile_gather(
|
||||
self,
|
||||
tasks: list[torch.Tensor],
|
||||
num_tiles: int,
|
||||
) -> list[torch.Tensor]:
|
||||
"""Equal-shape replacement for the checkpoint's tiled-result gather.
|
||||
|
||||
Contract kept identical to the checkpoint's method: return a list of
|
||||
``num_tiles`` tensors in global tile order, and raise on an empty local
|
||||
share so a rank that owns no tile cannot silently skip the collective.
|
||||
|
||||
Tile ownership is round-robin (``range(sp_rank, num_tiles, sp_size)``),
|
||||
so every rank can compute every other rank's task count from
|
||||
``num_tiles`` and ``sp_size`` alone. That makes the per-rank payloads
|
||||
equal once the leading task dimension is padded to ``max_tasks``, which
|
||||
is what lets a single ``all_gather_into_tensor`` into a stable buffer
|
||||
replace the variable-shape gather.
|
||||
|
||||
The landing buffer is a view onto the front of one byte workspace that
|
||||
this adapter keeps alive and only ever grows, so its address stays
|
||||
stable across requests while resident memory stays bounded by the
|
||||
largest geometry seen rather than growing per geometry. A byte
|
||||
workspace is dtype-agnostic, so mixed-precision requests share it too.
|
||||
|
||||
Returned tiles are cloned out of the buffer: the buffer is overwritten
|
||||
by the next call and callers hold the tiles past that point.
|
||||
"""
|
||||
|
||||
state = self._native_parallel_state()
|
||||
group = state["sp_process_group"]
|
||||
sp_size = int(state["sp_size"])
|
||||
sp_rank = int(state["sp_rank"])
|
||||
|
||||
if not tasks:
|
||||
raise ValueError(f"Found empty tasks on sp rank {sp_rank}")
|
||||
|
||||
max_tasks = -(-num_tiles // sp_size)
|
||||
if len(tasks) > max_tasks:
|
||||
raise ValueError(
|
||||
f"sp rank {sp_rank} holds {len(tasks)} tiles but round-robin "
|
||||
f"ownership of {num_tiles} tiles across {sp_size} ranks allows "
|
||||
f"at most {max_tasks}"
|
||||
)
|
||||
if len(tasks) == max_tasks:
|
||||
stacked = torch.stack(tasks, dim=0)
|
||||
else:
|
||||
# Pad the leading (task) dimension only. The padded slots belong to
|
||||
# ranks whose share is short by construction, and the unpacking loop
|
||||
# below never reads them back.
|
||||
stacked = tasks[0].new_empty((max_tasks, *tasks[0].shape))
|
||||
torch.stack(tasks, dim=0, out=stacked[: len(tasks)])
|
||||
stacked[len(tasks) :].zero_()
|
||||
|
||||
need_bytes = sp_size * stacked.numel() * stacked.element_size()
|
||||
workspace = self._tile_gather_workspace
|
||||
if workspace is None or workspace.device != stacked.device or workspace.numel() < need_bytes:
|
||||
# Drop the previous block before allocating its replacement so the
|
||||
# two are never resident at once; only one workspace is ever held.
|
||||
workspace = None
|
||||
self._tile_gather_workspace = None
|
||||
workspace = torch.empty(need_bytes, dtype=torch.uint8, device=stacked.device)
|
||||
self._tile_gather_workspace = workspace
|
||||
self._tile_gather_stats["allocs"] += 1
|
||||
self._tile_gather_stats["workspace_bytes"] = need_bytes
|
||||
reuse = "alloc"
|
||||
else:
|
||||
self._tile_gather_stats["hits"] += 1
|
||||
reuse = "hit"
|
||||
buffer = workspace[:need_bytes].view(stacked.dtype).view(sp_size, *stacked.shape)
|
||||
# Quantities, not just presence: a line that only says "installed"
|
||||
# cannot distinguish a buffer that is being reused from one that is
|
||||
# reallocated every request, which is the whole failure being fixed.
|
||||
# Debug level, because this fires once per decoder tile batch (12 times
|
||||
# per request on the canonical 1344x768 geometry) and the one-shot
|
||||
# "installed" line above is what an operator needs at info level.
|
||||
logger.debug(
|
||||
"[H3_VAE_GATHER] reuse=%s shape=%s/%s need_mib=%.2f workspace_mib=%.2f buf_ptr=0x%x hits=%d allocs=%d",
|
||||
reuse,
|
||||
tuple(stacked.shape),
|
||||
stacked.dtype,
|
||||
need_bytes / (1024.0 * 1024.0),
|
||||
self._tile_gather_stats["workspace_bytes"] / (1024.0 * 1024.0),
|
||||
workspace.data_ptr(),
|
||||
self._tile_gather_stats["hits"],
|
||||
self._tile_gather_stats["allocs"],
|
||||
)
|
||||
dist.all_gather_into_tensor(buffer, stacked, group=group)
|
||||
|
||||
results: list[torch.Tensor] = [None] * num_tiles # type: ignore[list-item]
|
||||
for rank in range(sp_size):
|
||||
num_rank_tasks = -(-(num_tiles - rank) // sp_size)
|
||||
for k in range(num_rank_tasks):
|
||||
results[k * sp_size + rank] = buffer[rank][k].clone()
|
||||
return results
|
||||
|
||||
def _decoder_tile_count(self, latent: torch.Tensor) -> int:
|
||||
"""Number of decoder tiles the checkpoint will split ``latent`` into.
|
||||
|
||||
@@ -464,6 +622,7 @@ class MiniMaxH3AudioVAE(nn.Module):
|
||||
*,
|
||||
device: torch.device,
|
||||
load_device: torch.device | None = None,
|
||||
trust_remote_code: bool = False,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self._device_target = device
|
||||
@@ -471,6 +630,7 @@ class MiniMaxH3AudioVAE(nn.Module):
|
||||
self.remote = _load_remote_component(
|
||||
component_path,
|
||||
self.config_dict,
|
||||
trust_remote_code=trust_remote_code,
|
||||
)
|
||||
# The checkpoint's audio VAE contract is FP32 for both reference
|
||||
# encoding and waveform decoding.
|
||||
|
||||
Reference in new issue
Block a user