forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepspeed.py
More file actions
149 lines (132 loc) · 4.64 KB
/
Copy pathdeepspeed.py
File metadata and controls
149 lines (132 loc) · 4.64 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
"""DeepSpeed configuration templates for multi-GPU training."""
import copy
import json
import tempfile
# ZeRO Stage 2: splits optimizer states + gradients across GPUs
ZERO_STAGE_2 = {
"bf16": {"enabled": True},
"zero_optimization": {
"stage": 2,
"offload_optimizer": {"device": "none"},
"allgather_partitions": True,
"allgather_bucket_size": 2e8,
"overlap_comm": True,
"reduce_scatter": True,
"reduce_bucket_size": 2e8,
"contiguous_gradients": True,
},
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"wall_clock_breakdown": False,
}
# ZeRO Stage 3: splits model params + optimizer + gradients across GPUs
ZERO_STAGE_3 = {
"bf16": {"enabled": True},
"zero_optimization": {
"stage": 3,
"offload_optimizer": {"device": "none"},
"offload_param": {"device": "none"},
"overlap_comm": True,
"contiguous_gradients": True,
"sub_group_size": 1e9,
"reduce_bucket_size": "auto",
"stage3_prefetch_bucket_size": "auto",
"stage3_param_persistence_threshold": "auto",
"stage3_max_live_parameters": 1e9,
"stage3_max_reuse_distance": 1e9,
"stage3_gather_16bit_weights_on_model_save": True,
},
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"wall_clock_breakdown": False,
}
# ZeRO Stage 2 with CPU offload (for memory-constrained setups)
ZERO_STAGE_2_OFFLOAD = {
"bf16": {"enabled": True},
"zero_optimization": {
"stage": 2,
"offload_optimizer": {"device": "cpu", "pin_memory": True},
"allgather_partitions": True,
"allgather_bucket_size": 2e8,
"overlap_comm": True,
"reduce_scatter": True,
"reduce_bucket_size": 2e8,
"contiguous_gradients": True,
},
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"wall_clock_breakdown": False,
}
# ZeRO++ (v0.27.0): stage-3 base + hierarchical partitioning + quantized
# weights/gradients. Reduces inter-node communication 4-8x on 8+ GPUs.
ZERO_PLUS_PLUS = {
"bf16": {"enabled": True},
"zero_optimization": {
"stage": 3,
"offload_optimizer": {"device": "none"},
"offload_param": {"device": "none"},
"overlap_comm": True,
"contiguous_gradients": True,
"sub_group_size": int(1e9),
"reduce_bucket_size": "auto",
"stage3_prefetch_bucket_size": "auto",
"stage3_param_persistence_threshold": "auto",
"stage3_max_live_parameters": int(1e9),
"stage3_max_reuse_distance": int(1e9),
"stage3_gather_16bit_weights_on_model_save": True,
# ZeRO++ specifics
"zero_hpz_partition_size": 8,
"zero_quantized_weights": True,
"zero_quantized_gradients": True,
},
"gradient_accumulation_steps": "auto",
"gradient_clipping": "auto",
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"wall_clock_breakdown": False,
}
CONFIGS = {
"zero2": ZERO_STAGE_2,
"zero3": ZERO_STAGE_3,
"zero2_offload": ZERO_STAGE_2_OFFLOAD,
"zero++": ZERO_PLUS_PLUS,
"zero_pp": ZERO_PLUS_PLUS,
}
def get_deepspeed_config(stage: str = "zero2") -> dict:
"""Get a DeepSpeed config dict by name."""
if stage not in CONFIGS:
raise ValueError(f"Unknown DeepSpeed config: {stage}. Options: {', '.join(CONFIGS.keys())}")
return copy.deepcopy(CONFIGS[stage])
def write_deepspeed_config(stage: str = "zero2") -> str:
"""Write a DeepSpeed config to a temp file and return the path."""
config = get_deepspeed_config(stage)
tmp = tempfile.NamedTemporaryFile(
mode="w", suffix=".json", prefix="ds_config_", delete=False
)
json.dump(config, tmp, indent=2)
tmp.close()
return tmp.name
def detect_multi_gpu() -> dict:
"""Detect multiple GPUs and return info."""
try:
import torch
if not torch.cuda.is_available():
return {"gpu_count": 0, "gpus": []}
gpu_count = torch.cuda.device_count()
gpus = []
for idx in range(gpu_count):
props = torch.cuda.get_device_properties(idx)
gpus.append({
"index": idx,
"name": props.name,
"memory_gb": props.total_memory / (1024 ** 3),
})
return {"gpu_count": gpu_count, "gpus": gpus}
except ImportError:
return {"gpu_count": 0, "gpus": []}