modelexpress-client 0.7.0

Client library for Model Express gRPC server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

from importlib import import_module

import pytest
import torch
from modelexpress.refit.reshard.rendezvous import (
    MxReshardRendezvous,
    unwrap_rendezvous_blob,
)
from modelexpress.refit.reshard.slice_plan import Shard
from modelexpress.refit.reshard.transfer_plan import SourceInfo, plan_transfer
from modelexpress_rl.inference.reshard.megatron import (
    MegatronReshardReceiver,
    MegatronTargetLayout,
    MegatronTargetSpec,
    lower_megatron_target,
)
from modelexpress_rl.train.engines.megatron import (
    MegatronAliasInput,
    MegatronPublishedTensorSpec,
    MegatronTensorSpec,
    build_hf_aliases,
    build_megatron_reshard_manifest,
    publish_megatron_reshard_view,
    publish_registered_shard_table,
)


def _bf16_sources() -> tuple[dict[str, SourceInfo], list[torch.Tensor]]:
    tensors = [
        torch.zeros((8, 8), dtype=torch.bfloat16),
        torch.zeros((8, 8), dtype=torch.bfloat16),
        torch.zeros((8, 8), dtype=torch.bfloat16),
        torch.zeros((8, 8), dtype=torch.bfloat16),
        torch.zeros((8,), dtype=torch.bfloat16),
    ]
    sources = {
        "expert_fc1": SourceInfo(
            (16, 8),
            torch.bfloat16,
            2,
            [
                Shard((0, 0), (8, 8), "tp0", tensors[0].data_ptr(), 2),
                Shard((8, 0), (8, 8), "tp1", tensors[1].data_ptr(), 2),
            ],
        ),
        "expert_fc2": SourceInfo(
            (8, 16),
            torch.bfloat16,
            2,
            [
                Shard((0, 0), (8, 8), "tp0", tensors[2].data_ptr(), 2),
                Shard((0, 8), (8, 8), "tp1", tensors[3].data_ptr(), 2),
            ],
        ),
        "norm": SourceInfo(
            (8,),
            torch.bfloat16,
            2,
            [Shard((0,), (8,), "tp0", tensors[4].data_ptr(), 2)],
        ),
    }
    return sources, tensors


def _specs() -> list[MegatronTargetSpec]:
    return [
        MegatronTargetSpec(
            "expert_fc1",
            "expert_column",
            (16, 8),
            torch.bfloat16,
            shard_axis=0,
            descriptor_extras={"expert_layout": "grouped"},
        ),
        MegatronTargetSpec(
            "expert_fc2",
            "expert_row",
            (8, 16),
            torch.bfloat16,
            shard_axis=1,
            descriptor_extras={"expert_layout": "grouped"},
        ),
        MegatronTargetSpec("norm", "replicated", (8,), torch.bfloat16),
    ]


