Skip to content

vllm.distributed.weight_transfer.m2n_engine

Inference-side weight transfer engine built on NCCL M2N (nccl_m2n).

Each ordered trainer source group shares one NCCL communicator with the inference workers. A parameter is routed to exactly one of those routes and moved with one nccl.m2n.reshard, which redistributes it from the trainer's layout (FSDP / TP / EP / arbitrary DTensor sharding) to the inference layout. The trainer sends local shards and never all-gathers a full tensor, which is what the broadcast NCCL backend forces it to do.

The destination is planned per parameter. A plan is either entirely direct (M2N writes live model storage), or it uses the model's layerwise reload lifecycle, with bounded EP-local staging for semantic fused-MoE consumers and full checkpoint-format fallback for everything else.

Members of each route participate in its reshards in the same authenticated order. A bounded wave schedule keeps launch order identical across routes while separate CUDA streams permit independent routes to overlap. The worker must be inside receive_weights while the trainer is sending; driving that from the trainer is the trainer engine's job.

Classes:

M2NWeightTransferEngine

Bases: WeightTransferEngine[M2NWeightTransferInitInfo, M2NWeightTransferUpdateInfo]

Receiver with immutable per-parameter M2N destinations.

Methods:

  • finish_weight_update –

    Finalize one update, or reset it so the trainer can replay it.

  • init_transfer_engine –

    Agree on source and local readiness before constructing NCCL.

  • receive_weights –

    Receive one exact contiguous slice of the immutable transfer plan.

  • shutdown –

    Finish pending GPU work and release M2N communication state.

  • start_weight_update –

    Enter one update and bind semantic callbacks to this generation.

  • update_weights –

    Preserve pre-transfer failures; abort failures after transfer starts.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
