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
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

"""Tests for ModelStreamerStrategy."""

import sys
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest
import torch

from modelexpress import p2p_pb2
from modelexpress.adapter import EngineAdapter
from modelexpress.adapter import StrategyFailed
from modelexpress.load_strategy.context import LoadResult


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


class _FakeAdapter(EngineAdapter):
    def __init__(self, *, is_cuda_alike: bool = True):
        self._is_cuda_alike = is_cuda_alike

    def discover_tensors(self, result: LoadResult):
        return {}

    def is_cuda_alike(self) -> bool:
        return self._is_cuda_alike

    def after_weight_iter_load(self, result: LoadResult):
        return result

    def apply_weight_iter(self, result: LoadResult, weights_iter):
        if result.model is not None:
            result.model.load_weights(weights_iter)
        return result

    def build_model_streamer_weight_iter(self, model_uri: str, model=None):
        return iter(())


def _make_load_context(**overrides):
    """Return a LoadContext with mocked dependencies."""
    from modelexpress.load_strategy import LoadContext

    defaults = dict(
        model_config=MagicMock(),
        load_config=MagicMock(),
        target_device=torch.device("cpu"),
        global_rank=0,
        worker_rank=0,
        local_rank=0,
        device_id=0,
        identity=p2p_pb2.SourceIdentity(
            model_name="test-model",
            tensor_parallel_size=1,
        ),
        mx_client=MagicMock(),
        worker_id="test-worker",
        adapter=_FakeAdapter(),
    )
    defaults.update(overrides)
    return LoadContext(**defaults)


# ---------------------------------------------------------------------------
# TestModelStreamerIsAvailable
# ---------------------------------------------------------------------------


class TestModelStreamerIsAvailable:
    def _make_strategy(self):
        from modelexpress.load_strategy.model_streamer_strategy import ModelStreamerStrategy
        return ModelStreamerStrategy()

    def test_available_with_s3_uri(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "s3://bucket/model"}):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is True

    def test_available_with_local_path_env(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "/models/deepseek"}):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is True

    def test_available_with_gcs_uri(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "gs://bucket/model"}):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is True

    def test_available_with_local_path(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "/models/deepseek-ai/DeepSeek-V3"}):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is True

    def test_available_with_hf_model_id(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "deepseek-ai/DeepSeek-V3"}):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is True

    def test_unavailable_no_env(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {}, clear=True):
            with patch("importlib.util.find_spec", return_value=MagicMock()):
                assert strategy.is_available(ctx) is False

    def test_unavailable_no_package(self):
        ctx = _make_load_context()
        strategy = self._make_strategy()
        with patch.dict("os.environ", {"MX_MODEL_URI": "s3://bucket/model"}):
            with patch("importlib.util.find_spec", return_value=None):
                assert strategy.is_available(ctx) is False


# ---------------------------------------------------------------------------
# TestModelStreamerLoad
# ---------------------------------------------------------------------------


