Skip to content

vllm.distributed.weight_transfer.m2n_trainer

Trainer-side weight transfer engine for the NCCL M2N backend.

Symmetric to M2NWeightTransferEngine but in the training process. Each trainer rank joins only the source-group communicators that own its parameters and sends those rank-local shards; only rank 0 touches the inference control plane.

Workers join the communicator during init and run reshard from inside update_weights, so those two RPCs overlap with work on this side. Both run on a helper thread and are joined afterwards, which keeps that overlap inside the engine -- where the TrainerWeightTransferEngine contract puts it -- rather than in every caller.

Classes:

M2NTrainerInitInfo dataclass

Bases: TrainerInitInfo

Trainer-side init info for nccl_m2n.

rank (from TrainerInitInfo) is this trainer process's global metadata rank. Each transfer route maps participating trainer ranks into its own communicator-local source order. Rank 0 drives the control plane.

Methods:

  • __post_init__ –

    Reject rank counts or destination meshes that cannot form a group.

  • data_plane_unique_ids –

    Decode one optional pre-shared NCCL UID per canonical route.

Attributes:

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
@dataclass(kw_only=True)
class M2NTrainerInitInfo(TrainerInitInfo):
    """Trainer-side init info for nccl_m2n.

    `rank` (from `TrainerInitInfo`) is this trainer process's global metadata
    rank. Each transfer route maps participating trainer ranks into its own
    communicator-local source order. Rank 0 drives the control plane.
    """

    backend: ClassVar[str] = "nccl_m2n"

    master_address: str
    master_port: int
    world_size: int
    """Trainer ranks + all inference workers."""
    num_trainer_ranks: int = 1
    dst_mesh_dims: tuple[int, int] | None = None
    """Optional fixed inference mesh. Omitted keeps the legacy flat mesh unless
    ``auto_dst_mesh`` is enabled."""
    auto_dst_mesh: bool = False
    """Resolve a destination mesh per parameter instead of fixing one mesh."""
    nccl_unique_id_b64: str | None = field(default=None, repr=False)
    """Optional UID for the NCCL data plane; TCP remains the metadata plane."""
    nccl_unique_ids_b64: list[str] | None = field(default=None, repr=False)
    """Optional UIDs in canonical transfer-route order."""
    max_cta: int | None = None
    listen_socket: socket.socket | None = field(default=None, repr=False, compare=False)
    """Optional rank-zero TCPStore reservation; never sent to 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.num_trainer_ranks, 1)

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

    def __post_init__(self) -> None:
        """Reject rank counts or destination meshes that cannot form a group."""
        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 0 < self.num_trainer_ranks < self.world_size:
            raise ValueError(
                f"`num_trainer_ranks` ({self.num_trainer_ranks}) must leave at "
                f"least one worker in world_size {self.world_size}"
            )
        num_workers = self.world_size - self.num_trainer_ranks
        if not isinstance(self.auto_dst_mesh, bool):
            raise TypeError("`auto_dst_mesh` must be a bool")
        if self.auto_dst_mesh and self.dst_mesh_dims is not None:
            raise ValueError(
                "`auto_dst_mesh=True` cannot be combined with `dst_mesh_dims`"
            )
        if self.dst_mesh_dims is not None:
            dims = tuple(self.dst_mesh_dims)
            try:
                mesh = M2NMesh(cast(tuple[int, int], dims), self.num_trainer_ranks)
            except (TypeError, ValueError) as exc:
                raise ValueError(f"invalid `dst_mesh_dims` {dims}: {exc}") from exc
            if mesh.size != num_workers:
                raise ValueError(
                    f"`dst_mesh_dims` {dims} must be two dims covering the "
                    f"{num_workers} inference workers"
                )
        if not 0 <= self.rank < self.num_trainer_ranks:
            raise ValueError(
                f"trainer `rank` ({self.rank}) must be below "
                f"`num_trainer_ranks` ({self.num_trainer_ranks})"
            )
        if self.listen_socket is not None:
            if self.rank != 0:
                raise ValueError("only trainer rank zero may reserve the TCPStore")
            if not isinstance(self.listen_socket, socket.socket):
                raise TypeError("listen_socket must be a socket.socket")
            if self.listen_socket.fileno() < 0:
                raise ValueError("listen_socket is already closed")
        if self.nccl_unique_id_b64 is not None and self.nccl_unique_ids_b64 is not None:
            raise ValueError(
                "pass `nccl_unique_id_b64` or `nccl_unique_ids_b64`, not both"
            )
        if self.nccl_unique_ids_b64 is not None:
            if (
                not isinstance(self.nccl_unique_ids_b64, list)
                or not self.nccl_unique_ids_b64
            ):
                raise ValueError("`nccl_unique_ids_b64` must be a non-empty list")
            if any(
                not isinstance(value, str) or not value
                for value in self.nccl_unique_ids_b64
            ):
                raise ValueError(
                    "every entry in `nccl_unique_ids_b64` must be a non-empty string"
                )

    @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="M2NTrainerInitInfo data plane",
        )

    def data_plane_unique_ids(self, num_routes: int) -> tuple[bytes | None, ...]:
        """Decode one optional pre-shared NCCL UID per canonical route."""
        if (
            not isinstance(num_routes, int)
            or isinstance(num_routes, bool)
            or num_routes <= 0
        ):
            raise ValueError("nccl_m2n needs at least one transfer route")
        if self.nccl_unique_ids_b64 is not None:
            if len(self.nccl_unique_ids_b64) != num_routes:
                raise ValueError(
                    f"`nccl_unique_ids_b64` has {len(self.nccl_unique_ids_b64)} "
                    f"entries, expected {num_routes} transfer routes"
                )
            return tuple(
                decode_nccl_unique_id(
                    master_address=None,
                    master_port=None,
                    nccl_unique_id_b64=value,
                    ctx=f"M2NTrainerInitInfo data plane route {route_index}",
                )
                for route_index, value in enumerate(self.nccl_unique_ids_b64)
            )
        unique_id = self.nccl_unique_id_bytes
        if unique_id is not None:
            if num_routes != 1:
                raise ValueError(
                    "singular `nccl_unique_id_b64` is valid only for one transfer "
                    "route; pass `nccl_unique_ids_b64` for pipeline groups"
                )
            return (unique_id,)
        return (None,) * num_routes

auto_dst_mesh = False class-attribute instance-attribute

Resolve a destination mesh per parameter instead of fixing one mesh.

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. Omitted keeps the legacy flat mesh unless auto_dst_mesh is enabled.

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

Optional rank-zero TCPStore reservation; never sent to workers.

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

Optional UID for the NCCL data plane; TCP remains the metadata plane.

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

Optional UIDs in canonical transfer-route order.

world_size instance-attribute

Trainer ranks + all inference workers.

__post_init__()

Reject rank counts or destination meshes that cannot form a group.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def __post_init__(self) -> None:
    """Reject rank counts or destination meshes that cannot form a group."""
    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 0 < self.num_trainer_ranks < self.world_size:
        raise ValueError(
            f"`num_trainer_ranks` ({self.num_trainer_ranks}) must leave at "
            f"least one worker in world_size {self.world_size}"
        )
    num_workers = self.world_size - self.num_trainer_ranks
    if not isinstance(self.auto_dst_mesh, bool):
        raise TypeError("`auto_dst_mesh` must be a bool")
    if self.auto_dst_mesh and self.dst_mesh_dims is not None:
        raise ValueError(
            "`auto_dst_mesh=True` cannot be combined with `dst_mesh_dims`"
        )
    if self.dst_mesh_dims is not None:
        dims = tuple(self.dst_mesh_dims)
        try:
            mesh = M2NMesh(cast(tuple[int, int], dims), self.num_trainer_ranks)
        except (TypeError, ValueError) as exc:
            raise ValueError(f"invalid `dst_mesh_dims` {dims}: {exc}") from exc
        if mesh.size != num_workers:
            raise ValueError(
                f"`dst_mesh_dims` {dims} must be two dims covering the "
                f"{num_workers} inference workers"
            )
    if not 0 <= self.rank < self.num_trainer_ranks:
        raise ValueError(
            f"trainer `rank` ({self.rank}) must be below "
            f"`num_trainer_ranks` ({self.num_trainer_ranks})"
        )
    if self.listen_socket is not None:
        if self.rank != 0:
            raise ValueError("only trainer rank zero may reserve the TCPStore")
        if not isinstance(self.listen_socket, socket.socket):
            raise TypeError("listen_socket must be a socket.socket")
        if self.listen_socket.fileno() < 0:
            raise ValueError("listen_socket is already closed")
    if self.nccl_unique_id_b64 is not None and self.nccl_unique_ids_b64 is not None:
        raise ValueError(
            "pass `nccl_unique_id_b64` or `nccl_unique_ids_b64`, not both"
        )
    if self.nccl_unique_ids_b64 is not None:
        if (
            not isinstance(self.nccl_unique_ids_b64, list)
            or not self.nccl_unique_ids_b64
        ):
            raise ValueError("`nccl_unique_ids_b64` must be a non-empty list")
        if any(
            not isinstance(value, str) or not value
            for value in self.nccl_unique_ids_b64
        ):
            raise ValueError(
                "every entry in `nccl_unique_ids_b64` must be a non-empty string"
            )

data_plane_unique_ids(num_routes)

Decode one optional pre-shared NCCL UID per canonical route.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def data_plane_unique_ids(self, num_routes: int) -> tuple[bytes | None, ...]:
    """Decode one optional pre-shared NCCL UID per canonical route."""
    if (
        not isinstance(num_routes, int)
        or isinstance(num_routes, bool)
        or num_routes <= 0
    ):
        raise ValueError("nccl_m2n needs at least one transfer route")
    if self.nccl_unique_ids_b64 is not None:
        if len(self.nccl_unique_ids_b64) != num_routes:
            raise ValueError(
                f"`nccl_unique_ids_b64` has {len(self.nccl_unique_ids_b64)} "
                f"entries, expected {num_routes} transfer routes"
            )
        return tuple(
            decode_nccl_unique_id(
                master_address=None,
                master_port=None,
                nccl_unique_id_b64=value,
                ctx=f"M2NTrainerInitInfo data plane route {route_index}",
            )
            for route_index, value in enumerate(self.nccl_unique_ids_b64)
        )
    unique_id = self.nccl_unique_id_bytes
    if unique_id is not None:
        if num_routes != 1:
            raise ValueError(
                "singular `nccl_unique_id_b64` is valid only for one transfer "
                "route; pass `nccl_unique_ids_b64` for pipeline groups"
            )
        return (unique_id,)
    return (None,) * num_routes

M2NTrainerWeightTransferEngine

Bases: TrainerWeightTransferEngine[M2NTrainerInitInfo]

Send source-group-local shards over their canonical transfer routes.

Called on every trainer rank. Ranks join only routes whose source group contains them; only rank 0 touches the inference control plane.

Methods:

  • __init__ –

    Hold the client and source; trainer_init fills in the group.

  • plan_summary –

    Return the authenticated static plan as JSON-safe run metadata.

  • send_weights –

    Drive one update, replaying it once after a reset finish failure.

  • shutdown –

    Finish pending GPU work and release M2N communication state.

  • trainer_init –

    Build the engine and rendezvous with the inference side.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 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
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
class M2NTrainerWeightTransferEngine(TrainerWeightTransferEngine[M2NTrainerInitInfo]):
    """Send source-group-local shards over their canonical transfer routes.

    Called on every trainer rank. Ranks join only routes whose source group
    contains them; only rank 0 touches the inference control plane.
    """

    init_info_cls = M2NTrainerInitInfo

    def __init__(
        self,
        *,
        client: VLLMWeightSyncClient,
        source: M2NWeightSource,
        is_sender: bool,
    ) -> None:
        """Hold the client and source; `trainer_init` fills in the group."""
        super().__init__(client=client, source=source, is_sender=is_sender)
        self.group: Any = None
        self._m2n: Any = None
        self._handle: Any = None
        self._metas: list[M2NParamMeta] = []
        self._wire_params: list[M2NWireParam] = []
        self._source_digest = ""
        self._destination_commit_token = ""
        self._destination_mesh_constraint: tuple[int, int] | None = None
        self._metadata_group: StatelessProcessGroup | None = None
        self._num_trainer_ranks = 0
        self._trainer_rank = -1
        self._destination_plan: list[M2NWireDestination] = []
        self._source_topology: M2NSourceTopology | None = None
        self._routes: tuple[M2NTransferRoute, ...] = ()
        self._param_routes: tuple[int, ...] = ()
        self._transfer_waves: tuple[M2NTransferWave, ...] = ()
        self._route_communicators: dict[int, Any] = {}
        self._route_streams: dict[int, torch.cuda.Stream] = {}
        self._round_id = 0

    @classmethod
    def trainer_init(
        cls,
        init_info: M2NTrainerInitInfo,
        *,
        client: VLLMWeightSyncClient,
        source: WeightSource | None = None,
    ) -> Self:
        """Build the engine and rendezvous with the inference side.

        Runs on *every* trainer rank. Rank 0 additionally ships the transfer
        plan to the workers and drives the control plane; the other ranks only
        build local state and join their declared source-group communicators.

        The ordering here is not arbitrary -- see the comments inline. Build a
        local validation envelope, start the worker RPC without waiting, join
        the metadata rendezvous, and require unanimous source and destination
        plan commits before constructing NCCL or the M2N handle.
        """
        # Trainer-side weight transfer engine for this rank. `__init__` only
        # holds the client and source; the communicator, meshes, and M2N handle
        # are filled in below.
        engine = cls(
            client=client,
            source=cast(M2NWeightSource, source),
            is_sender=init_info.is_sender,
        )
        engine._initialize(init_info, source)
        return engine

    def _initialize(
        self,
        init_info: M2NTrainerInitInfo,
        source: WeightSource | None,
    ) -> None:
        """Run source validation, metadata agreement, and NCCL rendezvous."""
        self._num_trainer_ranks = init_info.num_trainer_ranks
        self._trainer_rank = init_info.rank
        local_source_error: str | None = None
        try:
            if not isinstance(source, M2NWeightSource):
                raise TypeError(
                    "nccl_m2n needs per-parameter layouts, so its source must "
                    f"be an M2NWeightSource; got {type(source).__name__}"
                )
            self._m2n = import_m2n()
            self._prepare_source_plan(source, init_info.num_trainer_ranks)
        except Exception as exc:
            local_source_error = f"{type(exc).__name__}: {exc}"
            self._metas = []
            self._wire_params = []
            self._source_topology = M2NSourceTopology((), ())
            self._source_digest = source_plan_digest([], self._source_topology)

        executor: ThreadPoolExecutor | None = None
        init_future: Future[None] | None = None
        if self.is_sender:
            executor = ThreadPoolExecutor(
                max_workers=1, thread_name_prefix="m2n-weight-sync"
            )
            init_future = executor.submit(
                self.client.init_weight_transfer_engine,
                self._worker_init_info(init_info),
            )
        metadata_group: StatelessProcessGroup | None = None
        handle = None
        route_communicators: dict[int, Any] = {}
        route_streams: dict[int, torch.cuda.Stream] = {}
        try:
            reservation = init_info.listen_socket
            try:
                metadata_group = stateless_init_metadata_group(
                    init_info.master_address,
                    init_info.master_port,
                    init_info.rank,
                    init_info.world_size,
                    listen_socket=reservation,
                )
            finally:
                init_info.listen_socket = None
                if reservation is not None:
                    reservation.close()
            plan_digest = check_source_plan_agreement(
                metadata_group,
                self._wire_params,
                expected_digest=self._source_digest,
                local_error=local_source_error,
                topology=self._source_topology,
            )

            # The worker resolves its rank-local destinations, and every rank
            # commits the same ordered plan before constructing NCCL.
            destination_interval = M2NMesh(
                init_info.destination_mesh_dims, init_info.num_trainer_ranks
            )
            self._destination_mesh_constraint = init_info.destination_mesh_constraint
            self._destination_plan, commit_token = agree_destination_plan(
                metadata_group,
                source_digest=plan_digest,
                params=self._wire_params,
                metas=self._metas,
                dst_mesh=destination_interval,
                dst_mesh_constraint=self._destination_mesh_constraint,
                local_plan=None,
                local_error=None,
                source_topology=self._source_topology,
            )
            self._destination_commit_token = commit_token
            assert self._source_topology is not None
            self._routes = build_m2n_transfer_routes(
                self._source_topology,
                num_trainer_ranks=init_info.num_trainer_ranks,
                world_size=init_info.world_size,
                dst_mesh_dims=init_info.destination_mesh_dims,
            )
            self._param_routes = build_m2n_param_routes(self._routes, len(self._metas))
            self._transfer_waves = build_m2n_transfer_waves(
                self._routes,
                tuple(
                    m2n_load_group(destination.mode, destination.staging_group)
                    for destination in self._destination_plan
                ),
            )
            unique_ids: tuple[bytes | None, ...] = (None,) * len(self._routes)
            data_plane_error: str | None = None
            try:
                unique_ids = init_info.data_plane_unique_ids(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_ids,
                data_plane_error,
            )

            participates = any(
                init_info.rank in route.source_ranks for route in self._routes
            )
            runtime_error: str | None = None
            nccl = None
            device = None
            if participates:
                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)
            if participates:
                assert nccl is not None and device is not None and handle is not None

            # Every metadata rank bootstraps the fixed communicator list in
            # canonical order. Members initialize each communicator, then all
            # ranks report the complete ordered result once below.
            communicator_errors: list[str | None] = []
            for route_index, (route, unique_id) in enumerate(
                zip(self._routes, unique_ids, strict=True)
            ):
                route_error: str | None = None
                communicator = None
                try:
                    communicator = create_m2n_communicator(
                        nccl,
                        metadata_group,
                        unique_id,
                        members=route.members,
                    )
                    participates = metadata_group.rank in route.members
                    if participates != (communicator is not None):
                        raise RuntimeError(
                            f"transfer route {route_index} communicator membership "
                            "does not match its agreed rank list"
                        )
                    if communicator is not None:
                        assert device is not None
                        route_communicators[route_index] = communicator
                        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,
            )

            self._sync_trainer_init_outcome(metadata_group, init_future)
            logger.info(
                "nccl_m2n trainer ready: %d parameters, %d source groups, "
                "%d transfer routes, "
                "trainer ranks [0, %d), inference ranks [%d, %d)",
                len(self._metas),
                len(self._source_topology.groups),
                len(self._routes),
                init_info.num_trainer_ranks,
                init_info.num_trainer_ranks,
                init_info.world_size,
            )
            logger.info("nccl_m2n source plan digest: %s", plan_digest)
        except BaseException:
            self._destination_plan = []
            self._destination_commit_token = ""
            self._destination_mesh_constraint = None
            self._routes = ()
            self._param_routes = ()
            self._transfer_waves = ()
            self._route_streams = {}
            for communicator in reversed(tuple(route_communicators.values())):
                try:
                    communicator.abort()
                except BaseException:
                    logger.exception(
                        "failed to abort partially initialized nccl_m2n communicator"
                    )
            route_communicators.clear()
            if handle is not None:
                try:
                    handle.destroy()
                except BaseException:
                    logger.exception(
                        "failed to destroy partially initialized nccl_m2n handle"
                    )
            raise
        finally:
            if executor is not None:
                executor.shutdown(wait=False, cancel_futures=True)

        self._metadata_group = metadata_group
        self._handle = handle
        self._route_communicators = route_communicators
        self._route_streams = route_streams
        # Compatibility alias for the legacy single-communicator surface.
        self.group = next(iter(route_communicators.values()), None)

    def _sync_trainer_init_outcome(
        self,
        metadata_group: StatelessProcessGroup,
        init_future: Future[None] | None,
    ) -> None:
        """Require every trainer to observe rank zero's worker-init result."""
        if self.is_sender:
            if init_future is None:
                raise RuntimeError("trainer rank zero is missing its worker-init RPC")
            failure: BaseException | None = None
            try:
                init_future.result()
            except BaseException as exc:
                failure = exc
            message = {
                "phase": "trainer_init_outcome",
                "plan_token": self._destination_commit_token,
                "ok": failure is None,
                "error": (
                    None if failure is None else f"{type(failure).__name__}: {failure}"
                ),
            }
            for rank in range(1, self._num_trainer_ranks):
                metadata_group.send_obj(message, rank)
            if failure is not None:
                raise failure
            return

        message = metadata_group.recv_obj(0)
        expected_fields = {"phase", "plan_token", "ok", "error"}
        if not isinstance(message, Mapping) or set(message) != expected_fields:
            raise RuntimeError("trainer rank zero sent an invalid init outcome")
        if (
            message["phase"] != "trainer_init_outcome"
            or message["plan_token"] != self._destination_commit_token
        ):
            raise RuntimeError("trainer rank zero sent an invalid init outcome")
        ok = message["ok"]
        error = message["error"]
        if not isinstance(ok, bool) or ok != (error is None):
            raise RuntimeError("trainer rank zero sent an invalid init outcome")
        if not ok:
            if not isinstance(error, str) or not error:
                raise RuntimeError("trainer rank zero sent an invalid init outcome")
            raise RuntimeError(
                "nccl_m2n worker initialization failed on trainer rank zero: " + error
            )

    def _prepare_source_plan(
        self, source: M2NWeightSource, num_trainer_ranks: int
    ) -> None:
        """Build and validate this rank's immutable source wire plan."""
        self._metas = source.metadata()
        topology = source.source_topology()
        if topology is None:
            topology = M2NSourceTopology.single_group(
                num_trainer_ranks, len(self._metas)
            )
        elif not isinstance(topology, M2NSourceTopology):
            raise TypeError(
                "M2NWeightSource.source_topology() must return an "
                f"M2NSourceTopology or None, got {type(topology).__name__}"
            )
        topology.validate(
            num_trainer_ranks=num_trainer_ranks,
            num_params=len(self._metas),
        )
        self._source_topology = topology
        names: set[str] = set()
        for param_index, meta in enumerate(self._metas):
            if not isinstance(meta, M2NParamMeta):
                raise TypeError(
                    "nccl_m2n source metadata must contain M2NParamMeta "
                    f"entries, got {type(meta).__name__}"
                )
            if meta.name in names:
                raise ValueError(
                    "nccl_m2n source metadata contains duplicate parameter "
                    f"'{meta.name}'"
                )
            names.add(meta.name)
            check_transferable(meta.name, meta.dtype, meta.shape)
            layout = meta.source_layout
            if layout.mesh.start_rank != 0:
                raise ValueError(
                    f"parameter '{meta.name}' source mesh must start at rank 0, "
                    f"got {layout.mesh.start_rank}"
                )
            group_index = topology.param_groups[param_index]
            source_group_size = len(topology.groups[group_index])
            if layout.mesh.size != source_group_size:
                raise ValueError(
                    f"parameter '{meta.name}' source mesh covers "
                    f"{layout.mesh.size} ranks, but source group {group_index} "
                    f"contains {source_group_size} ranks"
                )
            expected_local_shape(meta)
        held_names = source.held_names()
        if held_names is not None:
            held_values = tuple(held_names)
            if any(not isinstance(name, str) for name in held_values):
                raise TypeError("M2NWeightSource.held_names() must contain strings")
            held = set(held_values)
            required = {
                meta.name
                for param_index, meta in enumerate(self._metas)
                if self._trainer_rank
                in topology.groups[topology.param_groups[param_index]]
            }
            missing = sorted(required - held)
            if missing:
                raise ValueError(
                    "M2NWeightSource.held_names() omits parameters assigned to "
                    f"trainer rank {self._trainer_rank}, including {missing[0]!r}"
                )
        self._wire_params = [
            M2NWireParam(
                name=meta.name,
                dtype_name=str(meta.dtype).split(".")[-1],
                shape=meta.shape,
                src_mesh_dims=meta.source_layout.mesh.dims,
                src_placements=meta.source_layout.placements,
                allow_full_fallback=meta.allow_full_fallback,
            )
            for meta in self._metas
        ]
        self._source_digest = source_plan_digest(self._wire_params, topology)

    def plan_summary(self) -> dict[str, Any]:
        """Return the authenticated static plan as JSON-safe run metadata."""
        if not self._destination_commit_token or not self._destination_plan:
            raise RuntimeError("nccl_m2n transfer plan is not initialized")
        assert self._source_topology is not None
        mode_counts: dict[str, int] = {}
        mesh_counts: dict[tuple[int, int], int] = {}
        logical_bytes_by_mode: dict[str, int] = {}
        receiver_bytes_by_mode: dict[str, int] = {}
        for meta, destination in zip(self._metas, self._destination_plan, strict=True):
            mode = destination.mode
            element_size = torch.empty((), dtype=meta.dtype).element_size()
            logical_bytes = math.prod(meta.shape) * element_size
            receiver_bytes = (
                math.prod(destination.local_shape)
                * element_size
                * math.prod(destination.mesh_dims)
            )
            mode_counts[mode] = mode_counts.get(mode, 0) + 1
            mesh_counts[destination.mesh_dims] = (
                mesh_counts.get(destination.mesh_dims, 0) + 1
            )
            logical_bytes_by_mode[mode] = (
                logical_bytes_by_mode.get(mode, 0) + logical_bytes
            )
            receiver_bytes_by_mode[mode] = (
                receiver_bytes_by_mode.get(mode, 0) + receiver_bytes
            )
        logical_global_bytes = sum(logical_bytes_by_mode.values())
        planned_receiver_payload_bytes = sum(receiver_bytes_by_mode.values())
        return {
            "wire_schema_version": M2N_WIRE_SCHEMA_VERSION,
            "source_digest": self._source_digest,
            "destination_commit_token": self._destination_commit_token,
            "destination_mesh_constraint": (
                None
                if self._destination_mesh_constraint is None
                else list(self._destination_mesh_constraint)
            ),
            "destination_mesh_counts": [
                {"dims": list(dims), "parameter_count": count}
                for dims, count in mesh_counts.items()
            ],
            "source_group_count": len(self._source_topology.groups),
            "parameter_count": len(self._metas),
            "transfer_communicators": [
                {
                    "source_group_index": route.source_group_index,
                    "source_ranks": list(route.source_ranks),
                    "destination_ranks": list(route.worker_ranks),
                    "param_indices": list(route.param_indices),
                }
                for route in self._routes
            ],
            "transfer_waves": [
                {
                    "issue_indices": list(wave.issue_indices),
                    "consume_index": wave.consume_index,
                }
                for wave in self._transfer_waves
            ],
            # Each complete logical parameter is counted once.
            "logical_global_bytes": logical_global_bytes,
            # Parameter counts, not byte counts.
            "destination_mode_counts": mode_counts,
            "logical_global_bytes_by_destination_mode": logical_bytes_by_mode,
            # Aggregate destination tensor bytes implied by the agreed plan;
            # this is not measured NCCL, InfiniBand, or protocol traffic.
            "planned_receiver_payload_bytes": planned_receiver_payload_bytes,
            "planned_receiver_payload_bytes_by_destination_mode": (
                receiver_bytes_by_mode
            ),
        }

    def _worker_init_info(self, init_info: M2NTrainerInitInfo) -> dict[str, Any]:
        """Handshake payload: rendezvous, layouts, and the transfer plan."""
        payload: dict[str, Any] = {
            "schema_version": M2N_WIRE_SCHEMA_VERSION,
            "master_address": init_info.master_address,
            "master_port": init_info.master_port,
            "nccl_unique_id_b64": init_info.nccl_unique_id_b64,
            "nccl_unique_ids_b64": init_info.nccl_unique_ids_b64,
            "rank_offset": init_info.num_trainer_ranks,
            "world_size": init_info.world_size,
            "dst_mesh_dims": (
                None
                if init_info.destination_mesh_constraint is None
                else list(init_info.destination_mesh_constraint)
            ),
            "source_digest": self._source_digest,
            "source_topology": (
                None
                if self._source_topology is None
                else self._source_topology.to_dict()
            ),
            "params": [param.to_dict() for param in self._wire_params],
            "max_cta": init_info.max_cta,
        }
        return {key: value for key, value in payload.items() if value is not None}

    def send_weights(self) -> None:
        """Drive one update, replaying it once after a reset finish failure."""
        participates = any(
            self._trainer_rank in route.source_ranks for route in self._routes
        )
        if not self._routes or (participates and self._handle is None):
            raise RuntimeError("nccl_m2n trainer engine is not initialized")

        for attempt in range(_FINISH_RETRY_LIMIT + 1):
            if not self._send_weight_round(retry_finish=attempt < _FINISH_RETRY_LIMIT):
                return
        raise AssertionError("unreachable nccl_m2n finish retry state")

    def _send_weight_round(self, *, retry_finish: bool) -> bool:
        """Run one complete update attempt; return whether to replay it."""
        if not self.is_sender:
            failure: BaseException | None = None
            retry_update = False
            try:
                error, requires_reinit = self._receive_round_dispatch()
            except BaseException as exc:
                failure = exc
                self._abort_data_plane()
            else:
                if requires_reinit:
                    self._abort_data_plane()
                if error is not None:
                    failure = RuntimeError(
                        "nccl_m2n worker update was not dispatched: " + error
                    )
            if failure is None:
                try:
                    self._send()
                except BaseException as exc:
                    failure = exc
            try:
                error, requires_reinit, retry_update = self._receive_round_outcome()
            except BaseException as exc:
                self._abort_data_plane()
                if failure is None:
                    failure = exc
                else:
                    logger.exception(
                        "failed to consume nccl_m2n round outcome after local failure"
                    )
            else:
                if requires_reinit:
                    self._abort_data_plane()
                if error is not None and failure is None and not retry_update:
                    failure = RuntimeError(
                        "nccl_m2n worker update failed on trainer rank zero: " + error
                    )
            finally:
                self._round_id += 1
            if failure is not None:
                raise failure
            return retry_update

        executor: ThreadPoolExecutor | None = None
        dispatch_attempted = False
        transfer_completed = False
        try:
            self.client.start_weight_update()
            executor = ThreadPoolExecutor(
                max_workers=1, thread_name_prefix="m2n-weight-sync"
            )
            update = executor.submit(
                self.client.update_weights,
                {"names": [m.name for m in self._metas]},
            )
            # Surface an immediate worker-side rejection before trainers
            # enter the collective sequence.
            if update.done():
                update.result()
            dispatch_attempted = True
            self._publish_round_dispatch(None)
            self._send()
            update.result()
            transfer_completed = True
            self.client.finish_weight_update()
        except BaseException as exc:
            requires_reinit = dispatch_attempted and not transfer_completed
            retry_update = transfer_completed and retry_finish
            if requires_reinit:
                self._abort_data_plane()
            if not dispatch_attempted:
                try:
                    dispatch_attempted = True
                    self._publish_round_dispatch(exc, requires_reinit=False)
                except BaseException:
                    self._abort_data_plane()
                    requires_reinit = True
                    logger.exception(
                        "failed to publish nccl_m2n round dispatch failure"
                    )
            try:
                self._publish_round_outcome(
                    exc,
                    requires_reinit=requires_reinit,
                    retry_update=retry_update,
                )
            except BaseException:
                self._abort_data_plane()
                logger.exception("failed to publish nccl_m2n round failure")
            if retry_update:
                return True
            raise
        else:
            try:
                self._publish_round_outcome(None)
            except BaseException:
                self._abort_data_plane()
                raise
            return False
        finally:
            if executor is not None:
                executor.shutdown(wait=False, cancel_futures=True)
            self._round_id += 1

    def _publish_round_dispatch(
        self,
        failure: BaseException | None,
        *,
        requires_reinit: bool = False,
    ) -> None:
        """Release non-senders only after rank zero dispatched worker receive."""
        self._publish_round_control(
            "round_dispatch",
            failure,
            requires_reinit=requires_reinit,
            retry_update=False,
        )

    def _receive_round_dispatch(self) -> tuple[str | None, bool]:
        """Wait for rank zero before entering any source-side M2N collective."""
        error, requires_reinit, retry_update = self._receive_round_control(
            "round_dispatch"
        )
        assert not retry_update
        return error, requires_reinit

    def _publish_round_outcome(
        self,
        failure: BaseException | None,
        *,
        requires_reinit: bool = False,
        retry_update: bool = False,
    ) -> None:
        """Tell non-sender trainer ranks whether worker finalization passed."""
        self._publish_round_control(
            "round_outcome",
            failure,
            requires_reinit=requires_reinit,
            retry_update=retry_update,
        )

    def _publish_round_control(
        self,
        phase: str,
        failure: BaseException | None,
        *,
        requires_reinit: bool,
        retry_update: bool,
    ) -> None:
        """Publish one fixed-count trainer-only control message and await ACKs."""
        if self._num_trainer_ranks <= 1:
            return
        if self._metadata_group is None:
            raise RuntimeError("nccl_m2n metadata group is unavailable")
        envelope = {
            "phase": phase,
            "round_id": self._round_id,
            "ok": failure is None,
            "error": (
                None if failure is None else f"{type(failure).__name__}: {failure}"
            ),
            "requires_reinit": requires_reinit,
            "retry_update": retry_update,
        }
        for rank in range(1, self._num_trainer_ranks):
            self._metadata_group.send_obj(envelope, rank)
        for rank in range(1, self._num_trainer_ranks):
            ack = self._metadata_group.recv_obj(rank)
            expected_ack = {
                "phase": f"{phase}_consumed",
                "round_id": self._round_id,
                "rank": rank,
                "error": None,
            }
            if ack == expected_ack:
                continue
            if (
                isinstance(ack, Mapping)
                and set(ack) == set(expected_ack)
                and ack.get("phase") == expected_ack["phase"]
                and ack.get("round_id") == self._round_id
                and ack.get("rank") == rank
                and isinstance(ack.get("error"), str)
                and ack["error"]
            ):
                raise RuntimeError(
                    f"trainer rank {rank} rejected {phase}: {ack['error']}"
                )
            else:
                raise RuntimeError(
                    f"trainer rank {rank} sent an invalid {phase} acknowledgement"
                )

    def _receive_round_outcome(self) -> tuple[str | None, bool, bool]:
        """Wait until trainer rank zero observed every worker's finish RPC."""
        return self._receive_round_control("round_outcome")

    def _receive_round_control(self, phase: str) -> tuple[str | None, bool, bool]:
        """Receive and acknowledge one rank-zero control message for this round."""
        if self._metadata_group is None:
            raise RuntimeError("nccl_m2n metadata group is unavailable")
        message = self._metadata_group.recv_obj(0)
        validation_error: str | None = None
        remote_error: str | None = None
        requires_reinit = False
        retry_update = False
        try:
            expected_fields = {
                "phase",
                "round_id",
                "ok",
                "error",
                "requires_reinit",
                "retry_update",
            }
            if not isinstance(message, Mapping) or set(message) != expected_fields:
                raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
            if message["phase"] != phase or message["round_id"] != self._round_id:
                raise RuntimeError(f"trainer rank zero sent an out-of-order {phase}")
            ok = message["ok"]
            remote_error = message["error"]
            requires_reinit = message["requires_reinit"]
            retry_update = message["retry_update"]
            if not isinstance(ok, bool) or ok != (remote_error is None):
                raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
            if not isinstance(requires_reinit, bool) or (ok and requires_reinit):
                raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
            if (
                not isinstance(retry_update, bool)
                or (ok and retry_update)
                or (requires_reinit and retry_update)
                or (phase != "round_outcome" and retry_update)
            ):
                raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
            if not ok and (not isinstance(remote_error, str) or not remote_error):
                raise RuntimeError(f"trainer rank zero sent an invalid {phase} error")
        except RuntimeError as exc:
            validation_error = str(exc)
        self._metadata_group.send_obj(
            {
                "phase": f"{phase}_consumed",
                "round_id": self._round_id,
                "rank": self._trainer_rank,
                "error": validation_error,
            },
            0,
        )
        if validation_error is not None:
            raise RuntimeError(validation_error)
        return remote_error, requires_reinit, retry_update

    def _send(self) -> None:
        """Reshard each local shard into its worker-planned destination."""
        assert self.source is not None
        assert self._source_topology is not None
        source = cast(M2NWeightSource, self.source)
        m2n = self._m2n
        source_group_size = len(self._source_topology.groups[0])

        joined_routes = {
            route_index
            for route_index, route in enumerate(self._routes)
            if self._trainer_rank in route.source_ranks
        }
        if not joined_routes:
            return

        iterators: dict[int, Any] = {}
        inflight_buffers: list[Any] = []
        try:
            # Optimizer work may have used another stream. Complete it before
            # each provider materializes onto its persistent transfer stream.
            if joined_routes:
                device_index = torch.accelerator.current_device_index()
                torch.accelerator.synchronize(device_index)
            # Replay the same bounded schedule as the workers. Keeping the
            # buffers in this frame protects them until completion or abort.
            for wave in self._transfer_waves:
                for param_index in wave.issue_indices:
                    group_index = self._source_topology.param_groups[param_index]
                    route_index = self._param_routes[param_index]
                    if route_index in joined_routes:
                        meta = self._metas[param_index]
                        communicator = self._route_communicators.get(route_index)
                        stream = self._route_streams.get(route_index)
                        if communicator is None or stream is None:
                            raise RuntimeError(
                                f"trainer rank {self._trainer_rank} is missing "
                                f"transfer route {route_index} for source group "
                                f"{group_index}"
                            )

                        with torch.cuda.stream(stream):
                            iterator = iterators.get(group_index)
                            if iterator is None:
                                iterator = iter(source.iter_group(group_index))
                                iterators[group_index] = iterator
                            try:
                                name, buffer = next(iterator)
                            except StopIteration as exc:
                                raise RuntimeError(
                                    f"source group {group_index} ended before "
                                    f"parameter {meta.name!r} at manifest position "
                                    f"{param_index}"
                                ) from exc
                            if name != meta.name:
                                raise RuntimeError(
                                    f"source group {group_index} yielded {name!r} "
                                    f"for manifest position {param_index}, expected "
                                    f"{meta.name!r}"
                                )
                            if buffer is None:
                                raise RuntimeError(
                                    f"source group {group_index} yielded no local "
                                    f"buffer for owned parameter {meta.name!r}"
                                )
                            validate_local_buffer(m2n, meta, buffer)
                            src_mesh, src_placements = resolve_layout(
                                meta.source_layout.mesh,
                                meta.source_layout.placements,
                            )
                            destination = self._destination_plan[param_index]
                            dst_mesh, dst_placements = resolve_layout(
                                M2NMesh(
                                    destination.mesh_dims,
                                    source_group_size,
                                ),
                                destination.placements,
                            )
                            raw_pointer_args = {}
                            if isinstance(buffer, int):
                                raw_pointer_args = {
                                    "src_local_shape": expected_local_shape(meta),
                                    "src_dtype": meta.dtype,
                                }
                            m2n.reshard(
                                buffer,
                                None,
                                communicator,
                                stream,
                                src_mesh=to_mesh(m2n, src_mesh),
                                src_placements=to_placements(m2n, src_placements),
                                dst_mesh=to_mesh(m2n, dst_mesh),
                                dst_placements=to_placements(m2n, dst_placements),
                                handle=self._handle,
                                **raw_pointer_args,
                            )
                            record_stream = getattr(buffer, "record_stream", None)
                            if callable(record_stream):
                                record_stream(stream)
                            elif not isinstance(buffer, int):
                                inflight_buffers.append(buffer)

            for group_index, iterator in iterators.items():
                route_index = next(
                    index
                    for index, route in enumerate(self._routes)
                    if route.source_group_index == group_index
                    and index in joined_routes
                )
                stream = self._route_streams[route_index]
                with torch.cuda.stream(stream):
                    try:
                        name, _ = next(iterator)
                    except StopIteration:
                        continue
                    raise RuntimeError(
                        f"source group {group_index} yielded extra parameter {name!r}"
                    )

            # Completion protects source-buffer lifetime and prevents optimizer
            # mutation before worker finalization.
            for route_index in range(len(self._routes)):
                stream = self._route_streams.get(route_index)
                if stream is not None:
                    stream.synchronize()
        except BaseException:
            self._abort_data_plane()
            raise

    def _abort_data_plane(self) -> None:
        """Abort every local communicator without waiting on CUDA streams."""
        handle, self._handle = self._handle, None
        legacy_group, self.group = self.group, None
        communicators = list(self._route_communicators.values())
        if legacy_group is not None and all(
            legacy_group is not communicator for communicator in communicators
        ):
            communicators.append(legacy_group)
        self._route_communicators = {}
        streams, self._route_streams = self._route_streams, {}
        self._routes = ()
        self._param_routes = ()
        self._transfer_waves = ()

        for communicator in reversed(communicators):
            try:
                communicator.abort()
            except BaseException:
                logger.exception("failed to abort nccl_m2n communicator")
        if handle is not None:
            try:
                handle.destroy()
            except BaseException:
                logger.exception("failed to destroy nccl_m2n handle")
        self._source_topology = None
        self._destination_plan = []
        self._destination_commit_token = ""
        self._destination_mesh_constraint = None
        self._m2n = None
        # Keep 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
        legacy_group, self.group = self.group, None
        communicators = list(self._route_communicators.values())
        if legacy_group is not None and all(
            legacy_group is not communicator for communicator in communicators
        ):
            communicators.append(legacy_group)
        self._route_communicators = {}
        streams, self._route_streams = self._route_streams, {}
        self._routes = ()
        self._param_routes = ()
        self._transfer_waves = ()
        self._source_topology = None
        self._metadata_group = None
        self._destination_plan = []
        self._destination_commit_token = ""
        self._destination_mesh_constraint = None
        self._m2n = None

        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 communicators:
            attempt(torch.accelerator.synchronize, "failed to synchronize nccl_m2n")
        if handle is not None:
            attempt(handle.destroy, "failed to destroy nccl_m2n handle")
        for communicator in reversed(communicators):
            attempt(
                communicator.abort,
                "failed to abort nccl_m2n communicator",
            )
        # Keep stream wrappers alive through communicator cleanup.
        del streams
        if first_error is not None:
            raise first_error