class M2NWeightTransferEngine(
    WeightTransferEngine[M2NWeightTransferInitInfo, M2NWeightTransferUpdateInfo]
):
    """Receiver with immutable per-parameter M2N destinations."""

    init_info_cls = M2NWeightTransferInitInfo
    update_info_cls = M2NWeightTransferUpdateInfo
    # The destination plan is resolved against the model this engine was built
    # for, so it cannot be retargeted at a draft model mid-flight.
    supports_draft_weight_update = False

    def __init__(
        self,
        config: WeightTransferConfig,
        vllm_config: "VllmConfig",
        device: torch.device,
        model: torch.nn.Module,
    ) -> None:
        super().__init__(config, vllm_config, device, model)
        self.model_update_group: Any = None
        self._model_update_groups: dict[int, Any] = {}
        self._route_streams: dict[int, torch.cuda.Stream] = {}
        self._routes: tuple[M2NTransferRoute, ...] = ()
        self._param_routes: tuple[int, ...] = ()
        self._source_topology = M2NSourceTopology((), ())
        self._m2n: Any = None
        self._handle: Any = None
        self._metas: list[M2NParamMeta] = []
        self._parameter_destinations: list[M2NDestination] = []
        self._uses_load_weights = False
        self._staging_pool: M2NStagingPool | None = None
        self._staged_targets: dict[int, ShardedWeightTarget] = {}
        self._next_parameter_index = 0

    def init_transfer_engine(self, init_info: M2NWeightTransferInitInfo) -> None:
        """Agree on source and local readiness before constructing NCCL."""
        if self._handle is not None or self._model_update_groups:
            raise RuntimeError(
                "nccl_m2n weight transfer is already initialized; call "
                "shutdown_weight_transfer_engine() before initializing again"
            )
        rendezvous_info = NCCLWeightTransferInitInfo(
            master_address=init_info.master_address,
            master_port=init_info.master_port,
            rank_offset=(
                init_info.rank_offset
                if init_info.worker_rank_offset is None
                else init_info.worker_rank_offset
            ),
            world_size=init_info.world_size,
        )
        metadata_group = worker_init_metadata_group(
            rendezvous_info, self.parallel_config
        )

        wire_params: tuple[M2NWireParam, ...] = ()
        source_topology = M2NSourceTopology((), ())
        source_error: str | None = None
        try:
            self._m2n = import_m2n()
            wire_params, source_topology = init_info.parse_source_plan()
            self._source_topology = source_topology
            self._prepare_source_plan(wire_params, source_topology)
            self._routes = build_m2n_transfer_routes(
                source_topology,
                num_trainer_ranks=init_info.rank_offset,
                world_size=init_info.world_size,
                dst_mesh_dims=init_info.destination_mesh_dims,
            )
            self._param_routes = build_m2n_param_routes(self._routes, len(wire_params))
        except Exception as exc:
            source_error = f"{type(exc).__name__}: {exc}"
            self._metas = []
            self._routes = ()
            self._param_routes = ()
        plan_digest = check_source_plan_agreement(
            metadata_group,
            wire_params,
            expected_digest=init_info.source_digest,
            local_error=source_error,
            topology=source_topology,
        )

        num_workers = init_info.world_size - init_info.rank_offset
        agreement_mesh = M2NMesh(init_info.destination_mesh_dims, init_info.rank_offset)

        local_wire_plan: list[M2NWireDestination] | None = None
        destination_error: str | None = None
        try:
            local_worker_rank = metadata_group.rank - init_info.rank_offset
            if not 0 <= local_worker_rank < num_workers:
                raise ValueError(
                    f"metadata rank {metadata_group.rank} is outside destination "
                    f"ranks [{init_info.rank_offset}, {init_info.world_size})"
                )
            self._prepare_destination_plan(
                local_worker_rank=local_worker_rank,
                num_workers=num_workers,
                dst_mesh_dims=init_info.destination_mesh_constraint,
            )
            local_wire_plan = [
                M2NWireDestination.from_destination(
                    destination,
                    dtype_name=wire_param.dtype_name,
                    local_worker_rank=local_worker_rank,
                )
                for wire_param, destination in zip(
                    wire_params, self._parameter_destinations
                )
            ]
        except Exception as exc:
            destination_error = f"{type(exc).__name__}: {exc}"
            self._clear_destination_plan()

        try:
            agree_destination_plan(
                metadata_group,
                source_digest=plan_digest,
                params=wire_params,
                source_topology=source_topology,
                metas=self._metas,
                dst_mesh=agreement_mesh,
                dst_mesh_constraint=init_info.destination_mesh_constraint,
                local_plan=local_wire_plan,
                local_error=destination_error,
            )
            unique_id_bytes: tuple[bytes | None, ...] = ()
            data_plane_error: str | None = None
            try:
                unique_id_bytes = init_info.nccl_unique_id_bytes_by_route(
                    len(self._routes)
                )
                check_multi_communicator_launch_order(len(self._routes))
            except Exception as exc:
                data_plane_error = f"{type(exc).__name__}: {exc}"
            check_data_plane_agreement(
                metadata_group,
                unique_id_bytes,
                data_plane_error,
            )
        except BaseException:
            self._clear_destination_plan()
            raise
        handle = None
        groups: dict[int, Any] = {}
        route_streams: dict[int, torch.cuda.Stream] = {}
        try:
            runtime_error = destination_error
            nccl = None
            device = None
            if runtime_error is None:
                try:
                    nccl, device, handle = prepare_m2n_local_runtime(
                        self._m2n, init_info.max_cta
                    )
                except Exception as exc:
                    runtime_error = f"{type(exc).__name__}: {exc}"
            check_runtime_ready_agreement(metadata_group, runtime_error)
            assert nccl is not None and device is not None and handle is not None
            communicator_errors: list[str | None] = []
            for route_index, (route, route_uid) in enumerate(
                zip(self._routes, unique_id_bytes, strict=True)
            ):
                route_error: str | None = None
                try:
                    group = create_m2n_communicator(
                        nccl,
                        metadata_group,
                        route_uid,
                        members=route.members,
                    )
                    if group is None:
                        raise RuntimeError(
                            "inference worker is missing from transfer route "
                            f"{route_index}"
                        )
                    groups[route_index] = group
                    route_streams[route_index] = torch.cuda.Stream(device=device)
                except Exception as exc:
                    route_error = f"{type(exc).__name__}: {exc}"
                communicator_errors.append(route_error)
            check_communicator_init_agreement(
                metadata_group,
                communicator_errors,
            )
        except BaseException:
            for route_group in groups.values():
                try:
                    route_group.abort()
                except BaseException:
                    logger.exception(
                        "failed to abort partially initialized nccl_m2n communicator"
                    )
            if handle is not None:
                try:
                    handle.destroy()
                except BaseException:
                    logger.exception(
                        "failed to destroy partially initialized nccl_m2n handle"
                    )
            self._clear_destination_plan()
            self._routes = ()
            self._param_routes = ()
            self._source_topology = M2NSourceTopology((), ())
            raise

        self._handle = handle
        self._model_update_groups = groups
        self._route_streams = route_streams
        # Retain the scalar alias for single-communicator callers and diagnostics.
        self.model_update_group = groups.get(0)

    def _clear_destination_plan(self) -> None:
        self._clear_update_bindings()
        self._parameter_destinations = []
        self._uses_load_weights = False
        self._staging_pool = None

    def _prepare_source_plan(
        self,
        wire_params: tuple[M2NWireParam, ...],
        source_topology: M2NSourceTopology,
    ) -> None:
        """Validate worker-visible source metadata without constructing NCCL."""
        self._metas = []
        for param_index, param in enumerate(wire_params):
            dtype = getattr(torch, param.dtype_name, None)
            if not isinstance(dtype, torch.dtype):
                raise ValueError(
                    f"parameter '{param.name}' has invalid dtype name "
                    f"'{param.dtype_name}'; expected the name of a torch.dtype"
                )
            check_transferable(param.name, dtype, param.shape)
            layout = M2NLayout(M2NMesh(param.src_mesh_dims, 0), param.src_placements)
            group_index = source_topology.param_groups[param_index]
            source_group = source_topology.groups[group_index]
            if layout.mesh.size != len(source_group):
                raise ValueError(
                    f"parameter '{param.name}' source mesh covers "
                    f"{layout.mesh.size} ranks, but source group {group_index} "
                    f"contains {len(source_group)} ranks"
                )
            meta = M2NParamMeta(
                param.name,
                dtype,
                param.shape,
                layout,
                param.allow_full_fallback,
            )
            expected_local_shape(meta)
            self._metas.append(meta)

    def _prepare_destination_plan(
        self,
        *,
        local_worker_rank: int,
        num_workers: int,
        dst_mesh_dims: tuple[int, int] | None,
    ) -> None:
        """Resolve and validate this worker's destination plan."""
        if not 0 <= local_worker_rank < num_workers:
            raise ValueError(
                f"local worker rank {local_worker_rank} must be in [0, {num_workers})"
            )
        # A quantized weight loader may dequantize / transpose / requantize, and
        # under PP a rank's local shape is not the checkpoint shape split over
        # the shard axis. Both disable raw and semantic sharded destinations, so
        # take the full-tensor fallback.
        allow_direct = (
            getattr(self.model_config, "quantization", None) is None
            and self.parallel_config.pipeline_parallel_size == 1
        )
        # Match every incoming checkpoint parameter to its rank-local target.
        # Resolvable parameters can be resharded directly into live model
        # storage; the rest receive a full tensor for `load_weights`.
        self._parameter_destinations = resolve_parameter_destinations(
            self.model,
            [m.name for m in self._metas],
            [m.dtype for m in self._metas],
            [m.shape for m in self._metas],
            num_workers=num_workers,
            local_worker_rank=local_worker_rank,
            dst_mesh_dims=dst_mesh_dims,
            allow_direct=allow_direct,
            allow_full_fallback=[meta.allow_full_fallback for meta in self._metas],
        )
        self._uses_load_weights = any(
            destination.mode is not M2NDestinationMode.IN_PLACE
            for destination in self._parameter_destinations
        )
        requirements = [
            M2NStagingRequirement(
                group=cast(int, destination.staging_group),
                slot=cast(int, destination.staging_slot),
                dtype=meta.dtype,
                shape=destination.local_shape,
            )
            for meta, destination in zip(self._metas, self._parameter_destinations)
            if destination.mode is M2NDestinationMode.SHARDED_STAGING
        ]
        self._staging_pool = (
            M2NStagingPool(requirements, self.device) if requirements else None
        )

    def start_weight_update(self) -> None:
        """Enter one update and bind semantic callbacks to this generation."""
        self._require_initialized()
        try:
            if self._staging_pool is not None:
                self._staging_pool.start_update()
            if self._uses_load_weights:
                from vllm.model_executor.model_loader.reload import (
                    initialize_layerwise_reload,
                )

                initialize_layerwise_reload(self.model)
            self._bind_staged_targets()
        except BaseException:
            try:
                self._reset_failed_update()
            except BaseException:
                logger.exception("failed to reset nccl_m2n update state")
            raise

    def finish_weight_update(self) -> None:
        """Finalize one update, or reset it so the trainer can replay it."""
        self._require_initialized()
        try:
            if self._next_parameter_index != len(self._metas):
                missing = self._metas[self._next_parameter_index].name
                raise RuntimeError(
                    "cannot finish an incomplete nccl_m2n update: received "
                    f"{self._next_parameter_index}/{len(self._metas)} "
                    f"parameters (next expected: {missing!r})"
                )

            if self._uses_load_weights:
                from vllm.model_executor.model_loader.reload import (
                    finalize_layerwise_reload,
                )

                finalize_layerwise_reload(self.model, self.model_config)
                # Complete copies from retained staging inputs before reuse.
                torch.accelerator.synchronize()
            if self._staging_pool is not None:
                self._staging_pool.release_retained_after_finalize()
                self._staging_pool.finish_update()

            self._clear_update_bindings()
        except BaseException as exc:
            try:
                self._reset_failed_update()
            except BaseException as reset_exc:
                raise RuntimeError(
                    "nccl_m2n failed to reset vLLM after a finish failure"
                ) from reset_exc
            raise RetryableWeightUpdateError(
                "vLLM failed to finalize the weight update; worker state was "
                "reset so the complete update can be retried"
            ) from exc

    def _reset_failed_update(self) -> None:
        """Reset loader and staging state without touching M2N transport."""
        if self._uses_load_weights:
            from vllm.model_executor.model_loader.reload import (
                abort_layerwise_reload,
            )

            # Loader callbacks can enqueue reads from retained staging buffers.
            # Finish them before restoring tensors and releasing those buffers.
            torch.accelerator.synchronize()
            abort_layerwise_reload(self.model)
        self._discard_update()

    def _bind_staged_targets(self) -> None:
        """Re-resolve update-scoped consumers after layerwise initialization."""
        group_to_key: dict[int, object] = {}
        key_to_group: dict[object, int] = {}
        for index, (meta, destination) in enumerate(
            zip(self._metas, self._parameter_destinations)
        ):
            if destination.mode is not M2NDestinationMode.SHARDED_STAGING:
                continue
            request = ShardedWeightRequest(meta.name, meta.dtype, meta.shape)
            target = resolve_sharded_weight_target(self.model, request)
            if target is None:
                raise RuntimeError(
                    f"staged destination {meta.name!r} disappeared after "
                    "initialize_layerwise_reload"
                )
            if target.spec != destination.sharded_spec:
                raise RuntimeError(
                    f"staged destination {meta.name!r} changed geometry after "
                    "initialize_layerwise_reload"
                )
            group = cast(int, destination.staging_group)
            group_size = cast(int, destination.staging_group_size)
            if target.retention_group_size != group_size:
                raise RuntimeError(
                    f"staged destination {meta.name!r} changed retention group "
                    f"size from {group_size} to {target.retention_group_size}"
                )
            key = target.retention_key
            if group in group_to_key and group_to_key[group] != key:
                raise RuntimeError(
                    f"staged destination group {group} changed retention owner"
                )
            if key in key_to_group and key_to_group[key] != group:
                raise RuntimeError(
                    f"retention owner for {meta.name!r} spans planned groups "
                    f"{key_to_group[key]} and {group}"
                )
            group_to_key[group] = key
            key_to_group[key] = group
            self._staged_targets[index] = target

    def update_weights(self, update_info: dict[str, Any]) -> None:
        """Preserve pre-transfer failures; abort failures after transfer starts."""
        self._require_initialized()
        try:
            typed_update_info = self.parse_update_info(update_info)
            self.receive_weights(typed_update_info)
        except BaseException:
            if self._handle is not None:
                self._reset_failed_update()
            raise
        try:
            torch.accelerator.synchronize()
        except BaseException:
            self._abort_data_plane()
            raise

    def receive_weights(self, update_info: M2NWeightTransferUpdateInfo) -> None:
        """Receive one exact contiguous slice of the immutable transfer plan."""
        self._require_initialized()
        self._receive_weights(update_info)

    def _receive_weights(self, update_info: M2NWeightTransferUpdateInfo) -> None:
        if not isinstance(update_info.names, list) or any(
            not isinstance(name, str) for name in update_info.names
        ):
            raise TypeError("nccl_m2n update names must be a list of strings")
        start = self._next_parameter_index
        end = start + len(update_info.names)
        expected = tuple(meta.name for meta in self._metas[start:end])
        received = tuple(update_info.names)
        if received != expected:
            raise ValueError(
                "nccl_m2n update order mismatch: expected the contiguous "
                f"slice {expected!r} at offset {start}, got {received!r}"
            )

        from vllm.model_executor.model_loader.mtp_validation import (
            disable_mtp_completeness_check,
        )

        processed = 0
        transfer_started = False
        pending_buffers: dict[int, torch.Tensor] = {}
        waves = build_m2n_transfer_waves(
            self._routes,
            [
                m2n_load_group(destination.mode, destination.staging_group)
                for destination in self._parameter_destinations
            ],
            active_indices=range(start, end),
        )

        def receive_requested_weights() -> Iterator[tuple[str, torch.Tensor]]:
            nonlocal processed, transfer_started
            for wave in waves:
                # One host dispatcher replays the agreed bounded wave. Distinct
                # streams may overlap, while every participant observes the
                # same cross-communicator launch order.
                for index in wave.issue_indices:
                    route_index = self._param_routes[index]
                    comm = self._model_update_groups[route_index]
                    stream = self._route_streams[route_index]
                    meta = self._metas[index]
                    destination = self._parameter_destinations[index]

                    if destination.mode is M2NDestinationMode.IN_PLACE:
                        buffer = self._resolve_direct_buffer(meta, destination)
                        transfer_started = True
                        self._reshard(
                            comm,
                            stream,
                            meta,
                            destination,
                            buffer,
                        )
                        processed += 1
                        continue

                    if destination.mode is M2NDestinationMode.FULL_FALLBACK:
                        buffer = torch.empty(
                            meta.shape, dtype=meta.dtype, device=self.device
                        )
                    else:
                        assert self._staging_pool is not None
                        buffer = self._staging_pool.acquire(
                            group=cast(int, destination.staging_group),
                            slot=cast(int, destination.staging_slot),
                            dtype=meta.dtype,
                            shape=destination.local_shape,
                        )
                    # Retain ownership before launching. If reshard raises,
                    # abort must complete before this buffer can be released.
                    pending_buffers[index] = buffer
                    transfer_started = True
                    self._reshard(
                        comm,
                        stream,
                        meta,
                        destination,
                        buffer,
                    )

                if wave.consume_index is None:
                    continue
                index = wave.consume_index
                meta = self._metas[index]
                destination = self._parameter_destinations[index]
                route_index = self._param_routes[index]
                stream = self._route_streams[route_index]
                stream.synchronize()
                try:
                    buffer = pending_buffers.pop(index)
                except KeyError as exc:
                    raise RuntimeError(
                        "nccl_m2n transfer schedule consumed parameter "
                        f"{meta.name!r} before issuing it"
                    ) from exc
                if destination.mode is M2NDestinationMode.FULL_FALLBACK:
                    processed += 1
                    yield meta.name, buffer
                    continue

                assert self._staging_pool is not None
                target = self._staged_targets[index]
                group = cast(int, destination.staging_group)
                slot = cast(int, destination.staging_slot)
                group_size = cast(int, destination.staging_group_size)
                if target.load(buffer):
                    if slot != group_size - 1:
                        raise RuntimeError(
                            f"staged consumer for {meta.name!r} released group "
                            f"{group} at slot {slot}, before planned final slot "
                            f"{group_size - 1}"
                        )
                    # Model loaders may enqueue reads of the staging inputs.
                    # Order every possible reuse stream after those reads.
                    consumer_stream = torch.cuda.current_stream(self.device)
                    for transfer_stream in self._route_streams.values():
                        transfer_stream.wait_stream(consumer_stream)
                    self._staging_pool.release_group(group)
                processed += 1

            if pending_buffers:
                raise RuntimeError(
                    "nccl_m2n route scheduler ended with pending buffers"
                )
            self._synchronize_route_streams()

        has_fallback = any(
            self._parameter_destinations[index].mode is M2NDestinationMode.FULL_FALLBACK
            for index in range(start, end)
        )
        try:
            with disable_mtp_completeness_check():
                received_weights = receive_requested_weights()
                if has_fallback:
                    loaded = self.model.load_weights(received_weights)
                    if loaded is not None:
                        for _ in loaded:
                            pass
                else:
                    for _ in received_weights:
                        pass
            if processed != end - start:
                raise RuntimeError(
                    "model.load_weights did not consume the complete nccl_m2n "
                    f"fallback sequence: processed {processed}/{end - start}"
                )
            self._next_parameter_index = end
        except BaseException:
            if transfer_started:
                # pending buffers and a suspended fallback iterator remain alive
                # until every local communicator has been aborted.
                self._abort_data_plane()
            raise

    def _resolve_direct_buffer(
        self, meta: M2NParamMeta, destination: M2NDestination
    ) -> torch.Tensor:
        """Validate the live destination captured before layerwise reload."""
        parameter_name = destination.name
        buffer = destination.tensor
        if buffer is None:
            raise RuntimeError(f"in-place destination {parameter_name!r} is missing")
        if buffer.dtype != meta.dtype:
            raise RuntimeError(
                f"in-place destination {parameter_name!r} changed dtype from "
                f"{meta.dtype} to {buffer.dtype}"
            )
        if tuple(buffer.shape) != destination.local_shape:
            raise RuntimeError(
                f"in-place destination {parameter_name!r} changed shape from "
                f"{destination.local_shape} to {tuple(buffer.shape)}"
            )
        if not buffer.is_contiguous():
            raise RuntimeError(
                f"in-place destination {parameter_name!r} is not contiguous"
            )
        if buffer.device != self.device:
            raise RuntimeError(
                f"in-place destination {parameter_name!r} is on "
                f"{buffer.device}, expected {self.device}"
            )
        return buffer

    def _reshard(
        self,
        comm: Any,
        stream: torch.cuda.Stream,
        src_meta: M2NParamMeta,
        destination: M2NDestination,
        dst_buffer: torch.Tensor,
    ) -> None:
        """Reshard one parameter from its trainer layout into a worker buffer.

        ``destination`` carries this parameter's agreed mesh and placement;
        ``dst_buffer`` is this rank's physical storage.
        """
        m2n = self._m2n
        src_mesh, resolved_src_placements = resolve_layout(
            src_meta.source_layout.mesh, src_meta.source_layout.placements
        )
        source_group_size = len(self._source_topology.groups[0])
        dst_mesh, resolved_dst_placements = resolve_layout(
            M2NMesh(destination.mesh_dims, source_group_size),
            destination.placements,
        )
        m2n.reshard(
            None,
            dst_buffer,
            comm,
            stream,
            src_mesh=to_mesh(m2n, src_mesh),
            src_placements=to_placements(m2n, resolved_src_placements),
            dst_mesh=to_mesh(m2n, dst_mesh),
            dst_placements=to_placements(m2n, resolved_dst_placements),
            handle=self._handle,
        )

    def _synchronize_route_streams(self) -> None:
        """Wait for every joined route without assuming a default stream."""
        for route_index in range(len(self._routes)):
            stream = self._route_streams.get(route_index)
            if stream is not None:
                stream.synchronize()

    def _clear_update_bindings(self) -> None:
        self._staged_targets.clear()
        self._next_parameter_index = 0

    def _discard_update(self) -> None:
        """Release only update-local staging after a failed update."""
        self._clear_update_bindings()
        if self._staging_pool is not None:
            self._staging_pool.discard_update()

    def _require_initialized(self) -> None:
        if self._handle is None or not self._model_update_groups:
            raise RuntimeError(
                "nccl_m2n weight transfer not initialized. "
                "Call init_transfer_engine() first."
            )

    def _abort_data_plane(self) -> None:
        """Abort every communicator without waiting on CUDA streams."""
        groups, self._model_update_groups = self._model_update_groups, {}
        handle, self._handle = self._handle, None
        streams, self._route_streams = self._route_streams, {}
        self.model_update_group = None

        for group_index, group in groups.items():
            try:
                group.abort()
            except BaseException:
                logger.exception(
                    "failed to abort nccl_m2n transfer communicator %d",
                    group_index,
                )
        if handle is not None:
            try:
                handle.destroy()
            except BaseException:
                logger.exception("failed to destroy nccl_m2n handle")
        try:
            self._discard_update()
        except BaseException:
            logger.exception("failed to discard nccl_m2n update state")

        self._routes = ()
        self._param_routes = ()
        self._source_topology = M2NSourceTopology((), ())
        self._m2n = None
        self._metas = []
        self._clear_destination_plan()
        # Keep the stream wrappers alive until communicator abort and handle
        # destruction have completed.
        del streams

    def shutdown(self) -> None:
        """Finish pending GPU work and release M2N communication state."""
        handle, self._handle = self._handle, None
        groups, self._model_update_groups = self._model_update_groups, {}
        self.model_update_group = None
        streams, self._route_streams = self._route_streams, {}
        self._routes = ()
        self._param_routes = ()
        self._source_topology = M2NSourceTopology((), ())
        self._m2n = None
        self._metas = []
        self._clear_destination_plan()

        first_error: BaseException | None = None

        def attempt(action: Any, message: str) -> None:
            nonlocal first_error
            try:
                action()
            except BaseException as exc:
                if first_error is None:
                    first_error = exc
                logger.exception(message)

        if handle is not None or groups:
            # M2N does not synchronize caller streams on finalize.
            attempt(torch.accelerator.synchronize, "failed to synchronize nccl_m2n")
        if handle is not None:
            attempt(handle.destroy, "failed to destroy nccl_m2n handle")
        for group_index, group in groups.items():
            attempt(
                group.abort,
                f"failed to abort nccl_m2n route {group_index} communicator",
            )
        # Keep stream wrappers alive through communicator cleanup.
        del streams
        if first_error is not None:
            raise first_error

