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 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
|
# Owner(s): ["oncall: distributed"]
import sys
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import distributed as dist
from torch.distributed.distributed_c10d import _get_default_group
from torch.distributed.algorithms._comm_hooks import default_hooks
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
from torch.distributed.fsdp import MixedPrecision
from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy
from torch.testing._internal.common_distributed import (
requires_nccl,
requires_nccl_version,
sandcastle_skip_if,
skip_if_lt_x_gpu,
skip_if_rocm,
)
from torch.testing._internal.common_fsdp import FSDPTest
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
)
if not dist.is_available():
print("Distributed not available, skipping tests", file=sys.stderr)
sys.exit(0)
# bfloat16 is only supported by CUDA 11+
BFLOAT16_AVAILABLE = (
torch.cuda.is_available()
and torch.version.cuda is not None
and int(torch.version.cuda.split('.')[0]) >= 11)
class Net(nn.Module):
def __init__(self, has_wrapping, sharding_strategy, mixed_precision=None):
# to ensure determinism
torch.manual_seed(0)
torch.cuda.manual_seed(0)
super().__init__()
if has_wrapping:
self.net = FSDP(nn.Sequential(
nn.Linear(8, 16),
nn.ReLU(),
FSDP(
nn.Linear(16, 8),
device_id=torch.cuda.current_device(),
sharding_strategy=sharding_strategy,
mixed_precision=mixed_precision,
)
),
device_id=torch.cuda.current_device(),
sharding_strategy=sharding_strategy,
mixed_precision=mixed_precision,
)
else:
self.net = nn.Sequential(
nn.Linear(8, 16),
nn.ReLU(),
nn.Linear(16, 8)
)
self.out = nn.Linear(8, 4)
def forward(self, x):
return self.out(F.relu(self.net(x)))
class DummyState(object):
__slots__ = [
"process_group",
"noise"
]
def __init__(self, process_group: dist.ProcessGroup, noise: int):
self.process_group = process_group
self.noise = noise
class DummyHook(object):
def dummy_hook_for_no_shard_fsdp(self, state: DummyState, grad: torch.Tensor):
"""
This communication hook is for illustration and testing purpose only.
This communication hook is used during FSDP ``NO_SHARD`` training. It adds some noise to
the provided ``grad`` parameter and uses ``all_reduce`` to communicate full, flattened,
unsharded gradient.
"""
grad.add_(state.noise)
dist.all_reduce(grad, group=state.process_group)
def custom_reduce_scatter(self, output, input, group=None):
"""
This function is for illustrative purpose only.
It is meant to implement a custom reduce-scatter
of a flattened tensor to all processes in a group.
Currently a no-op.
"""
pass
def dummy_hook_for_sharded_fsdp(self, state: DummyState, grad: torch.Tensor, output: torch.Tensor):
"""
This communication hook is for illustration and testing purposes only.
This communication hook is used during FSDP ``FULL_SHARD`` or ``SHARD_GRAD_OP`` training.
It adds some noise to the provided ``grad`` parameter, uses
``reduce_scatter`` for gradient communication and stores a sharded gradient in ``output``.
"""
grad.add_(state.noise)
self.custom_reduce_scatter(
output, grad, group=state.process_group
)
class TestCommunicationHooks(FSDPTest):
@skip_if_lt_x_gpu(2)
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_default_communication_hook_behavior(
self,
sharding_strategy: Optional[ShardingStrategy]
):
"""
Tests FSDP's default communication hook's behavior and correctness.
This test creates a simple linear net with weight shape ``1 X N``,
where ``N`` is the number of workers.
For sharded cases, each worker gets 1 element of the weight parameter. This test
checks that after backward, each worker has a proper value in its chunk of
the gradient, or the whole gradient on every worker is equal to an expected value.
Arguments:
sharding_strategy (Optional[ShardingStrategy]): Configures the FSDP algorithm.
"""
out_dim = self.world_size
net = torch.nn.Linear(1, out_dim, bias=False)
inpt = torch.tensor([self.rank]).float().cuda(self.rank)
net_default_hook = FSDP(
net,
device_id=torch.cuda.current_device(),
sharding_strategy=sharding_strategy
).to(self.rank)
# Check that default hook is set to `all_reduce` for `NO_SHARD`
# or `reduce_scatter` for sharded cases
default_hook = default_hooks.reduce_scatter_hook\
if sharding_strategy != ShardingStrategy.NO_SHARD\
else default_hooks.allreduce_hook
for entry in FSDP.fsdp_modules(net_default_hook):
self.assertEqual(entry._communication_hook, default_hook)
for _ in range(4):
# Clear gradients
net_default_hook.zero_grad()
loss = net_default_hook(inpt).sum()
loss.backward()
# For each worker, the gradient on the weight should be worker_rank.
grad = net_default_hook.params[0].grad
expected_grad = (
sum(i for i in range(dist.get_world_size())) / dist.get_world_size()
)
# Verify default hook produces expected gradients
self.assertEqual(
grad[0].item(),
expected_grad,
msg=f"Expected hook grad of {expected_grad} but got {grad[0].item()}")
def _get_submodules(self, fsdp_net):
return [
submodule for submodule in FSDP.fsdp_modules(fsdp_net)
if not submodule.check_is_root()
]
def _init_model(self, core, sharding_strategy, mixed_precision=None):
device = torch.device("cuda")
return FSDP(
core,
device_id=torch.cuda.current_device(),
sharding_strategy=sharding_strategy,
mixed_precision=mixed_precision,
).to(device)
@skip_if_lt_x_gpu(2)
@parametrize("has_wrapping", [True, False])
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_default_communication_hook_initialization(
self,
has_wrapping: bool,
sharding_strategy: Optional[ShardingStrategy]
):
"""
Tests FSDP's communication hook interface behavior.
Arguments:
has_wrapping (bool): Configures wrapping of a module.
sharding_strategy (Optional[ShardingStrategy]): Configures the FSDP algorithm.
"""
# Initialize a model
fsdp_model_with_hook = self._init_model(
Net(has_wrapping=has_wrapping, sharding_strategy=sharding_strategy),
sharding_strategy=sharding_strategy
)
# Check that default hook is set to `all_reduce` for `NO_SHARD`
# or `reduce_scatter` for sharded cases
default_hook = default_hooks.reduce_scatter_hook\
if sharding_strategy != ShardingStrategy.NO_SHARD\
else default_hooks.allreduce_hook
for entry in FSDP.fsdp_modules(fsdp_model_with_hook):
self.assertEqual(entry._communication_hook, default_hook)
dummy_state = DummyState(process_group=None, noise=1234)
dummy_hook = DummyHook.dummy_hook_for_no_shard_fsdp\
if sharding_strategy != ShardingStrategy.NO_SHARD\
else DummyHook.dummy_hook_for_sharded_fsdp
fsdp_model_with_hook.register_comm_hook(
dummy_state,
dummy_hook
)
# Check that we can't register comm hook twice
with self.assertRaisesRegex(AssertionError, '^communication hook can be only registered once$'):
fsdp_model_with_hook.register_comm_hook(
dummy_state,
dummy_hook
)
# Check dummy hook was registered for the root and all submodules if any
for entry in FSDP.fsdp_modules(fsdp_model_with_hook):
self.assertEqual(
entry._communication_hook,
dummy_hook
)
self.assertEqual(
entry._communication_hook_state,
dummy_state
)
for entry in FSDP.fsdp_modules(fsdp_model_with_hook):
entry._communication_hook = None
in_data = torch.rand(16, 8).cuda()
loss = fsdp_model_with_hook(in_data).sum()
# This Error is raised during backward pass and is checked with `p_assert`,
# i.e. it prints error string but AssertionError raises nothing
with self.assertRaises(AssertionError):
loss.backward()
for entry in FSDP.fsdp_modules(fsdp_model_with_hook):
entry._communication_hook = dummy_hook
entry._communication_hook_state = None
# Same as above
loss = fsdp_model_with_hook(in_data).sum()
with self.assertRaises(AssertionError):
loss.backward()
@skip_if_lt_x_gpu(2)
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_registering_hook_non_root(
self,
sharding_strategy: Optional[ShardingStrategy]
):
"""
Tests FSDP's communication hook registering for submodules.
Make sure it can't be registered for non-root submodules.
Currently tests only ``NO_SHARD`` strategy.
Arguments:
sharding_strategy (Optional[ShardingStrategy]): Configures the FSDP algorithm.
"""
fsdp_model_with_hook = self._init_model(
Net(has_wrapping=True, sharding_strategy=sharding_strategy),
sharding_strategy=sharding_strategy
)
dummy_state = DummyState(process_group=None, noise=1234)
dummy_hook = DummyHook.dummy_hook_for_no_shard_fsdp\
if sharding_strategy != ShardingStrategy.NO_SHARD\
else DummyHook.dummy_hook_for_sharded_fsdp
# Creating a list of non-root submodules to test
submodules = self._get_submodules(fsdp_model_with_hook)
# Check that assertion is raised for registering a comm hook on a non-root
with self.assertRaisesRegex(AssertionError, '^register_comm_hook can only be called on a root instance.$'):
submodules[1].register_comm_hook(dummy_state, dummy_hook)
@skip_if_lt_x_gpu(2)
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_registering_hook_submodules(
self,
sharding_strategy: Optional[ShardingStrategy]
):
"""
Tests FSDP's communication hook registering for submodules.
Checks behavior if a hook was registered for a non-root submodule
Currently tests only ``NO_SHARD`` strategy.
Arguments:
sharding_strategy (Optional[ShardingStrategy]): Configures the FSDP algorithm.
"""
fsdp_model_with_hook = self._init_model(
Net(has_wrapping=True, sharding_strategy=sharding_strategy),
sharding_strategy=sharding_strategy
)
dummy_state = DummyState(process_group=None, noise=1234)
dummy_hook = DummyHook.dummy_hook_for_no_shard_fsdp\
if sharding_strategy != ShardingStrategy.NO_SHARD\
else DummyHook.dummy_hook_for_sharded_fsdp
submodules = self._get_submodules(fsdp_model_with_hook)
# Simulate a registration of a hook on a submodule
submodules[1]._hook_registered = True
# Check that an error is raised when some of submodules have a non-default hook assigned
with self.assertRaisesRegex(AssertionError, '^communication hook can be only registered once$'):
fsdp_model_with_hook.register_comm_hook(dummy_state, dummy_hook)
# Reinitialize the model
fsdp_model_with_hook = self._init_model(
Net(has_wrapping=True, sharding_strategy=sharding_strategy),
sharding_strategy=sharding_strategy
)
submodules = self._get_submodules(fsdp_model_with_hook)
submodules[1]._communication_hook = dummy_hook
# Check that an error is raised when some of submodules have a non-default hook assigned
with self.assertRaisesRegex(
AssertionError,
f'^communication hook should be default, but it is {submodules[1]._communication_hook.__name__} instead$'
):
fsdp_model_with_hook.register_comm_hook(
dummy_state,
dummy_hook
)
def _check_low_precision_hook(self, state, hook, sharding_strategy, dtype, has_wrapping):
# keep everything deterministic for input data
torch.manual_seed(0)
torch.cuda.manual_seed(0)
fsdp_with_hook = self._init_model(
Net(has_wrapping=has_wrapping, sharding_strategy=sharding_strategy),
sharding_strategy=sharding_strategy
)
fsdp_with_hook.register_comm_hook(state, hook)
mp_only_grad = MixedPrecision(reduce_dtype=dtype)
fsdp_with_mp = self._init_model(
Net(has_wrapping=has_wrapping, sharding_strategy=sharding_strategy, mixed_precision=mp_only_grad),
sharding_strategy=sharding_strategy,
mixed_precision=mp_only_grad
)
optim_hook = torch.optim.SGD(fsdp_with_hook.parameters(), lr=0.1)
optim_mp = torch.optim.SGD(fsdp_with_mp.parameters(), lr=0.1)
in_data = torch.rand(16, 8).cuda()
fsdp_with_hook.train()
fsdp_with_mp.train()
loss_hook = fsdp_with_hook(in_data).sum()
loss_mp = fsdp_with_mp(in_data).sum()
loss_hook.backward()
# Make sure grads were cast to the parameter's precision
self.assertEqual(fsdp_with_hook.params[0].dtype, state.parameter_type)
loss_mp.backward()
optim_hook.step()
optim_mp.step()
dist.barrier()
for hook_param, mp_param in zip(fsdp_with_hook.parameters(), fsdp_with_mp.parameters()):
self.assertEqual(hook_param.grad, mp_param.grad)
@requires_nccl()
@skip_if_lt_x_gpu(2)
@parametrize("has_wrapping", [True, False])
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_fp16_hook(
self,
has_wrapping: bool,
sharding_strategy: Optional[ShardingStrategy]
):
state = default_hooks.LowPrecisionState(process_group=_get_default_group())
hook = default_hooks.fp16_compress_hook
self._check_low_precision_hook(state, hook, sharding_strategy, torch.float16, has_wrapping)
@requires_nccl()
@requires_nccl_version((2, 10), "Need NCCL 2.10+ for BF16_COMPRESS")
@sandcastle_skip_if(
not BFLOAT16_AVAILABLE,
"BFloat16 is only supported by CUDA 11+",
)
@skip_if_lt_x_gpu(2)
@skip_if_rocm
@parametrize("has_wrapping", [True, False])
@parametrize(
"sharding_strategy",
[
ShardingStrategy.NO_SHARD,
ShardingStrategy.FULL_SHARD,
ShardingStrategy.SHARD_GRAD_OP
])
def test_bf16_hook(
self,
has_wrapping: bool,
sharding_strategy: Optional[ShardingStrategy]
):
state = default_hooks.LowPrecisionState(process_group=_get_default_group())
hook = default_hooks.bf16_compress_hook
self._check_low_precision_hook(state, hook, sharding_strategy, torch.bfloat16, has_wrapping)
instantiate_parametrized_tests(TestCommunicationHooks)
if __name__ == "__main__":
run_tests()
|