python_code
stringlengths
0
456k
import copy import operator from functools import reduce from typing import List from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, ShardingStrategy, TrainCycleItem from colossalai.auto_parallel.tensor_shard.utils import ( enumerate_all_possible_1d_sharding, enumerate_all_possible_2d_sharding, ignore_sharding_exception, ) from .strategy_generator import StrategyGenerator class NormalPoolStrategyGenerator(StrategyGenerator): """ NormalPoolStrategyGenerator is a generic class to generate strategies for pool operation like MaxPoolxd. The reason we call this normal pool is AvgPoolxd and MaxPoolxd are taking the kernel size element from image, and reduce them depening on the operation type. """ def validate(self) -> bool: ''' In sanity check, we need make sure the input data having correct dimension size. For Pool1d, the dim of input data should be 3([N, C, L]). For Pool2d, the dim of input data should be 4([N, C, H, W]). For Pool3d, the dim of input data should be 5([N, C, H, W, D]). ''' input_op_data = self.op_data['input'] assert input_op_data.data.dim() in ( 3, 4, 5), f'We suppose the dim of input fed into Pool op should in range of [3, 5].' def update_compute_cost(self, strategy: ShardingStrategy) -> TrainCycleItem: ''' Compute the computation cost per device with this specific strategy. Note: compute_cost need to be devided by TFLOPS, now it just shows the computation size. ''' # TODO: compute_cost need to be devided by TFLOPS, now it just shows the computation size. # 1D: (Lout) * N * C * kernel # 2D: (H * W) * N * Cout * Cin * kernel # 3D: (H * W * D) * N * Cout * Cin * kernel sharded_output_shape = strategy.sharding_specs[self.op_data['output']].get_sharded_shape_per_device() sharded_input_shape = strategy.sharding_specs[self.op_data['input']].get_sharded_shape_per_device() kernel_size = self.op_data["other"].data if isinstance(kernel_size, int): kernel_size = [kernel_size] * (len(sharded_output_shape) - 2) kernel_size_product = reduce(operator.mul, kernel_size) output_size_product = reduce(operator.mul, sharded_output_shape) input_size_product = reduce(operator.mul, sharded_input_shape) forward_compute_cost = output_size_product * kernel_size_product backward_compute_cost = input_size_product * kernel_size_product total_compute_cost = forward_compute_cost + backward_compute_cost compute_cost = TrainCycleItem(fwd=forward_compute_cost, bwd=backward_compute_cost, total=total_compute_cost) strategy.compute_cost = compute_cost def update_memory_cost(self, strategy: ShardingStrategy) -> ShardingStrategy: forward_size_mapping = { 'input': self._compute_size_in_bytes(strategy, "input"), 'output': self._compute_size_in_bytes(strategy, "output") } backward_size_mapping = copy.deepcopy(forward_size_mapping) backward_size_mapping.pop("output") # compute fwd cost incurred # fwd_cost = input + output fwd_activation_cost = sum([v for k, v in forward_size_mapping.items()]) fwd_mem_cost = MemoryCost(activation=fwd_activation_cost, parameter=0) # compute bwd cost incurred # bwd_cost = input_grad bwd_activation_cost = sum([v for k, v in backward_size_mapping.items()]) bwd_mem_cost = MemoryCost(activation=bwd_activation_cost, parameter=0) # compute total cost total_mem_cost = MemoryCost(activation=fwd_activation_cost + bwd_activation_cost, parameter=0) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) strategy.memory_cost = memory_cost @ignore_sharding_exception def _generate_strategy_with_dim_partition(self, dim_partition): dim_partition_dict_mapping = {"input": dim_partition, "output": dim_partition} sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping) name = f'{sharding_spec_mapping["output"].sharding_sequence} = {sharding_spec_mapping["input"].sharding_sequence}' communication_action_mapping = {} strategy = self.get_sharding_strategy(name=name, sharding_spec_mapping=sharding_spec_mapping, communication_action_mapping=communication_action_mapping) return strategy def enumerate_all_possible_batch_dimensions_dim_partition(self, mesh_dim_0, mesh_dim_1): dim_partition_list = [] dim_partition_list.extend(enumerate_all_possible_1d_sharding(mesh_dim_0, 2)) dim_partition_list.extend(enumerate_all_possible_1d_sharding(mesh_dim_1, 2)) dim_partition_list.extend(enumerate_all_possible_2d_sharding(mesh_dim_0, mesh_dim_1, 2)) # append {} for non_split case dim_partition_list.append({}) return dim_partition_list def collate_strategies(self) -> List[ShardingStrategy]: strategy_list = [] dim_partition_list = self.enumerate_all_possible_batch_dimensions_dim_partition(0, 1) for dim_partition in dim_partition_list: strategy = self._generate_strategy_with_dim_partition(dim_partition) strategy_list.append(strategy) return strategy_list
from typing import Dict, List from torch.fx import Node from colossalai.auto_parallel.tensor_shard.sharding_strategy import ( MemoryCost, OperationData, ShardingStrategy, TrainCycleItem, ) from colossalai.device.device_mesh import DeviceMesh from .strategy_generator import OutputStrategyGenerator __all__ = ['OutputGenerator'] class OutputGenerator(OutputStrategyGenerator): """ OutputGenerator is a generic class to generate strategies for Output Node. """ def __init__(self, operation_data_mapping: Dict[str, OperationData], device_mesh: DeviceMesh, predecessor_nodes: List[Node], output_option: str): super().__init__(operation_data_mapping, device_mesh, predecessor_nodes) self.output_option = output_option def validate(self) -> bool: return super().validate() def update_compute_cost(self, strategy: ShardingStrategy): compute_cost = TrainCycleItem(fwd=10, bwd=10, total=20) strategy.compute_cost = compute_cost def update_memory_cost(self, strategy: ShardingStrategy): ''' Compute the memory cost per device with this specific strategy. ''' fwd_mem_cost = MemoryCost(activation=0, parameter=0) bwd_mem_cost = MemoryCost(activation=0, parameter=0) # compute total cost total_mem_cost = MemoryCost(activation=0, parameter=0) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) strategy.memory_cost = memory_cost def replica_strategy(self) -> List[ShardingStrategy]: """ Generate replica strategy for output node. """ dim_partition_dict_mapping = {} dim_partition_dict_for_output = [] for index, _ in enumerate(self.predecessor_nodes): mapping_name = f"input_{index}" if isinstance(self.op_data[mapping_name].data, (tuple, list)): dim_partition_dict_for_input = [{} for _ in range(len(self.op_data[mapping_name].data))] else: dim_partition_dict_for_input = {} dim_partition_dict_mapping[mapping_name] = dim_partition_dict_for_input dim_partition_dict_for_output.append(dim_partition_dict_for_input) if len(dim_partition_dict_for_output) == 1: dim_partition_dict_for_output = dim_partition_dict_for_output[0] else: dim_partition_dict_for_output = tuple(dim_partition_dict_for_output) dim_partition_dict_mapping['output'] = dim_partition_dict_for_output communication_action_mapping = {} sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping) name = 'Replica Output' strategy = self.get_sharding_strategy(name=name, sharding_spec_mapping=sharding_spec_mapping, communication_action_mapping=communication_action_mapping) return strategy def distributed_strategy(self, mesh_list: List[List[int]] = None) -> List[ShardingStrategy]: """ Generate distributed strategy for output node. """ # TODO: need to take care of the case when the first element of output only need to be sharded. output_op_data = self.op_data['output'] if isinstance(output_op_data.data, tuple): length = len(output_op_data.data) dim_partition_dict_mapping = { "output": [{ 0: mesh_list }] * length, } else: dim_partition_dict_mapping = { "output": { 0: mesh_list }, } for index, _ in enumerate(self.predecessor_nodes): mapping_name = f"input_{index}" dim_partition_dict_mapping[mapping_name] = {0: mesh_list} communication_action_mapping = {} sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping) name = 'Distributed Output' strategy = self.get_sharding_strategy(name=name, sharding_spec_mapping=sharding_spec_mapping, communication_action_mapping=communication_action_mapping) return strategy def collate_strategies(self) -> List[ShardingStrategy]: strategy_list = [] mesh_list = [0, 1] if self.output_option == 'replicated': strategy_list.append(self.replica_strategy()) elif self.output_option == 'distributed': strategy_list.append(self.distributed_strategy(mesh_list)) return strategy_list
import operator from functools import reduce from typing import List import torch from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, ShardingStrategy, TrainCycleItem from colossalai.auto_parallel.tensor_shard.utils import ( enumerate_all_possible_1d_sharding, enumerate_all_possible_2d_sharding, ignore_sharding_exception, ) from colossalai.tensor.sharding_spec import ShardingSpecException from .strategy_generator import StrategyGenerator __all__ = ['BinaryElementwiseStrategyGenerator'] class BinaryElementwiseStrategyGenerator(StrategyGenerator): """ An BinaryElementwiseStrategyGenerator is a node handler which deals with elementwise operations which have two operands and broadcasting occurs such as torch.add. The logical shape for this operation will be `input <op> other`. """ def validate(self) -> bool: assert len(self.op_data) == 3, \ f'BinaryElementwiseStrategyGenerator only accepts three operation data (input, other and output), but got {len(self.op_data)}' for name, op_data in self.op_data.items(): if not isinstance(op_data.data, (torch.Tensor, int, float)): raise TypeError(f'The operation data {name} is not a torch.Tensor/int/float.') def update_compute_cost(self, strategy: ShardingStrategy) -> ShardingStrategy: shape = strategy.sharding_specs[self.op_data['input']].get_sharded_shape_per_device() # since elementwise ops are not compute-intensive, # we approximate the backward compute cost # to be twice the fwd compute cost fwd_compute_cost = reduce(operator.mul, shape) bwd_compute_cost = fwd_compute_cost * 2 compute_cost = TrainCycleItem(fwd=fwd_compute_cost, bwd=bwd_compute_cost, total=fwd_compute_cost + bwd_compute_cost) strategy.compute_cost = compute_cost def update_memory_cost(self, strategy: ShardingStrategy) -> ShardingStrategy: # all input, output and outputs have the same shape shape = strategy.sharding_specs[self.op_data['input']].get_sharded_shape_per_device() # compute fwd memory cost in bytes # as the elementwise ops are not memory-intensive # we approximate the fwd memroy cost to be the output # and the backward memory cost to be grad of input and other input_bytes = self._compute_size_in_bytes(strategy, 'input') other_bytes = self._compute_size_in_bytes(strategy, 'other') output_bytes = self._compute_size_in_bytes(strategy, 'output') fwd_memory_cost = MemoryCost(activation=output_bytes) bwd_memory_cost = MemoryCost(activation=input_bytes + other_bytes) total_memory_cost = MemoryCost(activation=input_bytes + other_bytes + output_bytes) memory_cost = TrainCycleItem(fwd=fwd_memory_cost, bwd=bwd_memory_cost, total=total_memory_cost) strategy.memory_cost = memory_cost @ignore_sharding_exception def enumerate_all_possible_output(self, mesh_dim_0, mesh_dim_1): # we check for the output logical shape to get the number of dimensions dim_partition_list = [] dim_size = len(self.op_data['output'].logical_shape) # enumerate all the 2D sharding cases sharding_list_2d = enumerate_all_possible_2d_sharding(mesh_dim_0, mesh_dim_1, dim_size) dim_partition_list.extend(sharding_list_2d) # enumerate all the 1D sharding cases sharding_list_1d_on_dim_0 = enumerate_all_possible_1d_sharding(mesh_dim_0, dim_size) dim_partition_list.extend(sharding_list_1d_on_dim_0) sharding_list_1d_on_dim_1 = enumerate_all_possible_1d_sharding(mesh_dim_1, dim_size) dim_partition_list.extend(sharding_list_1d_on_dim_1) # add empty dict for fully replicated case dim_partition_list.append({}) # sharding strategy bookkeeping strategy_list = [] # convert these dim partition dict to sharding strategy for dim_partition_dict in dim_partition_list: dim_partition_dict_mapping = dict(input=dim_partition_dict, other=dim_partition_dict, output=dim_partition_dict) try: sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping) communication_action_mapping = {} # get name sharding_seq = sharding_spec_mapping['input'].sharding_sequence name = f'{sharding_seq} = {sharding_seq} <binary-elementwise-op> {sharding_seq}' sharding_strategy = self.get_sharding_strategy( name=name, sharding_spec_mapping=sharding_spec_mapping, communication_action_mapping=communication_action_mapping) strategy_list.append(sharding_strategy) except ShardingSpecException: continue return strategy_list def collate_strategies(self) -> List[ShardingStrategy]: strategy_list = self.enumerate_all_possible_output(0, 1) return strategy_list
import copy from typing import List from colossalai.auto_parallel.tensor_shard.sharding_strategy import ( CommAction, CommType, MemoryCost, ShardingStrategy, TrainCycleItem, ) from colossalai.tensor.shape_consistency import CollectiveCommPattern from colossalai.tensor.sharding_spec import ShardingSpec from .strategy_generator import StrategyGenerator __all__ = ['TensorConstructorGenerator'] class TensorConstructorGenerator(StrategyGenerator): """ TensorConstructorGenerator which deals with the sharding strategies for tensor constructor operation, such as torch.arange. """ def validate(self) -> bool: return super().validate() def update_compute_cost(self, strategy: ShardingStrategy): compute_cost = TrainCycleItem(fwd=10, bwd=10, total=20) strategy.compute_cost = compute_cost def update_memory_cost(self, strategy: ShardingStrategy): ''' Compute the memory cost per device with this specific strategy. ''' forward_size_mapping = {'output': self._compute_size_in_bytes(strategy, "output")} # compute fwd cost incurred # fwd_cost = input + output fwd_activation_cost = sum([v for k, v in forward_size_mapping.items() if not self.is_param(k)]) fwd_parameter_cost = sum([v for k, v in forward_size_mapping.items() if self.is_param(k)]) fwd_mem_cost = MemoryCost(activation=fwd_activation_cost, parameter=fwd_parameter_cost) # compute bwd cost incurred bwd_mem_cost = MemoryCost(activation=0, parameter=0) # compute total cost total_mem_cost = MemoryCost(activation=fwd_activation_cost, parameter=fwd_parameter_cost) memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost) strategy.memory_cost = memory_cost def collate_strategies(self) -> List[ShardingStrategy]: strategy_list = [] dim_partition_dict_mapping = { "output": {}, } communication_action_mapping = {} sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping) name = 'Replica Tensor Constructor' strategy = self.get_sharding_strategy(name=name, sharding_spec_mapping=sharding_spec_mapping, communication_action_mapping=communication_action_mapping) strategy_list.append(strategy) return strategy_list
""" This file will not be automatically imported by `colossalai.testing` as this file has a dependency on `pytest`. Therefore, you need to explicitly import this file `from colossalai.testing.pytest_wrapper import <func>`.from """ import pytest import os def run_on_environment_flag(name: str): """ Conditionally run a test based on the environment variable. If this environment variable is set to 1, this test will be executed. Otherwise, this test is skipped. The environment variable is default to 0. Args: name (str): the name of the environment variable flag. Usage: # in your pytest file @run_on_environment_flag(name='SOME_FLAG') def test_for_something(): do_something() # in your terminal # this will execute your test SOME_FLAG=1 pytest test_for_something.py # this will skip your test pytest test_for_something.py """ assert isinstance(name, str) flag = os.environ.get(name.upper(), '0') reason = f'Environment varialbe {name} is {flag}' if flag == '1': return pytest.mark.skipif(False, reason=reason) else: return pytest.mark.skipif(True, reason=reason)
from .comparison import assert_equal, assert_not_equal, assert_close, assert_close_loose, assert_equal_in_group from .utils import parameterize, rerun_on_exception, rerun_if_address_is_in_use, skip_if_not_enough_gpus __all__ = [ 'assert_equal', 'assert_not_equal', 'assert_close', 'assert_close_loose', 'assert_equal_in_group', 'parameterize', 'rerun_on_exception', 'rerun_if_address_is_in_use', 'skip_if_not_enough_gpus' ]
import random import numpy as np import torch def seed_all(seed, cuda_deterministic=False): random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) if torch.cuda.is_available(): torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) if cuda_deterministic: # slower, more reproducible torch.backends.cudnn.deterministic = True torch.backends.cudnn.benchmark = False else: torch.backends.cudnn.deterministic = False torch.backends.cudnn.benchmark = True
import torch import torch.distributed as dist from torch import Tensor from torch.distributed import ProcessGroup from torch.testing import assert_close def assert_equal(a: Tensor, b: Tensor): assert torch.all(a == b), f'expected a and b to be equal but they are not, {a} vs {b}' def assert_not_equal(a: Tensor, b: Tensor): assert not torch.all(a == b), f'expected a and b to be not equal but they are, {a} vs {b}' def assert_close_loose(a: Tensor, b: Tensor, rtol: float = 1e-3, atol: float = 1e-3): assert_close(a, b, rtol=rtol, atol=atol) def assert_equal_in_group(tensor: Tensor, process_group: ProcessGroup = None): # all gather tensors from different ranks world_size = dist.get_world_size(process_group) tensor_list = [torch.empty_like(tensor) for _ in range(world_size)] dist.all_gather(tensor_list, tensor, group=process_group) # check if they are equal one by one for i in range(world_size - 1): a = tensor_list[i] b = tensor_list[i + 1] assert torch.all(a == b), f'expected tensors on rank {i} and {i + 1} to be equal but they are not, {a} vs {b}'
import re import torch from typing import Callable, List, Any from functools import partial from inspect import signature from packaging import version def parameterize(argument: str, values: List[Any]) -> Callable: """ This function is to simulate the same behavior as pytest.mark.parameterize. As we want to avoid the number of distributed network initialization, we need to have this extra decorator on the function launched by torch.multiprocessing. If a function is wrapped with this wrapper, non-paramterized arguments must be keyword arguments, positioanl arguments are not allowed. Usgae:: # Example 1: @parameterize('person', ['xavier', 'davis']) def say_something(person, msg): print(f'{person}: {msg}') say_something(msg='hello') # This will generate output: # > xavier: hello # > davis: hello # Exampel 2: @parameterize('person', ['xavier', 'davis']) @parameterize('msg', ['hello', 'bye', 'stop']) def say_something(person, msg): print(f'{person}: {msg}') say_something() # This will generate output: # > xavier: hello # > xavier: bye # > xavier: stop # > davis: hello # > davis: bye # > davis: stop Args: argument (str): the name of the argument to parameterize values (List[Any]): a list of values to iterate for this argument """ def _wrapper(func): def _execute_function_by_param(**kwargs): for val in values: arg_map = {argument: val} partial_func = partial(func, **arg_map) partial_func(**kwargs) return _execute_function_by_param return _wrapper def rerun_on_exception(exception_type: Exception = Exception, pattern: str = None, max_try: int = 5) -> Callable: """ A decorator on a function to re-run when an exception occurs. Usage:: # rerun for all kinds of exception @rerun_on_exception() def test_method(): print('hey') raise RuntimeError('Address already in use') # rerun for RuntimeError only @rerun_on_exception(exception_type=RuntimeError) def test_method(): print('hey') raise RuntimeError('Address already in use') # rerun for maximum 10 times if Runtime error occurs @rerun_on_exception(exception_type=RuntimeError, max_try=10) def test_method(): print('hey') raise RuntimeError('Address already in use') # rerun for infinite times if Runtime error occurs @rerun_on_exception(exception_type=RuntimeError, max_try=None) def test_method(): print('hey') raise RuntimeError('Address already in use') # rerun only the exception message is matched with pattern # for infinite times if Runtime error occurs @rerun_on_exception(exception_type=RuntimeError, pattern="^Address.*$") def test_method(): print('hey') raise RuntimeError('Address already in use') Args: exception_type (Exception, Optional): The type of exception to detect for rerun pattern (str, Optional): The pattern to match the exception message. If the pattern is not None and matches the exception message, the exception will be detected for rerun max_try (int, Optional): Maximum reruns for this function. The default value is 5. If max_try is None, it will rerun foreven if exception keeps occurings """ def _match_lines(lines, pattern): for line in lines: if re.match(pattern, line): return True return False def _wrapper(func): def _run_until_success(*args, **kwargs): try_count = 0 assert max_try is None or isinstance(max_try, int), \ f'Expected max_try to be None or int, but got {type(max_try)}' while max_try is None or try_count < max_try: try: try_count += 1 ret = func(*args, **kwargs) return ret except exception_type as e: error_lines = str(e).split('\n') if try_count < max_try and (pattern is None or _match_lines(error_lines, pattern)): print('Exception is caught, retrying...') # when pattern is not specified, we always skip the exception # when pattern is specified, we only skip when pattern is matched continue else: print('Maximum number of attempts is reached or pattern is not matched, no more retrying...') raise e # Override signature # otherwise pytest.mark.parameterize will raise the following error: # function does not use argumetn xxx sig = signature(func) _run_until_success.__signature__ = sig return _run_until_success return _wrapper def rerun_if_address_is_in_use(): """ This function reruns a wrapped function if "address already in use" occurs in testing spawned with torch.multiprocessing Usage:: @rerun_if_address_is_in_use() def test_something(): ... """ # check version torch_version = version.parse(torch.__version__) assert torch_version.major == 1 # only torch >= 1.8 has ProcessRaisedException if torch_version.minor >= 8: exception = torch.multiprocessing.ProcessRaisedException else: exception = Exception func_wrapper = rerun_on_exception(exception_type=exception, pattern=".*Address already in use.*") return func_wrapper def skip_if_not_enough_gpus(min_gpus: int): """ This function is used to check the number of available GPUs on the system and automatically skip the test cases which require more GPUs. Note: The wrapped function must have `world_size` in its keyword argument. Usage: @skip_if_not_enough_gpus(min_gpus=8) def test_something(): # will be skipped if there are fewer than 8 GPUs available do_something() Arg: min_gpus (int): the minimum number of GPUs required to run this test. """ def _wrap_func(f): def _execute_by_gpu_num(*args, **kwargs): num_avail_gpu = torch.cuda.device_count() if num_avail_gpu >= min_gpus: f(*args, **kwargs) return _execute_by_gpu_num return _wrap_func
from typing import Tuple import torch import torch.nn as nn from colossalai.logging import get_dist_logger from colossalai.zero.sharded_model.sharded_model_v2 import ShardedModelV2 from colossalai.zero.sharded_optim import LowLevelZeroOptimizer, ShardedOptimizerV2 from ..nn.optimizer.zero_optimizer import ZeroOptimizer def convert_to_zero_v2(model: nn.Module, optimizer: torch.optim.Optimizer, model_config, optimizer_config) -> Tuple[ShardedModelV2, ShardedOptimizerV2]: """ A helper function to integrate the model and optimizer with ZeRO optimizer and off-loading :param model: Your model object :type model: :class:`torch.nn.Module` :param optimizer_config: Your optimizer object :type optimizer_config: :class:`dict` :return: (model, optimizer) :rtype: Tuple """ logger = get_dist_logger('convert_to_zero_v2') logger.info(f'optimizer_config is {optimizer_config}', ranks=[0]) if optimizer_config is None: optimizer_config = dict() logger.info(f'model_config is {model_config}', ranks=[0]) if model_config is None: model_config = dict() zero_model = ShardedModelV2(model, **model_config) zero_optimizer = ShardedOptimizerV2(zero_model, optimizer, **optimizer_config) return zero_model, zero_optimizer __all__ = ['convert_to_zero_v2', 'LowLevelZeroOptimizer', 'ShardedModelV2', 'ShardedOptimizerV2', 'ZeroOptimizer']
from .init_context import ZeroInitContext, no_shard_zero_context, no_shard_zero_decrator __all__ = ['ZeroInitContext', 'no_shard_zero_context', 'no_shard_zero_decrator']
import contextlib import functools from typing import Optional from contextlib import AbstractContextManager import torch import torch.nn as nn import torch.distributed as dist from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.context.singleton_meta import SingletonMeta from colossalai.logging import get_dist_logger from colossalai.zero.shard_utils import BaseShardStrategy from colossalai.zero.sharded_model._utils import cast_tensor_to_fp16 from colossalai.zero.sharded_model.sharded_model_v2 import ShardedModelV2 from colossalai.zero.sharded_param import ShardedParamV2 from colossalai.utils.model.utils import InsertPostInitMethodToModuleSubClasses class ZeroContextConfig(object): """The configuration used to control zero context initialization. Args: target_device (torch.device): The device where param data are after exiting the context. replicated (bool, optional): Whether the param is replicated across data parallel group. Some parameters are not replicated, e.g. parameters in MOE experts. shard_param (bool, optional): Is param sharded after exiting the context. Defaults to False. """ def __init__(self, target_device: torch.device, replicated: bool = True, shard_param: bool = False): super().__init__() if shard_param: assert replicated, "Non-replicated parameters can't be sharded." # replicated no-shard parameters should locate in cuda, since we will broadcast them soon if replicated and not shard_param: assert target_device.type == 'cuda', "Replicated no-shard paramters should locate in cuda." self.target_device = target_device self.is_replicated: bool = replicated self.shard_param: bool = shard_param class ZeroInitContext(InsertPostInitMethodToModuleSubClasses): """A context to initialize model. 1. Convert the model to fp16. 2. The paramaters of the module are adapted to type ShardedParameter. 3. Shard the param and grad according to flags. Args: target_device (torch.device): The device where param data are after exiting the context. shard_strategy (BaseShardStrategy): Shard strategy instance. seed (int, optional): Random seed for weight initialization shard_param (bool, optional): Is param sharded after exiting the context. Defaults to False. default_dtype (torch.dtype, optional): If it's not None, parameters will be initialized as ``default_dtype`` then converted to fp16. model_numel_tensor (torch.Tensor, optional): A tensor which will store the number of elements of model. Defaults to torch.zeros(1, dtype=torch.int). """ def __init__(self, target_device: torch.device, shard_strategy: BaseShardStrategy, seed: int = 2**10 - 1, shard_param: bool = False, default_dtype: Optional[torch.dtype] = None, model_numel_tensor: torch.Tensor = torch.zeros(1, dtype=torch.long)): super().__init__(default_dtype=default_dtype) self.shard_strategy = shard_strategy self.param_list = [] self.model_numel_tensor = model_numel_tensor self.seed = seed self.dp_process_group = gpc.get_group(ParallelMode.DATA) self.config = ZeroContextConfig(target_device=target_device, replicated=True, shard_param=shard_param) ZeroContextMgr().current_context = self self.param_numel = {} self.top_module = None @property def target_device(self): return self.config.target_device @property def is_replicated(self): return self.config.is_replicated @property def shard_param(self): return self.config.shard_param @staticmethod def calc_fanin_fanout(tensor: torch.Tensor): """We use this function to substitute fan-in and fan-out calculation in torch.nn.init. This can help us get correct fan-in and fan-out for sharded tensor. """ assert isinstance(tensor, nn.Parameter), "Sharded tensor initilization is only allowed for paramters" # get correct shape of input tensor if not hasattr(tensor, 'colo_attr') or not tensor.colo_attr.param_is_sharded: tensor_shape = tensor.shape else: tensor_shape = tensor.colo_attr.sharded_data_tensor.origin_shape dimensions = len(tensor_shape) if dimensions < 2: raise ValueError("Fan in and fan out can not be computed for tensor with fewer than 2 dimensions") num_input_fmaps = tensor_shape[1] num_output_fmaps = tensor_shape[0] receptive_field_size = 1 if dimensions > 2: # math.prod is not always available, accumulate the product manually # we could use functools.reduce but that is not supported by TorchScript for s in tensor_shape[2:]: receptive_field_size *= s fan_in = num_input_fmaps * receptive_field_size fan_out = num_output_fmaps * receptive_field_size return fan_in, fan_out def _pre_context_exec(self): """ The Callback function when entering the context """ self.logger = get_dist_logger("ZeroInitContext") # substitute fan-in and fan-out calculation self.nn_fanin_fanout = nn.init._calculate_fan_in_and_fan_out nn.init._calculate_fan_in_and_fan_out = self.calc_fanin_fanout self.module_load_from_state_dict = nn.Module._load_from_state_dict shard_strategy = self.shard_strategy if self.config.shard_param else None nn.Module._load_from_state_dict = functools.partialmethod(ShardedModelV2._colo_load_from_state_dict, shard_strategy=shard_strategy) self.module_state_dict = nn.Module.state_dict nn.Module.state_dict = functools.partialmethod(ShardedModelV2._colo_state_dict, shard_strategy=shard_strategy, state_dict_func=self.module_state_dict, process_group=self.dp_process_group) # reserve rng states self.cpu_rng_state = torch.get_rng_state() self.cuda_rng_state = torch.cuda.get_rng_state() # set new seed for initialization, since we initialize sharded tensor separately # we don't want all processes have the same seed # otherwise all sharded tensors are same after init offset = self.seed + 1 # we want to have more 1 in binary format seed torch.manual_seed(self.seed + offset * dist.get_rank()) def _post_context_exec(self): """The callback function when exiting context. """ # broadcast replicated no-shard parameters src_rank = gpc.get_ranks_in_group(ParallelMode.DATA)[0] for param in self.param_list: assert hasattr(param, 'colo_attr') if not param.colo_attr.param_is_sharded and param.colo_attr.is_replicated: dist.broadcast(tensor=param.data, src=src_rank, group=self.dp_process_group) param.colo_attr.set_data_none() del self.param_list nn.init._calculate_fan_in_and_fan_out = self.nn_fanin_fanout nn.Module.load_state_dict = self.module_load_from_state_dict nn.Module.state_dict = self.module_state_dict torch.set_rng_state(self.cpu_rng_state) torch.cuda.set_rng_state(self.cuda_rng_state) params = frozenset(self.top_module.parameters()) for param in self.param_numel.keys(): if param not in params: self.param_numel[param] = 0 self.model_numel_tensor.fill_(sum(self.param_numel.values())) def _post_init_method(self, module: torch.nn.Module, *args, **kwargs): """ The function to call at the end of the constructor of each module. NOTE() The module may be passed to this function multiple times. """ self.top_module = module def half_fn(t: torch.Tensor): return t.half() if t.is_floating_point() else t for param in module.parameters(recurse=False): # avoid adapting a param to ShardedParam twice if hasattr(param, 'colo_attr'): continue self.param_numel[param] = param.numel() # convert parameters to half param_half = half_fn(param) param.data = param_half if param.grad is not None: grad_half = half_fn(param.grad) param.grad.data = grad_half # move torch parameters to the target device target_device = self.target_device param.data = param.data.to(target_device) if param.grad is not None: param.grad = param.grad.to(target_device) param.colo_attr = ShardedParamV2(param, set_data_none=True) if self.shard_param: self.shard_strategy.shard([param.colo_attr.sharded_data_tensor], self.dp_process_group) param.data = param.colo_attr.data_payload # set param.data to payload # mark whether the param is replicated param.colo_attr.is_replicated = self.is_replicated # mark whether the param should keep not sharded # if True, the param is used as Zero stage 2 param.colo_attr.keep_not_shard = not self.shard_param self.param_list.append(param) # We must cast buffers # If we use BN, buffers may be on CPU and Float # We must cast them for buffer in module.buffers(recurse=False): buffer.data = buffer.data.to(device=torch.cuda.current_device()) buffer.data = cast_tensor_to_fp16(buffer.data) class ZeroContextMgr(metaclass=SingletonMeta): current_context: Optional[ZeroInitContext] = None @contextlib.contextmanager def hijack_context_config(self, **kwargs): if self.current_context is None: yield else: old_config = self.current_context.config self.current_context.config = ZeroContextConfig(**kwargs) yield self.current_context.config = old_config def no_shard_zero_context(is_replicated: bool = True) -> AbstractContextManager: return ZeroContextMgr().hijack_context_config(target_device=torch.device('cuda', torch.cuda.current_device()), replicated=is_replicated, shard_param=False) def no_shard_zero_decrator(is_replicated: bool = True): def _wrapper(init_func): def _no_shard(*args, **kwargs): with no_shard_zero_context(is_replicated): ret = init_func(*args, **kwargs) return ret return _no_shard return _wrapper
from functools import partial from typing import Optional import torch import torch.distributed as dist from torch.optim import Optimizer from colossalai.amp.naive_amp.grad_scaler import DynamicGradScaler from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.logging import get_dist_logger from colossalai.nn.optimizer import ColossalaiOptimizer from colossalai.tensor import ColoParameter, ProcessGroup from colossalai.utils.cuda import get_current_device from ._utils import ( calculate_global_norm_from_list, compute_norm, flatten, has_inf_or_nan, reduce_tensor_dp_group, release_param_grad, split_half_float_double, sync_param, ) from .bookkeeping import BucketStore, GradientStore, ParameterStore, TensorBucket class LowLevelZeroOptimizer(ColossalaiOptimizer): """Optimizer used for ZeRO-1 and ZeRO-2. """ def __init__( self, optimizer: Optimizer, initial_scale: int = 2**16, # grad scaler config min_scale: int = 1, growth_factor: float = 2., backoff_factor: float = .5, growth_interval: int = 2000, hysteresis: int = 2, max_scale: int = 2**24, clip_grad_norm: float = 0.0, # grad clipping verbose: bool = False, reduce_bucket_size: int = 1024 * 1024, # communication communication_dtype: Optional[torch.dtype] = None, overlap_communication: bool = False, partition_grad: bool = False, # stage 2 flag cpu_offload: bool = False, # cpu offload forced_dtype: Optional[torch.dtype] = None): # TODO: add support for # 1. fp16 master weights # 2. contiguous gradients # 3. cpu offload # 4. support when some parameters requires_grad = False super(LowLevelZeroOptimizer, self).__init__(optim=optimizer) self._dtype = self.optim.param_groups[0]['params'][0].dtype self._logger = get_dist_logger() self._verbose = verbose # stage 2 self._partition_grads = partition_grad self._cpu_offload = cpu_offload colo_pg = self._search_colo_process_group() if isinstance(colo_pg, ProcessGroup): self._local_rank = colo_pg.dp_local_rank() self._world_size = colo_pg.dp_world_size() self._dp_global_ranks = colo_pg.get_ranks_in_dp() self._dp_torch_group = colo_pg.dp_process_group() self._mp_torch_group = None if colo_pg.tp_world_size() > 1: self._mp_torch_group = colo_pg.tp_process_group() elif colo_pg is None: dp_parallel_mode = ParallelMode.DATA mp_parallel_mode = ParallelMode.MODEL self._dp_parallel_mode = dp_parallel_mode self._mp_parallel_mode = mp_parallel_mode self._local_rank = gpc.get_local_rank(dp_parallel_mode) self._world_size = gpc.get_world_size(dp_parallel_mode) self._dp_global_ranks = gpc.get_ranks_in_group(dp_parallel_mode) self._dp_torch_group = gpc.get_group(dp_parallel_mode) self._mp_torch_group = None if gpc.is_initialized(mp_parallel_mode) and gpc.get_world_size(mp_parallel_mode) > 1: self._mp_torch_group = gpc.get_group(mp_parallel_mode) else: raise NotImplementedError # fp16 and fp32 params for mixed precision training self._fp16_param_groups = dict() self._fp32_flat_param_groups_of_current_rank = dict() # communication params self._overlap_communication = overlap_communication self._reduce_bucket_size = reduce_bucket_size self._communication_dtype = communication_dtype # gradient 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, verbose=verbose) self._found_overflow = torch.FloatTensor([0]).to(get_current_device()) # gradient clipping self._clip_grad_norm = clip_grad_norm if forced_dtype: for group in self.optim.param_groups: group_params = group['params'] for param in group_params: param.data = param.data.to(forced_dtype) self._dtype = forced_dtype # check argument conflict self._sanity_checks() # ParameterStore will manage the tensor buffers used for zero # it will not manage the tensors used by mixed precision training self._param_store = ParameterStore(self._dp_torch_group) self._grad_store = GradientStore(self._dp_torch_group) self._bucket_store = BucketStore(self._dp_torch_group) # iterate over the param group in the optimizer # partition these param groups for data parallel training # and add buffers to parameter store for future access for group_id, param_group in enumerate(self.optim.param_groups): group_params = list() for param in param_group['params']: if param.requires_grad: group_params.append(param) # add the fp16 params to fp16_param_groups for bookkeeping self._fp16_param_groups[group_id] = group_params # assign parameters to ranks # the params in the list are sorted params_per_rank = self._partition_param_list(group_params) # store the mapping between param to rank # each param should belong to only one rank for rank, params in enumerate(params_per_rank): self._param_store.add_fp16_param_list_by_rank_group(rank, group_id, params) for param in params: self._param_store.set_param_to_rank(param, rank) # move to cpu to make room to create the flat tensor # move_tensor(params, device='cpu') for param in group_params: param.data = param.data.cpu() # flatten the reordered tensors for rank in range(self._world_size): tensor_list = self._param_store.get_fp16_params_by_rank_group(rank, group_id) with torch.no_grad(): flat_tensor = flatten(tensor_list) flat_tensor = flat_tensor.data.cuda() self._param_store.add_flat_fp16_param_by_rank_group(rank, group_id, flat_tensor) # sync parameters for rank in range(self._world_size): flat_tensor = self._param_store.get_flat_fp16_param_by_rank_group(rank, group_id) tensor_list = self._param_store.get_fp16_params_by_rank_group(rank, group_id) sync_param(flat_tensor=flat_tensor, tensor_list=tensor_list) # create a copy of fp32 weights of the parameters for which this rank is responsible fp16_flat_current_rank = self._param_store.get_flat_fp16_param_by_rank_group(self._local_rank, group_id) fp32_flat_current_rank = fp16_flat_current_rank.float() device = 'cpu' if self._cpu_offload else get_current_device() fp32_flat_current_rank = fp32_flat_current_rank.to(device) fp32_flat_current_rank.requires_grad = True self._fp32_flat_param_groups_of_current_rank[group_id] = fp32_flat_current_rank # need to replace the params in the `params` field in the optimizer # so that when the optimizer calls step(), it only updates the tensors # managed by this data parallel rank param_group['params'] = [fp32_flat_current_rank] # set reduction state for param in self._fp16_param_groups[group_id]: self._param_store.set_param_reduction_state(param, False) # intialize communication stream for # communication-compuation overlapping if self._overlap_communication: self._comm_stream = torch.cuda.Stream() # reduction hook is only used if overlapping communication # or stage 2 is used # if it is stage 1 without overlapping, no hook will be attached if self._overlap_communication or self._partition_grads: self._attach_reduction_hook() @property def dtype(self): return self._dtype @property def loss_scale(self): return self.grad_scaler.scale @property def num_param_groups(self): return len(self._fp16_param_groups) def _sanity_checks(self): assert torch.cuda.is_available(), 'CUDA is required' for param_group in self.optim.param_groups: group_params = param_group['params'] for param in group_params: assert param.dtype == self._dtype, \ f"Parameters are expected to have the same dtype `{self._dtype}`, but got `{param.dtype}`" def _search_colo_process_group(self): colo_flag = False colo_pg = None for param_group in self.optim.param_groups: group_params = param_group['params'] for param in group_params: if isinstance(param, ColoParameter): colo_flag = True if colo_pg is None: colo_pg = param.get_process_group() else: assert colo_pg == param.get_process_group(), "All parameters should be in a same process group" elif colo_flag: raise RuntimeError("All parameters should be ColoParameter if you use ColoParameter.") return colo_pg def _partition_param_list(self, param_list): params_per_rank = [[] for _ in range(self._world_size)] numel_per_rank = [0 for _ in range(self._world_size)] # partititon the parameters in a greedy fashion sorted_params = sorted(param_list, key=lambda x: x.numel(), reverse=True) for param in sorted_params: # allocate this parameter to the rank with # the smallest numel for load balancing purpose rank_to_go = numel_per_rank.index(min(numel_per_rank)) params_per_rank[rank_to_go].append(param) numel_per_rank[rank_to_go] += param.numel() if self._verbose: self._logger.info(f'Number of elements on ranks: {numel_per_rank}', ranks=[0]) return params_per_rank ########################### # Backward Reduction Hook # ########################### def _grad_handler(self, param, grad, reduce_rank): self._add_to_reduction_bucket(param, reduce_rank) return grad def _attach_reduction_hook(self): # we iterate over the fp16 params # on each param, we register a hook to its AccumulateGrad object for group_id in range(self.num_param_groups): param_group = self._fp16_param_groups[group_id] for param in param_group: if param.requires_grad: # determines the reduction destionation rank # this is only valid for stage 2 # dst_rank = None means using all-reduce # else using reduce if self._partition_grads: reduce_rank = self._param_store.get_param_rank(param) else: reduce_rank = None param.register_hook(partial(self._grad_handler, param, reduce_rank=reduce_rank)) def _reduce_tensor_bucket(self, bucket: TensorBucket, reduce_rank): if self._overlap_communication: torch.cuda.synchronize() self._param_store.clear_grads_of_previous_reduced_params() stream = self._comm_stream else: stream = torch.cuda.current_stream() with torch.cuda.stream(stream): flat = bucket.flatten() reduce_global_rank = None if reduce_rank is not None: reduce_global_rank = self._dp_global_ranks[reduce_rank] reduced_flat = reduce_tensor_dp_group(tensor=flat, dtype=self._communication_dtype, dst_local_rank=reduce_rank, dst_global_rank=reduce_global_rank, group=self._dp_torch_group) # update the reduced tensor if reduce_rank is None or reduce_rank == self._local_rank: bucket.unflatten_and_copy(reduced_flat) def _reduce_tensor_list_with_one_dtype(self, tensor_list, bucket_size, reduce_rank): param_bucket = TensorBucket(size=bucket_size) for tensor in tensor_list: param_bucket.add_to_bucket(tensor, allow_oversize=True) if param_bucket.is_full_or_oversized(): self._reduce_tensor_bucket(bucket=param_bucket, reduce_rank=reduce_rank) param_bucket.empty() if not param_bucket.is_empty(): self._reduce_tensor_bucket(bucket=param_bucket, reduce_rank=reduce_rank) def _reduce_grads(self, reduce_rank, grads, bucket_size): grad_buckets_by_dtype = split_half_float_double(grads) for tensor_list in grad_buckets_by_dtype: self._reduce_tensor_list_with_one_dtype(tensor_list=tensor_list, bucket_size=bucket_size, reduce_rank=reduce_rank) ####################### # Reduction Functions # ####################### def _run_reduction(self, reduce_rank=None): # reduce grads self._reduce_grads(reduce_rank=reduce_rank, grads=self._bucket_store.get_grad(reduce_rank=reduce_rank), bucket_size=self._bucket_store.num_elements_in_bucket(reduce_rank)) # use communication stream if overlapping # communication with computation if self._overlap_communication: stream = self._comm_stream else: stream = torch.cuda.current_stream() with torch.cuda.stream(stream): params_in_bucket = self._bucket_store.get_param(reduce_rank=reduce_rank) for param in params_in_bucket: # the is_param_reduced flag should be False showing that # this param is not reduced before calling self._reduce_grads_by_rank is_param_reduced = self._param_store.is_param_reduced(param) if is_param_reduced: msg = f'Parameter of size ({param.size()}) has been reduced, ' + \ 'duplicate reduction will lead to arithmetic incorrectness' raise RuntimeError(msg) # update the flag self._param_store.set_param_reduction_state(param, True) # if partition grads = True # we do not keep the gradient after reduction if self._partition_grads and not self._param_store.belongs_to_current_rank(param): if self._overlap_communication: # we need to keep this gradient for now as reduction may # be completed yet since it is using a different cuda stream self._param_store.add_previous_reduced_param(param) else: param.grad = None self._bucket_store.reset_by_rank(reduce_rank) def _add_to_reduction_bucket(self, param, reduce_rank=None): param_size = param.numel() # check if the bucket is full # if full, will reduce the grads already in the bucket # after reduction, the bucket will be empty if self._bucket_store.num_elements_in_bucket(reduce_rank) + param_size > self._reduce_bucket_size: self._run_reduction(reduce_rank) # the param must not be reduced to ensure correctness is_param_reduced = self._param_store.is_param_reduced(param) if is_param_reduced: msg = f'Parameter of size ({param.size()}) has already been reduced, ' \ + 'duplicate reduction will lead to arithmetic incorrectness' raise RuntimeError(msg) self._bucket_store.add_num_elements_in_bucket(param_size, reduce_rank) self._bucket_store.add_param(param, reduce_rank) ################################ # torch.optim.Optimizer methods ################################ def backward(self, loss, retain_graph=False, sync_grad=True): loss = self.loss_scale * loss loss.backward(retain_graph=retain_graph) # finish gradient reduction if not self._partition_grads: self._reduce_grad_stage1() else: # TODO: support async comm in reduce self._reduce_grad_stage2() # clear reduced grads if self._overlap_communication: torch.cuda.synchronize() self._param_store.clear_grads_of_previous_reduced_params() # gradient synchronization if sync_grad: self._sync_grad() def zero_grad(self, set_to_none=True): """ Set parameter gradients to zero. If set_to_none = True, gradient will be set to None to save memory. :param set_to_none: Whether set the gradient to None. Default value is True. :type set_to_none: bool """ for group_id, param_group in self._fp16_param_groups.items(): for param in param_group: if set_to_none: param.grad = None else: if param.grad is not None: param.grad.detach() param.grad.zero_() #################### # Update Parameter # #################### def step(self, closure=None): assert closure is None, 'closure is not supported by step()' # check for overflow found_inf = self._check_overflow() self.grad_scaler.update(found_inf) # update loss scale if overflow occurs if found_inf: self._grad_store._averaged_gradients = dict() self.zero_grad() return # copy the grad of fp16 param to fp32 param single_grad_partition_groups = [] norm_groups = [] for group_id in range(self.num_param_groups): # compute norm norm_group = compute_norm(gradients=self._grad_store._averaged_gradients[group_id], params=self._param_store.get_fp16_params_by_rank_group(group_id=group_id, rank=self._local_rank), dp_group=self._dp_torch_group, mp_group=self._mp_torch_group) norm_groups.append(norm_group) # create flat gradient for the flat fp32 params fp16_avg_grads = self._grad_store.get_averaged_gradients_by_group(group_id) flat_fp16_avg_grads = flatten(fp16_avg_grads) dtype = self._fp32_flat_param_groups_of_current_rank[group_id].dtype flat_fp32_avg_grads = flat_fp16_avg_grads.to(dtype) param_shape = self._fp32_flat_param_groups_of_current_rank[group_id].shape assert param_shape == flat_fp32_avg_grads.shape, \ f'fp32 param and grad have different shape {param_shape} vs {flat_fp32_avg_grads.shape}' single_grad_partition_groups.append(flat_fp32_avg_grads) device = self._fp32_flat_param_groups_of_current_rank[group_id].device self._fp32_flat_param_groups_of_current_rank[group_id].grad = flat_fp32_avg_grads.to(device) self._grad_store._averaged_gradients[group_id] = [] self._grad_store._averaged_gradients[group_id] = [] # unscale and clip grads global_norm = calculate_global_norm_from_list(norm_list=norm_groups) self._unscale_and_clip_grads(single_grad_partition_groups, global_norm) # update the parameters self.optim.step() # release the fp32 grad release_param_grad(self._fp32_flat_param_groups_of_current_rank.values()) # update fp16 partition updated by the current rank for group_id in range(len(self._fp16_param_groups)): fp16_param = self._param_store.get_flat_fp16_param_by_rank_group(rank=self._local_rank, group_id=group_id) fp32_param = self._fp32_flat_param_groups_of_current_rank[group_id] fp16_param.data.copy_(fp32_param) # broadcast the updated model weights handles = [] for group_id in range(self.num_param_groups): for index in range(self._world_size): rank = self._dp_global_ranks[index] fp16_param = self._param_store.get_flat_fp16_param_by_rank_group(rank=index, group_id=group_id) handle = dist.broadcast(fp16_param, src=rank, group=self._dp_torch_group, async_op=True) handles.append(handle) for handle in handles: handle.wait() ################## # FP16 Utilities # ################## def _check_overflow(self): # clear previous overflow record self._found_overflow.fill_(0.0) # check for overflow for group_id in range(len(self._fp16_param_groups)): for avg_grad in self._grad_store.get_averaged_gradients_by_group(group_id): if avg_grad is not None and has_inf_or_nan(avg_grad): self._found_overflow.fill_(1.0) break # all-reduce across dp group dist.all_reduce(self._found_overflow, op=dist.ReduceOp.MAX, group=self._dp_torch_group) # all-reduce over model parallel group if self._mp_torch_group: dist.all_reduce(self._found_overflow, op=dist.ReduceOp.MAX, group=self._mp_torch_group) if self._found_overflow.item() > 0: return True else: return False def _unscale_and_clip_grads(self, grad_groups_flat, total_norm): # compute combined scale factor for this group combined_scale = self.loss_scale if self._clip_grad_norm > 0.: # norm is in fact norm*scale clip = ((total_norm / self.loss_scale) + 1e-6) / self._clip_grad_norm if clip > 1: combined_scale = clip * self.loss_scale for grad in grad_groups_flat: grad.data.mul_(1. / combined_scale) ############################ # Gradient Synchronization # ############################ def _sync_grad(self): # update param already reduced flag reduction_states = self._param_store.get_param_reduction_states() for tensor, state in reduction_states.items(): reduction_states[tensor] = False # accumulate gradient for group_id in range(self.num_param_groups): param_group = self._param_store.get_fp16_params_by_rank_group(self._local_rank, group_id) avg_gradients_group = self._grad_store.get_averaged_gradients_by_group( group_id ) param_idx = 0 for param in param_group: if param.grad is not None: if len(avg_gradients_group) == param_idx: self._grad_store.append_average_gradient_by_group( group_id, param.grad ) else: self._grad_store.add_average_gradient_by_group( group_id, param_idx, param.grad ) param_idx += 1 # the gradients needed are stored in the avg_gradients buffer # thus, can clear this self.zero_grad() def _reduce_grad_stage1(self): # if not overlapping communication (no reduction hook is attached) # we need to manually reduce these gradients if not self._overlap_communication: for group_id in range(len(self._fp16_param_groups)): param_group = self._fp16_param_groups[group_id] for param in param_group: if param.grad is not None: self._add_to_reduction_bucket(param) # we need to reduce the gradients # left in the communication bucket self._run_reduction() def _reduce_grad_stage2(self): # when partition_grads is True, reduction hooks # are attached in the __init__ function, so we # only need to reduce the gradients # left in the communication bucket for reduce_rank in range(self._world_size): self._run_reduction(reduce_rank)
from .low_level_optim import LowLevelZeroOptimizer from .sharded_optim_v2 import ShardedOptimizerV2 __all__ = ['ShardedOptimizerV2', 'LowLevelZeroOptimizer']
from enum import Enum from os import stat from typing import Dict, Optional, Tuple import torch import torch.distributed as dist import torch.nn as nn from colossalai.amp.naive_amp.grad_scaler import DynamicGradScaler from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.logging import get_dist_logger from colossalai.nn.optimizer import ColossalaiOptimizer from colossalai.gemini.tensor_utils import (colo_model_data_tensor_move_inline, colo_tensor_mem_usage) from colossalai.zero.sharded_model import ShardedModelV2 from colossalai.zero.sharded_model._utils import cast_tensor_to_fp32 from torch import Tensor from torch.distributed import ProcessGroup from torch.nn.parameter import Parameter from torch.optim import Optimizer from colossalai.gemini.stateful_tensor import (StatefulTensor, TensorState) from colossalai.gemini.tensor_placement_policy import AutoTensorPlacementPolicy class OptimState(Enum): SCALED = 1 UNSCALED = 2 class ShardedOptimizerV2(ColossalaiOptimizer): """A wrapper for optimizer. ``ShardedOptimizerV2`` and ``ShardedModelV2`` implement Zero Redundancy Optimizer (ZeRO). By default the ZeRO optimizer stage 3 offload Optimizer States on CPU. We apply the Device-aware Operator Placement technique for OS placement from the following paper. `PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management`_ 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. We place as many OS chunks in the margin space as possible. 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. Note: You must use ``ShardedOptimizerV2`` with ``ShardedModelV2``. Note: Make sure you set ``tensor_placement_policy`` in ``ShardedModelV2`` to `"auto"`, if you set ``gpu_margin_mem_ratio > 0``. 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) which will be used when using hybrid CPU optimizer. This argument is meaningless when `tensor_placement_policy` of `ShardedModelV2` is not "auto". Defaults to 0.0. 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. .. _PatrickStar\: Parallel Training of Pre-trained Models via Chunk-based Memory Management: https://arxiv.org/abs/2108.05818 """ def __init__(self, sharded_model: ShardedModelV2, optimizer: Optimizer, gpu_margin_mem_ratio: float = 0.0, initial_scale: float = 2**32, min_scale: float = 1, growth_factor: float = 2, backoff_factor: float = 0.5, growth_interval: int = 1000, hysteresis: int = 2, max_scale: float = 2**32, dp_process_group: Optional[ProcessGroup] = None, mp_process_group: Optional[ProcessGroup] = None, verbose: bool = False) -> None: assert isinstance(sharded_model, ShardedModelV2), 'model must be wrapped with ShardedModel' assert not isinstance(optimizer, ShardedOptimizerV2), 'Nested ShardedOptimizerV2 is not supported.' super().__init__(optimizer) self.shard_strategy = sharded_model.shard_strategy self.model: ShardedModelV2 = sharded_model 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 self._should_move_fp32_shards_h2d: bool = sharded_model.cpu_offload and self.gpu_margin_mem_ratio > 0.0 and getattr( optimizer, 'num_fp32_shards_per_param', 0) >= 2 self.device = sharded_model._tensor_placement_policy.device or torch.device('cpu') 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) self._found_overflow: Tensor = torch.IntTensor([0]).to(torch.cuda.current_device()) self._logger = get_dist_logger("ShardedOptimizerV2") self._verbose = verbose # Store fp32 param shards self._register_master_weight() if self.gpu_margin_mem_ratio != 0.0 and not isinstance(sharded_model._tensor_placement_policy, AutoTensorPlacementPolicy): self._logger.warning(f'gpu_margin_mem_ratio is meaningless when tensor_placement_policy is not "auto"', ranks=[0]) if self._verbose: self._logger.debug( f"After init ShardedOptimizerV2 consumes {self.get_memory_usage()[0] / 1e6} MB CUDA Memory!", ranks=[0]) self._use_memory_tracer = self.model.use_memory_tracer @property def loss_scale(self): return self.grad_scaler.scale.item() def get_memory_usage(self) -> Tuple[int, int]: """ 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']``) 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 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: # 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 self.model.backward_by_grad(tensor, grad) def clip_grad_norm(self, model: nn.Module, max_norm: float): if self.optim_state == OptimState.SCALED: self._prepare_grads() self._unscale_grads() return super().clip_grad_norm(model, max_norm) def step(self, *args, **kwargs): # unscale grads if scaled if self.optim_state == OptimState.SCALED: self._prepare_grads() self._unscale_grads() self._maybe_move_fp32_shards() found_inf = self._check_overflow() self.grad_scaler.update(found_inf) if found_inf: self._logger.warning('found inf during ShardedOptimV2 step') self._zero_grad(recover_data=True) return self._point_param_fp16_to_master_param() 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]) ret = self.optim.step(*args, **kwargs) 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]) self._copy_master_model_to_model_fp16() return ret def _check_overflow(self): # clear previous overflow record self._found_overflow.fill_(self.model.overflow_counter) # all-reduce across dp group dist.all_reduce(self._found_overflow, group=self.dp_process_group) # all-reduce over model parallel group dist.all_reduce(self._found_overflow, group=self.mp_process_group) return self._found_overflow.item() > 0 def _unscale_grads(self): assert self.optim_state == OptimState.SCALED for group in self.optim.param_groups: for p in group['params']: if p.grad is not None: p.grad.data.div_(self.loss_scale) self.optim_state = OptimState.UNSCALED 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. """ # We must set grad to None # Because grad here is sharded # But next backward pass will create a full grad first # Which leads to wrong accumulation self.optim.zero_grad(set_to_none=True) for group in self.optim.param_groups: for p in group['params']: # p.colo_attr.sharded_data_tensor stores grad now # we have to recover fp16 param reuse_fp16_shard = (p.colo_attr.sharded_data_tensor.payload_size == 0) if recover_data and reuse_fp16_shard: self._copy_master_param_to_param_fp16(p) else: # release saved gradient p.colo_attr.saved_grad.set_null() self.model.overflow_counter = 0 # set overflow counter to zero def sync_grad(self): pass 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' 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 self.shard_strategy.shard([p.colo_attr.sharded_data_tensor], self.dp_process_group) self.master_params[p] = StatefulTensor(cast_tensor_to_fp32(p.colo_attr.data_payload.to(self.device))) if shard_flag: # 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) 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']: if p.colo_attr.saved_grad.is_null(): continue shard_mem = self.master_params[p].payload.numel() * self.master_params[p].payload.element_size() if fp32_shards_used_cuda_margin_mem + shard_mem < fp32_shards_available_cuda_margin_mem: colo_model_data_tensor_move_inline(self.master_params[p], torch.cuda.current_device()) colo_model_data_tensor_move_inline(p.colo_attr.saved_grad, torch.cuda.current_device()) p.colo_attr.offload_grad = False fp32_shards_used_cuda_margin_mem += shard_mem state = self.optim.state[p] for k, v in state.items(): if isinstance(v, Tensor): state[k] = v.cuda() def _prepare_grads(self): for group in self.optim.param_groups: for p in group['params']: if p.colo_attr.saved_grad.is_null(): continue p.colo_attr.saved_grad.trans_state(TensorState.COMPUTE) # If reuse_fp16_shard, grad fp16 which wasn't be offloaded may be evicted to CPU if not p.colo_attr.offload_grad: colo_model_data_tensor_move_inline(p.colo_attr.saved_grad, torch.cuda.current_device()) # 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 # We just set p.data = p.colo_attr.saved_grad.payload here p.data = p.colo_attr.grad_payload p.grad = p.colo_attr.grad_payload # Set p.data to empty tensor, in case of memory leaking p.colo_attr.set_data_none() def _point_param_fp16_to_master_param(self): # 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 def _copy_master_model_to_model_fp16(self): # 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']: self._copy_master_param_to_param_fp16(p) def _copy_master_param_to_param_fp16(self, p): # flush gradient 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() p.data = self.master_params[p].payload # 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()) p.colo_attr.set_data_none() 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) self.master_params[p].trans_state(TensorState.HOLD) 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 def load_state_dict(self, *args, **kwargs): 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) 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)
import math from typing import Optional import torch import torch.distributed as dist from torch._six import inf from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors from colossalai.tensor import ColoParameter from colossalai.utils import is_model_parallel_parameter def flatten(input_): return _flatten_dense_tensors(input_) def unflatten(flat, tensors): return _unflatten_dense_tensors(flat, tensors) def count_numel(tensor_list): res = 0 for tensor in tensor_list: res += tensor.numel() return res def calculate_padding(numel, unit_size): remainder = numel % unit_size return unit_size - remainder if remainder else remainder def shuffle_by_round_robin(tensor_list, num_partitions): partitions = dict() for tensor_idx, tensor in enumerate(tensor_list): partition_to_go = tensor_idx % num_partitions if partition_to_go not in partitions: partitions[partition_to_go] = [] partitions[partition_to_go].append(dict(tensor=tensor, index=tensor_idx)) partitions_count = len(partitions) new_tensor_list = [] tensor_index_mapping = dict() for partition_id in range(partitions_count): partition_tensors = partitions[partition_id] for item in partition_tensors: tensor_index_mapping[item['index']] = len(new_tensor_list) new_tensor_list.append(item['tensor']) return new_tensor_list, tensor_index_mapping # create a flat tensor aligned at the alignment boundary def flatten_dense_tensors_with_padding(tensor_list, unit_size): num_elements = count_numel(tensor_list) padding = calculate_padding(num_elements, unit_size=unit_size) if padding > 0: pad_tensor = torch.zeros(padding, device=tensor_list[0].device, dtype=tensor_list[0].dtype) padded_tensor_list = tensor_list + [pad_tensor] else: padded_tensor_list = tensor_list return flatten(padded_tensor_list) def is_nccl_aligned(tensor): return tensor.data_ptr() % 4 == 0 def get_grad_accumulate_object(tensor): """ Return the AccumulateGrad of the input tensor """ # grad_fn reference: # https://discuss.pytorch.org/t/in-the-grad-fn-i-find-a-next-functions-but-i-dont-understand-the-meaning-of-the-attribute/24463 # expand_as reference: https://pytorch.org/docs/stable/generated/torch.Tensor.expand.html#torch.Tensor.expand # # `next_functions` will return the backward graph where # the first element is the AccumulateGrad of the leaf nodes. # we want to get the AccumulateGrad of the input tensor instead of the leaf # node in the whole computation graph. # Therefore, we call expand_as to create a dummy graph # where tensor_tmp and tensor indeed point to the same object. # You can check this by print(tensor.data_ptr() == tensor_tmp.data_ptr()) tensor_tmp = tensor.expand_as(tensor) grad_acc_obj = tensor_tmp.grad_fn.next_functions[0][0] return grad_acc_obj def split_half_float_double(tensor_list): dtypes = ["torch.cuda.HalfTensor", "torch.cuda.FloatTensor", "torch.cuda.DoubleTensor", "torch.cuda.BFloat16Tensor"] buckets = [] for i, dtype in enumerate(dtypes): bucket = [t for t in tensor_list if t.type() == dtype] if bucket: buckets.append(bucket) return buckets def reduce_tensor_dp_group(tensor: torch.Tensor, dtype: Optional[torch.dtype] = None, dst_local_rank: Optional[int] = None, dst_global_rank: Optional[int] = None, group: Optional[dist.ProcessGroup] = None): """ Reduce the tensor in the data parallel process group :param tensor: A tensor object to reduce/all-reduce :param dtype: The data type used in communication :param dst_rank: The source rank for reduce. If dst_rank is None, :param parallel_mode: Communication parallel mode all-reduce will be used instead of reduce. Default is None. :type tensor: torch.Tensor :type dtype: torch.dtype, optional :type dst_rank: int, optional :type pg: ProcessGroup, optional """ # use the original dtype if dtype is None: dtype = tensor.dtype # cast the data to specified dtype for reduce/all-reduce if tensor.dtype != dtype: tensor_to_reduce = tensor.to(dtype) else: tensor_to_reduce = tensor world_size = dist.get_world_size(group=group) tensor_to_reduce.div_(world_size) # if rank is None, all reduce will be used # else, reduce is used use_all_reduce = dst_local_rank is None if use_all_reduce: dist.all_reduce(tensor_to_reduce, group=group) else: dist.reduce(tensor=tensor_to_reduce, dst=dst_global_rank, group=group) # recover the original dtype if tensor.dtype != dtype and tensor is not tensor_to_reduce: local_rank = dist.get_rank(group=group) if use_all_reduce or dst_local_rank == local_rank: tensor.copy_(tensor_to_reduce) return tensor def has_inf_or_nan(tensor): try: # if tensor is half, the .float() incurs an additional deep copy, but it's necessary if # Pytorch's .sum() creates a one-element tensor of the same type as tensor # (which is true for some recent version of pytorch). tensor_sum = float(tensor.float().sum()) # More efficient version that can be used if .sum() returns a Python scalar # tensor_sum = float(tensor.sum()) except RuntimeError as instance: # We want to check if inst is actually an overflow exception. # RuntimeError could come from a different error. # If so, we still want the exception to propagate. if "value cannot be converted" not in instance.args[0]: raise return True else: if tensor_sum == float('inf') or tensor_sum == -float('inf') or tensor_sum != tensor_sum: return True return False def release_param_grad(tensor_list): for tensor in tensor_list: tensor.grad = None def calculate_global_norm_from_list(norm_list): """ Compute total from a list of norms """ total_norm = 0.0 for norm in norm_list: total_norm += norm**2.0 return math.sqrt(total_norm) def compute_norm(gradients, params, dp_group, mp_group, norm_type=2): """Clips gradient norm of an iterable of parameters. This is adapted from torch.nn.utils.clip_grad.clip_grad_norm_ and added functionality to handle model parallel parameters. Note that the gradients are modified in place. Arguments: parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a single Tensor that will have gradients normalized max_norm (float or int): max norm of the gradients norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for infinity norm. Returns: Total norm of the parameters (viewed as a single vector). """ if mp_group is None: mp_rank = 0 else: mp_rank = dist.get_rank(mp_group) norm_type = float(norm_type) if norm_type == inf: total_norm = max(g.data.abs().max() for g in gradients) total_norm_cuda = torch.cuda.FloatTensor([float(total_norm)]) dist.all_reduce(total_norm_cuda, op=torch.distributed.ReduceOp.MAX, group=dp_group) # Take max across all GPUs. if mp_group is not None: dist.all_reduce(tensor=total_norm_cuda, op=torch.distributed.ReduceOp.MAX) total_norm = total_norm_cuda[0].item() else: total_norm = 0.0 # if dist.get_rank() == 0: # logger.info(f"Total Norm beginning {total_norm}") for g, p in zip(gradients, params): # Pipeline parallelism may replicate parameters. Avoid multi-counting. tp_param_flag = False if is_model_parallel_parameter(p) or (isinstance(p, ColoParameter) and not p.is_replicate()): tp_param_flag = True if tp_param_flag or mp_rank == 0: param_norm = g.data.double().norm(2) total_norm += param_norm.item()**2 # Sum across all model parallel GPUs. total_norm_cuda = torch.cuda.FloatTensor([float(total_norm)]) torch.distributed.all_reduce(total_norm_cuda, op=torch.distributed.ReduceOp.SUM, group=dp_group) if mp_group is not None: dist.all_reduce(tensor=total_norm_cuda, op=torch.distributed.ReduceOp.SUM, group=mp_group) total_norm = total_norm_cuda[0].item()**(1. / norm_type) if total_norm == float('inf') or total_norm == -float('inf') or total_norm != total_norm: total_norm = -1 return total_norm def sync_param(flat_tensor, tensor_list): """ Synchronize the flattened tensor and unflattened tensor list. When a list of tensor are flattened with `torch._utils._unflatten_dense_tensors`, a new tensor is created. Thus, the flat tensor and original tensor list do not share the same memory space. This function will update the tensor list so that they point to the same value. :param flat_tensor: A flat tensor obtained by calling `torch._utils._unflatten_dense_tensors` on a tensor lsit :param tensor_list: A list of tensors corresponding to the flattened tensor :type flat_tensor: torch.Tensor :type tensor_list: List[torch.Tensor] """ updated_params = unflatten(flat_tensor, tensor_list) # update the tensor data for p, q in zip(tensor_list, updated_params): p.data = q.data
from typing import List from torch import Tensor from torch.distributed import ProcessGroup from .base_store import BaseStore class ParameterStore(BaseStore): def __init__(self, torch_pg: ProcessGroup): super().__init__(torch_pg) # param partitioning data structures self._fp16_param_to_rank = dict() self._rank_groupid_to_fp16_param_list = dict() self._rank_group_id_to_flat_fp16_param = dict() # param reduction data structures self._is_param_reduced = dict() self._reduced_param = [] def set_param_to_rank(self, tensor: Tensor, rank: int) -> None: """ Set the mapping between parameter to rank, each parameter should be owned by a rank. :param tensor: A :class:`torch.Tensor` object :type tensor: torch.Tensor :param rank: The rank of which the process is responsible for updating the parameter :type rank: int """ self._fp16_param_to_rank[tensor] = rank def get_param_rank(self, tensor: Tensor) -> int: """ Gives the rank which the parameter belongs to :param tensor: A :class:`torch.Tensor` object :type tensor: torch.Tensor """ return self._fp16_param_to_rank[tensor] def belongs_to_current_rank(self, tensor) -> bool: """ Check whether a parameter is supposed to be updated by the process of the current rank :param tensor: A :class:`torch.Tensor` object :type tensor: torch.Tensor :return: True if the parameter should be updated by the current rank. Otherwise false. :rtype: bool """ tensor_rank = self._fp16_param_to_rank[tensor] return tensor_rank == self._local_rank def add_fp16_param_list_by_rank_group(self, rank, group_id, tensor_list) -> None: if rank not in self._rank_groupid_to_fp16_param_list: self._rank_groupid_to_fp16_param_list[rank] = dict() if group_id not in self._rank_groupid_to_fp16_param_list[rank]: self._rank_groupid_to_fp16_param_list[rank][group_id] = [] self._rank_groupid_to_fp16_param_list[rank][group_id].extend(tensor_list) def get_fp16_params_by_rank_group(self, rank, group_id) -> List[Tensor]: return self._rank_groupid_to_fp16_param_list[rank][group_id] def add_flat_fp16_param_by_rank_group(self, rank, group_id, tensor) -> None: if rank not in self._rank_group_id_to_flat_fp16_param: self._rank_group_id_to_flat_fp16_param[rank] = dict() self._rank_group_id_to_flat_fp16_param[rank][group_id] = tensor def get_flat_fp16_param_by_rank_group(self, rank, group_id) -> Tensor: return self._rank_group_id_to_flat_fp16_param[rank][group_id] def is_param_reduced(self, tensor): return self._is_param_reduced[tensor] def set_param_reduction_state(self, tensor, state): self._is_param_reduced[tensor] = state def get_param_reduction_states(self): return self._is_param_reduced def reset_previous_reduced_params(self): self._reduced_param = [] def add_previous_reduced_param(self, tensor): self._reduced_param.append(tensor) def clear_grads_of_previous_reduced_params(self): if len(self._reduced_param) > 0: for param in self._reduced_param: param.grad = None self.reset_previous_reduced_params()
from typing import List from torch import Tensor from .base_store import BaseStore class GradientStore(BaseStore): def __init__(self, *args): super().__init__(*args) # bookkeeping data structures self._averaged_gradients = dict() # for backward reduction hooks self._grad_acc_objs = [] def append_accumulate_grad_object(self, obj): """ Keep :class:`AccumulateGrad` objects. If these objects are not kept, reduction hooks may not be attached successfully. :param obj: An object of :class:`AccumulateGrad` class :type obj: :class:`AccumulateGrad` """ self._grad_acc_objs.append(obj) def get_averaged_gradients_by_group(self, group_id: int) -> List[Tensor]: """ Return average gradients of a parameter group :param group_id: The index of parameter group :type group_id: int :return: Return the list of averaged gradients of a parameter group. Each element is a gradient, not a parameter. :rtype: List[torch.Tensor] """ if group_id not in self._averaged_gradients: self._averaged_gradients[group_id] = [] return self._averaged_gradients[group_id] def append_average_gradient_by_group(self, group_id: int, tensor: Tensor) -> None: """ Append an average gradient to the list of averaged gradients of a parameter group :param group_id: The index of a parameter group :param tensor: A :class:`torch.Tensor` object :type group_id: int :type tensor: torch.Tensor """ if group_id in self._averaged_gradients: self._averaged_gradients[group_id].append(tensor) else: self._averaged_gradients[group_id] = [tensor] def add_average_gradient_by_group( self, group_id: int, tensor_idx: int, tensor: Tensor ) -> None: """ Add an average gradient to the list of averaged gradients of a parameter group :param group_id: The index of a parameter group :param tensor_idx: The index of a tensor in the list of averaged gradients :param tensor: A :class:`torch.Tensor` object :type group_id: int :type tensor_idx: int :type tensor: torch.Tensor """ self._averaged_gradients[group_id][tensor_idx].add_(tensor) def reset_average_gradients_by_group(self, group_id: int) -> None: """ Reset the bookkeeping data structure for averaged gradients to an empty list :param group_id: The index of a parameter group :type group_id: int """ self._averaged_gradients[group_id] = []
from .bucket_store import BucketStore from .gradient_store import GradientStore from .parameter_store import ParameterStore from .tensor_bucket import TensorBucket __all__ = ['GradientStore', 'ParameterStore', 'BucketStore', 'TensorBucket']
from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors class TensorBucket: def __init__(self, size): self._max_size = size self._current_size = 0 self._bucket = [] @property def max_size(self): return self._max_size @property def current_size(self): return self._current_size def is_full_or_oversized(self): return self._current_size >= self._max_size def is_empty(self): return len(self._bucket) == 0 def add_to_bucket(self, tensor, allow_oversize=False): tensor_size = tensor.numel() if not allow_oversize and self.will_exceed_max_size(tensor_size): msg = f"The param bucket max size {self._max_size} is exceeded" \ + f"by tensor (size {tensor_size})" raise RuntimeError(msg) self._bucket.append(tensor) self._current_size += tensor_size def will_exceed_max_size(self, tensor_size): expected_size = self._current_size + tensor_size return expected_size > self._max_size def get_bucket(self): return self._bucket def empty(self): self._bucket = [] self._size = 0 def flatten(self): return _flatten_dense_tensors(self._bucket) def unflatten_and_copy(self, flat_tensor): unflattened_tensor_list = _unflatten_dense_tensors(flat_tensor, self._bucket) for old, new in zip(self._bucket, unflattened_tensor_list): old.copy_(new)
import torch.distributed as dist from torch.distributed import ProcessGroup class BaseStore: def __init__(self, torch_pg: ProcessGroup): self._world_size = dist.get_world_size(group=torch_pg) self._local_rank = dist.get_rank(group=torch_pg) @property def world_size(self): return self._world_size @property def local_rank(self): return self._local_rank
from torch.distributed import ProcessGroup from .base_store import BaseStore class BucketStore(BaseStore): def __init__(self, torch_pg: ProcessGroup): super().__init__(torch_pg) self._params = dict() self._num_elements_in_bucket = dict() self.reset() def num_elements_in_bucket(self, reduce_rank: int = None): return self._num_elements_in_bucket[reduce_rank] def add_num_elements_in_bucket(self, num_elements, reduce_rank: int = None): self._num_elements_in_bucket[reduce_rank] += num_elements def add_param(self, tensor, reduce_rank: int = None): self._params[reduce_rank].append(tensor) def reset(self): keys = [None] + list(range(self._world_size)) self._params = {rank: [] for rank in keys} self._num_elements_in_bucket = {rank: 0 for rank in keys} def reset_by_rank(self, reduce_rank=None): self._params[reduce_rank] = [] self._num_elements_in_bucket[reduce_rank] = 0 def get_grad(self, reduce_rank: int = None): param_list = self.get_param(reduce_rank) for param in param_list: # the param must have grad for reduction assert param.grad is not None, f'Parameter of size ({param.size()}) has None grad, cannot be reduced' return [param.grad for param in param_list] def get_param(self, reduce_rank: int = None): return self._params[reduce_rank]
from typing import Optional import torch import torch.distributed as dist from colossalai.gemini.memory_tracer import MemStatsCollector from colossalai.gemini.ophooks import BaseOpHook from colossalai.gemini.stateful_tensor import TensorState from colossalai.gemini.stateful_tensor_mgr import StatefulTensorMgr from colossalai.logging import get_dist_logger from colossalai.registry import OPHOOKS from colossalai.utils import get_current_device from colossalai.zero.shard_utils import BaseShardStrategy @OPHOOKS.register_module class ZeroHook(BaseOpHook): """ A hook to process sharded param for ZeRO method. Warning: this class has been deprecated after version 0.1.12 """ def __init__(self, shard_strategy: BaseShardStrategy, memstarts_collector: Optional[MemStatsCollector] = None, stateful_tensor_mgr: Optional[StatefulTensorMgr] = None, process_group: Optional[dist.ProcessGroup] = None): super().__init__() self.logger = get_dist_logger("ZeROHook") self.shard_strategy = shard_strategy self.process_group = process_group # NOTE(jiaruifang) Now the computing device of FWD and BWD is always on GPU self.computing_device = get_current_device() self._memstarts_collector = memstarts_collector self._stateful_tensor_mgr = stateful_tensor_mgr def gather_parameters(self, module: torch.nn.Module): # gather sharded parameters if module.param_is_sharded: tensor_list = [] for param in module.parameters(recurse=False): assert hasattr(param, 'colo_attr') tensor_list.append(param.colo_attr.sharded_data_tensor) self.shard_strategy.gather(tensor_list, self.process_group) def shard_parameters(self, module: torch.nn.Module): # shard gathered parameters if module.param_is_sharded: tensor_list = [] for param in module.parameters(recurse=False): assert hasattr(param, 'colo_attr') tensor_list.append(param.colo_attr.sharded_data_tensor) self.shard_strategy.shard(tensor_list, self.process_group) def adjust_module_data(self, module: torch.nn.Module): # record overall data statistics if self._memstarts_collector: self._memstarts_collector.sample_overall_data() for param in module.parameters(recurse=False): param.colo_attr.sharded_data_tensor.trans_state(TensorState.COMPUTE) # adjust stateful tensor to get enough CUDA memory self._stateful_tensor_mgr.adjust_layout() # record model data statistics if self._memstarts_collector: self._memstarts_collector.record_model_data_volume() def pre_fwd_exec(self, module: torch.nn.Module, *args): self.adjust_module_data(module) self.gather_parameters(module) for param in module.parameters(recurse=False): param.data = param.colo_attr.data_payload assert param.data.device.type == 'cuda', f"PRE FWD param.data must be on CUDA" def post_fwd_exec(self, module: torch.nn.Module, *args): # change tensor state to HOLD_AFTER_FWD for param in module.parameters(recurse=False): param.colo_attr.sharded_data_tensor.trans_state(TensorState.HOLD_AFTER_FWD) self.shard_parameters(module) # remove torch payload for param in module.parameters(recurse=False): param.colo_attr.set_data_none() def pre_bwd_exec(self, module: torch.nn.Module, input, output): self.adjust_module_data(module) self.gather_parameters(module) for param in module.parameters(recurse=False): param.data = param.colo_attr.data_payload assert param.data.device.type == 'cuda', f"PRE BWD param.data must be on CUDA" def post_bwd_exec(self, module: torch.nn.Module, input): # change tensor state to HOLD_AFTER_BWD for param in module.parameters(recurse=False): param.colo_attr.sharded_data_tensor.trans_state(TensorState.HOLD_AFTER_BWD) self.shard_parameters(module) # remove torch payload for param in module.parameters(recurse=False): param.colo_attr.set_data_none() def pre_iter(self): pass def post_iter(self): if self._stateful_tensor_mgr: self.logger.debug( f"CPU-GPU data moving this iteration {self._stateful_tensor_mgr.cpu_gpu_move_volume/1e9} GB, get layout info time: {self._stateful_tensor_mgr._layout_time}, evict cpu time: {self._stateful_tensor_mgr._evict_time}", ranks=[0]) self._stateful_tensor_mgr.finish_iter()
from .zero_hook import ZeroHook __all__ = ['ZeroHook']
from contextlib import contextmanager from enum import Enum from functools import partial from typing import List import torch from colossalai.gemini import TensorState from colossalai.gemini.gemini_mgr import GeminiManager from colossalai.tensor.param_op_hook import ColoParamOpHook from colossalai.utils import is_ddp_ignored class TrainingPhase(Enum): FORWARD = 0 BACKWARD = 1 class GeminiZeROHook(ColoParamOpHook): def __init__(self, gemini_manager: GeminiManager) -> None: super().__init__() self._gemini_manager = gemini_manager self._chunk_manager = gemini_manager.chunk_manager self._training_phase = TrainingPhase.FORWARD def pre_op(self, params): params = [p for p in params if not is_ddp_ignored(p)] chunks = self._chunk_manager.get_chunks(params) for p in params: self._chunk_manager.trans_tensor_state(p, TensorState.COMPUTE) self._gemini_manager.sample_overall_data() self._gemini_manager.adjust_layout(chunks) for chunk in chunks: self._chunk_manager.access_chunk(chunk) # record cuda model data of the current OP self._gemini_manager.record_model_data_volume() def post_op(self, params): params = [p for p in params if not is_ddp_ignored(p)] for p in params: tensor_state = TensorState.HOLD if self._training_phase == TrainingPhase.FORWARD or not p.requires_grad else TensorState.HOLD_AFTER_BWD self._chunk_manager.trans_tensor_state(p, tensor_state) def pre_forward(self, params: List[torch.Tensor]) -> None: self.pre_op(params) def post_forward(self, params: List[torch.Tensor]) -> None: self.post_op(params) def pre_backward(self, params: List[torch.Tensor]) -> None: self.pre_op(params) def post_backward(self, params: List[torch.Tensor]) -> None: self.post_op(params) @contextmanager def switch_training_phase(self, training_phase: TrainingPhase = TrainingPhase.BACKWARD): old_training_phase = self._training_phase try: self._training_phase = training_phase yield finally: self._training_phase = old_training_phase switch_to_backward = switch_training_phase switch_to_forward = partial(switch_to_backward, training_phase=TrainingPhase.FORWARD)
import torch from colossalai.gemini.stateful_tensor import StatefulTensor, TensorState class ShardedTensor(StatefulTensor): def __init__(self, tensor: torch.Tensor, state: TensorState = TensorState.HOLD) -> None: r""" A tensor sharded in multiple processes. Constructed from an existing torch.Tensor instance. """ assert tensor.requires_grad is False super().__init__(tensor, state) # kept the shape, numel and dtype of the init tensor. self._origin_shape = tensor.shape self._origin_numel = tensor.numel() self._origin_dtype = tensor.dtype self._is_sharded = False @property def dtype(self) -> torch.dtype: assert self._payload.dtype == self._origin_dtype return self._payload.dtype @property def origin_numel(self) -> int: return self._origin_numel @property def origin_shape(self) -> int: return self._origin_shape @property def is_sharded(self): return self._is_sharded @is_sharded.setter def is_sharded(self, flag: bool): self._is_sharded = flag
import torch from typing import Optional, Tuple from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor from colossalai.gemini.tensor_utils import colo_tensor_mem_usage from colossalai.gemini.stateful_tensor import StatefulTensor, TensorState from typing import List EMPTY_TENSOR_DICT = {} def get_empty_tensor(device: torch.device, dtype: torch.dtype): key = (device, dtype) if key not in EMPTY_TENSOR_DICT: EMPTY_TENSOR_DICT[key] = torch.empty(0, dtype=dtype, device=device) return EMPTY_TENSOR_DICT[key] class ShardedParamV2(object): def __init__(self, param: torch.nn.Parameter, set_data_none: bool = False) -> None: self._sharded_data_tensor: ShardedTensor = ShardedTensor(param.data) self.saved_grad: StatefulTensor = StatefulTensor(None, TensorState.FREE) # This attribute must be initialized in ShardedModel self.offload_grad: bool = False # make sure the shared param is the only owner of payload # The param.data maybe used to init the other part of the model. # For example: File "resnet.py", line 190, in __init__ # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') # So we can not empty the .data at this time self.param = param if set_data_none: self.set_data_none() def get_payload_tensors(self) -> List[StatefulTensor]: """returns stateful tensors kept by this class. """ return [self._sharded_data_tensor] def set_data_none(self): self.param.data = get_empty_tensor(self.sharded_data_tensor.device, self.sharded_data_tensor.dtype) def set_grad_none(self): self.saved_grad.set_null() @property def sharded_data_tensor(self): return self._sharded_data_tensor @property def data_payload(self): assert not self.sharded_data_tensor.is_null() return self.sharded_data_tensor.payload @property def grad_payload(self): assert not self.saved_grad.is_null() return self.saved_grad.payload @property def param_is_sharded(self): return self.sharded_data_tensor.is_sharded def data_payload_reset(self, tensor: torch.Tensor): assert type(tensor) is torch.Tensor assert tensor.requires_grad is False self.sharded_data_tensor.payload_reset(tensor) def grad_payload_reset(self, tensor: torch.Tensor): assert type(tensor) is torch.Tensor assert tensor.requires_grad is False self.saved_grad.payload_reset(tensor) def get_memory_usage(self) -> Tuple[int, int]: """ get the memory usage of the param, including data and grad Returns: Tuple[int, int]: cuda mem usage in Byte, cpu memory usage in Byte """ cuda_mem_use, cpu_mem_use = 0, 0 def _update_mem_use(t: Optional[torch.Tensor]): if t is None: return assert isinstance(t, torch.Tensor) nonlocal cuda_mem_use nonlocal cpu_mem_use t_cuda, t_cpu = colo_tensor_mem_usage(t) cuda_mem_use += t_cuda cpu_mem_use += t_cpu address_set = set() _update_mem_use(self.data_payload) address_set.add(self.data_payload.data_ptr()) if not self.saved_grad.is_null() and self.saved_grad.data_ptr() not in address_set: _update_mem_use(self.grad_payload) address_set.add(self.saved_grad.data_ptr()) if self.param.data is not None and self.param.data.data_ptr() not in address_set: _update_mem_use(self.param.data) address_set.add(self.param.data.data_ptr()) if self.param.grad is not None and self.param.grad.data_ptr() not in address_set: _update_mem_use(self.param.grad) return cuda_mem_use, cpu_mem_use
from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor from colossalai.zero.sharded_param.sharded_param import ShardedParamV2 __all__ = ['ShardedTensor', 'ShardedParamV2']
from typing import List, Optional import torch import torch.distributed as dist from colossalai.utils import get_current_device from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor from torch._utils import _flatten_dense_tensors as flatten from .tensor_shard_strategy import TensorShardStrategy class BucketTensorShardStrategy(TensorShardStrategy): """Use the same shard scheme as `TensorShardStrategy`'s, but it gathers tensors of a sub-module together, which will fully utilize network bandwidth. It is especially useful when sub-module contains bias, since we cannot utilize network bandwidth well if we only gather a bias tensor (bias is usaully small). """ def gather(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None): tensor_list: List[ShardedTensor] = [t for t in tensor_list if t.is_sharded] if len(tensor_list) == 0: return target_device = tensor_list[0].device dtype = tensor_list[0].dtype buffer_list: List[torch.Tensor] = [] tensor_numels = [t.payload.numel() for t in tensor_list] buffer_size = sum(tensor_numels) world_size = dist.get_world_size(process_group) rank = dist.get_rank(process_group) for i in range(world_size): if i == rank: buffer_list.append(flatten([t.payload for t in tensor_list]).cuda(get_current_device())) else: buffer_list.append(torch.zeros(buffer_size, dtype=dtype, device=get_current_device())) dist.all_gather(buffer_list, buffer_list[rank], group=process_group) # Move to target device before splitting buffer # Ensure we utilize maximum PCIE bandwidth buffer_list = [buffer.to(target_device) for buffer in buffer_list] offset = 0 for i, t in enumerate(tensor_list): gathered_payload = [buffer[offset:offset + tensor_numels[i]] for buffer in buffer_list] gathered_payload = torch.cat(gathered_payload)[:t.origin_numel].view(t.origin_shape) t.payload_reset(gathered_payload) t.is_sharded = False offset += tensor_numels[i]
from .base_shard_strategy import BaseShardStrategy from .bucket_tensor_shard_strategy import BucketTensorShardStrategy from .tensor_shard_strategy import TensorShardStrategy __all__ = ['BaseShardStrategy', 'TensorShardStrategy', 'BucketTensorShardStrategy']
from abc import ABC, abstractmethod from typing import List, Optional import torch.distributed as dist from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor class BaseShardStrategy(ABC): def __init__(self) -> None: """Abstract Shard Strategy. Use to shard a tensors on multiple GPUs. """ super().__init__() @abstractmethod def shard(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None): pass @abstractmethod def gather(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None): pass
import torch import torch.nn.functional as F from typing import Tuple def get_shard(tensor: torch.Tensor, rank: int, world_size: int) -> Tuple[torch.Tensor, int]: """Return the local shard of a full tensor.""" # Shard using torch.chunk to match all-gather/reduce-scatter. chunks = list(torch.flatten(tensor).chunk(world_size)) while len(chunks) < world_size: chunks.append(chunks[0].new_empty(0)) # Determine number of padding elements. num_to_pad = chunks[0].numel() - chunks[rank].numel() assert num_to_pad >= 0, num_to_pad shard = torch.zeros_like(chunks[0]) length = chunks[rank].size(0) shard_temp = shard[:length] shard_temp.copy_(chunks[rank]) return shard, num_to_pad
from typing import List, Optional import torch import torch.distributed as dist from colossalai.utils import get_current_device from colossalai.zero.shard_utils import BaseShardStrategy from colossalai.zero.shard_utils.commons import get_shard from colossalai.zero.sharded_param.sharded_tensor import ShardedTensor from colossalai.gemini.tensor_utils import colo_model_data_tensor_move_inline class TensorShardStrategy(BaseShardStrategy): """ A naive implementation which shard each tensor evenly over all ranks """ def shard(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None): for t in tensor_list: self._shard_tensor(t, process_group) def gather(self, tensor_list: List[ShardedTensor], process_group: Optional[dist.ProcessGroup] = None): for t in tensor_list: self._gather_tensor(t, process_group) def _shard_tensor(self, t: ShardedTensor, process_group: Optional[dist.ProcessGroup] = None): """ Shard tensor among processes. Args: t (ShardedTensor): a tensor to be sharded. process_group (Optional[dist.ProcessGroup], optional): the process group among which tensor shards. Defaults to None. """ if t.is_sharded: return if t.payload.device.type == 'cuda': assert t.payload.device == get_current_device(), f"shard tensor on cuda device index {t.payload.device.index},"\ f" but current cuda device is {get_current_device()}" sharded_payload, _ = get_shard(t.payload, dist.get_rank(process_group), dist.get_world_size(process_group)) t.payload_reset(sharded_payload) t.is_sharded = True def _gather_tensor(self, t: ShardedTensor, process_group: Optional[dist.ProcessGroup] = None): if not t.is_sharded: return target_device = t.device payload_numel = t.payload.numel() world_size = dist.get_world_size(process_group) rank = dist.get_rank(process_group) buffer = torch.empty(payload_numel * world_size, dtype=t.payload.dtype, device=get_current_device()) buffer_list = list(torch.chunk(buffer, chunks=world_size, dim=0)) buffer_list[rank].copy_(t.payload) dist.all_gather(buffer_list, buffer_list[rank], group=process_group, async_op=False) gathered_payload = torch.narrow(buffer, 0, 0, t.origin_numel).reshape(t.origin_shape) t.payload_reset(gathered_payload) colo_model_data_tensor_move_inline(t, target_device) t.is_sharded = False
from .sharded_model_v2 import ShardedModelV2 __all__ = ['ShardedModelV2']
# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the BSD license found in the # LICENSE file in the root directory of this source tree. import functools import os from typing import Callable, Dict, List, Optional, Tuple import torch import torch.distributed as dist from torch import Tensor from torch.distributed import ProcessGroup # TODO: Remove the toggle-enable_nccl_base_collectives when github open issue #801 is resolved. if os.getenv("ENABLE_NCCL_BASE_COLLECTIVES", "1") == "0": enable_nccl_base_collectives = False else: enable_nccl_base_collectives = True class Bucket: def __init__(self, shard_size: int, dtype: torch.dtype, device: torch.device, group: ProcessGroup): self.buffer = torch.zeros((group.size(), shard_size), dtype=dtype, device=device) self.group = group self.offset = 0 self.callbacks: List[Callable] = [] self.output_shard = torch.zeros_like(self.buffer[0]) def flush(self) -> None: """Flush content of the bucket.""" if self.offset == 0: assert len(self.callbacks) == 0 return # reduce-scatter bucket if hasattr(dist, "_reduce_scatter_base") and enable_nccl_base_collectives: dist._reduce_scatter_base(self.output_shard[:self.offset], self.buffer[:, :self.offset].contiguous(), group=self.group) else: dist.reduce_scatter(self.output_shard[:self.offset], list(self.buffer[:, :self.offset].unbind(0)), group=self.group) # execute post-reduction callbacks for callback_fn in self.callbacks: callback_fn() # reuse input bucket but allocate a fresh output shard self.buffer[:, :self.offset].zero_() self.offset = 0 self.callbacks.clear() self.output_shard = torch.zeros_like(self.buffer[0]) def alloc(self) -> None: """Setup the buffers if they are not allocated. Using ``setup`` and ``teardown``, we can ensure that the bucket buffers are only allocated during the backward pass, hence saving more memory to other parts of the training process, such as the forward pass for activation memory. """ for tensor in [self.buffer, self.output_shard]: if tensor.storage().size() == 0: tensor.storage().resize_(tensor.size().numel()) def free(self) -> None: """Tear down the bucket by freeing the memory""" assert self.offset == 0 and self.callbacks == [], "Incorrect call of teardown" for tensor in [self.buffer, self.output_shard]: tensor.storage().resize_(0) def append(self, tensor_list: List[Tensor], callback_fn: Callable): # copy data from input_list into bucket tensor_size = tensor_list[0].numel() stacked_input = torch.stack(tensor_list).view(self.group.size(), tensor_size) offset = self.offset self.buffer[:, offset:offset + tensor_size].copy_(stacked_input) self.offset += tensor_size # callback will be given the reduced result if callback_fn is not None: result_view = self.output_shard[offset:offset + tensor_size].view_as(tensor_list[0]) self.callbacks.append(functools.partial(callback_fn, result_view)) class ReduceScatterBucketer: """ Helper for bucketing multiple reduce-scatter operations on small tensors into larger reduce-scatter ops to improve communication efficiency. Usage:: bucketer = ReduceScatterBucketer() bucketer.reduce_scatter_async( small_tensors, callback_fn=lambda result: print("small") ) bucketer.reduce_scatter_async( big_tensors, callback_fn=lambda result: print("big") ) bucketer.reduce_scatter_async( more_small_tensors, callback_fn=lambda result: print("small2") ) bucketer.flush() # callbacks only guaranteed to be called after flush() # Example output (note that it is out of order, due to bucketing): # big # small # small2 Args: bucket_size_mb (int, Optional): bucket size for communicating. Buckets are sub-divided based on world_size. Values <= 0 disable bucketing. """ def __init__(self, bucket_size_mb: int = 25): self.bucket_size_mb = bucket_size_mb self.buckets: Dict[Tuple[torch.dtype, torch.device, ProcessGroup], Bucket] = {} @torch.no_grad() def reduce_scatter_async( self, input_list: List[Tensor], group: ProcessGroup, callback_fn: Optional[Callable] = None, ) -> None: """ Reduce-scatter a list of tensors asynchronously, so smaller reductions can be bucketed together. The given callback (``callback_fn``) will be called with the reduced result at some later time. Call ``flush()`` to force all queued ops and callbacks to be executed. Note that large inputs will be reduced immediately, and this function may also flush the relevant bucket to make room for ``input_list``. Args: input_list (List[Tensor]): list of tensors to reduce-scatter. List should contain ``group.size()`` tensors and each tensor should have identical shape, dtype and device. group (ProcessGroup): process group for reduction callback_fn (Callable, Optional): callback function to call after the reduction executes. Function will be called with a single argument corresponding to the reduced result. """ world_size = group.size() assert (len(input_list) == world_size ), f"reduce_scatter received {len(input_list)} inputs, expected group.size() ({world_size})" first_input = input_list[0] first_input_size = first_input.numel() bucket_shard_size = self._get_shard_size(first_input.element_size(), world_size) if first_input_size > bucket_shard_size: # TODO: investigate how to avoid using torch.cat (because it seems to be slow for CPU tensors) # input is too big to fit in the bucket, reduce-scatter directly output = torch.zeros_like(input_list[0]) if hasattr(dist, "_reduce_scatter_base") and enable_nccl_base_collectives: input_flattened = torch.cat(input_list) dist._reduce_scatter_base(output, input_flattened, group=group) else: # fallback dist.reduce_scatter(output, input_list, group=group) if callback_fn is not None: callback_fn(output) return bucket = self._get_bucket(first_input, group) if first_input_size > bucket.buffer.size(1) - bucket.offset: # not enough space remaining in bucket, flush it now bucket.flush() bucket.append(input_list, callback_fn) @torch.no_grad() def flush(self) -> None: """Reduce-scatter any partial buckets.""" for bucket in self.buckets.values(): bucket.flush() @torch.no_grad() def free(self) -> None: """Free buffers from all buckets.""" for bucket in self.buckets.values(): bucket.free() @functools.lru_cache() def _get_shard_size(self, element_size: int, num_shards: int) -> int: if self.bucket_size_mb <= 0: # Values <= 0 disable bucketing. return 0 MB = 1024 * 1024 bucket_size = self.bucket_size_mb * MB / element_size return int(bucket_size // num_shards) def _get_bucket(self, tensor: Tensor, group: ProcessGroup) -> Bucket: key = (tensor.dtype, tensor.device, group) if key not in self.buckets: # buckets are divided into world_size pieces, bucket.data shaped (world_size, shard_size) world_size = group.size() shard_size = self._get_shard_size(tensor.element_size(), world_size) self.buckets[key] = Bucket(shard_size, tensor.dtype, tensor.device, group) self.buckets[key].alloc() return self.buckets[key]
import torch from colossalai.zero.sharded_model import ShardedModelV2 import copy def col_model_deepcopy(sharded_model: ShardedModelV2, other_model: torch.nn.Module): """ copy param of the ShardedModelV2 to other_model. Note the other_model has to be the same as self. """ for zero_param, param in zip(sharded_model.parameters(), other_model.parameters()): assert hasattr(zero_param, 'colo_attr') shard_flag = zero_param.colo_attr.sharded_data_tensor.is_sharded if shard_flag: sharded_model.shard_strategy.gather([zero_param.colo_attr.sharded_data_tensor]) param.data = copy.deepcopy(zero_param.colo_attr.data_payload) if shard_flag: sharded_model.shard_strategy.shard([zero_param.colo_attr.sharded_data_tensor])
import functools import itertools from collections import OrderedDict from copy import deepcopy from typing import Any, Iterator, Optional, Tuple import torch import torch.distributed as dist import torch.nn as nn from torch.distributed import ProcessGroup from torch.nn.parameter import Parameter from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.gemini.memory_tracer import MemStatsCollector, StaticMemStatsCollector from colossalai.gemini.ophooks import register_ophooks_recursively from colossalai.gemini.paramhooks import BaseParamHookMgr from colossalai.gemini.stateful_tensor import TensorState from colossalai.gemini.stateful_tensor_mgr import StatefulTensorMgr from colossalai.gemini.tensor_placement_policy import TensorPlacementPolicy, TensorPlacementPolicyFactory from colossalai.gemini.tensor_utils import colo_model_data_move_to_cpu from colossalai.logging import get_dist_logger from colossalai.utils import disposable, get_current_device from colossalai.utils.memory import colo_device_memory_capacity from colossalai.zero.shard_utils import BaseShardStrategy from colossalai.zero.sharded_model.reduce_scatter import ReduceScatterBucketer from colossalai.zero.utils import ZeroHook from ._utils import ( cast_float_arguments, cast_tensor_to_fp16, cast_tensor_to_fp32, chunk_and_pad, free_storage, get_gradient_predivide_factor, ) try: from torch.nn.modules.module import _EXTRA_STATE_KEY_SUFFIX except ImportError: _EXTRA_STATE_KEY_SUFFIX = '_extra_state' class ShardedModelV2(nn.Module): """ A wrapper for the PyTorch module shards the model parameters among multiple GPU memory. Only `1/#nproc` of parameters, gradients are stored in local CUDA memory, so forward and backward passes can be executed with limited CUDA memory budget. Note: You must use ``ShardedModelV2`` with ``ShardedOptimizerV2``. Note: Make sure you don't use gradient accumulation and your optimizer can work with fp16 gradient and fp32 parameter, if you enable ``reuse_fp16_shard``. Args: module (nn.Module): A sharded module, which must be initialized by `ZeroInitContext`. shard_strategy (BaseShardStrategy): A shard strategy to manage shard behavior. process_group (Optional[ProcessGroup], optional): Data parallel process group. Defaults to None. reduce_scatter_process_group (Optional[ProcessGroup], optional): Reduce-scatter process group. Generally, it should be `None`, and it's the same as `process_group`. Defaults to None. reduce_scatter_bucket_size_mb (int, optional): Reduce-scatter bucket size in *MB*. Defaults to 25. fp32_reduce_scatter (bool, optional): If set to `True`, gradients are forced to FP32 before reduce-scatter. Defaults to False. tensor_placement_policy (str): Which device to place *held* tensors. It can be 'cpu', 'cuda' and 'auto'. If it's 'cpu', parameters, gradients and optimizer states will be offloaded to CPU, which means min CUDA memory will be used. If it's 'cuda', they won't be offloaded, which means max CUDA memory will be used. If it's 'auto', they are moving dynamically based on CPU and CUDA memory usage. It will utilize heterogeneous memory space evenly and well. Note that 'auto' policy can only work well when no other processes use CUDA during your training. Defaults to 'cuda'. gradient_predivide_factor (Optional[float], optional): Gradient is divived by this value before reduce-scatter. Defaults to 1.0. reuse_fp16_shard (bool, optional): Whether to reuse fp16 shard for param and grad. Enabling this can reduce GPU memory usage, but you have to make sure you disable it when using gradient accumulation. In this mode, grad will be fp16. Make sure your optimizer supports mixed precision (fp32 param and fp16 grad). We find that PyTorch's optimizers don't support mixed precision, so we recommend you enable this only when using our CPUAdam with CPU offload. Defaults to False. """ def __init__(self, module: nn.Module, shard_strategy: BaseShardStrategy, process_group: Optional[ProcessGroup] = None, reduce_scatter_process_group: Optional[ProcessGroup] = None, reduce_scatter_bucket_size_mb: int = 25, fp32_reduce_scatter: bool = False, tensor_placement_policy: str = 'cuda', gradient_predivide_factor: Optional[float] = 1.0, reuse_fp16_shard: bool = False, *args, **kwargs): assert not isinstance(module, ShardedModelV2), 'Nested ShardedModelV2 is not supported.' super().__init__() self.logger = get_dist_logger() # We force users to use ZeroInitContext for submodule in module.modules(): sharded_cnt = 0 unshard_cnt = 0 for param in submodule.parameters(recurse=False): assert hasattr(param, 'colo_attr'), 'You must use ZeroInitContext to init your module first.' if param.colo_attr.param_is_sharded: sharded_cnt += 1 else: unshard_cnt += 1 assert (not sharded_cnt) or (not unshard_cnt), 'nn.Module can not both have shard param and unshard param' submodule.param_is_sharded = (sharded_cnt > 0) self.sharded_params = [] self.unshard_params = [] for param in module.parameters(): if param.colo_attr.param_is_sharded: self.sharded_params.append(param) else: self.unshard_params.append(param) self.module = module self.process_group = process_group or gpc.get_group(ParallelMode.DATA) self.reduce_scatter_process_group = reduce_scatter_process_group or self.process_group self.world_size = dist.get_world_size(self.process_group) self.rank = dist.get_rank(self.process_group) self.shard_strategy = shard_strategy self._use_memory_tracer = tensor_placement_policy == 'auto' if self._use_memory_tracer: self._memstats_collector = MemStatsCollector() self._start_collect_memstats = disposable(self._memstats_collector.start_collection) self._finish_collect_memstats = disposable(self._memstats_collector.finish_collection) else: self._memstats_collector = None self._tensor_placement_policy: TensorPlacementPolicy = TensorPlacementPolicyFactory.create( tensor_placement_policy)(mem_stats_collector=self._memstats_collector) if 'warmup_non_model_data_ratio' in kwargs: if tensor_placement_policy != 'auto': self.logger.warning('setting warmup_non_model_data_ratio is useless if not use auto placement') else: ratio = kwargs['warmup_non_model_data_ratio'] self._tensor_placement_policy._warmup_non_model_data_ratio = ratio self.logger.info(f'setting warmup_non_model_data_ratio as {ratio} for auto placement') self._stateful_tensor_mgr = StatefulTensorMgr(self._tensor_placement_policy) param_tensor_list = [p.colo_attr.sharded_data_tensor for p in module.parameters() if hasattr(p, 'colo_attr')] self._stateful_tensor_mgr.register_stateful_tensor_list(param_tensor_list) # Register hooks self._ophook_list = [ ZeroHook(self.shard_strategy, self._memstats_collector, self._stateful_tensor_mgr, self.process_group) ] register_ophooks_recursively(self.module, self._ophook_list) self.param_hook_mgr = BaseParamHookMgr(list(self.module.parameters())) self.param_hook_mgr.register_backward_hooks(self._grad_post_backward_hook) self.fp32_reduce_scatter = fp32_reduce_scatter self._cpu_offload: bool = tensor_placement_policy != 'cuda' for param in module.parameters(): # Init `offload_grad` param.colo_attr.offload_grad = self._cpu_offload # We find if gradient_predivide_factor != 1.0, there may be wrong precision problem # So we use 1.0 as the default gradient_predivide_factor # However, if you set gradient_predivide_factor to None, we will set # gradient_predivide_factor to a value >= 1.0 automatically self.gradient_predivide_factor: float = gradient_predivide_factor if \ gradient_predivide_factor is not None else \ get_gradient_predivide_factor(self.world_size) self.gradient_postdivide_factor: float = self.world_size / self.gradient_predivide_factor self.comm_stream: torch.cuda.Stream = torch.cuda.Stream() self.reducer = ReduceScatterBucketer(reduce_scatter_bucket_size_mb) self._require_backward_grad_sync: bool = True self._cuda_margin_space = 0 self.reuse_fp16_shard = reuse_fp16_shard # record whether gradients have inf or nan self.overflow_counter = 0 def adjust_stateful_tensor_layout(self) -> None: self._stateful_tensor_mgr.adjust_layout() @property def use_memory_tracer(self): return self._use_memory_tracer @property def cuda_margin_space(self): return self._cuda_margin_space @property def cpu_offload(self): return self._cpu_offload def dump_memory_stats(self, filename: Optional[str] = 'dump_mem_stats.log') -> None: """ dummy memory tracer collected infomation to a file. try: # forward: model(inputs) # backward: optimizer.backward() except Exception as e: model.dump_memory_stats() exit(0) """ if self._use_memory_tracer: self.logger.error(f'dump memort tracer collected infomation to a {filename}', ranks=[0]) if gpc.get_global_rank() == 0: with open(filename, 'w+') as f: f.write(f'cuda reserved {torch.cuda.memory_reserved(get_current_device()) / 1e9} GB\n') f.write(f'cuda max allocated {torch.cuda.max_memory_allocated(get_current_device()) / 1e9} GB\n') f.write('CUDA model data (GB)\n') f.write('\n') f.write('CUDA non model data (GB)\n') f.write(str(self._memstats_collector._memstats.non_model_data_list('cuda'))) f.write('CPU non model data (GB)\n') f.write(str(self._memstats_collector._memstats.non_model_data_list('cpu'))) f.write('\n') def _pre_forward_operations(self, *args): # the operation will affect the memory tracer behavior in ZeroHook if self._memstats_collector: self._start_collect_memstats() for p in self.module.parameters(): if hasattr(p, 'colo_attr'): p.colo_attr.sharded_data_tensor.trans_state(TensorState.HOLD) self._stateful_tensor_mgr.start_iter() def _post_forward_operations(self): for p in self.module.parameters(): if hasattr(p, 'colo_attr'): p.colo_attr.sharded_data_tensor.trans_state(TensorState.HOLD) def forward(self, *args: Any, **kwargs: Any) -> torch.Tensor: self._pre_forward_operations(*args) args, kwargs = cast_float_arguments(cast_tensor_to_fp16, *args, **kwargs) outputs = self.module(*args, **kwargs) self._post_forward_operations() return outputs def backward(self, loss): loss.backward() self._post_backward_operations() for ophook in self._ophook_list: ophook.post_iter() def backward_by_grad(self, tensor, grad): torch.autograd.backward(tensors=tensor, grad_tensors=grad) self._post_backward_operations() for ophook in self._ophook_list: ophook.post_iter() def _update_memstats(self): if self._memstats_collector: self._finish_collect_memstats() # cuda margin space = cuda mem capacity - max fwd/bwd cuda mem used. # the way to calculate margin space is based on the assumption that # model data is fixed in cuda during training. # cuda margin space can be used to store OS. self._cuda_margin_space = colo_device_memory_capacity( get_current_device()) - self._memstats_collector._memstats.max_overall_cuda @torch.no_grad() def _post_backward_operations(self) -> None: """ The method includes operations required to be processed after backward 1. update memory tracer. 2. flush the gradient in buckets. Reducing partial gradients in each process. 3. shard tensors not dealed in the zero hook 4. move sharded param grad payload to param.grad """ # 1. update memory tracer. self._update_memstats() # 2. flush the gradient in buckets. Reducing partial gradients in each process. if self._require_backward_grad_sync: # Flush any unreduced buckets in the post_backward stream. with torch.cuda.stream(self.comm_stream): self.reducer.flush() torch.cuda.current_stream().wait_stream(self.comm_stream) self.reducer.free() # 3. shard tensors not dealed in the zero hook tensor_list = [] for p in self.sharded_params: if not p.colo_attr.param_is_sharded: tensor_list.append(p.colo_attr.sharded_data_tensor) p.colo_attr.sharded_data_tensor.trans_state(TensorState.HOLD_AFTER_BWD) p.colo_attr.set_data_none() self.shard_strategy.shard(tensor_list, self.process_group) # 4. set all parameters' grad to None for p in self.module.parameters(): if not p.requires_grad: continue # Leave the gradient accumulation state (_require_backward_grad_sync) as-is if not synchronizing this pass. # NOTE() (no-sync)/sync pass: (not conduct)/conduct gradient allreducing between process group. # If _require_backward_grad_sync is True, # p.grad remains the accumulated unsharded gradient from prior no-sync passes. # We also allows to interleave no-sync pass with sync passes, if desired. if not self._require_backward_grad_sync: continue p.grad = None @torch.no_grad() def _grad_post_backward_hook(self, param: Parameter, grad: torch.Tensor) -> Optional[torch.Tensor]: """ At the start of :func:`_grad_post_backward_hook`, ``param.grad`` contains the full gradient for the local batch. The reduce-scatter op will save a single shard of the summed gradient across all GPUs to param.colo_attr.grad. This shard will align with the current GPU rank. For example:: before reduce_scatter: param.grad (GPU #0): [1, 2, 3, 4] param.grad (GPU #1): [5, 6, 7, 8] after reduce_scatter: param.grad (GPU #0): [6, 8] # 1+5, 2+6 param.grad (GPU #1): [10, 12] # 3+7, 4+8 The local GPU's ``optim.step`` is responsible for updating a single shard of params, also corresponding to the current GPU's rank. This alignment is created by `param.colo_attr.grad`, which ensures that the local optimizer only sees the relevant parameter shard. """ if grad is None: return assert not grad.requires_grad, 'ShardedModel only works with gradients that don\'t require gradients' if not self._require_backward_grad_sync: return # used to cheat Pytorch, since we can't return None empty_grad = torch.empty_like(grad) free_storage(empty_grad) # As torch didn't allow modifying grad in hook, we make a copy grad = grad.clone() if param.colo_attr.is_replicated: self._reduce_scatter_handler(param, grad) else: self._save_grad(param, grad) return empty_grad def _reduce_scatter_handler(self, param: Parameter, grad: torch.Tensor) -> None: self.comm_stream.wait_stream(torch.cuda.current_stream()) with torch.cuda.stream(self.comm_stream): if self.fp32_reduce_scatter: grad.data = grad.data.to(param.dtype) if self.gradient_predivide_factor > 1.0: # Average grad by world_size for consistency with PyTorch DDP. grad.data.div_(self.gradient_predivide_factor) if self.world_size > 1: grad_chunks = chunk_and_pad(grad, self.reduce_scatter_process_group.size()) self.reducer.reduce_scatter_async(grad_chunks, group=self.reduce_scatter_process_group, callback_fn=functools.partial(self._reduce_scatter_callback, param)) else: self._reduce_scatter_callback(param, grad) torch.cuda.current_stream().wait_stream(self.comm_stream) def _reduce_scatter_callback(self, param: Parameter, reduced_grad: torch.Tensor) -> None: assert isinstance(reduced_grad, torch.Tensor), f"_reduce_scatter_callback accept reduced_grad as {type(reduced_grad)}" reduced_grad.data = reduced_grad.data.contiguous().view(-1) if self.gradient_postdivide_factor > 1: # Average grad by world_size for consistency with PyTorch DDP. reduced_grad.data.div_(self.gradient_postdivide_factor) self._save_grad(param, reduced_grad) # FIXME(ver217): refactor the below line when impl eviction policy def _save_grad(self, param: Parameter, grad: torch.Tensor): # record whether we have overflow self.overflow_counter += torch.isinf(grad).any().item() self.overflow_counter += torch.isnan(grad).any().item() # move gradient to cpu if param.colo_attr.offload_grad: colo_model_data_move_to_cpu(grad) if self.reuse_fp16_shard: # make parameters point to gradient assert param.colo_attr.saved_grad.is_null( ), 'Gradien accumulation is not supported when reuse_fp16_shard=True' param.colo_attr.grad_payload_reset(grad.data) # release the memory of param # we set a false None for parameter's payload # so we can get paramter's device and dtype later in optimizer param.colo_attr.data_payload_reset(torch.empty(0, device=grad.device, dtype=grad.dtype)) if param.colo_attr.is_replicated: param.colo_attr.sharded_data_tensor.is_sharded = True else: fp32_grad = cast_tensor_to_fp32(grad) if param.colo_attr.saved_grad.is_null(): param.colo_attr.grad_payload_reset(fp32_grad) else: param.colo_attr.grad_payload.add_(fp32_grad.view_as(param.colo_attr.grad_payload)) # keep saved_grad in HOLD state param.colo_attr.saved_grad.trans_state(TensorState.HOLD) def parameters(self, recurse: bool = True) -> Iterator[Parameter]: return self.module.parameters(recurse=recurse) def named_parameters(self, prefix: str = '', recurse: bool = True) -> Iterator[Tuple[str, Parameter]]: return self.module.named_parameters(prefix, recurse) def state_dict(self, destination=None, prefix='', keep_vars=False) -> 'OrderedDict[str, torch.Tensor]': return self._colo_state_dict(destination, prefix, keep_vars, shard_strategy=self.shard_strategy, state_dict_func=nn.Module.state_dict, module_to_load=self.module, sharded_params=self.sharded_params, process_group=self.process_group) def load_state_dict(self, state_dict: 'OrderedDict[str, torch.Tensor]', strict: bool = True) -> None: for name, p in self.named_parameters(): if name in state_dict: p.colo_attr.data_payload_reset(state_dict[name].to(dtype=p.colo_attr.data_payload.dtype, device=p.colo_attr.data_payload.device)) # Force re-shard p.colo_attr.sharded_data_tensor.is_sharded = False self.shard_strategy.shard([p.colo_attr.sharded_data_tensor]) elif strict: raise RuntimeError(f'Missing key in state_dict: {name}') def _colo_state_dict(self, destination=None, prefix='', keep_vars=False, shard_strategy: Optional[BaseShardStrategy] = None, state_dict_func=None, module_to_load=None, sharded_params=[], process_group=None) -> 'OrderedDict[str, torch.Tensor]': if len(sharded_params) == 0: for param in self.parameters(): if param.colo_attr.param_is_sharded: sharded_params.append(param) if shard_strategy is not None: shard_strategy.gather([p.colo_attr.sharded_data_tensor for p in sharded_params], process_group) for p in sharded_params: p.data = p.colo_attr.data_payload module_to_load = module_to_load or self gathered_state_dict = state_dict_func(module_to_load, destination, prefix, keep_vars) gathered_state_dict = {k: v.cpu() if isinstance(v, torch.Tensor) else v for k, v in gathered_state_dict.items()} if shard_strategy is not None: shard_strategy.shard([p.colo_attr.sharded_data_tensor for p in sharded_params], process_group) for p in sharded_params: p.colo_attr.set_data_none() return gathered_state_dict def _colo_load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs, shard_strategy=None): r"""Copies parameters and buffers from :attr:`state_dict` into only this module, but not its descendants. This is called on every submodule in :meth:`~torch.nn.Module.load_state_dict`. Metadata saved for this module in input :attr:`state_dict` is provided as :attr:`local_metadata`. For state dicts without metadata, :attr:`local_metadata` is empty. Subclasses can achieve class-specific backward compatible loading using the version number at `local_metadata.get("version", None)`. .. note:: :attr:`state_dict` is not the same object as the input :attr:`state_dict` to :meth:`~torch.nn.Module.load_state_dict`. So it can be modified. Args: state_dict (dict): a dict containing parameters and persistent buffers. prefix (str): the prefix for parameters and buffers used in this module local_metadata (dict): a dict containing the metadata for this module. See strict (bool): whether to strictly enforce that the keys in :attr:`state_dict` with :attr:`prefix` match the names of parameters and buffers in this module missing_keys (list of str): if ``strict=True``, add missing keys to this list unexpected_keys (list of str): if ``strict=True``, add unexpected keys to this list error_msgs (list of str): error messages should be added to this list, and will be reported together in :meth:`~torch.nn.Module.load_state_dict` """ for hook in self._load_state_dict_pre_hooks.values(): hook(state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs) persistent_buffers = {k: v for k, v in self._buffers.items() if k not in self._non_persistent_buffers_set} local_name_params = itertools.chain(self._parameters.items(), persistent_buffers.items()) local_state = {k: v for k, v in local_name_params if v is not None} for name, param in local_state.items(): key = prefix + name if key in state_dict: input_param = state_dict[key] if hasattr(param, 'colo_attr'): param.colo_attr.data_payload_reset( input_param.to(dtype=param.colo_attr.data_payload.dtype, device=param.colo_attr.data_payload.device)) if shard_strategy is not None: # Force re-shard param.colo_attr.sharded_data_tensor.is_sharded = False shard_strategy.shard([param.colo_attr.sharded_data_tensor]) else: # This is used to avoid copying uninitialized parameters into # non-lazy modules, since they dont have the hook to do the checks # in such case, it will error when accessing the .shape attribute. is_param_lazy = torch.nn.parameter.is_lazy(param) # Backward compatibility: loading 1-dim tensor from 0.3.* to version 0.4+ if not is_param_lazy and len(param.shape) == 0 and len(input_param.shape) == 1: input_param = input_param[0] if not is_param_lazy and input_param.shape != param.shape: # local shape should match the one in checkpoint error_msgs.append('size mismatch for {}: copying a param with shape {} from checkpoint, ' 'the shape in current model is {}.'.format( key, input_param.shape, param.shape)) continue try: with torch.no_grad(): param.copy_(input_param) except Exception as ex: error_msgs.append('While copying the parameter named "{}", ' 'whose dimensions in the model are {} and ' 'whose dimensions in the checkpoint are {}, ' 'an exception occurred : {}.'.format(key, param.size(), input_param.size(), ex.args)) elif strict: missing_keys.append(key) extra_state_key = prefix + _EXTRA_STATE_KEY_SUFFIX if getattr(self.__class__, "set_extra_state", nn.Module.set_extra_state) is not nn.Module.set_extra_state: if extra_state_key in state_dict: self.set_extra_state(state_dict[extra_state_key]) elif strict: missing_keys.append(extra_state_key) elif strict and (extra_state_key in state_dict): unexpected_keys.append(extra_state_key) if strict: for key in state_dict.keys(): if key.startswith(prefix) and key != extra_state_key: input_name = key[len(prefix):] input_name = input_name.split('.', 1)[0] # get the name of param/buffer/child if input_name not in self._modules and input_name not in local_state: unexpected_keys.append(key) def __getitem__(self, idx: int): assert isinstance(self.module, nn.ModuleList) return self.module[idx] def __len__(self): assert isinstance(self.module, nn.ModuleList) return len(self.module) def __iter__(self): assert isinstance(self.module, nn.ModuleList) return iter(self.module)
from typing import Any, Callable, List, Tuple import torch import torch.nn.functional as F from typing import Union from colossalai.gemini.stateful_tensor import StatefulTensor def get_gradient_predivide_factor(world_size: int) -> float: factor: int = 1 while world_size % factor == 0 and world_size / factor > factor: factor *= 2 return float(factor) def free_storage(data: torch.Tensor) -> None: """Free underlying storage of a Tensor.""" if data.storage().size() > 0: # Since we're modifying the Tensor's Storage directly, make sure the Tensor # is the sole occupant of the Storage. assert data.storage_offset() == 0 data.storage().resize_(0) @torch.no_grad() def alloc_storage(data: torch.Tensor, size: torch.Size) -> None: """Allocate storage for a tensor.""" if data.storage().size() == size.numel(): # no need to reallocate return assert data.storage().size() == 0 data.storage().resize_(size.numel()) def cast_tensor_to_fp16(tensor: torch.Tensor) -> torch.Tensor: if isinstance(tensor, StatefulTensor): tensor = tensor.payload if torch.is_floating_point(tensor) and tensor.dtype is torch.float32: return tensor.half() return tensor def cast_tensor_to_fp32(tensor: Union[torch.Tensor, StatefulTensor]) -> torch.Tensor: if isinstance(tensor, StatefulTensor): tensor = tensor.payload if torch.is_floating_point(tensor) and tensor.dtype is torch.float16: return tensor.float() return tensor def apply_to_tensors(x: Any, fn: Callable): if torch.is_tensor(x): return fn(x) elif isinstance(x, list): return [apply_to_tensors(t, fn) for t in x] elif isinstance(x, tuple): return tuple(apply_to_tensors(t, fn) for t in x) elif isinstance(x, dict): return {key: apply_to_tensors(val, fn) for key, val in x.items()} else: return x def cast_float_arguments(fn: Callable, *args: Any, **kwargs: Any) -> Tuple[Any, Any]: return apply_to_tensors(args, fn), apply_to_tensors(kwargs, fn) def chunk_and_pad(tensor: torch.Tensor, num_chunks: int) -> List[torch.Tensor]: """Chunk a given Tensor into num_chunks parts and add any necessary padding.""" chunks = list(torch.flatten(tensor).chunk(num_chunks)) # torch.chunk may return fewer than num_chunks chunks, pad accordingly. num_pad_for_partial_chunk = chunks[0].numel() - chunks[-1].numel() if num_pad_for_partial_chunk > 0: chunks[-1] = F.pad(chunks[-1], [0, num_pad_for_partial_chunk]) if len(chunks) < num_chunks: chunks.extend([torch.zeros_like(chunks[0]) for _ in range(num_chunks - len(chunks))]) return chunks
#!/usr/bin/env python # -*- encoding: utf-8 -*- from .amp_type import AMP_TYPE from colossalai.context import Config import torch.nn as nn from torch.optim import Optimizer from torch.nn.modules.loss import _Loss from .torch_amp import convert_to_torch_amp from .apex_amp import convert_to_apex_amp from .naive_amp import convert_to_naive_amp __all__ = ['convert_to_amp', 'convert_to_naive_amp', 'convert_to_apex_amp', 'convert_to_torch_amp', 'AMP_TYPE'] def convert_to_amp(model: nn.Module, optimizer: Optimizer, criterion: _Loss, mode: AMP_TYPE, amp_config: Config = None): """A helper function to wrap training components with Torch AMP modules. Args: param model (:class:`torch.nn.Module`): your model object. optimizer (:class:`torch.optim.Optimizer`): your optimizer object. criterion (:class:`torch.nn.modules.loss._Loss`): your loss function object. mode (:class:`colossalai.amp.AMP_TYPE`): amp mode. amp_config (Union[:class:`colossalai.context.Config`, dict]): configuration for different amp modes. Returns: A tuple (model, optimizer, criterion). Note: ``amp_config`` may vary from different mode you choose. You should check the corresponding amp mode for more details about ``amp_config``. For ``apex_amp``, please check `apex_amp config <https://nvidia.github.io/apex/amp.html?highlight=apex%20amp>`_. For ``naive_amp``, please check `naive_amp config <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/amp/naive_amp/_fp16_optimizer.py#L42>`_. For ``torch_amp``, please check `torch_amp config <https://github.com/pytorch/pytorch/blob/master/torch/cuda/amp/grad_scaler.py#L97>`_. """ assert isinstance(mode, AMP_TYPE), \ f'expected the argument mode be AMP_TYPE, but got {type(mode)}' if amp_config is None: amp_config = Config() if mode == AMP_TYPE.TORCH: model, optimizer, criterion = convert_to_torch_amp(model, optimizer, criterion, amp_config) elif mode == AMP_TYPE.APEX: model, optimizer = convert_to_apex_amp(model, optimizer, amp_config) elif mode == AMP_TYPE.NAIVE: model, optimizer = convert_to_naive_amp(model, optimizer, amp_config) return model, optimizer, criterion
#!/usr/bin/env python # -*- encoding: utf-8 -*- from enum import Enum class AMP_TYPE(Enum): APEX = 'apex' TORCH = 'torch' NAIVE = 'naive'
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch import torch.distributed as dist from torch.distributed import ProcessGroup from torch.optim import Optimizer from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.kernel.op_builder import FusedOptimBuilder from colossalai.logging import get_dist_logger from colossalai.utils import clip_grad_norm_fp32, copy_tensor_parallel_attributes, multi_tensor_applier from ._utils import has_inf_or_nan, zero_gard_by_list from .grad_scaler import BaseGradScaler try: from colossalai._C import fused_optim except: fused_optim = None __all__ = ['FP16Optimizer'] def load_fused_optim(): global fused_optim if fused_optim is None: fused_optim = FusedOptimBuilder().load() def _multi_tensor_copy_this_to_that(this, that, overflow_buf=None): """ adapted from Megatron-LM (https://github.com/NVIDIA/Megatron-LM) Use multi-tensor-applier to copy values from one list to another. We don't have a blfoat16 implementation so for now if the overflow_buf is not provided, we default back to simple loop copy to be compatible with bfloat16. """ if overflow_buf: overflow_buf.fill_(0) # Scaling with factor `1.0` is equivalent to copy. global fused_optim load_fused_optim() multi_tensor_applier(fused_optim.multi_tensor_scale, overflow_buf, [this, that], 1.0) else: for this_, that_ in zip(this, that): that_.copy_(this_) class FP16Optimizer(Optimizer): """Float16 optimizer for fp16 and bf16 data types. Args: optimizer (torch.optim.Optimizer): base optimizer such as Adam or SGD grad_scaler (BaseGradScaler): grad scaler for gradient chose in ``constant_grad_scaler`` or ``dynamic_grad_scaler``. clip_grad_norm (float, optional): clip gradients with this global L2 norm. Default 0. Note that clipping is ignored if clip_grad == 0 verbose (bool, optional): if set to `True`, will print debug info. Default False. """ def __init__(self, optimizer: Optimizer, grad_scaler: BaseGradScaler, verbose: bool = False, clip_grad_norm=0, dp_process_group: ProcessGroup = None, mp_process_group: ProcessGroup = None): # have a defaults for compatibility with pytorch optim self._optimizer = optimizer self._defaults = optimizer.defaults # fp16-related params assert isinstance(grad_scaler, BaseGradScaler) self._grad_scaler = grad_scaler self._found_overflow = torch.cuda.FloatTensor([0.0]) self._dummy_overflow_buf = torch.cuda.IntTensor([0]) # misc params self._clip_grad_max_norm = clip_grad_norm # get process group def _get_process_group(parallel_mode): if gpc.is_initialized(parallel_mode) and gpc.get_world_size(parallel_mode): return gpc.get_group(parallel_mode) else: return None if dp_process_group is None: dp_process_group = _get_process_group(ParallelMode.DATA) if mp_process_group is None: mp_process_group = _get_process_group(ParallelMode.MODEL) self._dp_process_group = dp_process_group self._mp_process_group = mp_process_group # we maintain three groups of parameters # so that the model can have a mixture # of fp16 and fp32 params # fp16_param_groups: the fp16 params of the model # fp32_master_param_groups: the fp32 params cast from the fp16 param of the model # fp32_param_groups: the fp32 params of the model # NOTE: # 1. fp16_param_groups and fp32_master_param_groups have one-to-one correspondence # 2. fp32_param_groups and fp16_param_groups are exclusive of each other self._fp16_param_groups = [] self._fp32_master_param_groups = [] self._fp32_param_groups = [] # For all the groups in the original optimizer: for param_group in self._optimizer.param_groups: fp16_params = [] fp32_master_params = [] fp32_params = [] # For all the parameters in this group: for i, param in enumerate(param_group['params']): if param.requires_grad: # float16 params: if param.type() in ['torch.cuda.HalfTensor']: fp16_params.append(param) # Create a fp32 copy fp32_param = param.detach().clone().float() # Copy tensor model parallel attributes. copy_tensor_parallel_attributes(param, fp32_param) # Replace the optimizer params with the new fp32 copy. param_group['params'][i] = fp32_param fp32_master_params.append(fp32_param) # Reset existing state dict key to the new main param. if param in self._optimizer.state: self._optimizer.state[fp32_param] = self._optimizer.state.pop(param) # fp32 params. elif param.type() == 'torch.cuda.FloatTensor': fp32_params.append(param) else: raise TypeError('Expected parameter of type torch.cuda.FloatTensor ' f'or torch.cuda.HalfTensor, but got {param.type()}') self._fp16_param_groups.append(fp16_params) self._fp32_master_param_groups.append(fp32_master_params) self._fp32_param_groups.append(fp32_params) # Leverage state_dict() and load_state_dict() to # recast preexisting per-param state tensors self._optimizer.load_state_dict(self._optimizer.state_dict()) # log config self._logger = get_dist_logger() if verbose: self._logger.info( f"\n========= FP16 Optimizer Config =========\n" f"Optimizer: {optimizer.__class__.__name__}\n" f"clip_grad_norm = {clip_grad_norm}\n" f"grad_scaler = {self._grad_scaler.__class__.__name__}" f"==========================================", ranks=[0]) @property def max_norm(self): """Returns the maximum norm of gradient clipping. """ return self._clip_grad_max_norm @property def grad_scaler(self): """Returns the gradient scaler. Returns: :class:`BaseGradScaler`: gradient scaler. """ return self._grad_scaler @property def loss_scale(self): """Returns the loss scale. Returns: int: loss scale. """ return self._grad_scaler.scale @property def optimizer(self): """Returns the optimizer. Returns: :class:`torch.optim.Optimizer`: the optimizer object wrapped. """ return self._optimizer @property def defaults(self): """Returns the default arguments of optimizer. Returns: dict: optimizer arguments saved in defaults of the optimizer wrapped. """ return self._defaults def _check_overflow(self): # clear previous overflow record self._found_overflow.fill_(0.0) # check for overflow for group in self._optimizer.param_groups: for p in group['params']: if p.grad is not None and has_inf_or_nan(p.grad): self._found_overflow.fill_(1.0) break # all-reduce across dp group if self._dp_process_group: dist.all_reduce(self._found_overflow, op=dist.ReduceOp.MAX, group=self._dp_process_group) # all-reduce over model parallel group if self._mp_process_group: dist.all_reduce(self._found_overflow, op=dist.ReduceOp.MAX, group=self._mp_process_group) return self._found_overflow.item() > 0 def zero_grad(self, set_to_none=True): """Set gradient to zero. Args: set_to_none (bool): Whether set the gradient to None. """ # set_to_none = True can save some memory space for param_group in self._optimizer.param_groups: zero_gard_by_list(param_group['params'], set_to_none=set_to_none) def _get_fp32_param_groups_to_update(self): return self._fp32_master_param_groups + self._fp32_param_groups def _unscale_grads(self): for group in self._get_fp32_param_groups_to_update(): for p in group: if p.grad is not None: p.grad.data.div_(self.loss_scale) def _assign_grad_to_fp32_master_param(self): # This only needs to be done for the float16 group. for fp16_param_group, fp32_master_param_group in zip(self._fp16_param_groups, self._fp32_master_param_groups): for fp16_param, fp32_param in zip(fp16_param_group, fp32_master_param_group): if fp16_param.grad is not None: fp32_param.grad = fp16_param.grad.float() # clear unneeded grad on fp16 param fp16_param.grad = None def _update_fp16_param_from_fp32_param(self): fp16_param_data = [] fp32_master_param_data = [] for fp16_group, fp32_group in zip(self._fp16_param_groups, self._fp32_master_param_groups): for fp16_param, fp32_param in zip(fp16_group, fp32_group): fp16_param_data.append(fp16_param.data) fp32_master_param_data.append(fp32_param.data) _multi_tensor_copy_this_to_that(this=fp32_master_param_data, that=fp16_param_data, overflow_buf=self._dummy_overflow_buf) def step(self): """Update the model parameters. """ # Copy gradients from model params to main params. self._assign_grad_to_fp32_master_param() self._unscale_grads() overflow = self._check_overflow() self._grad_scaler.update(overflow) if overflow: self.zero_grad() # Clip the main gradients. grad_norm = None if self._clip_grad_max_norm > 0.0: grad_norm = self.clip_grad_norm(self._clip_grad_max_norm) if not overflow: # Step the optimizer. self._optimizer.step() # Update params from main params. self._update_fp16_param_from_fp32_param() # Successful update. return True, grad_norm else: return False, None def backward(self, loss): """Execute backward pass. Args: loss (:class:`torch.Tensor`): the loss value. """ scaled_loss = loss * self.grad_scaler.scale scaled_loss.backward() def state_dict(self): """Returns the states of the fp16 optimizer as a dict object. """ state_dict = {} state_dict['optimizer'] = self._optimizer.state_dict() if self.grad_scaler: state_dict['grad_scaler'] = self.grad_scaler.state_dict() state_dict['fp32_master_param_groups'] = self._fp32_master_param_groups return state_dict def load_state_dict(self, state_dict): """Load the states of the fp16 optimizer from a dict object. Args: state_dict (dict): the states of the fp16 optimizer """ # Optimizer. self._optimizer.load_state_dict(state_dict['optimizer']) # Grad scaler. if 'grad_scaler' in state_dict: self.grad_scaler.load_state_dict(state_dict['grad_scaler']) # Copy data for the main params. if 'fp32_master_param_groups' in state_dict: for current_group, ckpt_group in zip(self._fp32_master_param_groups, state_dict['fp32_master_param_groups']): for current_param, ckpt_param in zip(current_group, ckpt_group): current_param.data.copy_(ckpt_param.data) def clip_grad_norm(self, clip_grad): """Clip gradients by norm. Args: clip_grad (float): the max norm for clipping """ params = [] for param_group in self._optimizer.param_groups: for param in param_group['params']: params.append(param) return clip_grad_norm_fp32(params, clip_grad) # Promote state so it can be retrieved or set via # "optimizer_instance.state" def _get_state(self): return self._optimizer.state def _set_state(self, value): self._optimizer.state = value state = property(_get_state, _set_state) # Promote param_groups so it can be retrieved or set via # "optimizer_instance.param_groups" # (for example, to adjust the learning rate) def _get_param_groups(self): return self._optimizer.param_groups def _set_param_groups(self, value): self._optimizer.param_groups = value param_groups = property(_get_param_groups, _set_param_groups)
import inspect import torch.nn as nn from torch.optim import Optimizer from colossalai.utils import is_no_pp_or_last_stage from ._fp16_optimizer import FP16Optimizer from .grad_scaler import ConstantGradScaler, DynamicGradScaler from .naive_amp import NaiveAMPModel, NaiveAMPOptimizer def convert_to_naive_amp(model: nn.Module, optimizer: Optimizer, amp_config): """A helper function to wrap training components with naive AMP modules. In this mode, we forcibly cast the model weights and inputs to FP16, and cast the model outputs to FP32 to calculate loss, which is equivalent to Apex O3. Args: model (:class:`torch.nn.Module`): your model object optimizer (:class:`torch.optim.Optimizer`): your optimizer object amp_config (:class:`colossalai.context.Config` or dict): configuration for naive mode amp. Returns: Tuple: A tuple (model, optimizer) The ``amp_config`` should contain parameters below:: verbose (bool, optional): if set to `True`, will print debug info (Default: False). clip_grad_norm (float, optional): clip gradients with this global L2 norm (Default 0). Note that clipping is ignored if clip_grad == 0. dynamic_grad_scale (bool): whether to use dynamic grad scaler. """ if isinstance(model, nn.ModuleList): # interleaved pipeline module_list = [] for chunk, m in enumerate(model): output_to_fp32 = is_no_pp_or_last_stage() and chunk == len(model) - 1 module_list.append(NaiveAMPModel(m, output_to_fp32=output_to_fp32)) model = nn.ModuleList(module_list) else: output_to_fp32 = is_no_pp_or_last_stage() model = NaiveAMPModel(model, output_to_fp32=output_to_fp32) use_dynamic_grad_scaler = amp_config.pop('dynamic_grad_scale', True) if use_dynamic_grad_scaler: scaler_class = DynamicGradScaler else: scaler_class = ConstantGradScaler sig = inspect.signature(scaler_class.__init__) kwargs = dict() for param in sig.parameters.values(): if param.name in amp_config: kwargs[param.name] = amp_config.pop(param.name) grad_scaler = scaler_class(**kwargs) optimizer = NaiveAMPOptimizer(optimizer, grad_scaler, **amp_config) return model, optimizer __all__ = ['convert_to_naive_amp', 'NaiveAMPOptimizer', 'FP16Optimizer']
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import Any import torch import torch.distributed as dist import torch.nn as nn from torch import Tensor from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors from torch.distributed import ReduceOp from torch.optim import Optimizer from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.nn.optimizer import ColossalaiOptimizer from ._fp16_optimizer import FP16Optimizer class NaiveAMPOptimizer(ColossalaiOptimizer): """A wrapper class for optimizer to cast all parameters to fp16 Args: optim (torch.optim.Optimizer): A normal optimizer like Adam or SGD. grad_scaler (BaseGradScaler): grad scaler for gradient chose in ``constant_grad_scaler`` or ``dynamic_grad_scaler``. clip_grad_norm (float, optional): clip gradients with this global L2 norm. Default 0. verbose (bool, optional): if set to `True`, will print debug info. Default False. Note: clipping is ignored if ``clip_grad_norm`` equals 0. """ def __init__(self, optim: Optimizer, *args, **kwargs): optim = FP16Optimizer(optim, *args, **kwargs) super().__init__(optim) def backward(self, loss: Tensor): self.optim.backward(loss) def step(self): return self.optim.step() def clip_grad_norm(self, model: nn.Module, max_norm: float): if self.optim.max_norm == max_norm: return raise RuntimeError("NaiveAMP optimizer has clipped gradients during optimizer.step(). " "If you have supplied clip_grad_norm in the amp_config, " "executing the method clip_grad_norm is not allowed.") class NaiveAMPModel(nn.Module): r"""A wrapper class for model to cast the model into fp16 and automatically cast the input and output Args: model (torch.nn.Module): torch.nn.Module to be wrapped. output_to_fp32 (bool, optional): Whether cast output of this module into fp32. (Default: True) parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this module. (Default: ``ParallelMode.DATA``) sync_buffer (bool, optional): whether to synchronize buffer. (Default: True) Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ def __init__(self, model: nn.Module, output_to_fp32: bool = True, parallel_mode: ParallelMode = ParallelMode.DATA, sync_buffer: bool = True): super().__init__() self.model = model.half() self._output_to_fp32 = output_to_fp32 self._sync_buf = sync_buffer if gpc.is_initialized(parallel_mode) and gpc.get_world_size(parallel_mode) > 1: self._process_group = gpc.get_group(parallel_mode) self._world_size = gpc.get_world_size(parallel_mode) else: self._process_group = None self._world_size = 1 self._sync_buf = False self._first_eval_run = False @property def sync_buffer(self): return self._sync_buf @sync_buffer.setter def sync_buffer(self, state: bool): self._sync_buf = state def _convert_to_fp16(self, input_: Any): if isinstance(input_, Tensor) and input_.dtype == torch.float32: input_ = input_.half() return input_ def _convert_to_fp32(self, input_: Any): if isinstance(input_, Tensor) and input_.dtype == torch.float16: input_ = input_.float() return input_ def _reduce_module_buffer(self): """ All-reduce the buffers (e.g. running stats of batch normalization) across data parallel ranks so that all the ranks will produce consistent results when given the same input """ buf_list = [] # find valid buffers for buf in self.model.buffers(): if buf is not None: buf_list.append(buf) # reduce buffers across data parallel ranks if buf_list: coalesced_buf = _flatten_dense_tensors(buf_list) coalesced_buf.div_(self._world_size) dist.all_reduce(coalesced_buf, op=ReduceOp.SUM, group=self._process_group) unflattened_buf_list = _unflatten_dense_tensors(coalesced_buf, buf_list) for old, new in zip(buf_list, unflattened_buf_list): old.copy_(new) def eval(self): self.model.eval() # we only sync buffer in the first eval iteration # so that future eval iterations can be done without communication self._first_eval_run = True def forward(self, *args, **kwargs): # reduce buffers after forward will lead to error # as we cannot change the variables needed for gradient computation after forward # so we sync buffer before forward if (self.training or self._first_eval_run) and self._sync_buf: with torch.no_grad(): self._reduce_module_buffer() if self._first_eval_run: self._first_eval_run = False if args: args = [self._convert_to_fp16(arg) for arg in args] if kwargs: for k, v in kwargs.items(): kwargs[k] = self._convert_to_fp16(v) out = self.model(*args, **kwargs) if self._output_to_fp32: if isinstance(out, Tensor): out = self._convert_to_fp32(out) elif isinstance(out, (tuple, list)): out = [self._convert_to_fp32(val) for val in out] elif isinstance(out, dict): out = {key: self._convert_to_fp32(val) for key, val in out.items()} return out
from typing import List from torch import Tensor def has_inf_or_nan(tensor): """Check if tensor has inf or nan values. Args: tensor (:class:`torch.Tensor`): a torch tensor object Returns: bool: Whether the tensor has inf or nan. True for yes and False for no. """ try: # if tensor is half, the .float() incurs an additional deep copy, but it's necessary if # Pytorch's .sum() creates a one-element tensor of the same type as tensor # (which is true for some recent version of pytorch). tensor_sum = float(tensor.float().sum()) # More efficient version that can be used if .sum() returns a Python scalar # tensor_sum = float(tensor.sum()) except RuntimeError as instance: # We want to check if inst is actually an overflow exception. # RuntimeError could come from a different error. # If so, we still want the exception to propagate. if "value cannot be converted" not in instance.args[0]: raise return True else: if tensor_sum == float('inf') or tensor_sum == -float('inf') or tensor_sum != tensor_sum: return True return False def zero_gard_by_list(tensor_list: List[Tensor], set_to_none: bool = True) -> None: """Clear the gradient of a list of tensors, Note: copied from torch.optim.optimizer. """ for param in tensor_list: if param.grad is not None: if set_to_none: param.grad = None else: if param.grad.grad_fn is not None: param.grad.detach_() else: param.grad.requires_grad_(False) param.grad.zero_()
#!/usr/bin/env python # -*- encoding: utf-8 -*- from .base_grad_scaler import BaseGradScaler __all__ = ['ConstantGradScaler'] class ConstantGradScaler(BaseGradScaler): """A gradient scaler which uses constant loss scale Args: initial_scale (float): the initial loss scale verbose (bool): whether to log messages """ def __init__(self, initial_scale: int, verbose: bool): super().__init__(initial_scale, verbose) self.log(f"Constant Gradient Scaler is initialized with scale {self.scale}", ranks=[0]) def update(self, overflow: bool) -> None: """Do nothing to keep the loss scale constant. Args: overflow (bool): whether overflow occurs """ pass
from .base_grad_scaler import BaseGradScaler from .constant_grad_scaler import ConstantGradScaler from .dynamic_grad_scaler import DynamicGradScaler __all__ = ['BaseGradScaler', 'ConstantGradScaler', 'DynamicGradScaler']
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod from typing import Dict import torch from torch import Tensor from colossalai.logging import get_dist_logger __all__ = ['BaseGradScaler'] class BaseGradScaler(ABC): """A base class for the gradient scaler. Args: initial_scale (float): the initial loss scale verbose (bool): whether to log messages """ def __init__(self, initial_scale: float, verbose: bool): assert initial_scale > 0 self._scale = torch.cuda.FloatTensor([initial_scale]) self._verbose = verbose if self._verbose: self._logger = get_dist_logger() @property def scale(self) -> Tensor: """Returns the loss scale. """ return self._scale @property def inv_scale(self) -> Tensor: """Returns the inverse of the loss scale. """ return self._scale.double().reciprocal().float() def state_dict(self) -> Dict: """Returns the states of the gradient scaler as a dict object. """ state_dict = dict() state_dict['scale'] = self.scale return state_dict def load_state_dict(self, state_dict: Dict) -> None: """Load the states of the gradient scaler from a dict object. Args: state_dict (dict): the states of the gradient scaler """ self._scale = state_dict['scale'] @abstractmethod def update(self, overflow: bool) -> None: """Update the loss scale. Args: overflow (bool): whether overflow occurs """ pass def log(self, message, *args, **kwargs): """Log messages. Args: message (str): the message to log *args: positional arguments for :class:`colossalai.logging.DistributedLogger` **kwargs: key-word arguments for :class:`colossalai.logging.DistributedLogger` """ if self._verbose: self._logger.info(message, *args, **kwargs)
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import Optional import torch from .base_grad_scaler import BaseGradScaler __all__ = ['DynamicGradScaler'] class DynamicGradScaler(BaseGradScaler): """A gradient scaler which uses dynamic loss scale Args: initial_scale (float): the initial loss scale, defaults to 2**16 growth_factor (float): the multiplication factor for increasing loss scale, defaults to 2 backoff_factor (float): the multiplication factor for decreasing loss scale, defaults to 0.5 growth_interval (int): the number of steps to increase loss scale when no overflow occurs, defaults to 1000 min_scale (float): the minimum loss scale, defaults to None max_scale (float): the maximum loss scale, defaults to None hysteresis (int): the number of overflows before decreasing loss scale, defaults to 2 verbose (bool): whether to log messages, defaults to False """ def __init__(self, initial_scale: float = 2**16, growth_factor: float = 2, backoff_factor: float = 0.5, growth_interval: int = 1000, min_scale: Optional[float] = None, max_scale: Optional[float] = None, hysteresis: int = 2, verbose: bool = False): super().__init__(initial_scale, verbose) if min_scale: self._min_scale = torch.cuda.FloatTensor([min_scale]) else: self._min_scale = None if max_scale: self._max_scale = torch.cuda.FloatTensor([max_scale]) else: self._max_scale = None self._growth_factor = growth_factor self._backoff_factor = backoff_factor self._growth_interval = growth_interval self._growth_step = 0 self._hysteresis = hysteresis self._hysteresis_step = 0 self._sanity_checks() def _sanity_checks(self) -> None: """Check if the arguments are correct. """ if self._min_scale: assert self._min_scale > 0, 'The minimum gradient scale cannot be zero or negative' assert self._min_scale <= self._scale, 'The minimum gradient scale cannot be greater than the current scale' if self._max_scale: assert self._max_scale > 0, 'The maximum gradient scale cannot be zero or negative' assert self._max_scale >= self._scale, 'The maximum gradient scale cannot be smaller than the current scale' assert self._growth_factor > 1, 'The growth factor cannot be equal or smaller than 1' assert 0 < self._backoff_factor < 1, 'The backoff factor must be between 0 and 1' assert self._hysteresis >= 0, 'The hysteresis cannot be negative' def update(self, overflow: bool) -> None: """Update the loss scale. Args: overflow (bool): whether overflow occurs """ if overflow: self._hysteresis_step += 1 self._growth_step = 0 if self._hysteresis_step >= self._hysteresis: self._backoff_scale() self.log(f"Overflow occurs, the loss scale is adjusted to {self.scale.item()}", ranks=[0]) else: self._growth_step += 1 if self._growth_step == self._growth_interval: self._growth_step = 0 self._hysteresis_step = 0 self._grow_scale() self.log( f"No overflow for consecutive {self._growth_interval} steps, " f"the loss scale is adjusted to {self.scale.item()}", ranks=[0]) def _backoff_scale(self) -> None: """Decrease the loss scale """ self._scale = self._scale * self._backoff_factor if self._min_scale: self._scale = torch.max(self._scale, self._min_scale) def _grow_scale(self) -> None: """Increase the loss scale """ self._scale = self._scale * self._growth_factor if self._max_scale: self._scale = torch.min(self._scale, self._max_scale) def state_dict(self): state_dict = dict() state_dict['scale'] = self._scale state_dict['growth_factor'] = self._growth_factor state_dict['backoff_factor'] = self._backoff_factor state_dict['hysteresis'] = self._hysteresis return state_dict def load_state_dict(self, state_dict): self._scale = state_dict['scale'].cuda(torch.cuda.current_device()) self._growth_factor = state_dict['growth_factor'] self._backoff_factor = state_dict['backoff_factor'] self._hysteresis = state_dict['hysteresis']
#!/usr/bin/env python # -*- encoding: utf-8 -*- # modified from https://github.com/pytorch/pytorch/blob/master/torch/cuda/amp/grad_scaler.py # to support tensor parallel import warnings from collections import abc, defaultdict from enum import Enum from typing import Any, Dict, List, Optional, Tuple import torch import torch.distributed as dist from packaging import version from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors from colossalai.context import ParallelMode from colossalai.core import global_context as gpc class _MultiDeviceReplicator(object): """ Lazily serves copies of a tensor to requested devices. Copies are cached per-device. """ def __init__(self, master_tensor: torch.Tensor) -> None: assert master_tensor.is_cuda or master_tensor.device.type == 'xla' self.master = master_tensor self._per_device_tensors: Dict[torch.device, torch.Tensor] = {} def get(self, device) -> torch.Tensor: retval = self._per_device_tensors.get(device, None) if retval is None: retval = self.master.to(device=device, non_blocking=True, copy=True) self._per_device_tensors[device] = retval return retval # Defines default_factory for GradScaler's _per_optimizer_states defaultdict, # as well as associated "enum" values. Prefers defining these at top level because # - Lambdas can't be pickled, so we don't want to supply a lambda as the factory. # - Defining READY, UNSCALED, STEPPED and _refresh_per_optimizer_state within GradScaler # causes a circular reference, which we'd rather avoid. class OptState(Enum): READY = 0 UNSCALED = 1 STEPPED = 2 def _refresh_per_optimizer_state(): return {"stage": OptState.READY, "found_inf_per_device": {}} class GradScaler(object): _scale: Optional[torch.Tensor] _grows_tracker: Optional[torch.Tensor] _per_optimizer_states: Dict[int, Dict[str, Any]] """ An instance ``scaler`` of :class:`GradScaler` helps perform the steps of gradient scaling conveniently. * ``scaler.scale(loss)`` multiplies a given loss by ``scaler``'s current scale factor. * ``scaler.step(optimizer)`` safely unscales gradients and calls ``optimizer.step()``. * ``scaler.update()`` updates ``scaler``'s scale factor. Example: # Creates a GradScaler once at the beginning of training. scaler = GradScaler() for epoch in epochs: for input, target in data: optimizer.zero_grad() output = model(input) loss = loss_fn(output, target) # Scales loss. Calls backward() on scaled loss to create scaled gradients. scaler.scale(loss).backward() # scaler.step() first unscales gradients of the optimizer's params. # If gradients don't contain infs/NaNs, optimizer.step() is then called, # otherwise, optimizer.step() is skipped. scaler.step(optimizer) # Updates the scale for next iteration. scaler.update() See the :ref:`Automatic Mixed Precision examples<amp-examples>` for usage (along with autocasting) in more complex cases like gradient clipping, gradient accumulation, gradient penalty, and multiple losses/optimizers. ``scaler`` dynamically estimates the scale factor each iteration. To minimize gradient underflow, a large scale factor should be used. However, ``float16`` values can "overflow" (become inf or NaN) if the scale factor is too large. Therefore, the optimal scale factor is the largest factor that can be used without incurring inf or NaN gradient values. ``scaler`` approximates the optimal scale factor over time by checking the gradients for infs and NaNs during every ``scaler.step(optimizer)`` (or optional separate ``scaler.unscale_(optimizer)``, see :meth:`unscale_`). * If infs/NaNs are found, ``scaler.step(optimizer)`` skips the underlying ``optimizer.step()`` (so the params themselves remain uncorrupted) and ``update()`` multiplies the scale by ``backoff_factor``. * If no infs/NaNs are found, ``scaler.step(optimizer)`` runs the underlying ``optimizer.step()`` as usual. If ``growth_interval`` unskipped iterations occur consecutively, ``update()`` multiplies the scale by ``growth_factor``. The scale factor often causes infs/NaNs to appear in gradients for the first few iterations as its value calibrates. ``scaler.step`` will skip the underlying ``optimizer.step()`` for these iterations. After that, step skipping should occur rarely (once every few hundred or thousand iterations). Args: init_scale (float, optional, default=2.**16): Initial scale factor. growth_factor (float, optional, default=2.0): Factor by which the scale is multiplied during :meth:`update` if no inf/NaN gradients occur for ``growth_interval`` consecutive iterations. backoff_factor (float, optional, default=0.5): Factor by which the scale is multiplied during :meth:`update` if inf/NaN gradients occur in an iteration. growth_interval (int, optional, default=2000): Number of consecutive iterations without inf/NaN gradients that must occur for the scale to be multiplied by ``growth_factor``. enabled (bool, optional, default=True): If ``False``, disables gradient scaling. :meth:`step` simply invokes the underlying ``optimizer.step()``, and other methods become no-ops. """ def __init__(self, init_scale=2.**16, growth_factor=2.0, backoff_factor=0.5, growth_interval=2000, enabled=True): if enabled and not torch.cuda.is_available(): warnings.warn("torch.cuda.amp.GradScaler is enabled, but CUDA is not available. Disabling.") self._enabled = False else: self._enabled = enabled # check version torch_version = version.parse(torch.__version__) assert torch_version.major == 1 if torch_version.minor > 8: self._higher_than_torch18 = True else: self._higher_than_torch18 = False if self._enabled: assert growth_factor > 1.0, "The growth factor must be > 1.0." assert backoff_factor < 1.0, "The backoff factor must be < 1.0." self._init_scale = init_scale # self._scale will be lazily initialized during the first call to scale() self._scale = None self._growth_factor = growth_factor self._backoff_factor = backoff_factor self._growth_interval = growth_interval self._init_growth_tracker = 0 # self._growth_tracker will be lazily initialized during the first call to scale() self._growth_tracker = None self._per_optimizer_states = defaultdict(_refresh_per_optimizer_state) def _check_scale_growth_tracker(self, funcname) -> Tuple[torch.Tensor, torch.Tensor]: fix = "This may indicate your script did not use scaler.scale(loss or outputs) earlier in the iteration." assert self._scale is not None, "Attempted {} but _scale is None. ".format(funcname) + fix assert self._growth_tracker is not None, "Attempted {} but _growth_tracker is None. ".format(funcname) + fix return (self._scale, self._growth_tracker) def _lazy_init_scale_growth_tracker(self, dev): assert self._growth_tracker is None, "_growth_tracker initialized before _scale" self._scale = torch.full((1,), self._init_scale, dtype=torch.float32, device=dev) self._growth_tracker = torch.full((1,), self._init_growth_tracker, dtype=torch.int32, device=dev) def scale(self, outputs): """ Multiplies ('scales') a tensor or list of tensors by the scale factor. Returns scaled outputs. If this instance of :class:`GradScaler` is not enabled, outputs are returned unmodified. Args: outputs (Tensor or iterable of Tensors): Outputs to scale. """ if not self._enabled: return outputs # Short-circuit for the common case. if isinstance(outputs, torch.Tensor): assert outputs.is_cuda or outputs.device.type == 'xla' if self._scale is None: self._lazy_init_scale_growth_tracker(outputs.device) assert self._scale is not None return outputs * self._scale.to(device=outputs.device, non_blocking=True) # Invoke the more complex machinery only if we're treating multiple outputs. # holds a reference that can be overwritten by apply_scale stash: List[_MultiDeviceReplicator] = [] def apply_scale(val): if isinstance(val, torch.Tensor): assert val.is_cuda or val.device.type == 'xla' if len(stash) == 0: if self._scale is None: self._lazy_init_scale_growth_tracker(val.device) assert self._scale is not None stash.append(_MultiDeviceReplicator(self._scale)) return val * stash[0].get(val.device) elif isinstance(val, abc.Iterable): iterable = map(apply_scale, val) if isinstance(val, list) or isinstance(val, tuple): return type(val)(iterable) else: return iterable else: raise ValueError("outputs must be a Tensor or an iterable of Tensors") return apply_scale(outputs) def _unscale_grads_(self, optimizer, inv_scale, found_inf, allow_fp16): per_device_inv_scale = _MultiDeviceReplicator(inv_scale) per_device_found_inf = _MultiDeviceReplicator(found_inf) # To set up _amp_foreach_non_finite_check_and_unscale_, split grads by device and dtype. # There could be hundreds of grads, so we'd like to iterate through them just once. # However, we don't know their devices or dtypes in advance. # https://stackoverflow.com/questions/5029934/defaultdict-of-defaultdict # Google says mypy struggles with defaultdicts type annotations. per_device_and_dtype_grads = defaultdict(lambda: defaultdict(list)) # type: ignore[var-annotated] with torch.no_grad(): for group in optimizer.param_groups: for param in group["params"]: if param.grad is None: continue if (not allow_fp16) and param.grad.dtype == torch.float16: raise ValueError("Attempting to unscale FP16 gradients.") if param.grad.is_sparse: # is_coalesced() == False means the sparse grad has values with duplicate indices. # coalesce() deduplicates indices and adds all values that have the same index. # For scaled fp16 values, there's a good chance coalescing will cause overflow, # so we should check the coalesced _values(). if param.grad.dtype is torch.float16: param.grad = param.grad.coalesce() to_unscale = param.grad._values() else: to_unscale = param.grad # TODO: is there a way to split by device and dtype without appending in the inner loop? per_device_and_dtype_grads[to_unscale.device][to_unscale.dtype].append(to_unscale) for device, per_dtype_grads in per_device_and_dtype_grads.items(): for grads in per_dtype_grads.values(): torch._amp_foreach_non_finite_check_and_unscale_(grads, per_device_found_inf.get(device), per_device_inv_scale.get(device)) # For tensor parallel paramters it should be all-reduced over tensor parallel process group if gpc.is_initialized(ParallelMode.MODEL) and gpc.get_world_size(ParallelMode.MODEL) > 1: vals = [val for val in per_device_found_inf._per_device_tensors.values()] coalesced = _flatten_dense_tensors(vals) dist.all_reduce(coalesced, op=dist.ReduceOp.MAX, group=gpc.get_group(ParallelMode.MODEL)) for buf, synced in zip(vals, _unflatten_dense_tensors(coalesced, vals)): buf.copy_(synced) return per_device_found_inf._per_device_tensors def unscale_(self, optimizer): """ Divides ("unscales") the optimizer's gradient tensors by the scale factor. :meth:`unscale_` is optional, serving cases where you need to :ref:`modify or inspect gradients<working-with-unscaled-gradients>` between the backward pass(es) and :meth:`step`. If :meth:`unscale_` is not called explicitly, gradients will be unscaled automatically during :meth:`step`. Simple example, using :meth:`unscale_` to enable clipping of unscaled gradients:: ... scaler.scale(loss).backward() scaler.unscale_(optimizer) torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm) scaler.step(optimizer) scaler.update() Args: optimizer (torch.optim.Optimizer): Optimizer that owns the gradients to be unscaled. .. note:: :meth:`unscale_` does not incur a CPU-GPU sync. .. warning:: :meth:`unscale_` should only be called once per optimizer per :meth:`step` call, and only after all gradients for that optimizer's assigned parameters have been accumulated. Calling :meth:`unscale_` twice for a given optimizer between each :meth:`step` triggers a RuntimeError. .. warning:: :meth:`unscale_` may unscale sparse gradients out of place, replacing the ``.grad`` attribute. """ if not self._enabled: return self._check_scale_growth_tracker("unscale_") optimizer_state = self._per_optimizer_states[id(optimizer)] if optimizer_state["stage"] is OptState.UNSCALED: raise RuntimeError("unscale_() has already been called on this optimizer since the last update().") elif optimizer_state["stage"] is OptState.STEPPED: raise RuntimeError("unscale_() is being called after step().") # FP32 division can be imprecise for certain compile options, so we carry out the reciprocal in FP64. assert self._scale is not None inv_scale = self._scale.double().reciprocal().float() found_inf = torch.full((1,), 0.0, dtype=torch.float32, device=self._scale.device) optimizer_state["found_inf_per_device"] = self._unscale_grads_(optimizer, inv_scale, found_inf, False) optimizer_state["stage"] = OptState.UNSCALED def _maybe_opt_step(self, optimizer, optimizer_state, *args, **kwargs): retval = None if not sum(v.item() for v in optimizer_state["found_inf_per_device"].values()): retval = optimizer.step(*args, **kwargs) return retval def step(self, optimizer, *args, **kwargs): """ :meth:`step` carries out the following two operations: 1. Internally invokes ``unscale_(optimizer)`` (unless :meth:`unscale_` was explicitly called for ``optimizer`` earlier in the iteration). As part of the :meth:`unscale_`, gradients are checked for infs/NaNs. 2. If no inf/NaN gradients are found, invokes ``optimizer.step()`` using the unscaled gradients. Otherwise, ``optimizer.step()`` is skipped to avoid corrupting the params. ``*args`` and ``**kwargs`` are forwarded to ``optimizer.step()``. Returns the return value of ``optimizer.step(*args, **kwargs)``. Args: optimizer (torch.optim.Optimizer): Optimizer that applies the gradients. args: Any arguments. kwargs: Any keyword arguments. .. warning:: Closure use is not currently supported. """ if (not self._enabled): return optimizer.step(*args, **kwargs) if "closure" in kwargs: raise RuntimeError("Closure use is not currently supported if GradScaler is enabled.") self._check_scale_growth_tracker("step") optimizer_state = self._per_optimizer_states[id(optimizer)] if optimizer_state["stage"] is OptState.STEPPED: raise RuntimeError("step() has already been called since the last update().") retval = None if (hasattr(optimizer, "_step_supports_amp_scaling") and optimizer._step_supports_amp_scaling): # This optimizer has customized scale-handling logic, so we can call optimizer.step() directly. # The contract with custom optimizers is that their step() should accept an additional, # optional grad_scaler kwarg. We append self to the kwargs so the custom optimizer has full information: # it can query its own state, invoke unscale_ on itself, etc retval = optimizer.step(*args, **dict(kwargs, grad_scaler=self)) optimizer_state["stage"] = OptState.STEPPED return retval if optimizer_state["stage"] is OptState.READY: self.unscale_(optimizer) assert len(optimizer_state["found_inf_per_device"]) > 0, "No inf checks were recorded for this optimizer." retval = self._maybe_opt_step(optimizer, optimizer_state, *args, **kwargs) optimizer_state["stage"] = OptState.STEPPED return retval def update(self, new_scale=None): """ Updates the scale factor. If any optimizer steps were skipped the scale is multiplied by ``backoff_factor`` to reduce it. If ``growth_interval`` unskipped iterations occurred consecutively, the scale is multiplied by ``growth_factor`` to increase it. Passing ``new_scale`` sets the new scale value manually. (``new_scale`` is not used directly, it's used to fill GradScaler's internal scale tensor. So if ``new_scale`` was a tensor, later in-place changes to that tensor will not further affect the scale GradScaler uses internally.) Args: new_scale (float or :class:`torch.cuda.FloatTensor`, optional, default=None): New scale factor. .. warning:: :meth:`update` should only be called at the end of the iteration, after ``scaler.step(optimizer)`` has been invoked for all optimizers used this iteration. """ if not self._enabled: return _scale, _growth_tracker = self._check_scale_growth_tracker("update") if new_scale is not None: # Accept a new user-defined scale. if isinstance(new_scale, float): self._scale.fill_(new_scale) # type: ignore[union-attr] else: reason = "new_scale should be a float or a 1-element torch.cuda.FloatTensor with requires_grad=False." # type: ignore[attr-defined] assert isinstance(new_scale, torch.cuda.FloatTensor), reason assert new_scale.numel() == 1, reason assert new_scale.requires_grad is False, reason self._scale.copy_(new_scale) # type: ignore[union-attr] else: # Consume shared inf/nan data collected from optimizers to update the scale. # If all found_inf tensors are on the same device as self._scale, this operation is asynchronous. found_infs = [ found_inf.to(device=_scale.device, non_blocking=True) for state in self._per_optimizer_states.values() for found_inf in state["found_inf_per_device"].values() ] assert len(found_infs) > 0, "No inf checks were recorded prior to update." found_inf_combined = found_infs[0] if len(found_infs) > 1: for i in range(1, len(found_infs)): found_inf_combined += found_infs[i] if self._higher_than_torch18: torch._amp_update_scale_(_scale, _growth_tracker, found_inf_combined, self._growth_factor, self._backoff_factor, self._growth_interval) else: self._scale = torch._amp_update_scale(_growth_tracker, _scale, found_inf_combined, self._growth_factor, self._backoff_factor, self._growth_interval) # To prepare for next iteration, clear the data collected from optimizers this iteration. self._per_optimizer_states = defaultdict(_refresh_per_optimizer_state) def _get_scale_async(self): return self._scale def get_scale(self): """ Returns a Python float containing the current scale, or 1.0 if scaling is disabled. .. warning:: :meth:`get_scale` incurs a CPU-GPU sync. """ if self._enabled: return self._init_scale if self._scale is None else self._get_scale_async().item() else: return 1.0 def get_growth_factor(self): r""" Returns a Python float containing the scale growth factor. """ return self._growth_factor def set_growth_factor(self, new_factor): r""" Args: new_scale (float): Value to use as the new scale growth factor. """ self._growth_factor = new_factor def get_backoff_factor(self): r""" Returns a Python float containing the scale backoff factor. """ return self._backoff_factor def set_backoff_factor(self, new_factor): r""" Args: new_scale (float): Value to use as the new scale backoff factor. """ self._backoff_factor = new_factor def get_growth_interval(self): r""" Returns a Python int containing the growth interval. """ return self._growth_interval def set_growth_interval(self, new_interval): r""" Args: new_interval (int): Value to use as the new growth interval. """ self._growth_interval = new_interval def _get_growth_tracker(self): if self._enabled: return self._init_growth_tracker if self._growth_tracker is None else self._growth_tracker.item() else: return 0 def is_enabled(self): r""" Returns a bool indicating whether this instance is enabled. """ return self._enabled def state_dict(self): r""" Returns the state of the scaler as a :class:`dict`. It contains five entries: * ``"scale"`` - a Python float containing the current scale * ``"growth_factor"`` - a Python float containing the current growth factor * ``"backoff_factor"`` - a Python float containing the current backoff factor * ``"growth_interval"`` - a Python int containing the current growth interval * ``"_growth_tracker"`` - a Python int containing the number of recent consecutive unskipped steps. If this instance is not enabled, returns an empty dict. .. note:: If you wish to checkpoint the scaler's state after a particular iteration, :meth:`state_dict` should be called after :meth:`update`. """ return { "scale": self.get_scale(), "growth_factor": self._growth_factor, "backoff_factor": self._backoff_factor, "growth_interval": self._growth_interval, "_growth_tracker": self._get_growth_tracker() } if self._enabled else {} def load_state_dict(self, state_dict): r""" Loads the scaler state. If this instance is disabled, :meth:`load_state_dict` is a no-op. Args: state_dict(dict): scaler state. Should be an object returned from a call to :meth:`state_dict`. """ if not self._enabled: return if len(state_dict) == 0: raise RuntimeError("The source state dict is empty, possibly because it was saved " "from a disabled instance of GradScaler.") self._init_scale = state_dict["scale"] if self._scale is not None: self._scale.fill_(state_dict["scale"]) self._growth_factor = state_dict["growth_factor"] self._backoff_factor = state_dict["backoff_factor"] self._growth_interval = state_dict["growth_interval"] self._init_growth_tracker = state_dict["_growth_tracker"] if self._growth_tracker is not None: self._growth_tracker.fill_(state_dict["_growth_tracker"]) def __getstate__(self): state = self.__dict__.copy() if self._enabled: assert len(self._per_optimizer_states) == 0, "A GradScaler instance may only be pickled at the beginning "\ "of an iteration, or at the end after scaler.update()." # Pickling _scale and _growth_tracker Tensors directly triggers # "warnings.warn("pickle support for Storage will be removed in 1.5..." # so instead, we set the unpickled instance up to reinitialize them lazily. state['_init_scale'] = self.get_scale() state['_init_growth_tracker'] = self._get_growth_tracker() state['_scale'] = None state['_growth_tracker'] = None return state def __setstate__(self, state): self.__dict__.update(state) def _check_inf_per_device(self, optimizer): _scale, _ = self._check_scale_growth_tracker("_check_inf_per_device") dummy_inv_scale = torch.full((1,), 1.0, dtype=torch.float32, device=_scale.device) found_inf = torch.full((1,), 0.0, dtype=torch.float32, device=_scale.device) self._per_optimizer_states[id(optimizer)]["found_inf_per_device"] = \ self._unscale_grads_(optimizer, dummy_inv_scale, found_inf, True) return self._per_optimizer_states[id(optimizer)]["found_inf_per_device"] def _found_inf_per_device(self, optimizer): return self._per_optimizer_states[id(optimizer)]["found_inf_per_device"]
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.cuda.amp as torch_amp import torch.nn as nn from torch import Tensor from torch.nn.modules.loss import _Loss from torch.optim import Optimizer from colossalai.nn.optimizer import ColossalaiOptimizer from colossalai.utils import clip_grad_norm_fp32 from ._grad_scaler import GradScaler class TorchAMPOptimizer(ColossalaiOptimizer): """A wrapper class which integrate Pytorch AMP with an optimizer Args: optim (torch.optim.Optimizer): A normal optimizer like Adam or SGD. init_scale (float, optional, default=2.**16): Initial scale factor. growth_factor (float, optional, default=2.0): Factor by which the scale is multiplied during :meth:`update` if no inf/NaN gradients occur for ``growth_interval`` consecutive iterations. backoff_factor (float, optional, default=0.5): Factor by which the scale is multiplied during :meth:`update` if inf/NaN gradients occur in an iteration. growth_interval (int, optional, default=2000): Number of consecutive iterations without inf/NaN gradients that must occur for the scale to be multiplied by ``growth_factor``. enabled (bool, optional, default=True): If ``False``, disables gradient scaling. :meth:`step` simply invokes the underlying ``optimizer.step()``, and other methods become no-ops. """ def __init__(self, optim: Optimizer, *args, **kwargs): super().__init__(optim) self.scaler = GradScaler(*args, **kwargs) def backward(self, loss: Tensor): """Backward with torch amp gradient scaler Args: loss (torch.Tensor): Loss computed by a loss function """ self.scaler.scale(loss).backward() def step(self): """Update the parameters of the model """ self.scaler.step(self.optim) self.scaler.update() def clip_grad_norm(self, model: nn.Module, max_norm: float): """Apply gradient clipping to the model parameters Args: model (torch.nn.Module): Your model object max_norm (float): Max norm value for gradient clipping """ if max_norm > 0.0: self.scaler.unscale_(self.optim) clip_grad_norm_fp32(model.parameters(), max_norm) class TorchAMPModel(nn.Module): """A wrapper class for a model object which executes forward with values automatically cast to fp16 Args: model (:class:`torch.nn.Module`): a torch model instance """ def __init__(self, model: nn.Module) -> None: super().__init__() self.model = model @torch_amp.autocast() def forward(self, *args, **kwargs): """ Execute forward under the torch amp context """ return self.model(*args, **kwargs) class TorchAMPLoss(nn.Module): """A wrapper class for a criterion object which computes the loss in mixed-precision context Args: loss (torch.nn.modules.loss._Loss): A loss function object """ def __init__(self, loss: _Loss): super().__init__() self.loss = loss @torch_amp.autocast() def forward(self, *args, **kwargs): """ Execute forward under the torch amp context """ return self.loss(*args, **kwargs)
from typing import Optional import torch.nn as nn from torch.nn.modules.loss import _Loss from torch.optim import Optimizer from colossalai.context import Config from .torch_amp import TorchAMPLoss, TorchAMPModel, TorchAMPOptimizer def convert_to_torch_amp(model: nn.Module, optimizer: Optimizer, criterion: Optional[_Loss] = None, amp_config: Optional[Config] = None): """A helper function to wrap training components with Pytorch AMP modules Args: model (:class:`torch.nn.Module`): your model object. optimizer (:class:`torch.optim.Optimizer`): your optimizer object criterion (:class:`torch.nn.modules.loss._Loss`, optional): your loss function object amp_config (:class:`colossalai.context.Config` or dict, optional): configuration for Pytorch AMP. The ``amp_config`` should include parameters below: :: init_scale (float, optional, default=2.**16) growth_factor (float, optional, default=2.0) backoff_factor (float, optional, default=0.5) growth_interval (int, optional, default=2000) enabled (bool, optional, default=True) Returns: A tuple (model, optimizer, criterion) """ model = TorchAMPModel(model) if amp_config is None: amp_config = dict() optimizer = TorchAMPOptimizer(optimizer, **amp_config) if criterion: criterion = TorchAMPLoss(criterion) return model, optimizer, criterion __all__ = ['convert_to_torch_amp', 'TorchAMPModel', 'TorchAMPLoss', 'TorchAMPOptimizer']
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.nn as nn try: import apex.amp as apex_amp except ImportError: pass from torch import Tensor from colossalai.nn.optimizer import ColossalaiOptimizer from colossalai.utils import clip_grad_norm_fp32 class ApexAMPOptimizer(ColossalaiOptimizer): """ A wrapper class for APEX optimizer and it implements apex-specific backward and clip_grad_norm methods """ def backward(self, loss: Tensor): """Backward pass to get all gradients Args: loss (torch.Tensor): Loss computed by a loss function """ with apex_amp.scale_loss(loss, self.optim) as scaled_loss: scaled_loss.backward() def clip_grad_norm(self, model: nn.Module, max_norm: float): """Clip gradients by norm Args: model (torch.nn.Module): Your model object max_norm (float): The max norm value for gradient clipping """ if max_norm > 0: clip_grad_norm_fp32(apex_amp.master_params(self.optim), max_norm)
import torch.nn as nn from torch.optim import Optimizer from .apex_amp import ApexAMPOptimizer def convert_to_apex_amp(model: nn.Module, optimizer: Optimizer, amp_config): r"""A helper function to wrap training components with Apex AMP modules Args: model (:class:`torch.nn.Module`): your model object. optimizer (:class:`torch.optim.Optimizer`): your optimizer object. amp_config (Union[:class:`colossalai.context.Config`, dict]): configuration for initializing apex_amp. Returns: Tuple: A tuple (model, optimizer). The ``amp_config`` should include parameters below: :: enabled (bool, optional, default=True) opt_level (str, optional, default="O1") cast_model_type (``torch.dtype``, optional, default=None) patch_torch_functions (bool, optional, default=None) keep_batchnorm_fp32 (bool or str, optional, default=None master_weights (bool, optional, default=None) loss_scale (float or str, optional, default=None) cast_model_outputs (torch.dtype, optional, default=None) num_losses (int, optional, default=1) verbosity (int, default=1) min_loss_scale (float, default=None) max_loss_scale (float, default=2.**24) More details about ``amp_config`` refer to `amp_config <https://nvidia.github.io/apex/amp.html?highlight=apex%20amp>`_. """ import apex.amp as apex_amp model, optimizer = apex_amp.initialize(model, optimizer, **amp_config) optimizer = ApexAMPOptimizer(optimizer) return model, optimizer __all__ = ['convert_to_apex_amp', 'ApexAMPOptimizer']
#!/usr/bin/env python # -*- encoding: utf-8 -*- from types import ModuleType from typing import List class Registry: """This is a registry class used to register classes and modules so that a universal object builder can be enabled. Args: name (str): The name of the registry . third_party_library (list, optional): List of third party libraries which are used in the initialization of the register module. """ def __init__(self, name: str, third_party_library: List[ModuleType] = None): self._name = name self._registry = dict() self._third_party_lib = third_party_library @property def name(self): return self._name def register_module(self, module_class): """Registers a module represented in `module_class`. Args: module_class (class): The module to be registered. Returns: class: The module to be registered, so as to use it normally if via importing. Raises: AssertionError: Raises an AssertionError if the module has already been registered before. """ module_name = module_class.__name__ assert module_name not in self._registry, f"{module_name} not found in {self.name}" self._registry[module_name] = module_class # return so as to use it normally if via importing return module_class def get_module(self, module_name: str): """Retrieves a module with name `module_name` and returns the module if it has already been registered before. Args: module_name (str): The name of the module to be retrieved. Returns: :class:`object`: The retrieved module or None. Raises: NameError: Raises a NameError if the module to be retrieved has neither been registered directly nor as third party modules before. """ if module_name in self._registry: return self._registry[module_name] elif self._third_party_lib is not None: for lib in self._third_party_lib: if hasattr(lib, module_name): return getattr(lib, module_name) raise NameError(f'Module {module_name} not found in the registry {self.name}') def has(self, module_name: str): """Searches for a module with name `module_name` and returns a boolean value indicating whether the module has been registered directly or as third party modules before. Args: module_name (str): The name of the module to be searched for. Returns: bool: A boolean value indicating whether the module has been registered directly or as third party modules before. """ found_flag = module_name in self._registry if self._third_party_lib: for lib in self._third_party_lib: if hasattr(lib, module_name): found_flag = True break return found_flag
import torch.distributed.optim as dist_optim import torch.nn as nn import torch.optim as optim from .registry import Registry LAYERS = Registry("layers", third_party_library=[nn]) MODELS = Registry("models") OPTIMIZERS = Registry("optimizers", third_party_library=[optim, dist_optim]) DATASETS = Registry("datasets") DIST_GROUP_INITIALIZER = Registry("dist_group_initializer") GRADIENT_HANDLER = Registry("gradient_handler") LOSSES = Registry("losses", third_party_library=[nn]) HOOKS = Registry("hooks") TRANSFORMS = Registry("transforms") DATA_SAMPLERS = Registry("data_samplers") LR_SCHEDULERS = Registry("lr_schedulers") SCHEDULE = Registry("schedules") OPHOOKS = Registry("ophooks")
from .alpha_beta_profiler import AlphaBetaProfiler from .calc_pipeline_strategy import alpa_dp __all__ = ['AlphaBetaProfiler', 'alpa_dp']
import math import time from typing import Dict, List, Tuple import torch import torch.distributed as dist from colossalai.logging import get_dist_logger GB = int((1 << 30)) BYTE = 4 FRAMEWORK_LATENCY = 0 class AlphaBetaProfiler: ''' Profile alpha and beta value for a given device list. Usage: # Note: the environment of execution is supposed to be # multi-process with multi-gpu in mpi style. >>> physical_devices = [0, 1, 4, 5] >>> ab_profiler = AlphaBetaProfiler(physical_devices) >>> ab_dict = profiler.alpha_beta_dict >>> print(ab_dict) {(0, 1): (1.9641406834125518e-05, 4.74049549614719e-12), (0, 4): (1.9506998360157013e-05, 6.97421973297474e-11), (0, 5): (2.293858677148819e-05, 7.129930361393644e-11), (1, 4): (1.9010603427886962e-05, 7.077968863788975e-11), (1, 5): (1.9807778298854827e-05, 6.928845708992215e-11), (4, 5): (1.8681809306144713e-05, 4.7522367291330524e-12), (1, 0): (1.9641406834125518e-05, 4.74049549614719e-12), (4, 0): (1.9506998360157013e-05, 6.97421973297474e-11), (5, 0): (2.293858677148819e-05, 7.129930361393644e-11), (4, 1): (1.9010603427886962e-05, 7.077968863788975e-11), (5, 1): (1.9807778298854827e-05, 6.928845708992215e-11), (5, 4): (1.8681809306144713e-05, 4.7522367291330524e-12)} ''' def __init__(self, physical_devices: List[int], alpha_beta_dict: Dict[Tuple[int, int], Tuple[float, float]] = None, ctype: str = 'a', warmup: int = 5, repeat: int = 25, latency_iters: int = 5, homogeneous_tolerance: float = 0.1): ''' Args: physical_devices: A list of device id, each element inside it is the global rank of that device. alpha_beta_dict: A dict which maps a process group to alpha-beta value pairs. ctype: 'a' for all-reduce, 'b' for broadcast. warmup: Number of warmup iterations. repeat: Number of iterations to measure. latency_iters: Number of iterations to measure latency. ''' self.physical_devices = physical_devices self.ctype = ctype self.world_size = len(physical_devices) self.warmup = warmup self.repeat = repeat self.latency_iters = latency_iters self.homogeneous_tolerance = homogeneous_tolerance self.process_group_dict = None self._init_profiling() if alpha_beta_dict is None: self.alpha_beta_dict = self.profile_ab() else: self.alpha_beta_dict = alpha_beta_dict def _init_profiling(self): # Create process group list based on its global rank process_group_list = [] for f_index in range(self.world_size - 1): for b_index in range(f_index + 1, self.world_size): process_group_list.append((self.physical_devices[f_index], self.physical_devices[b_index])) # Create process group dict which maps process group to its handler process_group_dict = {} for process_group in process_group_list: pg_handler = dist.new_group(process_group) process_group_dict[process_group] = pg_handler self.process_group_dict = process_group_dict def _profile(self, process_group, pg_handler, nbytes): logger = get_dist_logger() rank = dist.get_rank() src_device_num = process_group[0] world_size = len(process_group) device = torch.cuda.current_device() buf = torch.randn(nbytes // 4).to(device) torch.cuda.synchronize() # warmup for _ in range(self.warmup): if self.ctype == "a": dist.all_reduce(buf, op=dist.ReduceOp.SUM, group=pg_handler) elif self.ctype == "b": dist.broadcast(buf, src=src_device_num, group=pg_handler) torch.cuda.synchronize() dist.barrier(group=pg_handler) begin = time.perf_counter() for _ in range(self.repeat): if self.ctype == "a": dist.all_reduce(buf, op=dist.ReduceOp.SUM, group=pg_handler) elif self.ctype == "b": dist.broadcast(buf, src=src_device_num, group=pg_handler) torch.cuda.synchronize() end = time.perf_counter() dist.barrier(group=pg_handler) if rank == src_device_num: avg_time_s = (end - begin) / self.repeat - FRAMEWORK_LATENCY alg_band = nbytes / avg_time_s if self.ctype == "a": # convert the bandwidth of all-reduce algorithm to the bandwidth of the hardware. bus_band = 2 * (world_size - 1) / world_size * alg_band bus_band = alg_band elif self.ctype == "b": bus_band = alg_band logger.info( f"GPU:{rank}, Bytes: {nbytes} B,Time: {round(avg_time_s * 1e6,2)} us, Bus bandwidth: {round(bus_band / GB,2)} GB/s" ) return (avg_time_s, alg_band) else: # Just a placeholder return (None, None) def profile_latency(self, process_group, pg_handler): ''' This function is used to profile the latency of the given process group with a series of bytes. Args: process_group: A tuple of global rank of the process group. pg_handler: The handler of the process group. Returns: latency: None if the latency is not measured, otherwise the median of the latency_list. ''' latency_list = [] for i in range(self.latency_iters): nbytes = int(BYTE << i) (t, _) = self._profile(process_group, pg_handler, nbytes) latency_list.append(t) if latency_list[0] is None: latency = None else: median_index = math.floor(self.latency_iters / 2) latency = latency_list[median_index] return latency def profile_bandwidth(self, process_group, pg_handler, maxbytes=(1 * GB)): ''' This function is used to profile the bandwidth of the given process group. Args: process_group: A tuple of global rank of the process group. pg_handler: The handler of the process group. ''' (_, bandwidth) = self._profile(process_group, pg_handler, maxbytes) return bandwidth def profile_ab(self): ''' This method is used to profiling the alpha and beta value for a given device list. Returns: alpha_beta_dict: A dict which maps process group to its alpha and beta value. ''' alpha_beta_dict: Dict[Tuple[int], Tuple[float]] = {} rank = dist.get_rank() global_pg_handler = dist.new_group(self.physical_devices) def get_max_nbytes(process_group: Tuple[int], pg_handler: dist.ProcessGroup): assert rank in process_group device = torch.cuda.current_device() rank_max_nbytes = torch.cuda.mem_get_info(device)[0] rank_max_nbytes = torch.tensor(rank_max_nbytes, device=device) dist.all_reduce(rank_max_nbytes, op=dist.ReduceOp.MIN, group=pg_handler) max_nbytes = min(int(1 * GB), int(GB << int(math.log2(rank_max_nbytes.item() / GB)))) return max_nbytes for process_group, pg_handler in self.process_group_dict.items(): if rank not in process_group: max_nbytes = None alpha = None bandwidth = None else: max_nbytes = get_max_nbytes(process_group, pg_handler) alpha = self.profile_latency(process_group, pg_handler) bandwidth = self.profile_bandwidth(process_group, pg_handler, maxbytes=max_nbytes) if bandwidth is None: beta = None else: beta = 1 / bandwidth broadcast_list = [alpha, beta] dist.broadcast_object_list(broadcast_list, src=process_group[0]) alpha_beta_dict[process_group] = tuple(broadcast_list) # add symmetry pair to the apha_beta_dict symmetry_ab_dict = {} for process_group, alpha_beta_pair in alpha_beta_dict.items(): symmetry_process_group = (process_group[1], process_group[0]) symmetry_ab_dict[symmetry_process_group] = alpha_beta_pair alpha_beta_dict.update(symmetry_ab_dict) return alpha_beta_dict def search_best_logical_mesh(self): ''' This method is used to search the best logical mesh for the given device list. The best logical mesh is searched in following steps: 1. detect homogeneous device groups, we assume that the devices in the alpha_beta_dict are homogeneous if the beta value is close enough. 2. Find the best homogeneous device group contains all the physical devices. The best homogeneous device group means the lowest beta value in the groups which contains all the physical devices. And the reason we require the group contains all the physical devices is that the devices not in the group will decrease the bandwidth of the group. 3. If the best homogeneous device group is found, we will construct the largest ring for each device based on the best homogeneous device group, and the best logical mesh will be the union of all the rings. Otherwise, the best logical mesh will be the balanced logical mesh, such as shape (2, 2) for 4 devices. Returns: best_logical_mesh: The best logical mesh for the given device list. Usage: >>> physical_devices = [0, 1, 2, 3] >>> ab_profiler = AlphaBetaProfiler(physical_devices) >>> best_logical_mesh = profiler.search_best_logical_mesh() >>> print(best_logical_mesh) [[0, 1], [2, 3]] ''' def _power_of_two(integer): return integer & (integer - 1) == 0 def _detect_homogeneous_device(alpha_beta_dict): ''' This function is used to detect whether the devices in the alpha_beta_dict are homogeneous. Note: we assume that the devices in the alpha_beta_dict are homogeneous if the beta value of the devices are in range of [(1 - self.homogeneous_tolerance), (1 + self.homogeneous_tolerance)] * base_beta. ''' homogeneous_device_dict: Dict[float, List[Tuple[int]]] = {} for process_group, (_, beta) in alpha_beta_dict.items(): if homogeneous_device_dict is None: homogeneous_device_dict[beta] = [] homogeneous_device_dict[beta].append(process_group) match_beta = None for beta_value in homogeneous_device_dict.keys(): if beta <= beta_value * (1 + self.homogeneous_tolerance) and beta >= beta_value * ( 1 - self.homogeneous_tolerance): match_beta = beta_value break if match_beta is not None: homogeneous_device_dict[match_beta].append(process_group) else: homogeneous_device_dict[beta] = [] homogeneous_device_dict[beta].append(process_group) return homogeneous_device_dict def _check_contain_all_devices(homogeneous_group: List[Tuple[int]]): ''' This function is used to check whether the homogeneous_group contains all physical devices. ''' flatten_mesh = [] for process_group in homogeneous_group: flatten_mesh.extend(process_group) non_duplicated_flatten_mesh = set(flatten_mesh) return len(non_duplicated_flatten_mesh) == len(self.physical_devices) def _construct_largest_ring(homogeneous_group: List[Tuple[int]]): ''' This function is used to construct the largest ring in the homogeneous_group for each rank. ''' # Construct the ring ring = [] ranks_in_ring = [] for rank in self.physical_devices: if rank in ranks_in_ring: continue stable_status = False ring_for_rank = [] ring_for_rank.append(rank) check_rank_list = [rank] rank_to_check_list = [] while not stable_status: stable_status = True check_rank_list.extend(rank_to_check_list) rank_to_check_list = [] for i in range(len(check_rank_list)): check_rank = check_rank_list.pop() for process_group in homogeneous_group: if check_rank in process_group: rank_to_append = process_group[0] if process_group[1] == check_rank else process_group[1] if rank_to_append not in ring_for_rank: stable_status = False rank_to_check_list.append(rank_to_append) ring_for_rank.append(rank_to_append) ring.append(ring_for_rank) ranks_in_ring.extend(ring_for_rank) return ring assert _power_of_two(self.world_size) power_of_two = int(math.log2(self.world_size)) median = power_of_two // 2 balanced_logical_mesh_shape = (2**median, 2**(power_of_two - median)) row_size, column_size = balanced_logical_mesh_shape[0], balanced_logical_mesh_shape[1] balanced_logical_mesh = [] for row_index in range(row_size): balanced_logical_mesh.append([]) for column_index in range(column_size): balanced_logical_mesh[row_index].append(self.physical_devices[row_index * column_size + column_index]) homogeneous_device_dict = _detect_homogeneous_device(self.alpha_beta_dict) beta_list = [b for b in homogeneous_device_dict.keys()] beta_list.sort() beta_list.reverse() homogeneous_types = len(beta_list) best_logical_mesh = None if homogeneous_types >= 2: for _ in range(homogeneous_types - 1): lowest_beta = beta_list.pop() best_homogeneous_group = homogeneous_device_dict[lowest_beta] # if the best homogeneous group contains all physical devices, # we will build the logical device mesh based on it. Otherwise, # we will check next level homogeneous group. if _check_contain_all_devices(best_homogeneous_group): # We choose the largest ring for each rank to maximum the best bus utilization. best_logical_mesh = _construct_largest_ring(best_homogeneous_group) break if homogeneous_types == 1 or best_logical_mesh is None: # in this case, we use balanced logical mesh as the best # logical mesh. best_logical_mesh = balanced_logical_mesh return best_logical_mesh def extract_alpha_beta_for_device_mesh(self): ''' Extract the mesh_alpha list and mesh_beta list based on the best logical mesh, which will be used to initialize the device mesh. Usage: >>> physical_devices = [0, 1, 2, 3] >>> ab_profiler = AlphaBetaProfiler(physical_devices) >>> mesh_alpha, mesh_beta = profiler.extract_alpha_beta_for_device_mesh() >>> print(mesh_alpha) [2.5917552411556242e-05, 0.00010312341153621673] >>> print(mesh_beta) [5.875573704655635e-11, 4.7361584445959614e-12] ''' best_logical_mesh = self.search_best_logical_mesh() first_axis = [row[0] for row in best_logical_mesh] second_axis = best_logical_mesh[0] # init process group for both axes first_axis_process_group = dist.new_group(first_axis) second_axis_process_group = dist.new_group(second_axis) # extract alpha and beta for both axes def _extract_alpha_beta(pg, pg_handler): latency = self.profile_latency(pg, pg_handler) bandwidth = self.profile_bandwidth(pg, pg_handler) broadcast_object = [latency, bandwidth] dist.broadcast_object_list(broadcast_object, src=pg[0]) return broadcast_object first_latency, first_bandwidth = _extract_alpha_beta(first_axis, first_axis_process_group) second_latency, second_bandwidth = _extract_alpha_beta(second_axis, second_axis_process_group) mesh_alpha = [first_latency, second_latency] # The beta values have been enlarged by 1e10 times temporarilly because the computation cost # is still estimated in the unit of TFLOPs instead of time. We will remove this factor in future. mesh_beta = [1e10 / first_bandwidth, 1e10 / second_bandwidth] return mesh_alpha, mesh_beta
from math import pow import numpy as np def get_submesh_choices(num_hosts, num_devices_per_host, mode="new"): submesh_choices = [] i = 1 p = -1 while i <= num_devices_per_host: i *= 2 p += 1 assert pow(2, p) == num_devices_per_host, ("Only supports the cases where num_devices_per_host is power of two, " f"while now num_devices_per_host = {num_devices_per_host}") if mode == "alpa": for i in range(p + 1): submesh_choices.append((1, pow(2, i))) for i in range(2, num_hosts + 1): submesh_choices.append((i, num_devices_per_host)) elif mode == "new": for i in range(p // 2 + 1): for j in range(i, p - i + 1): submesh_choices.append((pow(2, i), pow(2, j))) return submesh_choices def alpa_dp_impl(num_layers, num_devices, num_microbatches, submesh_choices, compute_cost, max_stage_cost, best_configs): """Implementation of Alpa DP for pipeline strategy Paper reference: https://www.usenix.org/system/files/osdi22-zheng-lianmin.pdf Arguments: num_layers: K num_devices: N*M num_microbatches: B submesh_choices: List[(n_i,m_i)] compute_cost: t_intra """ # For f, layer ID start from 0 # f[#pipeline stages, layer id that is currently being considered, number of devices used] f = np.full((num_layers + 1, num_layers + 1, num_devices + 1), np.inf, dtype=np.float32) f_stage_max = np.full((num_layers + 1, num_layers + 1, num_devices + 1), 0.0, dtype=np.float32) f_argmin = np.full((num_layers + 1, num_layers + 1, num_devices + 1, 3), -1, dtype=np.int32) f[0, num_layers, 0] = 0 for s in range(1, num_layers + 1): for k in range(num_layers - 1, -1, -1): for d in range(1, num_devices + 1): for m, submesh in enumerate(submesh_choices): n_submesh_devices = np.prod(np.array(submesh)) if n_submesh_devices <= d: # TODO: [luzgh]: Why alpa needs max_n_succ_stages? Delete. # if s - 1 <= max_n_succ_stages[i, k - 1, m, n_config]: # ... for i in range(num_layers, k, -1): stage_cost = compute_cost[k, i, m] new_cost = f[s - 1, k, d - n_submesh_devices] + stage_cost if (stage_cost <= max_stage_cost and new_cost < f[s, k, d]): f[s, k, d] = new_cost f_stage_max[s, k, d] = max(stage_cost, f_stage_max[s - 1, i, d - n_submesh_devices]) f_argmin[s, k, d] = (i, m, best_configs[k, i, m]) best_s = -1 best_total_cost = np.inf for s in range(1, num_layers + 1): if f[s, 0, num_devices] < best_total_cost: best_s = s best_total_cost = f[s, 0, num_devices] if np.isinf(best_total_cost): return np.inf, None total_cost = f[best_s, 0, num_devices] + (num_microbatches - 1) * f_stage_max[best_s, 0, num_devices] current_s = best_s current_layer = 0 current_devices = num_devices res = [] while current_s > 0 and current_layer < num_layers and current_devices > 0: next_start_layer, submesh_choice, autosharding_choice = (f_argmin[current_s, current_layer, current_devices]) assert next_start_layer != -1 and current_devices != -1 res.append(((current_layer, next_start_layer), submesh_choice, autosharding_choice)) current_s -= 1 current_layer = next_start_layer current_devices -= np.prod(np.array(submesh_choices[submesh_choice])) assert (current_s == 0 and current_layer == num_layers and current_devices == 0) return total_cost, res def alpa_dp(num_layers, num_devices, num_microbatches, submesh_choices, num_autosharding_configs, compute_cost, gap=1e-6): """Alpa auto stage dynamic programming. Code reference: https://github.com/alpa-projects/alpa/blob/main/alpa/pipeline_parallel/stage_construction.py Arguments: submesh_choices: List[(int,int)] num_autosharding_configs: Max number of t_intra(start_layer, end_layer, LogicalMesh) compute_cost: np.array(num_layers,num_layers,num_submesh_choices,num_autosharding_configs) """ assert np.shape(compute_cost) == (num_layers, num_layers, len(submesh_choices), num_autosharding_configs), "Cost shape wrong." all_possible_stage_costs = np.sort(np.unique(compute_cost)) best_cost = np.inf best_solution = None last_max_stage_cost = 0.0 # TODO: [luzgh]: Why alpa needs the num_autosharding_configs dimension in compute_cost? # In dp_impl it seems the argmin n_config will be chosen. Just amin here. best_configs = np.argmin(compute_cost, axis=3) best_compute_cost = np.amin(compute_cost, axis=3) assert len(all_possible_stage_costs), "no solution in auto stage construction." for max_stage_cost in all_possible_stage_costs: if max_stage_cost * num_microbatches >= best_cost: break if max_stage_cost - last_max_stage_cost < gap: continue cost, solution = alpa_dp_impl(num_layers, num_devices, num_microbatches, submesh_choices, best_compute_cost, max_stage_cost, best_configs) if cost < best_cost: best_cost = cost best_solution = solution last_max_stage_cost = max_stage_cost return best_cost, best_solution
import operator from functools import reduce from typing import List, Tuple import torch import torch.distributed as dist class DeviceMesh: """A logical view of a physical mesh. The logical view is used in the search process. A physical mesh can have multiple logical views. (e.g., a 2x8 physical mesh can be viewed as a 1x16 or a 4x4 logical mesh). Each mesh dimension has its own latency and bandwidth. We use alpha-beta model to model the communication cost. Arguments: physical_mesh_id (torch.Tensor): physical view of the devices in global rank. logical_mesh_id (torch.Tensor): logical view of the devices in global rank. mesh_shape (torch.Size, optional): shape of logical view. mesh_alpha (List[float], optional): coefficients used for computing communication cost (default: None) mesh_beta (List[float], optional): coefficients used for computing communication cost (default: None) init_process_group (bool, optional): initialize logical process group during initializing the DeviceMesh instance if the init_process_group set to True. Otherwise, users need to call create_process_groups_for_logical_mesh manually to init logical process group. (default: False) need_flatten(bool, optional): initialize flatten_device_mesh during initializing the DeviceMesh instance if the need_flatten set to True. """ def __init__(self, physical_mesh_id: torch.Tensor, mesh_shape: torch.Size = None, logical_mesh_id: torch.Tensor = None, mesh_alpha: List[float] = None, mesh_beta: List[float] = None, init_process_group: bool = False, need_flatten: bool = True): self.physical_mesh_id = physical_mesh_id if logical_mesh_id is None: self.mesh_shape = mesh_shape self._logical_mesh_id = self.physical_mesh_id.reshape(self.mesh_shape) else: self._logical_mesh_id = logical_mesh_id self.mesh_shape = self._logical_mesh_id.shape # map global rank into logical rank self.convert_map = {} self._global_rank_to_logical_rank_map(self._logical_mesh_id, []) # coefficient for alpha-beta communication model if mesh_alpha is None: mesh_alpha = [1] * len(self.mesh_shape) if mesh_beta is None: mesh_beta = [1] * len(self.mesh_shape) self.mesh_alpha = tuple(mesh_alpha) self.mesh_beta = tuple(mesh_beta) self.init_process_group = init_process_group self.need_flatten = need_flatten if self.init_process_group: self.process_groups_dict = self.create_process_groups_for_logical_mesh() if self.need_flatten and self._logical_mesh_id.dim() > 1: self.flatten_device_mesh = self.flatten() # Create a new member `flatten_device_meshes` to distinguish from original flatten methods (Because I'm not sure if there are functions that rely on the self.flatten()) # self.flatten_device_meshes = FlattenDeviceMesh(self.physical_mesh_id, self.mesh_shape, self.mesh_alpha, # self.mesh_beta) @property def shape(self): return self.mesh_shape @property def num_devices(self): return reduce(operator.mul, self.physical_mesh_id.shape, 1) @property def logical_mesh_id(self): return self._logical_mesh_id def __deepcopy__(self, memo): cls = self.__class__ result = cls.__new__(cls) memo[id(self)] = result for k, v in self.__dict__.items(): if k != 'process_groups_dict': setattr(result, k, __import__("copy").deepcopy(v, memo)) else: setattr(result, k, v) return result def flatten(self): """ Flatten the logical mesh into an effective 1d logical mesh, """ flatten_mesh_shape_size = len(self.mesh_shape) flatten_mesh_shape = [self.num_devices] return DeviceMesh(self.physical_mesh_id, tuple(flatten_mesh_shape), mesh_alpha=[max(self.mesh_alpha)] * (flatten_mesh_shape_size - 1), mesh_beta=[max(self.mesh_beta)] * (flatten_mesh_shape_size - 1), init_process_group=self.init_process_group, need_flatten=False) def _global_rank_to_logical_rank_map(self, tensor, index_list): ''' This method is a helper function to build convert_map recursively. ''' for index, inner_tensor in enumerate(tensor): if inner_tensor.numel() == 1: self.convert_map[int(inner_tensor)] = index_list + [index] else: self._global_rank_to_logical_rank_map(inner_tensor, index_list + [index]) def create_process_groups_for_logical_mesh(self): ''' This method is used to initialize the logical process groups which will be used in communications among logical device mesh. Note: if init_process_group set to False, you have to call this method manually. Otherwise, the communication related function, such as ShapeConsistencyManager.apply will raise errors. ''' process_groups_dict = {} check_duplicate_list = [] global_rank_flatten_list = self.physical_mesh_id.view(-1).tolist() for global_rank in global_rank_flatten_list: process_groups = self.global_rank_to_process_groups_with_global_rank(global_rank) for axis, process_group in process_groups.items(): if axis not in process_groups_dict: process_groups_dict[axis] = [] if process_group not in check_duplicate_list: check_duplicate_list.append(process_group) process_group_handler = dist.new_group(process_group) process_groups_dict[axis].append((process_group, process_group_handler)) return process_groups_dict def global_rank_to_logical_rank(self, rank): return self.convert_map[rank] def global_rank_to_process_groups_with_logical_rank(self, rank): ''' Give a global rank and return all logical process groups of this rank. for example: physical_mesh_id = torch.arange(0, 16).reshape(2, 8) mesh_shape = (4, 4) # [[0, 1, 2, 3], # [4, 5, 6, 7], # [8, 9, 10,11], # [12,13,14,15]] device_mesh = DeviceMesh(physical_mesh_id, mesh_shape) print(device_mesh.global_rank_to_process_groups_with_logical_rank(0)) output: # key is axis name # value is a list of logical ranks in same axis with rank 0 {0: [[0, 0], [1, 0], [2, 0], [3, 0]], 1: [[0, 0], [0, 1], [0, 2], [0, 3]]} ''' process_groups = {} for d in range(self.logical_mesh_id.dim()): for replacer in range(self.logical_mesh_id.shape[d]): if d not in process_groups: process_groups[d] = [] process_group_member = self.convert_map[rank].copy() process_group_member[d] = replacer process_groups[d].append(process_group_member) return process_groups def global_rank_to_process_groups_with_global_rank(self, rank): ''' Give a global rank and return all process groups of this rank. for example: physical_mesh_id = torch.arange(0, 16).reshape(2, 8) mesh_shape = (4, 4) # [[0, 1, 2, 3], # [4, 5, 6, 7], # [8, 9, 10,11], # [12,13,14,15]] device_mesh = DeviceMesh(physical_mesh_id, mesh_shape) print(device_mesh.global_rank_to_process_groups_with_global_rank(0)) output: # key is axis name # value is a list of global ranks in same axis with rank 0 {0: [0, 4, 8, 12], 1: [0, 1, 2, 3]} ''' logical_process_groups = self.global_rank_to_process_groups_with_logical_rank(rank) process_groups = {} for dim, logical_ranks in logical_process_groups.items(): process_groups[dim] = [] for logical_rank in logical_ranks: for g_rank, l_rank in self.convert_map.items(): if l_rank == logical_rank: process_groups[dim].append(g_rank) return process_groups def all_gather_cost(self, num_bytes, mesh_dim): num_devices = self.logical_mesh_id.shape[mesh_dim] return (self.mesh_alpha[mesh_dim] + self.mesh_beta[mesh_dim] * (num_devices - 1) / num_devices * num_bytes + 0.1) def all_reduce_cost(self, num_bytes, mesh_dim): num_devices = self.logical_mesh_id.shape[mesh_dim] return (self.mesh_alpha[mesh_dim] + self.mesh_beta[mesh_dim] * 2 * (num_devices - 1) / num_devices * num_bytes + 0.01) def reduce_scatter_cost(self, num_bytes, mesh_dim): num_devices = self.logical_mesh_id.shape[mesh_dim] return (self.mesh_alpha[mesh_dim] + self.mesh_beta[mesh_dim] * (num_devices - 1) / num_devices * num_bytes + 0.001) def all_to_all_cost(self, num_bytes, mesh_dim): num_devices = self.logical_mesh_id.shape[mesh_dim] penalty_factor = num_devices / 2.0 return (self.mesh_alpha[mesh_dim] + self.mesh_beta[mesh_dim] * (num_devices - 1) / num_devices / num_devices * num_bytes * penalty_factor + 0.001) class FlattenDeviceMesh(DeviceMesh): def __init__(self, physical_mesh_id, mesh_shape, mesh_alpha=None, mesh_beta=None): super().__init__(physical_mesh_id, mesh_shape, mesh_alpha, mesh_beta, init_process_group=False, need_flatten=False) # Different from flatten(), mesh_shape leaves unchanged, mesh_alpha and mesh_beta are scalars self.mesh_alpha = max(self.mesh_alpha) self.mesh_beta = min(self.mesh_beta) # Different from original process_groups_dict, rank_list is not stored self.process_number_dict = self.create_process_numbers_for_logical_mesh() def create_process_numbers_for_logical_mesh(self): ''' Build 1d DeviceMesh in column-major(0) and row-major(1) for example: mesh_shape = (2,4) # [[0, 1, 2, 3], # [4, 5, 6, 7]] # return {0: [0, 4, 1, 5, 2, 6, 3, 7], 1: [0, 1, 2, 3, 4, 5, 6, 7]} ''' num_devices = reduce(operator.mul, self.mesh_shape, 1) process_numbers_dict = {} process_numbers_dict[0] = torch.arange(num_devices).reshape(self.mesh_shape).transpose(1, 0).flatten().tolist() process_numbers_dict[1] = torch.arange(num_devices).reshape(self.mesh_shape).flatten().tolist() return process_numbers_dict def mix_gather_cost(self, num_bytes): num_devices = reduce(operator.mul, self.mesh_shape, 1) return (self.mesh_alpha + self.mesh_beta * (num_devices - 1) / num_devices * num_bytes + 0.1)
from enum import Enum class ComputePattern(Enum): TP1D = 0 TP2D = 1 TP2P5D = 2 TP3D = 3 class ComputeSpec(object): """ComputeSpec The Specification for compuattion pattern Args: compute_pattern (ComputePattern): an Enum instance for compute pattern. """ def __init__(self, compute_pattern: ComputePattern) -> None: assert isinstance(compute_pattern, ComputePattern) self.compute_pattern = compute_pattern # Make sure output tensors are replicate self.output_replicate = True def __repr__(self): return f'ComputeSpec(pattern={self.compute_pattern}, replicate_output={self.output_replicate})' def set_output_replicate(self, flag: bool = True): self.output_replicate = flag
from abc import ABC, abstractmethod from contextlib import contextmanager from typing import Any, List, Tuple import torch from colossalai.tensor.colo_tensor import ColoTensor from colossalai.tensor.tensor_spec import ColoTensorSpec class ColoParamOpHook(ABC): """ Hook which is triggered by each operation when operands contain ColoParameter. To customize it, you must inherit this abstract class, and implement ``pre_forward``, ``post_forward``, ``pre_backward`` and ``post_backward``. These four methods apply a list of ColoParameter as input args. """ @abstractmethod def pre_forward(self, params: List[torch.Tensor]) -> None: pass @abstractmethod def post_forward(self, params: List[torch.Tensor]) -> None: pass @abstractmethod def pre_backward(self, params: List[torch.Tensor]) -> None: pass @abstractmethod def post_backward(self, params: List[torch.Tensor]) -> None: pass class ColoParamOpHookManager: """ Manage your param op hooks. It only has static methods. The only static method you should call is ``use_hooks(*hooks)``. """ hooks: Tuple[ColoParamOpHook, ...] = tuple() @staticmethod @contextmanager def use_hooks(*hooks: ColoParamOpHook): """Change the param op hooks you use. Nested calling is allowed. Example: >>> with ColoParamOpHookManager.use_hooks(*hooks): >>> do_something() >>> with ColoParamOpHookManager.use_hooks(): >>> // clear hooks >>> do_something() """ try: old_param_op_hooks = ColoParamOpHookManager.hooks ColoParamOpHookManager.hooks = hooks yield finally: ColoParamOpHookManager.hooks = old_param_op_hooks @staticmethod def _trigger_pre_forward(params: List[torch.Tensor]) -> None: for hook in ColoParamOpHookManager.hooks: hook.pre_forward(params) @staticmethod def _trigger_post_forward(params: List[torch.Tensor]) -> None: for hook in ColoParamOpHookManager.hooks: hook.post_forward(params) @staticmethod def _trigger_pre_backward(params: List[torch.Tensor]) -> None: for hook in ColoParamOpHookManager.hooks: hook.pre_backward(params) @staticmethod def _trigger_post_backward(params: List[torch.Tensor]) -> None: for hook in ColoParamOpHookManager.hooks: hook.post_backward(params) @staticmethod def pre_op(params: List[torch.Tensor], *args: Any) -> list: ColoParamOpHookManager._trigger_pre_forward(params) grad_args, rear_args = _get_grad_args(*args) colo_info = _get_colo_tensors_info(*grad_args) rets = PreFwdPostBwd.apply(params, *grad_args) update_args = _update_colo_tensors(colo_info, *rets) if rear_args is None: return update_args else: arg_zero = (tuple(update_args),) return arg_zero + rear_args @staticmethod def post_op(params: List[torch.Tensor], arg: Any) -> Any: ColoParamOpHookManager._trigger_post_forward(params) colo_info = _get_colo_tensors_info(arg) ret = PostFwdPreBwd.apply(params, arg) res = _update_colo_tensors(colo_info, ret) if len(res) == 1: return res[0] else: return res @staticmethod def has_hook() -> bool: return len(ColoParamOpHookManager.hooks) > 0 class PreFwdPostBwd(torch.autograd.Function): @staticmethod def forward(ctx, params, *args): ctx.params = params return args @staticmethod def backward(ctx, *grads): ColoParamOpHookManager._trigger_post_backward(ctx.params) return (None,) + grads class PostFwdPreBwd(torch.autograd.Function): @staticmethod def forward(ctx, params, args): ctx.params = params return args @staticmethod def backward(ctx, *grads): ColoParamOpHookManager._trigger_pre_backward(ctx.params) return (None,) + grads def _is_grad_tensor(obj) -> bool: if torch.is_tensor(obj): if obj.grad_fn is not None or obj.requires_grad: return True return False def _has_grad_tensor(obj) -> bool: if isinstance(obj, tuple) or isinstance(obj, list): for x in obj: if _has_grad_tensor(x): return True return False elif isinstance(obj, dict): for x in obj.values(): if _has_grad_tensor(x): return True return False else: return _is_grad_tensor(obj) def _get_grad_args(*args): # if there is no grad tensors, do nothing if not _has_grad_tensor(args): return args, None # returns the identical args if there is a grad tensor for obj in args: if _is_grad_tensor(obj): return args, None # otherwise, the first arguement should be a tuple of grad tensors # if there is no grad tensor, the backward of PreFwdPostBwd can't be triggered arg_zero = args[0] if not isinstance(arg_zero, tuple): raise NotImplementedError("Some torch function is incompatible because of its complcated inputs.") check_grad_flag = False for obj in arg_zero: check_grad_flag |= _is_grad_tensor(obj) if not check_grad_flag: raise NotImplementedError("Some torch function is incompatible because of its complcated inputs.") return arg_zero, args[1:] def _get_colo_tensors_info(*args) -> list: info = [] for arg in args: if isinstance(arg, ColoTensor): info.append((arg.__class__, ColoTensorSpec(arg.get_process_group(), arg.dist_spec, arg.compute_spec))) else: info.append(None) return info def _update_colo_tensors(info, *args) -> list: ret = [] for t_info, arg in zip(info, args): if t_info is not None: t_cls, spec = t_info arg = t_cls.from_torch_tensor(arg, spec=spec) ret.append(arg) return ret
from . import distspec from .colo_parameter import ColoParameter from .colo_tensor import ColoTensor from .comm_spec import CollectiveCommPattern, CommSpec from .compute_spec import ComputePattern, ComputeSpec from .dist_spec_mgr import DistSpecManager from .distspec import ReplicaSpec, ShardSpec from .param_op_hook import ColoParamOpHook, ColoParamOpHookManager from .process_group import ProcessGroup from .tensor_spec import ColoTensorSpec from .utils import convert_dim_partition_dict, convert_parameter, merge_same_dim_mesh_list, named_params_with_colotensor __all__ = [ 'ColoTensor', 'convert_parameter', 'ComputePattern', 'ComputeSpec', 'named_params_with_colotensor', 'ColoParameter', 'distspec', 'DistSpecManager', 'ColoParamOpHook', 'ColoParamOpHookManager', 'ProcessGroup', 'ColoTensorSpec', 'ShardSpec', 'ReplicaSpec', 'CommSpec', 'CollectiveCommPattern', 'convert_dim_partition_dict', 'merge_same_dim_mesh_list' ]
from typing import Optional import torch from colossalai.tensor.colo_tensor import ColoTensor from colossalai.tensor.const import TensorType from colossalai.tensor.param_op_hook import ColoParamOpHookManager from colossalai.tensor.tensor_spec import ColoTensorSpec def filter_colo_parameters(*args, **kwargs): param_list = [] def get_colo_parameters(element) -> None: if isinstance(element, list) or isinstance(element, tuple): for e in element: get_colo_parameters(e) elif isinstance(element, dict): raise RuntimeError("Found Dict: ColoParameter can't deal with complicated arguments.") elif isinstance(element, ColoParameter): param_list.append(element) return for a in args: get_colo_parameters(a) for v in kwargs.values(): get_colo_parameters(v) return param_list def replace_args(args, kwargs, new_args): args = new_args[:len(args)] for k, v in zip(kwargs.keys(), new_args[len(args):]): kwargs[k] = v return tuple(args), kwargs class ColoParameter(ColoTensor, torch.nn.Parameter): r"""A kind of ColoTensor to be considered as a module parameter. """ def __new__(cls, data: Optional[torch.Tensor] = None, requires_grad: bool = True, spec: ColoTensorSpec = None) -> 'ColoParameter': if data is None: data = torch.empty(0) return torch.Tensor._make_subclass(cls, data, requires_grad) def __init__(self, data: Optional[torch.Tensor] = None, requires_grad: bool = True, spec: ColoTensorSpec = None) -> None: ColoTensor.__init__(self, data, spec) self._type = TensorType.MODEL # a list contains modules sharing this ColoParameter with others. self._shared_param_modules = [] @property def shared_param_modules(self): return self._shared_param_modules @staticmethod def from_torch_tensor(tensor: torch.Tensor, requires_grad: bool = True, spec: ColoTensorSpec = None) -> 'ColoParameter': tensor = tensor.as_subclass(ColoParameter) tensor.__init__(tensor, requires_grad=requires_grad, spec=spec) return tensor def __repr__(self): return super(ColoParameter, self).__repr__() @classmethod def __torch_function__(cls, func, types, args=..., kwargs=None): if ColoParamOpHookManager.has_hook(): if not func.__name__.startswith('__'): if kwargs is None: kwargs = {} params = filter_colo_parameters(*args, **kwargs) if len(params) > 0: with torch._C.DisableTorchFunction(): new_args = ColoParamOpHookManager.pre_op(params, *args, *kwargs.values()) args, kwargs = replace_args(args, kwargs, new_args) ret = super().__torch_function__(func, types, args, kwargs) with torch._C.DisableTorchFunction(): ret = ColoParamOpHookManager.post_op(params, ret) return ret return super().__torch_function__(func, types, args, kwargs) def __deepcopy__(self, memo): if id(self) in memo: return memo[id(self)] else: with torch._C.DisableTorchFunction(): data = self.data.clone() tensor = ColoParameter(data, self.requires_grad, spec=ColoTensorSpec(self.get_process_group(), self.dist_spec, self.compute_spec)) memo[id(self)] = tensor return tensor def __reduce_ex__(self, proto): # Adapted from torch._utils._rebuild_parameter # def _rebuild_colo_parameter(data, requires_grad, backward_hooks): # colo_param = ColoParameter(data, requires_grad) # colo_param._backward_hooks = backward_hooks # return colo_param # return ( # _rebuild_colo_parameter, # (self.data, self.requires_grad, OrderedDict()) # ) # TODO(jzy) we don't support object reflection now. # distspec cannot be pickled or rebuilt because it's tightly connected to runtime attribute `process_group`. raise NotImplementedError
from dataclasses import dataclass from typing import Optional from colossalai.tensor.distspec import DistPlacementPattern, _DistSpec from colossalai.tensor.process_group import ProcessGroup from .compute_spec import ComputeSpec @dataclass class ColoTensorSpec: """ ColoTensorSpec A data class for specifications of the `ColoTensor`. It contains attributes of `ProcessGroup`, `_DistSpec`, `ComputeSpec`. The latter two attributes are optional. If not set, they are default value is `Replicate()` and `None`. """ pg: ProcessGroup dist_attr: Optional[_DistSpec] = _DistSpec(DistPlacementPattern.REPLICATE) compute_attr: Optional[ComputeSpec] = None
import operator from enum import Enum from functools import reduce import torch import torch.distributed as dist from torch.distributed import ReduceOp __all__ = [ 'CollectiveCommPattern', 'CommSpec', ] def _all_gather(tensor, comm_spec): ''' Implement all gather operation on device mesh based on information provided by comm_spec. ''' process_groups_list = comm_spec.device_mesh.process_groups_dict[comm_spec.logical_process_axis] for rank_list, process_group in process_groups_list: if dist.get_rank() in rank_list: tensor_list = [ torch.zeros(tensor.shape, dtype=tensor.dtype, device=tensor.device) for _ in range(comm_spec.device_mesh.mesh_shape[comm_spec.logical_process_axis]) ] # without this contiguous operation, the all gather may get some unexpected results. tensor = tensor.contiguous() dist.all_gather(tensor_list, tensor, group=process_group) output = torch.cat(tuple(tensor_list), comm_spec.gather_dim).contiguous() return output def _split(tensor, comm_spec): ''' Implement shard operation on device mesh based on information provided by comm_spec. ''' process_groups_list = comm_spec.device_mesh.process_groups_dict[comm_spec.logical_process_axis] for rank_list, _ in process_groups_list: if dist.get_rank() in rank_list: dim = comm_spec.shard_dim length = tensor.shape[comm_spec.shard_dim] // len(rank_list) start = length * rank_list.index(dist.get_rank()) output = torch.narrow(tensor, dim, start, length).contiguous() return output def _all_to_all(tensor, comm_spec): ''' Implement all to all operation on device mesh based on information provided by comm_spec. ''' process_groups_list = comm_spec.device_mesh.process_groups_dict[comm_spec.logical_process_axis] for rank_list, process_group in process_groups_list: if dist.get_rank() in rank_list: new_shape = list(tensor.shape) new_shape[comm_spec.shard_dim] = new_shape[comm_spec.shard_dim] // len(rank_list) new_shape = torch.Size(new_shape) output_tensor_list = [ torch.zeros(new_shape, dtype=tensor.dtype, device=tensor.device) for _ in range(len(rank_list)) ] dim = comm_spec.shard_dim length = tensor.shape[comm_spec.shard_dim] // len(rank_list) input_tensor_list = [ torch.narrow(tensor, dim, length * i, length).contiguous() for i in range(len(rank_list)) ] group = process_group dist.all_to_all(output_tensor_list, input_tensor_list, group) output = torch.cat(tuple(output_tensor_list), comm_spec.gather_dim).contiguous() return output def _all_reduce(tensor, comm_spec, async_op=False): ''' Implement all reduce operation on device mesh based on information provided by comm_spec. ''' process_groups_list = comm_spec.device_mesh.process_groups_dict[comm_spec.logical_process_axis] for rank_list, process_group in process_groups_list: if dist.get_rank() in rank_list: if not tensor.is_contiguous(): tensor = tensor.contiguous() dist.all_reduce(tensor, op=ReduceOp.SUM, group=process_group, async_op=async_op) return tensor def _mix_gather(tensor, comm_spec): ''' Implement mix gather operation on device mesh based on information provided by comm_spec. Mix gather is the all-gather operation on all devices in the device_mesh(FlattenDeviceMesh) of the comm_spec. It is different from _all_gather because _mix_gather does all-gather in two dimensions of device mesh, while _all_gather only does all-gather in one dimension. Assume index of f and b target pairs are 'f' and 'b' ShardingSpec => gather_dim, logical_process_axes S0S1 => [b, f], (1, 0) S1S0 => [b, f], (0, 1) S01R => [f], (1, 1) RS01 => [b], (1, 1) Example: mesh_shape = (2,4) # [[0, 1, 2, 3], # [4, 5, 6, 7]] # return {0: [0, 4, 1, 5, 2, 6, 3, 7], 1: [0, 1, 2, 3, 4, 5, 6, 7]} S0S1: leading_group_dim = 1 process_group = "[0, 1, 2, 3, 4, 5, 6, 7]" tensor_list = [(0,0),(0,1),(0,2),(0,3),(1,0),(1,1),(1,2),(1,3)] # [(slice_id_f, slice_id_b),...] mesh_shape = (2,4) cat_slice = [4,2] tmp_tensor_list = [(...,shape[f],shape[b]*4,...),(...,shape[f],shape[b]*4,...)] tmp_tensor_list[0] = torch.cat(((0,0),(0,1),(0,2),(0,3)), dim=b) tmp_tensor_list[1] = torch.cat(((1,0),(1,1),(1,2),(1,3)), dim=b) output = torch.cat((tmp_tensor_list[0],tmp_tensor_list[1]), dim=a) S1S0: leading_group_dim = 0 process_group = "[0, 4, 1, 5, 2, 6, 3, 7]" tensor_list = [(0,0),(0,1),(1,0),(1,1),(2,0),(2,1),(3,0),(3,1)] mesh_shape = (2,4) cat_slice = [2,4] tmp_tensor_list = [(...,shape[f],shape[b]*2,...),(...,shape[f],shape[b]*2,...),(...,shape[f],shape[b]*2,...),(...,shape[f],shape[b]*2,...)] tmp_tensor_list[0] = torch.cat(((0,0),(0,1)), dim=b) tmp_tensor_list[1] = torch.cat(((1,0),(1,1)), dim=b) tmp_tensor_list[2] = torch.cat(((2,0),(2,1)), dim=b) tmp_tensor_list[3] = torch.cat(((3,0),(3,1)), dim=b) S10R: leading_group_dim = 0 process_group = "[0, 4, 1, 5, 2, 6, 3, 7]" tensor_list = [(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0)] S01R: leading_group_dim = 1 process_group = "[0, 1, 2, 3, 4, 5, 6, 7]" tensor_list = [(0,0),(1,0),(2,0),(3,0),(4,0),(5,0),(6,0),(7,0)] ''' total_slices = comm_spec.device_mesh.mesh_shape[0] tensor_list = [torch.zeros(tensor.shape, dtype=tensor.dtype, device=tensor.device) for _ in range(total_slices)] leading_group_dim = comm_spec.logical_process_axes[0] assert len(comm_spec.device_mesh.process_groups_dict) == 1 _, process_group = comm_spec.device_mesh.process_groups_dict[0][0] process_number_list = comm_spec.device_meshes.process_number_dict[leading_group_dim] # Global all_gather dist.all_gather(tensor_list, tensor, group=process_group) # This is very ugly. I'm figuring out more elegant methods tensor_list_sorted = [ torch.zeros(tensor.shape, dtype=tensor.dtype, device=tensor.device) for _ in range(total_slices) ] for i in range(total_slices): tensor_list_sorted[i] = tensor_list[process_number_list[i]] tensor_list = tensor_list_sorted if comm_spec.logical_process_axes[0] == comm_spec.logical_process_axes[1]: output = torch.cat(tuple(tensor_list), comm_spec.gather_dim[0]).contiguous() else: mesh_shape = comm_spec.device_meshes.mesh_shape cat_slice = [mesh_shape[comm_spec.logical_process_axes[0]], mesh_shape[comm_spec.logical_process_axes[1]]] tmp_tensor_shape = list(tensor.shape) tmp_tensor_shape[comm_spec.gather_dim[0]] *= cat_slice[0] tmp_tensor_shape = torch.Size(tmp_tensor_shape) tmp_tensor_list = [ torch.zeros(tmp_tensor_shape, dtype=tensor.dtype, device=tensor.device) for _ in range(cat_slice[1]) ] for i in range(cat_slice[1]): tmp_tensor_list[i] = torch.cat(tuple(tensor_list[i * cat_slice[0]:(i + 1) * cat_slice[0]]), comm_spec.gather_dim[0]).contiguous() output = torch.cat(tuple(tmp_tensor_list), comm_spec.gather_dim[1]).contiguous() return output def _mix_split(tensor, comm_spec): ''' Implement mix split operation. Mix split is only called for the backward of mix gather (Use ctx to keep consistent) Mix split shards the tensor on device mesh based on information provided by comm_spec. It is different from split because _mix_split shards the tensor in two dimensions of device mesh, while _split only shards in one dimension. Assume index of f and b target pairs are 'f' and 'b' S0S1 => [b, f], (1, 0) S1S0 => [b, f], (0, 1) S01R => [f], (0, 0) RS01 => [b], (0, 0) Example: mesh_shape = (2,4) # [[0, 1, 2, 3], # [4, 5, 6, 7]] # return {0: [0, 4, 1, 5, 2, 6, 3, 7], 1: [0, 1, 2, 3, 4, 5, 6, 7]} ''' mesh_shape = comm_spec.device_meshes.mesh_shape dim = comm_spec.gather_dim total_slices = comm_spec.device_mesh.mesh_shape[0] # Get global rank rank = dist.get_rank() leading_group_dim = comm_spec.logical_process_axes[0] process_number_list = comm_spec.device_meshes.process_number_dict[leading_group_dim] rank = process_number_list.index(rank) if comm_spec.logical_process_axes[0] == comm_spec.logical_process_axes[1]: length = tensor.shape[dim[0]] // total_slices start = length * rank output = torch.narrow(tensor, dim[0], start, length).contiguous() else: tensor_shape = [tensor.shape[dim[0]], tensor.shape[dim[1]]] rank_slice = [mesh_shape[comm_spec.logical_process_axes[0]], mesh_shape[comm_spec.logical_process_axes[1]]] length = [tensor_shape[0] // rank_slice[0], tensor_shape[1] // rank_slice[1]] start = [(rank % rank_slice[0]) * length[0], (rank // rank_slice[0]) * length[1]] tmp_output = torch.narrow(tensor, dim[0], start[0], length[0]).contiguous() output = torch.narrow(tmp_output, dim[1], start[1], length[1]).contiguous() return output class _ReduceGrad(torch.autograd.Function): """ A customized communication operation which forward is an identity operation, backward is all_reduce operation. Args: input_: input matrix. comm_spec: comm_spec will give information like process group, rank list, etc. """ @staticmethod def symbolic(graph, input_): return input_ @staticmethod def forward(ctx, input_, comm_spec): ctx.comm_spec = comm_spec return input_ @staticmethod def backward(ctx, grad_output): return _all_reduce(grad_output, ctx.comm_spec), None class _ReduceInput(torch.autograd.Function): """ A customized communication operation which forward is all_reduce operation, backward is an identity operation. Args: input_: input matrix. comm_spec: comm_spec will give information like process group, rank list, etc. """ @staticmethod def symbolic(graph, input_): return _all_reduce(input_) @staticmethod def forward(ctx, input_, comm_spec): return _all_reduce(input_, comm_spec) @staticmethod def backward(ctx, grad_output): return grad_output, None class _SplitForwardGatherBackward(torch.autograd.Function): """ A customized communication operation which forward is split operation, backward is an all gather operation. Args: input_: input matrix. comm_spec: comm_spec will give information like process group, rank list, etc. """ @staticmethod def symbolic(graph, input_): return _split(input_) @staticmethod def forward(ctx, input_, comm_spec): ctx.comm_spec = comm_spec return _split(input_, comm_spec) @staticmethod def backward(ctx, grad_output): return _all_gather(grad_output, ctx.comm_spec), None class _GatherForwardSplitBackward(torch.autograd.Function): """ A customized communication operation which forward is an all gather operation, backward is split operation. Args: input_: input matrix. comm_spec: comm_spec will give information like process group, rank list, etc. """ @staticmethod def symbolic(graph, input_): return _all_gather(input_) @staticmethod def forward(ctx, input_, comm_spec): ctx.comm_spec = comm_spec return _all_gather(input_, comm_spec) @staticmethod def backward(ctx, grad_output): return _split(grad_output, ctx.comm_spec), None class _AllToAll(torch.autograd.Function): """ A customized communication operation which forward is an all to all operation, backward is an all to all operation. Args: input_: input matrix. comm_spec: comm_spec will give information like process group, rank list, etc. """ @staticmethod def symbolic(graph, input_): return _all_to_all(input_) @staticmethod def forward(ctx, input_, comm_spec): output = _all_to_all(input_, comm_spec) comm_spec_for_backward = CommSpec(comm_pattern=comm_spec.comm_pattern, sharding_spec=comm_spec.sharding_spec, gather_dim=comm_spec.shard_dim, shard_dim=comm_spec.gather_dim, logical_process_axis=comm_spec.logical_process_axis) ctx.comm_spec = comm_spec_for_backward return output @staticmethod def backward(ctx, grad_outputs): return _all_to_all(grad_outputs, ctx.comm_spec), None class _MixGatherForwardMixSplitBackward(torch.autograd.Function): @staticmethod def symbolic(graph, input_): return _mix_gather(input_) @staticmethod def forward(ctx, input_, comm_spec): ctx.comm_spec = comm_spec return _mix_gather(input_, comm_spec) @staticmethod def backward(ctx, grad_output): return _mix_split(grad_output, ctx.comm_spec), None def reduce_grad(input_, comm_spec): return _ReduceGrad.apply(input_, comm_spec) def reduce_input(input_, comm_spec): return _ReduceInput.apply(input_, comm_spec) def split_forward_gather_backward(input_, comm_spec): return _SplitForwardGatherBackward.apply(input_, comm_spec) def gather_forward_split_backward(input_, comm_spec): return _GatherForwardSplitBackward.apply(input_, comm_spec) def all_to_all(input_, comm_spec): return _AllToAll.apply(input_, comm_spec) def mixgather_forward_split_backward(input_, comm_spec): return _MixGatherForwardMixSplitBackward.apply(input_, comm_spec) class CollectiveCommPattern(Enum): GATHER_FWD_SPLIT_BWD = 'gather_fwd_split_bwd' ALL2ALL_FWD_ALL2ALL_BWD = 'all2all_fwd_all2all_bwd' SPLIT_FWD_GATHER_BWD = 'split_fwd_gather_bwd' ALLREDUCE_FWD_IDENTITY_BWD = 'all_reduce_fwd_identity_bwd' IDENTITY_FWD_ALLREDUCE_BWD = 'identity_fwd_all_reduce_bwd' MIXGATHER_FWD_SPLIT_BWD = "mixgather_fwd_split_bwd" class CommSpec: ''' Communication spec is used to record the communication action. It has two main functions: 1. Compute the communication cost which will be used in auto parallel solver. 2. Convert the communication spec to real action which will be used in runtime. It contains comm_pattern to determine the communication method, sharding_spec to determine the communication size, gather_dim and shard_dim to determine the buffer shape, and logical_process_axis Argument: comm_pattern(CollectiveCommPattern): decribe the communication method used in this spec. sharding_spec(ShardingSpec): This is sharding spec of the tensor which will join the communication action. gather_dim(int, Optional): The gather_dim of the tensor will be gathered. shard_dim(int, Optional): The shard_dim of the tensor will be sharded. logical_process_axis(Union(int, List[int]), Optional): The mesh_dim to implement the communication action. ''' def __init__(self, comm_pattern, sharding_spec, gather_dim=None, shard_dim=None, logical_process_axis=None, forward_only=False, mix_gather=False): self.comm_pattern = comm_pattern self.sharding_spec = sharding_spec self.gather_dim = gather_dim self.shard_dim = shard_dim self.logical_process_axis = logical_process_axis self.forward_only = forward_only if isinstance(self.logical_process_axis, list): if not mix_gather: self.device_mesh = self.sharding_spec.device_mesh.flatten_device_mesh self.logical_process_axis = 0 else: self.device_meshes = self.sharding_spec.device_mesh.flatten_device_meshes self.device_mesh = self.sharding_spec.device_mesh.flatten_device_mesh # Create a new member `logical_process_axes` to distinguish from original flatten self.logical_process_axes = logical_process_axis else: self.device_mesh = self.sharding_spec.device_mesh def __repr__(self): res_list = ["CommSpec:("] if self.comm_pattern == CollectiveCommPattern.GATHER_FWD_SPLIT_BWD: res_list.append(f"comm_pattern:GATHER_FWD_SPLIT_BWD, ") res_list.append(f"gather_dim:{self.gather_dim}, ") res_list.append(f"logical_process_axis:{self.logical_process_axis})") elif self.comm_pattern == CollectiveCommPattern.ALL2ALL_FWD_ALL2ALL_BWD: res_list.append(f"comm_pattern:ALL2ALL_FWD_ALL2ALL_BWD, ") res_list.append(f"gather_dim:{self.gather_dim}, ") res_list.append(f"shard_dim:{self.shard_dim}, ") res_list.append(f"logical_process_axis: {self.logical_process_axis})") elif self.comm_pattern == CollectiveCommPattern.SPLIT_FWD_GATHER_BWD: res_list.append(f"comm_pattern:SPLIT_FWD_GATHER_BWD, ") res_list.append(f"shard_dim:{self.shard_dim}, ") res_list.append(f"logical_process_axis:{self.logical_process_axis})") elif self.comm_pattern == CollectiveCommPattern.ALLREDUCE_FWD_IDENTITY_BWD: res_list.append(f"comm_pattern:ALLREDUCE_FWD_IDENTITY_BWD, ") res_list.append(f"logical_process_axis:{self.logical_process_axis})") elif self.comm_pattern == CollectiveCommPattern.IDENTITY_FWD_ALLREDUCE_BWD: res_list.append(f"comm_pattern:IDENTITY_FWD_ALLREDUCE_BWD, ") res_list.append(f"logical_process_axis:{self.logical_process_axis})") elif self.comm_pattern == CollectiveCommPattern.MIXGATHER_FWD_SPLIT_BWD: res_list.append(f"comm_pattern:MIXGATHER_FWD_SPLIT_BWD, ") res_list.append(f"gather_dim:{self.gather_dim}, ") res_list.append(f"logical_process_asex:{self.logical_process_axes})") return ''.join(res_list) def get_comm_cost(self): ''' For all_gather, all2all, and all_reduce operation, the formula provided in DeviceMesh with alpha-beta model is used to compute the communication cost. For shard operation, it is an on-chip operation, so the communication cost is zero. ''' comm_size = reduce(operator.mul, self.sharding_spec.get_sharded_shape_per_device(), 1) cost_dict = {} if self.comm_pattern == CollectiveCommPattern.GATHER_FWD_SPLIT_BWD: forward_communication_cost = self.device_mesh.all_gather_cost(comm_size, self.logical_process_axis) # give a tiny cost to shard backward_communication_cost = 100 if self.comm_pattern == CollectiveCommPattern.ALL2ALL_FWD_ALL2ALL_BWD: forward_communication_cost = self.device_mesh.all_to_all_cost(comm_size, self.logical_process_axis) # grad should have same shape as input tensor # all to all operation has same logical process axis as forward. backward_communication_cost = self.device_mesh.all_to_all_cost(comm_size, self.logical_process_axis) if self.comm_pattern == CollectiveCommPattern.ALLREDUCE_FWD_IDENTITY_BWD: forward_communication_cost = self.device_mesh.all_reduce_cost(comm_size, self.logical_process_axis) backward_communication_cost = 0 if self.comm_pattern == CollectiveCommPattern.IDENTITY_FWD_ALLREDUCE_BWD: forward_communication_cost = 0 backward_communication_cost = self.device_mesh.all_reduce_cost(comm_size, self.logical_process_axis) if self.comm_pattern == CollectiveCommPattern.SPLIT_FWD_GATHER_BWD: # give a tiny cost to shard forward_communication_cost = 100 backward_communication_cost = self.device_mesh.all_gather_cost(comm_size, self.logical_process_axis) if self.comm_pattern == CollectiveCommPattern.MIXGATHER_FWD_SPLIT_BWD: # no need for axis because all devices are used in mix_gather forward_communication_cost = self.device_mesh.mix_gather_cost(comm_size) backward_communication_cost = 100 if self.forward_only: cost_dict["forward"] = forward_communication_cost cost_dict["backward"] = 0 cost_dict["total"] = cost_dict["forward"] + cost_dict["backward"] else: cost_dict["forward"] = forward_communication_cost cost_dict["backward"] = backward_communication_cost cost_dict["total"] = cost_dict["forward"] + cost_dict["backward"] return cost_dict def covert_spec_to_action(self, tensor): ''' Convert CommSpec into runtime action, implement real collection communication to target tensor. The collection communication action is directed by the CommSpec. Argument: tensor(torch.Tensor): Tensor stored in each device, which could be different in different ranks. ''' if self.comm_pattern in pattern_to_func_dict: tensor = pattern_to_func_dict[self.comm_pattern](tensor, self) else: tensor = tensor return tensor pattern_to_func_dict = { CollectiveCommPattern.GATHER_FWD_SPLIT_BWD: gather_forward_split_backward, CollectiveCommPattern.ALL2ALL_FWD_ALL2ALL_BWD: all_to_all, CollectiveCommPattern.SPLIT_FWD_GATHER_BWD: split_forward_gather_backward, CollectiveCommPattern.ALLREDUCE_FWD_IDENTITY_BWD: reduce_input, CollectiveCommPattern.IDENTITY_FWD_ALLREDUCE_BWD: reduce_grad, CollectiveCommPattern.MIXGATHER_FWD_SPLIT_BWD: mixgather_forward_split_backward, }
import math from copy import deepcopy from dataclasses import dataclass from typing import Dict, List, Tuple import numpy as np import torch from colossalai.auto_parallel.tensor_shard.sharding_strategy import MemoryCost, TrainCycleItem from colossalai.context.singleton_meta import SingletonMeta from colossalai.tensor.sharding_spec import ShardingSpec, ShardingSpecException from colossalai.tensor.utils import all_gather_simulator, all_to_all_simulator, mix_gather_simulator, shard_simulator from .comm_spec import * __all__ = ['ShapeConsistencyManager', 'ShapeConsistencyOptions', 'set_shape_consistency_options'] @dataclass class ShapeConsistencyOptions: """ ShapeConsistencyOptions is a dataclass which specifies the preferences for shape consistency. """ # TODO: shape consistency option is not implemented yet pass def to_global(distributed_tensor: torch.Tensor, sharding_spec: ShardingSpec) -> torch.Tensor: shape_consistency_manager = ShapeConsistencyManager() global_sharding_spec = ShardingSpec(sharding_spec.device_mesh, sharding_spec.entire_shape, {}) with torch.no_grad(): global_tensor = shape_consistency_manager.apply_for_autoparallel_runtime(distributed_tensor, sharding_spec, global_sharding_spec) return global_tensor def set_shape_consistency_options(options: ShapeConsistencyOptions): """ Configure the shape consistency manager via function call. """ manager = ShapeConsistencyManager() manager.options = options class ShapeConsistencyManager(metaclass=SingletonMeta): def __init__(self): self._options = None self._forward_only = False self.total_communication_cost = 0 self.total_transform_steps = 0 self.cached_spec_pairs_transform_path = {} @property def options(self): return self._options @options.setter def options(self, options_: ShapeConsistencyOptions): assert isinstance(options_, ShapeConsistencyOptions) self._options = options_ @property def forward_only(self): return self._forward_only @forward_only.setter def forward_only(self, value): assert isinstance(value, bool) self._forward_only = value def get_all_all_gather_spec(self, source_spec: ShardingSpec, orig_cost_dict: Dict[str, float]) -> Dict[ShardingSpec, float]: ''' Get all valid sharding specs from source_spec with single all-gather operation, and accumulate commucation cost on origin cost which will finally be used in auto sharding solver. For the all-gather operation, we just care about the S dimension. Argument: source_spec(ShardingSpec): the ShardingSpec of the source_spec. orig_cost(Dict[str, float]): the original communication cost before this operation. Return: valid_spec_dict(Dict[ShardingSpec, float]): all valid sharding specs from source_spec with single all-gather operation. Example: dim_partition_dict = {0: [0], 1: [1]} # DistSpec: # shard_sequence: S0,S1,R # device_mesh_shape: (4, 4) sharding_spec = ShardingSpec(device_mesh, entire_shape, dim_partition_dict) shape_consistency_manager = ShapeConsistencyManager() rst_dict = shape_consistency_manager.get_all_all_gather_spec(sharding_spec, {'forward': 0, 'backward': 0, 'total': 0}) print(rst_dict) Output: {DistSpec: shard_sequence: R,S1,R device_mesh_shape: (4, 4): 0, DistSpec: shard_sequence: S0,R,R device_mesh_shape: (4, 4): 0} ''' valid_spec_dict = {} comm_pattern = CollectiveCommPattern.GATHER_FWD_SPLIT_BWD for target_pair in source_spec.dim_partition_dict.items(): shard_list = all_gather_simulator(target_pair) index = target_pair[0] new_dim_partition_dict = deepcopy(source_spec.dim_partition_dict) # We won't add empty list into dim_partition_dict # The key will be popped if the related shard_list is empty if shard_list: new_dim_partition_dict[index] = shard_list else: new_dim_partition_dict.pop(index) # generate the CommSpec to record the action of source_sharding_spec->new_sharding_spec gather_dim = index logical_process_axis = target_pair[1][-1] comm_spec = CommSpec( comm_pattern, sharding_spec=source_spec, gather_dim=gather_dim, # shard_dim will be used during backward shard_dim=gather_dim, logical_process_axis=logical_process_axis, forward_only=self.forward_only) # compute the communication cost with CommSpec cost_dict = comm_spec.get_comm_cost() # generate new sharding spec try: new_sharding_spec = ShardingSpec(source_spec.device_mesh, source_spec.entire_shape, dim_partition_dict=new_dim_partition_dict) for phase, cost in cost_dict.items(): cost_dict[phase] = cost + orig_cost_dict[phase] valid_spec_dict[new_sharding_spec] = (comm_spec, cost_dict) except ShardingSpecException: pass return valid_spec_dict def get_all_all_to_all_spec(self, source_spec: ShardingSpec, orig_cost_dict: Dict[str, float]) -> Dict[ShardingSpec, float]: ''' Get all valid sharding specs from source_spec with single all-to-all operation, and accumulate commucation cost on origin cost which will finally be used in auto sharding solver. For the all-to-all operation, we just care about the pairs containing S dimension. Argument: source_spec(ShardingSpec): the ShardingSpec of the source_spec. orig_cost(Dict[str, float]): the original communication cost before this operation. Return: valid_spec_dict(Dict[ShardingSpec, float]): all valid sharding specs from source_spec with single all-to-all operation. Example: dim_partition_dict = {0: [0], 1: [1]} # DistSpec: # shard_sequence: S0,S1,R # device_mesh_shape: (4, 4) sharding_spec = ShardingSpec(device_mesh, entire_shape, dim_partition_dict) shape_consistency_manager = ShapeConsistencyManager() rst_dict = shape_consistency_manager.get_all_all_to_all_spec(sharding_spec, {'forward': 0, 'backward': 0, 'total': 0}) print(rst_dict) Output: {DistSpec: shard_sequence: S01,R,R device_mesh_shape: (4, 4): 0, DistSpec: shard_sequence: R,S1,S0 device_mesh_shape: (4, 4): 0, DistSpec: shard_sequence: S0,R,S1 device_mesh_shape: (4, 4): 0} ''' valid_spec_dict = {} comm_pattern = CollectiveCommPattern.ALL2ALL_FWD_ALL2ALL_BWD tensor_dims = len(source_spec.entire_shape) for f_index in range(tensor_dims - 1): for b_index in range(f_index + 1, tensor_dims): # skip (R, R) cases if f_index not in source_spec.dim_partition_dict and b_index not in source_spec.dim_partition_dict: continue else: if f_index in source_spec.dim_partition_dict: # skip (S01, R) -> (R, S01) is NOT allowed if len(source_spec.dim_partition_dict[f_index]) >= 2: continue f_target_pair = (f_index, deepcopy(source_spec.dim_partition_dict[f_index])) else: f_target_pair = (f_index, []) if b_index in source_spec.dim_partition_dict: # skip (R, S01) -> (S01, R) is NOT allowed if len(source_spec.dim_partition_dict[b_index]) >= 2: continue b_target_pair = (b_index, deepcopy(source_spec.dim_partition_dict[b_index])) else: b_target_pair = (b_index, []) # skip (S1, S0) -> S10 if f_target_pair[1] and b_target_pair[1] and f_target_pair[1][0] >= b_target_pair[1][0]: continue f_shard_list, b_shard_list = all_to_all_simulator(f_target_pair, b_target_pair) f_index = f_target_pair[0] b_index = b_target_pair[0] # generate the CommSpec to record the action of source_sharding_spec->new_sharding_spec if len(f_shard_list) < len(f_target_pair[1]): gather_dim = f_index shard_dim = b_index logical_process_axis = f_target_pair[1][-1] else: gather_dim = b_index shard_dim = f_index logical_process_axis = b_target_pair[1][-1] comm_spec = CommSpec(comm_pattern, sharding_spec=source_spec, gather_dim=gather_dim, shard_dim=shard_dim, logical_process_axis=logical_process_axis, forward_only=self.forward_only) # compute the communication cost with CommSpec cost_dict = comm_spec.get_comm_cost() new_dim_partition_dict = deepcopy(source_spec.dim_partition_dict) # We won't add empty list into dim_partition_dict # The key will be popped if the related shard_list is empty if f_shard_list: new_dim_partition_dict[f_index] = f_shard_list else: new_dim_partition_dict.pop(f_index) if b_shard_list: new_dim_partition_dict[b_index] = b_shard_list else: new_dim_partition_dict.pop(b_index) # generate new sharding spec try: new_sharding_spec = ShardingSpec(source_spec.device_mesh, source_spec.entire_shape, dim_partition_dict=new_dim_partition_dict) for phase, cost in cost_dict.items(): cost_dict[phase] = cost + orig_cost_dict[phase] valid_spec_dict[new_sharding_spec] = (comm_spec, cost_dict) except ShardingSpecException: pass return valid_spec_dict def get_all_shard_spec(self, source_spec: ShardingSpec, orig_cost_dict): ''' Get all valid sharding specs from source_spec with single shard operation, and accumulate commucation cost on origin cost which will finally be used in auto sharding solver. For the sharding operation, we just care about legal sharding dimensions. Argument: source_spec(ShardingSpec): the ShardingSpec of the source_spec. orig_cost(float): the original communication cost before this operation. Return: valid_spec_dict(Dict[ShardingSpec, float]): all valid sharding specs from source_spec with single all-to-all operation. Example: dim_partition_dict = {0: [0]} # DistSpec: # shard_sequence: S0,R,R # device_mesh_shape: (4, 4) sharding_spec = ShardingSpec(device_mesh, entire_shape, dim_partition_dict) shape_consistency_manager = ShapeConsistencyManager() rst_dict = shape_consistency_manager.get_all_shard_spec(sharding_spec, {'forward': 0, 'backward': 0, 'total': 0}) print(rst_dict) Output: {DistSpec: shard_sequence: S01,R,R device_mesh_shape: (4, 4): 0, DistSpec: shard_sequence: S0,S1,R device_mesh_shape: (4, 4): 0, DistSpec: shard_sequence: S0,R,S1 device_mesh_shape: (4, 4): 0} ''' valid_spec_dict = {} comm_pattern = CollectiveCommPattern.SPLIT_FWD_GATHER_BWD # legal sharding dims means the mesh_id is still available to use. legal_sharding_dims = [i for i in range(len(source_spec.device_mesh.mesh_shape))] for dim, shard_list in source_spec.dim_partition_dict.items(): for element in shard_list: legal_sharding_dims.remove(element) if len(legal_sharding_dims) == 0: return valid_spec_dict tensor_dims = len(source_spec.entire_shape) for index in range(tensor_dims): if index not in source_spec.dim_partition_dict: shard_list_list = shard_simulator((index, []), legal_sharding_dims) else: shard_list_list = shard_simulator((index, source_spec.dim_partition_dict[index]), legal_sharding_dims) if not shard_list_list: continue for shard_list in shard_list_list: new_dim_partition_dict = deepcopy(source_spec.dim_partition_dict) new_dim_partition_dict[index] = shard_list # generate the CommSpec to record the action of source_sharding_spec->new_sharding_spec shard_dim = index logical_process_axis = shard_list[-1] comm_spec = CommSpec(comm_pattern, sharding_spec=source_spec, gather_dim=shard_dim, shard_dim=shard_dim, logical_process_axis=logical_process_axis, forward_only=self.forward_only) # compute the communication cost with CommSpec cost_dict = comm_spec.get_comm_cost() # generate new sharding spec try: new_sharding_spec = ShardingSpec(source_spec.device_mesh, source_spec.entire_shape, dim_partition_dict=new_dim_partition_dict) for phase, cost in cost_dict.items(): cost_dict[phase] = cost + orig_cost_dict[phase] valid_spec_dict[new_sharding_spec] = (comm_spec, cost_dict) except ShardingSpecException: pass return valid_spec_dict def get_all_mix_gather_spec(self, source_spec: ShardingSpec, orig_cost_dict: Dict[str, float]) -> Dict[ShardingSpec, float]: ''' S0S1 -> RR S1S0 -> RR S01R -> RR RS01 -> RR ''' valid_spec_dict = {} comm_pathern = CollectiveCommPattern.MIXGATHER_FWD_SPLIT_BWD tensor_dims = len(source_spec.entire_shape) for f_index in range(tensor_dims - 1): for b_index in range(f_index + 1, tensor_dims): if (f_index not in source_spec.dim_partition_dict) and (b_index not in source_spec.dim_partition_dict): continue else: if f_index in source_spec.dim_partition_dict: # skip (S10, R) -> (R, R) if len(f_target_pair[1]) == 2 and f_target_pair[1][0] >= f_target_pair[1][1]: continue f_target_pair = (f_index, deepcopy(source_spec.dim_partition_dict[f_index])) else: f_target_pair = (f_index, []) if b_index in source_spec.dim_partition_dict: # skip (R, S10) -> (R, R) if len(b_target_pair[1]) == 2 and b_target_pair[1][0] >= b_target_pair[1][1]: continue b_target_pair = (b_index, deepcopy(source_spec.dim_partition_dict[b_index])) else: b_target_pair = (b_index, []) gather_dim, logical_process_axes = mix_gather_simulator(f_target_pair, b_target_pair) comm_spec = CommSpec(comm_pathern, sharding_spec=source_spec, gather_dim=gather_dim, logical_process_axis=logical_process_axes, forward_only=self.forward_only, mix_gather=True) cost_dict = comm_spec.get_comm_cost() new_dim_partition_dict = {} # generate new sharding spec try: new_sharding_spec = ShardingSpec(source_spec.device_mesh, source_spec.entire_shape, dim_partition_dict=new_dim_partition_dict) for phase, cost in cost_dict.items(): cost_dict[phase] = cost + orig_cost_dict[phase] valid_spec_dict[new_sharding_spec] = (comm_spec, cost_dict) except ShardingSpecException: pass return valid_spec_dict def get_all_one_step_transform_spec(self, source_spec: ShardingSpec, orig_cost_dict) -> Dict[ShardingSpec, float]: ''' Get all valid sharding specs from source_spec with one step transform, and accumulate commucation cost on origin cost which will finally be used in auto sharding solver. Note: all-gather will eliminate a sharding dimension, all-to-all will keep sharding dimension same as before, and shard will add a sharding dimension. Therefore, the result of above operations are mutual exclusive, we could safely put them together. Argument: source_spec(ShardingSpec): the ShardingSpec of the source_spec. orig_cost(float): the original communication cost before this operation. Return: valid_spec_dict(Dict[ShardingSpec, float]): all valid sharding specs from source_spec with single all-to-all operation. ''' valid_spec_dict = {} valid_spec_dict.update(self.get_all_all_gather_spec(source_spec, orig_cost_dict)) valid_spec_dict.update(self.get_all_all_to_all_spec(source_spec, orig_cost_dict)) valid_spec_dict.update(self.get_all_shard_spec(source_spec, orig_cost_dict)) return valid_spec_dict def mem_cost(self, comm_action_sequence: List[CommSpec]) -> TrainCycleItem: """memory cost of the communication action sequence Args: comm_action_sequence (List[CommSpec]): list of communication actions Returns: TrainCycleItem: memory (numel) cost of such comm_action_sequence """ def compute_shape(sharding_spec: ShardingSpec): shape = sharding_spec.entire_shape new_shape = [] for dim, shard in sharding_spec.dim_partition_dict.items(): new_shape.append(shape[dim] // len(shard)) return new_shape def gather_analysis(comm_spec: CommSpec, discard_input: bool, alloc_numel: int, peak_numel: int): """analyze all_gather memory footprint all_gather will allocate memory for the output tensor, and there will be temp memory for all_gather operation, which is twice the size of output tensor Args: comm_spec (CommSpec): input CommSpec discard_input (bool): whether to discard the input tensor alloc_numel (int): current allocated numel peak_numel (int): current peak numel """ input_shape = compute_shape(comm_spec.sharding_spec) input_numel = np.prod(input_shape) output_numel = input_numel * comm_spec.device_mesh.mesh_shape[comm_spec.logical_process_axis] peak_numel = max(peak_numel, alloc_numel + output_numel * 2) alloc_numel += output_numel if discard_input: alloc_numel -= input_numel return alloc_numel, peak_numel def split_analysis(comm_spec: CommSpec, discard_input: bool, alloc_numel: int, peak_numel: int): """analyze split memory footprint split will allocate memory for the output tensor if we don't apply shard on the first dimension of the input tensor. If we apply shard on the first dimension, the `torch.tensor.contiguous()` will not generate new tensor in this case, so no memory will be allocated. Args: comm_spec (CommSpec): input CommSpec discard_input (bool): whether to discard the input tensor alloc_numel (int): current allocated numel peak_numel (int): current peak numel """ shard_dim = comm_spec.shard_dim if shard_dim != 0: # if we don't shard the tensor on the first dimension, the split action will # generate a new tensor input_shape = compute_shape(comm_spec.sharding_spec) input_numel = np.prod(input_shape) output_numel = input_numel // comm_spec.device_mesh.mesh_shape[comm_spec.logical_process_axis] alloc_numel += output_numel peak_numel = max(peak_numel, alloc_numel) if discard_input: alloc_numel -= input_numel else: # if we shard the tensor on the first dimension, the split action will not generate # a new tensor, and as it will preserve a reference to the input tensor, we could # override the discard_input option here # NOTE: this special case might fail in some weird cases, e.g. if we have three split # actions in the comm actions sequence, the first split action operate on the second dimension, # the second split action operate on the first dimension, and the third split action operate, again, # on the second dimension. Therefore, after the first two actions in the sequence, we will allocate # memory the same size as the output of first split action. However, the third split action will discard # the input tensor, and it actually should discard the tensor generated by the first split action, so in # the current memory estimation framework, we will overestimate the memory usage. But the above case is # kind of weird, and I think we could ignore it for now. pass return alloc_numel, peak_numel def reduce_analysis(comm_spec: CommSpec, discard_input: bool, alloc_numel: int, peak_numel: int): """ a dummy function for reduce memory footprint analysis, as the reduce action doesn't allocate extra memory """ return alloc_numel, peak_numel def all2all_analysis(comm_spec: CommSpec, discard_input: bool, alloc_numel: int, peak_numel: int): """analyze all_to_all memory footprint all_to_all will allocate memory for the output tensor, and temp memory of all_to_all action is twice the size of output tensor if we shard input tensor on the first dimension, otherwise the temp memory is three times the size of output tensor Args: comm_spec (CommSpec): input CommSpec discard_input (bool): whether to discard the input tensor alloc_numel (int): current allocated numel peak_numel (int): current peak numel """ input_shape = compute_shape(comm_spec.sharding_spec) input_numel = np.prod(input_shape) output_numel = input_numel shard_dim = comm_spec.shard_dim if shard_dim != 0: peak_numel = max(peak_numel, alloc_numel + output_numel * 3) else: peak_numel = max(peak_numel, alloc_numel + output_numel * 2) alloc_numel += output_numel if discard_input: alloc_numel -= input_numel return alloc_numel, peak_numel def identity_analysis(comm_spec: CommSpec, discard_input: bool, alloc_numel: int, peak_numel: int): """ a dummy function for identity memory footprint analysis, as the identity action doesn't allocate extra memory """ return alloc_numel, peak_numel pattern_to_func_dict = { CollectiveCommPattern.GATHER_FWD_SPLIT_BWD: [gather_analysis, split_analysis], CollectiveCommPattern.ALL2ALL_FWD_ALL2ALL_BWD: [all2all_analysis, all2all_analysis], CollectiveCommPattern.SPLIT_FWD_GATHER_BWD: [split_analysis, gather_analysis], CollectiveCommPattern.ALLREDUCE_FWD_IDENTITY_BWD: [reduce_analysis, identity_analysis], CollectiveCommPattern.IDENTITY_FWD_ALLREDUCE_BWD: [identity_analysis, reduce_analysis], CollectiveCommPattern.MIXGATHER_FWD_SPLIT_BWD: [], } fwd_actions = [] bwd_actions = [] # construct forward and backward comm actions sequence for comm_spec in comm_action_sequence: comm_spec: CommSpec fwd_action, bwd_action = pattern_to_func_dict[comm_spec.comm_pattern] fwd_actions.append(fwd_action) bwd_actions.append(bwd_action) # analyze memory footprint of forward comm actions sequence fwd_alloc_numel = 0 fwd_peak_numel = 0 for idx, action_spec_pair in enumerate(zip(fwd_actions, comm_action_sequence)): # the first forward comm action will not discard input fwd_action, comm_spec = action_spec_pair fwd_alloc_numel, fwd_peak_numel = fwd_action(comm_spec, False, fwd_alloc_numel, fwd_peak_numel) if idx == 0 else fwd_action( comm_spec, True, fwd_alloc_numel, fwd_peak_numel) # analyze memory footprint for backward comm actions sequence bwd_alloc_numel = 0 bwd_peak_numel = 0 for idx, action_spec_pair in enumerate(zip(reversed(bwd_actions), reversed(comm_action_sequence))): bwd_action, comm_spec = action_spec_pair bwd_alloc_numel, bwd_peak_numel = bwd_action(comm_spec, False, bwd_alloc_numel, bwd_peak_numel) if idx == 0 else bwd_action( comm_spec, True, bwd_alloc_numel, bwd_peak_numel) fwd_mem = MemoryCost(activation=fwd_alloc_numel, temp=fwd_peak_numel - fwd_alloc_numel) bwd_mem = MemoryCost(activation=bwd_alloc_numel, temp=bwd_peak_numel - bwd_alloc_numel) total_mem = MemoryCost(activation=fwd_alloc_numel + bwd_alloc_numel) return TrainCycleItem(fwd_mem, bwd_mem, total_mem) def shape_consistency(self, source_spec: ShardingSpec, target_spec: ShardingSpec) -> Tuple[List[ShardingSpec], List[CommSpec], float]: ''' This method will find a path to transform source_spec to target_spec with a greedy algorithm. The basic idea is: Step1: Generate all one-step transform sequences from source_spec. Step2: Pick the 'best' sharding spec following the heuristic function. Step3: Repeat above steps until the source spec transform to target spec. During finding the transform path, commucation cost will be accumulated, and it will be finally used in auto parallel solver. Additionally, to avoid repeating the path search in runtime, we cached all solved path in auto parallel strategy building time, which could handle most of cases in runtime. Argument: source_spec(ShardingSpec): ShardingSpec of the source activation. target_spec(ShardingSpec): ShardingSpec of the target activation. Return: transform_path(List[ShardingSpec]): The transform path from source_spec to target_spec, it contains the source_spec and target_spec. comm_action_sequence(List[CommSpec]): Keep the communication operations to complete the shape consistency in order. total_cost(float): total cost to complete shape consistency transform. Example: dim_partition_source = {1: [0, 1]} dim_partition_target = {0: [0, 1]} # DistSpec: # shard_sequence: R,S01,R # device_mesh_shape: (4, 4) sharding_spec_source = ShardingSpec(device_mesh, entire_shape, dim_partition_source) # DistSpec: # shard_sequence: S01,R,R # device_mesh_shape: (4, 4) sharding_spec_target = ShardingSpec(device_mesh, entire_shape, dim_partition_target) transform_path, comm_action_sequence, total_cost = shape_consistency_manager.shape_consistency(sharding_spec_source, sharding_spec_target) print(f'transform_path: {transform_path}') print(f'comm_action_sequence: {comm_action_sequence}') print(f'total_cost: {total_cost}') output: transform_path: [DistSpec: shard_sequence: R,S01,R device_mesh_shape: (4, 4), DistSpec: shard_sequence: R,S0,R device_mesh_shape: (4, 4), DistSpec: shard_sequence: S0,R,R device_mesh_shape: (4, 4), DistSpec: shard_sequence: S01,R,R device_mesh_shape: (4, 4)] comm_action_sequence: [CommSpec:(comm_pattern:allgather, gather_dim:1, logical_process_axis:1), CommSpec:(comm_pattern:all2all, gather_dim:1, shard_dim:0, logical_process_axis: 0), CommSpec:(comm_pattern:shard, shard_dim:0, logical_process_axis:1)] total_cost: 12294.402000000002 ''' MAX_TRANSFORM_STEPS = 20 total_cost_dict = {'forward': 0, 'backward': 0, 'total': 0} total_steps = 0 transform_path = [] comm_action_sequence = [] spec_pairs = (str(source_spec.sharding_sequence), str(target_spec.sharding_sequence)) self.cached_spec_pairs_transform_path[spec_pairs] = (None, None) # We do nothing if the sharding spec is all the same. if source_spec.sharding_sequence_difference(target_spec) == 0: self.cached_spec_pairs_transform_path[spec_pairs] = (transform_path, comm_action_sequence) return (transform_path, comm_action_sequence, total_cost_dict) temp_sharding_spec = source_spec transform_path.append(temp_sharding_spec) # To avoid dead loop, the loop will break after MAX_TRANSFORM_STEPS transforms while total_steps <= MAX_TRANSFORM_STEPS: valid_transform_spec_dict = self.get_all_one_step_transform_spec(temp_sharding_spec, total_cost_dict) best_difference_score = math.inf for sharding_spec, info_pairs in valid_transform_spec_dict.items(): comm_spec, cost_dict = info_pairs spec_difference = sharding_spec.sharding_sequence_difference(target_spec) if spec_difference == 0: for phase, cost in total_cost_dict.items(): total_cost_dict[phase] = cost + cost_dict[phase] transform_path.append(sharding_spec) comm_action_sequence.append(comm_spec) self.cached_spec_pairs_transform_path[spec_pairs] = (transform_path, comm_action_sequence) return (transform_path, comm_action_sequence, total_cost_dict) if spec_difference < best_difference_score: temp_sharding_spec = sharding_spec temp_cost_dict = cost_dict temp_comm_spec = comm_spec best_difference_score = spec_difference transform_path.append(temp_sharding_spec) comm_action_sequence.append(temp_comm_spec) for phase, cost in total_cost_dict.items(): total_cost_dict[phase] = cost + temp_cost_dict[phase] total_steps += 1 raise RuntimeError(f"Could not find a valid transform path with in {MAX_TRANSFORM_STEPS} steps.") def apply(self, tensor_with_sharding_spec: torch.Tensor, target_spec: ShardingSpec) -> torch.Tensor: ''' Apply target_spec to tensor with source sharding spec, the transform path is generated by the shape_consistency method. Argument: tensor_with_sharding_spec (torch.Tensor): a tensor with source sharding spec to be transformed to the target spec. target_spec (ShardingSpec): The tensor transform processes will be directed by the target_spec. Example: physical_mesh_id = torch.arange(0, 4) mesh_shape = (2, 2) # [[0, 1, # [2, 3]] device_mesh = DeviceMesh(physical_mesh_id, mesh_shape, init_process_group=True) entire_shape = torch.Size((4, 2)) shape_consistency_manager = ShapeConsistencyManager() dim_partition_source = {0: [0]} dim_partition_target = {1: [0]} # DistSpec: # shard_sequence: S0,R # device_mesh_shape: (2, 2) sharding_spec_source = ShardingSpec(device_mesh, entire_shape, dim_partition_source) # DistSpec: # shard_sequence: R,S0 # device_mesh_shape: (2, 2) sharding_spec_target = ShardingSpec(device_mesh, entire_shape, dim_partition_target) if rank in (0, 1): sharded_tensor_0 = torch.zeros(2, 1) sharded_tensor_1 = torch.ones(2, 1) # tensor([[0., 1.], # [0., 1.]]) tensor_to_comm = torch.cat((sharded_tensor_0, sharded_tensor_1), 1).cuda() if rank in (2, 3): sharded_tensor_0 = torch.ones(2, 1) * 2 sharded_tensor_1 = torch.ones(2, 1) * 3 # tensor([[2., 3.], # [2., 3.]]) tensor_to_comm = torch.cat((sharded_tensor_0, sharded_tensor_1), 1).cuda() tensor_to_comm.sharding_spec = sharding_spec_source shape_consistency_manager.apply(tensor_to_comm, sharding_spec_target) print(tensor_to_comm) Output in rank0 and rank2: tensor([[0.], [0.], [2.], [2.]]) Output in rank1 and rank3: tensor([[1.], [1.], [3.], [3.]]) ''' _, comm_action_sequence, _ = self.shape_consistency(tensor_with_sharding_spec.sharding_spec, target_spec) for comm_spec in comm_action_sequence: tensor_with_sharding_spec = comm_spec.covert_spec_to_action(tensor_with_sharding_spec) tensor_with_sharding_spec.sharding_spec = target_spec return tensor_with_sharding_spec def apply_for_autoparallel_runtime(self, tensor, source_spec, target_spec): _, comm_action_sequence, _ = self.shape_consistency(source_spec, target_spec) for comm_spec in comm_action_sequence: tensor = comm_spec.covert_spec_to_action(tensor) tensor.sharding_spec = target_spec return tensor
from enum import Enum from typing import List __all__ = ['ReplicaSpec', 'ShardSpec'] class DistPlacementPattern(Enum): REPLICATE = 'r' SHARD = 's' class _DistSpec: """_DistSpec A class indicates Distributed Specification. The DistSpec is only works for the tensor parallel process groups. Because the dist spec of data parallel process group can be automatically deduced. This is an internal data structrue. The API for users should be `ShardSpec` and `ReplicaSpec`. Args: dist_placement_pattern (DistPlacementPattern): the pattern describing how tensors are distributed among processes. The dist_placement_pattern is picked from a limited set, now including two patterns: replicate and shard. process_group (Optional[ProcessGroup], optional): the process group contains processes. Defaults to None. """ def __init__(self, dist_placement_pattern: DistPlacementPattern, **meta_info): self.placement = dist_placement_pattern for k, v in meta_info.items(): setattr(self, k, v) def __eq__(self, other: "_DistSpec") -> bool: if dir(self) != dir(other): return False for attr in dir(self): if not attr.startswith('__') and getattr(self, attr) != getattr(other, attr): return False return True def __repr__(self) -> str: attr_list = [] for attr in dir(self): if not attr.startswith('__'): attr_list.append(f'{attr}={str(getattr(self, attr))}') attr_str = ", ".join(attr_list) return "DistSpec(" + attr_str + ")" def ReplicaSpec() -> _DistSpec: """ReplicaSpec A distributed specification represents the tensor is replicated among the tensor parallel process group. Returns: _DistSpec: an replicated dist spec instance. """ return _DistSpec(DistPlacementPattern.REPLICATE) def ShardSpec(dims: List[int], num_partitions: List[int]) -> _DistSpec: """ShardSpec A distributed specification represents the tensor is sharded among the tensor parallel process group. Note: Currently, only shard on one dimension is valid. In another word, dims should be of size 1. Args: dims (List[int]): a list of dimensions num_partitions (List[int]): a list of partition number of each dimensions. Returns: _DistSpec: an shard dist spec instance. """ assert isinstance(dims, list) and isinstance(num_partitions, list) assert len(dims) == len(num_partitions) return _DistSpec(DistPlacementPattern.SHARD, dims=tuple(dims), num_partitions=tuple(num_partitions))
from typing import ( Callable, Dict, ) import functools # Custom sharded ops _COLOSSAL_OPS: Dict[str, Callable] = {} def _register_colo_op(op, func): global _COLOSSAL_OPS _COLOSSAL_OPS[op] = func def colo_op_impl(func): """ Provides a way for users to write their own custom operator. This can be used to override existing ColoTensor operators or write a new one not supported by ColoTensor. If the operator in question is covered by ``__torch_function__`` dispatch and has a ColoTensor as any of its parameters, the function provided will be invoked for that operator. Example: >>> @colo_op_impl(torch.nn.functional.linear) >>> def my_custom_linear(types, args, kwargs, process_group): >>> .... >>> >>> input = torch.rand(10, 32) >>> weight = ColoTensor(torch.rand(32, 16)) >>> bias = ColoTensor(torch.rand(16)) >>> # This will call `my_custom_linear` instead of the default. >>> torch.nn.functional.linear(input, weight, bias) The types, args and kwargs parameters are the same parameters that are passed to ``__torch_function__`` dispatch API (https://pytorch.org/docs/stable/notes/extending.html#extending-torch). Args: func(Callable): Torch function for which we want to provide a sharded implementation (ex: torch.nn.functional.linear) """ def decorator_sharded_func(wrapped_func): _register_colo_op(func, wrapped_func) @functools.wraps(wrapped_func) def wrapper(*args, **kwargs): return wrapped_func(*args, **kwargs) return wrapper return decorator_sharded_func
from typing import Dict, Iterator, List, Tuple, Union import torch import torch.nn as nn from colossalai.tensor.colo_tensor import ColoTensor def all_gather_simulator(target_pair): ''' Simulating all-gather operation, analyze the communication cost and simulate the influence of the DimSpec. We don't allow uncontiguous layout, such as all-gather(S012)->S02 is NOT allowed. Therefore, all gather operation just remove the last element in shard list, e.g.: all-gather(S01) -> S0 Argument: target_pair(Tuple[int, List[int]]): The first element is the dimension of tensor to be sharded, and the second element decribes which logical axis will be sharded in that dimension. ''' _, shard_list = target_pair new_shard_list = shard_list[:-1] return new_shard_list def all_to_all_simulator(f_target_pair, b_target_pair): ''' Simulating all-to-all operation, analyze the communication cost and simulate the influence of the DimSpec. We BANNED all representations which shard_list in decreasing order, such as S10, so all-to-all(S0, S1) -> RS01 is NOT allowed. Therefore, if the behind shard_list is not None, we just extend it to the front shard_list. Argument: target_pair(Tuple[int, List[int]]): The first element is the dimension of tensor to be sharded, and the second element decribes which logical axis will be sharded in that dimension. e.g.: all-to-all(S0, S1) -> [S01, R] all-to-all(S0, R) -> [R, S0] Otherwise, we extend the front shard_list to behind. e.g.: all-to-all(R, S1) -> [S1, R] Argument: target_pair(Tuple[int, List[int]]): The first element is the dimension of tensor to be sharded, and the second element decribes which logical axis will be sharded in that dimension. ''' _, f_shard_list = f_target_pair _, b_shard_list = b_target_pair if not len(b_shard_list): b_shard_list.extend(f_shard_list) f_shard_list = [] else: f_shard_list.extend(b_shard_list) b_shard_list = [] return f_shard_list, b_shard_list def shard_simulator(target_pair, legal_sharding_dims): ''' Simulating shard operation, analyze the communication cost(always ZERO) and simulate the influence of the DimSpec. We don't allow uncontiguous layout, such as shard(S0)->S02 is NOT allowed. In addition, We BANNED all representations which shard_list in decreasing order, such as S10, so shard(S0) -> S10 is NOT allowed. Therefore, for the R dimension, we could just append any legal sharding dim on it. e.g.: shard(R) -> S0 For the S dimension, we need to make sure the shard_list after sharding still keep rising order. e.g: shard(S0) -> S01 Argument: target_pair(Tuple[int, List[int]]): The first element is the dimension of tensor to be sharded, and the second element decribes which logical axis will be sharded in that dimension. ''' _, shard_list = target_pair shard_list_list = [] for dim in legal_sharding_dims: if len(shard_list) != 0 and dim <= shard_list[-1]: continue new_shard_list = shard_list + [dim] shard_list_list.append(new_shard_list) return shard_list_list def mix_gather_simulator(f_target_pair, b_target_pair): ''' Assume index of f and b target pairs are 'f' and 'b' S0S1 => Input: (f, [0]), (b, [1]) Output: [b, f], (1, 0) S1S0 => Input: (f, [1]), (b, [0]) Output: [b, f], (0, 1) S01R => Input: (f, [0, 1]), (b, []) Output: [f], (1, 1) RS01 => Input: (f, []), (b, [0, 1]) Output: [b], (1, 1) S10R => Input: (f, [0, 1]), (b, []) Output: [f], (0, 0) RS10 => Input: (f, []), (b, [0, 1]) Output: [b], (0, 0) ''' if f_target_pair[1] and b_target_pair[1]: leading_dim = b_target_pair[1] > f_target_pair[1] return [b_target_pair[0], f_target_pair[0]], [int(leading_dim), int(leading_dim ^ 1)] if f_target_pair[1]: leading_dim = f_target_pair[1][0] < f_target_pair[1][1] return [ f_target_pair[0], ], [int(leading_dim), int(leading_dim)] if b_target_pair[1]: leading_dim = b_target_pair[1][0] < b_target_pair[1][1] return [ b_target_pair[0], ], [int(leading_dim), int(leading_dim)] # The function is credited to PyTorch Team def named_params_with_colotensor( module: nn.Module, prefix: str = '', recurse: bool = True, ) -> Iterator[Tuple[str, Union[nn.Parameter, ColoTensor]]]: r"""Returns an iterator over module parameters (together with the ColoTensor parameters), yielding both the name of the parameter as well as the parameter itself. This is typically passed to a :class:torchshard._shard.sharded_optim.ShardedOptimizer Args: prefix (str): prefix to prepend to all parameter names. recurse (bool): if True, then yields parameters of this module and all submodules. Otherwise, yields only parameters that are direct members of this module. Yields: (string, Union[Tensor, ColoTensor]): Tuple containing the name and parameter (or ColoTensor parameter) Example: >>> model = torch.nn.Linear(*linear_size) >>> delattr(model.weight) >>> setattr(model.weight, ColoTensor(...)) >>> for name, param in named_params_with_colotensor(model): >>> if name in ['weight']: >>> print(param.size()) """ modules = module.named_modules(prefix=prefix) if recurse else [(prefix, module)] memo = set() for mod_prefix, mod in modules: # find all sharded tensor params for name, val in vars(mod).items(): if isinstance(val, ColoTensor) and val not in memo: memo.add(val) name = mod_prefix + ('.' if mod_prefix else '') + name yield name, val # find all nn.Parameters for name, val in module.named_parameters(): yield name, val def _convert_tensor(tensor: torch.Tensor) -> ColoTensor: return ColoTensor(tensor) def convert_parameter(module: torch.nn.Module, param_name: str): # Perform some validation first. if not hasattr(module, param_name): raise ValueError(f'module: {module} does not have parameter with name: {param_name}') tensor = getattr(module, param_name) if not isinstance(tensor, torch.Tensor): raise ValueError( f'Expected {type(module).__name__}.{param_name} to be a Tensor, but found {type(tensor).__name__}') if not tensor.is_contiguous(): raise ValueError(f'param: {param_name} is not a contiguous Tensor') st = _convert_tensor(tensor) # Replace param with ColoTensor. # Need to delete the attribute first since param_name might be # torch.nn.Parameter and can't be replaced with ColoTensor which is # not torch.nn.Parameter. delattr(module, param_name) # Now we can set the attribute appropriately. setattr(module, param_name, st) def convert_dim_partition_dict(dim_size: int, dim_partition_dict: Dict[int, List[int]]) -> Dict[int, List[int]]: ''' This method is used to convert the negative dim value to positive. ''' dims_to_convert = [] for dim, mesh_list in dim_partition_dict.items(): if dim < 0: dims_to_convert.append(dim) for dim in dims_to_convert: dim_partition_dict.pop(dim) dim_partition_dict[dim_size + dim] = mesh_list return dim_partition_dict def merge_same_dim_mesh_list(dim_size: int, dim_partition_dict: Dict[int, List[int]]) -> Dict[int, List[int]]: ''' This method is used to merge the different key value which points to same physical position. For example: dim_partition_dict: {1 :[0], -1: [1]} or {1: [0], 1: [1]} for a 2d tensor, the dim 1 and -1 point same physical position. In this method, above dim_partition_dict will be converted to {1: [0, 1]} ''' converted_dim_partition_dict = {} for dim, mesh_list in dim_partition_dict.items(): if dim < 0: dim = dim_size + dim if dim not in converted_dim_partition_dict: converted_dim_partition_dict[dim] = mesh_list else: converted_dim_partition_dict[dim].extend(mesh_list) return converted_dim_partition_dict
import math from copy import copy from functools import lru_cache from typing import Callable, Optional, Set import torch from colossalai.tensor.dist_spec_mgr import DistSpecManager from colossalai.tensor.distspec import DistPlacementPattern, ReplicaSpec, _DistSpec from colossalai.tensor.process_group import ProcessGroup from colossalai.tensor.tensor_spec import ColoTensorSpec from .const import TensorType from .op_wrapper import _COLOSSAL_OPS @lru_cache(None) def _get_my_nowrap_functions() -> Set[Callable]: Tensor = torch.Tensor return { Tensor._base.__get__, Tensor.grad.__get__, Tensor._grad.__get__, Tensor.data.__get__, # make .data returns torch.Tensor rather than ColoTensor } def _convert_output(output, colo_spec: ColoTensorSpec): if type(output) == torch.Tensor: return ColoTensor.from_torch_tensor(output, colo_spec) elif isinstance(output, (list, tuple)): return type(output)(_convert_output(o, colo_spec) for o in output) else: return output def _get_spec_from_args(args, kwargs) -> ColoTensorSpec: for elem in args: if isinstance(elem, ColoTensor): pg = elem.get_process_group() dp = elem.dist_spec return ColoTensorSpec(pg, dp) elif isinstance(elem, (list, tuple)): spec = _get_spec_from_args(elem, {}) if spec is not None: return spec for k, v in kwargs.items(): if isinstance(v, ColoTensor): pg = v.get_process_group() dp = v.dist_spec return ColoTensorSpec(pg, dp) return None class ColoTensor(torch.Tensor): """ Data Structure for Tensor in Colossal-AI. It is a subclass of torch.Tensor. The Colotensor can be initialized with a PyTorch tensor in the following ways. >>> pg = ProcessGroup() >>> colo_t1 = ColoTensor(torch.randn(2,3), spec = ColoTensorSpec(pg, ReplicaSpec())) >>> # The tensor passed in is a tensor after sharding but not a global tensor. >>> shard_spec = ShardSpec(process_group=ProcessGroup(tp=world_size), >>> dims=[0], >>> num_partitions=[world_size]) >>> tensor_spec = ColoTensorSpec(pg, shard_spec) >>> colo_t2 = ColoTensor.from_torch_tensor(t_ref.clone(), tensor_spec) Args: data (torch.Tensor): a torch tensor used as the payload the colotensor. spec (ColoTensorSpec, optional): the tensor spec of initialization. Defaults to ColoTensorSpec(ReplicaSpec()). """ torch_major = int(torch.__version__.split('.')[0]) torch_minor = int(torch.__version__.split('.')[1]) def __new__(cls, data: torch.Tensor, spec: ColoTensorSpec) -> 'ColoTensor': """ The signature of the __new__ has to be consistent with the torch.Tensor. Args: data (torch.Tensor): a torch tensor used as the payload the colotensor. spec (TensorSpec, optional): the tensor spec of initialization. Returns: ColoTensor: a ColoTensor wrappers the data. """ if data is None: data = torch.empty(0) return torch.Tensor._make_subclass(cls, data, data.requires_grad) def __init__(self, data: torch.Tensor, spec: Optional[ColoTensorSpec] = None) -> None: # If not set spec, use a DP process group and replicate dist spec if spec is None: self.has_initialized = False self.dist_spec = ReplicaSpec() self.compute_spec = None self.process_group = ProcessGroup() else: self.has_initialized = True self.dist_spec = spec.dist_attr self.compute_spec = spec.compute_attr if spec.pg is None: self.process_group = ProcessGroup() else: self.process_group = spec.pg self._type = TensorType.NONMODEL def has_compute_spec(self) -> bool: return self.compute_spec is not None def is_model_data(self) -> bool: return self._type == TensorType.MODEL def get_process_group(self) -> 'ProcessGroup': return self.process_group def set_process_group(self, pg: ProcessGroup): """set_process_group change the pg of the ColoTensor. Note that the valid use cases is limited. It works for the target pg is DP and TP only and current dist spec of the Tensor is Replica. Args: pg (ProcessGroup): target pg """ assert isinstance(pg, ProcessGroup), f"pg as type {type(pg)} is invalid" # if the new pg is the same as the old pg, just returns if self.process_group == pg: return assert self.process_group.tp_world_size() == 1 or self.process_group.dp_world_size() == 1, \ "Can not set_process_group on a ColoTensor whose process_group is both tp > 1 and world group > 1" assert self.dist_spec.placement.value == 'r', \ "Can not set_process_group on a ColoTensor whose dist spec is not Replica" self.process_group = pg def get_tp_world_size(self) -> int: return self.process_group.tp_world_size() def set_dist_spec(self, dist_spec: _DistSpec): """set_dist_spec set dist spec and change the payloads. Args: dist_spec (_DistSpec): target dist spec. """ assert isinstance(dist_spec, _DistSpec) assert self.process_group is not None self._redistribute(dist_spec) def set_tensor_spec(self, dist_spec, compute_spec): if dist_spec is not None: assert isinstance(dist_spec, _DistSpec), f"{type(dist_spec)}" self.set_dist_spec(dist_spec) if compute_spec is not None: self.compute_spec = compute_spec def has_compute_pattern(self, compute_pattern): return self.compute_spec.compute_pattern == compute_pattern @classmethod def __torch_function__(cls, func, types, args=(), kwargs=None): if kwargs is None: kwargs = {} if not all(issubclass(cls, t) for t in types): return NotImplemented global _COLOSSAL_OPS if func in _COLOSSAL_OPS: func = _COLOSSAL_OPS[func] if cls.torch_major > 1 or (cls.torch_major == 1 and cls.torch_minor >= 12): # in order to trigger pre-op hook in the forward of checkpoint module # we have to capture the `backward` function # and make sure that it does not in `torch._C.DisableTorchFunction()` context if func is torch.Tensor.backward: assert len(args) == 1 # only has 1 paramter backward_tensor = torch.Tensor(args[0]) tensor_kwargs = {k: torch.Tensor(v) if torch.is_tensor(v) else v for k, v in kwargs.items()} return backward_tensor.backward(**tensor_kwargs) with torch._C.DisableTorchFunction(): ret = func(*args, **kwargs) if func in _get_my_nowrap_functions(): return ret else: colo_spec = _get_spec_from_args(args, kwargs) return _convert_output(ret, colo_spec) def __repr__(self): output_list = [super(ColoTensor, self).__repr__()] output_list.append(str(self.process_group)) output_list.append(str(self.dist_spec)) if self.compute_spec is not None: output_list.append(str(self.compute_spec)) return "\n".join(output_list) def _redistribute(self, dist_spec: _DistSpec) -> None: """_redistribute Note the function will not handle the logic of backward propagation! It is used during model tensor initializations as an internal function. Args: dist_spec (_DistSpec): the target dist. spec. """ assert self.grad_fn is None, "Current tensor has grad_fn and it can't get converted" with DistSpecManager.no_grad(): self.data = DistSpecManager.handle_trans_spec(self.data, self.dist_spec, dist_spec, self.process_group) self.dist_spec = dist_spec def redistribute(self, dist_spec: _DistSpec, pg: Optional[ProcessGroup] = None) -> 'ColoTensor': """redistribute Redistribute the tensor among processes. The rule is like this: 1. If the pg is None, then redistribute the tensor payload among the TP process group. Keep the DP process group not changed. 2. If the pg is not not None and not equal to the current process group. First, convert the tensor as replicated among the TP process group. Second, reset the process group to the new pg. Third, conver the tensor (new replicated both among the tp process group) to the new dist_spec. Args: dist_spec (_DistSpec): the new dist spec. pg (Optional[ProcessGroup], optional): the new process group . Defaults to None. Returns: ColoTensor: a redistributed colotensor """ if pg is not None and pg != self.get_process_group(): # if the pg is not equal, convert the current tensor to replicated handled = self.redistribute(ReplicaSpec()) else: handled = self pg = self.process_group ret = DistSpecManager.handle_trans_spec(handled, handled.dist_spec, dist_spec, pg) return ColoTensor.from_torch_tensor(ret, ColoTensorSpec(pg=pg, dist_attr=dist_spec)) def to_replicate_(self): """to_replicate_ an inline member function, converting dist spec of the tensor to REPLICATE """ self._redistribute(dist_spec=ReplicaSpec()) def to_replicate(self) -> 'ColoTensor': """to_replicate converting dist spec of the tensor to ReplicaSpec() """ return self.redistribute(ReplicaSpec()) @staticmethod def from_torch_tensor(tensor: torch.Tensor, spec: Optional[ColoTensorSpec] = None) -> 'ColoTensor': """from_torch_tensor A static method builds a `ColoTensor` from a PyTorch Tensor. Args: tensor (torch.Tensor): the pytorch tensor, which is a local tensor for this rank not a global tensor. spec (Optional[ColoTensorSpec], optional): tensor spec. Defaults to None. Returns: ColoTensor: a ColoTensor """ tensor = tensor.as_subclass(ColoTensor) tensor.__init__(tensor, spec=spec) return tensor def __deepcopy__(self, memo): if id(self) in memo: return memo[id(self)] else: with torch._C.DisableTorchFunction(): data = self.data.clone() tensor = ColoTensor(data, spec=copy(ColoTensorSpec(self.process_group, self.dist_spec, self.compute_spec))) memo[id(self)] = tensor return tensor # override builtin functions which must use tensor in replicate placement # def size_local(self, *args) -> torch.Size: with torch._C.DisableTorchFunction(): return super().size(*args) def size_global(self, *args) -> torch.Size: """size_global override the torch buildin size() the shape passed in must be in a replicate placement. Returns: torch.Size: the global tensor shape """ if self.is_replicate(): return self.size_local(*args) spec = self.dist_spec dims = spec.dims num_partitions = spec.num_partitions # import inspect # print(*['{:40}| {}:{}\n'.format(x.function, x.filename, x.lineno) for x in inspect.stack()]) size_list = list(self.size_local()) for dim, num_partition in zip(dims, num_partitions): size_list[dim] *= num_partition if args == (): return torch.Size(size_list) else: return size_list[args[0]] def numel_global(self): """Returns the number of elements in the tensor when it's replicated. """ return math.prod(self.size_global()) # Some API for dist spec check def is_replicate(self): return self.dist_spec.placement == DistPlacementPattern.REPLICATE \ or (len(self.dist_spec.num_partitions) == 1 and self.dist_spec.num_partitions[0] == 1) \ or (self.process_group.tp_world_size() == 1) def is_shard_1dcol(self): return self.dist_spec.placement == DistPlacementPattern.SHARD \ and len(self.dist_spec.dims) == 1 and self.dist_spec.dims[0] == -1 def is_shard_1drow(self): return self.dist_spec.placement == DistPlacementPattern.SHARD \ and len(self.dist_spec.dims) == 1 and self.dist_spec.dims[0] == 0 def is_sharded(self): return self.dist_spec.placement == DistPlacementPattern.SHARD
from contextlib import contextmanager import torch import torch.distributed as dist # from colossalai.nn.layer.utils import divide from numpy import prod from packaging import version from colossalai.logging import get_dist_logger from colossalai.tensor.distspec import _DistSpec from colossalai.tensor.process_group import ProcessGroup # TODO(jiaruifang) circle import, move the divide to colossalai.commons. # colossalai.tensor shall not import any submodule from colossal.nn def divide(numerator, denominator): """Only allow exact division. Args: numerator (int): Numerator of the division. denominator (int): Denominator of the division. Returns: int: the result of exact division. """ assert denominator != 0, 'denominator can not be zero' assert numerator % denominator == 0, \ '{} is not divisible by {}'.format(numerator, denominator) return numerator // denominator class TransformDistSpec(torch.autograd.Function): @staticmethod def forward(ctx, tensor, old_dist_spec, dist_spec, pg, forward_trans_func, backward_trans_func): ctx.old_dist_spec = old_dist_spec ctx.dist_spec = dist_spec ctx.backward_trans_func = backward_trans_func ctx.pg = pg return forward_trans_func(tensor, old_dist_spec, dist_spec, pg) @staticmethod def backward(ctx, grad_outputs): return ctx.backward_trans_func(grad_outputs, ctx.dist_spec, ctx.old_dist_spec, ctx.pg), None, None, None, None, None class DistSpecManager: _use_autograd_function: bool = True @staticmethod def _sanity_check(old_dist_spec: _DistSpec, dist_spec: _DistSpec) -> None: pass @staticmethod def _shard_as(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: """_shard_as: shard the tensor w.r.t a distributed specification. Assuming the tensor passed in is a global (replicated) tensor. Args: tensor (torch.Tensor): a global (replicated) tensor before shard dist_spec (_DistSpec): the distributed spec. to be sharded as. pg (ProcessGrouo): the process group of the corresponding colotensor Returns: torch.Tensor: a torch tensor after sharded. """ assert old_dist_spec.placement.value == 'r', f"The old_dist_spec of DistSpecManager._shard_as must be REPLICATE!" DistSpecManager._sanity_check(old_dist_spec, dist_spec) chunk = tensor idx = pg.tp_local_rank() num_parts = prod(dist_spec.num_partitions) for i, dim in enumerate(dist_spec.dims): num_parts //= dist_spec.num_partitions[i] chunk_size = divide(tensor.size(dim), dist_spec.num_partitions[i]) chunk = chunk.narrow(dim, idx // num_parts * chunk_size, chunk_size) idx %= num_parts return chunk.clone().detach().contiguous() @staticmethod def _gather(tensor: torch.Tensor, old_dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: """_gather gather sharded tensors to a replicated one. Args: tensor (torch.Tensor): a shared torch tensor old_dist_spec (_DistSpec): the distributed spec. of the tensor. Returns: torch.Tensor: a replicated tensor. """ assert old_dist_spec.placement.value == 's', f"The old_dist_spec of DistSpecManager._gather must be SHARD!" is_cpu_tensor = False if tensor.device.type == 'cpu': # pytorch lower than 1.11 dose not support gather a cpu tensor. # Therefore, we transfer tensor to GPU before gather. saved_dev = tensor.device tensor.data = tensor.data.cuda() is_cpu_tensor = True buffer = [torch.empty_like(tensor) for _ in range(pg.tp_world_size())] assert tensor.device.type == 'cuda' dist.all_gather(buffer, tensor, group=pg.tp_process_group()) for i in range(len(old_dist_spec.dims) - 1, -1, -1): new_buffer = [] dim = old_dist_spec.dims[i] num_parts = old_dist_spec.num_partitions[i] for start in range(0, len(buffer), num_parts): new_buffer.append(torch.cat(buffer[start:start + num_parts], dim)) buffer = new_buffer assert len(buffer) == 1 if is_cpu_tensor: buffer[0].data = buffer[0].data.to(saved_dev) return buffer[0] @staticmethod def _all_to_all(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: world_size = pg.tp_world_size() if world_size == 1: return tensor assert tensor.device.type == "cuda", \ "Currently, only CUDA Tensor with NCCL backend is supported for the requested AlltoAll " \ f"collective function, however, we got {tensor.device.type} device" gather_dim = old_dist_spec.dims[0] scatter_dim = dist_spec.dims[0] shapes = list(tensor.shape) scattered_dim_size = shapes[scatter_dim] // world_size gathered_dim_size = shapes[gather_dim] * world_size shapes[scatter_dim] = scattered_dim_size scatter_list = [t.contiguous() for t in torch.tensor_split(tensor, world_size, scatter_dim)] gather_list = [torch.empty(*shapes, dtype=tensor.dtype, device=tensor.device) for _ in range(world_size)] dist.all_to_all(gather_list, scatter_list, group=pg.tp_process_group()) output_ = torch.cat(gather_list, dim=gather_dim).contiguous() assert output_.shape[scatter_dim] == scattered_dim_size and output_.shape[gather_dim] == gathered_dim_size return output_ @staticmethod def _r2r(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: DistSpecManager._sanity_check(old_dist_spec, dist_spec) return tensor @staticmethod def _r2s(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: DistSpecManager._sanity_check(old_dist_spec, dist_spec) return DistSpecManager._shard_as(tensor, old_dist_spec, dist_spec, pg) @staticmethod def _s2r(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: DistSpecManager._sanity_check(old_dist_spec, dist_spec) return DistSpecManager._gather(tensor, old_dist_spec, pg) @staticmethod def _s2s(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: DistSpecManager._sanity_check(old_dist_spec, dist_spec) if old_dist_spec == dist_spec: return tensor if len(old_dist_spec.dims) == 1 and len(dist_spec.dims) == 1: # use all-to-all to save memory return DistSpecManager._all_to_all(tensor, old_dist_spec, dist_spec, pg) tensor = DistSpecManager._gather(tensor, old_dist_spec, pg) return DistSpecManager._shard_as(tensor, old_dist_spec, dist_spec, pg) @staticmethod def handle_trans_spec(tensor: torch.Tensor, old_dist_spec: _DistSpec, dist_spec: _DistSpec, pg: ProcessGroup) -> torch.Tensor: assert isinstance(old_dist_spec, _DistSpec), f"{type(old_dist_spec)} should be _DistSpec" assert isinstance(dist_spec, _DistSpec), f"{type(dist_spec)} should be _DistSpec" forward_trans_handle = getattr(DistSpecManager, f'_{old_dist_spec.placement.value}2{dist_spec.placement.value}') if not DistSpecManager._use_autograd_function: return forward_trans_handle(tensor, old_dist_spec, dist_spec, pg) backward_trans_handle = getattr(DistSpecManager, f'_{dist_spec.placement.value}2{old_dist_spec.placement.value}') return TransformDistSpec.apply(tensor, old_dist_spec, dist_spec, pg, forward_trans_handle, backward_trans_handle) @staticmethod @contextmanager def no_grad(): try: DistSpecManager._use_autograd_function = False yield finally: DistSpecManager._use_autograd_function = True
import operator from copy import deepcopy from functools import reduce import torch from colossalai.device.device_mesh import DeviceMesh from .utils import merge_same_dim_mesh_list __all__ = ['_DimSpec', 'ShardingException', 'ShardingSpec'] ALLGATHER_COST = 20 SHARD_COST = 5 STEP_PENALTY = 6 NAN = 'nan' class _DimSpec: ''' Sharding spec for single dimension of the sharded tensor decribe the sharding dimension of logical device mesh and give a method to compute the difference between them. This class is used internally in ShardingSpec. Argument: shard_list(List[int]): if shard_list is None, the dim spec will be 'R' type. Otherwise, the element in shard_list means the data will be sharded in that dimension. ''' def __init__(self, shard_list): self.is_replica = len(shard_list) == 0 self.shard_list = shard_list self.build_difference_2d_dict() def __eq__(self, other): return str(self) == str(other) def __repr__(self): if self.is_replica: return 'R' target = 'S' for dim in self.shard_list: target += str(dim) return target def _convert_str_to_shard_list(self, str_spec): ''' Conver str_spec into shard_list. Argument: str_spec(str): dim spec in str type. ''' if str_spec == 'R': return [] if str_spec == 'S0': return [0] if str_spec == 'S1': return [1] if str_spec == 'S01': return [0, 1] def build_difference_2d_dict(self): ''' Build a difference maping for 2D device mesh case. It will be used to compute the difference between DimSpec pairs. ''' source_spec_list = ['R', 'S0', 'S1', 'S01'] target_spec_list = ['R', 'S0', 'S1', 'S01'] difference_dict = {} for source_spec in source_spec_list: for target_spec in target_spec_list: legal_sharding_dims = [] spec_pair = (deepcopy(source_spec), deepcopy(target_spec)) source_shard_list = self._convert_str_to_shard_list(source_spec) target_shard_list = self._convert_str_to_shard_list(target_spec) # source same as target if source_shard_list == target_shard_list: difference = 0 # all_gather(source) -> target elif len(source_shard_list ) == len(target_shard_list) + 1 and source_shard_list[:-1] == target_shard_list: difference = ALLGATHER_COST # shard(source) -> target elif len(source_shard_list) == len( target_shard_list) - 1 and source_shard_list == target_shard_list[:-1] and target_shard_list[ -1] not in source_shard_list: difference = SHARD_COST # S1 -> S0 or S0 -> S1 elif len(source_shard_list) == len(target_shard_list): # source -> R -> target difference = ALLGATHER_COST + STEP_PENALTY + SHARD_COST # R -> S01 elif len(source_shard_list) == len(target_shard_list) - 2: difference = SHARD_COST + STEP_PENALTY + SHARD_COST # S01 -> R elif len(source_shard_list) == len(target_shard_list) + 2: difference = ALLGATHER_COST + STEP_PENALTY + ALLGATHER_COST # S1 -> S01 elif len(source_shard_list) == len(target_shard_list) - 1: difference = ALLGATHER_COST + STEP_PENALTY + SHARD_COST + STEP_PENALTY + SHARD_COST # S01 -> S1 elif len(source_shard_list) == len(target_shard_list) + 1: difference = ALLGATHER_COST + STEP_PENALTY + ALLGATHER_COST + STEP_PENALTY + SHARD_COST else: difference = NAN difference_dict[spec_pair] = difference self.difference_dict = difference_dict def difference(self, other): ''' The difference between two _DimSpec. Argument: other(_DimSpec): the dim spec to compare with. Return: difference(int): the difference between two _DimSpec. Example: dim_spec = _DimSpec([0]) other_dim_spec = _DimSpec([0, 1]) print(dim_spec.difference(other_dim_spec)) Output: 5 ''' difference = self.difference_dict[(str(self), str(other))] return difference class ShardingSpecException(Exception): pass class ShardingOutOfIndexError(ShardingSpecException): pass class DuplicatedShardingDimensionError(ShardingSpecException): pass class ShardingNotDivisibleError(ShardingSpecException): pass class ShardingSpec: ''' Sharding spec for a tensor, it contains info of the logical device mesh this tensor belong to, the entire shape of the tensor before sharded, and the sharding sequence looks like [R, R, S0, S1]. Argument: device_mesh(DeviceMesh): A logical view of a physical mesh. entire_shape(torch.Size): The entire shape of tensor before sharded. dim_partition_dict(Dict[int, List[int]], optional): The key is the dimension of tensor to be sharded, and the value of the key decribe which logical axis will be sharded in that dimension. sharding_sequence(List[_DimSpec], optional): A straight view of ShardingSpec looks like [R, R, S0, S1]. ''' def __init__(self, device_mesh: DeviceMesh, entire_shape: torch.Size, dim_partition_dict=None, sharding_sequence=None): self.device_mesh = device_mesh if isinstance(entire_shape, (list, tuple)): entire_shape = torch.Size(entire_shape) self.entire_shape = entire_shape self.dim_partition_dict = dim_partition_dict self.sharding_sequence = sharding_sequence if self.sharding_sequence is None: assert self.dim_partition_dict is not None, f'dim_partition_dict should not be None, if sharding_sequence is NoneType object.' self.dim_partition_dict = merge_same_dim_mesh_list(dim_size=len(entire_shape), dim_partition_dict=self.dim_partition_dict) self.convert_dict_to_shard_sequence() elif self.dim_partition_dict is None: assert self.sharding_sequence is not None, f'sharding_sequence should not be None, if dim_partition_dict is NoneType object.' self.convert_shard_sequence_to_dict() self._sanity_check() def __repr__(self): res_list = ["DistSpec:"] res_list.append(f"\n\tshard_sequence: " + ",".join(str(dimspec) for dimspec in self.sharding_sequence)) res_list.append(f"\n\tdevice_mesh_shape: {self.device_mesh.mesh_shape}") return ' '.join(res_list) def _sanity_check(self): # make sure all axes in logical device mesh only be used once dim_check_list = list(range(self.device_mesh.logical_mesh_id.dim())) for dim, shard_list in self.dim_partition_dict.items(): for element in shard_list: if element in dim_check_list: dim_check_list.remove(element) else: raise DuplicatedShardingDimensionError( f"find an invalid sharding axis {element} in dim_partition_dict in tensor dimension {dim}.") # make sure that the dimension is not out of index for dim in self.dim_partition_dict.keys(): if dim >= len(self.entire_shape): raise ShardingOutOfIndexError( f"The dim_partition_dict specifies to shard dimension {dim} but the entire_shape only has {len(self.entire_shape)} dimensions" ) # make sure that the sharding for a dimension is divisible by the number of devices for dim, shard_list in self.dim_partition_dict.items(): tensor_dim_size = self.entire_shape[dim] num_devices = 1 for element in shard_list: num_devices *= self.device_mesh.mesh_shape[element] if tensor_dim_size % num_devices != 0: raise ShardingNotDivisibleError( f'The size of dimension at index {dim} is {tensor_dim_size}, it cannot be sharded over {num_devices} devices.' ) def convert_dict_to_shard_sequence(self): ''' Convert dim_partition_dict into list of _DimSpec, and assign it to sharding_sequence. ''' sharding_sequence = [_DimSpec([])] * len(self.entire_shape) for dim, shard_list in self.dim_partition_dict.items(): sharding_sequence[dim] = _DimSpec(shard_list) self.sharding_sequence = sharding_sequence def convert_shard_sequence_to_dict(self): ''' Convert sharding_sequence into dim_partition_dict. ''' new_dim_partition_dict = {} for index, dim_spec in enumerate(self.sharding_sequence): if not dim_spec.is_replica: if index not in new_dim_partition_dict: new_dim_partition_dict[index] = [] new_dim_partition_dict[index].extend(dim_spec.shard_list) self.dim_partition_dict = new_dim_partition_dict def sharding_sequence_difference(self, other): ''' This function is a naive version of difference computation. It just simply accumulates difference every dimension between the pair of sharding sequence. Example: dim_partition_dict = {0: [0, 1]} # DistSpec: # shard_sequence: S01,R,R # device_mesh_shape: (4, 4) sharding_spec = ShardingSpec(device_mesh, entire_shape, dim_partition_dict) dim_partition_dict_to_compare = {0: [0], 1: [1]} # DistSpec: # shard_sequence: S0,S1,R # device_mesh_shape: (4, 4) sharding_spec_to_compare = ShardingSpec(device_mesh, entire_shape, dim_partition_dict_to_compare) print(sharding_spec.sharding_sequence_difference(sharding_spec_to_compare)) Output: 25 Argument: other(ShardingSpec): The ShardingSpec to compared with. Return: difference(int): Difference between two ShardingSpec. ''' assert len(self.sharding_sequence) == len( other.sharding_sequence), f'Cannot compare difference for two sharding specs with different length.' difference = 0 for orig_dim_spec, other_dim_spec in zip(self.sharding_sequence, other.sharding_sequence): difference += orig_dim_spec.difference(other_dim_spec) return difference def get_sharded_shape_per_device(self): sharded_shape = list(self.entire_shape) for dim, shard_list in self.dim_partition_dict.items(): mesh_list = [self.device_mesh.mesh_shape[mesh_dim] for mesh_dim in shard_list] shard_partitions = reduce(operator.mul, mesh_list, 1) assert sharded_shape[ dim] % shard_partitions == 0, f'Cannot shard dimension {dim} into {shard_partitions} partitions.' sharded_shape[dim] //= shard_partitions return torch.Size(sharded_shape)
from typing import List, Optional import torch from colossalai.context.singleton_meta import SingletonMeta from colossalai.logging import get_dist_logger class PyTorchProcessGroupDict(metaclass=SingletonMeta): def __init__(self): # distributed settings # use this dict to record all Pytorch ProcessGroups self.dict = {} # set a distributed logger self.logger = get_dist_logger('ProcessGroup') def log_pg_init(self, rank_list: List[int], backend: str): str_list = ["Pytorch ProcessGroup Init:"] str_list.append(f"backend: {backend}") str_list.append(f"ranks: {rank_list}") self.logger.info("\n\t".join(str_list), ranks=[0]) def get(self, rank_list: List[int], backend: str = 'nccl'): """Reuse Pytorch ProcessGroup when such a group is initialized """ # we need to convert the passed list to a tuple # since List is unhashable processgroup_key = (backend, tuple(rank_list)) if processgroup_key not in self.dict: self.log_pg_init(rank_list=rank_list, backend=backend) self.dict[processgroup_key] = torch.distributed.new_group(ranks=rank_list, backend=backend) return self.dict[processgroup_key] PYTORCHPGDICT_ = PyTorchProcessGroupDict() class ProcessGroup: """ProcessGroup Process Group indicates how processes are organized in groups for parallel execution using Tensor Parallelism and Data Parallelism. NOTE, the ProcessGroup must be used after `torch.distributed.initialize()` Args: rank: the global rank of the current process. ranks: List[int], a list of rank id belongings to this process group. backend: str, the backend of the process group. tp_degree: Optional[int], tensor parallelism degree. How many processes are inside a tp process group. default None means 1. dp_degree: Optional[int], data parallelism degree. How many processes are inside a dp process group. . default None means len(ranks). """ def __init__(self, rank: Optional[int] = None, ranks: Optional[List[int]] = None, tp_degree: Optional[int] = None, dp_degree: Optional[int] = None) -> None: if not torch.distributed.is_initialized(): self.is_init = False return assert torch.distributed.is_initialized(), f"ProcessGroup must be used after distributed initialized" self._rank = torch.distributed.get_rank() if rank is not None: assert self._rank == rank # make sure that the global rank is correct if ranks is None: self._rank_list = list(range(torch.distributed.get_world_size())) else: self._rank_list = ranks self._rank_list.sort() # ensure that the list is in order self._world_size = len(self._rank_list) if dp_degree is None and tp_degree is None: self._dp_degree = self._world_size self._tp_degree = 1 elif dp_degree and not tp_degree: self._dp_degree = dp_degree assert self._world_size % self._dp_degree == 0, f"DP degree {dp_degree} should be divisible by {self._world_size} hen DP degree is None" self._tp_degree = self._world_size // dp_degree elif not dp_degree and tp_degree: self._tp_degree = tp_degree assert self._world_size % self._tp_degree == 0, f"TP degree {tp_degree} should be divisible by {self._world_size} when DP degree is None" self._dp_degree = self._world_size // tp_degree else: self._dp_degree = dp_degree self._tp_degree = tp_degree assert self._dp_degree * self._tp_degree == self._world_size, \ f"the world size {self._world_size} should equals to the product of DP degree {self._dp_degree}" \ f"and TP degree {self._tp_degree}" self._tp_rank_list = None self._dp_rank_list = None for i in range(self._dp_degree): i_tp_list = [self._rank_list[i * self._tp_degree + j] for j in range(self._tp_degree)] PYTORCHPGDICT_.get(i_tp_list, 'nccl') if self._rank in i_tp_list: self._tp_rank_list = i_tp_list for j in range(self._tp_degree): j_dp_list = [self._rank_list[i * self._tp_degree + j] for i in range(self._dp_degree)] PYTORCHPGDICT_.get(j_dp_list, 'nccl') if self._rank in j_dp_list: self._dp_rank_list = j_dp_list self._has_cpu_groups = False self.is_init = True def set_cpu_groups(self): """set_cpu_groups Initialize Pytorch process groups for cpu communications. """ if self.has_cpu_groups: return for i in range(self._dp_degree): i_tp_list = [self._rank_list[i * self._tp_degree + j] for j in range(self._tp_degree)] PYTORCHPGDICT_.get(i_tp_list, 'gloo') for j in range(self._tp_degree): j_dp_list = [self._rank_list[i * self._tp_degree + j] for i in range(self._dp_degree)] PYTORCHPGDICT_.get(j_dp_list, 'gloo') self._has_cpu_groups = True @property def has_cpu_groups(self) -> bool: """has_cpu_groups If cpu groups have been initailized. Returns: bool: cpu process groups have been initialized or not. """ return self._has_cpu_groups def __repr__(self): if self.is_init: ranks_str = f"ProcessGroup(ranks={self._rank_list},\n" personal_str = f" rank={self._rank}, dp={self._dp_degree}, tp={self._tp_degree})" return ranks_str + personal_str else: return "ProcessGroup not initialized" def __eq__(self, obj: 'ProcessGroup') -> bool: if not isinstance(obj, ProcessGroup): return False if self._rank != obj._rank: return False if self._rank_list != obj._rank_list: return False if self._tp_rank_list != obj._tp_rank_list: return False if self._dp_rank_list != obj._dp_rank_list: return False if self._tp_degree != obj._tp_degree: return False if self._dp_degree != obj._dp_degree: return False return True def rank(self) -> int: """rank The current rank in the global process group. Returns: int: the rank number """ return self._rank def ranks_in_group(self) -> List[int]: """ranks_in_group a list of rank number in in the global process group. Returns: List[int]: a list of rank number. """ return self._rank_list def world_size(self) -> int: """world_size The world size of the global process group. Returns: int: world size """ return self._world_size def tp_rank_list(self) -> List[int]: """tp_rank_list the rank list in the TP process group containing the current rank. Returns: List[int]: the list of rank number. """ return self._tp_rank_list def dp_rank_list(self) -> List[int]: """dp_rank_list the rank list in the DP process group containing the current rank. Returns: List[int]: the list of rank number. """ return self._dp_rank_list def tp_local_rank(self) -> int: """tp_local_rank The local rank number in the current TP process group. Returns: int: tp rank number. """ return self._rank % self._tp_degree def dp_local_rank(self) -> int: """dp_local_rank The local rank number in the current DP process group. Returns: int: dp rank number. """ return self._rank // self._tp_degree def dp_world_size(self) -> int: """dp_world_size The world size of the current DP process group. Returns: int: dp world size """ return len(self._dp_rank_list) def tp_world_size(self) -> int: """tp_world_size The world size of the current TP process group. Returns: int: tp world size """ return len(self._tp_rank_list) def dp_process_group(self): """dp_process_group the pytorch DP process group containing the current rank. Returns: `torch._C._distributed_c10d.ProcessGroup`: the pytorch DP process group. """ return PYTORCHPGDICT_.get(self._dp_rank_list, 'nccl') def tp_process_group(self): """tp_process_group the pytorch TP process group containing the current rank. Returns: `torch._C._distributed_c10d.ProcessGroup`: the pytorch TP process group. """ return PYTORCHPGDICT_.get(self._tp_rank_list, 'nccl') def cpu_dp_process_group(self): """cpu_dp_process_group the pytorch CPU DP process group containing the current rank. assert failed if cpu process group is not initialized. Returns: `torch._C._distributed_c10d.ProcessGroup`: the pytorch DP process group. """ assert self._has_cpu_groups return PYTORCHPGDICT_.get(self._dp_rank_list, 'gloo') def cpu_tp_process_group(self): """cpu_tp_process_group the pytorch CPU TP process group containing the current rank. assert failed if cpu process group is not initialized. Returns: `torch._C._distributed_c10d.ProcessGroup`: the pytorch TP process group. """ assert self._has_cpu_groups return PYTORCHPGDICT_.get(self._tp_rank_list, 'gloo') def get_ranks_in_dp(self) -> List[int]: """get_ranks_in_dp ranks in current dp process group. Returns: List[int]: a list of rank number. """ return self._dp_rank_list def get_ranks_in_tp(self): """get_ranks_in_tp ranks in current tp process group. Returns: List[int]: a list of rank number. """ return self._tp_rank_list
from enum import Enum class TensorType(Enum): MODEL = 0 NONMODEL = 1 # mainly activations
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch import torch.distributed as dist from torch import Tensor from torch.distributed import ReduceOp from colossalai.context import ParallelMode from colossalai.core import global_context as gpc _all_gather_func = dist._all_gather_base \ if "all_gather_into_tensor" not in dir(dist) else dist.all_gather_into_tensor _reduce_scatter_func = dist._reduce_scatter_base \ if "reduce_scatter_tensor" not in dir(dist) else dist.reduce_scatter_tensor def all_gather(tensor: Tensor, dim: int, parallel_mode: ParallelMode, async_op: bool = False) -> Tensor: r"""Gathers all tensors from the parallel group and concatenates them in a specific dimension. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. Args: tensor (:class:`torch.Tensor`): Tensor to be gathered. dim (int): The dimension concatenating in. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication. async_op (bool, optional): Whether operations are asynchronous. Returns: Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of all-together only, if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True. """ depth = gpc.get_world_size(parallel_mode) if depth == 1: out = tensor work = None else: tensor_in = tensor.contiguous() if dim == 0 else tensor.transpose(0, dim).contiguous() out_shape = (tensor_in.shape[0] * depth,) + tensor_in.shape[1:] tensor_out = torch.empty(out_shape, dtype=tensor.dtype, device=tensor.device) group = gpc.get_cpu_group(parallel_mode) if tensor.device.type == "cpu" else gpc.get_group(parallel_mode) work = _all_gather_func(tensor_out, tensor_in, group=group, async_op=async_op) out = tensor_out if dim == 0 else tensor_out.transpose(0, dim) if async_op: return out, work else: return out def reduce_scatter(tensor: Tensor, dim: int, parallel_mode: ParallelMode, op: ReduceOp = ReduceOp.SUM, async_op: bool = False) -> Tensor: r"""Reduces all tensors then scatters it in a specific dimension to all members in the parallel group. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. Args: tensor (:class:`torch.Tensor`): Tensor to be reduce_scattered. dim (int): The dimension concatenating in. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication. op (torch.distributed.ReduceOp, optional): The type of reduce operation, should be included in [SUM, AVG, PRODUCT, MIN, MAX, BAND, BOR, BXOR]. More details about ReduceOp please refer to `ReduceOp <https://pytorch.org/docs/stable/distributed.html#torch.distributed.ReduceOp>`_. async_op (bool, optional): Whether operations are asynchronous. Returns: Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of reduce_scatter only, if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True. """ depth = gpc.get_world_size(parallel_mode) if depth == 1: out = tensor work = None else: tensor_in = tensor.contiguous() if dim == 0 else tensor.transpose(0, dim).contiguous() out_shape = (tensor_in.shape[0] // depth,) + tensor_in.shape[1:] tensor_out = torch.empty(out_shape, dtype=tensor.dtype, device=tensor.device) group = gpc.get_cpu_group(parallel_mode) if tensor.device.type == "cpu" else gpc.get_group(parallel_mode) work = _reduce_scatter_func(tensor_out, tensor_in, op=op, group=group, async_op=async_op) out = tensor_out if dim == 0 else tensor_out.transpose(0, dim) if async_op: return out, work else: return out def all_reduce(tensor: Tensor, parallel_mode: ParallelMode, op: ReduceOp = ReduceOp.SUM, async_op: bool = False) -> Tensor: r"""Reduces the tensor data across whole parallel group in such a way that all get the final result. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. Args: tensor (:class:`torch.Tensor`): Tensor to be all-reduced. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication. op (torch.distributed.ReduceOp, optional): The type of reduce operation, should be included in [SUM, AVG, PRODUCT, MIN, MAX, BAND, BOR, BXOR]. More details about ReduceOp please refer to `ReduceOp <https://pytorch.org/docs/stable/distributed.html#torch.distributed.ReduceOp>`_. async_op (bool, optional): Whether operations are asynchronous. Returns: Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of all-gather only, if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True. """ depth = gpc.get_world_size(parallel_mode) if depth == 1: out = tensor work = None else: out = tensor.contiguous() group = gpc.get_cpu_group(parallel_mode) if tensor.device.type == "cpu" else gpc.get_group(parallel_mode) work = dist.all_reduce(out, op=op, group=group, async_op=async_op) if async_op: return out, work else: return out def broadcast(tensor: Tensor, src: int, parallel_mode: ParallelMode, async_op: bool = False): r"""Broadcast tensors to whole parallel group. Tensor must have the same number of elements in all processes participating in the collective. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. Args: tensor (:class:`torch.Tensor`): Tensor to be broadcast. src (int): Source rank. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication. async_op (bool, optional): Whether operations are asynchronous. Returns: Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The tensor need to be broadcast only, if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True. """ depth = gpc.get_world_size(parallel_mode) if depth == 1: out = tensor work = None else: out = tensor.contiguous() group = gpc.get_cpu_group(parallel_mode) if tensor.device.type == "cpu" else gpc.get_group(parallel_mode) work = dist.broadcast(out, src=src, group=group, async_op=async_op) if async_op: return out, work else: return out def reduce(tensor: Tensor, dst: int, parallel_mode: ParallelMode, op: ReduceOp = ReduceOp.SUM, async_op: bool = False): r"""Reduce tensors across whole parallel group. Only the process with rank ``dst`` is going to receive the final result. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. Args: tensor (:class:`torch.Tensor`): Tensor to be reduced. dst (int): Destination rank. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel group mode used in this communication. async_op (bool, optional): Whether operations are asynchronous. Returns: Union[tuple(:class:`torch.Tensor`, work handle), :class:`torch.Tensor`]: The result of reduce only, if async_op is set to False. A tuple of output of all-gather and Async work handle, if async_op is set to True. """ depth = gpc.get_world_size(parallel_mode) if depth == 1: out = tensor work = None else: out = tensor.contiguous() group = gpc.get_cpu_group(parallel_mode) if tensor.device.type == "cpu" else gpc.get_group(parallel_mode) work = dist.reduce(out, dst=dst, op=op, group=group, async_op=async_op) if async_op: return out, work else: return out def scatter_object_list(scatter_object_output_list, scatter_object_input_list, src=0, group=None) -> None: r"""Modified from `torch.distributed.scatter_object_list <https://pytorch.org/docs/stable/_modules/torch/distributed/distributed_c10d.html#scatter_object_list>` to fix issues """ if dist.distributed_c10d._rank_not_in_group(group): return if (not isinstance(scatter_object_output_list, list) or len(scatter_object_output_list) < 1): raise RuntimeError("Expected argument scatter_object_output_list to be a list of size at least 1.") # set tensor device to cuda if backend is nccl device = torch.cuda.current_device() if dist.get_backend(group) == 'nccl' else torch.device("cpu") my_rank = dist.get_rank() # use global rank if my_rank == src: tensor_list, tensor_sizes = zip( *[dist.distributed_c10d._object_to_tensor(obj) for obj in scatter_object_input_list]) tensor_list = list(map(lambda x: x.to(device), tensor_list)) tensor_sizes = list(map(lambda x: x.to(device), tensor_sizes)) # Src rank broadcasts the maximum tensor size. This is because all ranks are # expected to call into scatter() with equal-sized tensors. if my_rank == src: max_tensor_size = max(tensor_sizes) for tensor in tensor_list: tensor.resize_(max_tensor_size) else: max_tensor_size = torch.tensor([0], dtype=torch.long).to(device) dist.broadcast(max_tensor_size, src=src, group=group) # Scatter actual serialized objects output_tensor = torch.empty(max_tensor_size.item(), dtype=torch.uint8).to(device) dist.scatter( output_tensor, scatter_list=None if my_rank != src else tensor_list, src=src, group=group, ) # Scatter per-object sizes to trim tensors when deserializing back to object obj_tensor_size = torch.tensor([0], dtype=torch.long).to(device) dist.scatter( obj_tensor_size, scatter_list=None if my_rank != src else tensor_sizes, src=src, group=group, ) output_tensor, obj_tensor_size = output_tensor.cpu(), obj_tensor_size.cpu() # Deserialize back to object scatter_object_output_list[0] = dist.distributed_c10d._tensor_to_object(output_tensor, obj_tensor_size)
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import List, Tuple, Union import torch import torch.distributed as dist from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.utils import get_current_device from functools import reduce import operator from .utils import split_tensor_into_1d_equal_chunks, gather_split_1d_tensor TensorShape = Union[torch.Size, List[int], Tuple[int]] def _get_tensor_shape(tensor_shape: TensorShape, chunk_tensor: bool = False) -> Tuple[TensorShape, bool]: """get the exact tensor shape when communicating and return whether the tensor is a chunk Args: tensor_shape (:class:`torch.Size`): shape of tensor chunk_tensor (bool, optional): whether to chunk tensor, defaults to False Returns: Tuple[Union[:class:`torch.Size`, List[int], Tuple[int]], bool]: exact tensor shape, whether to chunk tensor """ if chunk_tensor: tensor_chunk_shape = reduce(operator.mul, tensor_shape, 1) tensor_parallel_world_size = gpc.get_world_size(ParallelMode.TENSOR) if tensor_chunk_shape % tensor_parallel_world_size == 0: tensor_chunk_shape = tensor_chunk_shape // tensor_parallel_world_size else: tensor_chunk_shape = tensor_shape chunk_tensor = False else: tensor_chunk_shape = tensor_shape return tensor_chunk_shape, chunk_tensor def create_recv_buffer_with_shapes(recv_shapes, dtype, scatter_gather_tensors): if isinstance(recv_shapes, torch.Size): recv_chunk_shape, recv_split = _get_tensor_shape(recv_shapes, scatter_gather_tensors) buffer_recv = torch.empty(recv_chunk_shape, requires_grad=True, device=get_current_device(), dtype=dtype) return buffer_recv, recv_split buffer_recv = [] for recv_shape in recv_shapes: recv_chunk_shape, recv_split = _get_tensor_shape(recv_shape, scatter_gather_tensors) tensor_recv = torch.empty(recv_chunk_shape, requires_grad=True, device=get_current_device(), dtype=dtype) buffer_recv.append(tensor_recv) return buffer_recv, recv_split def process_object_to_send(object_send, scatter_gather_tensors): if isinstance(object_send, torch.Tensor): send_split = _get_tensor_shape(object_send.shape, scatter_gather_tensors)[1] if send_split: object_send = split_tensor_into_1d_equal_chunks(object_send) return object_send object_send_list = [] for tensor_send in object_send: send_split = _get_tensor_shape(tensor_send.shape, scatter_gather_tensors)[1] if send_split: object_send_list.append(split_tensor_into_1d_equal_chunks(tensor_send)) else: object_send_list.append(tensor_send) object_send = tuple(object_send_list) return object_send def filling_ops_queue(obj, comm_op, comm_rank, ops_queue): if isinstance(obj, torch.Tensor): op_to_add = dist.P2POp(comm_op, obj, comm_rank) ops_queue.append(op_to_add) else: for tensor_to_comm in obj: op_to_add = dist.P2POp(comm_op, tensor_to_comm, comm_rank) ops_queue.append(op_to_add) def _communicate(object_send_next: Union[torch.Tensor, List[torch.Tensor]] = None, object_send_prev: Union[torch.Tensor, List[torch.Tensor]] = None, recv_prev: bool = False, recv_next: bool = False, recv_prev_shape: Union[torch.Size, List[torch.Size]] = None, recv_next_shape: Union[torch.Size, List[torch.Size]] = None, prev_rank: int = None, next_rank: int = None, dtype: torch.dtype = None, scatter_gather_tensors: bool = False) -> Tuple[Union[torch.Tensor, List[torch.Tensor]]]: """ Adapted from megatron.p2p_communication. Communicate tensors between stages. Used as helper method in other communication methods that are used in pipeline schedule. Takes the following arguments: object_send_next (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): tensor to send to next rank (no tensor sent if set to None). object_send_prev (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): tensor to send to prev rank (no tensor sent if set to None). recv_prev (bool): boolean for whether tensor should be received from previous rank. recv_next (bool): boolean for whether tensor should be received from next rank. recv_prev_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): shape of the tensor to be received from the previous stage, defualts to None. recv_next_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): shape of the tensor to be received from the next stage, defualts to None. prev_rank (int): the rank of the previous pipeline stage, defualts to None, next_rank (int): the rank of the next pipeline stage, defualts to None, dtype (torch.dtype): data type of intermediate buffers, defaults to None scatter_gather_tensors (bool): whether to scatter and gather tensor between pipeline stages, defaults to False Returns: Tuple[Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]]: returns tensor_recv_prev, tensor_recv_next """ # Create placeholder tensors for receive in forward and backward directions # if needed. tensor_recv_prev = None tensor_recv_next = None if recv_prev: assert recv_prev_shape is not None tensor_recv_prev, recv_prev_split = create_recv_buffer_with_shapes(recv_prev_shape, dtype, scatter_gather_tensors) if recv_next: assert recv_next_shape is not None tensor_recv_next, recv_next_split = create_recv_buffer_with_shapes(recv_next_shape, dtype, scatter_gather_tensors) if object_send_prev is not None or recv_prev: if prev_rank is None: prev_rank = gpc.get_prev_global_rank(ParallelMode.PIPELINE) if object_send_next is not None or recv_next: if next_rank is None: next_rank = gpc.get_next_global_rank(ParallelMode.PIPELINE) if object_send_prev is not None: object_send_prev = process_object_to_send(object_send_prev, scatter_gather_tensors) if object_send_next is not None: object_send_next = process_object_to_send(object_send_next, scatter_gather_tensors) ops = [] if object_send_prev is not None: filling_ops_queue(object_send_prev, dist.isend, prev_rank, ops) if tensor_recv_prev is not None: filling_ops_queue(tensor_recv_prev, dist.irecv, prev_rank, ops) if tensor_recv_next is not None: filling_ops_queue(tensor_recv_next, dist.irecv, next_rank, ops) if object_send_next is not None: filling_ops_queue(object_send_next, dist.isend, next_rank, ops) if len(ops) > 0: reqs = dist.batch_isend_irecv(ops) for req in reqs: req.wait() # To protect against race condition when using batch_isend_irecv(). torch.cuda.synchronize() if recv_prev and recv_prev_split: if isinstance(tensor_recv_prev, torch.Tensor): tensor_recv_prev = gather_split_1d_tensor(tensor_recv_prev).view(recv_prev_shape).requires_grad_() else: for index in range(len(tensor_recv_prev)): tensor_recv_prev[index] = gather_split_1d_tensor(tensor_recv_prev[index]).view( recv_prev_shape[index]).requires_grad_() if recv_next and recv_next_split: if isinstance(tensor_recv_next, torch.Tensor): tensor_recv_next = gather_split_1d_tensor(tensor_recv_next).view(recv_next_shape).requires_grad_() else: for index in range(len(tensor_recv_next)): tensor_recv_next[index] = gather_split_1d_tensor(tensor_recv_next[index]).view( recv_next_shape[index]).requires_grad_() return tensor_recv_prev, tensor_recv_next def recv_forward(input_tensor_shape, prev_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Copy the forward output from the previous stage in pipeline as the input tensor of this stage. Args: input_tensor_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. prev_rank (int, optional): The rank of the source of the tensor. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input tensor or input tensor list. """ if gpc.is_pipeline_first_stage(): input_tensor = None else: input_tensor, _ = _communicate(recv_prev=True, recv_prev_shape=input_tensor_shape, prev_rank=prev_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return input_tensor def recv_backward(output_grad_shape, next_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Copy the gradient tensor from the next stage in pipeline as the input gradient of this stage. Args: output_grad_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. next_rank (int, optional): The rank of the source of the tensor. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input gradient tensor or gradident tensor list. """ if gpc.is_pipeline_last_stage(): output_tensor_grad = None else: _, output_tensor_grad = _communicate(recv_next=True, recv_next_shape=output_grad_shape, next_rank=next_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return output_tensor_grad def send_forward(output_tensor, next_rank=None, scatter_gather_tensors=False) -> None: """Sends the input tensor to the next stage in pipeline. Args: output_tensor (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent. next_rank (int, optional): The rank of the recipient of the tensor. """ if not gpc.is_pipeline_last_stage(): _communicate(object_send_next=output_tensor, next_rank=next_rank, scatter_gather_tensors=scatter_gather_tensors) def send_backward(input_tensor_grad, prev_rank=None, scatter_gather_tensors=False) -> None: """Sends the gradient tensor to the previous stage in pipeline. Args: input_tensor_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent prev_rank (int, optional): The rank of the recipient of the tensor """ if not gpc.is_pipeline_first_stage(): _communicate(object_send_prev=input_tensor_grad, prev_rank=prev_rank, scatter_gather_tensors=scatter_gather_tensors) def send_forward_recv_backward(output_tensor, output_grad_shape, recv_next=True, next_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Batched communication operation. Sends the input tensor to the next stage in pipeline, while receives the gradient tensor from the next stage in pipeline as the input gradient tensor of this stage. Args: output_tensor (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent. output_grad_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input gradient tensor. """ if gpc.is_pipeline_last_stage(): output_tensor_grad = None else: _, output_tensor_grad = _communicate(object_send_next=output_tensor, recv_next=recv_next, recv_next_shape=output_grad_shape, next_rank=next_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return output_tensor_grad def send_backward_recv_forward(input_tensor_grad, input_tensor_shape, recv_prev=True, prev_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Batched communication operation. Sends the gradient tensor to the previous stage in pipeline, while receives the output tensor from the previous stage in pipeline as the input of this stage. Args: input_tensor_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent. input_tensor_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input tensor. """ if gpc.is_pipeline_first_stage(): input_tensor = None else: input_tensor, _ = _communicate(object_send_prev=input_tensor_grad, recv_prev=recv_prev, recv_prev_shape=input_tensor_shape, prev_rank=prev_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return input_tensor def send_forward_recv_forward(output_tensor, input_tensor_shape, recv_prev=True, prev_rank=None, next_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Batched communication operation. Sends the input tensor to the next stage in pipeline, while receives the output tensor from the previous stage in pipeline as the input of this stage. Args: output_tensor (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent. input_tensor_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input tensor. """ input_tensor, _ = _communicate(object_send_next=output_tensor, recv_prev=recv_prev, recv_prev_shape=input_tensor_shape, prev_rank=prev_rank, next_rank=next_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return input_tensor def send_backward_recv_backward(input_tensor_grad, output_grad_shape, recv_next=True, prev_rank=None, next_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Union[torch.Tensor, List[torch.Tensor]]: """Batched communication operation. Sends the gradient tensor to the previous stage in pipeline, while receives the gradient tensor from the next member in pipeline as the input of this stage. Args: input_tensor_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent. output_grad_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: The input gradient tensor. """ _, output_tensor_grad = _communicate(object_send_prev=input_tensor_grad, recv_next=recv_next, recv_next_shape=output_grad_shape, prev_rank=prev_rank, next_rank=next_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return output_tensor_grad def send_forward_backward_recv_forward_backward( output_tensor, input_tensor_grad, input_tensor_shape, output_grad_shape, recv_prev=True, recv_next=True, prev_rank=None, next_rank=None, dtype=torch.float, scatter_gather_tensors=False) -> Tuple[Union[torch.Tensor, List[torch.Tensor]]]: """Batched communication operation. Sends the input tensor to the next stage in pipeline and the gradient tensor to the previous stage, while receives the input gradient tensor from the next stage and the input tensor from the previous stage. Args: output_tensor (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor sent to the next. input_tensor_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor sent to the previous. input_tensor_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor received from the previous. output_grad_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor received from the next. Returns: Tuple(Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]], Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): (the input tensor, the input gradient tensor) """ input_tensor, output_tensor_grad = _communicate(object_send_next=output_tensor, object_send_prev=input_tensor_grad, recv_prev=recv_prev, recv_next=recv_next, recv_prev_shape=input_tensor_shape, recv_next_shape=output_grad_shape, prev_rank=prev_rank, next_rank=next_rank, dtype=dtype, scatter_gather_tensors=scatter_gather_tensors) return input_tensor, output_tensor_grad
from .collective import all_gather, reduce_scatter, all_reduce, broadcast, reduce from .p2p import (send_forward, send_forward_recv_forward, send_backward_recv_forward, send_backward, send_backward_recv_backward, send_forward_recv_backward, send_forward_backward_recv_forward_backward, recv_forward, recv_backward) from .ring import ring_forward from .utils import send_obj_meta, recv_obj_meta __all__ = [ 'all_gather', 'reduce_scatter', 'all_reduce', 'broadcast', 'reduce', 'send_forward', 'send_forward_recv_forward', 'send_forward_backward_recv_forward_backward', 'send_backward', 'send_backward_recv_backward', 'send_backward_recv_forward', 'send_forward_recv_backward', 'recv_backward', 'recv_forward', 'ring_forward', 'send_obj_meta', 'recv_obj_meta', ]
import torch import torch.distributed as dist from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.utils import get_current_device from typing import Union, List, Tuple TensorShape = Union[torch.Size, List[int], Tuple[int]] def send_meta_helper(obj, next_rank, tensor_kwargs): send_shape = torch.tensor(obj.size(), **tensor_kwargs) send_ndims = torch.tensor(len(obj.size()), **tensor_kwargs) dist.send(send_ndims, next_rank) dist.send(send_shape, next_rank) def send_obj_meta(obj, need_meta=True, next_rank=None) -> bool: """Sends obj meta information before sending a specific obj. Since the recipient must know the shape of the obj in p2p communications, meta information of the obj should be sent before communications. This function synchronizes with :func:`recv_obj_meta`. Args: obj (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): obj to be sent. need_meta (bool, optional): If False, meta information won't be sent. next_rank (int): The rank of the next member in pipeline parallel group. Returns: bool: False """ if need_meta: if next_rank is None: next_rank = gpc.get_next_global_rank(ParallelMode.PIPELINE) tensor_kwargs = {'dtype': torch.long, 'device': get_current_device()} if isinstance(obj, torch.Tensor): send_obj_nums = torch.tensor(1, **tensor_kwargs) dist.send(send_obj_nums, next_rank) send_meta_helper(obj, next_rank, tensor_kwargs) else: send_obj_nums = torch.tensor(len(obj), **tensor_kwargs) dist.send(send_obj_nums, next_rank) for tensor_to_send in obj: send_meta_helper(tensor_to_send, next_rank, tensor_kwargs) return False def recv_meta_helper(prev_rank, tensor_kwargs): recv_ndims = torch.empty((), **tensor_kwargs) dist.recv(recv_ndims, prev_rank) recv_shape = torch.empty(recv_ndims, **tensor_kwargs) dist.recv(recv_shape, prev_rank) return recv_shape def recv_obj_meta(obj_shape, prev_rank=None) -> torch.Size: """Receives obj meta information before receiving a specific obj. Since the recipient must know the shape of the obj in p2p communications, meta information of the obj should be received before communications. This function synchronizes with :func:`send_obj_meta`. Args: obj_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the obj to be received. prev_rank (int): The rank of the source of the obj. Returns: Union[:class:`torch.Size`, List[:class:`torch.Size`]]: The shape of the obj to be received. """ if obj_shape is None: if prev_rank is None: prev_rank = gpc.get_prev_global_rank(ParallelMode.PIPELINE) tensor_kwargs = {'dtype': torch.long, 'device': get_current_device()} recv_obj_nums = torch.empty((), **tensor_kwargs) dist.recv(recv_obj_nums, prev_rank) if recv_obj_nums.item() == 1: recv_shape = recv_meta_helper(prev_rank, tensor_kwargs) obj_shape = torch.Size(recv_shape) else: obj_shape = [] for i in range(recv_obj_nums.item()): recv_shape = recv_meta_helper(prev_rank, tensor_kwargs) obj_shape.append(torch.Size(recv_shape)) return obj_shape def split_tensor_into_1d_equal_chunks(tensor: torch.Tensor, new_buffer=False) -> torch.Tensor: """Break a tensor into equal 1D chunks. Args: tensor (:class:`torch.Tensor`): Tensor to be split before communication. new_buffer (bool, optional): Whether to use a new buffer to store sliced tensor. Returns: :class:`torch.Tensor`: The split tensor """ partition_size = torch.numel(tensor) // gpc.get_world_size(ParallelMode.PARALLEL_1D) start_index = partition_size * gpc.get_local_rank(ParallelMode.PARALLEL_1D) end_index = start_index + partition_size if new_buffer: data = torch.empty(partition_size, dtype=tensor.dtype, device=torch.cuda.current_device(), requires_grad=False) data.copy_(tensor.view(-1)[start_index:end_index]) else: data = tensor.view(-1)[start_index:end_index] return data def gather_split_1d_tensor(tensor: torch.Tensor) -> torch.Tensor: """Opposite of above function, gather values from model parallel ranks. Args: tensor (:class:`torch.Tensor`): Tensor to be gathered after communication. Returns: :class:`torch.Tensor`: The gathered tensor. """ world_size = gpc.get_world_size(ParallelMode.PARALLEL_1D) numel = torch.numel(tensor) numel_gathered = world_size * numel gathered = torch.empty(numel_gathered, dtype=tensor.dtype, device=torch.cuda.current_device(), requires_grad=False) chunks = [gathered[i * numel:(i + 1) * numel] for i in range(world_size)] dist.all_gather(chunks, tensor, group=gpc.get_group(ParallelMode.PARALLEL_1D)) return gathered
#!/usr/bin/env python # -*- encoding: utf-8 -*- import io import pickle from typing import Any, List, Tuple, Union import torch import torch.distributed as dist from torch.distributed import ProcessGroupNCCL from torch.distributed import distributed_c10d as c10d from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc TensorShape = Union[torch.Size, List[int], Tuple[int]] _pg_manager = {} _unpickler = pickle.Unpickler def init_process_group(): """intialise process group by dist.new_group in the adjacent stages Args: None Returns: None """ world_size = gpc.get_world_size(ParallelMode.PIPELINE) for i in range(world_size - 1): _pg_manager[(i, i + 1)] = dist.new_group([i, i + 1]) def _acquire_pair_group_handle(first_rank: int, second_rank: int) -> ProcessGroupNCCL: """get the group handle of two given ranks Args: first_rank (int): first rank in the pair second_rank (int): second rank in the pair Returns: :class:`ProcessGroupNCCL`: the handle of the group consisting of the given two ranks """ if len(_pg_manager) == 0: init_process_group() if first_rank > second_rank: first_rank, second_rank = second_rank, first_rank pair_key = (first_rank, second_rank) return _pg_manager[pair_key] def _cuda_safe_tensor_to_object(tensor: torch.Tensor, tensor_size: torch.Size) -> object: """transform tensor to object with unpickle. Info of the device in bytes stream will be modified into current device before unpickling Args: tensor (:class:`torch.tensor`): tensor to be unpickled tensor_size (:class:`torch.Size`): Size of the real info in bytes Returns: Any: object after unpickled """ buf = tensor.numpy().tobytes()[:tensor_size] if b'cuda' in buf: buf_array = bytearray(buf) device_index = torch.cuda.current_device() buf_array[buf_array.find(b'cuda') + 5] = 48 + device_index buf = bytes(buf_array) io_bytes = io.BytesIO(buf) byte_pickler = _unpickler(io_bytes) unpickle = byte_pickler.load() return unpickle def _broadcast_object_list(object_list: List[Any], src: int, dst: int, device=None): """This is a modified version of the broadcast_object_list in torch.distribution The only difference is that object will be move to correct device after unpickled. If local_rank = src, then object list will be sent to rank src. Otherwise, object list will be updated with data sent from rank src. Args: object_list (List[Any]): list of object to broadcast src (int): source rank to broadcast dst (int): dst rank to broadcast device (:class:`torch.device`): device to do broadcast. current device in default """ group = _acquire_pair_group_handle(src, dst) if c10d._rank_not_in_group(group): c10d._warn_not_in_group("broadcast_object_list") return local_rank = gpc.get_local_rank(ParallelMode.PIPELINE) # Serialize object_list elements to tensors on src rank. if local_rank == src: tensor_list, size_list = zip(*[c10d._object_to_tensor(obj) for obj in object_list]) object_sizes_tensor = torch.cat(size_list) else: object_sizes_tensor = torch.empty(len(object_list), dtype=torch.long) is_nccl_backend = c10d._check_for_nccl_backend(group) current_device = None if device is not None: if is_nccl_backend and device.type != "cuda": raise ValueError("device type must be cuda for nccl backend") current_device = device else: current_device = torch.device("cpu") if is_nccl_backend: current_device = torch.device("cuda", torch.cuda.current_device()) if is_nccl_backend: object_sizes_tensor = object_sizes_tensor.to(current_device) # Broadcast object sizes c10d.broadcast(object_sizes_tensor, src=src, group=group, async_op=False) # Concatenate and broadcast serialized object tensors if local_rank == src: object_tensor = torch.cat(tensor_list) else: object_tensor = torch.empty( # type: ignore[call-overload] torch.sum(object_sizes_tensor).item(), # type: ignore[arg-type] dtype=torch.uint8, ) if is_nccl_backend: object_tensor = object_tensor.to(current_device) c10d.broadcast(object_tensor, src=src, group=group, async_op=False) # Deserialize objects using their stored sizes. offset = 0 if local_rank != src: for i, obj_size in enumerate(object_sizes_tensor): obj_view = object_tensor[offset:offset + obj_size] obj_view = obj_view.type(torch.uint8) if obj_view.device != torch.device("cpu"): obj_view = obj_view.cpu() offset += obj_size # unpickle unpickle_object = _cuda_safe_tensor_to_object(obj_view, obj_size) # unconsistence in device if isinstance(unpickle_object, torch.Tensor) and unpickle_object.device.index != torch.cuda.current_device(): unpickle_object = unpickle_object.cuda() object_list[i] = unpickle_object def _send_object(object: Any, dst: int) -> None: """send anything to dst rank Args: object (Any): object needed to be sent dst (int): rank of the destination Returns: None """ local_rank = gpc.get_local_rank(ParallelMode.PIPELINE) # handler = _acquire_pair_group_handle(local_rank, dst) # transform to list if not if isinstance(object, torch.Tensor): object = [object] # broadcast length first # TODO : more elegant ? P.S. reduce a _broadcast_object_list _broadcast_object_list([len(object)], local_rank, dst) # then broadcast safely _broadcast_object_list(object, local_rank, dst) def _recv_object(src: int) -> Any: """recv anything from src Args: src (int): source rank of data. local rank will receive data from src rank. Returns: Any: Object received from src. """ local_rank = gpc.get_local_rank(ParallelMode.PIPELINE) # handler = _acquire_pair_group_handle(local_rank, src) # recv length first length = [0] _broadcast_object_list(length, src, local_rank) # then create recv buff from length[0] and broadcast object = [None] * length[0] _broadcast_object_list(object, src, local_rank) if length[0] == 1: object = object[0] return object def recv_forward(prev_rank: int = None) -> Any: """Copy the forward output from the previous stage in pipeline as the input tensor of this stage. Args: input_tensor_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. prev_rank (int, optional): The rank of the source of the tensor. Returns: Any: The input tensor or input tensor list. """ if gpc.is_pipeline_first_stage(): input_tensor = None else: if prev_rank is None: prev_rank = gpc.get_prev_global_rank(ParallelMode.PIPELINE) input_tensor = _recv_object(prev_rank) return input_tensor def recv_backward(next_rank: int = None) -> Any: """Copy the gradient tensor from the next stage in pipeline as the input gradient of this stage. Args: output_grad_shape (Union[:class:`torch.Size`, List[:class:`torch.Size`]]): The shape of the tensor to be received. next_rank (int, optional): The rank of the source of the tensor. Returns: Any: The input gradient tensor or gradident tensor list. """ if gpc.is_pipeline_last_stage(): output_tensor_grad = None else: if next_rank is None: next_rank = gpc.get_next_global_rank(ParallelMode.PIPELINE) output_tensor_grad = _recv_object(next_rank) return output_tensor_grad def send_forward(output_object: Any, next_rank: int = None) -> None: """Sends the input tensor to the next stage in pipeline. Args: output_object Any: Object to be sent. next_rank (int, optional): The rank of the recipient of the tensor. """ if not gpc.is_pipeline_last_stage(): if next_rank is None: next_rank = gpc.get_next_global_rank(ParallelMode.PIPELINE) _send_object(output_object, next_rank) def send_backward(input_object: Any, prev_rank: int = None) -> None: """Sends the gradient tensor to the previous stage in pipeline. Args: input_tensor_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Tensor to be sent prev_rank (int, optional): The rank of the recipient of the tensor """ if not gpc.is_pipeline_first_stage(): if prev_rank is None: prev_rank = gpc.get_prev_global_rank(ParallelMode.PIPELINE) _send_object(input_object, prev_rank)
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.utils import get_current_device, synchronize def ring_forward(tensor_send_next: torch.Tensor, parallel_mode: ParallelMode) -> torch.Tensor: """Sends a tensor to the next member and receives a tensor from the previous member. This function returns the received tensor from the previous member. Args: tensor_send_next (:class:`torch.Tensor`): Tensor sent to next member parallel_mode (ParallelMode): Parallel group mode used in this communication Returns: :class:`torch.Tensor`: The tensor received from the previous. Note: The parallel_mode should be concluded in ``ParallelMode``. More details about ``ParallelMode`` could be found in `parallel_mode <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/context/parallel_mode.py>`_. """ buffer_shape = tensor_send_next.size() ops = [] current_rank = gpc.get_global_rank() tensor_recv_prev = torch.empty(buffer_shape, requires_grad=True, device=get_current_device(), dtype=tensor_send_next.dtype) # send to next rank send_next_op = torch.distributed.P2POp(torch.distributed.isend, tensor_send_next, gpc.get_next_global_rank(parallel_mode)) ops.append(send_next_op) # receive from prev rank recv_prev_op = torch.distributed.P2POp(torch.distributed.irecv, tensor_recv_prev, gpc.get_prev_global_rank(parallel_mode)) ops.append(recv_prev_op) if current_rank % 2 == 0: ops = ops[::-1] reqs = torch.distributed.batch_isend_irecv(ops) for req in reqs: req.wait() # To protect against race condition when using batch_isend_irecv(). synchronize() return tensor_recv_prev
from .builder import build_from_config, build_from_registry, build_gradient_handler __all__ = ['build_gradient_handler', 'build_from_config', 'build_from_registry']
#!/usr/bin/env python # -*- encoding: utf-8 -*- import inspect from colossalai.registry import * def build_from_config(module, config: dict): """Returns an object of :class:`module` constructed from `config`. Args: module: A python or user-defined class config: A python dict containing information used in the construction of the return object Returns: An ``object`` of interest Raises: AssertionError: Raises an AssertionError if `module` is not a class """ assert inspect.isclass(module), 'module must be a class' return module(**config) def build_from_registry(config, registry: Registry): r"""Returns an object constructed from `config`, the type of the object is specified by `registry`. Note: the `config` is used to construct the return object such as `LAYERS`, `OPTIMIZERS` and other support types in `registry`. The `config` should contain all required parameters of corresponding object. The details of support types in `registry` and the `mod_type` in `config` could be found in `registry <https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/registry/__init__.py>`_. Args: config (dict or :class:`colossalai.context.colossalai.context.Config`): information used in the construction of the return object. registry (:class:`Registry`): A registry specifying the type of the return object Returns: A Python object specified by `registry`. Raises: Exception: Raises an Exception if an error occurred when building from registry. """ config_ = config.copy() # keep the original config untouched assert isinstance(registry, Registry), f'Expected type Registry but got {type(registry)}' mod_type = config_.pop('type') assert registry.has(mod_type), f'{mod_type} is not found in registry {registry.name}' try: obj = registry.get_module(mod_type)(**config_) except Exception as e: print(f'An error occurred when building {mod_type} from registry {registry.name}', flush=True) raise e return obj def build_gradient_handler(config, model, optimizer): """Returns a gradient handler object of :class:`BaseGradientHandler` constructed from `config`, `model` and `optimizer`. Args: config (dict or :class:`colossalai.context.Config`): A python dict or a :class:`colossalai.context.Config` object containing information used in the construction of the ``GRADIENT_HANDLER``. model (:class:`nn.Module`): A model containing parameters for the gradient handler optimizer (:class:`torch.optim.Optimizer`): An optimizer object containing parameters for the gradient handler Returns: An object of :class:`colossalai.engine.BaseGradientHandler` """ config_ = config.copy() config_['model'] = model config_['optimizer'] = optimizer return build_from_registry(config_, GRADIENT_HANDLER)
from ._base_engine import Engine from .gradient_handler import * __all__ = ['Engine']
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import List, Iterable from torch.nn import Module from torch.nn.modules.loss import _Loss from colossalai.logging import get_dist_logger from torch import Tensor from colossalai.gemini.ophooks import register_ophooks_recursively, BaseOpHook from colossalai.engine.schedule import BaseSchedule, NonPipelineSchedule, PipelineSchedule, InterleavedPipelineSchedule from typing import Optional, Type from colossalai.engine.gradient_handler import BaseGradientHandler from colossalai.logging import get_dist_logger class Engine: """Basic engine class for training and evaluation. It runs a specific process method :meth:`step` which is based on the given :attr:`schedule` over each batch of a dataset. It controls a iteration in training. Args: model (``torch.nn.Module``): The neural network model. optimizer (``colossalai.nn.optimizer.ColossalaiOptimizer``): Optimizer for updating the parameters. criterion (``torch.nn.modules.loss._Loss``, optional): Loss function for calculating loss. gradient_handlers (List[``BaseGradientHandler``], optional): A list of gradient handler used in backward. clip_grad_norm (float, optional): The norm of gradient clipping. ophook_list (list): List of ophook. verbose (bool): whether to display log info. schedule (''BaseSchedule''): Runtime schedule. Examples: >>> # define model, criterion, optimizer, lr_scheduler, train_dataloader for your training >>> model = ... >>> criterion = ... >>> optimizer = ... >>> train_dataloader = ... >>> engine, _, _, _ = colossalai.initialize(model, optimizer, criterion) >>> engine.train() >>> for inputs, labels in train_dataloader >>> # set gradients to zero >>> engine.zero_grad() >>> # run forward pass >>> outputs = engine(inputs) >>> # compute loss value and run backward pass >>> loss = engine.criterion(outputs, labels) >>> engine.backward(loss) >>> # update parameters >>> engine.step() The example of using Engine in training could be find in `Training with engine and trainer <https://www.colossalai.org/docs/basics/engine_trainer>`_. and `Run resnet cifar10 with engine <https://github.com/hpcaitech/ColossalAI-Examples/blob/main/image/resnet/run_resnet_cifar10_with_engine.py>`_. """ def __init__(self, model: Module, optimizer: "ColossalaiOptimizer", criterion: Optional[_Loss] = None, gradient_handlers: Optional[List[BaseGradientHandler]] = None, clip_grad_norm: float = 0.0, ophook_list: Optional[List[BaseOpHook]] = None, verbose: bool = True, schedule: Optional[BaseSchedule] = None): self._model = model self._optimizer = optimizer self._criterion = criterion self._clip_grad_norm = clip_grad_norm self._verbose = verbose self._logger = get_dist_logger() # state self.training = True # default # build gradient handler if gradient_handlers: self._gradient_handlers = gradient_handlers else: self._gradient_handlers = [] if ophook_list is None: self._ophook_list = [] else: self._ophook_list = ophook_list # build schedule if schedule: assert isinstance(schedule, BaseSchedule), \ f'expected schedule to be of type BaseSchedule, but got {type(schedule)}' self._schedule = schedule else: self._schedule = NonPipelineSchedule() if self.uses_pipeline: self._schedule.pre_processing(self) #register hook if any if len(self._ophook_list) > 0: register_ophooks_recursively(self._model, self._ophook_list) @property def ophooks(self): """show current activated ophooks""" return self._ophook_list @property def model(self): """Model attached to the engine""" return self._model @property def optimizer(self): """Optimizer attached to the engine""" return self._optimizer @property def criterion(self): """Criterion attached to the engine""" return self._criterion @property def schedule(self): """Schedule attached to the engine""" return self._schedule @property def uses_pipeline(self): """show the pipeline parallel used or not""" return isinstance(self._schedule, (PipelineSchedule, InterleavedPipelineSchedule)) def add_hook(self, ophook: Type[BaseOpHook]) -> None: """add necessary hook""" # whether this hook exist for h in self._ophook_list: if type(h) == type(ophook): logger = get_dist_logger() logger.warning(f"duplicate hooks, at least two instance of {type(ophook)}") self._ophook_list.append(ophook) register_ophooks_recursively(self._model, self._ophook_list) def remove_hook(self, ophook: Type[BaseOpHook]) -> None: """remove hook""" logger = get_dist_logger() logger.warning(f"removing hooks is currently not supported") def zero_grad(self): """Set the gradient of parameters to zero """ self.optimizer.zero_grad() def step(self): """Execute parameter update """ self._all_reduce_gradients() self.optimizer.clip_grad_norm(self.model, self._clip_grad_norm) return self.optimizer.step() def backward(self, loss: Tensor): """Start backward propagation given the loss value computed by a loss function. Args: loss (:class:`torch.Tensor`): Loss value computed by a loss function. """ ret = self.optimizer.backward(loss) for ophook in self._ophook_list: ophook.post_iter() return ret def backward_by_grad(self, tensor, grad): """Start backward propagation given the gradient of the output tensor. Args: tensor (:class:`torch.Tensor`): Output tensor. grad (:class:`torch.Tensor`): Gradient passed back to the output. """ ret = self.optimizer.backward_by_grad(tensor, grad) for ophook in self._ophook_list: ophook.post_iter() return ret def __call__(self, *args, **kwargs): """Run the forward step for the model. Returns: Tuple[:class:`torch.Tensor`] or :class:`torch.Tensor`: Output of the model. """ return self.model(*args, **kwargs) def _all_reduce_gradients(self): """Handles all-reduce operations of gradients across different parallel groups. """ for handler in self._gradient_handlers: handler.handle_gradient() def execute_schedule(self, data_iter: Iterable, **kwargs): """Run the forward, loss computation, and backward for the model. Returns a tuple of (output, label, loss). Returns: Tuple[:class:`torch.Tensor`]: A tuple of (output, label, loss). """ output, label, loss = self._schedule.forward_backward_step(self, data_iter, **kwargs) return output, label, loss def train(self): """Sets the model to training mode. """ self.training = True self._model.train() def eval(self): """Sets the model to evaluation mode. """ self.training = False self._model.eval()
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod import torch from typing import Iterable, Callable from colossalai.logging import get_dist_logger from colossalai.utils import get_current_device class BaseSchedule(ABC): """A basic helper class to control the process of training or evaluation. It mainly composes of forward_backward_step for gradient backward and optimizer_step for parameters update. For the convenience to enable FP16, we aggregate all codes that contain the control of FP16 in class schedule. Args: data_process_func (Callable, optional): The preprocessing function which receives a batch of data and arranges them into data and label. """ def __init__(self, data_process_func: Callable = None): self.logger = get_dist_logger() self.data_process_func = data_process_func @staticmethod def _move_tensor(element): if torch.is_tensor(element): if not element.is_cuda: return element.to(get_current_device()).detach() return element def _move_to_device(self, data): if isinstance(data, torch.Tensor): data = data.to(get_current_device()) elif isinstance(data, (list, tuple)): data_to_return = [] for element in data: if isinstance(element, dict): data_to_return.append({k: self._move_tensor(v) for k, v in element.items()}) else: data_to_return.append(self._move_tensor(element)) data = data_to_return elif isinstance(data, dict): data = {k: self._move_tensor(v) for k, v in data.items()} else: raise TypeError( f"Expected batch data to be of type torch.Tensor, list, tuple, or dict, but got {type(data)}") return data def _get_batch_size(self, data): if isinstance(data, torch.Tensor): return data.size(0) elif isinstance(data, (list, tuple)): if isinstance(data[0], dict): return data[0][list(data[0].keys())[0]].size(0) return data[0].size(0) elif isinstance(data, dict): return data[list(data.keys())[0]].size(0) def load_batch(self, data_iter, to_gpu=True): """Loads a batch from data iterator. It returns the data and labels which are already in the same GPU as where the model's. Args: data_iter (Iterable): Data iterator from which get a batch of data, obtained by calling iter(dataloader). to_gpu (bool, optional): Whether the data should be moved to GPU Returns: Tuple (:class:`Tensor`, :class:`torch.Tensor`): A tuple of (data, label). """ if data_iter is None: raise RuntimeError('Dataloader is not defined.') batch_data = next(data_iter) if to_gpu: batch_data = self._move_to_device(batch_data) self.batch_size = self._get_batch_size(batch_data) return batch_data def pre_processing(self, engine): """To perform actions before running the schedule. """ pass @abstractmethod def forward_backward_step(self, engine, data_iter: Iterable, forward_only: bool, return_loss: bool = True, return_output_label: bool = True): """The process function over a batch of dataset for training or evaluation. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. data_iter (Iterable): Data iterator from which get a batch of data, obtained by calling iter(dataloader). forward_only (bool): If True, the process won't include backward. return_loss (bool, optional): If False, the loss won't be returned. return_output_label (bool, optional): If False, the output and label won't be returned. """ pass @staticmethod def _call_engine(engine, inputs): if isinstance(inputs, torch.Tensor): return engine(inputs) elif isinstance(inputs, (list, tuple)): return engine(*inputs) elif isinstance(inputs, dict): return engine(**inputs) else: TypeError( f"Expected engine inputs to be of type torch.Tensor, list, tuple, or dict, but got {type(inputs)}") @staticmethod def _call_engine_criterion(engine, outputs, labels): assert isinstance(outputs, (torch.Tensor, list, tuple, dict)), f'Expect output of model is (torch.Tensor, list, tuple), got {type(outputs)}' if isinstance(outputs, torch.Tensor): outputs = (outputs,) if isinstance(labels, torch.Tensor): labels = (labels,) if isinstance(outputs, (tuple, list)) and isinstance(labels, (tuple, list)): return engine.criterion(*outputs, *labels) elif isinstance(outputs, (tuple, list)) and isinstance(labels, dict): return engine.criterion(*outputs, **labels) elif isinstance(outputs, dict) and isinstance(labels, dict): return engine.criterion(**outputs, **labels) elif isinstance(outputs, dict) and isinstance(labels, (list, tuple)): raise ValueError(f"Expected labels to be a dict when the model outputs are dict, but got {type(labels)}") else: raise TypeError(f"Expected model outputs and labels to be of type torch.Tensor ' \ '(which is auto-converted to tuple), list, tuple, or dict, ' \ 'but got {type(outputs)} (model outputs) and {type(labels)} (labels)")
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import Tuple, Iterable from colossalai import engine import colossalai.communication.p2p_v2 as comm import torch.cuda from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.utils.cuda import get_current_device from ._pipeline_schedule import PipelineSchedule def pack_return_tensors(return_tensors): output, label = tuple(zip(*return_tensors)) if isinstance(output[0], torch.Tensor): output = torch.cat(output, dim=0) elif isinstance(output[0], (list, tuple)): output = tuple(torch.cat(tensors, dim=0) for tensors in zip(*output)) else: raise TypeError(f'Output of model must be tensor or list/tuple of tensors') if isinstance(label[0], torch.Tensor): label = torch.cat(label, dim=0) else: merged_label = {k: [] for k in label[0].keys()} for d in label: for k, v in d.items(): merged_label[k].append(v) label = {k: torch.cat(v, dim=0) for k, v in merged_label.items()} return output, label class PipelineScheduleV2(PipelineSchedule): """Derived class of PipelineSchedule, the only difference is that forward_backward_step is reconstructed with p2p_v2 Args: num_microbatches (int): The number of microbatches. data_process_func (Callable, optional): The preprocessing function which receives a batch of data, and it will be executed in `load_batch`. tensor_shape (torch.Size, optional): Specified shape in pipeline communication. scatter_gather_tensors (bool, optional): If set to `True`, communication will be reduced over pipeline when using 1D tensor parallelization. Example: # this shows an example of customized data_process_func def data_process_func(stage_output, dataloader_output): output1, output2 = stage_output item1, item2, item3 = dataloader_output # assume item2 is not needed data = (output1, output2, item1) label = item3 return data, label """ def forward_backward_step(self, engine: engine.Engine, data_iter: Iterable, forward_only=False, return_loss=True, return_output_label=True) -> Tuple[torch.Tensor]: """Runs non-interleaved 1F1B schedule, with communication between pipeline stages. Returns a tuple with losses if the last stage, an empty tuple otherwise. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. data_iter (Iterable): Dataloader as the form of an iterator, obtained by calling iter(dataloader). forward_only (bool, optional): Whether run forward step only. Default is false. If true, no backward will be run. return_loss (bool, optional): Whether returns the loss value. Default is true. return_output_label (bool, optional): If False, the output and label won't be returned. Returns: Tuple[:class:`torch.Tensor`]: A tuple of (output, label, loss), loss and label could be None. """ assert forward_only or return_loss, \ 'The argument \'return_loss\' has to be True when \'forward_only\' is False, but got False.' self.load_batch(data_iter) # num_warmup_microbatches is the step when not all the processers are working num_warmup_microbatches = \ (gpc.get_world_size(ParallelMode.PIPELINE) - gpc.get_local_rank(ParallelMode.PIPELINE) - 1) num_warmup_microbatches = min(num_warmup_microbatches, self.num_microbatches) num_microbatches_remaining = self.num_microbatches - num_warmup_microbatches # Input, output tensors only need to be saved when doing backward passes input_objs = None output_objs = None # local_rank = gpc.get_local_rank(ParallelMode.PIPELINE) if not forward_only: input_objs = [] output_objs = [] return_tensors = [] if return_loss and gpc.is_pipeline_last_stage(ignore_virtual=True): accum_loss = torch.zeros(1, device=get_current_device()) else: accum_loss = None # Run warmup forward passes. for i in range(num_warmup_microbatches): input_obj = comm.recv_forward() output_obj = self._forward_step(engine, input_obj, return_tensors, return_output_label=return_output_label, accum_loss=accum_loss) comm.send_forward(output_obj) if not forward_only: input_objs.append(input_obj) output_objs.append(output_obj) # Before running 1F1B, need to receive first forward tensor. # If all microbatches are run in warmup / cooldown phase, then no need to # receive this tensor here. if num_microbatches_remaining > 0: input_obj = comm.recv_forward() # Run 1F1B in steady state. for i in range(num_microbatches_remaining): last_iteration = (i == (num_microbatches_remaining - 1)) output_obj = self._forward_step(engine, input_obj, return_tensors, return_output_label=return_output_label, accum_loss=accum_loss) if forward_only: comm.send_forward(output_obj) if not last_iteration: input_obj = comm.recv_forward() else: # TODO adjust here comm.send_forward(output_obj) output_obj_grad = comm.recv_backward() # Add input_obj and output_obj to end of list. input_objs.append(input_obj) output_objs.append(output_obj) # Pop output_obj and output_obj from the start of the list for # the backward pass. input_obj = input_objs.pop(0) output_obj = output_objs.pop(0) input_obj_grad = self._backward_step(engine, input_obj, output_obj, output_obj_grad) if last_iteration: input_obj = None comm.send_backward(input_obj_grad) else: input_obj = comm.recv_forward() comm.send_backward(input_obj_grad) # Run cooldown backward passes. if not forward_only: for i in range(num_warmup_microbatches): input_obj = input_objs.pop(0) output_obj = output_objs.pop(0) output_obj_grad = comm.recv_backward() input_obj_grad = self._backward_step(engine, input_obj, output_obj, output_obj_grad) comm.send_backward(input_obj_grad) if len(return_tensors) > 0: output, label = pack_return_tensors(return_tensors) return output, label, accum_loss else: return None, None, accum_loss
from ._base_schedule import BaseSchedule from ._pipeline_schedule import PipelineSchedule, InterleavedPipelineSchedule, get_tensor_shape from ._non_pipeline_schedule import NonPipelineSchedule __all__ = ['BaseSchedule', 'NonPipelineSchedule', 'PipelineSchedule', 'InterleavedPipelineSchedule', 'get_tensor_shape']
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import Iterable import torch import inspect from ._base_schedule import BaseSchedule from colossalai.utils import conditional_context from typing import Callable class NonPipelineSchedule(BaseSchedule): """A helper schedule class for no pipeline parallelism running environment. During one process, it loads a batch of dataset and feeds it to the model. After getting the output and calculating the loss, it will use :meth:`step` to update the parameters if it is in training mode. Args: data_process_func (Callable, optional): The preprocessing function which receives a batch of data and returns a tuple in the form of (data, label). and it will be executed in load_batch. Example: # this shows an example of customized data_process_func def data_process_func(dataloader_output): item1, item2, item3 = dataloader_output data = (item1, item2) label = item3 return data, label """ def __init__(self, data_process_func: Callable = None): # check that non-pipeline schedule data process func only takes in one parameter # which is the batch data if data_process_func: sig = inspect.signature(data_process_func) assert len(sig.parameters) == 1, \ 'The data_process_func only takes in one parameter for NonPipelineSchedule, ' \ 'which is a tuple of tensors for the current batch, ' \ 'i.e. data_process_func(dataloader_output).' super().__init__(data_process_func) def forward_backward_step(self, engine, data_iter: Iterable, forward_only: bool = False, return_loss: bool = True, return_output_label: bool = True): """The process function that loads a batch of dataset and feeds it to the model. The returned labels and loss will None if :attr:`return_loss` is False. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. data_iter (Iterable): Dataloader as the form of an iterator, obtained by calling iter(dataloader). forward_only (bool, optional): If True, the model is run for the forward pass, else back propagation will be executed. return_loss (bool, optional): Loss will be returned if True. return_output_label (bool, optional): Output and label will be returned if True. Returns: Tuple[:class:`torch.Tensor`]: A tuple of (output, label, loss), loss and label could be None. """ assert forward_only or return_loss, \ "The argument 'return_loss' has to be True when 'forward_only' is False, but got False." batch_data = self.load_batch(data_iter) if self.data_process_func: data, label = self.data_process_func(batch_data) else: # if not batch data process func is given, # then we regard the batch data as a simple tuple of (data, label) data, label = batch_data # forward with conditional_context(torch.no_grad(), enable=forward_only): output = self._call_engine(engine, data) if return_loss: loss = self._call_engine_criterion(engine, output, label) if not forward_only: engine.backward(loss) if return_output_label: if return_loss: return output, label, loss else: return output, label, None else: if return_loss: return None, None, loss else: return None, None, None
#!/usr/bin/env python # -*- encoding: utf-8 -*- import inspect from typing import Callable, List, Tuple, Union import colossalai.communication as comm import torch.cuda from colossalai.amp.naive_amp import NaiveAMPModel from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.logging import get_dist_logger from colossalai.utils import switch_virtual_pipeline_parallel_rank from colossalai.utils.cuda import get_current_device from ._base_schedule import BaseSchedule def get_tensor_shape(): if hasattr(gpc.config, 'TENSOR_SHAPE'): return gpc.config.TENSOR_SHAPE if not gpc.is_initialized(ParallelMode.PIPELINE): return None if hasattr(gpc.config, 'SEQ_LENGTH') and hasattr(gpc.config, 'GLOBAL_BATCH_SIZE') and hasattr( gpc.config, 'GLOBAL_BATCH_SIZE') and hasattr(gpc.config, 'HIDDEN_SIZE'): if gpc.is_initialized(ParallelMode.DATA): dp_size = gpc.get_world_size(ParallelMode.DATA) else: dp_size = 1 if gpc.is_initialized(ParallelMode.SEQUENCE): seq_size = gpc.get_world_size(ParallelMode.SEQUENCE) else: seq_size = 1 tensor_shape = (gpc.config.SEQ_LENGTH // seq_size, gpc.config.GLOBAL_BATCH_SIZE // dp_size // gpc.config.NUM_MICRO_BATCHES, gpc.config.HIDDEN_SIZE) return tensor_shape else: return None def pack_return_tensors(return_tensors): output, label = tuple(zip(*return_tensors)) if isinstance(output[0], torch.Tensor): output = torch.cat(output, dim=0) elif isinstance(output[0], (list, tuple)): output = tuple(torch.cat(tensors, dim=0) for tensors in zip(*output)) else: raise TypeError(f'Output of model must be tensor or list/tuple of tensors') if isinstance(label[0], torch.Tensor): label = torch.cat(label, dim=0) else: merged_label = {k: [] for k in label[0].keys()} for d in label: for k, v in d.items(): merged_label[k].append(v) label = {k: torch.cat(v, dim=0) for k, v in merged_label.items()} return output, label class PipelineSchedule(BaseSchedule): """A helper schedule class for pipeline parallelism running environment. It uses non-interleaved 1F1B strategy. Other properties are similar as :class:`NonPipelineSchedule`. Args: num_microbatches (int): The number of microbatches. data_process_func (Callable, optional): The preprocessing function which receives a batch of data, and it will be executed in `load_batch`. tensor_shape (torch.Size, optional): Specified shape in pipeline communication. scatter_gather_tensors (bool, optional): If set to `True`, communication will be reduced over pipeline when using 1D tensor parallelization. Example: # this shows an example of customized data_process_func def data_process_func(stage_output, dataloader_output): output1, output2 = stage_output item1, item2, item3 = dataloader_output # assume item2 is not needed data = (output1, output2, item1) label = item3 return data, label """ def __init__(self, num_microbatches, data_process_func: Callable = None, tensor_shape: Union[torch.Size, List[int], Tuple[int]] = None, scatter_gather_tensors: bool = False): # we need to make sure that the signature of the data_process_func is valid if data_process_func: sig = inspect.signature(data_process_func) assert len(sig.parameters) == 2, \ 'The data_process_func only takes in two parameters for NonPipelineSchedule, ' \ 'which is the tensors passed by the previous pipeline stage and the dataloader output from this stage, ' \ 'i.e. data_process_func(stage_output, dataloader_output).' super().__init__(data_process_func=data_process_func) assert num_microbatches > 0, f'expected num_microbatches to be larger then 1, but got {num_microbatches}' self.num_microbatches = num_microbatches self.dtype = torch.float assert not isinstance(tensor_shape, int), "tensor_shape type should be one of Union[torch.Size, List[int], Tuple[int]]." if tensor_shape is None: self.tensor_shape = tensor_shape elif isinstance(tensor_shape, torch.Size): self.tensor_shape = tensor_shape else: self.tensor_shape = torch.Size(tensor_shape) self.scatter_gather_tensors = False if gpc.is_initialized(ParallelMode.PARALLEL_1D) and gpc.get_world_size(ParallelMode.PARALLEL_1D) > 1: self.scatter_gather_tensors = scatter_gather_tensors self._logger = get_dist_logger() # cache for the batch data self.batch_data = None def load_batch(self, data_iter): # Pipeline schedule just puts data in memory batch_data = super().load_batch(data_iter, to_gpu=False) self.microbatch_offset = 0 assert self.batch_size % self.num_microbatches == 0, \ "Batch size should divided by the number of microbatches" self.microbatch_size = self.batch_size // self.num_microbatches self.batch_data = batch_data def _get_data_slice(self, data, offset): if isinstance(data, torch.Tensor): return data[offset:offset + self.microbatch_size] elif isinstance(data, (list, tuple)): data_dict = {} for element in data: if isinstance(element, dict): data_dict.update({k: v[offset:offset + self.microbatch_size] for k, v in element.items()}) elif data_dict: data_dict['label'] = element[offset:offset + self.microbatch_size] if data_dict: return data_dict return [val[offset:offset + self.microbatch_size] for val in data] elif isinstance(data, dict): return {k: v[offset:offset + self.microbatch_size] for k, v in data.items()} else: raise TypeError(f"Expected data to be of type torch.Tensor, list, tuple, or dict, but got {type(data)}") def load_micro_batch(self): mciro_batch_data = self._get_data_slice(self.batch_data, self.microbatch_offset) self.microbatch_offset += self.microbatch_size return self._move_to_device(mciro_batch_data) def pre_processing(self, engine): from colossalai.zero.sharded_model.sharded_model_v2 import ShardedModelV2 # TODO: remove this after testing new zero with pipeline parallelism model = engine.model if isinstance(model, NaiveAMPModel): self.dtype = torch.half model = model.model if isinstance(model, ShardedModelV2): self.dtype = torch.half model = model.module # sig = inspect.signature(model.forward) # for p in sig.parameters.values(): # assert p.kind != inspect.Parameter.VAR_POSITIONAL, '*args is not supported' @staticmethod def _call_engine(model, data): if data is not None: if isinstance(data, torch.Tensor): return model(data) elif isinstance(data, (list, tuple)): return model(*data) elif isinstance(data, dict): stage_output = None if 'stage_output' in data: stage_output = data.pop('stage_output') if stage_output is None: return model(**data) elif isinstance(stage_output, torch.Tensor): return model(stage_output, **data) elif isinstance(stage_output, (tuple, list)): return model(*stage_output, **data) else: raise TypeError( f"Expected stage_output to be of type torch.Tensor, list, or tuple, but got {type(stage_output)}" ) else: raise TypeError(f"Expected data to be of type torch.Tensor, list, tuple, or dict, but got {type(data)}") def _get_actual_forward_func(self, module): if isinstance(module, NaiveAMPModel): sig = inspect.signature(module.model.forward) elif hasattr(module, 'colo_attr'): sig = inspect.signature(module.module.forward) else: sig = inspect.signature(module.forward) return sig def _get_data_label_for_current_step(self, stage_output, micro_batch_data, criterion, model): if self.data_process_func: # use customized function to get data and label data, label = self.data_process_func(stage_output, micro_batch_data) else: if isinstance(micro_batch_data, (tuple, list)): if gpc.is_first_rank(ParallelMode.PIPELINE): # for the first stage, we use the data from the # dataloader output by default data, label = micro_batch_data else: # for non-first stage, we use the output passed # by the previous as the model input data = stage_output _, label = micro_batch_data elif isinstance(micro_batch_data, dict): data = {} data['stage_output'] = stage_output if 'label' in micro_batch_data: label = micro_batch_data.pop('label') else: label = None load_data = micro_batch_data data.update(load_data) return data, label def _forward_step(self, engine, input_obj, return_tensors, return_output_label=True, accum_loss=None): """Forward step for passed-in model. If it is the first stage, the input tensor is obtained from data_iterator, otherwise the passed-in input_obj is used. Returns output tensor. This is a helper function and can be ignored by users. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. input_obj (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Input tensor for this pipeline stage. return_tensors (List[:class:`torch.Tensor`]): A list of tensors to return. return_output_label (bool, optional): Whether returns output labels. accum_loss (optional): Where accumulated loss stores. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: output or the loss value of the current pipeline stage. """ micro_batch_data = self.load_micro_batch() data, label = self._get_data_label_for_current_step(input_obj, micro_batch_data, engine.criterion, engine.model) output_obj = self._call_engine(engine.model, data) if gpc.is_last_rank(ParallelMode.PIPELINE): if return_output_label: return_tensors.append((output_obj, label)) if accum_loss is not None: loss_reduced = self._call_engine_criterion(engine, output_obj, label) / self.num_microbatches accum_loss.add_(loss_reduced.detach()) return loss_reduced else: # forward only, it's useless since backward is not needed return output_obj else: if isinstance(output_obj, torch.Tensor): self._logger.debug( f'Global rank {gpc.get_global_rank()}, pipeline rank {gpc.get_local_rank(ParallelMode.PIPELINE)} forward output tensor {output_obj.shape}, dtype {output_obj.dtype}' ) return output_obj def _backward_step(self, engine, input_obj, output_obj, output_obj_grad): """Backward step through the passed-in output tensor. If it is the last stage, the output_obj_grad is None, otherwise it is the gradients with respect to stage's output tensor. Returns the gradients with respect to the input tensor (None if first stage). This is a helper function and can be ignored by users. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. input_obj (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): input tensor for this pipeline stage. output_obj (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): output tensor for this pipeline stage. output_obj_grad (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): gradient of output tensor for this pipeline stage. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: gradient of input tensor. """ # Retain the grad on the input_obj. if input_obj is not None: if isinstance(input_obj, torch.Tensor): input_obj.retain_grad() else: for in_tensor in input_obj: if in_tensor is not None: in_tensor.retain_grad() # Backward pass. if output_obj_grad is None: engine.backward(output_obj) else: engine.backward_by_grad(output_obj, output_obj_grad) # Collect the grad of the input_obj. input_obj_grad = None if input_obj is not None: if isinstance(input_obj, torch.Tensor): input_obj_grad = input_obj.grad else: input_obj_grad = [] for in_tensor in input_obj: input_obj_grad.append(in_tensor.grad) return input_obj_grad def forward_backward_step(self, engine, data_iter, forward_only=False, return_loss=True, return_output_label=True): """Runs non-interleaved 1F1B schedule, with communication between pipeline stages. Returns a tuple with losses if the last stage, an empty tuple otherwise. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. data_iter (Iterable): Dataloader as the form of an iterator, obtained by calling iter(dataloader). forward_only (bool, optional): Whether run forward step only. Default is false. If true, no backward will be run. return_loss (bool, optional): Whether returns the loss value. Default is true. return_output_label (bool, optional): If False, the output and label won't be returned. Returns: Tuple[:class:`torch.Tensor`]: A tuple of (output, label, loss), loss and label could be None. """ assert forward_only or return_loss, \ 'The argument \'return_loss\' has to be True when \'forward_only\' is False, but got False.' self.load_batch(data_iter) num_warmup_microbatches = \ (gpc.get_world_size(ParallelMode.PIPELINE) - gpc.get_local_rank(ParallelMode.PIPELINE) - 1) num_warmup_microbatches = min(num_warmup_microbatches, self.num_microbatches) num_microbatches_remaining = self.num_microbatches - num_warmup_microbatches # Input, output tensors only need to be saved when doing backward passes input_objs = None output_objs = None if not forward_only: input_objs = [] output_objs = [] return_tensors = [] if return_loss and gpc.is_pipeline_last_stage(ignore_virtual=True): accum_loss = torch.zeros(1, device=get_current_device()) else: accum_loss = None # Used for tensor meta information communication ft_shapes = self.tensor_shape bt_shapes = None fs_checker = self.tensor_shape is None # Run warmup forward passes. for i in range(num_warmup_microbatches): if not gpc.is_first_rank(ParallelMode.PIPELINE): ft_shapes = comm.recv_obj_meta(ft_shapes) input_obj = comm.recv_forward(ft_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) output_obj = self._forward_step(engine, input_obj, return_tensors, return_output_label=return_output_label, accum_loss=accum_loss) if not gpc.is_last_rank(ParallelMode.PIPELINE): if isinstance(output_obj, torch.Tensor): bt_shapes = output_obj.shape else: bt_shapes = [] for out_tensor in output_obj: bt_shapes.append(out_tensor.shape) fs_checker = comm.send_obj_meta(output_obj, fs_checker) comm.send_forward(output_obj, scatter_gather_tensors=self.scatter_gather_tensors) if not forward_only: input_objs.append(input_obj) output_objs.append(output_obj) # Before running 1F1B, need to receive first forward tensor. # If all microbatches are run in warmup / cooldown phase, then no need to # receive this tensor here. if num_microbatches_remaining > 0: if not gpc.is_first_rank(ParallelMode.PIPELINE): ft_shapes = comm.recv_obj_meta(ft_shapes) input_obj = comm.recv_forward(ft_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) # Run 1F1B in steady state. for i in range(num_microbatches_remaining): last_iteration = (i == (num_microbatches_remaining - 1)) output_obj = self._forward_step(engine, input_obj, return_tensors, return_output_label=return_output_label, accum_loss=accum_loss) if forward_only: comm.send_forward(output_obj, scatter_gather_tensors=self.scatter_gather_tensors) if not last_iteration: input_obj = comm.recv_forward(ft_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) else: output_obj_grad = comm.send_forward_recv_backward(output_obj, bt_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) # Add input_obj and output_obj to end of list. input_objs.append(input_obj) output_objs.append(output_obj) # Pop output_obj and output_obj from the start of the list for # the backward pass. input_obj = input_objs.pop(0) output_obj = output_objs.pop(0) input_obj_grad = self._backward_step(engine, input_obj, output_obj, output_obj_grad) if last_iteration: input_obj = None comm.send_backward(input_obj_grad, scatter_gather_tensors=self.scatter_gather_tensors) else: input_obj = comm.send_backward_recv_forward(input_obj_grad, ft_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) # Run cooldown backward passes. if not forward_only: for i in range(num_warmup_microbatches): input_obj = input_objs.pop(0) output_obj = output_objs.pop(0) output_obj_grad = comm.recv_backward(bt_shapes, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) input_obj_grad = self._backward_step(engine, input_obj, output_obj, output_obj_grad) comm.send_backward(input_obj_grad, scatter_gather_tensors=self.scatter_gather_tensors) if len(return_tensors) > 0: output, label = pack_return_tensors(return_tensors) return output, label, accum_loss else: return None, None, accum_loss class InterleavedPipelineSchedule(PipelineSchedule): def __init__(self, num_microbatches: int, num_model_chunks: int, data_process_func: Callable = None, tensor_shape: Union[torch.Size, List[int], Tuple[int]] = None, scatter_gather_tensors: bool = False): """A helper schedule class for pipeline parallelism running environment. It uses interleaved 1F1B strategy. Other properties are similar as :class:`NonPipelineSchedule`. Args: num_microbatches (int): The number of microbatches. num_model_chunks (int): The number of model chunks. data_process_func (Callable, optional): The preprocessing function which receives a batch of data, and it will be executed in `load_batch`. tensor_shape (torch.Size, optional): Specified shape in pipeline communication. scatter_gather_tensors (bool, optional): If set to `True`, communication will be reduced over pipeline when using 1D tensor parallelization. """ assert num_microbatches % gpc.get_world_size(ParallelMode.PIPELINE) == 0, \ 'num_microbatches must be an integer multiple of pipeline parallel world size' assert isinstance(num_model_chunks, int) and num_model_chunks > 0, \ f'expected num_model_chunks to be an integer and larger than 0, but got {num_model_chunks}' super().__init__(num_microbatches, data_process_func=data_process_func, tensor_shape=tensor_shape, scatter_gather_tensors=scatter_gather_tensors) gpc.set_virtual_pipeline_parallel_size(num_model_chunks) gpc.set_virtual_pipeline_parallel_rank(0) self.num_model_chunks = num_model_chunks def pre_processing(self, engine): from colossalai.zero.sharded_model.sharded_model_v2 import ShardedModelV2 if isinstance(engine.model, ShardedModelV2): self.dtype = torch.half elif isinstance(engine.model[0], NaiveAMPModel): self.dtype = torch.half for model in engine.model: if isinstance(model, NaiveAMPModel): model = model.model sig = inspect.signature(model.forward) for p in sig.parameters.values(): assert p.kind != inspect.Parameter.VAR_POSITIONAL, '*args is not supported' def load_batch(self, data_iter): super().load_batch(data_iter) # overwrite microbatch_offset, since model chunks load the same microbatch, and should tract the offset self.microbatch_offset = [0 for _ in range(self.num_model_chunks)] def load_micro_batch(self, model_chunk_id): data = self._get_data_slice(self.batch_data, self.microbatch_offset[model_chunk_id]) self.microbatch_offset[model_chunk_id] += self.microbatch_size return self._move_to_device(data) def _forward_step(self, engine, model_chunk_id, input_obj, return_tensors, return_output_label=True, accum_loss=None): """Forward step for passed-in model. If it is the first stage, the input tensor is obtained from data_iterator, otherwise the passed-in input_obj is used. Returns output tensor. This is a helper function and can be ignored by users. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. model_chunk_id (int): The id of model chunks. input_obj (Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]): Input tensor for this pipeline stage. return_tensors (List[:class:`torch.Tensor`]): A list of tensors to return. return_output_label (bool, optional): Whether returns output labels. accum_loss (optional): Where accumulated loss stores. Returns: Union[:class:`torch.Tensor`, List[:class:`torch.Tensor`]]: output or the loss value of the current pipeline stage. """ micro_batch_data = self.load_micro_batch(model_chunk_id) data, label = self._get_data_label_for_current_step(input_obj, micro_batch_data, engine.criterion, engine.model[model_chunk_id]) output_obj = self._call_engine(engine.model[model_chunk_id], data) if gpc.is_pipeline_last_stage(): if return_output_label: return_tensors.append((output_obj, label)) if accum_loss is not None: loss_reduced = self._call_engine_criterion(engine, output_obj, label) / self.num_microbatches accum_loss.add_(loss_reduced.detach()) return loss_reduced else: # forward only, it's useless since backward is not needed return output_obj else: if isinstance(output_obj, torch.Tensor): self._logger.debug( f'Global rank {gpc.get_global_rank()}, pipeline rank {gpc.get_local_rank(ParallelMode.PIPELINE)} forward output tensor {output_obj.shape}, dtype {output_obj.dtype}' ) return output_obj def forward_backward_step(self, engine, data_iter, forward_only=False, return_loss=True, return_output_label=True): """Run interleaved 1F1B schedule (model split into model chunks), with communication between pipeline stages as needed. Args: engine (colossalai.engine.Engine): Colossalai engine for training and inference. data_iter (Iterable): Dataloader as the form of an iterator, obtained by calling iter(dataloader). forward_only (bool, optional): Whether run forward step only. Default is false. If true, no backward will be run. return_loss (bool, optional): Whether returns the loss value. Default is true. return_output_label (bool, optional): If False, the output and label won't be returned. Returns: Tuple[:class:`torch.Tensor`]: A tuple of (output, label, loss), loss and label could be None. The loss would be returned only in the last stage. """ assert forward_only or return_loss, \ 'The argument \'return_loss\' has to be True when \'forward_only\' is False, but got False.' self.load_batch(data_iter) model = engine.model input_objs = [[] for _ in range(len(model))] output_objs = [[] for _ in range(len(model))] return_tensors = [] if not forward_only: output_obj_grads = [[] for _ in range(len(model))] if return_loss and gpc.is_pipeline_last_stage(ignore_virtual=True): accum_loss = torch.zeros(1, device=get_current_device()) else: accum_loss = None # Used for obj meta information communication input_obj_shapes = [self.tensor_shape for _ in range(len(model))] output_obj_shapes = [None for _ in range(len(model))] send_tensor_shape_flags = [self.tensor_shape is None for _ in range(len(model))] pipeline_parallel_size = gpc.get_world_size(ParallelMode.PIPELINE) pipeline_parallel_rank = gpc.get_local_rank(ParallelMode.PIPELINE) # Compute number of warmup and remaining microbatches. num_model_chunks = len(model) num_microbatches = self.num_microbatches * num_model_chunks all_warmup_microbatches = False if forward_only: num_warmup_microbatches = num_microbatches else: # Run all forward passes and then all backward passes if number of # microbatches is just the number of pipeline stages. # Otherwise, perform (num_model_chunks-1)*pipeline_parallel_size on # all workers, followed by more microbatches after depending on # stage ID (more forward passes for earlier stages, later stages can # immediately start with 1F1B). if self.num_microbatches == pipeline_parallel_size: num_warmup_microbatches = num_microbatches all_warmup_microbatches = True else: num_warmup_microbatches = \ (pipeline_parallel_size - pipeline_parallel_rank - 1) * 2 num_warmup_microbatches += (num_model_chunks - 1) * pipeline_parallel_size num_warmup_microbatches = min(num_warmup_microbatches, num_microbatches) num_microbatches_remaining = \ num_microbatches - num_warmup_microbatches def get_model_chunk_id(microbatch_id, forward): """Helper method to get the model chunk ID given the iteration number.""" microbatch_id_in_group = microbatch_id % (pipeline_parallel_size * num_model_chunks) model_chunk_id = microbatch_id_in_group // pipeline_parallel_size if not forward: model_chunk_id = (num_model_chunks - model_chunk_id - 1) return model_chunk_id def _forward_step_helper(microbatch_id): """Helper method to run forward step with model split into chunks (run set_virtual_pipeline_model_parallel_rank() before calling forward_step()).""" model_chunk_id = get_model_chunk_id(microbatch_id, forward=True) gpc.set_virtual_pipeline_parallel_rank(model_chunk_id) # forward step if gpc.is_pipeline_first_stage(): if len(input_objs[model_chunk_id]) == \ len(output_objs[model_chunk_id]): input_objs[model_chunk_id].append(None) input_obj = input_objs[model_chunk_id][-1] output_obj = self._forward_step(engine, model_chunk_id, input_obj, return_tensors, return_output_label=return_output_label, accum_loss=accum_loss) output_objs[model_chunk_id].append(output_obj) # if forward-only, no need to save tensors for a backward pass if forward_only: input_objs[model_chunk_id].pop() output_objs[model_chunk_id].pop() return output_obj def _backward_step_helper(microbatch_id): """Helper method to run backward step with model split into chunks (run set_virtual_pipeline_model_parallel_rank() before calling backward_step()).""" model_chunk_id = get_model_chunk_id(microbatch_id, forward=False) gpc.set_virtual_pipeline_parallel_rank(model_chunk_id) if gpc.is_pipeline_last_stage(): if len(output_obj_grads[model_chunk_id]) == 0: output_obj_grads[model_chunk_id].append(None) input_obj = input_objs[model_chunk_id].pop(0) output_obj = output_objs[model_chunk_id].pop(0) output_obj_grad = output_obj_grads[model_chunk_id].pop(0) input_obj_grad = self._backward_step(engine, input_obj, output_obj, output_obj_grad) return input_obj_grad # Run warmup forward passes. gpc.set_virtual_pipeline_parallel_rank(0) if not gpc.is_pipeline_first_stage(): input_obj_shapes[0] = comm.recv_obj_meta(input_obj_shapes[0]) input_objs[0].append( comm.recv_forward(input_obj_shapes[0], dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors)) for k in range(num_warmup_microbatches): model_chunk_id = get_model_chunk_id(k, forward=True) output_obj = _forward_step_helper(k) if not gpc.is_pipeline_last_stage(): if isinstance(output_obj, torch.Tensor): output_obj_shapes[model_chunk_id] = output_obj.shape else: output_obj_shapes[model_chunk_id] = [] for out_tensor in output_obj: output_obj_shapes[model_chunk_id].append(out_tensor.shape) send_tensor_shape_flags[model_chunk_id] = comm.send_obj_meta(output_obj, send_tensor_shape_flags[model_chunk_id]) # Determine if tensor should be received from previous stage. next_forward_model_chunk_id = get_model_chunk_id(k + 1, forward=True) recv_prev = True if gpc.is_pipeline_first_stage(ignore_virtual=True): if next_forward_model_chunk_id == 0: recv_prev = False if k == (num_microbatches - 1): recv_prev = False # Don't send tensor downstream if on last stage. if gpc.is_pipeline_last_stage(): output_obj = None with switch_virtual_pipeline_parallel_rank(next_forward_model_chunk_id): if not gpc.is_pipeline_first_stage(): input_obj_shapes[next_forward_model_chunk_id] = comm.recv_obj_meta( input_obj_shapes[next_forward_model_chunk_id]) # Send and receive tensors as appropriate (send tensors computed # in this iteration; receive tensors for next iteration). input_shape = input_obj_shapes[next_forward_model_chunk_id] if recv_prev else None if k == (num_warmup_microbatches - 1) and not forward_only and \ not all_warmup_microbatches: input_obj_grad = None recv_next = True if gpc.is_pipeline_last_stage(ignore_virtual=True): recv_next = False output_shape = output_obj_shapes[num_model_chunks - 1] if recv_next else None input_obj, output_obj_grad = \ comm.send_forward_backward_recv_forward_backward( output_obj, input_obj_grad, input_shape, output_shape, recv_prev=recv_prev, recv_next=recv_next, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) output_obj_grads[num_model_chunks - 1].append(output_obj_grad) else: input_obj = \ comm.send_forward_recv_forward( output_obj, input_shape, recv_prev=recv_prev, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) input_objs[next_forward_model_chunk_id].append(input_obj) # Run 1F1B in steady state. for k in range(num_microbatches_remaining): # Forward pass. forward_k = k + num_warmup_microbatches output_obj = _forward_step_helper(forward_k) # Backward pass. backward_k = k input_obj_grad = _backward_step_helper(backward_k) # Send output_obj and input_obj_grad, receive input_obj # and output_obj_grad. # Determine if current stage has anything to send in either direction, # otherwise set obj to None. forward_model_chunk_id = get_model_chunk_id(forward_k, forward=True) gpc.set_virtual_pipeline_parallel_rank(forward_model_chunk_id) if gpc.is_pipeline_last_stage(): output_obj = None backward_model_chunk_id = get_model_chunk_id(backward_k, forward=False) gpc.set_virtual_pipeline_parallel_rank(backward_model_chunk_id) if gpc.is_pipeline_first_stage(): input_obj_grad = None # Determine if peers are sending, and where in data structure to put # received tensors. recv_prev = True if gpc.is_pipeline_first_stage(ignore_virtual=True): # First stage is ahead of last stage by (pipeline_parallel_size - 1). next_forward_model_chunk_id = get_model_chunk_id(forward_k - (pipeline_parallel_size - 1), forward=True) if next_forward_model_chunk_id == (num_model_chunks - 1): recv_prev = False next_forward_model_chunk_id += 1 else: next_forward_model_chunk_id = get_model_chunk_id(forward_k + 1, forward=True) recv_next = True if gpc.is_pipeline_last_stage(ignore_virtual=True): # Last stage is ahead of first stage by (pipeline_parallel_size - 1). next_backward_model_chunk_id = get_model_chunk_id(backward_k - (pipeline_parallel_size - 1), forward=False) if next_backward_model_chunk_id == 0: recv_next = False next_backward_model_chunk_id -= 1 else: next_backward_model_chunk_id = get_model_chunk_id(backward_k + 1, forward=False) # If last iteration, don't receive; we already received one extra # before the start of the for loop. if k == (num_microbatches_remaining - 1): recv_prev = False input_shape = input_obj_shapes[next_forward_model_chunk_id] if recv_prev else None output_shape = output_obj_shapes[next_backward_model_chunk_id] if recv_next else None # Communicate objs. input_obj, output_obj_grad = \ comm.send_forward_backward_recv_forward_backward( output_obj, input_obj_grad, input_shape, output_shape, recv_prev=recv_prev, recv_next=recv_next, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors) # Put input_obj and output_obj_grad in data structures in the # right location. if recv_prev: input_objs[next_forward_model_chunk_id].append(input_obj) if recv_next: output_obj_grads[next_backward_model_chunk_id].append(output_obj_grad) # Run cooldown backward passes (flush out pipeline). if not forward_only: if all_warmup_microbatches: output_obj_grads[num_model_chunks - 1].append( comm.recv_backward(output_obj_shapes[num_model_chunks - 1], scatter_gather_tensors=self.scatter_gather_tensors)) for k in range(num_microbatches_remaining, num_microbatches): input_obj_grad = _backward_step_helper(k) next_backward_model_chunk_id = get_model_chunk_id(k + 1, forward=False) recv_next = True if gpc.is_pipeline_last_stage(ignore_virtual=True): if next_backward_model_chunk_id == (num_model_chunks - 1): recv_next = False if k == (num_microbatches - 1): recv_next = False output_shape = output_obj_shapes[next_backward_model_chunk_id] if recv_next else None output_obj_grads[next_backward_model_chunk_id].append( comm.send_backward_recv_backward(input_obj_grad, output_shape, recv_next=recv_next, dtype=self.dtype, scatter_gather_tensors=self.scatter_gather_tensors)) if len(return_tensors) > 0: output, label = pack_return_tensors(return_tensors) return output, label, accum_loss else: return None, None, accum_loss
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod class BaseGradientHandler(ABC): """A basic helper class to handle all-reduce operations of gradients across different parallel groups before optimization. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def __init__(self, model, optimizer): self._model = model self._optimizer = optimizer @abstractmethod def handle_gradient(self): """A method to accumulate gradients across different parallel groups. Users should write their own functions or just use the functions in pre-defined subclasses. """ pass
from ._base_gradient_handler import BaseGradientHandler from ._data_parallel_gradient_handler import DataParallelGradientHandler from ._zero_gradient_handler import ZeROGradientHandler from ._sequence_parallel_gradient_handler import SequenceParallelGradientHandler from ._pipeline_parallel_gradient_handler import PipelineSharedModuleGradientHandler from ._moe_gradient_handler import MoeGradientHandler from ._sequence_parallel_gradient_handler import SequenceParallelGradientHandler __all__ = [ 'BaseGradientHandler', 'DataParallelGradientHandler', 'ZeROGradientHandler', 'PipelineSharedModuleGradientHandler', 'MoeGradientHandler', 'SequenceParallelGradientHandler' ]
from colossalai.core import global_context as gpc from colossalai.registry import GRADIENT_HANDLER from ._base_gradient_handler import BaseGradientHandler from ...context.parallel_mode import ParallelMode from .utils import bucket_allreduce @GRADIENT_HANDLER.register_module class DataParallelGradientHandler(BaseGradientHandler): """A helper class to handle all-reduce operations in a data parallel group. A all-reduce collective communication will be operated in :func:`handle_gradient` among a data parallel group. For better performance, it bucketizes the gradients of all parameters that are the same type to improve the efficiency of communication. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def handle_gradient(self): """A method running a all-reduce operation in a data parallel group. """ # TODO: add memory buffer if gpc.data_parallel_size > 1: bucket_allreduce(param_list=self._model.parameters(), group=gpc.get_group(ParallelMode.DATA))
#!/usr/bin/env python from collections import defaultdict import torch import torch.distributed as dist from colossalai.core import global_context as gpc from colossalai.registry import GRADIENT_HANDLER from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors from ._base_gradient_handler import BaseGradientHandler @GRADIENT_HANDLER.register_module class PipelineSharedModuleGradientHandler(BaseGradientHandler): """A helper class to handle all-reduce operations in sub parallel groups. A all-reduce collective communication will be operated in :func:`handle_gradient` among all sub pipeline parallel groups. For better performance, it bucketizes the gradients of all parameters that are the same type to improve the efficiency of communication. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def handle_gradient(self): """A method running a all-reduce operation in sub pipeline parallel groups. """ if gpc.pipeline_parallel_size > 1: # bucketize and all-reduce buckets = defaultdict(lambda: defaultdict(list)) # Pack the buckets. for param in self._model.parameters(): group = getattr(param, 'pipeline_shared_module_pg', None) if param.requires_grad and group is not None and ( (hasattr(param, 'colo_attr') and not param.colo_attr.saved_grad.is_null()) or param.grad is not None): tp = param.data.type() buckets[group][tp].append(param) # For each bucket, all-reduce and copy all-reduced grads. for group, group_buckets in buckets.items(): for tp, bucket in group_buckets.items(): grads = [ param.colo_attr.grad_payload if hasattr(param, 'colo_attr') else param.grad.data for param in bucket ] coalesced = _flatten_dense_tensors(grads).to(torch.cuda.current_device()) dist.all_reduce(coalesced, op=dist.ReduceOp.SUM, group=group) for buf, synced in zip(grads, _unflatten_dense_tensors(coalesced, grads)): buf.copy_(synced)
from typing import Iterable import torch.distributed as dist import torch.nn as nn from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors def bucket_allreduce(param_list: Iterable[nn.Parameter], group=None): # get communication world size comm_size = dist.get_world_size(group) # bucketize and all-reduce buckets = {} # Pack the buckets. for param in param_list: if param.requires_grad and param.grad is not None: tp = param.data.type() if tp not in buckets: buckets[tp] = [] buckets[tp].append(param) # For each bucket, all-reduce and copy all-reduced grads. for tp in buckets: bucket = buckets[tp] grads = [param.grad.data for param in bucket] coalesced = _flatten_dense_tensors(grads) coalesced /= comm_size dist.all_reduce(coalesced, group=group) for buf, synced in zip(grads, _unflatten_dense_tensors(coalesced, grads)): buf.copy_(synced)
from colossalai.core import global_context as gpc from colossalai.registry import GRADIENT_HANDLER from colossalai.utils.moe import get_moe_epsize_param_dict from ._base_gradient_handler import BaseGradientHandler from ...context.parallel_mode import ParallelMode from .utils import bucket_allreduce from colossalai.context.moe_context import MOE_CONTEXT @GRADIENT_HANDLER.register_module class MoeGradientHandler(BaseGradientHandler): """A helper class to handle all-reduce operations in a data parallel group and moe model parallel. A all-reduce collective communication will be operated in :func:`handle_gradient` among a data parallel group. For better performance, it bucketizes the gradients of all parameters that are the same type to improve the efficiency of communication. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def __init__(self, model, optimizer=None): super().__init__(model, optimizer) def handle_gradient(self): """A method running an all-reduce operation in a data parallel group. Then running an all-reduce operation for all parameters in experts across moe model parallel group """ global_data = gpc.data_parallel_size if global_data > 1: epsize_param_dict = get_moe_epsize_param_dict(self._model) # epsize is 1, indicating the params are replicated among processes in data parallelism # use the ParallelMode.DATA to get data parallel group # reduce gradients for all parameters in data parallelism if 1 in epsize_param_dict: bucket_allreduce(param_list=epsize_param_dict[1], group=gpc.get_group(ParallelMode.DATA)) for ep_size in epsize_param_dict: if ep_size != 1 and ep_size != MOE_CONTEXT.world_size: bucket_allreduce(param_list=epsize_param_dict[ep_size], group=MOE_CONTEXT.parallel_info_dict[ep_size].dp_group)
from colossalai.registry import GRADIENT_HANDLER from ._base_gradient_handler import BaseGradientHandler @GRADIENT_HANDLER.register_module class ZeROGradientHandler(BaseGradientHandler): """A helper class to handle all-reduce operations in a data parallel group. A all-reduce collective communication will be operated in :func:`handle_gradient` among a data parallel group. This class is specialized with ZeRO optimization. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def handle_gradient(self): """A method running a all-reduce operation in a data parallel group. """ self._optimizer.sync_grad()
from colossalai.core import global_context as gpc from colossalai.registry import GRADIENT_HANDLER from ._base_gradient_handler import BaseGradientHandler from ...context.parallel_mode import ParallelMode from .utils import bucket_allreduce @GRADIENT_HANDLER.register_module class SequenceParallelGradientHandler(BaseGradientHandler): """A helper class to handle all-reduce operations in a data parallel group. A all-reduce collective communication will be operated in :func:`handle_gradient` among a data parallel group. For better performance, it bucketizes the gradients of all parameters that are the same type to improve the efficiency of communication. Args: model (Module): Model where the gradients accumulate. optimizer (Optimizer): Optimizer for updating the parameters. """ def handle_gradient(self): """A method running a all-reduce operation in a data parallel group. """ if gpc.get_world_size(ParallelMode.SEQUENCE_DP) > 1: bucket_allreduce(param_list=self._model.parameters(), group=gpc.get_group(ParallelMode.SEQUENCE_DP))
import torch.nn as nn from typing import List from colossalai.engine import BaseGradientHandler from typing import Iterable from torch.optim import Optimizer from torch.optim.lr_scheduler import _LRScheduler from ._gradient_accumulation import GradAccumDataloader, GradAccumOptimizer, GradAccumLrSchedulerByStep, GradAccumGradientHandler __all__ = [ 'accumulate_gradient', 'GradAccumDataloader', 'GradAccumOptimizer', 'GradAccumLrSchedulerByStep', 'GradAccumGradientHandler' ] def accumulate_gradient(model: nn.Module, optimizer: Optimizer, dataloader: Iterable, accumulate_size: int, gradient_handlers: List[BaseGradientHandler] = None, lr_scheduler: _LRScheduler = None): r"""Turning model, optimizer, dataloader into corresponding object for gradient accumulation. Args: model (:class:`torch.nn.Module`): your model object for gradient accumulation. optimizer (:class:`torch.optim.Optimizer`): your optimizer object for gradient accumulation. dataloader (:class:`torch.utils.data.DataLoader` or iterable objects): your dataloader object, would be called like iter(dataloader) accumulate_size (int): the number of steps to accumulate gradients gradient_handlers (List[:class:`colossalai.engine.BaseGradientHandler`]): list of gradient handler objects. Default is None. lr_scheduler (`torch.optim.lr_scheduler` or `colossalai.nn.lr_scheduler`): your ``lr_scheduler`` object for gradient accumulation. Defaults to None. More details about `gradient_handlers` could be found in `Gradient_handler <https://github.com/hpcaitech/ColossalAI/tree/main/colossalai/engine/gradient_handler>`_. More details about `lr_scheduler` could be found `lr_scheduler <https://github.com/hpcaitech/ColossalAI/tree/main/colossalai/nn/lr_scheduler>`_. and `how to adjust learning rate <https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate>`_. """ optimizer = GradAccumOptimizer(optimizer, accumulate_size=accumulate_size, model=model) dataloader = GradAccumDataloader(dataloader, accumulate_size=accumulate_size) if gradient_handlers is not None: gradient_handlers = [GradAccumGradientHandler(handler, accumulate_size) for handler in gradient_handlers] if lr_scheduler is not None: lr_scheduler = GradAccumLrSchedulerByStep(lr_scheduler, accumulate_size=accumulate_size) return optimizer, dataloader, gradient_handlers, lr_scheduler
#!/usr/bin/env python # -*- encoding: utf-8 -*- from typing import Union import torch.nn as nn from torch import Tensor from typing import Iterable, Any, Tuple from colossalai.nn.optimizer import ColossalaiOptimizer from torch.nn.parallel.distributed import DistributedDataParallel from torch.optim import Optimizer from torch.optim.lr_scheduler import _LRScheduler from torch.utils.data import DataLoader from colossalai.utils import conditional_context from colossalai.engine import BaseGradientHandler class GradAccumOptimizer(ColossalaiOptimizer): """A wrapper for the optimizer to enable gradient accumulation by skipping the steps before accumulation size is reached. Args: optim (:class:`torch.optim.Optimizer`): Your optimizer object for gradient accumulation. accumulate_size (int): The number of steps to accumulate gradients. model (:class:`torch.nn.Module`): Your model object to check if it is DistributedDataParallel for special handling of no_sync() context. """ def __init__(self, optim: Optimizer, accumulate_size: int, model: nn.Module = None): super().__init__(optim) self.accumulate_size = accumulate_size self.accumulate_step = 0 # handle pytorch ddp auto all reduce self.model = model self.is_torch_ddp = isinstance(self.model, DistributedDataParallel) def zero_grad(self, *args, **kwargs) -> None: """ Set all gradients to zero. Args: *args: positional arguments for the optimizer wrapped **kwargs: keyword arguments for the optimizer wrapped """ if self.accumulate_step == 0: self.optim.zero_grad(*args, **kwargs) def step(self, *args, **kwargs) -> None: """ Update the model parameters. Args: *args: positional arguments for the optimizer wrapped **kwargs: keyword arguments for the optimizer wrapped """ if self.accumulate_step < self.accumulate_size: return None else: self.accumulate_step = 0 return self.optim.step(*args, **kwargs) def clip_grad_norm(self, model: nn.Module, max_norm: float) -> None: """ Clip gradients by norm. Args: model (:class:`torch.nn.Module`): a torch module instance max_norm (float): the max norm for gradient clipping """ if self.accumulate_step < self.accumulate_size: pass else: self.optim.clip_grad_norm(model, max_norm) def backward(self, loss: Tensor) -> None: """Execute backward pass. Args: loss (:class:`torch.Tensor`): the loss value. """ self.accumulate_step += 1 if self.is_torch_ddp: no_sync = self.accumulate_step < self.accumulate_size with conditional_context(self.model.no_sync(), enable=no_sync): scaled_loss = loss / self.accumulate_size self.optim.backward(scaled_loss) else: scaled_loss = loss / self.accumulate_size self.optim.backward(scaled_loss) def backward_by_grad(self, tensor: Tensor, grad: Tensor) -> None: """Execute backward pass given the gradients of the output. Args: loss (:class:`torch.Tensor`): the loss value. grad (:class:`torch.Tensor`): the output gradient. """ self.accumulate_step += 1 no_sync = self.is_torch_ddp and self.accumulate_step < self.accumulate_size if no_sync: with self.model.no_sync(): self.optim.backward_by_grad(tensor, grad) else: self.optim.backward_by_grad(tensor, grad) class GradAccumDataloader: """A wrapper for dataloader to enable gradient accumulation by dropping the last incomplete steps. Note: The dataloader would drop the last incomplete steps for gradient accumulation. For example, if a dataloader has 10 batches of data and accumulate size is 4. The model parameters will be updated only twice at step 4 and step 8. The last two batches of data do not form a complete 4-step cycle. Thus, they will be automatically skipped by this class. If the dataloader is not standard PyTorch dataloader, (e.g. Dali dataloader), this class will automatically consume (load data for nothing) the remaining 2 batches. Args: dataloader (``Iterable``): Your dataloader object for gradient accumulation. accumulate_size (int): The number of steps to accumulate gradients. """ def __init__(self, dataloader: Iterable, accumulate_size: int) -> None: self.dataloader = dataloader self.consume_remain_data = not isinstance(dataloader, DataLoader) self.steps_per_epoch = len(dataloader) - len(dataloader) % accumulate_size def __getattr__(self, __name: str) -> Any: return getattr(self.dataloader, __name) def __len__(self) -> int: return self.steps_per_epoch def __iter__(self) -> Iterable: self._cur_step = 0 self._dataiter = iter(self.dataloader) return self def __next__(self) -> Union[Tensor, Tuple[Tensor]]: if self._cur_step < self.steps_per_epoch: self._cur_step += 1 data = next(self._dataiter) if self._cur_step == self.steps_per_epoch and self.consume_remain_data: # this is to handle non standard pytorch dataloader # such as dali dataloader while True: try: _ = next(self._dataiter) except StopIteration: break return data else: raise StopIteration class GradAccumLrSchedulerByStep(_LRScheduler): """A wrapper for the LR scheduler to enable gradient accumulation by skipping the steps before accumulation size is reached. Args: lr_scheduler (:class:`torch.optim.lr_scheduler._LRScheduler`): Your ``lr_scheduler`` object for gradient accumulation. accumulate_size (int): The number of steps to accumulate gradients. """ def __init__(self, lr_scheduler: _LRScheduler, accumulate_size: int) -> None: self.lr_scheduler = lr_scheduler self.accumulate_size = accumulate_size self.accumulate_step = 0 @staticmethod def compute_effective_steps_per_epoch(dataloader: Iterable, accumulate_size: int) -> int: """ Computes the number of effective training iterations. An effective iteration is defined as the the aggregation of <accumulate_size> iterations. For examples, if accumulate_size = 4, then 4 iterations are considered as one effective iteration. Args: dataloader (``Iterable``): Your dataloader object for gradient accumulation. accumulate_size (int): The number of steps to accumulate gradients. """ return len(dataloader) // accumulate_size def __getattr__(self, __name: str) -> Any: return getattr(self.lr_scheduler, __name) def step(self, *args, **kwargs) -> None: """ Update the learning rate. Args: *args: positional arguments for the lr scheduler wrapped. **kwargs: keyword arguments for the lr scheduler wrapped. """ self.accumulate_step += 1 if self.accumulate_step < self.accumulate_size: pass else: self.accumulate_step = 0 self.lr_scheduler.step(*args, **kwargs) def get_lr(self) -> Tensor: """ Compute the next learning rate. Returns: Tensor: the upcoming learning rate. """ return self.lr_scheduler.get_lr() def get_last_lr(self) -> Tensor: """ Returns the current learning rate. Returns: Tensor: the current learning rate. """ return self.lr_scheduler.get_last_lr() def print_lr(self, *args, **kwargs) -> None: """ Print he learning rate. Args: *args: positional arguments for the lr scheduler wrapped. **kwargs: keyword arguments for the lr scheduler wrapped. """ self.lr_scheduler.print_lr(*args, **kwargs) def state_dict(self) -> dict: """ Returns the states of the lr scheduler as dictionary. Returns: dict: the states of the lr scheduler. """ return self.lr_scheduler.state_dict() def load_state_dict(self, state_dict: dict) -> None: """ Load the states of the lr scheduler from a dictionary object. Returns: dict: the states of the lr scheduler. """ self.lr_scheduler.load_state_dict(state_dict) class GradAccumGradientHandler: r"""A wrapper for the gradient handler to enable gradient accumulation by skipping the steps before accumulation size is reached. Args: grad_handler (:class:`colossalai.engine.BaseGradientHandler`): Your ``gradient_handler`` object for gradient accumulation, would be called when achieving `accumulate_size`. accumulate_size (int): The number of steps to accumulate gradients. More details about ``gradient_handlers`` could be found in `Gradient_handler <https://github.com/hpcaitech/ColossalAI/tree/main/colossalai/engine/gradient_handler>`_. """ def __init__(self, grad_handler: BaseGradientHandler, accumulate_size: int) -> None: assert isinstance(grad_handler, BaseGradientHandler), \ f'expected grad_handler to be type BaseGradientHandler, but got {type(grad_handler)}' self.grad_handler = grad_handler self.accumulate_size = accumulate_size self.accumulate_step = 0 def handle_gradient(self) -> None: """ Handle gradients reduction only in the last gradient accumulation step. """ self.accumulate_step += 1 if self.accumulate_step < self.accumulate_size: pass else: self.accumulate_step = 0 self.grad_handler.handle_gradient()