_abort_data_plane()

Abort every communicator without waiting on CUDA streams.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _abort_data_plane(self) -> None:
    """Abort every communicator without waiting on CUDA streams."""
    groups, self._model_update_groups = self._model_update_groups, {}
    handle, self._handle = self._handle, None
    streams, self._route_streams = self._route_streams, {}
    self.model_update_group = None

    for group_index, group in groups.items():
        try:
            group.abort()
        except BaseException:
            logger.exception(
                "failed to abort nccl_m2n transfer communicator %d",
                group_index,
            )
    if handle is not None:
        try:
            handle.destroy()
        except BaseException:
            logger.exception("failed to destroy nccl_m2n handle")
    try:
        self._discard_update()
    except BaseException:
        logger.exception("failed to discard nccl_m2n update state")

    self._routes = ()
    self._param_routes = ()
    self._source_topology = M2NSourceTopology((), ())
    self._m2n = None
    self._metas = []
    self._clear_destination_plan()
    # Keep the stream wrappers alive until communicator abort and handle
    # destruction have completed.
    del streams

_bind_staged_targets()

Re-resolve update-scoped consumers after layerwise initialization.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _bind_staged_targets(self) -> None:
    """Re-resolve update-scoped consumers after layerwise initialization."""
    group_to_key: dict[int, object] = {}
    key_to_group: dict[object, int] = {}
    for index, (meta, destination) in enumerate(
        zip(self._metas, self._parameter_destinations)
    ):
        if destination.mode is not M2NDestinationMode.SHARDED_STAGING:
            continue
        request = ShardedWeightRequest(meta.name, meta.dtype, meta.shape)
        target = resolve_sharded_weight_target(self.model, request)
        if target is None:
            raise RuntimeError(
                f"staged destination {meta.name!r} disappeared after "
                "initialize_layerwise_reload"
            )
        if target.spec != destination.sharded_spec:
            raise RuntimeError(
                f"staged destination {meta.name!r} changed geometry after "
                "initialize_layerwise_reload"
            )
        group = cast(int, destination.staging_group)
        group_size = cast(int, destination.staging_group_size)
        if target.retention_group_size != group_size:
            raise RuntimeError(
                f"staged destination {meta.name!r} changed retention group "
                f"size from {group_size} to {target.retention_group_size}"
            )
        key = target.retention_key
        if group in group_to_key and group_to_key[group] != key:
            raise RuntimeError(
                f"staged destination group {group} changed retention owner"
            )
        if key in key_to_group and key_to_group[key] != group:
            raise RuntimeError(
                f"retention owner for {meta.name!r} spans planned groups "
                f"{key_to_group[key]} and {group}"
            )
        group_to_key[group] = key
        key_to_group[key] = group
        self._staged_targets[index] = target