class TestModelStreamerLoad:
    def _make_strategy(self):
        from modelexpress.load_strategy.model_streamer_strategy import ModelStreamerStrategy
        return ModelStreamerStrategy()

    def _make_ctx_with_uri(self, *, model_weights=None, model="ignored"):
        model_config = MagicMock()
        model_config.model_weights = model_weights
        model_config.model = model
        return _make_load_context(model_config=model_config)

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_success_path_s3(self, mock_register):
        model = MagicMock()
        ctx = self._make_ctx_with_uri(model_weights="s3://bucket/model")
        strategy = self._make_strategy()

        with patch(
            "modelexpress.load_strategy.model_streamer_strategy."
            "ModelStreamerStrategy._stream_weights"
        ) as mock_stream:
            mock_stream.return_value = iter([
                ("layer.0.weight", torch.randn(4, 4)),
                ("layer.1.weight", torch.randn(4, 4)),
            ])
            result = strategy.load(model, ctx)

        assert isinstance(result, LoadResult)
        assert result.model is model
        mock_stream.assert_called_once_with("s3://bucket/model", ctx, model)
        model.load_weights.assert_called_once()
        mock_register.assert_called_once_with(result, ctx)

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_success_path_local_falls_back_to_model(self, mock_register):
        """When model_weights is unset, the URI comes from model_config.model."""
        model = MagicMock()
        ctx = self._make_ctx_with_uri(model_weights=None, model="/models/llama")
        strategy = self._make_strategy()

        with patch(
            "modelexpress.load_strategy.model_streamer_strategy."
            "ModelStreamerStrategy._stream_weights"
        ) as mock_stream:
            mock_stream.return_value = iter([
                ("layer.0.weight", torch.randn(4, 4)),
            ])
            result = strategy.load(model, ctx)

        assert isinstance(result, LoadResult)
        mock_stream.assert_called_once_with("/models/llama", ctx, model)

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_success_path_sglang_model_path_without_model(self, mock_register):
        """SGLang ModelConfig exposes model_path, not model."""
        model = MagicMock()
        model_config = SimpleNamespace(
            model_weights=None,
            model_path="/models/deepseek",
        )
        ctx = _make_load_context(model_config=model_config)
        strategy = self._make_strategy()

        with patch(
            "modelexpress.load_strategy.model_streamer_strategy."
            "ModelStreamerStrategy._stream_weights"
        ) as mock_stream:
            mock_stream.return_value = iter([
                ("layer.0.weight", torch.randn(4, 4)),
            ])
            result = strategy.load(model, ctx)

        assert isinstance(result, LoadResult)
        mock_stream.assert_called_once_with("/models/deepseek", ctx, model)

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_uri_from_env_with_hf_model_as_fallback(self, mock_register):
        model = MagicMock()
        ctx = self._make_ctx_with_uri(
            model_weights=None, model="Qwen/Qwen2.5-0.5B"
        )
        strategy = self._make_strategy()

        with patch.dict("os.environ", {"MX_MODEL_URI": "s3://other/path-in-env"}):
            with patch(
                "modelexpress.load_strategy.model_streamer_strategy."
                "ModelStreamerStrategy._stream_weights"
            ) as mock_stream:
                mock_stream.side_effect = RuntimeError("expected")
                with pytest.raises(StrategyFailed, match="expected"):
                    strategy.load(model, ctx)

        mock_stream.assert_called_once_with("s3://other/path-in-env", ctx, model)

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_raises_strategy_failed_on_error(self, mock_register):
        model = MagicMock()
        ctx = self._make_ctx_with_uri(model_weights="s3://bucket/model")
        strategy = self._make_strategy()

        with patch(
            "modelexpress.load_strategy.model_streamer_strategy."
            "ModelStreamerStrategy._stream_weights",
            side_effect=RuntimeError("S3 connection failed"),
        ):
            with pytest.raises(StrategyFailed, match="S3 connection failed") as exc:
                strategy.load(model, ctx)

        assert exc.value.mutated is False
        mock_register.assert_not_called()

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_apply_weight_iter_failure_is_mutated(self, mock_register):
        model = MagicMock()
        adapter = _FakeAdapter()
        adapter.apply_weight_iter = MagicMock(side_effect=RuntimeError("partial load"))
        ctx = _make_load_context(adapter=adapter)
        strategy = self._make_strategy()

        with patch.dict("os.environ", {"MX_MODEL_URI": "s3://bucket/model"}):
            with patch(
                "modelexpress.load_strategy.model_streamer_strategy."
                "ModelStreamerStrategy._stream_weights",
                return_value=iter([("layer.0.weight", torch.randn(4, 4))]),
            ):
                with pytest.raises(StrategyFailed, match="partial load") as exc:
                    strategy.load(model, ctx)

        assert exc.value.mutated is True
        mock_register.assert_not_called()

    @patch("modelexpress.load_strategy.model_streamer_strategy.register_tensors")
    def test_after_weight_iter_failure_is_mutated(self, mock_register):
        model = MagicMock()
        adapter = _FakeAdapter()
        adapter.after_weight_iter_load = MagicMock(side_effect=RuntimeError("post load"))
        ctx = _make_load_context(adapter=adapter)
        strategy = self._make_strategy()

        with patch.dict("os.environ", {"MX_MODEL_URI": "s3://bucket/model"}):
            with patch(
                "modelexpress.load_strategy.model_streamer_strategy."
                "ModelStreamerStrategy._stream_weights",
                return_value=iter([("layer.0.weight", torch.randn(4, 4))]),
            ):
                with pytest.raises(StrategyFailed, match="post load") as exc:
                    strategy.load(model, ctx)

        assert exc.value.mutated is True
        mock_register.assert_not_called()