__init__(*, client, source, is_sender)

Hold the client and source; trainer_init fills in the group.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def __init__(
    self,
    *,
    client: VLLMWeightSyncClient,
    source: M2NWeightSource,
    is_sender: bool,
) -> None:
    """Hold the client and source; `trainer_init` fills in the group."""
    super().__init__(client=client, source=source, is_sender=is_sender)
    self.group: Any = None
    self._m2n: Any = None
    self._handle: Any = None
    self._metas: list[M2NParamMeta] = []
    self._wire_params: list[M2NWireParam] = []
    self._source_digest = ""
    self._destination_commit_token = ""
    self._destination_mesh_constraint: tuple[int, int] | None = None
    self._metadata_group: StatelessProcessGroup | None = None
    self._num_trainer_ranks = 0
    self._trainer_rank = -1
    self._destination_plan: list[M2NWireDestination] = []
    self._source_topology: M2NSourceTopology | None = None
    self._routes: tuple[M2NTransferRoute, ...] = ()
    self._param_routes: tuple[int, ...] = ()
    self._transfer_waves: tuple[M2NTransferWave, ...] = ()
    self._route_communicators: dict[int, Any] = {}
    self._route_streams: dict[int, torch.cuda.Stream] = {}
    self._round_id = 0

_abort_data_plane()