_discard_update()

Release only update-local staging after a failed update.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _discard_update(self) -> None:
    """Release only update-local staging after a failed update."""
    self._clear_update_bindings()
    if self._staging_pool is not None:
        self._staging_pool.discard_update()

_prepare_destination_plan(*, local_worker_rank, num_workers, dst_mesh_dims)

Resolve and validate this worker's destination plan.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _prepare_destination_plan(
    self,
    *,
    local_worker_rank: int,
    num_workers: int,
    dst_mesh_dims: tuple[int, int] | None,
) -> None:
    """Resolve and validate this worker's destination plan."""
    if not 0 <= local_worker_rank < num_workers:
        raise ValueError(
            f"local worker rank {local_worker_rank} must be in [0, {num_workers})"
        )
    # A quantized weight loader may dequantize / transpose / requantize, and
    # under PP a rank's local shape is not the checkpoint shape split over
    # the shard axis. Both disable raw and semantic sharded destinations, so
    # take the full-tensor fallback.
    allow_direct = (
        getattr(self.model_config, "quantization", None) is None
        and self.parallel_config.pipeline_parallel_size == 1
    )
    # Match every incoming checkpoint parameter to its rank-local target.
    # Resolvable parameters can be resharded directly into live model
    # storage; the rest receive a full tensor for `load_weights`.
    self._parameter_destinations = resolve_parameter_destinations(
        self.model,
        [m.name for m in self._metas],
        [m.dtype for m in self._metas],
        [m.shape for m in self._metas],
        num_workers=num_workers,
        local_worker_rank=local_worker_rank,
        dst_mesh_dims=dst_mesh_dims,
        allow_direct=allow_direct,
        allow_full_fallback=[meta.allow_full_fallback for meta in self._metas],
    )
    self._uses_load_weights = any(
        destination.mode is not M2NDestinationMode.IN_PLACE
        for destination in self._parameter_destinations
    )
    requirements = [
        M2NStagingRequirement(
            group=cast(int, destination.staging_group),
            slot=cast(int, destination.staging_slot),
            dtype=meta.dtype,
            shape=destination.local_shape,
        )
        for meta, destination in zip(self._metas, self._parameter_destinations)
        if destination.mode is M2NDestinationMode.SHARDED_STAGING
    ]
    self._staging_pool = (
        M2NStagingPool(requirements, self.device) if requirements else None
    )

