Skip to content

vllm.distributed.weight_transfer.m2n_layout ¶

Destination-layout resolution for the NCCL M2N weight transfer backend.

For every incoming checkpoint parameter the worker needs a destination buffer plus its placement over the inference mesh. Three outcomes are possible:

  • in-place — when the model is not quantized, the parameter maps 1:1 onto a live vLLM parameter and its loader is a small, known-safe copy or tensor- parallel loader. The loader's declared input/output dimension determines the placement; shapes are validation, not an inference mechanism. The reshard writes straight into the live parameter, so each rank receives only its own shard and nothing is copied afterwards.
  • sharded staging — a model module exposes the logical shard owned by this rank and an update-scoped callback that consumes it through the module's native loading path. M2N transfers only that shard into bounded staging.
  • full fallback — when explicitly allowed, the reshard delivers the whole tensor to every rank and load_weights performs the model-specific loading, matching the broadcast NCCL backend.

Parameters that require owner-local transfer fail closed when neither an in-place nor a semantic sharded destination can be resolved.

Classes:

  • M2NDestination –

    Immutable worker-local plan for one logical checkpoint tensor.

Functions:

M2NDestination dataclass ¶

Immutable worker-local plan for one logical checkpoint tensor.

Attributes:

  • tensor (Tensor | None) –

    Stable live destination for in-place updates; omitted from wire plans.

Source code in vllm/distributed/weight_transfer/m2n_layout.py
@dataclass(frozen=True)
class M2NDestination:
    """Immutable worker-local plan for one logical checkpoint tensor."""

    name: str
    mode: M2NDestinationMode
    mesh_dims: tuple[int, int]
    placements: Placements | None
    local_shape: tuple[int, ...]
    tensor: torch.Tensor | None = field(compare=False, repr=False)
    """Stable live destination for in-place updates; omitted from wire plans."""
    sharded_spec: ShardedWeightSpec | None = None
    staging_group: int | None = None
    staging_slot: int | None = None
    staging_group_size: int | None = None

    def __post_init__(self) -> None:
        dims = tuple(self.mesh_dims)
        M2NMesh(cast(tuple[int, int], dims), 0)
        object.__setattr__(self, "mesh_dims", dims)
        if self.placements is not None:
            object.__setattr__(self, "placements", tuple(self.placements))

    @property
    def direct(self) -> bool:
        return self.mode is M2NDestinationMode.IN_PLACE

tensor = field(compare=False, repr=False) class-attribute instance-attribute ¶

Stable live destination for in-place updates; omitted from wire plans.

_base_loader_shard_dim(param) ¶

Return a known-safe loader's shard dim, count, and rank, or reject it.

Loader identity is deliberately fail-closed. A missing loader is not treated as default_weight_loader because a model-level load_weights implementation may transform the tensor before it reaches that default. Wrappers and partials are also rejected: recognizing their wrapped callable would ignore behavior added by the wrapper.