Abort every local communicator without waiting on CUDA streams.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _abort_data_plane(self) -> None:
    """Abort every local communicator without waiting on CUDA streams."""
    handle, self._handle = self._handle, None
    legacy_group, self.group = self.group, None
    communicators = list(self._route_communicators.values())
    if legacy_group is not None and all(
        legacy_group is not communicator for communicator in communicators
    ):
        communicators.append(legacy_group)
    self._route_communicators = {}
    streams, self._route_streams = self._route_streams, {}
    self._routes = ()
    self._param_routes = ()
    self._transfer_waves = ()

    for communicator in reversed(communicators):
        try:
            communicator.abort()
        except BaseException:
            logger.exception("failed to abort nccl_m2n communicator")
    if handle is not None:
        try:
            handle.destroy()
        except BaseException:
            logger.exception("failed to destroy nccl_m2n handle")
    self._source_topology = None
    self._destination_plan = []
    self._destination_commit_token = ""
    self._destination_mesh_constraint = None
    self._m2n = None
    # Keep stream wrappers alive until communicator abort and handle
    # destruction have completed.
    del streams

_initialize(init_info, source)

Run source validation, metadata agreement, and NCCL rendezvous.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _initialize(
    self,
    init_info: M2NTrainerInitInfo,
    source: WeightSource | None,
) -> None:
    """Run source validation, metadata agreement, and NCCL rendezvous."""
    self._num_trainer_ranks = init_info.num_trainer_ranks
    self._trainer_rank = init_info.rank
    local_source_error: str | None = None
    try:
        if not isinstance(source, M2NWeightSource):
            raise TypeError(
                "nccl_m2n needs per-parameter layouts, so its source must "
                f"be an M2NWeightSource; got {type(source).__name__}"
            )
        self._m2n = import_m2n()
        self._prepare_source_plan(source, init_info.num_trainer_ranks)
    except Exception as exc:
        local_source_error = f"{type(exc).__name__}: {exc}"
        self._metas = []
        self._wire_params = []
        self._source_topology = M2NSourceTopology((), ())
        self._source_digest = source_plan_digest([], self._source_topology)

    executor: ThreadPoolExecutor | None = None
    init_future: Future[None] | None = None
    if self.is_sender:
        executor = ThreadPoolExecutor(
            max_workers=1, thread_name_prefix="m2n-weight-sync"
        )
        init_future = executor.submit(
            self.client.init_weight_transfer_engine,
            self._worker_init_info(init_info),
        )
    metadata_group: StatelessProcessGroup | None = None
    handle = None
    route_communicators: dict[int, Any] = {}
    route_streams: dict[int, torch.cuda.Stream] = {}
    try:
        reservation = init_info.listen_socket
        try:
            metadata_group = stateless_init_metadata_group(
                init_info.master_address,
                init_info.master_port,
                init_info.rank,
                init_info.world_size,
                listen_socket=reservation,
            )
        finally:
            init_info.listen_socket = None
            if reservation is not None:
                reservation.close()
        plan_digest = check_source_plan_agreement(
            metadata_group,
            self._wire_params,
            expected_digest=self._source_digest,
            local_error=local_source_error,
            topology=self._source_topology,
        )

        # The worker resolves its rank-local destinations, and every rank
        # commits the same ordered plan before constructing NCCL.
        destination_interval = M2NMesh(
            init_info.destination_mesh_dims, init_info.num_trainer_ranks
        )
        self._destination_mesh_constraint = init_info.destination_mesh_constraint
        self._destination_plan, commit_token = agree_destination_plan(
            metadata_group,
            source_digest=plan_digest,
            params=self._wire_params,
            metas=self._metas,
            dst_mesh=destination_interval,
            dst_mesh_constraint=self._destination_mesh_constraint,
            local_plan=None,
            local_error=None,
            source_topology=self._source_topology,
        )
        self._destination_commit_token = commit_token
        assert self._source_topology is not None
        self._routes = build_m2n_transfer_routes(
            self._source_topology,
            num_trainer_ranks=init_info.num_trainer_ranks,
            world_size=init_info.world_size,
            dst_mesh_dims=init_info.destination_mesh_dims,
        )
        self._param_routes = build_m2n_param_routes(self._routes, len(self._metas))
        self._transfer_waves = build_m2n_transfer_waves(
            self._routes,
            tuple(
                m2n_load_group(destination.mode, destination.staging_group)
                for destination in self._destination_plan
            ),
        )
        unique_ids: tuple[bytes | None, ...] = (None,) * len(self._routes)
        data_plane_error: str | None = None
        try:
            unique_ids = init_info.data_plane_unique_ids(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_ids,
            data_plane_error,
        )

        participates = any(
            init_info.rank in route.source_ranks for route in self._routes
        )
        runtime_error: str | None = None
        nccl = None
        device = None
        if participates:
            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)
        if participates:
            assert nccl is not None and device is not None and handle is not None

        # Every metadata rank bootstraps the fixed communicator list in
        # canonical order. Members initialize each communicator, then all
        # ranks report the complete ordered result once below.
        communicator_errors: list[str | None] = []
        for route_index, (route, unique_id) in enumerate(
            zip(self._routes, unique_ids, strict=True)
        ):
            route_error: str | None = None
            communicator = None
            try:
                communicator = create_m2n_communicator(
                    nccl,
                    metadata_group,
                    unique_id,
                    members=route.members,
                )
                participates = metadata_group.rank in route.members
                if participates != (communicator is not None):
                    raise RuntimeError(
                        f"transfer route {route_index} communicator membership "
                        "does not match its agreed rank list"
                    )
                if communicator is not None:
                    assert device is not None
                    route_communicators[route_index] = communicator
                    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,
        )

        self._sync_trainer_init_outcome(metadata_group, init_future)
        logger.info(
            "nccl_m2n trainer ready: %d parameters, %d source groups, "
            "%d transfer routes, "
            "trainer ranks [0, %d), inference ranks [%d, %d)",
            len(self._metas),
            len(self._source_topology.groups),
            len(self._routes),
            init_info.num_trainer_ranks,
            init_info.num_trainer_ranks,
            init_info.world_size,
        )
        logger.info("nccl_m2n source plan digest: %s", plan_digest)
    except BaseException:
        self._destination_plan = []
        self._destination_commit_token = ""
        self._destination_mesh_constraint = None
        self._routes = ()
        self._param_routes = ()
        self._transfer_waves = ()
        self._route_streams = {}
        for communicator in reversed(tuple(route_communicators.values())):
            try:
                communicator.abort()
            except BaseException:
                logger.exception(
                    "failed to abort partially initialized nccl_m2n communicator"
                )
        route_communicators.clear()
        if handle is not None:
            try:
                handle.destroy()
            except BaseException:
                logger.exception(
                    "failed to destroy partially initialized nccl_m2n handle"
                )
        raise
    finally:
        if executor is not None:
            executor.shutdown(wait=False, cancel_futures=True)

    self._metadata_group = metadata_group
    self._handle = handle
    self._route_communicators = route_communicators
    self._route_streams = route_streams
    # Compatibility alias for the legacy single-communicator surface.
    self.group = next(iter(route_communicators.values()), None)