_prepare_source_plan(wire_params, source_topology)

Validate worker-visible source metadata without constructing NCCL.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _prepare_source_plan(
    self,
    wire_params: tuple[M2NWireParam, ...],
    source_topology: M2NSourceTopology,
) -> None:
    """Validate worker-visible source metadata without constructing NCCL."""
    self._metas = []
    for param_index, param in enumerate(wire_params):
        dtype = getattr(torch, param.dtype_name, None)
        if not isinstance(dtype, torch.dtype):
            raise ValueError(
                f"parameter '{param.name}' has invalid dtype name "
                f"'{param.dtype_name}'; expected the name of a torch.dtype"
            )
        check_transferable(param.name, dtype, param.shape)
        layout = M2NLayout(M2NMesh(param.src_mesh_dims, 0), param.src_placements)
        group_index = source_topology.param_groups[param_index]
        source_group = source_topology.groups[group_index]
        if layout.mesh.size != len(source_group):
            raise ValueError(
                f"parameter '{param.name}' source mesh covers "
                f"{layout.mesh.size} ranks, but source group {group_index} "
                f"contains {len(source_group)} ranks"
            )
        meta = M2NParamMeta(
            param.name,
            dtype,
            param.shape,
            layout,
            param.allow_full_fallback,
        )
        expected_local_shape(meta)
        self._metas.append(meta)

_reset_failed_update()

Reset loader and staging state without touching M2N transport.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _reset_failed_update(self) -> None:
    """Reset loader and staging state without touching M2N transport."""
    if self._uses_load_weights:
        from vllm.model_executor.model_loader.reload import (
            abort_layerwise_reload,
        )

        # Loader callbacks can enqueue reads from retained staging buffers.
        # Finish them before restoring tensors and releasing those buffers.
        torch.accelerator.synchronize()
        abort_layerwise_reload(self.model)
    self._discard_update()

_reshard(comm, stream, src_meta, destination, dst_buffer)

Reshard one parameter from its trainer layout into a worker buffer.

destination carries this parameter's agreed mesh and placement; dst_buffer is this rank's physical storage.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _reshard(
    self,
    comm: Any,
    stream: torch.cuda.Stream,
    src_meta: M2NParamMeta,
    destination: M2NDestination,
    dst_buffer: torch.Tensor,
) -> None:
    """Reshard one parameter from its trainer layout into a worker buffer.

    ``destination`` carries this parameter's agreed mesh and placement;
    ``dst_buffer`` is this rank's physical storage.
    """
    m2n = self._m2n
    src_mesh, resolved_src_placements = resolve_layout(
        src_meta.source_layout.mesh, src_meta.source_layout.placements
    )
    source_group_size = len(self._source_topology.groups[0])
    dst_mesh, resolved_dst_placements = resolve_layout(
        M2NMesh(destination.mesh_dims, source_group_size),
        destination.placements,
    )
    m2n.reshard(
        None,
        dst_buffer,
        comm,
        stream,
        src_mesh=to_mesh(m2n, src_mesh),
        src_placements=to_placements(m2n, resolved_src_placements),
        dst_mesh=to_mesh(m2n, dst_mesh),
        dst_placements=to_placements(m2n, resolved_dst_placements),
        handle=self._handle,
    )

_resolve_direct_buffer(meta, destination)

Validate the live destination captured before layerwise reload.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _resolve_direct_buffer(
    self, meta: M2NParamMeta, destination: M2NDestination
) -> torch.Tensor:
    """Validate the live destination captured before layerwise reload."""
    parameter_name = destination.name
    buffer = destination.tensor
    if buffer is None:
        raise RuntimeError(f"in-place destination {parameter_name!r} is missing")
    if buffer.dtype != meta.dtype:
        raise RuntimeError(
            f"in-place destination {parameter_name!r} changed dtype from "
            f"{meta.dtype} to {buffer.dtype}"
        )
    if tuple(buffer.shape) != destination.local_shape:
        raise RuntimeError(
            f"in-place destination {parameter_name!r} changed shape from "
            f"{destination.local_shape} to {tuple(buffer.shape)}"
        )
    if not buffer.is_contiguous():
        raise RuntimeError(
            f"in-place destination {parameter_name!r} is not contiguous"
        )
    if buffer.device != self.device:
        raise RuntimeError(
            f"in-place destination {parameter_name!r} is on "
            f"{buffer.device}, expected {self.device}"
        )
    return buffer

_synchronize_route_streams()

Wait for every joined route without assuming a default stream.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _synchronize_route_streams(self) -> None:
    """Wait for every joined route without assuming a default stream."""
    for route_index in range(len(self._routes)):
        stream = self._route_streams.get(route_index)
        if stream is not None:
            stream.synchronize()

finish_weight_update()

Finalize one update, or reset it so the trainer can replay it.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def finish_weight_update(self) -> None:
    """Finalize one update, or reset it so the trainer can replay it."""
    self._require_initialized()
    try:
        if self._next_parameter_index != len(self._metas):
            missing = self._metas[self._next_parameter_index].name
            raise RuntimeError(
                "cannot finish an incomplete nccl_m2n update: received "
                f"{self._next_parameter_index}/{len(self._metas)} "
                f"parameters (next expected: {missing!r})"
            )

        if self._uses_load_weights:
            from vllm.model_executor.model_loader.reload import (
                finalize_layerwise_reload,
            )

            finalize_layerwise_reload(self.model, self.model_config)
            # Complete copies from retained staging inputs before reuse.
            torch.accelerator.synchronize()
        if self._staging_pool is not None:
            self._staging_pool.release_retained_after_finalize()
            self._staging_pool.finish_update()

        self._clear_update_bindings()
    except BaseException as exc:
        try:
            self._reset_failed_update()
        except BaseException as reset_exc:
            raise RuntimeError(
                "nccl_m2n failed to reset vLLM after a finish failure"
            ) from reset_exc
        raise RetryableWeightUpdateError(
            "vLLM failed to finalize the weight update; worker state was "
            "reset so the complete update can be retried"
        ) from exc

init_transfer_engine(init_info)

