2022-03-03 07:06:18 +00:00
|
|
|
from enum import Enum
|
2022-03-29 01:08:18 +00:00
|
|
|
from os import stat
|
|
|
|
from typing import Dict, Optional, Tuple
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
import torch
|
|
|
|
import torch.distributed as dist
|
|
|
|
import torch.nn as nn
|
2022-03-15 02:05:38 +00:00
|
|
|
from colossalai.amp.naive_amp.grad_scaler import DynamicGradScaler
|
2022-03-03 07:06:18 +00:00
|
|
|
from colossalai.context.parallel_mode import ParallelMode
|
|
|
|
from colossalai.core import global_context as gpc
|
2022-03-18 08:18:31 +00:00
|
|
|
from colossalai.logging import get_dist_logger
|
2022-03-03 07:06:18 +00:00
|
|
|
from colossalai.nn.optimizer import ColossalaiOptimizer
|
2022-04-24 09:17:22 +00:00
|
|
|
from colossalai.gemini.tensor_utils import (colo_model_data_tensor_move_inline, colo_tensor_mem_usage)
|
2022-03-03 07:06:18 +00:00
|
|
|
from colossalai.zero.sharded_model import ShardedModelV2
|
2022-03-25 06:54:39 +00:00
|
|
|
from colossalai.zero.sharded_model._utils import cast_tensor_to_fp32
|
2022-03-30 10:14:50 +00:00
|
|
|
from torch import Tensor
|
|
|
|
from torch.distributed import ProcessGroup
|
|
|
|
from torch.nn.parameter import Parameter
|
|
|
|
from torch.optim import Optimizer
|
2022-04-24 05:08:48 +00:00
|
|
|
from colossalai.gemini.stateful_tensor import (StatefulTensor, TensorState)
|
2022-04-14 08:40:26 +00:00
|
|
|
from colossalai.gemini.tensor_placement_policy import AutoTensorPlacementPolicy
|
2022-03-29 07:45:48 +00:00
|
|
|
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
class OptimState(Enum):
|
|
|
|
SCALED = 1
|
|
|
|
UNSCALED = 2
|
|
|
|
|
|
|
|
|
2022-03-03 07:55:27 +00:00
|
|
|
class ShardedOptimizerV2(ColossalaiOptimizer):
|
2022-04-01 06:50:56 +00:00
|
|
|
"""A wrapper for optimizer. ``ShardedOptimizerV2`` and ``ShardedModelV2`` implement Zero Redundancy Optimizer (ZeRO).
|
2022-03-29 01:08:18 +00:00
|
|
|
|
2022-03-24 06:29:41 +00:00
|
|
|
By default the ZeRO optimizer stage 3 offload Optimizer States on CPU.
|
2022-03-29 04:48:00 +00:00
|
|
|
|
2022-03-24 06:29:41 +00:00
|
|
|
We apply the Device-aware Operator Placement technique for OS placement from the following paper.
|
2022-03-29 01:08:18 +00:00
|
|
|
|
2022-04-01 06:50:56 +00:00
|
|
|
`PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management`_
|
2022-03-29 04:48:00 +00:00
|
|
|
|
2022-03-24 06:29:41 +00:00
|
|
|
GPU margin space is the remaining space after removing peak non-model data from the overall GPU memory,
|
|
|
|
which is detected by a runtime memory tracer.
|
2022-03-29 04:48:00 +00:00
|
|
|
|
2022-03-24 06:29:41 +00:00
|
|
|
We place as many OS chunks in the margin space as possible.
|
2022-03-29 04:48:00 +00:00
|
|
|
|
2022-04-01 06:50:56 +00:00
|
|
|
The size of margin space can be controlled by ``gpu_margin_mem_ratio``.
|
|
|
|
If it is set as ``0.0``, it is the same as classical ZeRO optimizer.
|
2022-03-24 06:29:41 +00:00
|
|
|
|
2022-04-01 06:50:56 +00:00
|
|
|
Note:
|
|
|
|
You must use ``ShardedOptimizerV2`` with ``ShardedModelV2``.
|
|
|
|
|
|
|
|
Note:
|
2022-04-14 13:03:59 +00:00
|
|
|
Make sure you set ``tensor_placement_policy`` in ``ShardedModelV2`` to `"auto"`,
|
2022-04-01 06:50:56 +00:00
|
|
|
if you set ``gpu_margin_mem_ratio > 0``.
|
2022-03-18 08:48:20 +00:00
|
|
|
|
2022-03-22 06:56:59 +00:00
|
|
|
Args:
|
|
|
|
sharded_model (ShardedModelV2): A sharded model initialized by class ShardedModelV2. The optimizer will use the
|
|
|
|
shard strategy provided by sharded model to shard param fp32 tensors.
|
|
|
|
optimizer (Optimizer): An Optimizer instance.
|
|
|
|
gpu_margin_mem_ratio (float, optional): The ratio of GPU remaining memory (after the first forward-backward)
|
2022-04-01 06:50:56 +00:00
|
|
|
which will be used when using hybrid CPU optimizer.
|
2022-04-13 07:00:48 +00:00
|
|
|
This argument is meaningless when `tensor_placement_policy` of `ShardedModelV2` is not "auto".
|
2022-04-01 06:50:56 +00:00
|
|
|
Defaults to 0.0.
|
2022-03-22 06:56:59 +00:00
|
|
|
initial_scale (float, optional): Initial scale used by DynamicGradScaler. Defaults to 2**32.
|
|
|
|
min_scale (float, optional): Min scale used by DynamicGradScaler. Defaults to 1.
|
|
|
|
growth_factor (float, optional): growth_factor used by DynamicGradScaler. Defaults to 2.
|
|
|
|
backoff_factor (float, optional): backoff_factor used by DynamicGradScaler. Defaults to 0.5.
|
|
|
|
growth_interval (float, optional): growth_interval used by DynamicGradScaler. Defaults to 1000.
|
|
|
|
hysteresis (float, optional): hysteresis used by DynamicGradScaler. Defaults to 2.
|
|
|
|
max_scale (int, optional): max_scale used by DynamicGradScaler. Defaults to 2**32.
|
|
|
|
dp_process_group (Optional[ProcessGroup], optional): data paralle process group. Defaults to None.
|
|
|
|
mp_process_group (Optional[ProcessGroup], optional): model paralle process group. Defaults to None.
|
2022-04-01 06:50:56 +00:00
|
|
|
|
|
|
|
.. _PatrickStar\: Parallel Training of Pre-trained Models via Chunk-based Memory Management:
|
|
|
|
https://arxiv.org/abs/2108.05818
|
2022-03-22 06:56:59 +00:00
|
|
|
"""
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
def __init__(self,
|
2022-03-09 08:09:36 +00:00
|
|
|
sharded_model: ShardedModelV2,
|
2022-03-18 07:44:47 +00:00
|
|
|
optimizer: Optimizer,
|
2022-03-22 06:56:59 +00:00
|
|
|
gpu_margin_mem_ratio: float = 0.0,
|
2022-03-03 07:06:18 +00:00
|
|
|
initial_scale: float = 2**32,
|
|
|
|
min_scale: float = 1,
|
|
|
|
growth_factor: float = 2,
|
|
|
|
backoff_factor: float = 0.5,
|
2022-04-18 05:57:03 +00:00
|
|
|
growth_interval: int = 1000,
|
|
|
|
hysteresis: int = 2,
|
|
|
|
max_scale: float = 2**32,
|
2022-03-03 07:06:18 +00:00
|
|
|
dp_process_group: Optional[ProcessGroup] = None,
|
2022-04-18 05:57:03 +00:00
|
|
|
mp_process_group: Optional[ProcessGroup] = None,
|
|
|
|
verbose: bool = False) -> None:
|
2022-03-09 08:09:36 +00:00
|
|
|
assert isinstance(sharded_model, ShardedModelV2), 'model must be wrapped with ShardedModel'
|
2022-06-21 03:33:53 +00:00
|
|
|
assert not isinstance(optimizer, ShardedOptimizerV2), 'Nested ShardedOptimizerV2 is not supported.'
|
2022-03-14 12:48:41 +00:00
|
|
|
|
2022-03-18 07:44:47 +00:00
|
|
|
super().__init__(optimizer)
|
2022-03-15 02:45:55 +00:00
|
|
|
self.shard_strategy = sharded_model.shard_strategy
|
2022-03-09 08:09:36 +00:00
|
|
|
self.model: ShardedModelV2 = sharded_model
|
2022-04-13 07:00:48 +00:00
|
|
|
|
2022-03-22 06:56:59 +00:00
|
|
|
self.gpu_margin_mem_ratio: float = float(gpu_margin_mem_ratio)
|
|
|
|
assert 0.0 <= self.gpu_margin_mem_ratio <= 1.0, f'gpu_margin_mem_ratio must >=0.0 and <=1.0'
|
|
|
|
# Only move fp32 shards from CPU to GPU when user allows and inner optimizer is valid
|
|
|
|
# Inner optimizer must support optimizing hybrid (CPU and CUDA) tensors,
|
|
|
|
# and it must set `num_fp32_shards_per_param` correctly
|
2022-04-13 07:00:48 +00:00
|
|
|
self._should_move_fp32_shards_h2d: bool = sharded_model.cpu_offload and self.gpu_margin_mem_ratio > 0.0 and getattr(
|
2022-03-22 06:56:59 +00:00
|
|
|
optimizer, 'num_fp32_shards_per_param', 0) >= 2
|
2022-04-13 07:00:48 +00:00
|
|
|
self.device = sharded_model._tensor_placement_policy.device or torch.device('cpu')
|
2022-03-03 07:06:18 +00:00
|
|
|
self.optim_state: OptimState = OptimState.UNSCALED
|
|
|
|
self.dp_process_group = dp_process_group or gpc.get_group(ParallelMode.DATA)
|
|
|
|
self.mp_process_group = mp_process_group or gpc.get_group(ParallelMode.MODEL)
|
|
|
|
# Grad scaler
|
|
|
|
self.grad_scaler = DynamicGradScaler(initial_scale=initial_scale,
|
|
|
|
min_scale=min_scale,
|
|
|
|
growth_factor=growth_factor,
|
|
|
|
backoff_factor=backoff_factor,
|
|
|
|
growth_interval=growth_interval,
|
|
|
|
hysteresis=hysteresis,
|
|
|
|
max_scale=max_scale)
|
2022-04-11 07:40:13 +00:00
|
|
|
self._found_overflow: Tensor = torch.IntTensor([0]).to(torch.cuda.current_device())
|
2022-03-29 01:08:18 +00:00
|
|
|
self._logger = get_dist_logger("ShardedOptimizerV2")
|
2022-04-18 05:57:03 +00:00
|
|
|
self._verbose = verbose
|
2022-03-03 07:42:53 +00:00
|
|
|
|
2022-04-01 12:10:47 +00:00
|
|
|
# Store fp32 param shards
|
|
|
|
self._register_master_weight()
|
2022-04-14 13:03:59 +00:00
|
|
|
if self.gpu_margin_mem_ratio != 0.0 and not isinstance(sharded_model._tensor_placement_policy,
|
|
|
|
AutoTensorPlacementPolicy):
|
2022-04-20 02:05:39 +00:00
|
|
|
self._logger.warning(f'gpu_margin_mem_ratio is meaningless when tensor_placement_policy is not "auto"',
|
|
|
|
ranks=[0])
|
2022-04-18 05:57:03 +00:00
|
|
|
|
|
|
|
if self._verbose:
|
|
|
|
self._logger.debug(
|
|
|
|
f"After init ShardedOptimizerV2 consumes {self.get_memory_usage()[0] / 1e6} MB CUDA Memory!", ranks=[0])
|
2022-03-29 01:08:18 +00:00
|
|
|
|
2022-03-29 07:45:48 +00:00
|
|
|
self._use_memory_tracer = self.model.use_memory_tracer
|
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
@property
|
|
|
|
def loss_scale(self):
|
|
|
|
return self.grad_scaler.scale.item()
|
|
|
|
|
2022-03-29 01:08:18 +00:00
|
|
|
def get_memory_usage(self) -> Tuple[int, int]:
|
2022-04-01 06:50:56 +00:00
|
|
|
""" Get the memory usage of the optimizer. Including master_params (param fp32),
|
|
|
|
momentum (``self.state[p]['exp_avg']``) variance (``self.state[p]['exp_avg_sq']``)
|
2022-03-29 01:08:18 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[int, int]: cuda/cpu memory usage in Byte.
|
|
|
|
"""
|
|
|
|
cuda_use = 0
|
|
|
|
cpu_use = 0
|
|
|
|
|
|
|
|
def update_mem_use(t):
|
|
|
|
nonlocal cuda_use
|
|
|
|
nonlocal cpu_use
|
|
|
|
t_cuda_use, t_cpu_use = colo_tensor_mem_usage(t)
|
|
|
|
cuda_use += t_cuda_use
|
|
|
|
cpu_use += t_cpu_use
|
|
|
|
|
|
|
|
for _, p_fp32 in self.master_params.items():
|
|
|
|
update_mem_use(p_fp32)
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
|
|
|
state = self.optim.state[p]
|
|
|
|
for k, v in state.items():
|
|
|
|
update_mem_use(v)
|
|
|
|
|
|
|
|
return cuda_use, cpu_use
|
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
def zero_grad(self, *args, **kwargs):
|
|
|
|
self._zero_grad()
|
|
|
|
|
|
|
|
def backward(self, loss: Tensor) -> None:
|
|
|
|
loss = self.loss_scale * loss
|
|
|
|
self.optim_state = OptimState.SCALED
|
|
|
|
self.model.backward(loss)
|
|
|
|
|
|
|
|
def backward_by_grad(self, tensor: Tensor, grad: Tensor) -> None:
|
2022-07-08 05:34:48 +00:00
|
|
|
# This function is called except the last stage of pipeline parallel
|
|
|
|
# It receives the scaled grad from the previous rank
|
|
|
|
# No need to scale the grad again
|
|
|
|
# Need to unscale when optimizing
|
|
|
|
self.optim_state = OptimState.SCALED
|
2022-04-11 05:38:51 +00:00
|
|
|
self.model.backward_by_grad(tensor, grad)
|
|
|
|
|
|
|
|
def clip_grad_norm(self, model: nn.Module, max_norm: float):
|
|
|
|
if self.optim_state == OptimState.SCALED:
|
2022-07-08 05:34:48 +00:00
|
|
|
self._prepare_grads()
|
2022-04-11 05:38:51 +00:00
|
|
|
self._unscale_grads()
|
|
|
|
return super().clip_grad_norm(model, max_norm)
|
|
|
|
|
2022-03-03 07:06:18 +00:00
|
|
|
def step(self, *args, **kwargs):
|
2022-03-22 06:56:59 +00:00
|
|
|
|
2022-03-03 07:06:18 +00:00
|
|
|
# unscale grads if scaled
|
|
|
|
if self.optim_state == OptimState.SCALED:
|
2022-07-08 05:34:48 +00:00
|
|
|
self._prepare_grads()
|
2022-03-03 07:06:18 +00:00
|
|
|
self._unscale_grads()
|
|
|
|
|
2022-07-08 05:34:48 +00:00
|
|
|
self._maybe_move_fp32_shards()
|
2022-03-03 07:06:18 +00:00
|
|
|
found_inf = self._check_overflow()
|
|
|
|
self.grad_scaler.update(found_inf)
|
|
|
|
|
|
|
|
if found_inf:
|
2022-03-29 01:08:18 +00:00
|
|
|
self._logger.warning('found inf during ShardedOptimV2 step')
|
2022-04-01 04:41:20 +00:00
|
|
|
self._zero_grad(recover_data=True)
|
2022-03-03 07:06:18 +00:00
|
|
|
return
|
|
|
|
|
2022-04-03 13:48:06 +00:00
|
|
|
self._point_param_fp16_to_master_param()
|
2022-03-10 09:51:50 +00:00
|
|
|
|
2022-04-18 05:57:03 +00:00
|
|
|
if self._verbose:
|
|
|
|
gpu_mem, cpu_mem = self.get_memory_usage()
|
|
|
|
self._logger.debug(
|
|
|
|
f"Before step ShardedOptimizerV2 consumes {gpu_mem / 1e6} MB CUDA Memory, {cpu_mem / 1e6} MB CUDA Memory!",
|
|
|
|
ranks=[0])
|
2022-03-18 07:44:47 +00:00
|
|
|
ret = self.optim.step(*args, **kwargs)
|
2022-03-10 09:51:50 +00:00
|
|
|
|
2022-04-18 05:57:03 +00:00
|
|
|
if self._verbose:
|
|
|
|
gpu_mem, cpu_mem = self.get_memory_usage()
|
|
|
|
self._logger.debug(
|
|
|
|
f"After step ShardedOptimizerV2 consumes {gpu_mem / 1e6} MB CUDA Memory, {cpu_mem / 1e6} MB CUDA Memory!",
|
|
|
|
ranks=[0])
|
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
self._copy_master_model_to_model_fp16()
|
2022-03-03 07:06:18 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def _check_overflow(self):
|
|
|
|
# clear previous overflow record
|
2022-04-11 07:40:13 +00:00
|
|
|
self._found_overflow.fill_(self.model.overflow_counter)
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
# all-reduce across dp group
|
2022-04-11 07:40:13 +00:00
|
|
|
dist.all_reduce(self._found_overflow, group=self.dp_process_group)
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
# all-reduce over model parallel group
|
2022-04-11 07:40:13 +00:00
|
|
|
dist.all_reduce(self._found_overflow, group=self.mp_process_group)
|
2022-03-03 07:06:18 +00:00
|
|
|
|
2022-03-03 07:50:30 +00:00
|
|
|
return self._found_overflow.item() > 0
|
2022-03-03 07:06:18 +00:00
|
|
|
|
|
|
|
def _unscale_grads(self):
|
|
|
|
assert self.optim_state == OptimState.SCALED
|
2022-03-18 07:44:47 +00:00
|
|
|
for group in self.optim.param_groups:
|
2022-03-03 07:06:18 +00:00
|
|
|
for p in group['params']:
|
|
|
|
if p.grad is not None:
|
|
|
|
p.grad.data.div_(self.loss_scale)
|
|
|
|
self.optim_state = OptimState.UNSCALED
|
2022-03-09 08:09:36 +00:00
|
|
|
|
2022-04-01 04:41:20 +00:00
|
|
|
def _zero_grad(self, recover_data: bool = False):
|
|
|
|
"""zero grad and maybe recover fp16 params
|
|
|
|
When `reuse_fp16_shard` is enabled,
|
|
|
|
p.colo_attr.sharded_data_tensor stores grad here.
|
|
|
|
We have to recover them from fp32 params.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
recover_data (bool, optional): Whether to recover fp16 param from fp32 param. Defaults to False.
|
|
|
|
"""
|
2022-03-09 08:09:36 +00:00
|
|
|
# We must set grad to None
|
2022-04-01 04:41:20 +00:00
|
|
|
# Because grad here is sharded
|
|
|
|
# But next backward pass will create a full grad first
|
|
|
|
# Which leads to wrong accumulation
|
2022-03-18 07:44:47 +00:00
|
|
|
self.optim.zero_grad(set_to_none=True)
|
2022-03-31 08:26:54 +00:00
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
2022-04-01 04:41:20 +00:00
|
|
|
# p.colo_attr.sharded_data_tensor stores grad now
|
|
|
|
# we have to recover fp16 param
|
2022-04-24 05:08:48 +00:00
|
|
|
reuse_fp16_shard = (p.colo_attr.sharded_data_tensor.payload_size == 0)
|
2022-04-01 04:41:20 +00:00
|
|
|
if recover_data and reuse_fp16_shard:
|
2022-04-11 05:38:51 +00:00
|
|
|
self._copy_master_param_to_param_fp16(p)
|
|
|
|
else:
|
|
|
|
# release saved gradient
|
|
|
|
p.colo_attr.saved_grad.set_null()
|
2022-04-11 07:40:13 +00:00
|
|
|
self.model.overflow_counter = 0 # set overflow counter to zero
|
2022-03-16 11:29:37 +00:00
|
|
|
|
|
|
|
def sync_grad(self):
|
|
|
|
pass
|
2022-03-22 07:53:48 +00:00
|
|
|
|
2022-04-01 12:10:47 +00:00
|
|
|
def _register_master_weight(self):
|
|
|
|
self.master_params: Dict[Parameter, StatefulTensor] = {}
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
|
|
|
assert hasattr(p, 'colo_attr'), 'The parameter must be wrapped with ShardedParam'
|
2022-04-11 05:38:51 +00:00
|
|
|
shard_flag = not p.colo_attr.sharded_data_tensor.is_sharded and p.colo_attr.is_replicated
|
|
|
|
if shard_flag:
|
|
|
|
# we always shard replicated paramters
|
2022-04-01 12:10:47 +00:00
|
|
|
self.shard_strategy.shard([p.colo_attr.sharded_data_tensor], self.dp_process_group)
|
2022-04-13 06:54:26 +00:00
|
|
|
self.master_params[p] = StatefulTensor(cast_tensor_to_fp32(p.colo_attr.data_payload.to(self.device)))
|
2022-04-11 05:38:51 +00:00
|
|
|
if shard_flag:
|
2022-04-01 12:10:47 +00:00
|
|
|
# In this branch, there's no need to shard param
|
|
|
|
# So we gather here
|
|
|
|
self.shard_strategy.gather([p.colo_attr.sharded_data_tensor], self.dp_process_group)
|
|
|
|
|
2022-03-22 07:53:48 +00:00
|
|
|
def _maybe_move_fp32_shards(self):
|
|
|
|
if self._should_move_fp32_shards_h2d:
|
|
|
|
self._should_move_fp32_shards_h2d = False
|
|
|
|
available_cuda_margin_mem = self.model.cuda_margin_space * self.gpu_margin_mem_ratio
|
|
|
|
fp32_shards_available_cuda_margin_mem = available_cuda_margin_mem / self.optim.num_fp32_shards_per_param
|
|
|
|
fp32_shards_used_cuda_margin_mem = 0
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
2022-03-31 08:26:54 +00:00
|
|
|
shard_mem = self.master_params[p].payload.numel() * self.master_params[p].payload.element_size()
|
2022-03-22 07:53:48 +00:00
|
|
|
if fp32_shards_used_cuda_margin_mem + shard_mem < fp32_shards_available_cuda_margin_mem:
|
2022-03-31 08:26:54 +00:00
|
|
|
colo_model_data_tensor_move_inline(self.master_params[p], torch.cuda.current_device())
|
2022-04-26 10:08:31 +00:00
|
|
|
colo_model_data_tensor_move_inline(p.colo_attr.saved_grad, torch.cuda.current_device())
|
2022-03-31 04:25:45 +00:00
|
|
|
p.colo_attr.offload_grad = False
|
2022-03-22 07:53:48 +00:00
|
|
|
fp32_shards_used_cuda_margin_mem += shard_mem
|
2022-06-24 10:05:16 +00:00
|
|
|
state = self.optim.state[p]
|
|
|
|
for k, v in state.items():
|
|
|
|
if isinstance(v, Tensor):
|
|
|
|
state[k] = v.cuda()
|
2022-03-30 10:14:50 +00:00
|
|
|
|
|
|
|
def _prepare_grads(self):
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
2022-04-13 14:36:11 +00:00
|
|
|
if p.colo_attr.saved_grad.is_null():
|
|
|
|
continue
|
2022-03-31 08:26:54 +00:00
|
|
|
p.colo_attr.saved_grad.trans_state(TensorState.COMPUTE)
|
2022-04-18 06:07:39 +00:00
|
|
|
# If reuse_fp16_shard, grad fp16 which wasn't be offloaded may be evicted to CPU
|
|
|
|
if not p.colo_attr.offload_grad:
|
2022-04-26 10:08:31 +00:00
|
|
|
colo_model_data_tensor_move_inline(p.colo_attr.saved_grad, torch.cuda.current_device())
|
2022-03-30 10:14:50 +00:00
|
|
|
# FIXME(ver217): p.data here is an empty tensor on CUDA and has no useful infomation
|
|
|
|
# If we change p.grad directly
|
|
|
|
# it may raise error because of different shape/dtype/device of p.data and p.grad
|
2022-03-31 04:25:45 +00:00
|
|
|
# We just set p.data = p.colo_attr.saved_grad.payload here
|
2022-04-13 06:54:26 +00:00
|
|
|
p.data = p.colo_attr.grad_payload
|
|
|
|
p.grad = p.colo_attr.grad_payload
|
2022-03-31 08:26:54 +00:00
|
|
|
# Set p.data to empty tensor, in case of memory leaking
|
2022-04-13 06:54:26 +00:00
|
|
|
p.colo_attr.set_data_none()
|
2022-03-31 08:26:54 +00:00
|
|
|
|
2022-04-03 13:48:06 +00:00
|
|
|
def _point_param_fp16_to_master_param(self):
|
2022-03-31 08:26:54 +00:00
|
|
|
# assign master param pointers to p.data.
|
|
|
|
# We will not trigger data copy here.
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
|
|
|
self.master_params[p].trans_state(TensorState.COMPUTE)
|
|
|
|
p.data = self.master_params[p].payload
|
|
|
|
# Now p.data is sharded
|
|
|
|
# So optimizer states are sharded naturally
|
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
def _copy_master_model_to_model_fp16(self):
|
2022-03-31 08:26:54 +00:00
|
|
|
# Copy master param data (fp32) to payload of colo_attr (fp16)
|
|
|
|
# TODO() improve efficiency by gathering tensors into a chunk and transfering
|
|
|
|
# a chunk.
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
2022-04-11 05:38:51 +00:00
|
|
|
self._copy_master_param_to_param_fp16(p)
|
2022-03-31 08:26:54 +00:00
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
def _copy_master_param_to_param_fp16(self, p):
|
|
|
|
# flush gradient
|
2022-04-24 05:08:48 +00:00
|
|
|
if p.colo_attr.sharded_data_tensor.payload_size == 0:
|
|
|
|
# here reuse_fp16_shard is True
|
|
|
|
# in order to use copy below, we should give sharded data tensor a payload
|
|
|
|
p.colo_attr.sharded_data_tensor.payload_relay(p.colo_attr.saved_grad)
|
|
|
|
else:
|
|
|
|
p.colo_attr.saved_grad.set_null()
|
2022-03-31 08:26:54 +00:00
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
p.data = self.master_params[p].payload
|
2022-04-24 05:08:48 +00:00
|
|
|
|
|
|
|
# we need to allocate new memory for keep_not_shard paramters
|
|
|
|
# in order to use copy, otherwise, the sizes of tensor is not compatible
|
|
|
|
if p.colo_attr.data_payload.numel() != p.data.numel():
|
|
|
|
p.colo_attr.data_payload_reset(
|
|
|
|
torch.empty(p.data.shape, dtype=p.colo_attr.data_payload.dtype, device=p.colo_attr.data_payload.device))
|
|
|
|
|
|
|
|
# TODO() optimize this line CPU (fp32) -> GPU (fp16)
|
|
|
|
p.colo_attr.sharded_data_tensor.payload_copy(p.half().detach())
|
2022-04-13 06:54:26 +00:00
|
|
|
p.colo_attr.set_data_none()
|
2022-04-11 05:38:51 +00:00
|
|
|
|
|
|
|
if p.colo_attr.keep_not_shard and p.colo_attr.is_replicated:
|
|
|
|
# We gather full fp16 param here
|
|
|
|
p.colo_attr.sharded_data_tensor.is_sharded = True # since only gradient is sharded, we should set to True
|
|
|
|
self.shard_strategy.gather([p.colo_attr.sharded_data_tensor], self.dp_process_group)
|
2022-04-08 12:23:26 +00:00
|
|
|
|
2022-04-11 05:38:51 +00:00
|
|
|
self.master_params[p].trans_state(TensorState.HOLD)
|
2022-06-24 10:05:16 +00:00
|
|
|
|
2022-07-21 07:21:21 +00:00
|
|
|
def state_dict(self):
|
|
|
|
optim_state_dict = super().state_dict()
|
|
|
|
scaler_state_dict = self.grad_scaler.state_dict()
|
|
|
|
optim_state_dict['scaler'] = scaler_state_dict
|
|
|
|
return optim_state_dict
|
|
|
|
|
2022-06-24 10:05:16 +00:00
|
|
|
def load_state_dict(self, *args, **kwargs):
|
2022-07-21 07:21:21 +00:00
|
|
|
if 'scaler' not in args[0]:
|
|
|
|
self._logger.warning('Missing scaler when loading optimizer state dict', ranks=[0])
|
|
|
|
else:
|
|
|
|
scaler_state_dict = args[0].pop('scaler')
|
|
|
|
self.grad_scaler.load_state_dict(scaler_state_dict)
|
2022-06-24 10:05:16 +00:00
|
|
|
super().load_state_dict(*args, **kwargs)
|
|
|
|
for group in self.optim.param_groups:
|
|
|
|
for p in group['params']:
|
|
|
|
state = self.optim.state[p]
|
|
|
|
for k, v in state.items():
|
|
|
|
if isinstance(v, Tensor):
|
|
|
|
state[k] = v.to(dtype=self.master_params[p].dtype, device=self.master_params[p].device)
|