@pytest.mark.parametrize(
    ("tp_size", "tp_rank", "expected_bytes", "expected_segments"),
    [(2, 1, 272, 3), (4, 2, 144, 10)],
)
def test_megatron_target_lowers_to_destination_owned_bytes(
    tp_size: int,
    tp_rank: int,
    expected_bytes: int,
    expected_segments: int,
):
    sources, keepalive = _bf16_sources()
    capture, layouts = lower_megatron_target(
        _specs(), MegatronTargetLayout(tp_size=tp_size, tp_rank=tp_rank)
    )

    plan = plan_transfer(capture, sources)

    assert plan.fallback == []
    assert plan.bytes_planned() == expected_bytes
    assert len(plan.segments) == expected_segments
    assert layouts["expert_fc1"][0] == (16 // tp_size, 8)
    assert layouts["expert_fc2"][0] == (8, 16 // tp_size)
    assert layouts["norm"][0] == (8,)
    assert all(tensor.data_ptr() for tensor in keepalive)


def test_leading_axis_experts_preserve_expert_dimension():
    capture, layouts = lower_megatron_target(
        [
            MegatronTargetSpec(
                "fc1",
                "expert_column",
                (4, 16, 8),
                torch.bfloat16,
                descriptor_extras={"expert_layout": "leading_axis"},
            ),
            MegatronTargetSpec(
                "fc2",
                "expert_row",
                (4, 8, 16),
                torch.bfloat16,
                descriptor_extras={"expert_layout": "leading_axis"},
            ),
        ],
        MegatronTargetLayout(tp_size=4, tp_rank=3),
    )

    assert layouts == {
        "fc1": ((4, 4, 8), torch.bfloat16),
        "fc2": ((4, 8, 4), torch.bfloat16),
    }
    assert capture.copies[0].op_chain == (("narrow", (1, 12, 4), ()),)
    assert capture.copies[1].op_chain == (("narrow", (2, 12, 4), ()),)


def test_non_divisible_target_geometry_fails_closed():
    with pytest.raises(ValueError, match="not divisible"):
        lower_megatron_target(
            [
                MegatronTargetSpec(
                    "column",
                    "column",
                    (10, 8),
                    torch.bfloat16,
                )
            ],
            MegatronTargetLayout(tp_size=4, tp_rank=0),
        )


@pytest.mark.parametrize("extent", [4.5, True])
def test_target_spec_rejects_non_integral_global_shape(extent):
    with pytest.raises(ValueError, match="positive integer extents"):
        MegatronTargetSpec("column", "column", (extent, 8), torch.bfloat16)


@pytest.mark.parametrize(
    ("tp_size", "tp_rank", "message"),
    [
        (2.5, 0, "tp_size must be an integer"),
        (True, 0, "tp_size must be an integer"),
        (2, 0.5, "tp_rank must be an integer"),
        (2, False, "tp_rank must be an integer"),
    ],
)
def test_target_layout_rejects_non_integral_geometry(tp_size, tp_rank, message):
    with pytest.raises(ValueError, match=message):
        MegatronTargetLayout(tp_size=tp_size, tp_rank=tp_rank)


def test_legacy_megatron_publisher_imports_remain_available():
    legacy_aliases = import_module(
        "modelexpress.refit.reshard.megatron_aliases"
    )
    legacy_publisher = import_module(
        "modelexpress.refit.reshard.megatron_publisher"
    )

    assert legacy_aliases.MegatronAliasInput is MegatronAliasInput
    assert legacy_aliases.build_hf_aliases is build_hf_aliases
    assert (
        legacy_publisher.publish_registered_shard_table
        is publish_registered_shard_table
    )


def test_receiver_seam_validates_manifest_and_invokes_installer():
    installed = []
    receiver = object.__new__(MegatronReshardReceiver)
    receiver._target_specs = _specs()
    receiver._target_layout = MegatronTargetLayout(tp_size=4, tp_rank=0)
    receiver._install_native = installed.append
    manifest = [
        ("expert_fc1", torch.bfloat16, (16, 8)),
        ("expert_fc2", torch.bfloat16, (8, 16)),
        ("norm", torch.bfloat16, (8,)),
    ]

    capture, layouts = receiver._capture(manifest)
    buffers = {
        name: torch.empty(shape, dtype=dtype)
        for name, (shape, dtype) in layouts.items()
    }
    receiver._install(buffers)

    assert len(capture.copies) == 3
    assert installed == [buffers]


def test_receiver_seam_rejects_stale_manifest_geometry():
    receiver = object.__new__(MegatronReshardReceiver)
    receiver._target_specs = _specs()
    receiver._target_layout = MegatronTargetLayout(tp_size=2, tp_rank=0)

    with pytest.raises(RuntimeError, match="disagrees"):
        receiver._capture(
            [
                ("expert_fc1", torch.bfloat16, (8, 8)),
                ("expert_fc2", torch.bfloat16, (8, 16)),
                ("norm", torch.bfloat16, (8,)),
            ]
        )


class _Manager:
    agent_name = "trainer-r3"
    nixl_metadata = b"agent-metadata"


class _PublishClient:
    def __init__(self):
        self.worker = None

    def publish_metadata(self, _identity, worker, _worker_id):
        self.worker = worker
        return "source-id"

    def update_status(self, **_kwargs):
        return True


def test_manifest_builder_reuses_registered_tensor_addresses():
    tensor = torch.zeros((8, 8), dtype=torch.bfloat16)
    published = build_hf_aliases(
        [
            MegatronTensorSpec(
                name="column",
                tensor=tensor,
                role="column",
                hf_names=("column",),
                global_shape=(16, 8),
                placement_kind="SHARD",
                shard_axis=0,
                local_shard_range=(8, 16),
            )
        ],
        agent_name="trainer-r3",
    )

    manifest = build_megatron_reshard_manifest(
        manager=_Manager(),
        published=published,
        metadata_endpoint="10.0.0.3:19003",
    )

    payload = unwrap_rendezvous_blob(manifest.blob)
    assert (payload.agent_metadata, payload.agent_name, payload.metadata_endpoint) == (
        b"agent-metadata",
        "trainer-r3",
        "10.0.0.3:19003",
    )
    assert payload.tensors[0].shards[0].addr == tensor.data_ptr()
    assert payload.tensors[0].shards[0].shard_offset == (8, 0)


def test_existing_reshard_publisher_remains_compatible():
    client = _PublishClient()
    rendezvous = MxReshardRendezvous(
        client,
        role="trainer",
        rank=3,
        model_name="model",
        worker_id="worker-3",
    )
    tensor = torch.zeros((8, 8), dtype=torch.bfloat16)

    try:
        source_id = publish_megatron_reshard_view(
            manager=_Manager(),
            rendezvous=rendezvous,
            tensors={"column": tensor},
            specs=[
                MegatronPublishedTensorSpec(
                    name="column",
                    global_shape=(16, 8),
                    shard_axis=0,
                    local_shard_range=(8, 16),
                )
            ],
            metadata_endpoint="10.0.0.3:19003",
        )
    finally:
        rendezvous.close()

    assert source_id == "source-id"
    payload = unwrap_rendezvous_blob(client.worker.nixl_metadata)
    assert payload.tensors[0].shards[0].addr == tensor.data_ptr()


def test_manifest_builder_rejects_duplicate_tensor_names():
    tensor = torch.zeros((8, 8), dtype=torch.bfloat16)
    published = build_hf_aliases(
        [
            MegatronTensorSpec(
                name="column",
                tensor=tensor,
                role="column",
                hf_names=("column",),
                global_shape=(8, 8),
                placement_kind="REPLICATE",
                shard_axis=None,
                local_shard_range=None,
            )
        ],
        agent_name="trainer-r3",
    )

    with pytest.raises(ValueError, match="duplicate published tensor"):
        build_megatron_reshard_manifest(
            manager=_Manager(),
            published=[published[0], published[0]],
            metadata_endpoint="10.0.0.3:19003",
        )


def test_gated_aliases_split_each_tp_shard_into_hf_gate_and_up():
    fused = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)

    gate, up = build_hf_aliases(
        [
            MegatronAliasInput(
                name="linear_fc1.weight",
                tensor=fused,
                role="gated_mlp_column",
                hf_names=("gate_proj.weight", "up_proj.weight"),
                global_shape=(16, 4),
                placement_kind="SHARD",
                shard_axis=0,
                local_shard_range=(8, 16),
                extras={"gated_mlp_order": "gate_then_up"},
            )
        ],
        agent_name="trainer-tp1",
    )

    assert gate.full_shape == up.full_shape == (8, 4)
    assert gate.shards[0].shape == up.shards[0].shape == (4, 4)
    assert gate.shards[0].shard_offset == up.shards[0].shard_offset == (4, 0)
    assert gate.shards[0].addr == fused.data_ptr()
    assert up.shards[0].addr == fused[4:].data_ptr()


def test_an_unknown_fused_gate_up_order_is_rejected():
    """The halves map to `hf_names` positionally, so an up-then-gate layout would
    publish the gate projection's bytes under the up projection's name. No digest
    gate can see that: both names receive the bytes their publisher advertised."""
    fused = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)

    with pytest.raises(ValueError, match="gated_mlp_order"):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_fc1.weight",
                    tensor=fused,
                    role="gated_mlp_column",
                    hf_names=("gate_proj.weight", "up_proj.weight"),
                    global_shape=(16, 4),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(8, 16),
                    extras={"gated_mlp_order": "up_then_gate"},
                )
            ],
            agent_name="trainer-tp1",
        )