Agree on source and local readiness before constructing NCCL.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def init_transfer_engine(self, init_info: M2NWeightTransferInitInfo) -> None:
    """Agree on source and local readiness before constructing NCCL."""
    if self._handle is not None or self._model_update_groups:
        raise RuntimeError(
            "nccl_m2n weight transfer is already initialized; call "
            "shutdown_weight_transfer_engine() before initializing again"
        )
    rendezvous_info = NCCLWeightTransferInitInfo(
        master_address=init_info.master_address,
        master_port=init_info.master_port,
        rank_offset=(
            init_info.rank_offset
            if init_info.worker_rank_offset is None
            else init_info.worker_rank_offset
        ),
        world_size=init_info.world_size,
    )
    metadata_group = worker_init_metadata_group(
        rendezvous_info, self.parallel_config
    )

    wire_params: tuple[M2NWireParam, ...] = ()
    source_topology = M2NSourceTopology((), ())
    source_error: str | None = None
    try:
        self._m2n = import_m2n()
        wire_params, source_topology = init_info.parse_source_plan()
        self._source_topology = source_topology
        self._prepare_source_plan(wire_params, source_topology)
        self._routes = build_m2n_transfer_routes(
            source_topology,
            num_trainer_ranks=init_info.rank_offset,
            world_size=init_info.world_size,
            dst_mesh_dims=init_info.destination_mesh_dims,
        )
        self._param_routes = build_m2n_param_routes(self._routes, len(wire_params))
    except Exception as exc:
        source_error = f"{type(exc).__name__}: {exc}"
        self._metas = []
        self._routes = ()
        self._param_routes = ()
    plan_digest = check_source_plan_agreement(
        metadata_group,
        wire_params,
        expected_digest=init_info.source_digest,
        local_error=source_error,
        topology=source_topology,
    )

    num_workers = init_info.world_size - init_info.rank_offset
    agreement_mesh = M2NMesh(init_info.destination_mesh_dims, init_info.rank_offset)

    local_wire_plan: list[M2NWireDestination] | None = None
    destination_error: str | None = None
    try:
        local_worker_rank = metadata_group.rank - init_info.rank_offset
        if not 0 <= local_worker_rank < num_workers:
            raise ValueError(
                f"metadata rank {metadata_group.rank} is outside destination "
                f"ranks [{init_info.rank_offset}, {init_info.world_size})"
            )
        self._prepare_destination_plan(
            local_worker_rank=local_worker_rank,
            num_workers=num_workers,
            dst_mesh_dims=init_info.destination_mesh_constraint,
        )
        local_wire_plan = [
            M2NWireDestination.from_destination(
                destination,
                dtype_name=wire_param.dtype_name,
                local_worker_rank=local_worker_rank,
            )
            for wire_param, destination in zip(
                wire_params, self._parameter_destinations
            )
        ]
    except Exception as exc:
        destination_error = f"{type(exc).__name__}: {exc}"
        self._clear_destination_plan()

    try:
        agree_destination_plan(
            metadata_group,
            source_digest=plan_digest,
            params=wire_params,
            source_topology=source_topology,
            metas=self._metas,
            dst_mesh=agreement_mesh,
            dst_mesh_constraint=init_info.destination_mesh_constraint,
            local_plan=local_wire_plan,
            local_error=destination_error,
        )
        unique_id_bytes: tuple[bytes | None, ...] = ()
        data_plane_error: str | None = None
        try:
            unique_id_bytes = init_info.nccl_unique_id_bytes_by_route(
                len(self._routes)
            )
            check_multi_communicator_launch_order(len(self._routes))
        except Exception as exc:
            data_plane_error = f"{type(exc).__name__}: {exc}"
        check_data_plane_agreement(
            metadata_group,
            unique_id_bytes,
            data_plane_error,
        )
    except BaseException:
        self._clear_destination_plan()
        raise
    handle = None
    groups: dict[int, Any] = {}
    route_streams: dict[int, torch.cuda.Stream] = {}
    try:
        runtime_error = destination_error
        nccl = None
        device = None
        if runtime_error is None:
            try:
                nccl, device, handle = prepare_m2n_local_runtime(
                    self._m2n, init_info.max_cta
                )
            except Exception as exc:
                runtime_error = f"{type(exc).__name__}: {exc}"
        check_runtime_ready_agreement(metadata_group, runtime_error)
        assert nccl is not None and device is not None and handle is not None
        communicator_errors: list[str | None] = []
        for route_index, (route, route_uid) in enumerate(
            zip(self._routes, unique_id_bytes, strict=True)
        ):
            route_error: str | None = None
            try:
                group = create_m2n_communicator(
                    nccl,
                    metadata_group,
                    route_uid,
                    members=route.members,
                )
                if group is None:
                    raise RuntimeError(
                        "inference worker is missing from transfer route "
                        f"{route_index}"
                    )
                groups[route_index] = group
                route_streams[route_index] = torch.cuda.Stream(device=device)
            except Exception as exc:
                route_error = f"{type(exc).__name__}: {exc}"
            communicator_errors.append(route_error)
        check_communicator_init_agreement(
            metadata_group,
            communicator_errors,
        )
    except BaseException:
        for route_group in groups.values():
            try:
                route_group.abort()
            except BaseException:
                logger.exception(
                    "failed to abort partially initialized nccl_m2n communicator"
                )
        if handle is not None:
            try:
                handle.destroy()
            except BaseException:
                logger.exception(
                    "failed to destroy partially initialized nccl_m2n handle"
                )
        self._clear_destination_plan()
        self._routes = ()
        self._param_routes = ()
        self._source_topology = M2NSourceTopology((), ())
        raise

    self._handle = handle
    self._model_update_groups = groups
    self._route_streams = route_streams
    # Retain the scalar alias for single-communicator callers and diagnostics.
    self.model_update_group = groups.get(0)

receive_weights(update_info)

Receive one exact contiguous slice of the immutable transfer plan.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def receive_weights(self, update_info: M2NWeightTransferUpdateInfo) -> None:
    """Receive one exact contiguous slice of the immutable transfer plan."""
    self._require_initialized()
    self._receive_weights(update_info)

shutdown()

Finish pending GPU work and release M2N communication state.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def shutdown(self) -> None:
    """Finish pending GPU work and release M2N communication state."""
    handle, self._handle = self._handle, None
    groups, self._model_update_groups = self._model_update_groups, {}
    self.model_update_group = None
    streams, self._route_streams = self._route_streams, {}
    self._routes = ()
    self._param_routes = ()
    self._source_topology = M2NSourceTopology((), ())
    self._m2n = None
    self._metas = []
    self._clear_destination_plan()

    first_error: BaseException | None = None

    def attempt(action: Any, message: str) -> None:
        nonlocal first_error
        try:
            action()
        except BaseException as exc:
            if first_error is None:
                first_error = exc
            logger.exception(message)

    if handle is not None or groups:
        # M2N does not synchronize caller streams on finalize.
        attempt(torch.accelerator.synchronize, "failed to synchronize nccl_m2n")
    if handle is not None:
        attempt(handle.destroy, "failed to destroy nccl_m2n handle")
    for group_index, group in groups.items():
        attempt(
            group.abort,
            f"failed to abort nccl_m2n route {group_index} communicator",
        )
    # Keep stream wrappers alive through communicator cleanup.
    del streams
    if first_error is not None:
        raise first_error

start_weight_update()

Enter one update and bind semantic callbacks to this generation.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def start_weight_update(self) -> None:
    """Enter one update and bind semantic callbacks to this generation."""
    self._require_initialized()
    try:
        if self._staging_pool is not None:
            self._staging_pool.start_update()
        if self._uses_load_weights:
            from vllm.model_executor.model_loader.reload import (
                initialize_layerwise_reload,
            )

            initialize_layerwise_reload(self.model)
        self._bind_staged_targets()
    except BaseException:
        try:
            self._reset_failed_update()
        except BaseException:
            logger.exception("failed to reset nccl_m2n update state")
        raise

