-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathtest_load_spec.py
More file actions
423 lines (348 loc) · 16.5 KB
/
test_load_spec.py
File metadata and controls
423 lines (348 loc) · 16.5 KB
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
import os
import pytest
import torch
import torch.distributed as dist
from pydantic import ValidationError
from torch.distributed.device_mesh import DeviceMesh
from torch.distributed.tensor import Shard as DTensorShard
from torch.distributed.tensor import distribute_tensor
from torch.distributed.tensor.placement_types import _StridedShard
from xtuner.v1.model.base import BaseModel, XTunerBaseModelConfig
from xtuner.v1.utils import load_spec as load_spec_module
from xtuner.v1.utils.load_spec import LoadSpec, ShardDescriptor, unshard_tensors_for_hf_save
@pytest.fixture(scope="module")
def single_rank_group() -> dist.ProcessGroup:
# ShardDescriptor.group is typed as `dist.ProcessGroup`; Pydantic enforces
# the isinstance check even with `arbitrary_types_allowed=True`, so schema
# tests need a real (but minimal) process group. A single-rank gloo group
# is sufficient and avoids any CUDA / multi-process plumbing.
if not dist.is_initialized():
os.environ.setdefault("RANK", "0")
os.environ.setdefault("WORLD_SIZE", "1")
os.environ.setdefault("MASTER_ADDR", "127.0.0.1")
os.environ.setdefault("MASTER_PORT", "29555")
dist.init_process_group(backend="gloo", rank=0, world_size=1)
group = dist.group.WORLD
assert group is not None
return group
class TestLoadSpecSchema:
"""New-schema fields should describe layout without legacy dispatch state."""
def test_same_unsharded_spec(self) -> None:
spec = LoadSpec(
name="layers.0.mlp.gate.weight",
global_hf_keys=["model.layers.0.mlp.gate.weight"],
global_shape=(128, 64),
)
assert spec.is_fused is False
assert spec.is_sharded is False
assert spec.fused_dim is None
assert spec.shards == []
assert spec.origin_shape is None
assert spec.unpadded_global_shape == spec.global_shape
def test_from_tensor_derives_plain_tensor_layout(self) -> None:
spec = LoadSpec.from_tensor(
name="layers.0.experts.fused_w1w3.weight",
hf_keys=["k0", "k1"],
tensor=torch.empty(128, 64),
origin_shape=(120, 64),
)
assert spec.global_hf_keys == ["k0", "k1"]
assert spec.global_shape == (128, 64)
assert spec.fused_dim == 0
assert spec.shards == []
assert spec.origin_shape == (120, 64)
def test_from_tensor_derives_dtensor_shards(self, single_rank_group: dist.ProcessGroup) -> None:
assert single_rank_group is not None
mesh = DeviceMesh("cpu", [0])
tensor = distribute_tensor(torch.empty(128, 64), mesh, [DTensorShard(0)])
spec = LoadSpec.from_tensor(name="layers.0.mlp.gate.weight", hf_keys=["gate"], tensor=tensor)
assert spec.global_hf_keys == ["gate"]
assert spec.global_shape == (128, 64)
assert spec.fused_dim is None
assert [(shard.dim, shard.start, shard.end) for shard in spec.shards] == [(0, 0, 128)]
def test_dtensor_shards_follow_explicit_placement_order(self, single_rank_group: dist.ProcessGroup) -> None:
class FakeDeviceMesh:
shape = (2, 2)
def size(self, mesh_dim: int) -> int:
return self.shape[mesh_dim]
def get_local_rank(self, mesh_dim: int) -> int:
return (1, 0)[mesh_dim]
def get_group(self, mesh_dim: int) -> dist.ProcessGroup:
return single_rank_group
class FakeDTensor:
shape = (8,)
placements = (_StridedShard(0, split_factor=2), DTensorShard(0))
device_mesh = FakeDeviceMesh()
shards = load_spec_module._dtensor_shards(FakeDTensor()) # type: ignore[arg-type]
assert [(shard.dim, shard.start, shard.end) for shard in shards] == [(0, 0, 4), (0, 2, 4)]
def test_fused_spec_requires_fused_dim(self) -> None:
with pytest.raises(ValidationError, match="fused_dim"):
LoadSpec(
name="layers.0.mlp.fused_w1w3.weight",
global_hf_keys=[
"model.layers.0.mlp.experts.0.gate_proj.weight",
"model.layers.0.mlp.experts.0.up_proj.weight",
],
global_shape=(256, 64),
)
def test_multi_axis_shards_preserve_order(self, single_rank_group: dist.ProcessGroup) -> None:
ep = ShardDescriptor(dim=0, start=64, end=128, group=single_rank_group)
fsdp = ShardDescriptor(dim=0, start=16, end=32, group=single_rank_group)
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=[
"model.layers.0.mlp.experts.0.gate_proj.weight",
"model.layers.0.mlp.experts.0.up_proj.weight",
],
global_shape=(256, 64),
fused_dim=0,
shards=[ep, fsdp],
)
assert [(shard.start, shard.end) for shard in spec.shards] == [(64, 128), (16, 32)]
assert spec.is_fused is True
assert spec.is_sharded is True
def test_ordered_shard_bounds_are_validated(self, single_rank_group: dist.ProcessGroup) -> None:
with pytest.raises(ValidationError, match="Invalid shard descriptor"):
LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["model.layers.0.mlp.experts.0.gate_proj.weight"],
global_shape=(128, 64),
shards=[
ShardDescriptor(dim=0, start=64, end=128, group=single_rank_group),
ShardDescriptor(dim=0, start=65, end=80, group=single_rank_group),
],
)
def test_zero_size_dtensor_shards_are_valid(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="embeddings.cls_embedding",
global_hf_keys=["embeddings.cls_embedding"],
global_shape=(1, 1, 1024),
shards=[ShardDescriptor(dim=0, start=1, end=1, group=single_rank_group)],
)
plan = spec.plan_hf_load()
assert plan.zero_fill is True
assert plan.hf_keys == []
class TestHFLoadPlan:
"""LoadSpec should derive HF read plans from shards only."""
def test_fused_slice_selects_overlapping_hf_keys(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1", "k2", "k3"],
global_shape=(400, 64),
fused_dim=0,
shards=[ShardDescriptor(dim=0, start=150, end=260, group=single_rank_group)],
)
plan = spec.plan_hf_load()
assert plan.hf_keys == ["k1", "k2"]
assert plan.fused_dim == 0
assert [(load_slice.dim, load_slice.start, load_slice.end) for load_slice in plan.slices] == [(0, 50, 160)]
assert not hasattr(plan, "loaded_shape")
def test_non_fused_slice_keeps_single_hf_key(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.self_attn.q_proj.weight",
global_hf_keys=["q_proj"],
global_shape=(128, 256),
shards=[ShardDescriptor(dim=1, start=64, end=192, group=single_rank_group)],
)
plan = spec.plan_hf_load()
assert plan.hf_keys == ["q_proj"]
assert plan.fused_dim is None
assert [(load_slice.dim, load_slice.start, load_slice.end) for load_slice in plan.slices] == [(1, 64, 192)]
def test_origin_shape_clips_runtime_padding(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1", "k2", "k3"],
global_shape=(480, 64),
fused_dim=0,
shards=[ShardDescriptor(dim=0, start=350, end=450, group=single_rank_group)],
origin_shape=(400, 64),
)
plan = spec.plan_hf_load()
assert plan.hf_keys == ["k3"]
assert [(load_slice.dim, load_slice.start, load_slice.end) for load_slice in plan.slices] == [(0, 50, 100)]
assert plan.zero_fill is False
def test_origin_shape_returns_zero_fill_for_pad_only_rank(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1", "k2", "k3"],
global_shape=(480, 64),
fused_dim=0,
shards=[ShardDescriptor(dim=0, start=420, end=480, group=single_rank_group)],
origin_shape=(400, 64),
)
plan = spec.plan_hf_load()
assert plan.zero_fill is True
assert plan.hf_keys == []
assert plan.slices == []
class TestHFSavePolicy:
"""HF save should preserve the old distributed write policy from the new schema."""
def test_fused_keys_are_split_across_save_ranks(self, monkeypatch: pytest.MonkeyPatch) -> None:
model = BaseModel(XTunerBaseModelConfig())
model.config.hf_save_cfg.max_save_rank = 4
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=[f"k{i}" for i in range(8)],
global_shape=(800, 64),
fused_dim=0,
)
monkeypatch.setattr(dist, "is_initialized", lambda: True)
monkeypatch.setattr(dist, "get_world_size", lambda group=None: 8)
expected_ranges = {
0: (0, 2),
1: (2, 4),
2: (4, 6),
3: (6, 8),
4: (0, 0),
}
for rank, expected_range in expected_ranges.items():
monkeypatch.setattr(dist, "get_rank", lambda group=None, rank=rank: rank)
assert model._hf_save_key_range(spec.plan_hf_save(distributed_save=True)) == expected_range
def test_preserved_fused_shard_exposes_local_hf_keys(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1", "k2", "k3"],
global_shape=(400, 64),
fused_dim=0,
shards=[ShardDescriptor(dim=0, start=100, end=200, group=single_rank_group)],
)
save_plan = spec.plan_hf_save(preserve_process_group=single_rank_group)
assert save_plan.preserves_shards is True
assert save_plan.hf_keys == ["k1"]
def test_preserved_fused_shard_must_align_with_hf_key_boundary(self, single_rank_group: dist.ProcessGroup) -> None:
spec = LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1", "k2", "k3"],
global_shape=(400, 64),
fused_dim=0,
shards=[ShardDescriptor(dim=0, start=50, end=150, group=single_rank_group)],
)
with pytest.raises(AssertionError, match="must align with HF key size"):
spec.plan_hf_save(preserve_process_group=single_rank_group)
class TestHFSaveUnshardScheduler:
"""Save unshard should batch independent work without violating per-tensor dependencies."""
@staticmethod
def _patch_foreach_all_gather(monkeypatch: pytest.MonkeyPatch) -> list[dict[str, object]]:
calls: list[dict[str, object]] = []
def fake_foreach_all_gather(
tensor_list: list[torch.Tensor],
group: dist.ProcessGroup,
) -> list[list[torch.Tensor]]:
calls.append(
{
"group": group,
"shapes": [tuple(tensor.shape) for tensor in tensor_list],
"dtypes": [tensor.dtype for tensor in tensor_list],
}
)
return [[tensor] for tensor in tensor_list]
monkeypatch.setattr(load_spec_module, "foreach_all_gather", fake_foreach_all_gather)
return calls
def test_single_tensor_single_step(
self, monkeypatch: pytest.MonkeyPatch, single_rank_group: dist.ProcessGroup
) -> None:
calls = self._patch_foreach_all_gather(monkeypatch)
spec = LoadSpec(
name="layers.0.mlp.gate.weight",
global_hf_keys=["gate"],
global_shape=(4, 2),
shards=[ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group)],
)
output = unshard_tensors_for_hf_save(
[torch.ones(2, 2)],
[spec.plan_hf_save()],
)
assert [tuple(tensor.shape) for tensor in output] == [(4, 2)]
assert [call["shapes"] for call in calls] == [[(4, 2)]]
def test_same_group_same_dtype_tensors_are_batched(
self, monkeypatch: pytest.MonkeyPatch, single_rank_group: dist.ProcessGroup
) -> None:
calls = self._patch_foreach_all_gather(monkeypatch)
specs = [
LoadSpec(
name="layers.0.mlp.gate.weight",
global_hf_keys=["gate"],
global_shape=(4, 2),
shards=[ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group)],
),
LoadSpec(
name="layers.0.mlp.up.weight",
global_hf_keys=["up"],
global_shape=(6, 2),
shards=[ShardDescriptor(dim=0, start=2, end=5, group=single_rank_group)],
),
]
output = unshard_tensors_for_hf_save(
[torch.ones(2, 2), torch.ones(3, 2)],
[spec.plan_hf_save() for spec in specs],
)
assert [tuple(tensor.shape) for tensor in output] == [(4, 2), (6, 2)]
assert [call["shapes"] for call in calls] == [[(4, 2), (6, 2)]]
def test_same_group_different_dtype_tensors_are_split(
self, monkeypatch: pytest.MonkeyPatch, single_rank_group: dist.ProcessGroup
) -> None:
calls = self._patch_foreach_all_gather(monkeypatch)
specs = [
LoadSpec(
name="layers.0.mlp.gate.weight",
global_hf_keys=["gate"],
global_shape=(4, 2),
shards=[ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group)],
),
LoadSpec(
name="layers.0.mlp.up.weight",
global_hf_keys=["up"],
global_shape=(4, 2),
shards=[ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group)],
),
]
output = unshard_tensors_for_hf_save(
[torch.ones(2, 2, dtype=torch.float32), torch.ones(2, 2, dtype=torch.float64)],
[spec.plan_hf_save() for spec in specs],
)
assert [tuple(tensor.shape) for tensor in output] == [(4, 2), (4, 2)]
assert [call["dtypes"] for call in calls] == [[torch.float32], [torch.float64]]
def test_multi_step_tensor_waits_for_previous_step(
self, monkeypatch: pytest.MonkeyPatch, single_rank_group: dist.ProcessGroup
) -> None:
calls = self._patch_foreach_all_gather(monkeypatch)
specs = [
LoadSpec(
name="layers.0.experts.fused_w1w3.weight",
global_hf_keys=["k0", "k1"],
global_shape=(8, 2),
fused_dim=0,
shards=[
ShardDescriptor(dim=0, start=0, end=4, group=single_rank_group),
ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group),
],
),
LoadSpec(
name="layers.0.mlp.gate.weight",
global_hf_keys=["gate"],
global_shape=(4, 2),
shards=[ShardDescriptor(dim=0, start=1, end=3, group=single_rank_group)],
),
]
output = unshard_tensors_for_hf_save(
[torch.ones(2, 2), torch.ones(2, 2)],
[spec.plan_hf_save() for spec in specs],
)
assert [tuple(tensor.shape) for tensor in output] == [(8, 2), (4, 2)]
assert [call["shapes"] for call in calls] == [[(4, 2), (4, 2)], [(8, 2)]]
class TestBaseModelHFSave:
"""BaseModel save should preserve state semantics outside LoadSpec."""
def test_non_dtensor_buffers_keep_runtime_dtype(self) -> None:
class BufferModel(BaseModel):
def __init__(self) -> None:
super().__init__(XTunerBaseModelConfig())
self.register_buffer("rotary_coef", torch.tensor([1.25], dtype=torch.float32), persistent=True)
self._init_load_spec()
def to_hf_key_list(self, key: str) -> list[str]:
return [key]
model = BufferModel()
[(names, tensors)] = list(
model._get_hf_param(model._load_spec_params(), dtype=torch.bfloat16, distributed_save=True)
)
assert names == ["rotary_coef"]
assert tensors[0].dtype == torch.float32
assert torch.equal(tensors[0], model.rotary_coef)