# ---------------------------------------------------------------------------
# TestStreamWeights
# ---------------------------------------------------------------------------


class TestStreamWeights:
    def _make_strategy(self):
        from modelexpress.load_strategy.model_streamer_strategy import ModelStreamerStrategy
        return ModelStreamerStrategy()

    def test_delegates_to_adapter_without_cloning(self):
        strategy = self._make_strategy()
        tensor = torch.randn(2, 2)
        adapter = _FakeAdapter()
        adapter.build_model_streamer_weight_iter = MagicMock(
            return_value=iter([("w", tensor)])
        )
        ctx = _make_load_context(adapter=adapter)

        model = torch.nn.Module()
        weights = list(strategy._stream_weights("s3://bucket/model", ctx, model))

        adapter.build_model_streamer_weight_iter.assert_called_once_with(
            "s3://bucket/model",
            model=model,
        )
        assert weights[0][0] == "w"
        assert weights[0][1] is tensor


class TestVllmModelStreamerIterator:
    def _make_adapter(self, *, tp_size: int = 8, extra_config=None):
        from modelexpress.engines.vllm.adapter import VllmAdapter

        load_config = SimpleNamespace(
            device=None,
            model_loader_extra_config=extra_config,
        )
        vllm_config = MagicMock()
        vllm_config.load_config = load_config
        vllm_config.device_config.device = "cuda"
        vllm_config.parallel_config.tensor_parallel_size = tp_size
        model_config = SimpleNamespace(revision="main")
        return VllmAdapter(vllm_config, model_config), load_config

    def _patch_runai_loader(self, tensors):
        loader_instance = MagicMock()
        loader_instance._get_weights_iterator.return_value = iter(tensors)
        loader_cls = MagicMock(return_value=loader_instance)
        module = SimpleNamespace(RunaiModelStreamerLoader=loader_cls)
        return patch.dict(
            sys.modules,
            {"vllm.model_executor.model_loader.runai_streamer_loader": module},
        ), loader_cls, loader_instance

    def test_uses_vllm_native_iterator_and_enables_distributed(self):
        tensor = torch.randn(2, 2)
        adapter, _load_config = self._make_adapter(tp_size=8)
        patcher, loader_cls, loader_instance = self._patch_runai_loader([("w", tensor)])

        with patcher, patch.dict("os.environ", {"MX_MS_DISTRIBUTED": "1"}):
            weights = list(adapter.build_model_streamer_weight_iter("az://models/model"))

        native_load_config = loader_cls.call_args.args[0]
        assert native_load_config.model_loader_extra_config["distributed"] is True
        loader_instance._get_weights_iterator.assert_called_once_with(
            "az://models/model", "main"
        )
        assert weights[0][0] == "w"
        assert weights[0][1] is tensor

    def test_preserves_existing_extra_config_when_distributed_disabled(self):
        adapter, _load_config = self._make_adapter(
            tp_size=1,
            extra_config={"concurrency": 4},
        )
        patcher, loader_cls, _loader_instance = self._patch_runai_loader([])

        with patcher, patch.dict("os.environ", {"MX_MS_DISTRIBUTED": "1"}):
            list(adapter.build_model_streamer_weight_iter("az://models/model"))

        native_load_config = loader_cls.call_args.args[0]
        assert native_load_config.model_loader_extra_config == {"concurrency": 4}

    def test_distributed_enabled_by_default_when_tp_gt_one(self):
        adapter, _load_config = self._make_adapter(
            tp_size=8,
            extra_config={"concurrency": 4},
        )
        patcher, loader_cls, _loader_instance = self._patch_runai_loader([])

        with patcher, patch.dict("os.environ", {}, clear=True):
            list(adapter.build_model_streamer_weight_iter("az://models/model"))

        native_load_config = loader_cls.call_args.args[0]
        assert native_load_config.model_loader_extra_config == {
            "concurrency": 4,
            "distributed": True,
        }

    def test_distributed_can_be_disabled_via_env(self):
        adapter, _load_config = self._make_adapter(
            tp_size=8,
            extra_config={"concurrency": 4},
        )
        patcher, loader_cls, _loader_instance = self._patch_runai_loader([])

        with patcher, patch.dict("os.environ", {"MX_MS_DISTRIBUTED": "0"}):
            list(adapter.build_model_streamer_weight_iter("az://models/model"))

        native_load_config = loader_cls.call_args.args[0]
        assert native_load_config.model_loader_extra_config == {"concurrency": 4}