_prepare_source_plan(source, num_trainer_ranks)

Build and validate this rank's immutable source wire plan.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _prepare_source_plan(
    self, source: M2NWeightSource, num_trainer_ranks: int
) -> None:
    """Build and validate this rank's immutable source wire plan."""
    self._metas = source.metadata()
    topology = source.source_topology()
    if topology is None:
        topology = M2NSourceTopology.single_group(
            num_trainer_ranks, len(self._metas)
        )
    elif not isinstance(topology, M2NSourceTopology):
        raise TypeError(
            "M2NWeightSource.source_topology() must return an "
            f"M2NSourceTopology or None, got {type(topology).__name__}"
        )
    topology.validate(
        num_trainer_ranks=num_trainer_ranks,
        num_params=len(self._metas),
    )
    self._source_topology = topology
    names: set[str] = set()
    for param_index, meta in enumerate(self._metas):
        if not isinstance(meta, M2NParamMeta):
            raise TypeError(
                "nccl_m2n source metadata must contain M2NParamMeta "
                f"entries, got {type(meta).__name__}"
            )
        if meta.name in names:
            raise ValueError(
                "nccl_m2n source metadata contains duplicate parameter "
                f"'{meta.name}'"
            )
        names.add(meta.name)
        check_transferable(meta.name, meta.dtype, meta.shape)
        layout = meta.source_layout
        if layout.mesh.start_rank != 0:
            raise ValueError(
                f"parameter '{meta.name}' source mesh must start at rank 0, "
                f"got {layout.mesh.start_rank}"
            )
        group_index = topology.param_groups[param_index]
        source_group_size = len(topology.groups[group_index])
        if layout.mesh.size != source_group_size:
            raise ValueError(
                f"parameter '{meta.name}' source mesh covers "
                f"{layout.mesh.size} ranks, but source group {group_index} "
                f"contains {source_group_size} ranks"
            )
        expected_local_shape(meta)
    held_names = source.held_names()
    if held_names is not None:
        held_values = tuple(held_names)
        if any(not isinstance(name, str) for name in held_values):
            raise TypeError("M2NWeightSource.held_names() must contain strings")
        held = set(held_values)
        required = {
            meta.name
            for param_index, meta in enumerate(self._metas)
            if self._trainer_rank
            in topology.groups[topology.param_groups[param_index]]
        }
        missing = sorted(required - held)
        if missing:
            raise ValueError(
                "M2NWeightSource.held_names() omits parameters assigned to "
                f"trainer rank {self._trainer_rank}, including {missing[0]!r}"
            )
    self._wire_params = [
        M2NWireParam(
            name=meta.name,
            dtype_name=str(meta.dtype).split(".")[-1],
            shape=meta.shape,
            src_mesh_dims=meta.source_layout.mesh.dims,
            src_placements=meta.source_layout.placements,
            allow_full_fallback=meta.allow_full_fallback,
        )
        for meta in self._metas
    ]
    self._source_digest = source_plan_digest(self._wire_params, topology)

