-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
160 lines (136 loc) · 5 KB
/
config.py
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
import os
import json
import yaml
import torch
import deepspeed.comm as dist
from typing import Union
import hashlib
def get_config_md5(dict):
return hashlib.md5(json.dumps(dict).encode()).hexdigest()
def save_temp_config(config, config_type, tmp_dir=".tmp", format="json"):
if not os.path.exists(tmp_dir):
os.makedirs(tmp_dir)
# get md5 hash of ds_config
config_hash = get_config_md5(config)
# get first 8 characters of hash
config_hash = config_hash[:8]
config_path = os.path.join(tmp_dir, f"{config_type}.{config_hash}.{format}")
print(f"Saving {config_type} config to {config_path}")
with open(config_path, "w") as fp:
if format == "json":
json.dump(config, fp)
elif format == "yaml":
yaml.dump(config, fp)
else:
raise ValueError(f"Unknown format: '{format}'")
return config_path
# SOURCE https://github.com/microsoft/DeepSpeedExamples/blob/bae2afb8417697407ffe7cf6a21388a840679059/applications/DeepSpeed-Chat/training/utils/ds_utils.py
def get_train_ds_config(
stage=2,
offload=False,
enable_hybrid_engine=False,
inference_tp_size=1,
release_inference_cache=False,
pin_parameters=True,
pin_memory=True,
tp_gather_partition_size=8,
max_out_tokens=512,
enable_mixed_precision_lora=False,
):
device = "cpu" if offload else "none"
zero_opt_dict = {
"stage": stage,
"offload_param": {"device": device, "pin_memory": pin_memory},
"offload_optimizer": {"device": device, "pin_memory": pin_memory},
"stage3_param_persistence_threshold": 1e4,
"stage3_max_live_parameters": 3e7,
"stage3_prefetch_bucket_size": 3e7,
"stage3_gather_16bit_weights_on_model_save": True,
"memory_efficient_linear": False,
}
if enable_mixed_precision_lora:
zero_opt_dict["zero_quantized_nontrainable_weights"] = True
if dist.get_world_size() != torch.cuda.device_count():
zero_opt_dict["zero_hpz_partition_size"] = torch.cuda.device_count()
return {
"train_batch_size": "auto",
"gradient_accumulation_steps": "auto",
"train_micro_batch_size_per_gpu": "auto",
"zero_optimization": zero_opt_dict,
"fp16": {"enabled": "auto", "loss_scale_window": 100},
"bf16": {"enabled": "auto"},
"gradient_clipping": "auto",
"prescale_gradients": False,
"wall_clock_breakdown": False,
"hybrid_engine": {
"enabled": enable_hybrid_engine,
"max_out_tokens": max_out_tokens,
"inference_tp_size": inference_tp_size,
"release_inference_cache": release_inference_cache,
"pin_parameters": pin_parameters,
"tp_gather_partition_size": tp_gather_partition_size,
},
}
def get_eval_ds_config(offload, pin_memory=True, stage=0):
device = "cpu" if offload else "none"
zero_opt_dict = {
"stage": stage,
"stage3_param_persistence_threshold": 1e4,
"offload_param": {"device": device, "pin_memory": pin_memory},
"memory_efficient_linear": False,
}
return {
"train_batch_size": "auto",
"train_micro_batch_size_per_gpu": "auto",
"zero_optimization": zero_opt_dict,
"fp16": {"enabled": "auto", "loss_scale_window": 100},
"gradient_clipping": "auto",
"prescale_gradients": False,
"wall_clock_breakdown": False,
}
def get_accelerate_config(
task: str = "train",
distributed: bool = True,
deepspeed: bool = False,
stage: int = 2,
offload: bool = False,
pin_memory: bool = True,
mixed_precision: str = "no",
num_machines: int = 1,
num_processes: int = 1,
tmp_dir: Union[str, None] = ".tmp",
):
distributed_type = "DEEPSPEED" if deepspeed else "MULTI_GPU" if distributed else "NO"
deepspeed_config = {}
accelerate_kwargs = {}
if deepspeed:
if task == "eval":
ds_config = get_eval_ds_config(offload=offload, pin_memory=pin_memory, stage=stage)
else:
ds_config = get_train_ds_config(offload=offload, pin_memory=pin_memory, stage=stage)
ds_config_path = save_temp_config(ds_config, config_type="deepspeed", tmp_dir=tmp_dir, format="json")
deepspeed_config = {
"deepspeed_config_file": ds_config_path,
"zero_init_flag": True,
}
else:
accelerate_kwargs = {"mixed_precision": mixed_precision}
return {
"compute_environment": "LOCAL_MACHINE",
"debug": False,
"distributed_type": distributed_type,
"downcast_bf16": "no",
"gpu_ids": "all",
"machine_rank": 0,
"main_training_function": "main",
"num_machines": num_machines,
"num_processes": num_processes,
"rdzv_backend": "static",
"same_network": True,
"tpu_env": [],
"tpu_use_cluster": False,
"tpu_use_sudo": False,
"use_cpu": offload,
"deepspeed_config": deepspeed_config,
**accelerate_kwargs,
}