Source code in vllm/distributed/weight_transfer/m2n_layout.py
def _base_loader_shard_dim(
    param: torch.nn.Parameter,
) -> tuple[int, int, int] | None:
    """Return a known-safe loader's shard dim, count, and rank, or reject it.

    Loader identity is deliberately fail-closed. A missing loader is not
    treated as ``default_weight_loader`` because a model-level ``load_weights``
    implementation may transform the tensor before it reaches that default.
    Wrappers and partials are also rejected: recognizing their wrapped callable
    would ignore behavior added by the wrapper.
    """
    loader = getattr(param, "weight_loader", None)
    if loader is None or isinstance(loader, partial):
        return None

    owner = loader.__self__ if inspect.ismethod(loader) else None
    loader_fn = loader.__func__ if inspect.ismethod(loader) else loader

    # Lazy imports avoid making the weight-transfer registry import all model
    # executor layers during normal vLLM startup.
    from vllm.model_executor.layers.linear import (
        ColumnParallelLinear,
        ReplicatedLinear,
        RowParallelLinear,
    )
    from vllm.model_executor.model_loader.weight_utils import default_weight_loader

    replicated_loaders = {
        default_weight_loader,
        ReplicatedLinear.weight_loader,
    }
    column_loaders = {
        ColumnParallelLinear.weight_loader,
        ColumnParallelLinear.weight_loader_v2,
    }
    row_loaders = {
        RowParallelLinear.weight_loader,
        RowParallelLinear.weight_loader_v2,
    }

    # These attributes signal packing, a transform, or pre-sharded checkpoint
    # storage. Such semantics cannot be reproduced by a plain M2N placement.
    # Check them before accepting replicated loaders too: model-level loading
    # may transform a tensor before handing it to an otherwise plain copier.
    metadata_attrs = ("packed_dim", "packed_factor", "marlin_tile_size")
    flag_attrs = ("needs_scalar_to_array", "is_sharded_weight", "is_transposed")
    if any(getattr(param, attr, None) is not None for attr in metadata_attrs):
        return None
    if any(bool(getattr(param, attr, False)) for attr in flag_attrs):
        return None

    if loader_fn in replicated_loaders:
        return REPLICATE, 1, 0
    if loader_fn not in column_loaders | row_loaders:
        return None

    if owner is None:
        return None
    tp_size = getattr(owner, "tp_size", None)
    tp_rank = getattr(owner, "tp_rank", None)
    if (
        not isinstance(tp_size, int)
        or isinstance(tp_size, bool)
        or tp_size <= 0
        or not isinstance(tp_rank, int)
        or isinstance(tp_rank, bool)
        or not 0 <= tp_rank < tp_size
    ):
        return None

    attr = "output_dim" if loader_fn in column_loaders else "input_dim"
    dim = getattr(param, attr, None)
    if not isinstance(dim, int) or isinstance(dim, bool) or dim < 0:
        return None
    return dim, tp_size, tp_rank

_destination_placements(shard_dim) ¶

Place one tensor shard on the configured destination mesh axis.

Source code in vllm/distributed/weight_transfer/m2n_layout.py
def _destination_placements(shard_dim: int) -> Placements:
    """Place one tensor shard on the configured destination mesh axis."""
    placements = [REPLICATE] * MESH_NDIMS
    placements[DESTINATION_REPLICA_AXIS] = REPLICATE
    placements[DESTINATION_SHARD_AXIS] = shard_dim
    return cast(Placements, tuple(placements))

_validated_shard_dim(global_shape, local_shape, declared_dim, shard_axis_size) ¶

Validate a loader-declared shard dimension against both shapes.

Source code in vllm/distributed/weight_transfer/m2n_layout.py
def _validated_shard_dim(
    global_shape: Sequence[int],
    local_shape: Sequence[int],
    declared_dim: int,
    shard_axis_size: int,
) -> int | None:
    """Validate a loader-declared shard dimension against both shapes."""
    if len(global_shape) != len(local_shape):
        return None

    if declared_dim == REPLICATE:
        return REPLICATE if tuple(global_shape) == tuple(local_shape) else None
    if declared_dim >= len(global_shape):
        return None
    for dim, (whole, local) in enumerate(zip(global_shape, local_shape)):
        expected = local * shard_axis_size if dim == declared_dim else local
        if whole != expected:
            return None
    return declared_dim

resolve_parameter_destinations(model, names, dtypes, shapes, *, num_workers, local_worker_rank, dst_mesh_dims, allow_direct, allow_full_fallback) ¶

Resolve semantic targets, then conservative exact-name destinations.