_publish_round_control(phase, failure, *, requires_reinit, retry_update)

Publish one fixed-count trainer-only control message and await ACKs.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _publish_round_control(
    self,
    phase: str,
    failure: BaseException | None,
    *,
    requires_reinit: bool,
    retry_update: bool,
) -> None:
    """Publish one fixed-count trainer-only control message and await ACKs."""
    if self._num_trainer_ranks <= 1:
        return
    if self._metadata_group is None:
        raise RuntimeError("nccl_m2n metadata group is unavailable")
    envelope = {
        "phase": phase,
        "round_id": self._round_id,
        "ok": failure is None,
        "error": (
            None if failure is None else f"{type(failure).__name__}: {failure}"
        ),
        "requires_reinit": requires_reinit,
        "retry_update": retry_update,
    }
    for rank in range(1, self._num_trainer_ranks):
        self._metadata_group.send_obj(envelope, rank)
    for rank in range(1, self._num_trainer_ranks):
        ack = self._metadata_group.recv_obj(rank)
        expected_ack = {
            "phase": f"{phase}_consumed",
            "round_id": self._round_id,
            "rank": rank,
            "error": None,
        }
        if ack == expected_ack:
            continue
        if (
            isinstance(ack, Mapping)
            and set(ack) == set(expected_ack)
            and ack.get("phase") == expected_ack["phase"]
            and ack.get("round_id") == self._round_id
            and ack.get("rank") == rank
            and isinstance(ack.get("error"), str)
            and ack["error"]
        ):
            raise RuntimeError(
                f"trainer rank {rank} rejected {phase}: {ack['error']}"
            )
        else:
            raise RuntimeError(
                f"trainer rank {rank} sent an invalid {phase} acknowledgement"
            )

