API Reference
This document provides detailed API references for all modules in Decent-DP.
decent_dp.ddp
The core module containing the DecentralizedDataParallel class, which is the main wrapper for enabling decentralized training on PyTorch models.
decent_dp.ddp
OPTIM_FN_TYPE = Callable[[List[Tuple[str, Tensor]]], Optimizer]
module-attribute
Data type for the optimizer function
LR_SCHEDULER_FN_TYPE = Callable[[Optimizer], LRScheduler]
module-attribute
Data type for the learning rate scheduler function
DecentralizedDataParallel
Bases: Module
Decentralized data parallel wrapper for PyTorch module
- The wrapper places hooks during the backward pass to trace the order of used parameters in the first iteration, and 2. Split the parameters into buckets and create optimizers and LR schedulers for each bucket, Add hooks on the last parameter of each bucket to perform the bucket-wise update and communication, 3. During the backward passes in the training loop, the hooks are triggered to perform the bucket-wise update and communication
Note
The wrapper currently does not support "channels_last" memory format.
Note
The wrapper assumes that the parameter will only be used once in the backward pass
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
PyTorch module to be wrapped |
required |
optim_fn
|
OPTIM_FN_TYPE
|
Function to create the optimizer, which takes a list of tuples of parameters and their names |
required |
lr_scheduler_fn
|
Optional[LR_SCHEDULER_FN_TYPE]
|
Function to create the learning rate scheduler, which takes the optimizer as input. Defaults to None. |
None
|
topology
|
str
|
Topology of the decentralized communication graph. Defaults to 'complete'. |
'complete'
|
scaler
|
Optional[GradScaler]
|
Gradient scaler for mixed precision training. Defaults to None. |
None
|
grad_clip_norm
|
float
|
Gradient clipping norm, set to 0.0 if no gradient clipping is applied. Defaults to 0.0. |
0.0
|
param_as_bucket_view
|
bool
|
Whether to use the parameter as a view of part of the contiguous buffer. Defaults to True. |
True
|
sync_buffer_in_global_avg
|
bool
|
Whether to synchronize the float buffers in the global average. Defaults to False. |
False
|
bucket_size_in_mb
|
int
|
Size of the bucket in MB. Defaults to 25 MB. |
25
|
_local_world_size
|
Optional[int]
|
Provide the local world size and not using the environment variable. Defaults to None. |
None
|
Source code in src/decent_dp/ddp.py
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |
set_accumulate_grad(enable=True)
Set the gradient accumulation mode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enable
|
bool
|
Whether to accumulate the gradients. Defaults to True. |
True
|
train(mode=True)
Set the module in training mode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
bool
|
Whether to set the module in training mode. Defaults to True. |
True
|
eval()
forward(*args, **kwargs)
Forward pass of the model
Source code in src/decent_dp/ddp.py
parameters(recurse=True)
Get the parameters of the model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
recurse
|
bool
|
Whether to get the parameters recursively. Defaults to True. |
True
|
Yields:
| Type | Description |
|---|---|
Parameter
|
Iterator[Parameter]: The iterator of the parameters |
Source code in src/decent_dp/ddp.py
named_parameters(prefix='', recurse=True, remove_duplicate=True)
Get the named parameters of the model
Source code in src/decent_dp/ddp.py
global_avg()
Perform global average on the parameters (and buffers if sync_buffer_in_global_avg is True) The function is called at the end of the training loop to synchronize the parameters across all nodes for evaluation
Source code in src/decent_dp/ddp.py
decent_dp.optim
This module provides optimizer functions and custom optimizers designed specifically for decentralized training scenarios.
decent_dp.optim
AccumAdamW
Bases: Optimizer
AccumAdamW optimizer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Any
|
parameters list or groups |
required |
lr
|
float
|
base learning rate. Defaults to 1e-3. |
0.001
|
betas
|
Tuple[float, float]
|
beta1 and beta2. Defaults to (0.9, 0.999). |
(0.9, 0.999)
|
eps
|
float
|
epsilon. Defaults to 1e-8. |
1e-08
|
weight_decay
|
float
|
weight decay. Defaults to 0. |
0
|
accum_iter
|
int
|
number of accumulation steps. Defaults to 4. should be scaling up with the number of workers. |
4
|
Source code in src/decent_dp/optim.py
AccumAdam
Bases: Optimizer
AccumAdamW optimizer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Any
|
parameters list or groups |
required |
lr
|
float
|
base learning rate. Defaults to 1e-3. |
0.001
|
betas
|
Tuple[float, float]
|
beta1 and beta2. Defaults to (0.9, 0.999). |
(0.9, 0.999)
|
eps
|
float
|
epsilon. Defaults to 1e-8. |
1e-08
|
weight_decay
|
float
|
weight decay. Defaults to 0. |
0.0
|
accum_iter
|
int
|
number of accumulation steps. Defaults to 4. should be scaling up with the number of workers. |
4
|
Source code in src/decent_dp/optim.py
optim_fn_adam(params, lr=0.001, beta1=0.9, beta2=0.999, weight_decay=1.0 / 32768, eps=1e-08)
An example of a function that creates an Adam optimizer with the given parameters and their names.
To change the hyperparameters of the optimizer, you can wrap it with functools.partial and pass the new values.
Returns:
| Name | Type | Description |
|---|---|---|
Optimizer |
Optimizer
|
an Adam optimizer |
Source code in src/decent_dp/optim.py
optim_fn_adamw(params, lr=0.001, beta1=0.9, beta2=0.999, weight_decay=0.1, eps=1e-08)
An example of a function that creates an AdamW optimizer with the given parameters and their names.
To change the hyperparameters of the optimizer, you can wrap it with functools.partial and pass the new values.
Returns:
| Name | Type | Description |
|---|---|---|
Optimizer |
Optimizer
|
an AdamW optimizer |
Source code in src/decent_dp/optim.py
optim_fn_accum_adam(params, lr=0.001, beta1=0.9, beta2=0.999, eps=1e-08, weight_decay=1.0 / 32768, accum_iter=4)
An example of a function that creates an AccumAdam optimizer with the given parameters and their names.
To change the hyperparameters of the optimizer, you can wrap it with functools.partial and pass the new values.
Returns:
| Name | Type | Description |
|---|---|---|
Optimizer |
Optimizer
|
an AccumAdam optimizer |
Source code in src/decent_dp/optim.py
optim_fn_accum_adamw(params, lr=0.001, beta1=0.9, beta2=0.999, eps=1e-08, weight_decay=0.1, accum_iter=4)
An example of a function that creates an AccumAdamW optimizer with the given parameters and their names.
To change the hyperparameters of the optimizer, you can wrap it with functools.partial and pass the new values.
Returns:
| Name | Type | Description |
|---|---|---|
Optimizer |
Optimizer
|
an AccumAdamW optimizer |
Source code in src/decent_dp/optim.py
lr_scheduler_fn_cosine_with_warmup(optimizer, t_max, t_warmup, cosine_eta_min=1e-06, warmup_decay=0.01)
An example of a function that creates a learning rate scheduler that combines a warmup and a cosine annealing schedule.
Returns:
| Name | Type | Description |
|---|---|---|
LRScheduler |
LRScheduler
|
a learning rate scheduler with the linear warmup followed by the cosine annealing |
Source code in src/decent_dp/optim.py
accum_adamw_foreach(params, grads, exp_avgs, exp_avg_sqs, accum_grads, state_steps, beta1, beta2, lr, weight_decay, eps, accum_iter)
Optimized version of AccumAdamW optimizer using torch._foreach TODO: fused kernel
Source code in src/decent_dp/optim.py
accum_adam_foreach(params, grads, exp_avgs, exp_avg_sqs, accum_grads, state_steps, beta1, beta2, lr, weight_decay, eps, accum_iter)
Optimized version of AccumAdam optimizer using torch._foreach TODO: write a fused kernel for this
Source code in src/decent_dp/optim.py
decent_dp.topo
This module contains the topology classes that define communication patterns between workers in decentralized training.
decent_dp.topo
Edge
dataclass
Edge class for defining communication patterns among workers.
Weight defines the fraction of the message that each worker keeps. For example, if the weight is 0.3, then the worker keeps 30% of its message and shares 70% with other workers. $$x_i = w \cdot x_i + \frac{1}{|\text{ranks}|}\sum_{j \in \text{ranks}} x_j * (1 - w)$$. The weight should be between 0 and 1 for convergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ranks
|
List[int]
|
List of ranks of workers that communicate in this edge |
required |
weight
|
List[float]
|
Weight for each worker in the edge |
required |
group
|
Optional[ProcessGroup]
|
Process group for the edge, which will be created by Topology class |
None
|
Source code in src/decent_dp/topo.py
Topology
Source code in src/decent_dp/topo.py
__init__(local_world_size)
Topology class for defining communication patterns between workers.
The class is responsible for creating process groups for each edge in the topology.
The edges are defined as a list of lists of Edge objects, where each list of edges corresponds to one iteration of the communication pattern.
The topology is defined by implementing the _get_topo_edges method, which should return a list of lists of Edge objects. When creating a new topology, the method should be implemented to return the edges for the topology. Usable variables are self._world_size, self._local_world_size, and self._n_nodes for the number of processes, processes per node, and number of nodes, respectively.
A valid topology is one where each node participates in exactly one communication in each iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
local_world_size
|
int
|
Number of processes in each node (added as argument for some testing purposes, should be set as the environment variable LOCAL_WORLD_SIZE for normal cases) |
required |
Source code in src/decent_dp/topo.py
CompleteTopology
Bases: Topology
Complete topology where each node communicates with all other nodes. The weights are 1/n.
Source code in src/decent_dp/topo.py
RingTopology
Bases: Topology
One-peer ring topology where each node communicates with one of its left and right neighbors (by index) in each iteration. The weights are 0.5 for each neighbor.
Source code in src/decent_dp/topo.py
OnePeerExpTopology
Bases: Topology
One-peer exponential topology.
Source code in src/decent_dp/topo.py
decent_dp.utils
Utility functions for setting up and managing the distributed training environment.
decent_dp.utils
initialize_dist()
A utility function to initialize the distributed environment
Returns:
| Type | Description |
|---|---|
Tuple[int, int]
|
Tuple[int, int]: rank and world size |