def test_a_missing_fused_gate_up_order_is_rejected():
    """Absent metadata is not evidence of gate-then-up storage."""
    fused = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)

    with pytest.raises(ValueError, match="gated_mlp_order"):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_fc1.weight",
                    tensor=fused,
                    role="gated_mlp_column",
                    hf_names=("gate_proj.weight", "up_proj.weight"),
                    global_shape=(16, 4),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(8, 16),
                )
            ],
            agent_name="trainer-tp1",
        )


def test_gated_aliases_reject_inconsistent_declared_global_shape():
    fused = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)

    with pytest.raises(
        ValueError, match=r"linear_fc1\.weight: derived gate/up shape"
    ):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_fc1.weight",
                    tensor=fused,
                    role="gated_mlp_column",
                    hf_names=("gate_proj.weight", "up_proj.weight"),
                    global_shape=(16, 5),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(8, 16),
                    extras={"gated_mlp_order": "gate_then_up"},
                )
            ],
            agent_name="trainer-tp1",
        )


def test_qkv_aliases_expose_hf_head_ranges_without_copy():
    qkv = torch.arange(48, dtype=torch.bfloat16).reshape(12, 4)

    q, k, v = build_hf_aliases(
        [
            MegatronAliasInput(
                name="linear_qkv.weight",
                tensor=qkv,
                role="qkv_column",
                hf_names=("q_proj.weight", "k_proj.weight", "v_proj.weight"),
                global_shape=(24, 4),
                placement_kind="SHARD",
                shard_axis=0,
                local_shard_range=(12, 24),
                extras={
                    "num_heads_local": "4",
                    "num_kv_heads_local": "1",
                    "head_dim": "2",
                },
            )
        ],
        agent_name="trainer-tp1",
    )

    assert q.full_shape == (16, 4)
    assert k.full_shape == v.full_shape == (4, 4)
    assert q.shards[0].shape == (8, 4)
    assert k.shards[0].shape == v.shards[0].shape == (2, 4)
    assert q.shards[0].shard_offset == (8, 0)
    assert k.shards[0].shard_offset == v.shards[0].shard_offset == (2, 0)
    assert q.shards[0].addr == qkv.data_ptr()
    assert k.shards[0].addr == qkv[8:].data_ptr()
    assert v.shards[0].addr == qkv[10:].data_ptr()