_publish_round_dispatch(failure, *, requires_reinit=False)

Release non-senders only after rank zero dispatched worker receive.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _publish_round_dispatch(
    self,
    failure: BaseException | None,
    *,
    requires_reinit: bool = False,
) -> None:
    """Release non-senders only after rank zero dispatched worker receive."""
    self._publish_round_control(
        "round_dispatch",
        failure,
        requires_reinit=requires_reinit,
        retry_update=False,
    )

_publish_round_outcome(failure, *, requires_reinit=False, retry_update=False)

Tell non-sender trainer ranks whether worker finalization passed.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _publish_round_outcome(
    self,
    failure: BaseException | None,
    *,
    requires_reinit: bool = False,
    retry_update: bool = False,
) -> None:
    """Tell non-sender trainer ranks whether worker finalization passed."""
    self._publish_round_control(
        "round_outcome",
        failure,
        requires_reinit=requires_reinit,
        retry_update=retry_update,
    )

_receive_round_control(phase)

Receive and acknowledge one rank-zero control message for this round.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _receive_round_control(self, phase: str) -> tuple[str | None, bool, bool]:
    """Receive and acknowledge one rank-zero control message for this round."""
    if self._metadata_group is None:
        raise RuntimeError("nccl_m2n metadata group is unavailable")
    message = self._metadata_group.recv_obj(0)
    validation_error: str | None = None
    remote_error: str | None = None
    requires_reinit = False
    retry_update = False
    try:
        expected_fields = {
            "phase",
            "round_id",
            "ok",
            "error",
            "requires_reinit",
            "retry_update",
        }
        if not isinstance(message, Mapping) or set(message) != expected_fields:
            raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
        if message["phase"] != phase or message["round_id"] != self._round_id:
            raise RuntimeError(f"trainer rank zero sent an out-of-order {phase}")
        ok = message["ok"]
        remote_error = message["error"]
        requires_reinit = message["requires_reinit"]
        retry_update = message["retry_update"]
        if not isinstance(ok, bool) or ok != (remote_error is None):
            raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
        if not isinstance(requires_reinit, bool) or (ok and requires_reinit):
            raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
        if (
            not isinstance(retry_update, bool)
            or (ok and retry_update)
            or (requires_reinit and retry_update)
            or (phase != "round_outcome" and retry_update)
        ):
            raise RuntimeError(f"trainer rank zero sent an invalid {phase}")
        if not ok and (not isinstance(remote_error, str) or not remote_error):
            raise RuntimeError(f"trainer rank zero sent an invalid {phase} error")
    except RuntimeError as exc:
        validation_error = str(exc)
    self._metadata_group.send_obj(
        {
            "phase": f"{phase}_consumed",
            "round_id": self._round_id,
            "rank": self._trainer_rank,
            "error": validation_error,
        },
        0,
    )
    if validation_error is not None:
        raise RuntimeError(validation_error)
    return remote_error, requires_reinit, retry_update

