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
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""Engine adapter contract for ModelExpress loading strategies."""
"""Raised by default bodies for adapter capabilities."""
"""Expected strategy miss that lets the chain try the next strategy.
Set mutated=True when the current strategy may have changed model weights
or model structure before failing. The chain will run rollback() for
strategy-owned cleanup, then ask the adapter to re-initialize the model.
"""
=
"""Create an optional adapter method that engines must override to support it.
Strategies compare their required EngineAdapter methods by object identity.
If an engine inherits this default method, the strategy is not eligible; if
the engine overrides it, the strategy may call the engine implementation.
Calling the inherited default still raises UnsupportedCapability.
"""
= True
return
"""Engine-specific boundary used by shared loading strategies.
Load strategies own ModelExpress policy: fallback order, metadata
publishing, RDMA transfer, and retry decisions. Engine adapters own the
operations that depend on a framework's model object, device mapping, and
post-load processing rules.
Methods decorated with @gated_capability are optional capabilities. A
strategy lists the exact adapter methods it needs in its `requires` tuple;
the strategy is eligible only when the concrete engine overrides those
methods. Plain lifecycle hooks below are no-op by default because they are
safe extension points around an already-selected strategy.
"""
"""Return the stable identity used to match compatible source workers."""
...
"""Return the model-shard key used to pair source and target workers.
Data-parallel replicas should normally return the same key because
their weights are interchangeable. Tensor, pipeline, and expert
parallel shards should return distinct keys when they own distinct
model weights.
"""
...
"""Return the engine's global distributed rank for logging and metadata."""
...
"""Return the local CUDA device id owned by this adapter instance."""
...
"""Return publishable tensors from the loaded engine model.
Strategies call this after weights are ready. Implementations may run
engine-specific tensor adoption or normalization before collecting the
tensors, but should not change model weights.
"""
...
"""Apply a stream of named tensors to the engine model.
This hook may mutate model weights. If a strategy catches an exception
from this method, it should raise StrategyFailed(mutated=True) so the
chain reinitializes the model before trying another strategy.
"""
...
"""Return the engine-native ModelStreamer weight iterator.
The shared strategy owns fallback and registration policy. Engines own
the concrete ModelStreamer integration because native loader APIs and
distributed-device selection are framework-specific. Some engine-native
loaders need the initialized model to discover secondary weight sources.
"""
...
"""Return the engine-native InstantTensor weight iterator.
InstantTensor loads the model's local safetensors directly onto CUDA
with distributed loading, pipelined prefetching, and direct I/O (with
GPUDirect Storage when available). Unlike ModelStreamer it needs no
streaming URI; the engine resolves the model's own weight files. Some
engine-native loaders need the initialized model to discover secondary
weight sources.
"""
...
"""Load the model using the engine's native disk/checkpoint loader."""
...
"""Replace a possibly-mutated model with a fresh engine model instance."""
...
"""Return a best-effort unique id for engines without custom identity."""
return f
return f
"""Return the torch device where target model state should live."""
return
"""Return whether this engine is running on a CUDA-like platform."""
return False
"""Return whether RDMA must cover every source and target tensor."""
return False
"""Prepare target-side model storage before receiving RDMA weights."""
return
"""Run engine post-processing needed before RDMA writes into tensors."""
return
"""Run engine post-processing after RDMA weights have been received."""
return
"""Run engine post-processing after apply_weight_iter() succeeds."""
return
"""Run engine post-processing after load_via_native() succeeds."""
return