# ---------------------------------------------------------------------------
# TestChainOrder
# ---------------------------------------------------------------------------


class TestChainOrder:
    @patch(
        "modelexpress.load_strategy.rdma_strategy.RdmaStrategy.is_available",
        return_value=True,
    )
    @patch(
        "modelexpress.load_strategy.model_streamer_strategy."
        "ModelStreamerStrategy.is_available",
        return_value=True,
    )
    @patch(
        "modelexpress.load_strategy.gds_strategy.GdsStrategy.is_available",
        return_value=True,
    )
    @patch(
        "modelexpress.load_strategy.default_strategy.DefaultStrategy.is_available",
        return_value=True,
    )
    def test_model_streamer_in_chain(self, _d, _g, _ms, _r):
        from modelexpress.load_strategy import LoadStrategyChain

        call_order: list[str] = []

        def track_load(strategy_name):
            def _load(self_or_model, *args, **kwargs):
                call_order.append(strategy_name)
                if strategy_name != "default":
                    raise StrategyFailed(f"{strategy_name} miss")
                return args[0]
            return _load

        model = MagicMock()
        ctx = _make_load_context()

        with patch(
            "modelexpress.load_strategy.rdma_strategy.RdmaStrategy.load",
            track_load("rdma"),
        ), patch(
            "modelexpress.load_strategy.model_streamer_strategy."
            "ModelStreamerStrategy.load",
            track_load("model_streamer"),
        ), patch(
            "modelexpress.load_strategy.gds_strategy.GdsStrategy.load",
            track_load("gds"),
        ), patch(
            "modelexpress.load_strategy.default_strategy.DefaultStrategy.load",
            track_load("default"),
        ):
            LoadStrategyChain.run(model, ctx)

        assert call_order == ["rdma", "model_streamer", "gds", "default"]

    @patch(
        "modelexpress.load_strategy.rdma_strategy.RdmaStrategy.is_available",
        return_value=True,
    )
    @patch(
        "modelexpress.load_strategy.model_streamer_strategy."
        "ModelStreamerStrategy.is_available",
        return_value=False,
    )
    @patch(
        "modelexpress.load_strategy.gds_strategy.GdsStrategy.is_available",
        return_value=True,
    )
    @patch(
        "modelexpress.load_strategy.default_strategy.DefaultStrategy.is_available",
        return_value=True,
    )
    def test_skips_unavailable_strategy(self, _d, _g, _ms, _r):
        from modelexpress.load_strategy import LoadStrategyChain

        call_order: list[str] = []

        def track_load(strategy_name):
            def _load(self_or_model, *args, **kwargs):
                call_order.append(strategy_name)
                if strategy_name != "default":
                    raise StrategyFailed(f"{strategy_name} miss")
                return args[0]
            return _load

        model = MagicMock()
        ctx = _make_load_context()

        with patch(
            "modelexpress.load_strategy.rdma_strategy.RdmaStrategy.load",
            track_load("rdma"),
        ), patch(
            "modelexpress.load_strategy.gds_strategy.GdsStrategy.load",
            track_load("gds"),
        ), patch(
            "modelexpress.load_strategy.default_strategy.DefaultStrategy.load",
            track_load("default"),
        ):
            LoadStrategyChain.run(model, ctx)

        assert call_order == ["rdma", "gds", "default"]