Zero3 conversion lib
Helper utility for converting ZeRO3 and ZeRO2 checkpoints to PyTorch.
ZeroModelState
dataclass
A dataclass representing the state of a ZeRO model.
Attributes:
Name | Type | Description |
---|---|---|
buffers |
Dict
|
Buffers in the model state. |
extra_states |
Dict
|
Extra states in the model state. |
param_shapes |
List
|
Shapes of the parameters. |
shared_params |
List
|
Shared parameters in the model state. |
ds_version |
int
|
Version of the DeepSpeed checkpoint. |
frozen_param_shapes |
Dict
|
Shapes of the frozen parameters. |
frozen_param_fragments |
Dict
|
Fragments of the frozen parameters. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
atoi(text)
Converts a string to an integer if it is a digit, otherwise returns the string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to be converted. |
required |
Returns:
Type | Description |
---|---|
int or str: The converted integer or the original string. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
110 111 112 113 114 115 116 117 118 119 |
|
create_ds_output_path(rank)
Creates the output path for a DeepSpeed checkpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank
|
int
|
The rank to create the output path for. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The output path for the DeepSpeed checkpoint. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
187 188 189 190 191 192 193 194 195 196 |
|
create_zero3_model_state_path(dp_rank, mp_rank)
Creates the path for a ZeRO3 model state file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dp_rank
|
int
|
The data parallel rank. |
required |
mp_rank
|
int
|
The model parallel rank. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The path for the ZeRO3 model state file. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
199 200 201 202 203 204 205 206 207 208 209 |
|
create_zero3_optim_state_path(dp_rank, mp_rank)
Creates the path for a ZeRO3 optimizer state file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dp_rank
|
int
|
The data parallel rank. |
required |
mp_rank
|
int
|
The model parallel rank. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The path for the ZeRO3 optimizer state file. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
212 213 214 215 216 217 218 219 220 221 222 |
|
get_checkpoint_files(checkpoint_dir, glob_pattern)
Retrieves checkpoint files from a directory based on a glob pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
checkpoint_dir
|
str
|
The directory to search for checkpoint files. |
required |
glob_pattern
|
str
|
The glob pattern to match files. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A sorted list of checkpoint files. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no files matching the glob pattern are found. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
get_elapsed(t)
Converts elapsed time in seconds to a formatted string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t
|
float
|
The elapsed time in seconds. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The formatted elapsed time as a string. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
|
get_model_files_by_rank(checkpoint_dir, rank)
Retrieves model files for a specific rank from a checkpoint directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
checkpoint_dir
|
str
|
The directory to search for model files. |
required |
rank
|
int
|
The rank to search for. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of model files for the specified rank. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
161 162 163 164 165 166 167 168 169 170 171 |
|
get_model_state_file(checkpoint_dir, zero_stage)
Retrieves the model state file from a checkpoint directory based on the ZeRO stage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
checkpoint_dir
|
str
|
The directory to search for the model state file. |
required |
zero_stage
|
int
|
The ZeRO stage to search for. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
The path to the model state file. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the directory or model state file is not found. |
ValueError
|
If the ZeRO stage is not supported. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
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 |
|
get_optim_files_by_rank(checkpoint_dir, rank)
Retrieves optimizer files for a specific rank from a checkpoint directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
checkpoint_dir
|
str
|
The directory to search for optimizer files. |
required |
rank
|
int
|
The rank to search for. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
A list of optimizer files for the specified rank. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
174 175 176 177 178 179 180 181 182 183 184 |
|
natural_keys(text)
Sorts a list in human order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
The text to be sorted. |
required |
Returns:
Name | Type | Description |
---|---|---|
list |
The sorted list. |
Note
alist.sort(key=natural_keys) sorts in human order. http://nedbatchelder.com/blog/200712/human_sorting.html (See Toothy's implementation in the comments)
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
parse_model_states(files)
Parses model state files and returns a list of ZeroModelState objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files
|
Set[str]
|
A set of file paths to parse. |
required |
Returns:
Type | Description |
---|---|
List[ZeroModelState]: A list of parsed ZeroModelState objects. |
Raises:
Type | Description |
---|---|
ValueError
|
If a file is not a model state checkpoint. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
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 |
|
parse_optim_states(files, ds_checkpoint_dir)
Parses optimizer state files and returns the ZeRO stage, world size, and fp32 flat groups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files
|
Set[str]
|
A set of file paths to parse. |
required |
ds_checkpoint_dir
|
str
|
The directory containing the DeepSpeed checkpoint. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing the ZeRO stage, world size, and fp32 flat groups. |
Raises:
Type | Description |
---|---|
ValueError
|
If a file is not a ZeRO checkpoint or if the number of files does not match the expected world size. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
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 |
|
print_pid(msg)
Prints the process ID along with a message.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
msg
|
str
|
The message to be printed. |
required |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
100 101 102 103 104 105 106 107 |
|
process_single_rank(rank, ds_checkpoint_dir, output_dir, overwrite=False, exclude_frozen_parameters=False)
Processes a single rank to gather and save the state dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rank
|
int
|
The rank to process. |
required |
ds_checkpoint_dir
|
str
|
Path to the DeepSpeed checkpoint folder. |
required |
output_dir
|
str
|
Directory to save the output. |
required |
overwrite
|
bool
|
Whether to overwrite existing files. Default is False. |
False
|
exclude_frozen_parameters
|
bool
|
Whether to exclude frozen parameters. Default is False. |
False
|
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
|
profile_memory_decorator(func)
A decorator to profile memory usage of a function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Iterable
|
The function to be decorated. |
required |
Returns:
Name | Type | Description |
---|---|---|
wrapper |
The decorated function with memory profiling. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
zero3_partitioned_param_info(unpartitioned_numel, world_size)
Returns the partitioned and padding number of elements for a parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
unpartitioned_numel
|
int
|
The number of elements in the unpartitioned parameter. |
required |
world_size
|
int
|
The world size. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
A tuple containing the partitioned number of elements and the padding number of elements. |
Source code in bionemo/evo2/utils/checkpoint/zero3_conversion_lib.py
451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|