_receive_round_dispatch()

Wait for rank zero before entering any source-side M2N collective.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _receive_round_dispatch(self) -> tuple[str | None, bool]:
    """Wait for rank zero before entering any source-side M2N collective."""
    error, requires_reinit, retry_update = self._receive_round_control(
        "round_dispatch"
    )
    assert not retry_update
    return error, requires_reinit

_receive_round_outcome()

Wait until trainer rank zero observed every worker's finish RPC.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _receive_round_outcome(self) -> tuple[str | None, bool, bool]:
    """Wait until trainer rank zero observed every worker's finish RPC."""
    return self._receive_round_control("round_outcome")

_send()

Reshard each local shard into its worker-planned destination.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _send(self) -> None:
    """Reshard each local shard into its worker-planned destination."""
    assert self.source is not None
    assert self._source_topology is not None
    source = cast(M2NWeightSource, self.source)
    m2n = self._m2n
    source_group_size = len(self._source_topology.groups[0])

    joined_routes = {
        route_index
        for route_index, route in enumerate(self._routes)
        if self._trainer_rank in route.source_ranks
    }
    if not joined_routes:
        return

    iterators: dict[int, Any] = {}
    inflight_buffers: list[Any] = []
    try:
        # Optimizer work may have used another stream. Complete it before
        # each provider materializes onto its persistent transfer stream.
        if joined_routes:
            device_index = torch.accelerator.current_device_index()
            torch.accelerator.synchronize(device_index)
        # Replay the same bounded schedule as the workers. Keeping the
        # buffers in this frame protects them until completion or abort.
        for wave in self._transfer_waves:
            for param_index in wave.issue_indices:
                group_index = self._source_topology.param_groups[param_index]
                route_index = self._param_routes[param_index]
                if route_index in joined_routes:
                    meta = self._metas[param_index]
                    communicator = self._route_communicators.get(route_index)
                    stream = self._route_streams.get(route_index)
                    if communicator is None or stream is None:
                        raise RuntimeError(
                            f"trainer rank {self._trainer_rank} is missing "
                            f"transfer route {route_index} for source group "
                            f"{group_index}"
                        )

                    with torch.cuda.stream(stream):
                        iterator = iterators.get(group_index)
                        if iterator is None:
                            iterator = iter(source.iter_group(group_index))
                            iterators[group_index] = iterator
                        try:
                            name, buffer = next(iterator)
                        except StopIteration as exc:
                            raise RuntimeError(
                                f"source group {group_index} ended before "
                                f"parameter {meta.name!r} at manifest position "
                                f"{param_index}"
                            ) from exc
                        if name != meta.name:
                            raise RuntimeError(
                                f"source group {group_index} yielded {name!r} "
                                f"for manifest position {param_index}, expected "
                                f"{meta.name!r}"
                            )
                        if buffer is None:
                            raise RuntimeError(
                                f"source group {group_index} yielded no local "
                                f"buffer for owned parameter {meta.name!r}"
                            )
                        validate_local_buffer(m2n, meta, buffer)
                        src_mesh, src_placements = resolve_layout(
                            meta.source_layout.mesh,
                            meta.source_layout.placements,
                        )
                        destination = self._destination_plan[param_index]
                        dst_mesh, dst_placements = resolve_layout(
                            M2NMesh(
                                destination.mesh_dims,
                                source_group_size,
                            ),
                            destination.placements,
                        )
                        raw_pointer_args = {}
                        if isinstance(buffer, int):
                            raw_pointer_args = {
                                "src_local_shape": expected_local_shape(meta),
                                "src_dtype": meta.dtype,
                            }
                        m2n.reshard(
                            buffer,
                            None,
                            communicator,
                            stream,
                            src_mesh=to_mesh(m2n, src_mesh),
                            src_placements=to_placements(m2n, src_placements),
                            dst_mesh=to_mesh(m2n, dst_mesh),
                            dst_placements=to_placements(m2n, dst_placements),
                            handle=self._handle,
                            **raw_pointer_args,
                        )
                        record_stream = getattr(buffer, "record_stream", None)
                        if callable(record_stream):
                            record_stream(stream)
                        elif not isinstance(buffer, int):
                            inflight_buffers.append(buffer)

        for group_index, iterator in iterators.items():
            route_index = next(
                index
                for index, route in enumerate(self._routes)
                if route.source_group_index == group_index
                and index in joined_routes
            )
            stream = self._route_streams[route_index]
            with torch.cuda.stream(stream):
                try:
                    name, _ = next(iterator)
                except StopIteration:
                    continue
                raise RuntimeError(
                    f"source group {group_index} yielded extra parameter {name!r}"
                )

        # Completion protects source-buffer lifetime and prevents optimizer
        # mutation before worker finalization.
        for route_index in range(len(self._routes)):
            stream = self._route_streams.get(route_index)
            if stream is not None:
                stream.synchronize()
    except BaseException:
        self._abort_data_plane()
        raise

_send_weight_round(*, retry_finish)

Run one complete update attempt; return whether to replay it.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _send_weight_round(self, *, retry_finish: bool) -> bool:
    """Run one complete update attempt; return whether to replay it."""
    if not self.is_sender:
        failure: BaseException | None = None
        retry_update = False
        try:
            error, requires_reinit = self._receive_round_dispatch()
        except BaseException as exc:
            failure = exc
            self._abort_data_plane()
        else:
            if requires_reinit:
                self._abort_data_plane()
            if error is not None:
                failure = RuntimeError(
                    "nccl_m2n worker update was not dispatched: " + error
                )
        if failure is None:
            try:
                self._send()
            except BaseException as exc:
                failure = exc
        try:
            error, requires_reinit, retry_update = self._receive_round_outcome()
        except BaseException as exc:
            self._abort_data_plane()
            if failure is None:
                failure = exc
            else:
                logger.exception(
                    "failed to consume nccl_m2n round outcome after local failure"
                )
        else:
            if requires_reinit:
                self._abort_data_plane()
            if error is not None and failure is None and not retry_update:
                failure = RuntimeError(
                    "nccl_m2n worker update failed on trainer rank zero: " + error
                )
        finally:
            self._round_id += 1
        if failure is not None:
            raise failure
        return retry_update

    executor: ThreadPoolExecutor | None = None
    dispatch_attempted = False
    transfer_completed = False
    try:
        self.client.start_weight_update()
        executor = ThreadPoolExecutor(
            max_workers=1, thread_name_prefix="m2n-weight-sync"
        )
        update = executor.submit(
            self.client.update_weights,
            {"names": [m.name for m in self._metas]},
        )
        # Surface an immediate worker-side rejection before trainers
        # enter the collective sequence.
        if update.done():
            update.result()
        dispatch_attempted = True
        self._publish_round_dispatch(None)
        self._send()
        update.result()
        transfer_completed = True
        self.client.finish_weight_update()
    except BaseException as exc:
        requires_reinit = dispatch_attempted and not transfer_completed
        retry_update = transfer_completed and retry_finish
        if requires_reinit:
            self._abort_data_plane()
        if not dispatch_attempted:
            try:
                dispatch_attempted = True
                self._publish_round_dispatch(exc, requires_reinit=False)
            except BaseException:
                self._abort_data_plane()
                requires_reinit = True
                logger.exception(
                    "failed to publish nccl_m2n round dispatch failure"
                )
        try:
            self._publish_round_outcome(
                exc,
                requires_reinit=requires_reinit,
                retry_update=retry_update,
            )
        except BaseException:
            self._abort_data_plane()
            logger.exception("failed to publish nccl_m2n round failure")
        if retry_update:
            return True
        raise
    else:
        try:
            self._publish_round_outcome(None)
        except BaseException:
            self._abort_data_plane()
            raise
        return False
    finally:
        if executor is not None:
            executor.shutdown(wait=False, cancel_futures=True)
        self._round_id += 1