update_weights(update_info)

Preserve pre-transfer failures; abort failures after transfer starts.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def update_weights(self, update_info: dict[str, Any]) -> None:
    """Preserve pre-transfer failures; abort failures after transfer starts."""
    self._require_initialized()
    try:
        typed_update_info = self.parse_update_info(update_info)
        self.receive_weights(typed_update_info)
    except BaseException:
        if self._handle is not None:
            self._reset_failed_update()
        raise
    try:
        torch.accelerator.synchronize()
    except BaseException:
        self._abort_data_plane()
        raise

M2NWeightTransferInitInfo dataclass

Bases: WeightTransferInitInfo

Worker-side init info: the rendezvous plus the full transfer plan.

Layouts are static for the whole run, so they ride the one-time init handshake and the per-round update info stays a list of names. Everything is plain JSON so the HTTP control plane carries it unchanged.

Methods:

Attributes:

Source code in vllm/distributed/weight_transfer/m2n_engine.py
@dataclass(kw_only=True)
class M2NWeightTransferInitInfo(WeightTransferInitInfo):
    """Worker-side init info: the rendezvous plus the full transfer plan.

    Layouts are static for the whole run, so they ride the one-time init
    handshake and the per-round update info stays a list of names. Everything is
    plain JSON so the HTTP control plane carries it unchanged.
    """

    schema_version: int
    master_address: str
    master_port: int
    rank_offset: int
    """First worker rank, i.e. the number of trainer ranks."""
    world_size: int
    """Trainer ranks + all inference workers."""
    dst_mesh_dims: list[int] | None = None
    """Optional fixed inference mesh; ``None`` selects per-parameter meshes."""
    source_digest: str
    """SHA-256 of the ordered, versioned source parameter plan."""
    params: list[dict[str, Any]]
    """Ordered per-parameter source metadata and layouts."""
    source_topology: dict[str, Any] | None = None
    """Ordered source rank groups and one owning group per parameter.

    Omitted payloads retain the legacy single-source-group topology: all
    trainer ranks own the complete ordered manifest.
    """
    worker_rank_offset: int | None = None
    """Deployment-local worker-rank offset.

    ``rank_offset`` remains the common start rank of the complete destination
    mesh. Multi-deployment inference sets this field to the start rank of one
    deployment so vLLM's deployment-local DP ranks remain globally unique. A
    single deployment may leave it unset.
    """
    nccl_unique_id_b64: str | None = field(default=None, repr=False)
    """Optional single-communicator UID retained for compatibility."""
    nccl_unique_ids_b64: list[str] | None = field(default=None, repr=False)
    """Optional UIDs in canonical transfer-route order."""
    max_cta: int | None = None

    def __post_init__(self) -> None:
        if not isinstance(self.master_address, str) or not self.master_address:
            raise ValueError("`master_address` must name the metadata rendezvous")
        if (
            not isinstance(self.master_port, int)
            or isinstance(self.master_port, bool)
            or not 0 < self.master_port < 65536
        ):
            raise ValueError("`master_port` must be a valid metadata port")
        if (
            not isinstance(self.rank_offset, int)
            or isinstance(self.rank_offset, bool)
            or not isinstance(self.world_size, int)
            or isinstance(self.world_size, bool)
            or self.rank_offset < 1
            or self.rank_offset >= self.world_size
        ):
            raise ValueError(
                f"`rank_offset` ({self.rank_offset}) must leave at least one "
                f"trainer rank and one worker in world_size {self.world_size}"
            )
        if self.worker_rank_offset is not None and (
            not isinstance(self.worker_rank_offset, int)
            or isinstance(self.worker_rank_offset, bool)
            or not self.rank_offset <= self.worker_rank_offset < self.world_size
        ):
            raise ValueError(
                "`worker_rank_offset` must lie inside the common destination "
                f"rank interval [{self.rank_offset}, {self.world_size}); got "
                f"{self.worker_rank_offset}"
            )
        if self.dst_mesh_dims is not None:
            dst = tuple(self.dst_mesh_dims)
            try:
                dst_mesh = M2NMesh(cast(tuple[int, int], dst), self.rank_offset)
            except (TypeError, ValueError) as exc:
                raise ValueError(f"invalid `dst_mesh_dims` {dst}: {exc}") from exc
            num_workers = self.world_size - self.rank_offset
            if dst_mesh.size != num_workers:
                raise ValueError(
                    f"`dst_mesh_dims` {dst} must cover the {num_workers} "
                    "inference workers"
                )

    @property
    def destination_mesh_dims(self) -> tuple[int, int]:
        """Concrete mesh used to describe the destination rank interval."""
        if self.dst_mesh_dims is not None:
            return cast(tuple[int, int], tuple(self.dst_mesh_dims))
        return (self.world_size - self.rank_offset, 1)

    @property
    def destination_mesh_constraint(self) -> tuple[int, int] | None:
        """Fixed mesh constraint, or ``None`` for per-parameter planning."""
        if self.dst_mesh_dims is None:
            return None
        return cast(tuple[int, int], tuple(self.dst_mesh_dims))

    @property
    def nccl_unique_id_bytes(self) -> bytes | None:
        if self.nccl_unique_id_b64 is None:
            return None
        return decode_nccl_unique_id(
            master_address=None,
            master_port=None,
            nccl_unique_id_b64=self.nccl_unique_id_b64,
            ctx="M2NWeightTransferInitInfo data plane",
        )

    def nccl_unique_id_bytes_by_route(
        self, num_routes: int
    ) -> tuple[bytes | None, ...]:
        """Decode the optional pre-shared UID table for canonical routes."""
        if (
            not isinstance(num_routes, int)
            or isinstance(num_routes, bool)
            or num_routes <= 0
        ):
            raise ValueError("num_routes must be a positive integer")
        if self.nccl_unique_id_b64 is not None and self.nccl_unique_ids_b64 is not None:
            raise ValueError(
                "set only one of `nccl_unique_id_b64` and `nccl_unique_ids_b64`"
            )
        if self.nccl_unique_ids_b64 is not None:
            if (
                not isinstance(self.nccl_unique_ids_b64, list)
                or len(self.nccl_unique_ids_b64) != num_routes
            ):
                raise ValueError(
                    "`nccl_unique_ids_b64` must contain exactly one UID per "
                    f"transfer route ({num_routes})"
                )
            return tuple(
                decode_nccl_unique_id(
                    master_address=None,
                    master_port=None,
                    nccl_unique_id_b64=value,
                    ctx=f"M2NWeightTransferInitInfo route {route_index} data plane",
                )
                for route_index, value in enumerate(self.nccl_unique_ids_b64)
            )
        if self.nccl_unique_id_b64 is not None:
            if num_routes != 1:
                raise ValueError(
                    "`nccl_unique_id_b64` is valid only for a single transfer route; "
                    "use `nccl_unique_ids_b64` for pipeline groups"
                )
            return (self.nccl_unique_id_bytes,)
        return (None,) * num_routes

    def parse_source_plan(
        self,
    ) -> tuple[tuple[M2NWireParam, ...], M2NSourceTopology]:
        """Parse and authenticate parameters together with source routing."""
        params = self._parse_wire_params_unchecked()
        if self.source_topology is None:
            topology = M2NSourceTopology.single_group(self.rank_offset, len(params))
        else:
            if not isinstance(self.source_topology, Mapping):
                raise TypeError("`source_topology` must be a mapping")
            topology = M2NSourceTopology.from_dict(self.source_topology)
        topology.validate(
            num_trainer_ranks=self.rank_offset,
            num_params=len(params),
        )
        computed_digest = source_plan_digest(params, topology)
        if self.source_digest != computed_digest:
            raise ValueError(
                "nccl_m2n init source digest does not match params/topology: "
                f"computed {computed_digest}, declared {self.source_digest}"
            )
        return params, topology

    def parse_wire_params(self) -> tuple[M2NWireParam, ...]:
        """Compatibility view of :meth:`parse_source_plan`."""
        return self.parse_source_plan()[0]

    def _parse_wire_params_unchecked(self) -> tuple[M2NWireParam, ...]:
        """Parse the ordered parameter records before topology authentication."""
        if self.schema_version != M2N_WIRE_SCHEMA_VERSION:
            raise ValueError(
                "unsupported nccl_m2n init schema version "
                f"{self.schema_version}; expected {M2N_WIRE_SCHEMA_VERSION}"
            )
        if not isinstance(self.params, list):
            raise TypeError("`params` must be a list of wire parameter mappings")
        if not isinstance(self.source_digest, str) or not self.source_digest:
            raise ValueError("`source_digest` must be a non-empty string")
        wire_params: list[M2NWireParam] = []
        names: set[str] = set()
        for index, value in enumerate(self.params):
            if not isinstance(value, Mapping):
                raise TypeError(
                    f"`params[{index}]` must be a mapping, got {type(value).__name__}"
                )
            param = M2NWireParam.from_dict(value)
            if param.name in names:
                raise ValueError(
                    f"nccl_m2n init params contain duplicate parameter '{param.name}'"
                )
            names.add(param.name)
            wire_params.append(param)
        parsed = tuple(wire_params)
        return parsed