Source code in vllm/distributed/weight_transfer/m2n_layout.py
def resolve_parameter_destinations(
    model: torch.nn.Module,
    names: Sequence[str],
    dtypes: Sequence[torch.dtype],
    shapes: Sequence[Sequence[int]],
    *,
    num_workers: int,
    local_worker_rank: int,
    dst_mesh_dims: tuple[int, int] | None,
    allow_direct: bool,
    allow_full_fallback: Sequence[bool],
) -> list[M2NDestination]:
    """Resolve semantic targets, then conservative exact-name destinations."""
    if not (len(names) == len(dtypes) == len(shapes)):
        raise ValueError("destination inputs must have the same length")
    fallback_allowed = tuple(allow_full_fallback)
    if len(fallback_allowed) != len(names) or any(
        not isinstance(value, bool) for value in fallback_allowed
    ):
        raise ValueError("allow_full_fallback must contain one bool per destination")
    if (
        not isinstance(num_workers, int)
        or isinstance(num_workers, bool)
        or num_workers <= 0
    ):
        raise ValueError("num_workers must be a positive integer")
    if not 0 <= local_worker_rank < num_workers:
        raise ValueError(
            f"local worker rank {local_worker_rank} must be in [0, {num_workers})"
        )
    fixed_mesh_dims = None
    if dst_mesh_dims is not None:
        fixed_mesh = M2NMesh(cast(tuple[int, int], tuple(dst_mesh_dims)), 0)
        if fixed_mesh.size != num_workers:
            raise ValueError(
                f"destination mesh {fixed_mesh.dims} must cover {num_workers} workers"
            )
        fixed_mesh_dims = fixed_mesh.dims
    flat_mesh_dims = (num_workers, 1)

    params = dict(model.named_parameters()) if allow_direct else {}
    blocked_param_ids = {
        id(param)
        for module in model.modules()
        if _module_type_name(module) in _DIRECT_WEIGHT_LOADING_BLOCKLIST
        for param in module.parameters()
    }

    destinations: list[M2NDestination] = []
    closed_retention_keys: set[object] = set()
    active_retention_key: object | None = None
    active_staging_slot = -1
    active_staging_group_size = 0

    def close_staging_group() -> None:
        nonlocal active_retention_key
        if active_retention_key is None:
            return
        actual_size = active_staging_slot + 1
        if actual_size != active_staging_group_size:
            raise ValueError(
                "semantic destination group starting at "
                f"'{destinations[-actual_size].name}' contains "
                f"{actual_size} tensors, but its provider requires "
                f"{active_staging_group_size}"
            )
        closed_retention_keys.add(active_retention_key)
        active_retention_key = None

    for name, dtype, shape_value, fallback_ok in zip(
        names, dtypes, shapes, fallback_allowed
    ):
        shape = tuple(shape_value)
        request = ShardedWeightRequest(name, dtype, shape)
        target = resolve_sharded_weight_target(model, request)
        # Resolve first so malformed recognized tensors still fail closed.
        if not allow_direct:
            target = None
        if target is not None:
            mesh_dims = _validate_semantic_target(
                request,
                target,
                num_workers=num_workers,
                local_worker_rank=local_worker_rank,
                dst_mesh_dims=fixed_mesh_dims,
            )
            retention_key = target.retention_key
            if retention_key != active_retention_key:
                close_staging_group()
                if retention_key in closed_retention_keys:
                    raise ValueError(
                        f"semantic destination group for '{name}' is not "
                        "contiguous in transfer order"
                    )
                active_retention_key = retention_key
                active_staging_slot = 0
                active_staging_group_size = target.retention_group_size
            else:
                if target.retention_group_size != active_staging_group_size:
                    raise ValueError(
                        f"semantic destination group for '{name}' disagrees on "
                        "its required tensor count"
                    )
                active_staging_slot += 1
            destinations.append(
                M2NDestination(
                    name=name,
                    mode=M2NDestinationMode.SHARDED_STAGING,
                    mesh_dims=mesh_dims,
                    placements=_destination_placements(target.spec.shard_dim),
                    local_shape=target.spec.local_shape,
                    tensor=None,
                    sharded_spec=target.spec,
                    staging_group=len(closed_retention_keys),
                    staging_slot=active_staging_slot,
                    staging_group_size=active_staging_group_size,
                )
            )
            continue

        close_staging_group()
        param = params.get(name)
        dim = None
        mesh_dims = flat_mesh_dims
        if (
            param is not None
            and id(param) not in blocked_param_ids
            and param.dtype == dtype
            and param.data.is_contiguous()
        ):
            declared_dim: int | None
            shard_info = _base_loader_shard_dim(param)
            if shard_info is not None:
                declared_dim, shard_count, shard_index = shard_info
                expected_shards = (
                    fixed_mesh_dims[1] if fixed_mesh_dims is not None else shard_count
                )
                valid_coordinate = declared_dim == REPLICATE or (
                    shard_count == expected_shards
                    and num_workers % shard_count == 0
                    and shard_index == local_worker_rank % shard_count
                )
                if valid_coordinate:
                    mesh_dims = (
                        flat_mesh_dims
                        if declared_dim == REPLICATE
                        else fixed_mesh_dims
                        or (num_workers // shard_count, shard_count)
                    )
                else:
                    declared_dim = None
            else:
                declared_dim = None
            if declared_dim is not None:
                dim = _validated_shard_dim(
                    shape,
                    param.shape,
                    declared_dim,
                    1 if declared_dim == REPLICATE else shard_count,
                )

        if dim is None:
            if not fallback_ok:
                raise ValueError(
                    f"parameter '{name}' requires a sharded destination, but "
                    "the model did not provide one"
                )
            destinations.append(
                M2NDestination(
                    name=name,
                    mode=M2NDestinationMode.FULL_FALLBACK,
                    mesh_dims=flat_mesh_dims,
                    placements=REPLICATED,
                    local_shape=shape,
                    tensor=None,
                )
            )
            continue

        assert param is not None
        destinations.append(
            M2NDestination(
                name=name,
                mode=M2NDestinationMode.IN_PLACE,
                mesh_dims=mesh_dims,
                placements=(
                    REPLICATED if dim == REPLICATE else _destination_placements(dim)
                ),
                local_shape=tuple(param.shape),
                tensor=param.data,
            )
        )

    close_staging_group()

    # Layerwise reload restores model-format tensors as a unit. Mixing direct
    # and model-loader parameters within one module can copy stale storage
    # over a direct load.
    reload_modules = {
        name.rpartition(".")[0]
        for name, destination in zip(names, destinations)
        if not destination.direct and name in params
    }
    for index, destination in enumerate(destinations):
        if destination.direct and destination.name.rpartition(".")[0] in reload_modules:
            if not fallback_allowed[index]:
                raise ValueError(
                    f"parameter '{destination.name}' cannot share a module "
                    "with a model-loader destination without a full fallback"
                )
            destinations[index] = M2NDestination(
                name=destination.name,
                mode=M2NDestinationMode.FULL_FALLBACK,
                mesh_dims=flat_mesh_dims,
                placements=REPLICATED,
                local_shape=tuple(shapes[index]),
                tensor=None,
            )

    num_direct = sum(d.direct for d in destinations)
    num_staged = sum(
        destination.mode is M2NDestinationMode.SHARDED_STAGING
        for destination in destinations
    )
    parameter_bytes = [
        math.prod(shape) * dtype.itemsize for dtype, shape in zip(dtypes, shapes)
    ]
    total_bytes = sum(parameter_bytes)
    direct_bytes = sum(
        nbytes
        for destination, nbytes in zip(destinations, parameter_bytes)
        if destination.direct
    )
    direct_byte_percentage = 100 * direct_bytes / total_bytes if total_bytes else 0
    logger.info(
        "nccl_m2n destination plan: %d/%d parameters resharded directly into "
        "the model, %d via sharded staging, %d via full-tensor fallback; "
        "direct byte coverage: "
        "%d/%d bytes (%.1f%%)",
        num_direct,
        len(destinations),
        num_staged,
        len(destinations) - num_direct - num_staged,
        direct_bytes,
        total_bytes,
        direct_byte_percentage,
    )
    return destinations