_sync_trainer_init_outcome(metadata_group, init_future)

Require every trainer to observe rank zero's worker-init result.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _sync_trainer_init_outcome(
    self,
    metadata_group: StatelessProcessGroup,
    init_future: Future[None] | None,
) -> None:
    """Require every trainer to observe rank zero's worker-init result."""
    if self.is_sender:
        if init_future is None:
            raise RuntimeError("trainer rank zero is missing its worker-init RPC")
        failure: BaseException | None = None
        try:
            init_future.result()
        except BaseException as exc:
            failure = exc
        message = {
            "phase": "trainer_init_outcome",
            "plan_token": self._destination_commit_token,
            "ok": failure is None,
            "error": (
                None if failure is None else f"{type(failure).__name__}: {failure}"
            ),
        }
        for rank in range(1, self._num_trainer_ranks):
            metadata_group.send_obj(message, rank)
        if failure is not None:
            raise failure
        return

    message = metadata_group.recv_obj(0)
    expected_fields = {"phase", "plan_token", "ok", "error"}
    if not isinstance(message, Mapping) or set(message) != expected_fields:
        raise RuntimeError("trainer rank zero sent an invalid init outcome")
    if (
        message["phase"] != "trainer_init_outcome"
        or message["plan_token"] != self._destination_commit_token
    ):
        raise RuntimeError("trainer rank zero sent an invalid init outcome")
    ok = message["ok"]
    error = message["error"]
    if not isinstance(ok, bool) or ok != (error is None):
        raise RuntimeError("trainer rank zero sent an invalid init outcome")
    if not ok:
        if not isinstance(error, str) or not error:
            raise RuntimeError("trainer rank zero sent an invalid init outcome")
        raise RuntimeError(
            "nccl_m2n worker initialization failed on trainer rank zero: " + error
        )

_worker_init_info(init_info)

Handshake payload: rendezvous, layouts, and the transfer plan.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def _worker_init_info(self, init_info: M2NTrainerInitInfo) -> dict[str, Any]:
    """Handshake payload: rendezvous, layouts, and the transfer plan."""
    payload: dict[str, Any] = {
        "schema_version": M2N_WIRE_SCHEMA_VERSION,
        "master_address": init_info.master_address,
        "master_port": init_info.master_port,
        "nccl_unique_id_b64": init_info.nccl_unique_id_b64,
        "nccl_unique_ids_b64": init_info.nccl_unique_ids_b64,
        "rank_offset": init_info.num_trainer_ranks,
        "world_size": init_info.world_size,
        "dst_mesh_dims": (
            None
            if init_info.destination_mesh_constraint is None
            else list(init_info.destination_mesh_constraint)
        ),
        "source_digest": self._source_digest,
        "source_topology": (
            None
            if self._source_topology is None
            else self._source_topology.to_dict()
        ),
        "params": [param.to_dict() for param in self._wire_params],
        "max_cta": init_info.max_cta,
    }
    return {key: value for key, value in payload.items() if value is not None}

plan_summary()

Return the authenticated static plan as JSON-safe run metadata.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def plan_summary(self) -> dict[str, Any]:
    """Return the authenticated static plan as JSON-safe run metadata."""
    if not self._destination_commit_token or not self._destination_plan:
        raise RuntimeError("nccl_m2n transfer plan is not initialized")
    assert self._source_topology is not None
    mode_counts: dict[str, int] = {}
    mesh_counts: dict[tuple[int, int], int] = {}
    logical_bytes_by_mode: dict[str, int] = {}
    receiver_bytes_by_mode: dict[str, int] = {}
    for meta, destination in zip(self._metas, self._destination_plan, strict=True):
        mode = destination.mode
        element_size = torch.empty((), dtype=meta.dtype).element_size()
        logical_bytes = math.prod(meta.shape) * element_size
        receiver_bytes = (
            math.prod(destination.local_shape)
            * element_size
            * math.prod(destination.mesh_dims)
        )
        mode_counts[mode] = mode_counts.get(mode, 0) + 1
        mesh_counts[destination.mesh_dims] = (
            mesh_counts.get(destination.mesh_dims, 0) + 1
        )
        logical_bytes_by_mode[mode] = (
            logical_bytes_by_mode.get(mode, 0) + logical_bytes
        )
        receiver_bytes_by_mode[mode] = (
            receiver_bytes_by_mode.get(mode, 0) + receiver_bytes
        )
    logical_global_bytes = sum(logical_bytes_by_mode.values())
    planned_receiver_payload_bytes = sum(receiver_bytes_by_mode.values())
    return {
        "wire_schema_version": M2N_WIRE_SCHEMA_VERSION,
        "source_digest": self._source_digest,
        "destination_commit_token": self._destination_commit_token,
        "destination_mesh_constraint": (
            None
            if self._destination_mesh_constraint is None
            else list(self._destination_mesh_constraint)
        ),
        "destination_mesh_counts": [
            {"dims": list(dims), "parameter_count": count}
            for dims, count in mesh_counts.items()
        ],
        "source_group_count": len(self._source_topology.groups),
        "parameter_count": len(self._metas),
        "transfer_communicators": [
            {
                "source_group_index": route.source_group_index,
                "source_ranks": list(route.source_ranks),
                "destination_ranks": list(route.worker_ranks),
                "param_indices": list(route.param_indices),
            }
            for route in self._routes
        ],
        "transfer_waves": [
            {
                "issue_indices": list(wave.issue_indices),
                "consume_index": wave.consume_index,
            }
            for wave in self._transfer_waves
        ],
        # Each complete logical parameter is counted once.
        "logical_global_bytes": logical_global_bytes,
        # Parameter counts, not byte counts.
        "destination_mode_counts": mode_counts,
        "logical_global_bytes_by_destination_mode": logical_bytes_by_mode,
        # Aggregate destination tensor bytes implied by the agreed plan;
        # this is not measured NCCL, InfiniBand, or protocol traffic.
        "planned_receiver_payload_bytes": planned_receiver_payload_bytes,
        "planned_receiver_payload_bytes_by_destination_mode": (
            receiver_bytes_by_mode
        ),
    }

send_weights()

Drive one update, replaying it once after a reset finish failure.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def send_weights(self) -> None:
    """Drive one update, replaying it once after a reset finish failure."""
    participates = any(
        self._trainer_rank in route.source_ranks for route in self._routes
    )
    if not self._routes or (participates and self._handle is None):
        raise RuntimeError("nccl_m2n trainer engine is not initialized")

    for attempt in range(_FINISH_RETRY_LIMIT + 1):
        if not self._send_weight_round(retry_finish=attempt < _FINISH_RETRY_LIMIT):
            return
    raise AssertionError("unreachable nccl_m2n finish retry state")

shutdown()

Finish pending GPU work and release M2N communication state.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
def shutdown(self) -> None:
    """Finish pending GPU work and release M2N communication state."""
    handle, self._handle = self._handle, None
    legacy_group, self.group = self.group, None
    communicators = list(self._route_communicators.values())
    if legacy_group is not None and all(
        legacy_group is not communicator for communicator in communicators
    ):
        communicators.append(legacy_group)
    self._route_communicators = {}
    streams, self._route_streams = self._route_streams, {}
    self._routes = ()
    self._param_routes = ()
    self._transfer_waves = ()
    self._source_topology = None
    self._metadata_group = None
    self._destination_plan = []
    self._destination_commit_token = ""
    self._destination_mesh_constraint = None
    self._m2n = None

    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 communicators:
        attempt(torch.accelerator.synchronize, "failed to synchronize nccl_m2n")
    if handle is not None:
        attempt(handle.destroy, "failed to destroy nccl_m2n handle")
    for communicator in reversed(communicators):
        attempt(
            communicator.abort,
            "failed to abort nccl_m2n communicator",
        )
    # Keep stream wrappers alive through communicator cleanup.
    del streams
    if first_error is not None:
        raise first_error

trainer_init(init_info, *, client, source=None) classmethod

Build the engine and rendezvous with the inference side.

Runs on every trainer rank. Rank 0 additionally ships the transfer plan to the workers and drives the control plane; the other ranks only build local state and join their declared source-group communicators.

The ordering here is not arbitrary -- see the comments inline. Build a local validation envelope, start the worker RPC without waiting, join the metadata rendezvous, and require unanimous source and destination plan commits before constructing NCCL or the M2N handle.

Source code in vllm/distributed/weight_transfer/m2n_trainer.py
@classmethod
def trainer_init(
    cls,
    init_info: M2NTrainerInitInfo,
    *,
    client: VLLMWeightSyncClient,
    source: WeightSource | None = None,
) -> Self:
    """Build the engine and rendezvous with the inference side.

    Runs on *every* trainer rank. Rank 0 additionally ships the transfer
    plan to the workers and drives the control plane; the other ranks only
    build local state and join their declared source-group communicators.

    The ordering here is not arbitrary -- see the comments inline. Build a
    local validation envelope, start the worker RPC without waiting, join
    the metadata rendezvous, and require unanimous source and destination
    plan commits before constructing NCCL or the M2N handle.
    """
    # Trainer-side weight transfer engine for this rank. `__init__` only
    # holds the client and source; the communicator, meshes, and M2N handle
    # are filled in below.
    engine = cls(
        client=client,
        source=cast(M2NWeightSource, source),
        is_sender=init_info.is_sender,
    )
    engine._initialize(init_info, source)
    return engine