destination_mesh_constraint property

Fixed mesh constraint, or None for per-parameter planning.

destination_mesh_dims property

Concrete mesh used to describe the destination rank interval.

dst_mesh_dims = None class-attribute instance-attribute

Optional fixed inference mesh; None selects per-parameter meshes.

nccl_unique_id_b64 = field(default=None, repr=False) class-attribute instance-attribute

Optional single-communicator UID retained for compatibility.

nccl_unique_ids_b64 = field(default=None, repr=False) class-attribute instance-attribute

Optional UIDs in canonical transfer-route order.

params instance-attribute

Ordered per-parameter source metadata and layouts.

rank_offset instance-attribute

First worker rank, i.e. the number of trainer ranks.

source_digest instance-attribute

SHA-256 of the ordered, versioned source parameter plan.

source_topology = None class-attribute instance-attribute

Ordered source rank groups and one owning group per parameter.

Omitted payloads retain the legacy single-source-group topology: all trainer ranks own the complete ordered manifest.

worker_rank_offset = None class-attribute instance-attribute

Deployment-local worker-rank offset.

rank_offset remains the common start rank of the complete destination mesh. Multi-deployment inference sets this field to the start rank of one deployment so vLLM's deployment-local DP ranks remain globally unique. A single deployment may leave it unset.

world_size instance-attribute

Trainer ranks + all inference workers.

_parse_wire_params_unchecked()

Parse the ordered parameter records before topology authentication.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def _parse_wire_params_unchecked(self) -> tuple[M2NWireParam, ...]:
    """Parse the ordered parameter records before topology authentication."""
    if self.schema_version != M2N_WIRE_SCHEMA_VERSION:
        raise ValueError(
            "unsupported nccl_m2n init schema version "
            f"{self.schema_version}; expected {M2N_WIRE_SCHEMA_VERSION}"
        )
    if not isinstance(self.params, list):
        raise TypeError("`params` must be a list of wire parameter mappings")
    if not isinstance(self.source_digest, str) or not self.source_digest:
        raise ValueError("`source_digest` must be a non-empty string")
    wire_params: list[M2NWireParam] = []
    names: set[str] = set()
    for index, value in enumerate(self.params):
        if not isinstance(value, Mapping):
            raise TypeError(
                f"`params[{index}]` must be a mapping, got {type(value).__name__}"
            )
        param = M2NWireParam.from_dict(value)
        if param.name in names:
            raise ValueError(
                f"nccl_m2n init params contain duplicate parameter '{param.name}'"
            )
        names.add(param.name)
        wire_params.append(param)
    parsed = tuple(wire_params)
    return parsed

nccl_unique_id_bytes_by_route(num_routes)

Decode the optional pre-shared UID table for canonical routes.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def nccl_unique_id_bytes_by_route(
    self, num_routes: int
) -> tuple[bytes | None, ...]:
    """Decode the optional pre-shared UID table for canonical routes."""
    if (
        not isinstance(num_routes, int)
        or isinstance(num_routes, bool)
        or num_routes <= 0
    ):
        raise ValueError("num_routes must be a positive integer")
    if self.nccl_unique_id_b64 is not None and self.nccl_unique_ids_b64 is not None:
        raise ValueError(
            "set only one of `nccl_unique_id_b64` and `nccl_unique_ids_b64`"
        )
    if self.nccl_unique_ids_b64 is not None:
        if (
            not isinstance(self.nccl_unique_ids_b64, list)
            or len(self.nccl_unique_ids_b64) != num_routes
        ):
            raise ValueError(
                "`nccl_unique_ids_b64` must contain exactly one UID per "
                f"transfer route ({num_routes})"
            )
        return tuple(
            decode_nccl_unique_id(
                master_address=None,
                master_port=None,
                nccl_unique_id_b64=value,
                ctx=f"M2NWeightTransferInitInfo route {route_index} data plane",
            )
            for route_index, value in enumerate(self.nccl_unique_ids_b64)
        )
    if self.nccl_unique_id_b64 is not None:
        if num_routes != 1:
            raise ValueError(
                "`nccl_unique_id_b64` is valid only for a single transfer route; "
                "use `nccl_unique_ids_b64` for pipeline groups"
            )
        return (self.nccl_unique_id_bytes,)
    return (None,) * num_routes

parse_source_plan()

Parse and authenticate parameters together with source routing.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def parse_source_plan(
    self,
) -> tuple[tuple[M2NWireParam, ...], M2NSourceTopology]:
    """Parse and authenticate parameters together with source routing."""
    params = self._parse_wire_params_unchecked()
    if self.source_topology is None:
        topology = M2NSourceTopology.single_group(self.rank_offset, len(params))
    else:
        if not isinstance(self.source_topology, Mapping):
            raise TypeError("`source_topology` must be a mapping")
        topology = M2NSourceTopology.from_dict(self.source_topology)
    topology.validate(
        num_trainer_ranks=self.rank_offset,
        num_params=len(params),
    )
    computed_digest = source_plan_digest(params, topology)
    if self.source_digest != computed_digest:
        raise ValueError(
            "nccl_m2n init source digest does not match params/topology: "
            f"computed {computed_digest}, declared {self.source_digest}"
        )
    return params, topology

parse_wire_params()

Compatibility view of :meth:parse_source_plan.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
def parse_wire_params(self) -> tuple[M2NWireParam, ...]:
    """Compatibility view of :meth:`parse_source_plan`."""
    return self.parse_source_plan()[0]

M2NWeightTransferUpdateInfo dataclass

Bases: WeightTransferUpdateInfo

Per-round update info: which parameters this chunk carries, in order.

Shapes, dtypes and layouts were fixed at init, so a round only needs to say what is coming and in what order — both sides must issue their reshards in exactly that order.

Source code in vllm/distributed/weight_transfer/m2n_engine.py
@dataclass
class M2NWeightTransferUpdateInfo(WeightTransferUpdateInfo):
    """Per-round update info: which parameters this chunk carries, in order.

    Shapes, dtypes and layouts were fixed at init, so a round only needs to say
    what is coming and in what order — both sides must issue their reshards in
    exactly that order.
    """

    names: list[str]