def test_qkv_aliases_report_missing_extras_with_tensor_name():
    qkv = torch.arange(48, dtype=torch.bfloat16).reshape(12, 4)

    with pytest.raises(
        ValueError,
        match=r"linear_qkv\.weight: QKV aliasing requires extras",
    ):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_qkv.weight",
                    tensor=qkv,
                    role="qkv_column",
                    hf_names=("q_proj.weight", "k_proj.weight", "v_proj.weight"),
                    global_shape=(24, 4),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(12, 24),
                    extras={"head_dim": "2"},
                )
            ],
            agent_name="trainer-tp1",
        )


def test_a_shard_range_beyond_the_global_extent_is_rejected():
    """A range of (16, 24) against a global extent of 16 clears every other
    check: it is the right width, it divides evenly, and it yields source rank 2
    of a 2-rank group. The alias would then describe bytes past the end of the
    tensor it claims to be part of."""
    fused = torch.arange(32, dtype=torch.bfloat16).reshape(8, 4)

    with pytest.raises(ValueError, match="outside the global extent"):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_fc1.weight",
                    tensor=fused,
                    role="gated_mlp_column",
                    hf_names=("gate_proj.weight", "up_proj.weight"),
                    global_shape=(16, 4),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(16, 24),
                    extras={"gated_mlp_order": "gate_then_up"},
                )
            ],
            agent_name="trainer-tp1",
        )


def test_a_non_contiguous_qkv_tensor_is_rejected():
    """A shard is published as one address plus a shape, so a strided view would
    advertise bytes that belong to the columns it skipped."""
    strided = torch.arange(96, dtype=torch.bfloat16).reshape(12, 8)[:, :4]
    assert not strided.is_contiguous()

    with pytest.raises(ValueError, match="requires contiguous storage"):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_qkv.weight",
                    tensor=strided,
                    role="qkv_column",
                    hf_names=("q_proj.weight", "k_proj.weight", "v_proj.weight"),
                    global_shape=(24, 4),
                    placement_kind="SHARD",
                    shard_axis=0,
                    local_shard_range=(12, 24),
                    extras={
                        "num_heads_local": "4",
                        "num_kv_heads_local": "1",
                        "head_dim": "2",
                    },
                )
            ],
            agent_name="trainer-tp1",
        )


def test_a_non_contiguous_ordinary_alias_is_rejected():
    """The single-name path publishes the tensor's own address, so a transpose
    would hand a reader the untransposed bytes."""
    transposed = torch.arange(32, dtype=torch.bfloat16).reshape(4, 8).t()
    assert not transposed.is_contiguous()

    with pytest.raises(ValueError, match="requires contiguous storage"):
        build_hf_aliases(
            [
                MegatronAliasInput(
                    name="linear_fc2.weight",
                    tensor=transposed,
                    role="row",
                    hf_names=("down_proj.weight",),
                    global_shape=(8, 4),
                    placement_kind="REPLICATE",
                    shard_axis=None,
                    local_shard_range=None,
                )
            ],
            agent_name="trainer-tp1",
        )