Megatron parallel state utils
This package contains utilities for managing the state of distributed model parallelism in Megatron and Apex.
In general you should just use the context manager distributed_model_parallel_state
to manage the state of
your test. This context manager will handle the setup and teardown of the distributed model parallel state for you.
Example usage:
from bionemo.testing import megatron_parallel_state_utils
def my_test():
with megatron_parallel_state_utils.distributed_model_parallel_state():
# your test code that requires megatron/apex parallel state to be set up here
_MockMegatronParallelStateSingleton
Source code in bionemo/testing/megatron_parallel_state_utils.py
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 |
|
__init__(world_size=torch.cuda.device_count(), rank=int(os.getenv('LOCAL_RANK', 0)), inited=False, store=FakeStore())
A singleton to deal with global megatron state for simulating a fake cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
world_size
|
the cluster size. Defaults to torch.cuda.device_count(). |
device_count()
|
|
rank
|
rank of this node. Defaults to int(os.getenv("LOCAL_RANK", 0)). |
int(getenv('LOCAL_RANK', 0))
|
|
inited
|
if this global cluster has been initiated. Defaults to False. |
False
|
|
store
|
the FakeStore for process groups. Defaults to FakeStore(). |
FakeStore()
|
Source code in bionemo/testing/megatron_parallel_state_utils.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
_reset_microbatch_calculator()
Resets _GLOBAL_NUM_MICROBATCHES_CALCULATOR in megatron which is used in NeMo to initilised model parallel in nemo.collections.nlp.modules.common.megatron.megatron_init.initialize_model_parallel_for_nemo
Source code in bionemo/testing/megatron_parallel_state_utils.py
60 61 62 63 64 |
|
clean_up_distributed_and_parallel_states()
Clean up parallel states, torch.distributed and torch cuda cache.
Source code in bionemo/testing/megatron_parallel_state_utils.py
67 68 69 70 71 72 73 |
|
distributed_model_parallel_state(seed=42, rank=0, world_size=1, backend='nccl', **initialize_model_parallel_kwargs)
Context manager for torch distributed and parallel state testing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
random seed to be passed into tensor_parallel.random (https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/tensor_parallel/random.py). default to 42. |
42
|
rank
|
int
|
global rank of the current cuda device. default to 0. |
0
|
world_size
|
int
|
world size or number of devices. default to 1. |
1
|
backend
|
str
|
backend to torch.distributed.init_process_group. default to 'nccl'. |
'nccl'
|
**initialize_model_parallel_kwargs
|
kwargs to be passed into initialize_model_parallel (https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/parallel_state.py). |
{}
|
Source code in bionemo/testing/megatron_parallel_state_utils.py
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 |
|
mock_distributed_parallel_state(world_size=8, rank=0, tensor_model_parallel_size=1, pipeline_model_parallel_size=1, virtual_pipeline_model_parallel_size=None, context_parallel_size=1, expert_model_parallel_size=1, seed=42)
A context manager that facilitates easy mocking of torch.distributed for an arbitrary GPU in a simulated cluster.
Key functions that are mocked
torch.distributed.new_group
whenbackend="gloo"
which doesn't support abackend="fake"
torch.distributed.destroy_process_group
whenbackend="gloo"
since new "gloo" groups are not actually madetorch._C._cuda_setDevice
which changes the current device behind the scenes. We assign devices round-robin to supportworld_size > torch.cuda.device_count()
.
Outside of this mocking, a fake cluster is initialized using backend="fake"
in torch.distributed
. This sets up
enough global state and environment for megatron to think that it is initializing a larger cluster with some
settings where the current context has some user defined rank. You can then test the megatron state on a
hypothetical rank in some large world size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
world_size
|
int
|
The world size (cluster size). Defaults to 8. |
8
|
rank
|
int
|
the GPU number globally in the cluster. Defaults to 0. |
0
|
tensor_model_parallel_size
|
int
|
tensor model parallel setting for megatron. Defaults to 1. |
1
|
pipeline_model_parallel_size
|
int
|
pipeline model parallel setting for megatron. Defaults to 1. |
1
|
virtual_pipeline_model_parallel_size
|
Optional[int]
|
virtual pipeline model parallel size for megatron. Defaults to None. |
None
|
context_parallel_size
|
int
|
context parallel size. Defaults to 1. |
1
|
expert_model_parallel_size
|
int
|
expert model parallel size. Defaults to 1. |
1
|
seed
|
int | None
|
seed for RNG state. Defaults to 42. |
42
|
Source code in bionemo/testing/megatron_parallel_state_utils.py
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 |
|