python_code
stringlengths
0
456k
from .pipelinable import PipelinableContext, PipelinableModel from .layer_spec import LayerSpec __all__ = ['PipelinableModel', 'PipelinableContext', 'LayerSpec']
import heapq import inspect import torch from colossalai.logging import get_dist_logger from colossalai.nn.layer.utils import CheckpointModule from typing import List from collections import OrderedDict def _binary_partition(weights: List, start: int, end: int): """Returns the binary partition position of `weights`, given the start position `st` and the end position `ed`. Args: weights (list): A python list to be binary partitioned start (int): the start position of the binary partition end (int): the end position of the binary partition Returns: int: the binary partition position of `weights` """ w_sum = weights[end - 1] prefix = 0 if start > 0: w_sum -= weights[start - 1] prefix = weights[start - 1] minimum = float("inf") for idx in range(start + 1, end): front = weights[idx - 1] - prefix diff = abs(w_sum - 2 * front) if diff < minimum: pos = idx minimum = diff return start, pos, end def _heap_addition(weights: List, intervals: int, add_cnt: int): """ """ def _heap_push(heap, st, ed): value = weights[ed - 1] if st > 0: value -= weights[st - 1] heapq.heappush(heap, (-value, st, ed)) ret_intervals = [] heap = [] for st, ed in intervals: _heap_push(heap, st, ed) while add_cnt > 0: _, st, ed = heapq.heappop(heap) if ed - st == 1: ret_intervals.append((st, ed)) else: l, m, r = _binary_partition(weights, st, ed) _heap_push(heap, l, m) _heap_push(heap, m, r) add_cnt -= 1 while heap: _, st, ed = heapq.heappop(heap) ret_intervals.append((st, ed)) ret_intervals.sort() return ret_intervals def _calc_partitions(weights, value): prev = 0 prefix = 0 num_block = 0 intervals = [] for idx, w in enumerate(weights): if weights[idx] - prefix > value: intervals.append((prev, idx)) prev = idx prefix = weights[idx - 1] num_block += 1 intervals.append((prev, len(weights))) return num_block + 1, intervals def _binary_search(weights, num): length = len(weights) prefix = [1 if w == 0 else w for w in weights] for i in range(1, length): prefix[i] += prefix[i - 1] lower_bound = max(weights) upper_bound = prefix[length - 1] while upper_bound > lower_bound: mid = (upper_bound + lower_bound) // 2 number, _ = _calc_partitions(prefix, mid) if number <= num: upper_bound = mid else: lower_bound = mid + 1 num_block, intervals = _calc_partitions(prefix, upper_bound) if num_block < num: intervals = _heap_addition(prefix, intervals, num - num_block) return intervals def partition_uniform(num_items, pipeline_parallel_size, num_chunks): assert num_items % num_chunks == 0, \ "Layer length should be divided by the number of chunks, otherwise parameter method is recomended" logger = get_dist_logger() parts = [[] for _ in range(pipeline_parallel_size)] partition_items = num_items // num_chunks for idx in range(num_chunks): base_idx = idx * partition_items chunk_size = partition_items // pipeline_parallel_size left = pipeline_parallel_size - partition_items % pipeline_parallel_size if chunk_size == 0: logger.warning("Some nodes in Pipeline have no requests") for p in range(pipeline_parallel_size): st = base_idx base_idx += chunk_size + (p >= left) parts[p].append((st, base_idx)) return parts def partition_balanced(weights, pipeline_parallel_size, num_chunks): num_total = pipeline_parallel_size * num_chunks num_items = len(weights) if num_items <= num_total: return partition_uniform(num_items, pipeline_parallel_size, num_chunks) intervals = _binary_search(weights, num_total) current = 0 parts = [[] for _ in range(pipeline_parallel_size)] for inter in intervals: parts[current].append(inter) current = (current + 1) % pipeline_parallel_size return parts def build_kwargs_for_module(function, input_tensor, kw_dict): """ Generally, the first argument of module.forward is an input tensor come from the previous layer. Therefore, we just filter the kwargs from second element of the dictionary. """ sig = inspect.signature(function) if input_tensor is None: kwargs_offset = 0 elif isinstance(input_tensor, torch.Tensor): kwargs_offset = 1 elif isinstance(input_tensor, (tuple, OrderedDict)): #assert isinstance(input_tensor, tuple), f'input_tensor should be a torch.Tensor or a tuple object.' # Huggingface will take their own structures based on OrderedDict as the output # between layers so we've to close this check. kwargs_offset = len(input_tensor) args_name_list = list(sig.parameters.keys()) kw_dict = {k: v for k, v in kw_dict.items() if k in args_name_list[kwargs_offset:]} if len(kw_dict) == 0: return None return kw_dict def build_kwargs_for_function(function, kw_dict): sig = inspect.signature(function) kw_dict = {k: v for k, v in kw_dict.items() if k in sig.parameters} if len(kw_dict) == 0: return None return kw_dict def exec_func_with_kwargs(func, kw_dict, input_tensor, kwargs): """ We suppose the callable object passed to to_layer_list method in two purpose: a. use the callable object to modify input tensor, such as \ lambda x: torch.flatten(x, 1) b. use the callable object to modify kwargs value, such as \ def foo(attention_mask=None): if attention_mask is not None: batch_size = input_ids.shape[0] attention_mask = attention_mask.view(batch_size, -1) return attention_mask """ if kw_dict is not None: rst = func(**kw_dict) if isinstance(rst, tuple): for i, k in enumerate(kw_dict.keys()): kwargs[k] = rst[i] else: for k in kw_dict.keys(): kwargs[k] = rst return input_tensor if isinstance(input_tensor, tuple): assert len(input_tensor) > 0, f'input_tensor should not be empty, when kw_dict is None.' sig = inspect.signature(func) func_args_num = len(sig.parameters) assert func_args_num <= len( input_tensor), f'func requires {func_args_num} arguments, but input_tensors only have {len(input_tensor)}.' if func_args_num < len(input_tensor): return func(*input_tensor[:func_args_num]) else: return func(*input_tensor) assert isinstance(input_tensor, torch.Tensor), 'input_tensor should be a type of torch.Tensor or tuple.' return func(input_tensor) def exec_funcs_with_kwargs(func_dict, func_key, input_tensor, kwargs): assert func_key in func_dict, f"{func_key} is not in the function_dict." funcs_to_exec = func_dict[func_key] if isinstance(funcs_to_exec, list): for f in funcs_to_exec: f_kwargs = build_kwargs_for_function(f, kwargs) input_tensor = exec_func_with_kwargs(f, f_kwargs, input_tensor, kwargs) else: f_kwargs = build_kwargs_for_function(funcs_to_exec, kwargs) input_tensor = exec_func_with_kwargs(funcs_to_exec, f_kwargs, input_tensor, kwargs) return input_tensor def call_module(module, args=None, kwargs=None): if args is None: args = () if kwargs is None: kwargs = {} if isinstance(module, CheckpointModule): forward_func = module._forward else: forward_func = module.forward sig = inspect.signature(forward_func) param_nums = len(sig.parameters) feed_nums = len(args) + len(kwargs) args_needed_nums = param_nums - len(kwargs) args_needed = args[:args_needed_nums] if isinstance(module, CheckpointModule): convert_kwargs_to_args = [] for v in kwargs.values(): convert_kwargs_to_args.append(v) return module(*args_needed, *convert_kwargs_to_args) else: return module(*args_needed, **kwargs) def customized_partition(exec_seq): ''' This function will analyze the exec_seq. In the exec_seq, users will use 'SPLIT_NODE' as an annotation to note the partition point. ''' customized_parts = {} start = 0 stop = 0 rank = 0 for element in exec_seq: if isinstance(element, str): if element == 'SPLIT_NODE': customized_parts[rank] = [(start, stop)] start = stop rank += 1 else: stop += 1 customized_parts[rank] = [(start, stop)] return customized_parts
import torch from colossalai.utils.model.utils import call_to_str class LayerSpec: """ """ def __init__(self, typename, *module_args, **module_kwargs): self.typename = typename self.module_args = module_args self.module_kwargs = module_kwargs self.children = None self._param_count = 0 if not issubclass(typename, torch.nn.Module): raise RuntimeError('LayerSpec only supports torch.nn.Module types.') def __repr__(self): return call_to_str(self.typename.__name__, self.module_args, self.module_kwargs) @property def param_count(self): return self._param_count def build(self): """Build the stored specification.""" recovered_args = [] for obj in self.module_args: if isinstance(obj, LayerSpec): obj = obj.build() recovered_args.append(obj) recovered_args = tuple(recovered_args) recovered_kwargs = {} for k, v in self.module_kwargs.items(): if isinstance(v, LayerSpec): v = v.build() recovered_kwargs[k] = v return self.typename(*recovered_args, **recovered_kwargs) def set_children(self, children): self.children = children def count_params(self): self._param_count = 0 layer = self.build() for param in layer.parameters(): self._param_count += param.numel() return self._param_count def reset_param_count(self): self._param_count = 0
from .topo import Topo, Partition, PartitionOutputVal, PartitionInputVal __all__ = ['Topo', 'Partition', 'PartitionOutputVal', 'PartitionInputVal']
from typing import Dict, List from dataclasses import dataclass # This file includes data structure used by Pipeline Middleware. @dataclass class ValPosition: partition_id: int offset: int def __str__(self) -> str: res = f'[partition_id:{self.partition_id},offset:{self.offset}]' return res def __repr__(self) -> str: return self.__str__() class PartitionInputVal(object): def __init__(self, partition_id, offset) -> None: # every input from which partition_id and which offset val_pos = ValPosition(partition_id, offset) self._from_partition_and_offset: ValPosition = val_pos def get(self): return self._from_partition_and_offset def __str__(self) -> str: res = '' res += f'<-({self._from_partition_and_offset})' return res def __repr__(self) -> str: return self.__str__() class PartitionOutputVal(object): def __init__(self) -> None: # every output to which partition_id and which offset self._to_partition_and_offset: List[ValPosition] = [] def add(self, partition_id, offset): val_pos = ValPosition(partition_id, offset) self._to_partition_and_offset.append(val_pos) def get(self): return self._to_partition_and_offset def __str__(self) -> str: res = '' res += '->(' for val_pos in self._to_partition_and_offset: res += f'{val_pos},' res += ')' return res def __repr__(self) -> str: return self.__str__() class Partition(object): def __init__(self) -> None: self._input_vals: List[PartitionInputVal] = [] self._output_vals: List[PartitionOutputVal] = [] def add_input_val(self, input_val: PartitionInputVal): self._input_vals.append(input_val) def add_output_val(self, output_val: PartitionOutputVal): self._output_vals.append(output_val) def get_input_vals(self): return self._input_vals def get_output_vals(self): return self._output_vals # get the output offsets sent to dst_partition_id def get_output_offsets(self, dst_partition_id): res = [] for offset, output_val in enumerate(self._output_vals): outputs = output_val.get() for val_pos in outputs: if val_pos.partition_id == dst_partition_id: res.append(offset) return res # get all input dst partition_ids def get_input_partition_ids(self): res = [] for input_val in self._input_vals: val_pos = input_val.get() if val_pos.partition_id not in res: res.append(val_pos.partition_id) return res # get all output dst partition_ids def get_output_partition_ids(self): res = [] for output_val in self._output_vals: outputs = output_val.get() for val_pos in outputs: if val_pos.partition_id not in res: res.append(val_pos.partition_id) return res def __str__(self) -> str: res = '' res += f' input:\n' res += f' length:{len(self._input_vals)}\n' for i, input_val in enumerate(self._input_vals): res += f' offset={i}:{input_val}\n' res += f' output:\n' res += f' length:{len(self._output_vals)}\n' for i, output_val in enumerate(self._output_vals): res += f' offset={i}:{output_val}\n' return res def __repr__(self) -> str: return self.__str__() # This class is a middleware between partition splitter # and Pipeline Scheduler. It records the graph info about # partition input/output and provides it to scheduler. # There are three kinds of partition in Pipeline Middleware Design # which represents the whole process of a model execution: input-fwd-output # 1. input_partition: records the input of a model. # 2. mid_partition: record the splitted forwards execution of a model. # 3. output_partition: records the output of a model. # attributes: # _partitions: include all partitions # _input_partition_id: the key represents input_partition # _output_partition_id: the key represents output_partition class Topo(object): def __init__(self, input_partition_id=None, output_partition_id=None) -> None: self._partitions: Dict[int, Partition] = {} self._input_partition_id = input_partition_id self._output_partition_id = output_partition_id def set_input_partition_id(self, partition_id: int): self._input_partition_id = partition_id def set_output_partition_id(self, partition_id: int): self._output_partition_id = partition_id def get_input_partition_id(self): return self._input_partition_id def get_output_partition_id(self): return self._output_partition_id def set_partitions(self, partition_id: int, partition: Partition): self._partitions[partition_id] = partition def get_mid_partitions(self): res = {} #{partition_id: Partition} for partition_id, partition in self._partitions.items(): if self._input_partition_id == partition_id or self._output_partition_id == partition_id: continue res[partition_id] = partition return res def get_mid_partition_ids(self): return list(self.get_mid_partitions().keys()) def get_input_partition(self): if self._input_partition_id is not None: return self._partitions[self._input_partition_id] return None def get_output_partition(self): if self._output_partition_id is not None: return self._partitions[self._output_partition_id] return None def get_partition_by_id(self, partition_id): return self._partitions[partition_id] def __str__(self) -> str: res = '' if len(self._partitions) == 0: return 'Empty Topo Graph.' input_part = self.get_input_partition() if input_part is not None: res += '{\n' res += f'InputPartition:\n partition_id={self._input_partition_id}\n{input_part}' res += '}\n' mid_parts = self.get_mid_partitions() for i, (partition_id, part) in enumerate(mid_parts.items()): res += '{\n' res += f'SubPartition_{i}:\n partition_id={partition_id}\n {part}' res += '}\n' output_part = self.get_output_partition() if output_part is not None: res += '{\n' res += f'OutputPartition:\n partition_id={self._output_partition_id}\n{output_part}' res += '}\n' return res def __repr__(self) -> str: return self.__str__()
from .fx import get_topology as get_fx_topology __all__ = ['get_fx_topology']
from torch.fx.graph_module import GraphModule from colossalai.pipeline.middleware.topo import Partition, PartitionInputVal, PartitionOutputVal, Topo import torch def partition_name_to_id(partition_name, is_input=False, is_output=False): if is_input: partition_id = 0 elif is_output: partition_id = 1 else: prefix = 'submod_' partition_id = int(partition_name.split(prefix)[-1]) + 2 return partition_id # There are two kinds of def in fx.graph # 1. non direct_use & non direct_def, which means the output is used by next partition with a temporary mid value. # e.g. submod1 = call_module(...) # temporary_val = submod1[0] # submod2 = call_module(temporary_val, ...) # 2. direct_use & direct_def, which means the output is used by next partition directly. # e.g. submod1 = call_module(...) # submod2 = call_module(submod1, ...) def find_input_in_partition(node, partitions, input_partitions=None): p_input_val = None direct_def = not node.name.startswith('getitem') # search in input if direct_def and input_partitions is not None: partition_id = partition_name_to_id('', is_input=True) for i, input_node in enumerate(input_partitions): if input_node == node: p_input_val = PartitionInputVal(partition_id=partition_id, offset=i) return p_input_val # search submod in mid part if direct_def: for partition in partitions: if partition == node: partition_id = partition_name_to_id(partition.name) p_input_val = PartitionInputVal(partition_id=partition_id, offset=0) return p_input_val # search temporary value in graph else: for partition in partitions: for offset, mid_val in enumerate(partition.users): if mid_val == node: partition_id = partition_name_to_id(partition.name) p_input_val = PartitionInputVal(partition_id=partition_id, offset=offset) return p_input_val return p_input_val def find_output_in_partition(node, partitions, output_partitions=None): p_output_val = PartitionOutputVal() for user in node.users: direct_use = not user.name.startswith('getitem') # user is mid partition for partition in partitions: # direct call if direct_use: if user == partition: partition_id = partition_name_to_id(partition.name) for i, arg in enumerate(partition.args): if arg == node: p_output_val.add(partition_id=partition_id, offset=i) break # getitem call else: if user in partition.args: partition_id = partition_name_to_id(partition.name) for i, arg in enumerate(partition.args): if arg == user: p_output_val.add(partition_id=partition_id, offset=i) break # user is output if output_partitions is not None: output_node = output_partitions[0] if user.op == output_node.op: output_keys = {} partition_id = partition_name_to_id('', is_output=True) torch.fx.graph.map_arg(output_node.args[0], lambda n: output_keys.setdefault(n)) for i, arg in enumerate(output_keys): if arg == node: p_output_val.add(partition_id=partition_id, offset=i) break return p_output_val def get_topology(gm: GraphModule): topo = Topo() topo_output_partition = Partition() input_partitions = [] partitions = [] output_partitions = [] for node in gm.graph.nodes: if node.op == 'placeholder': input_partitions.append(node) elif node.name.startswith('submod_'): partitions.append(node) elif node.op == 'output': output_partitions.append(node) else: continue # set output for input_partition topo_input_partition = Partition() for partition in input_partitions: cur_node = partition p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_input_partition.add_output_val(p_output_val) topo.set_partitions(partition_id=0, partition=topo_input_partition) topo.set_input_partition_id(partition_id=0) for i, partition in enumerate(partitions): topo_mid_partition = Partition() # set input for submodule for arg in partition.args: cur_node = arg p_input_val = find_input_in_partition(cur_node, partitions, input_partitions) topo_mid_partition.add_input_val(p_input_val) # set output for submodule direct_use = True for user in partition.users: if user.name.startswith('getitem'): direct_use = False break if direct_use: cur_node = partition p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_mid_partition.add_output_val(p_output_val) else: for user in partition.users: cur_node = user p_output_val = find_output_in_partition(cur_node, partitions, output_partitions) topo_mid_partition.add_output_val(p_output_val) topo.set_partitions(partition_id=i+2, partition=topo_mid_partition) # set input for output_partition for partition in output_partitions: topo_output_partition = Partition() torch.fx.graph.map_arg(partition.args[0], lambda n: topo_output_partition.add_input_val( find_input_in_partition(n, partitions, input_partitions))) topo.set_partitions(partition_id=1, partition=topo_output_partition) topo.set_output_partition_id(partition_id=1) return topo
from ._pipeline_schedule import FillDrainPipelineEngine, OneFOneBPipelineEngine, ChimeraPipelineEngine from .utils import pytree_map __all__ = ['FillDrainPipelineEngine', 'OneFOneBPipelineEngine', 'ChimeraPipelineEngine', 'pytree_map']
import inspect import math import threading from abc import ABC, abstractmethod from enum import Enum from functools import partial from typing import Any, Callable, Dict, List, Tuple import torch import torch.distributed.rpc as rpc from torch import autograd, nn, optim from torch._C._distributed_rpc import PyRRef from torch.futures import Future from colossalai.pipeline.middleware import Partition, PartitionInputVal, PartitionOutputVal, Topo from colossalai.pipeline.pipeline_process_group import ppg from colossalai.pipeline.rpc.utils import ( get_batch_lengths, pyobj_map, pytree_filter, pytree_map, split_batch, tensor_shape_list, type_detail, ) class Phase(Enum): FORWARD = 0 BACKWARD = 1 UPDATE = 2 INPUT = 3 class UniqueKey: __slots__ = ('microbatch_id', 'phase') microbatch_id: int phase: Phase def __init__(self, microbatch_id, phase) -> None: self.microbatch_id = microbatch_id self.phase = phase def __eq__(self, __o: object) -> bool: return (self.microbatch_id == __o.microbatch_id) and (self.phase == __o.phase) def __hash__(self) -> int: return tuple.__hash__((self.microbatch_id, self.phase)) def __repr__(self) -> str: return f'Key(microbatch_id={self.microbatch_id}, phase={self.phase})' class WorkItem: __slots__ = ('stage_id', 'phase', 'args', 'kwargs', 'output', 'refcount', 'microbatch_id', 'batch_id', 'num_microbatches', 'forward_only') stage_id: int phase: Phase args: Tuple[Any] kwargs: Dict[str, Any] output: Future microbatch_id: int refcount: int batch_id: int num_microbatches: int forward_only: bool def __init__(self, stage_id, phase, args, kwargs, output, microbatch_id, batch_id, num_microbatches, forward_only, refcount=0) -> None: for attr_name in self.__slots__: setattr(self, attr_name, locals()[attr_name]) class BackwardCache: __slots__ = ('checkpoint', 'stage_input_args', 'stage_input_kwargs', 'stage_outputs') checkpoint: bool stage_input_args: Tuple[Any] stage_input_kwargs: Dict[Any, Any] stage_outputs: Tuple[Any] def __init__(self, stage_input_args: Tuple[Any], stage_input_kwargs: Dict[Any, Any] = None, stage_outputs: Tuple[Any] = None, checkpoint: bool = False) -> None: for arg_name in self.__slots__: setattr(self, arg_name, locals()[arg_name]) class WorkerBase(ABC): def __init__(self, partition_fn: Callable, partition_args: tuple, pp_rank: int, actual_stage_num: int, num_microbatches: int, device: str, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None) -> None: super().__init__() self.pp_rank = pp_rank self.actual_stage_num = actual_stage_num self.num_microbatches = num_microbatches self.checkpoint = checkpoint if data_process_func is not None: self.data_process_func = partial(data_process_func, pp_rank) self.device = device self._initialize_outstanding_range() # variable and const for context managment self.outstanding = 0 self.forward_times = 0 self.backward_times = 0 self.reset_key = UniqueKey(0, Phase.FORWARD) # rref of other workers self.pp_rank_to_worker_rref: Dict[int, PyRRef] = None # lock for the list self._initialize_lock() # topology info self.producer_stage_ids: List[int] = None self.consumer_stage_ids: List[int] = None # module partitions self.partition_fn = partition_fn self.partition_args = partition_args self.criterion = criterion self.metric = metric self.reset = False # context to maintain loop self._initialize_context_container() # main loop self.main_loop_thread = threading.Thread(target=self._work_loop, name=f'rank_{pp_rank}', daemon=True) self.main_loop_thread.start() def _get_future_by_device(self): return torch.futures.Future(devices=None if self.device in (None, 'cpu') else [self.device]) def _initialize_outstanding_range(self): outstanding_range = None if self.pp_rank == self.actual_stage_num - 1: outstanding_range = (0, 1) else: outstanding_range = (self.actual_stage_num, self.actual_stage_num) self.outstanding_range = outstanding_range def _initialize_context_container(self): self.microbatch_id_to_backward_cache: Dict[int, BackwardCache] = dict() self.microbatch_id_to_labels: Dict[int, Any] = dict() self.work_list: Dict[UniqueKey, WorkItem] = dict() self.output_list: Dict[UniqueKey, WorkItem] = dict() def _initialize_lock(self): self.partition_condition_lock = threading.Condition(threading.Lock()) self.work_list_condition_lock = threading.Condition(threading.Lock()) self.output_list_condition_lock = threading.Condition(threading.Lock()) self.label_lock = threading.Condition(threading.Lock()) self.reset_condition = threading.Condition(threading.Lock()) def _initialize_partition(self): partition_fn = self.partition_fn partition_args = self.partition_args device = self.device with self.partition_condition_lock: self.module_partition: nn.Module = partition_fn(*partition_args).to(device) self.partition_condition_lock.notify_all() def _get_output_all(self, key: UniqueKey, ref_use=False, rank=None): with self.output_list_condition_lock: self.output_list_condition_lock.wait_for(lambda: key in self.output_list) output_work_item = self.output_list[key] output = output_work_item.output if not ref_use and output_work_item.phase != Phase.INPUT: self.output_list.pop(key) if not ref_use and output_work_item.phase != Phase.INPUT: output_work_item.refcount += 1 refcount = output_work_item.refcount # lifecycle management for DAG scheduler if output_work_item.phase == Phase.FORWARD: lifecycle = len(self.get_consumer_stage_ids()) if self.is_model_output(): # an extra reference for scheduler collecting results lifecycle += 1 elif output_work_item.phase == Phase.BACKWARD: lifecycle = len(self.get_producer_stage_ids()) if self.is_model_input() and self._is_last_step( output_work_item): # an extra reference for ensure_backward lifecycle += 1 else: lifecycle = 0 refcount = 0 with self.output_list_condition_lock: if refcount <= lifecycle: self.output_list[key] = output_work_item self.output_list_condition_lock.notify_all() if isinstance(output, Future): output = output.wait() return output def sync_global_worker_rrefs(self, pp_rank_to_worker_rref: Dict[int, PyRRef]) -> None: assert self.pp_rank_to_worker_rref is None, f"in rank {self.pp_rank}, worker has sync global workers rrefs" assert pp_rank_to_worker_rref is not None, "stage_to_workers must be a dict instead of None" self.pp_rank_to_worker_rref = pp_rank_to_worker_rref # for some schedule need the other worker's info to initialise partition (like Chimera) # construction of partition is executed after the registion of pp_rank_to_worker_rref self._initialize_partition() # res_use works for lifecycle counter, # if ref_use is True, lifecycle won't add. # offset supports get partial output to reduce comm costs. def get_output_by_key(self, key: UniqueKey, ref_use=False, rank=None, offsets=None) -> Any: output = self._get_output_all(key, ref_use, rank) if offsets is None: # get all for non iterable output return output else: # get part for iterable output output = [output[i] for i in offsets] return output def get_numels(self) -> int: numel = sum(param.numel() for param in self.module_partition.parameters()) return numel def get_parameters(self) -> List[torch.Tensor]: return [p for p in self.module_partition.parameters()] def get_parameter_gradients(self) -> List[torch.Tensor]: return [p.grad for p in self.module_partition.parameters()] def get_partition(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, 'module_partition')) return self.module_partition def get_partition_state_dict(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, 'module_partition')) return self.module_partition.state_dict() def _make_args_kwargs(self, microbatch, merge=False): if isinstance(microbatch, dict): if merge: return list(microbatch.values()), {} return [], microbatch elif isinstance(microbatch, torch.Tensor): return [microbatch], {} elif isinstance(microbatch, (tuple, list)): args = [] kwargs = {} for arg in microbatch: if isinstance(arg, dict): kwargs.update(arg) else: args.append(arg) if merge: arg_lst = args for arg in kwargs.values(): arg_lst.append(arg) return arg_lst, {} return args, kwargs else: raise TypeError(f"Input batch can be only dict, list, tuple or tensor, but receive {type(microbatch)}") # just for first pp_rank def set_input(self, microbatch_id: int, microbatch: Tuple[Any], forward_only: bool): key = UniqueKey(microbatch_id, Phase.FORWARD) output = self._get_future_by_device() if not self.use_middleware(): # make args and kwargs args, kwargs = self._make_args_kwargs(microbatch) work_item = WorkItem(self.pp_rank, Phase.FORWARD, args, kwargs, output, microbatch_id, None, self.num_microbatches, forward_only) with self.work_list_condition_lock: self.work_list[key] = work_item self.work_list_condition_lock.notify_all() else: # make args and kwargs arg_lst, _ = self._make_args_kwargs(microbatch, merge=True) # first stage assign correct input into other stages topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) input_partition = topo.get_input_partition() self_input_offsets = input_partition.get_output_offsets(self_partition_id) recv_input_key = UniqueKey(microbatch_id, Phase.INPUT) # set input for self rank self_arg_lst = [] for off in self_input_offsets: self_arg_lst.append(arg_lst[off]) work_item = WorkItem(self.pp_rank, Phase.FORWARD, self_arg_lst, {}, output, microbatch_id, None, self.num_microbatches, forward_only) with self.work_list_condition_lock: self.work_list[key] = work_item self.work_list_condition_lock.notify_all() # put input tensor which other nodes need into output_list as Phase.INPUT work_item_remote = WorkItem(self.pp_rank, Phase.INPUT, [], {}, arg_lst, microbatch_id, None, self.num_microbatches, forward_only) with self.output_list_condition_lock: self.output_list[recv_input_key] = work_item_remote self.output_list_condition_lock.notify_all() # just for last pp_rank def set_labels(self, microbatch_id: int, microlabels: Any): with self.label_lock: self.microbatch_id_to_labels[microbatch_id] = microlabels self.label_lock.notify_all() # just for last pp_rank def _begin_backward(self, microbatch_id: int): with self.work_list_condition_lock: assert self.producer_stage_ids is not None key = UniqueKey(microbatch_id, Phase.BACKWARD) output = self._get_future_by_device() grad_wrt_loss = None work_item = WorkItem(self.pp_rank, Phase.BACKWARD, grad_wrt_loss, {}, output, microbatch_id, None, self.num_microbatches, False) self.work_list[key] = work_item self.work_list_condition_lock.notify_all() def _subscribe_producer(self, microbatch_id: int, forward_only: bool): """ You should call this function asynchronously """ stage_id = self.pp_rank output = self._get_future_by_device() if not self.use_middleware(): producer_num = len(self.producer_stage_ids) subscribe_forward_futures: List[Future] = [None] * producer_num for i in range(producer_num): producer_stage_id = self.producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] subscribe_forward_futures[i] = producer_worker_rref.rpc_async().get_output_by_key(producer_output_key) else: producer_stage_ids = self.get_producer_stage_ids() producer_num = len(producer_stage_ids) if self.need_model_input(): producer_num += 1 # for input partition subscribe_forward_futures: List[Future] = [None] * producer_num # TODO(jiangziyue) get single value instead of the whole output if self.need_model_input(): producer_stage_id = 0 producer_output_key = UniqueKey(microbatch_id, Phase.INPUT) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] offsets = self._get_input_offsets_by_index(target_index=0) subscribe_forward_futures[0] = producer_worker_rref.rpc_async().get_output_by_key(producer_output_key, rank=self.pp_rank, offsets=offsets) for i in range(0, producer_num - 1): producer_stage_id = producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] target_index = i + 1 offsets = self._get_input_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_forward_futures[target_index] = [] else: subscribe_forward_futures[target_index] = producer_worker_rref.rpc_async().get_output_by_key( producer_output_key, rank=self.pp_rank, offsets=offsets) else: for i in range(producer_num): producer_stage_id = producer_stage_ids[i] producer_output_key = UniqueKey(microbatch_id, Phase.FORWARD) producer_worker_rref = self.pp_rank_to_worker_rref[producer_stage_id] target_index = i offsets = self._get_input_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_forward_futures[target_index] = [] else: subscribe_forward_futures[target_index] = producer_worker_rref.rpc_async().get_output_by_key( producer_output_key, rank=self.pp_rank, offsets=offsets) work_item_from_producer = WorkItem(stage_id, Phase.FORWARD, subscribe_forward_futures, {}, output, microbatch_id, None, self.num_microbatches, forward_only) return work_item_from_producer # TODO(jiangziyue) Profile the side effect of the lock for lifecycle protection and consider a better one. def subscribe_producer(self, microbatch_id: int, forward_only: bool): key = UniqueKey(microbatch_id, Phase.FORWARD) with self.work_list_condition_lock: if key not in self.work_list: # On current PP middleware design for DAG, get_output_by_key used by _subscribe_producer # can only be executed once for every producer-consumer stage pair, which is necessary # to count the lifecycle of work_item. So, keeping the _subscribe_producer in the same # lock of work_item queue operation gurantees the consistency of lifecycle counter. work_item_from_producer = self._subscribe_producer(microbatch_id, forward_only) self.work_list[key] = work_item_from_producer self.work_list_condition_lock.notify_all() def _subscribe_consumer(self, microbatch_id: int): """ You should call this function asynchronously """ stage_id = self.pp_rank output = self._get_future_by_device() if not self.use_middleware(): consumer_stage_ids = self.consumer_stage_ids else: consumer_stage_ids = self.get_consumer_stage_ids() consumer_num = len(consumer_stage_ids) subscribe_backward_futures: List[Future] = [None] * consumer_num for i in range(consumer_num): consumer_stage_id = consumer_stage_ids[i] consumer_output_key = UniqueKey(microbatch_id, Phase.BACKWARD) consumer_worker_rref = self.pp_rank_to_worker_rref[consumer_stage_id] target_index = i offsets = self._get_output_offsets_by_index(target_index=target_index) if offsets is not None and len(offsets) == 0: # no need to do rpc subscribe_backward_futures[target_index] = [] else: subscribe_backward_futures[target_index] = consumer_worker_rref.rpc_async().get_output_by_key( consumer_output_key, rank=self.pp_rank, offsets=offsets) # flatten args work_item_from_consumer = WorkItem(stage_id, Phase.BACKWARD, subscribe_backward_futures, {}, output, microbatch_id, None, self.num_microbatches, False) return work_item_from_consumer def subscribe_consumer(self, microbatch_id: int): key = UniqueKey(microbatch_id, Phase.BACKWARD) with self.work_list_condition_lock: if key not in self.work_list: # On current PP middleware design for DAG, get_output_by_key used by subscribe_consumer # can only be executed once for every producer-consumer stage pair, which is necessary # to count the lifecycle of work_item. So, keeping the subscribe_consumer in the same # lock of work_item queue operation gurantees the consistency of lifecycle counter. work_item_from_consumer = self._subscribe_consumer(microbatch_id) self.work_list[key] = work_item_from_consumer self.work_list_condition_lock.notify_all() def get_producer_stage_ids(self): producer_stage_ids = [] rank = self.pp_rank if not self.use_middleware(): prev_rank = rank - 1 if prev_rank >= 0: producer_stage_ids.append(prev_rank) else: topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) input_partition_ids = self_partition.get_input_partition_ids() model_input_partition_id = topo.get_input_partition_id() for partition_id in input_partition_ids: # ignore input partition in current implementation. # it will be specially tackled. if partition_id != model_input_partition_id: producer_stage_ids.append(self.partition_id_to_pp_rank(partition_id, topo)) return producer_stage_ids def get_consumer_stage_ids(self): consumer_stage_ids = [] rank = self.pp_rank if not self.use_middleware(): next_rank = rank + 1 if next_rank <= self.actual_stage_num - 1: consumer_stage_ids.append(next_rank) else: topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_partition_ids = self_partition.get_output_partition_ids() model_output_partition_id = topo.get_output_partition_id() for partition_id in output_partition_ids: if model_output_partition_id != partition_id: consumer_stage_ids.append(self.partition_id_to_pp_rank(partition_id, topo)) return consumer_stage_ids def _get_producer_consumer(self) -> None: rank = self.pp_rank assert self.producer_stage_ids is None, f"all the producers of rank {rank} has been subscribed" assert self.consumer_stage_ids is None, f"all the consumers of rank {rank} has been subscribed" # should be aranged in order, the order of the input of current forward self.producer_stage_ids = self.get_producer_stage_ids() self.consumer_stage_ids = self.get_consumer_stage_ids() def pp_rank_to_partition_id(self, pp_rank: int, topo: Topo): partition_ids = topo.get_mid_partition_ids() return partition_ids[pp_rank] def partition_id_to_pp_rank(self, partition_id: int, topo: Topo): partition_ids = topo.get_mid_partition_ids() for i, id in enumerate(partition_ids): if id == partition_id: return i def get_topo(self): with self.partition_condition_lock: self.partition_condition_lock.wait_for(lambda: hasattr(self, 'module_partition')) if hasattr(self.module_partition, '_topo'): return self.module_partition._topo else: return None def use_middleware(self): topo = self.get_topo() return topo is not None def _get_input_offsets_by_index(self, target_index): res = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) model_input_partition_id = topo.get_input_partition_id() input_vals = self_partition.get_input_vals() producer_stage_ids = self.get_producer_stage_ids() if self.need_model_input(): # 0 for data from input batch # >= 1 for data from prev stages base = 1 else: # data from prev stages base = 0 for val in input_vals: val_pos = val.get() src_partition_id = val_pos.partition_id src_offset = val_pos.offset src_index = base src_partition = topo.get_partition_by_id(src_partition_id) output_len = len(src_partition.get_output_vals()) # data from not-input partition if src_partition_id != model_input_partition_id: src_stage_id = self.partition_id_to_pp_rank(src_partition_id, topo) src_index = base for i, stage_id in enumerate(producer_stage_ids): if stage_id == src_stage_id: src_index += i break else: # data from input partition src_index = 0 # when output_len = 1, not iterable if target_index == src_index: if output_len == 1: res = None # offset = None to get all outputs return res else: res.append(src_offset) return res def _get_output_offsets_by_index(self, target_index): res = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_vals = self_partition.get_output_vals() consumer_stage_ids = self.get_consumer_stage_ids() for val_list in output_vals: # An output may be passed to many down stages. target = None for val_pos in val_list.get(): dst_partition_id = val_pos.partition_id dst_offset = val_pos.offset dst_partition = topo.get_partition_by_id(dst_partition_id) input_len = len(dst_partition.get_input_vals()) dst_stage_id = self.partition_id_to_pp_rank(dst_partition_id, topo) for i, stage_id in enumerate(consumer_stage_ids): if stage_id == dst_stage_id: dst_index = i break if target_index == dst_index: if input_len == 1: res = None # offset = None to get all outputs return res else: res.append(dst_offset) return res # TODO(jiangziyue) get single value instead of the whole output def _get_real_args_kwargs_fwd(self, args_or_kwargs): if not self.use_middleware(): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args else: args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] if self.is_first_stage(): pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) else: # get by offset topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) model_input_partition_id = topo.get_input_partition_id() input_vals = self_partition.get_input_vals() producer_stage_ids = self.get_producer_stage_ids() if self.need_model_input(): # 0 for data from input batch # >= 1 for data from prev stages base = 1 else: # data from prev stages base = 0 for val in input_vals: val_pos = val.get() src_partition_id = val_pos.partition_id src_offset = val_pos.offset src_index = base src_partition = topo.get_partition_by_id(src_partition_id) output_len = len(src_partition.get_output_vals()) # data from not-input partition if src_partition_id != model_input_partition_id: src_stage_id = self.partition_id_to_pp_rank(src_partition_id, topo) src_index = base for i, stage_id in enumerate(producer_stage_ids): if stage_id == src_stage_id: src_index += i break else: # data from input partition src_index = 0 # when output_len = 1, not iterable if output_len == 1: target = args_or_kwargs[src_index] else: offsets = self._get_input_offsets_by_index(src_index) real_offset = offsets.index(src_offset) target = args_or_kwargs[src_index][real_offset] flatten_args.append(target) args_or_kwargs = flatten_args return args_or_kwargs # TODO(jiangziyue) get single value instead of the whole output def _get_real_args_kwargs_bwd(self, args_or_kwargs): if not self.use_middleware(): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args else: for i, arg in enumerate(args_or_kwargs): args_or_kwargs[i] = arg.wait() if args_or_kwargs is not None: # get by offset flatten_args = [] topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition: Partition = topo.get_partition_by_id(self_partition_id) output_vals = self_partition.get_output_vals() consumer_stage_ids = self.get_consumer_stage_ids() for val_list in output_vals: # An output may be passed to many down stages. target = None for val_pos in val_list.get(): dst_partition_id = val_pos.partition_id dst_offset = val_pos.offset dst_partition = topo.get_partition_by_id(dst_partition_id) input_len = len(dst_partition.get_input_vals()) dst_stage_id = self.partition_id_to_pp_rank(dst_partition_id, topo) for i, stage_id in enumerate(consumer_stage_ids): if stage_id == dst_stage_id: dst_index = i break if input_len == 1: part_grad = args_or_kwargs[dst_index] else: offsets = self._get_output_offsets_by_index(dst_index) real_offsets = offsets.index(dst_offset) part_grad = args_or_kwargs[dst_index][real_offsets] if target is None: target = part_grad elif part_grad is not None: target += part_grad else: continue flatten_args.append(target) args_or_kwargs = flatten_args return args_or_kwargs @abstractmethod def _get_work_item_key(self) -> UniqueKey: """ this method control the order of the microbatch to consume """ def is_first_stage(self): return self.pp_rank == 0 def is_last_stage(self): return self.pp_rank == self.actual_stage_num - 1 def need_model_input(self): need_input = False topo: Topo = self.get_topo() self_partition_id = self.pp_rank_to_partition_id(self.pp_rank, topo) self_partition = topo.get_partition_by_id(self_partition_id) partition_inputs = self_partition.get_input_partition_ids() model_input_partition_id = topo.get_input_partition_id() if model_input_partition_id in partition_inputs: need_input = True return not self.is_first_stage() and need_input def is_model_output(self): return self.is_last_stage() def is_model_input(self): return self.is_first_stage() def _default_data_process_func(self, args_kwargs): if self.is_first_stage(): args = args_kwargs[0] kwargs = args_kwargs[1] else: args = args_kwargs kwargs = {} return args, kwargs def _consume_work_item_by_phase(self, work_item: WorkItem): phase = work_item.phase args = work_item.args kwargs = work_item.kwargs microbatch_id = work_item.microbatch_id forward_only = work_item.forward_only data_process_func = getattr(self, 'data_process_func', self._default_data_process_func) consume_result = None is_first_stage = self.is_first_stage() is_last_stage = self.is_last_stage() if phase == Phase.FORWARD: # remind its consumer to get data before forward if not is_last_stage: for stage_id in self.consumer_stage_ids: consumer_worker_rref = self.pp_rank_to_worker_rref[stage_id] consumer_worker_rref.remote().subscribe_producer(microbatch_id, forward_only) # sustain pipeline context self.forward_times += 1 if not forward_only: self.outstanding += 1 # parse and integrate args and kwargs if is_first_stage: args = self._get_real_args_kwargs_fwd(args) kwargs = self._get_real_args_kwargs_fwd(kwargs) args_kwargs = (args, kwargs) else: args_kwargs = self._get_real_args_kwargs_fwd(args) args_kwargs = pyobj_map(args_kwargs, fn=lambda x: x.to(self.device).detach(), process_types=torch.Tensor) # torch rpc doesn't support args or rets in GPU args_kwargs = pyobj_map(args_kwargs, fn=lambda x: self.device, process_types=torch.device) # change devices from last stage to current device args, kwargs = data_process_func(args_kwargs) stage_outputs = None stage_input_args = args stage_input_kwargs = kwargs use_checkpoint = None if forward_only: with torch.no_grad(): consume_result = self.module_partition(*args, **kwargs) if is_last_stage and self.criterion: with self.label_lock: self.label_lock.wait_for(lambda: microbatch_id in self.microbatch_id_to_labels) labels = self.microbatch_id_to_labels.pop(microbatch_id) loss: torch.Tensor = self.criterion(consume_result, labels) if self.metric is not None: metric_result = self.metric(consume_result, labels) if isinstance(metric_result, torch.Tensor): metric_result = metric_result.item() else: metric_result = None consume_result = [loss.item(), metric_result] # last stage doesn't need to do checkpoint, for it will do backward instantly stage_input_args = None stage_input_kwargs = None stage_outputs = consume_result elif self.checkpoint and not is_last_stage: with torch.no_grad(): consume_result = self.module_partition(*args, **kwargs) stage_outputs = consume_result use_checkpoint = True else: consume_result = self.module_partition(*args, **kwargs) if is_last_stage and self.criterion: with self.label_lock: self.label_lock.wait_for(lambda: microbatch_id in self.microbatch_id_to_labels) labels = self.microbatch_id_to_labels.pop(microbatch_id) loss: torch.Tensor = self.criterion(consume_result, labels) if self.metric is not None: metric_result = self.metric(consume_result, labels) if isinstance(metric_result, torch.Tensor): metric_result = metric_result.item() else: metric_result = None consume_result = [loss.item(), metric_result] else: loss = consume_result stage_outputs = loss use_checkpoint = False if not forward_only: self.microbatch_id_to_backward_cache[microbatch_id] = BackwardCache(stage_input_args, stage_input_kwargs, stage_outputs, checkpoint=use_checkpoint) consume_result = pyobj_map(consume_result, fn=lambda x: x.to('cpu'), process_types=torch.Tensor) # torch rpc doesn't support args or rets in # if not forward_only, do the backward if not forward_only: if is_last_stage: # if it is the last stage, trigger backward automatic self._begin_backward(microbatch_id) elif phase == Phase.BACKWARD: # remind its producer to get data before backward if not is_first_stage: for stage_id in self.producer_stage_ids: producer_worker_rref = self.pp_rank_to_worker_rref[stage_id] producer_worker_rref.remote().subscribe_consumer(microbatch_id) self.backward_times += 1 self.outstanding -= 1 assert microbatch_id in self.microbatch_id_to_backward_cache, f"microbatch_id {microbatch_id} not in backward cache" backward_cache = self.microbatch_id_to_backward_cache.pop(microbatch_id) stage_outputs = backward_cache.stage_outputs stage_input_args = backward_cache.stage_input_args stage_input_kwargs = backward_cache.stage_input_kwargs use_checkpoint = backward_cache.checkpoint if use_checkpoint: stage_outputs = [self.module_partition(*stage_input_args, **stage_input_kwargs)] # overlap recompute and future.wait if not is_last_stage: grad_tensors = self._get_real_args_kwargs_bwd(args) else: grad_tensors = None # take tensor only (for only tensor can do backward) # TODO(jiangziyue) : All values which should do bp are torch.Tensor? stage_outputs = pytree_filter(lambda x: True, stage_outputs, process_types=torch.Tensor) grad_tensors = pytree_filter(lambda x: True, grad_tensors, process_types=torch.Tensor) # output all input's grad to producer, even it has no grad(output None) # to make the offset aligned to the topo's record. if grad_tensors is not None: filtered_outputs = [] filtered_grads = [] for i, grad in enumerate(grad_tensors): stage_output = stage_outputs[i] if stage_output.requires_grad and grad is not None: filtered_outputs.append(stage_output) filtered_grads.append(grad) stage_outputs = filtered_outputs grad_tensors = pyobj_map(filtered_grads, fn=lambda x: x.to(self.device), process_types=torch.Tensor) # torch rpc doesn't support args or rets in GPU autograd.backward(stage_outputs, grad_tensors=grad_tensors) # collect grad of input tensor consume_result = [] if not is_first_stage: # In current design, input mush be a flatten args. for arg in stage_input_args: if isinstance(arg, torch.Tensor): consume_result.append(arg.grad) else: consume_result.append(None) consume_result = pyobj_map( consume_result, fn=lambda x: x.to('cpu'), process_types=torch.Tensor) # torch rpc doesn't support args or rets in GPU else: raise TypeError(f"Unknown phase appears in _consume_work_item_by_phase {phase}") return consume_result def _get_store_len(self): return f'work_list:{len(self.work_list)} output_list:{len(self.output_list)} backward_cache:{len(self.microbatch_id_to_backward_cache)} label_cache:{len(self.microbatch_id_to_labels)}' def _get_parameter_grad_sum(self): grad_sum = 0 for p in self.module_partition.parameters(): if p.grad is not None: grad_sum += p.grad.sum() return grad_sum def _is_first_step(self, work_item: WorkItem) -> bool: return work_item.phase == Phase.FORWARD and work_item.microbatch_id == 0 def _is_last_step(self, work_item: WorkItem) -> bool: if work_item.forward_only: last_phase = Phase.FORWARD else: last_phase = Phase.BACKWARD is_last_phase = work_item.phase == last_phase is_last_microbatch = work_item.microbatch_id == self.num_microbatches - 1 return is_last_phase and is_last_microbatch def _hook_before_step(self): pass # install the main loop to wait for next batch input def _wait_for_reset(self): with self.reset_condition: self.reset_condition.wait_for(lambda: self.reset) self.reset = False # do the main loop to consume ready_list def _work_loop(self): # for init self._get_producer_consumer() torch.cuda.set_device(ppg.get_local_pp_rank()) # main loop while True: work_item_key = self._get_work_item_key() # move current work item to output_list to activate subscribe in advance with self.work_list_condition_lock: self.work_list_condition_lock.wait_for(lambda: work_item_key in self.work_list) work_item = self.work_list[work_item_key] with self.output_list_condition_lock: # assert work_item_key not in self.output_list self.output_list[work_item_key] = work_item self.output_list_condition_lock.notify_all() consume_result = self._consume_work_item_by_phase(work_item) with self.work_list_condition_lock: self.work_list.pop(work_item_key) work_item.output.set_result(consume_result) # if is last step in one batch reset context and do step if self._is_last_step(work_item): self._wait_for_reset() # reset context and resume loop def reset_context(self): self.forward_times = 0 self.backward_times = 0 self.outstanding = 0 self._initialize_outstanding_range() with self.work_list_condition_lock: self.work_list.clear() with self.output_list_condition_lock: self.output_list.clear() with self.reset_condition: self.reset = True self.reset_condition.notify_all() def initialize_optimizer(self, optimizer_class: type, **kwargs): self.optimizer: optim.Optimizer = optimizer_class(self.module_partition.parameters(), **kwargs) def step(self): self._hook_before_step() self.optimizer.step() self.optimizer.zero_grad() class PipelineEngineBase(ABC, nn.Module): def __init__(self, worker_type, partition_fn: Callable, stage_num, num_microbatches, device: str, use_1F1B=False, chunk: int = 1, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None) -> None: super().__init__() self.worker_type = worker_type self.partition_fn: Callable = partition_fn self.chunk = chunk self.criterion = criterion self.metric = metric self.num_microbatches = num_microbatches self.device = device self.use_1F1B = use_1F1B self.stage_num = stage_num self.checkpoint = checkpoint self.data_process_func = data_process_func self.pp_rank_to_worker_rref: Dict[int, PyRRef] = dict() self._check_argument() self._create_pp_rank_to_rpc_worker_id() self._create_pp_rank_to_module_partition_id() self._init_worker() def _check_argument(self) -> None: # make virtual stage num self.virtual_stage_num = self.stage_num * self.chunk assert self.stage_num <= torch.cuda.device_count(), "stage_num must be smaller than device count!" # check data_process_func data_process_func = self.data_process_func if data_process_func is not None: assert callable(data_process_func), "data_process_func must be a function" assert '<locals>' not in data_process_func.__repr__(), "data_process_func must be a global function" assert '<lambda>' not in data_process_func.__repr__(), "data_process_func cannot be a lambda expression" sig = inspect.signature(data_process_func) assert len( sig.parameters ) == 2, f"length of data_process_func' arguments must be 2, receive {len(sig.parameters)} arguments instead" def _get_actual_stage_num(self) -> int: return self.stage_num if self.chunk == 1 else self.virtual_stage_num def _create_pp_rank_to_rpc_worker_id(self) -> None: """create a map from model partition to stage_id, which is useful when use_interleave is True. e.g. If a model is splited into 4 parts, which means stage_num is 2, chunk is 2, then pp_rank_to_rpc_worker_id = [0, 1, 0, 1], that means first and third part of partitions will be moved to device 0 and the others to device 1 """ stage_num = self.stage_num actual_stage_num = self._get_actual_stage_num() self.pp_rank_to_rpc_worker_id = [0] * actual_stage_num for pp_rank in range(actual_stage_num): self.pp_rank_to_rpc_worker_id[pp_rank] = pp_rank % stage_num def _create_pp_rank_to_module_partition_id(self) -> None: """By default(both fill drain and 1F1B), length of model partitions equal to actual_stage_num, so allocate model partition to corresponding stage """ actual_stage_num = self._get_actual_stage_num() self.pp_rank_to_module_partition_id = [0] * actual_stage_num for pp_rank in range(actual_stage_num): self.pp_rank_to_module_partition_id[pp_rank] = pp_rank def _init_worker(self) -> None: actual_stage_num = self._get_actual_stage_num() worker_type = self.worker_type checkpoint = self.checkpoint num_microbatches = self.num_microbatches device = self.device criterion = self.criterion metric = self.metric partition_fn = self.partition_fn chunk = self.chunk data_process_func = self.data_process_func for pp_rank in range(len(self.pp_rank_to_rpc_worker_id)): partition_id = self.pp_rank_to_module_partition_id[pp_rank] partition_args = (partition_id, chunk, actual_stage_num) rpc_worker_id = self.pp_rank_to_rpc_worker_id[pp_rank] if device[:4] == 'cuda': device = f'cuda:{rpc_worker_id}' self.pp_rank_to_worker_rref[pp_rank] = rpc.remote(rpc_worker_id, worker_type, args=(partition_fn, partition_args, pp_rank, actual_stage_num, num_microbatches, device, criterion, metric, checkpoint, data_process_func)) # let each worker know global worker rref (include itself) sync_futs = [] for pp_rank in self.pp_rank_to_worker_rref: fut = self.pp_rank_to_worker_rref[pp_rank].rpc_async().sync_global_worker_rrefs(self.pp_rank_to_worker_rref) sync_futs.append(fut) for fut in sync_futs: fut.wait() def remote_numels(self) -> Dict[int, int]: numels = {} actual_stage_num = self._get_actual_stage_num() for stage_id in range(actual_stage_num): worker_rref = self.pp_rank_to_worker_rref[stage_id] numel = worker_rref.rpc_sync().get_numels() numels[stage_id] = numel return numels def remote_parameters(self) -> Dict[int, List[torch.Tensor]]: parameters = {} actual_stage_num = self._get_actual_stage_num() for stage_id in range(actual_stage_num): parameters[stage_id] = [] worker_rref = self.pp_rank_to_worker_rref[stage_id] for p in worker_rref.rpc_sync().get_parameters(): parameters[stage_id].append(p) return parameters def remote_grad(self) -> Dict[int, List[torch.Tensor]]: grads = {} actual_stage_num = self._get_actual_stage_num() for stage_id in range(actual_stage_num): grads[stage_id] = [] worker_rref = self.pp_rank_to_worker_rref[stage_id] for grad in worker_rref.rpc_sync().get_parameter_gradients(): grads[stage_id].append(grad) return grads def get_input_pp_ranks(self) -> List[int]: return [0] def get_output_pp_ranks(self) -> List[int]: return [self._get_actual_stage_num() - 1] def _consume_constraint(self, microbatch_id: int, forward_only: bool, input_pp_ranks: List[int], output_pp_ranks: List[int], ret_future): actual_stage_num = self._get_actual_stage_num() use_1F1B = self.use_1F1B if microbatch_id >= actual_stage_num: if forward_only or not use_1F1B: for pp_rank in output_pp_ranks: ret_future[pp_rank][microbatch_id - actual_stage_num].wait() else: key = UniqueKey(microbatch_id - actual_stage_num, Phase.BACKWARD) futs = [] for pp_rank in input_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank] fut = worker_rref.rpc_async().get_output_by_key(key, ref_use=True, offsets=[]) futs.append(fut) for fut in futs: fut.wait() def _create_ret_future(self, output_pp_ranks: List[int]) -> Dict[int, List[Future]]: num_microbatches = self.num_microbatches return {pp_rank: [None] * num_microbatches for pp_rank in output_pp_ranks} def _set_input(self, input_pp_ranks: List[int], microbatch_id: int, microbatch, forward_only: bool): for pp_rank in input_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank] # TODO : add relationship between input_pp_ranks and parts of microbatch worker_rref.remote().set_input(microbatch_id, microbatch, forward_only) def _set_labels(self, output_pp_ranks: List[int], microbatch_id: int, microlabels): for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank] # TODO : add relationship between output_pp_ranks and parts of microlabels worker_rref.remote().set_labels(microbatch_id, microlabels) # TODO(jiangziyue) : get model output with single value, instead of merging into last stage. def _subscribe_forward(self, microbatch_id: int, output_pp_ranks: List[int], ret_future: Dict[int, List[Future]]): key = UniqueKey(microbatch_id, Phase.FORWARD) for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank] ret_future[pp_rank][microbatch_id] = worker_rref.rpc_async().get_output_by_key(key) def _ensure_backward(self, forward_only: bool, input_pp_ranks: List[int]): if not forward_only: backward_result = [] for pp_rank in input_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank] key = UniqueKey(self.num_microbatches - 1, Phase.BACKWARD) fut = worker_rref.rpc_async().get_output_by_key( key, offsets=[]) # only ensure the res exists, no need for real data. backward_result.append(fut) for fut in backward_result: fut.wait() def _collect_forward_result(self, output_pp_ranks: List[int], ret_future: Dict[int, List[Future]]): forward_result = [] for pp_rank in output_pp_ranks: worker_forward_result = [None] * self.num_microbatches for microbatch_id in range(self.num_microbatches): ret = ret_future[pp_rank][microbatch_id].wait() # TODO : more stable format ret = [ret] if isinstance(ret, torch.Tensor) else ret worker_forward_result[microbatch_id] = ret worker_forward_result = list(zip(*worker_forward_result)) forward_result.extend(worker_forward_result) return forward_result def _reset_worker(self): actual_stage_num = self._get_actual_stage_num() reset_futs: List[Future] = [] for pp_rank in range(actual_stage_num): worker_rref = self.pp_rank_to_worker_rref[pp_rank] fut = worker_rref.rpc_async().reset_context() reset_futs.append(fut) for fut in reset_futs: fut.wait() def forward_backward(self, batch: torch.Tensor, labels: torch.Tensor = None, forward_only: bool = False): batch_lengths = get_batch_lengths(batch) batch_length = batch_lengths[0] if labels is not None and not forward_only: assert hasattr( self, 'optimizer_class'), "call `initialize_optimizer` to initialize optimizer before forward_backward" num_microbatches = self.num_microbatches assert batch_length >= num_microbatches, "num_microbatches is greater than the size of a batch, which is illegal" microbatch_size = math.ceil(batch_length / num_microbatches) device = self.device # If Chimera mode is used, then rank of down pipeline is excluded from 'input_pp_ranks' or 'output_pp_ranks' input_pp_ranks = self.get_input_pp_ranks() output_pp_ranks = self.get_output_pp_ranks() # a cache to collect data and control flow ret_future = self._create_ret_future(output_pp_ranks) for microbatch_id in range(num_microbatches): # control data input speed # to prevent exceed of wait limitations # self._consume_constraint(microbatch_id, forward_only, input_pp_ranks, output_pp_ranks, ret_future) batch_start = microbatch_size * microbatch_id batch_end = min(batch_start + microbatch_size, batch_length) # set input microbatch = split_batch(batch, batch_start, batch_end, device) self._set_input(input_pp_ranks, microbatch_id, microbatch, forward_only) # set labels if labels is not None: # microlabels = labels[microbatch_size * microbatch_id:microbatch_size * (microbatch_id + 1)] microlabels = split_batch(labels, batch_start, batch_end, device) self._set_labels(output_pp_ranks, microbatch_id, microlabels) # get data asynchronously self._subscribe_forward(microbatch_id, output_pp_ranks, ret_future) # wait for first rank to ensure all backwards are done self._ensure_backward(forward_only, input_pp_ranks) # collect forward result forward_result = self._collect_forward_result(output_pp_ranks, ret_future) if not forward_only and hasattr(self, 'optimizer_class'): self.step() self._reset_worker() # reset worker attributes for next batch return forward_result def initialize_optimizer(self, optimizer_class: type, **kwargs): self.optimizer_class = optimizer_class for pp_rank in self.pp_rank_to_worker_rref: worker_rref = self.pp_rank_to_worker_rref[pp_rank] worker_rref.remote().initialize_optimizer(optimizer_class, **kwargs) def step(self): actual_stage_num = self._get_actual_stage_num() step_futs: List[Future] = [] for pp_rank in range(actual_stage_num): worker_rref = self.pp_rank_to_worker_rref[pp_rank] fut = worker_rref.rpc_async().step() step_futs.append(fut) for fut in step_futs: fut.wait()
import argparse import os import warnings from typing import Any, Callable, Dict, List, Tuple, Type, Union import torch import torch.distributed.rpc as rpc import torch.multiprocessing as mp from torch._C._distributed_rpc import _is_current_rpc_agent_set from torch.futures import Future from colossalai.initialize import launch from colossalai.pipeline.pipeline_process_group import ppg def pyobj_map(obj: Any, fn: Callable, process_types: Union[Type, Tuple[Type]] = ()) -> Any: if isinstance(obj, process_types): return fn(obj) elif type(obj) is dict: return {k: pyobj_map(obj[k], fn, process_types) for k in obj} elif type(obj) is tuple: return tuple(pyobj_map(o, fn, process_types) for o in obj) elif type(obj) is list: return list(pyobj_map(o, fn, process_types) for o in obj) else: return obj def pytree_map(obj: Any, fn: Callable, process_types: Union[Type, Tuple[Type]] = (), map_all: bool = False) -> Any: """process object recursively, like pytree Args: obj (:class:`Any`): object to process fn (:class:`Callable`): a function to process subobject in obj process_types (:class: `type | tuple[type]`): types to determine the type to process map_all (:class: `bool`): if map_all is True, then any type of element will use fn Returns: :class:`Any`: returns have the same structure of `obj` and type in process_types after map of `fn` """ if isinstance(obj, dict): return {k: pytree_map(obj[k], fn, process_types, map_all) for k in obj} elif isinstance(obj, tuple): return tuple(pytree_map(o, fn, process_types, map_all) for o in obj) elif isinstance(obj, list): return list(pytree_map(o, fn, process_types, map_all) for o in obj) elif isinstance(obj, process_types): return fn(obj) else: return fn(obj) if map_all else obj def tensor_shape_list(obj): return pytree_map(obj, fn=lambda x: x.shape, process_types=torch.Tensor) def get_batch_lengths(batch): lengths = [] pytree_map(batch, fn=lambda x: lengths.append(len(x)), process_types=torch.Tensor) return lengths def split_batch(batch: Any, start, stop, device: str): if device == 'cuda': fn = lambda x: x[start:stop].cuda() else: fn = lambda x: x[start:stop] return pytree_map(batch, fn=fn, process_types=torch.Tensor) def type_detail(obj): return pytree_map(obj, lambda x: type(x), map_all=True) def pytree_filter(fn, obj, process_types): if obj is None: return None filters = [] def condition_append(obj): if fn(obj): filters.append(obj) pytree_map(obj, fn=condition_append, process_types=process_types) return filters def get_real_args_kwargs(args_or_kwargs): args_or_kwargs = pytree_map(args_or_kwargs, fn=lambda x: x.wait(), process_types=Future) # TODO : combine producer and consumer # by default, merge all args in the output args or kwargs if args_or_kwargs is not None: if isinstance(args_or_kwargs, dict): pass else: flatten_args = [] pytree_map(args_or_kwargs, fn=lambda x: flatten_args.append(x), map_all=True) args_or_kwargs = flatten_args return args_or_kwargs def run_worker(rank, args, master_func): os.environ['MASTER_ADDR'] = args.master_addr os.environ['MASTER_PORT'] = args.master_port device = args.device world_size = args.world_size dp_degree = args.dp_degree tp_degree = args.tp_degree num_worker_threads = args.num_worker_threads host = args.master_addr port = args.master_port backend = 'nccl' if device == 'cuda' else 'gloo' launch(dict(), rank, world_size, host, int(port), backend, verbose=False) ppg.set_global_info(rank=rank, world_size=world_size, dp_degree=dp_degree, tp_degree=tp_degree, num_worker_threads=num_worker_threads, device=device) ppg.args = args # in rpc mode, only rank 0 is needed to be coded if rank == 0: master_func(args) # barrier here if _is_current_rpc_agent_set(): rpc.shutdown() else: warnings.warn("RPC has not been initialized") def rpc_run(args, master_func): world_size = args.world_size mp.spawn(run_worker, args=(args, master_func), nprocs=world_size) def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--epoch', type=int, default=1) parser.add_argument('--world_size', type=int, default=2) parser.add_argument('--batch_size', type=int, default=16) parser.add_argument('--dp_degree', type=int, default=1) parser.add_argument('--tp_degree', type=int, default=1) parser.add_argument('--num_microbatches', type=int, default=2) parser.add_argument('--chunk', type=int, default=1) parser.add_argument('--use_checkpoint', action='store_true') parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'RMSprop'], default='SGD') parser.add_argument('--device', type=str, choices=['cpu', 'cuda'], default='cuda') parser.add_argument('--master_addr', type=str, default='localhost') parser.add_argument('--master_port', type=str, default='29020') parser.add_argument('--num_worker_threads', type=int, default=128) return parser.parse_args()
import threading from typing import Callable, Dict, List import torch import torch.distributed as dist from torch._C._distributed_rpc import PyRRef from torch.futures import Future from colossalai.pipeline.pipeline_process_group import ppg from colossalai.pipeline.rpc._pipeline_base import Phase, PipelineEngineBase, UniqueKey, WorkerBase, WorkItem # Implementation of different Pipeline schedule # <strategy>Worker defines the worker for each stage # <strategy>PipelineEngine is the class for use class FillDrainWorker(WorkerBase): def _get_work_item_key(self) -> UniqueKey: # execute backward first (if backward phase in work_list) num_microbatches = self.num_microbatches if self.forward_times < num_microbatches: target_phase = Phase.FORWARD target_microbatch_id = self.forward_times else: target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times target_key = UniqueKey(target_microbatch_id, target_phase) return target_key class FillDrainPipelineEngine(PipelineEngineBase): def __init__(self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, chunk: int = 1, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None) -> None: if chunk > 1: assert num_microbatches % stage_num == 0, \ "if you use interleaving strategy, make sure 'num_microbatches' is a multiple of stage_num!" use_1F1B = False super().__init__(FillDrainWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func) class OneFOneBWorker(WorkerBase): def _get_work_item_key(self) -> UniqueKey: # execute backward first (if backward phase in work_list) pp_rank = self.pp_rank actual_stage_num = self.actual_stage_num num_microbatches = self.num_microbatches is_last_stage = pp_rank == actual_stage_num - 1 if self.outstanding <= self.outstanding_range[0]: target_phase = Phase.FORWARD target_microbatch_id = self.forward_times elif self.outstanding >= self.outstanding_range[1]: target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times else: raise ValueError("outstanding_range[1] - outstanding_range[0] must be in [0, 1]") target_key = UniqueKey(target_microbatch_id, target_phase) # change outstanding_range at: # 1. forward times reach actual_stage_num, this is the end of continuous forward # 2. forward times reach num_microbatches, this is the end of 1F1B mode if not is_last_stage and \ target_key.phase == Phase.FORWARD: if target_key.microbatch_id == actual_stage_num - 1 and num_microbatches > 2: # Why need num_microbatches > 2 ? Because there is no steady stage when num_microbatches <= 2 outstanding_min = actual_stage_num - pp_rank - 1 outstanding_max = actual_stage_num - pp_rank self.outstanding_range = (outstanding_min, outstanding_max) if target_key.microbatch_id == num_microbatches - 1: self.outstanding_range = (0, 0) return target_key class OneFOneBPipelineEngine(PipelineEngineBase): def __init__(self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, chunk: int = 1, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None) -> None: if chunk > 1: assert num_microbatches % stage_num == 0, \ "if you use interleaving strategy, make sure 'num_microbatches' is a multiple of stage_num!" # assert num_microbatches > stage_num * chunk, "num_microbatches must be greater than stage_num * chunk" use_1F1B = True super().__init__(OneFOneBWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func) class ChimeraWorker(WorkerBase): def _get_producer_consumer(self) -> None: rank = self.pp_rank min_pp_rank = (rank // self.actual_stage_num) * self.actual_stage_num max_pp_rank = min_pp_rank + self.actual_stage_num - 1 assert self.producer_stage_ids is None, f"all the producers of rank {rank} has been subscribed" assert self.consumer_stage_ids is None, f"all the consumers of rank {rank} has been subscribed" # should be aranged in order, the order of the input of current forward self.producer_stage_ids = [] self.consumer_stage_ids = [] # Just for demo prev_rank = rank - 1 next_rank = rank + 1 if prev_rank >= min_pp_rank: self.producer_stage_ids.append(prev_rank) if next_rank <= max_pp_rank: self.consumer_stage_ids.append(next_rank) def _get_work_item_key(self) -> UniqueKey: pp_rank = self.pp_rank stage_num = self.actual_stage_num real_microbatch_num = self.num_microbatches // 2 forward_block_size = 1 if self.num_microbatches < stage_num else self.num_microbatches // stage_num forward_block_num = self.forward_times // forward_block_size if self.forward_times >= real_microbatch_num or \ ((pp_rank + 1) % stage_num == 0 and forward_block_num > self.backward_times): target_phase = Phase.BACKWARD target_microbatch_id = self.backward_times else: # others target_phase = Phase.FORWARD target_microbatch_id = self.forward_times # In up pipeline, microbatch_id to consume is 0, 2, 4 (2n) # In down pipeline, microbatch_id to consume is 1, 3, 5 (2n + 1) real_target_microbatch_id = target_microbatch_id * 2 if pp_rank >= stage_num: real_target_microbatch_id += 1 target_key = UniqueKey(real_target_microbatch_id, target_phase) with self.work_list_condition_lock: self.work_list_condition_lock.wait_for(lambda: target_key in self.work_list) return target_key def _initialize_partition(self): # In order to ensure the down pipeline share the same parameter # with the up pipeline, partition of down partition will be copied # from corresponding up stage pp_rank = self.pp_rank stage_num = self.actual_stage_num device = self.device if pp_rank < stage_num: super()._initialize_partition() else: # if it is down pipeline, create partition by origin method co_up_pp_worker_rref = self.pp_rank_to_worker_rref[pp_rank - stage_num] # get the coresponding model state dict and wait for its init state_dict = co_up_pp_worker_rref.rpc_sync().get_partition_state_dict() super()._initialize_partition() self.module_partition.load_state_dict(state_dict) # init group for chimera in ppg ppg.get_chimera_all_reduce_group(pp_rank) # lock for step sync self.step_sync_lock = threading.Lock() self.step_sync_lock.acquire() self.have_grad_lock = threading.Lock() self.have_grad_lock.acquire() def _get_lock_gradient(self): self.have_grad_lock.acquire() grads = self.get_parameter_gradients() self.step_sync_lock.release() return grads def is_first_stage(self): return (self.pp_rank % self.actual_stage_num) == 0 def is_last_stage(self): return (self.pp_rank % self.actual_stage_num) == self.actual_stage_num - 1 def _is_last_step(self, work_item: WorkItem) -> bool: if work_item.forward_only: last_phase = Phase.FORWARD else: last_phase = Phase.BACKWARD is_last_phase = work_item.phase == last_phase last_microbatch_id = self.num_microbatches - 1 if self.pp_rank < self.actual_stage_num: last_microbatch_id -= 1 is_last_microbatch = work_item.microbatch_id == last_microbatch_id return is_last_phase and is_last_microbatch def _get_step_order(self) -> List[int]: # TODO : If you want to extend it to multi head chimera, overwrite here stage_num = self.actual_stage_num pp_rank = self.pp_rank # pp_rank in the same device local_device_pp_ranks = [pp_rank, stage_num * 2 - pp_rank - 1] local_device_pp_ranks.sort(reverse=min(local_device_pp_ranks) < stage_num // 2) return local_device_pp_ranks def _hook_before_step(self): self.have_grad_lock.release() pp_rank = self.pp_rank stage_num = self.actual_stage_num co_pp_rank = (pp_rank + stage_num) % (2 * stage_num) # if currrent pp_rank is not the first to do step # wait its previous pp_rank finish step grads = self.get_parameter_gradients() # send co_worker = self.pp_rank_to_worker_rref[co_pp_rank] co_grads = co_worker.rpc_sync()._get_lock_gradient() # sync self.step_sync_lock.acquire() for i in range(len(grads)): grads[i] += co_grads[i] class ChimeraPipelineEngine(PipelineEngineBase): def __init__(self, partition_fn: Callable, stage_num: int, num_microbatches: int, device: str, criterion: Callable = None, metric: Callable = None, checkpoint: bool = False, data_process_func: Callable = None) -> None: assert num_microbatches % stage_num == 0, \ "In Chimera, num_microbatches must be the multiply of stage_num!" use_1F1B = False chunk = 1 super().__init__(ChimeraWorker, partition_fn, stage_num, num_microbatches, device, use_1F1B, chunk, criterion, metric, checkpoint, data_process_func) def _consume_constraint(self, microbatch_id: int, forward_only: bool, input_pp_ranks: List[int], output_pp_ranks: List[int], ret_future): pass def _create_pp_rank_to_rpc_worker_id(self) -> None: stage_num = self.stage_num self.pp_rank_to_rpc_worker_id = [0] * (stage_num * 2) for pp_rank in range(stage_num): self.pp_rank_to_rpc_worker_id[pp_rank] = pp_rank self.pp_rank_to_rpc_worker_id[pp_rank + stage_num] = stage_num - pp_rank - 1 def _create_pp_rank_to_module_partition_id(self) -> None: stage_num = self.stage_num self.pp_rank_to_module_partition_id = [0] * (stage_num * 2) for pp_rank in range(stage_num): self.pp_rank_to_module_partition_id[pp_rank] = pp_rank self.pp_rank_to_module_partition_id[pp_rank + stage_num] = pp_rank def _create_ret_future(self, output_pp_ranks: List[int]) -> Dict[int, List[Future]]: num_microbatches = self.num_microbatches stage_num = self.stage_num up_ret_future = {pp_rank: [None] * num_microbatches for pp_rank in output_pp_ranks} down_ret_future = {pp_rank + stage_num: [None] * num_microbatches for pp_rank in output_pp_ranks} # merge up and down return {**up_ret_future, **down_ret_future} def _set_input(self, input_pp_ranks: List[int], microbatch_id: int, microbatch, forward_only: bool): # offset is 0 for all the ranks in up pipeline # offset is stage_num for all the ranks in down pipeline offset = (microbatch_id % 2) * self.stage_num for pp_rank in input_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] worker_rref.remote().set_input(microbatch_id, microbatch, forward_only) def _set_labels(self, output_pp_ranks: List[int], microbatch_id: int, microlabels): # offset is 0 for all the ranks in up pipeline # offset is stage_num for all the ranks in down pipeline offset = (microbatch_id % 2) * self.stage_num for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] worker_rref.remote().set_labels(microbatch_id, microlabels) def _subscribe_forward(self, microbatch_id: int, output_pp_ranks: List[int], ret_future: Dict[int, List[Future]]): key = UniqueKey(microbatch_id, Phase.FORWARD) offset = (microbatch_id % 2) * self.stage_num for pp_rank in output_pp_ranks: worker_rref = self.pp_rank_to_worker_rref[pp_rank + offset] ret_future[pp_rank + offset][microbatch_id] = worker_rref.rpc_async().get_output_by_key(key) def _ensure_backward(self, forward_only: bool, input_pp_ranks: List[int]): stage_num = self.stage_num num_microbatches = self.num_microbatches if not forward_only: for pp_rank in input_pp_ranks: up_last_microbatch_id = num_microbatches - 2 down_last_microbatch_id = num_microbatches - 1 up_worker_rref = self.pp_rank_to_worker_rref[pp_rank] down_worker_rref = self.pp_rank_to_worker_rref[pp_rank + stage_num] up_key = UniqueKey(up_last_microbatch_id, Phase.BACKWARD) down_key = UniqueKey(down_last_microbatch_id, Phase.BACKWARD) up_worker_rref.rpc_sync().get_output_by_key(up_key) down_worker_rref.rpc_sync().get_output_by_key(down_key) def _collect_forward_result(self, output_pp_ranks: List[int], ret_future: Dict[PyRRef, List[Future]]): """Logic of collection of forward in Chimera. Currently, only one input one output model is supported """ stage_num = self.stage_num forward_result = [] for pp_rank in output_pp_ranks: worker_forward_result = [None] * self.num_microbatches for microbatch_id in range(self.num_microbatches): offset = (microbatch_id % 2) * stage_num ret = ret_future[pp_rank + offset][microbatch_id].wait() ret = [ret] if isinstance(ret, torch.Tensor) else ret worker_forward_result[microbatch_id] = ret worker_forward_result = list(zip(*worker_forward_result)) forward_result.extend(worker_forward_result) return forward_result
#!/usr/bin/env python # -*- encoding: utf-8 -*- import inspect import sys from importlib.machinery import SourceFileLoader from pathlib import Path from colossalai.logging import get_dist_logger class Config(dict): """This is a wrapper class for dict objects so that values of which can be accessed as attributes. Args: config (dict): The dict object to be wrapped. """ def __init__(self, config: dict = None): if config is not None: for k, v in config.items(): self._add_item(k, v) def __missing__(self, key): raise KeyError(key) def __getattr__(self, key): try: value = super(Config, self).__getitem__(key) return value except KeyError: raise AttributeError(key) def __setattr__(self, key, value): super(Config, self).__setitem__(key, value) def _add_item(self, key, value): if isinstance(value, dict): self.__setattr__(key, Config(value)) else: self.__setattr__(key, value) def update(self, config): assert isinstance(config, (Config, dict)), 'can only update dictionary or Config objects.' for k, v in config.items(): self._add_item(k, v) return self @staticmethod def from_file(filename: str): """Reads a python file and constructs a corresponding :class:`Config` object. Args: filename (str): Name of the file to construct the return object. Returns: :class:`Config`: A :class:`Config` object constructed with information in the file. Raises: AssertionError: Raises an AssertionError if the file does not exist, or the file is not .py file """ # check config path if isinstance(filename, str): filepath = Path(filename).absolute() elif isinstance(filename, Path): filepath = filename.absolute() assert filepath.exists(), f'{filename} is not found, please check your configuration path' # check extension extension = filepath.suffix assert extension == '.py', 'only .py files are supported' # import the config as module remove_path = False if filepath.parent not in sys.path: sys.path.insert(0, (filepath)) remove_path = True module_name = filepath.stem source_file = SourceFileLoader(fullname=str(module_name), path=str(filepath)) module = source_file.load_module() # load into config config = Config() for k, v in module.__dict__.items(): if k.startswith('__') or inspect.ismodule(v) or inspect.isclass(v): continue else: config._add_item(k, v) logger = get_dist_logger() logger.debug('variables which starts with __, is a module or class declaration are omitted in config file') # remove module del sys.modules[module_name] if remove_path: sys.path.pop(0) return config class ConfigException(Exception): pass
from .config import Config, ConfigException from .parallel_context import ParallelContext from .parallel_mode import ParallelMode from .moe_context import MOE_CONTEXT from .process_group_initializer import * from .random import *
#!/usr/bin/env python # -*- encoding: utf-8 -*- import random import socket from collections import Counter from threading import local from typing import Union import numpy as np import torch import torch.distributed as dist from colossalai.constants import ALLOWED_MODES, INITIALIZER_MAPPING from colossalai.context.config import Config from colossalai.global_variables import tensor_parallel_env as env from colossalai.logging import get_dist_logger from colossalai.registry import DIST_GROUP_INITIALIZER from .parallel_mode import ParallelMode from .random import add_seed, get_seeds, set_mode from colossalai.context.singleton_meta import SingletonMeta class ParallelContext(metaclass=SingletonMeta): """This class provides interface functions for users to get the parallel context, such as the global rank, the local rank, the world size, etc. of each device. Note: The parallel_mode used in this class 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): # distributed settings self._global_ranks = dict() self._local_ranks = dict() self._world_sizes = dict() self._groups = dict() self._cpu_groups = dict() self._ranks_in_group = dict() # load config from file self._config = None # default 3D parallel args, will be overwritten during process group intialization self.world_size = 1 self.data_parallel_size = 1 self.pipeline_parallel_size = 1 self.tensor_parallel_size = 1 self.num_processes_on_current_node = -1 self.virtual_pipeline_parallel_size = None self.virtual_pipeline_parallel_rank = None # logging self._verbose = False self._logger = get_dist_logger() @property def config(self): return self._config @property def verbose(self): return self._verbose @verbose.setter def verbose(self, verbose_: bool): self._verbose = verbose_ def load_config(self, config: Union[dict, str]): """Loads the configuration from either a dict or a file. Args: config (dict or str): Either a dict containing the configuration information or the filename of a file containing the configuration information. Raises: TypeError: Raises a TypeError if `config` is neither a dict nor a str. """ if isinstance(config, str): self._config = Config.from_file(config) elif isinstance(config, dict): self._config = Config(config) else: raise TypeError("Invalid type for config, only dictionary or string is supported") def detect_num_processes_on_current_node(self): hostname = socket.gethostname() hostname_list = [None for _ in range(self.get_world_size(ParallelMode.GLOBAL))] dist.all_gather_object(hostname_list, hostname, group=self.get_group(ParallelMode.GLOBAL)) counter = Counter(hostname_list) self.num_processes_on_current_node = counter[hostname] @staticmethod def _check_parallel_mode(parallel_mode: ParallelMode): assert isinstance(parallel_mode, ParallelMode), \ f'expected the argument parallel_mode to be of enum ParallelMode, but got {type(parallel_mode)}' def get_global_rank(self): """Returns the global rank of the current device. Returns: int: The global rank of the current device """ return self._global_ranks[ParallelMode.GLOBAL] def add_global_rank(self, parallel_mode: ParallelMode, rank: int): """Adds the global rank of the current device for `parallel_mode` to the context. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The parallel mode for the rank. rank (int): The rank to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._global_ranks[parallel_mode] = rank def get_local_rank(self, parallel_mode: ParallelMode): """Returns the local rank of the current device. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: int: The local rank of the current device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._local_ranks[parallel_mode] def _add_local_rank(self, parallel_mode: ParallelMode, rank: int): """Adds the local rank of the current device for `parallel_mode` to the context. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The parallel mode for the rank. rank (int): The rank to be added. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._local_ranks[parallel_mode] = rank def get_next_global_rank(self, parallel_mode: ParallelMode): """Returns the global rank of the next device. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: int: The global rank of the next device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) # get rank and world size local_rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) ranks_in_group = self.get_ranks_in_group(parallel_mode) return ranks_in_group[(local_rank + 1) % world_size] def get_prev_global_rank(self, parallel_mode: ParallelMode): """Returns the global rank of the previous device. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: int: The global rank of the previous device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) # get rank and world size local_rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) ranks_in_group = self.get_ranks_in_group(parallel_mode) return ranks_in_group[(local_rank - 1) % world_size] def is_first_rank(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: bool: a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. """ rank = self.get_local_rank(parallel_mode) return rank == 0 def is_last_rank(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether the current device is the last one among its group for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: bool: a boolean value indicating whether the current device is the first one among its group for `parallel_mode`. """ rank = self.get_local_rank(parallel_mode) world_size = self.get_world_size(parallel_mode) return rank == world_size - 1 def is_pipeline_first_stage(self, ignore_virtual=False): if not ignore_virtual: if self.virtual_pipeline_parallel_size is not None and self.virtual_pipeline_parallel_rank != 0: return False return self.is_first_rank(ParallelMode.PIPELINE) def is_pipeline_last_stage(self, ignore_virtual=False): if not ignore_virtual: if self.virtual_pipeline_parallel_size \ is not None and self.virtual_pipeline_parallel_rank != self.virtual_pipeline_parallel_size - 1: return False return self.is_last_rank(ParallelMode.PIPELINE) def get_world_size(self, parallel_mode: ParallelMode): """Returns the world size for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: int: The world size for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._world_sizes[parallel_mode] def _add_world_size(self, parallel_mode: ParallelMode, world_size: int): """Adds world size for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The parallel mode correponding to the process group world_size (int): The world size to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._world_sizes[parallel_mode] = world_size def get_group(self, parallel_mode: ParallelMode): """Returns the group of the current device for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: torch.distributed.ProcessGroup: The group of the current device for `parallel_mode`. """ self._check_parallel_mode(parallel_mode) return self._groups[parallel_mode] def _add_group(self, parallel_mode: ParallelMode, group: dist.ProcessGroup): """Adds the group of the current device for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. group (torch.distributed.ProcessGroup): The group to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._groups[parallel_mode] = group def get_cpu_group(self, parallel_mode: ParallelMode): """Returns the Gloo group of the current device for `parallel_mode`. :param parallel_mode: The chosen parallel mode :type parallel_mode: :class:`colossalai.context.ParallelMode` :raises AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode` :return: The group of the current device for `parallel_mode` :rtype: torch.distributed.ProcessGroup """ self._check_parallel_mode(parallel_mode) return self._cpu_groups[parallel_mode] def _add_cpu_group(self, parallel_mode: ParallelMode, group: dist.ProcessGroup): """Adds the Gloo group of the current device for `parallel_mode`. :param parallel_mode: The chosen parallel mode :type parallel_mode: :class:`colossalai.context.ParallelMode` :param group: The group to be added :type group: torch.distributed.ProcessGroup :raises AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode` """ self._check_parallel_mode(parallel_mode) self._cpu_groups[parallel_mode] = group def get_ranks_in_group(self, parallel_mode: ParallelMode): """Returns the rank of the current device for `parallel_mode` in the group. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. Returns: int: The rank of the current device for `parallel_mode` in the group. """ self._check_parallel_mode(parallel_mode) return self._ranks_in_group[parallel_mode] def _add_ranks_in_group(self, parallel_mode: ParallelMode, ranks: list): """Adds the ranks of the current device for `parallel_mode` in the group. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. ranks (list): List of ranks to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode`. """ self._check_parallel_mode(parallel_mode) self._ranks_in_group[parallel_mode] = ranks def init_global_dist(self, rank: int, world_size: int, backend: str, host: str, port: int): """Initializes the global distributed environment Args: rank (int): rank for the default process group. world_size (int): world size of the default process group. backend (str): backend for ``torch.distributed`` host (str): the master address for distributed training. port (str): the master port for distributed training """ # initialize the default process group init_method = f'tcp://[{host}]:{port}' dist.init_process_group(rank=rank, world_size=world_size, backend=backend, init_method=init_method) # None will give the default global process group for pytorch dist operations ranks = list(range(world_size)) cpu_group = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else None self._register_dist(rank, world_size, dist.GroupMember.WORLD, cpu_group, ranks, ParallelMode.GLOBAL) self.add_global_rank(ParallelMode.GLOBAL, rank) def _register_dist(self, local_rank, world_size, process_group, cpu_group, ranks_in_group, mode): self._add_local_rank(mode, local_rank) self._add_world_size(mode, world_size) self._add_group(mode, process_group) self._add_cpu_group(mode, cpu_group) self._add_ranks_in_group(mode, ranks_in_group) def check_sanity(self): """Checks sanity of the parallel context. Raises: AssertionError: Raises an AssertionError if the world size does not equal to the product of data parallel size, pipeline parallel size and tensor parallel size. """ dps = self.data_parallel_size pps = self.pipeline_parallel_size tps = self.tensor_parallel_size ws = self.world_size assert ws == dps * pps * \ tps, f"Expected the world size {ws} to be equal to data" \ f" parallel size ({dps}) * pipeline parallel size " \ f"({pps}) * tensor parallel size ({tps})" def _set_parallel_size_from_config(self, config: dict, key: str, attr_name: str): if key in config: ele = config[key] if isinstance(ele, int): setattr(self, attr_name, ele) elif isinstance(ele, dict): setattr(self, attr_name, ele['size']) else: raise NotImplementedError( f'{"Parallel configuration does not support this kind of argument, please use int or dict"}') def init_parallel_groups(self): """Initializes the parallel groups. Raises: AssertionError: Raises an AssertionError if the field parallel is not present in the config file. """ # get rank and world size rank = self.get_global_rank() world_size = self.get_world_size(ParallelMode.GLOBAL) self.world_size = world_size # set parallel size as attributes for global context parallel_config = self.config.get('parallel', None) if parallel_config is not None: self._set_parallel_size_from_config(parallel_config, 'pipeline', 'pipeline_parallel_size') self._set_parallel_size_from_config(parallel_config, 'tensor', 'tensor_parallel_size') # the user should not set the data parallel size manually # instead, it should be calculated based on other parallel config self.data_parallel_size = self.world_size // (self.pipeline_parallel_size * self.tensor_parallel_size) # get the tensor parallel mode and check tensor_parallel_mode = None if parallel_config is not None and 'tensor' in \ parallel_config and 'mode' in parallel_config['tensor']: tensor_parallel_mode = parallel_config['tensor']['mode'] assert tensor_parallel_mode in ALLOWED_MODES, \ f"mode in the parallel config must be set to one of {ALLOWED_MODES}" env.mode = tensor_parallel_mode self.check_sanity() pg_init = [] # LSG: init data parallel process group for compatibility with other parallel module such as zero pg_init.append(dict(type=INITIALIZER_MAPPING['data'])) # LSG: init model parallel process group for compatibility with amp and clip grad pg_init.append(dict(type=INITIALIZER_MAPPING['model'])) if self.pipeline_parallel_size > 1: pg_init.append(dict(type=INITIALIZER_MAPPING['pipeline'])) pg_init.append(dict(type=INITIALIZER_MAPPING['tensor'])) # init specific tensor parallel group if tensor_parallel_mode is not None: tensor_parallel_cfg = parallel_config['tensor'].copy() # remove duplicate parameters tensor_parallel_cfg.pop('mode') tensor_parallel_cfg.pop('size') # add this config to initialize later pg_init.append(dict(type=INITIALIZER_MAPPING[tensor_parallel_mode.lower()], **tensor_parallel_cfg)) # run initialization of different process groups for initializer_cfg in pg_init: cfg = initializer_cfg.copy() initializer_type = cfg.pop('type') initializer = DIST_GROUP_INITIALIZER.get_module(initializer_type)(rank, world_size, self.config, self.data_parallel_size, self.pipeline_parallel_size, self.tensor_parallel_size, **cfg) parallel_setting = initializer.init_dist_group() if isinstance(parallel_setting, list): for args in parallel_setting: self._register_dist(*args) else: self._register_dist(*parallel_setting) def is_initialized(self, parallel_mode: ParallelMode): """Returns a boolean value indicating whether `parallel_mode` is initialized in the current system. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. Returns: bool: a boolean value indicating whether `parallel_mode` is initialized in the current system. """ return parallel_mode in self._groups def destroy(self): """Destroys the current distributed parallel environment. """ for mode, group in self._groups.items(): if mode is not ParallelMode.GLOBAL: dist.destroy_process_group(group) # destroy global process group dist.destroy_process_group() self._groups.clear() def set_device(self, device_ordinal: int = None): """Sets distributed processes to be bound to devices. Args: device_ordinal (int, optional): the device id to be bound to """ global_rank = self.get_global_rank() if device_ordinal is None: devices_per_node = torch.cuda.device_count() device_ordinal = global_rank % devices_per_node torch.cuda.set_device(device_ordinal) if self._verbose: self._logger.info(f'process rank {global_rank} is bound to device {device_ordinal}') def set_seed(self, seed: int): """Sets seeds for all random libraries. Args: seed (int): seed for random states """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed) global_rank = self.get_global_rank() if torch.cuda.is_available(): # create random seed for different parallel modes # data parallel seed are kept the same parallel_seed = seed add_seed(ParallelMode.DATA, parallel_seed) # model parallel seeds are different across ranks pipeline_offset = self._local_ranks.get(ParallelMode.PIPELINE, 0) # add seed for data parallel and tensor parallel only if self.is_initialized(ParallelMode.TENSOR): tp_rank = self.get_local_rank(ParallelMode.TENSOR) # 100 is only to increase the diff in seeds between pipeline stages tp_rank_with_offset = tp_rank + pipeline_offset * 1024 tp_seed = seed + tp_rank_with_offset add_seed(ParallelMode.TENSOR, tp_seed) set_mode(ParallelMode.DATA) seeds = get_seeds() seed_str = ', '.join([f'{k}: {v}' for k, v in seeds.items()]) if self._verbose: self._logger.info(f"initialized seed on rank {global_rank}, " f"numpy: {seed}, python random: {seed}, {seed_str}," f"the default parallel seed is {ParallelMode.DATA}.") else: if self._verbose: self._logger.info( f"initialized seed on rank {global_rank}, " f"numpy: {seed}, python random: {seed}, pytorch: {seed}", ranks=[0]) self._logger.info( 'WARNING: CUDA is not available, thus CUDA RNG cannot be used to track CUDA random number states', ranks=[0]) def set_virtual_pipeline_parallel_size(self, size): self.virtual_pipeline_parallel_size = size def set_virtual_pipeline_parallel_rank(self, rank): self.virtual_pipeline_parallel_rank = rank global_context = ParallelContext()
from typing import Tuple import torch import torch.distributed as dist from colossalai.context.parallel_mode import ParallelMode from colossalai.context.singleton_meta import SingletonMeta from colossalai.tensor import ProcessGroup def _check_sanity(): from colossalai.core import global_context as gpc if gpc.tensor_parallel_size > 1 or gpc.pipeline_parallel_size > 1: raise NotImplementedError("Moe is not compatible with tensor or " "pipeline parallel at present.") class MoeParallelInfo: """Moe parallelism information, storing parallel sizes and groups. """ def __init__(self, ep_size: int, dp_size: int): _check_sanity() self.ep_size = ep_size self.dp_size = dp_size self.pg = ProcessGroup(tp_degree=ep_size, dp_degree=dp_size) self.ep_group = self.pg.tp_process_group() self.dp_group = self.pg.dp_process_group() class MoeContext(metaclass=SingletonMeta): """MoE parallel context manager. This class manages different parallel groups in MoE context and MoE loss in training. """ def __init__(self): self.world_size = 1 # Users may want to set maximum expert parallel size smaller than the world size # since very low bandwidth across nodes may constrain the performance of MoE # When we have a maximum expert parallel size, we have a minimum data parallel size naturally self.max_ep_size = 1 self.min_dp_size = 1 self.aux_loss = None self.use_kernel_optim = True self.has_setup = False self._parallel_info_dict = dict() @property def parallel_info_dict(self): return self._parallel_info_dict @property def is_initialized(self): return self.has_setup def setup(self, seed: int, use_kernel_optim: bool = True): assert not self.is_initialized, "MoE distributed context shouldn't be set up again" _check_sanity() assert torch.cuda.is_available(), "MoE requires to enable CUDA first" self.world_size = dist.get_world_size() from colossalai.core import global_context as gpc self.max_ep_size = gpc.config.get('max_ep_size', self.world_size) assert self.world_size % self.max_ep_size == 0, \ "Maximum epxert parallel size must be a factor of the number of GPUs" self.min_dp_size = self.world_size // self.max_ep_size # Enabling kernel optimization may raise error in some cases # Users can close kernel optimization manually self.use_kernel_optim = use_kernel_optim from .random import moe_set_seed moe_set_seed(seed) self.has_setup = True def get_info(self, num_experts: int) -> Tuple[int, MoeParallelInfo]: """Calculate the Data Parallel Group and Expert Parallel Group. Parameters ---------- num_experts : int The number experts Returns ------- int, MoeParallelInfo number of local experts, the MoeParallelInfo of the current ep_size """ gt_flag = num_experts % self.max_ep_size == 0 # check whether num_experts is greater lt_flag = self.max_ep_size % num_experts == 0 # check whether num_experts is less assert gt_flag or lt_flag, "Automatic experts placement dose not not support expert number" \ " is not a multiple of ep size or vice versa." # If the number of experts is greater than maximum expert parallel size. a.k.a ep_size, # there are multiple experts in each GPU and each GPU has different experts # So it's data parallel size is 1 # Otherwise, there is only one expert in each GPU # The data parallel size should be calculated dp_size = 1 if gt_flag else self.max_ep_size // num_experts ep_size = self.max_ep_size // dp_size # Calculate the number of experts for each GPU num_local_experts = 1 if lt_flag else num_experts // self.max_ep_size # Don't forget to multiply minimum data parallel size dp_size *= self.min_dp_size if not (ep_size in self.parallel_info_dict): self.parallel_info_dict[ep_size] = MoeParallelInfo(ep_size, dp_size) return num_local_experts, self.parallel_info_dict[ep_size] def set_kernel_not_use(self): self.use_kernel_optim = False def reset_loss(self): self.aux_loss = 0 def add_loss(self, loss): self.aux_loss += loss def get_loss(self): return self.aux_loss MOE_CONTEXT = MoeContext()
class SingletonMeta(type): """ The Singleton class can be implemented in different ways in Python. Some possible methods include: base class, decorator, metaclass. We will use the metaclass because it is best suited for this purpose. """ _instances = {} def __call__(cls, *args, **kwargs): """ Possible changes to the value of the `__init__` argument do not affect the returned instance. """ if cls not in cls._instances: instance = super().__call__(*args, **kwargs) cls._instances[cls] = instance else: assert len(args) == 0 and len( kwargs) == 0, f'{cls.__name__} is a singleton class and a instance has been created.' return cls._instances[cls]
#!/usr/bin/env python # -*- encoding: utf-8 -*- from enum import Enum # parallel modes class ParallelMode(Enum): """This is an enumeration class containing all possible parallel modes. """ GLOBAL = 'global' # common parallel DATA = 'data' # model parallel - containing tensor and pipeline parallel groups # this is added to facilitate amp and grad clipping in hybrid parallel MODEL = 'model' # pipeline parallel PIPELINE = 'pipe' # containing all ranks in tensor parallel TENSOR = 'tensor' # sequence parallel SEQUENCE = 'sequence' SEQUENCE_DP = 'sequence_dp' # 1D Parallel PARALLEL_1D = '1d' # 2D parallel PARALLEL_2D_ROW = '2d_row' PARALLEL_2D_COL = '2d_col' # 3D parallel PARALLEL_3D_INPUT = '3d_input' PARALLEL_3D_WEIGHT = '3d_weight' PARALLEL_3D_OUTPUT = '3d_output' PARALLEL_3D_INPUT_X_WEIGHT = "3d_input_x_weight" PARALLEL_3D_OUTPUT_X_WEIGHT = "3d_output_x_weight" # 2.5D parallel PARALLEL_2P5D_ROW = '2p5d_row' PARALLEL_2P5D_COL = '2p5d_col' PARALLEL_2P5D_DEP = '2p5d_dep' PARALLEL_2P5D_XZ = '2p5d_xz'
#!/usr/bin/env python # -*- encoding: utf-8 -*- from torch import distributed as dist from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Pipeline(ProcessGroupInitializer): """A ProcessGroupInitializer for pipeline parallelism. Args: rank (int): The rank of current process world_size (int): Size of whole communication world config (Config): Running configuration data_parallel_size (int): Size of data parallel pipeline_parallel_size (int): Size of pipeline parallel tensor_parallel_size (int): Size of tensor parallel """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.data_group_size = self.world_size // self.data_parallel_size self.pipeline_stage_size = self.data_group_size // self.pipeline_parallel_size def init_dist_group(self): """Initialize pipeline parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: A Pipeline parallelism's information in list of tuples. """ dist_settings = list() for i in range(self.data_parallel_size): for j in range(self.pipeline_stage_size): pipe_ranks = list( range(i * self.data_group_size + j, (i + 1) * self.data_group_size, self.pipeline_stage_size)) pipe_group_size = len(pipe_ranks) pipe_group = dist.new_group(pipe_ranks) group_cpu = dist.new_group(pipe_ranks, backend='gloo') if dist.get_backend() != 'gloo' else pipe_group if self.rank in pipe_ranks: local_rank = pipe_ranks.index(self.rank) group_world_size = pipe_group_size process_group = pipe_group cpu_group = group_cpu ranks_in_group = pipe_ranks dist_settings.append( tuple((local_rank, group_world_size, process_group, cpu_group, ranks_in_group, ParallelMode.PIPELINE))) return dist_settings
#!/usr/bin/env python # -*- encoding: utf-8 -*- import math import torch.distributed as dist from colossalai.global_variables import tensor_parallel_env as env from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_depth_env_var(depth): # check global variable env_depth = env.depth_3d if env_depth: assert int(env_depth) == depth, \ 'DEPTH_3D has been set in the current environment and ' \ 'does not match with the value passed to this initialized' else: env.depth_3d = depth class Initializer_3D_Input(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_INPUT env.input_group_3d = mode for h in range(self.num_group): for i in range(self.depth): for k in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for j in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_Weight(ProcessGroupInitializer): """3D tensor parallel initialization among weight. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among weight, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among weight in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_WEIGHT env.weight_group_3d = mode for h in range(self.num_group): for k in range(self.depth): for j in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for i in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_Output(ProcessGroupInitializer): """3D tensor parallel initialization among output. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among output, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among output in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_OUTPUT env.output_group_3d = mode for h in range(self.num_group): for i in range(self.depth): for j in range(self.depth): ranks = [h * self.depth**3 + i + self.depth * (j + self.depth * k) for k in range(self.depth)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_InputxWeight(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_INPUT_X_WEIGHT env.input_x_weight_group_3d = mode for h in range(self.num_group): for k in range(self.depth): ranks = [ h * self.depth**3 + i + self.depth * (j + self.depth * k) for j in range(self.depth) for i in range(self.depth) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_3D_OutputxWeight(ProcessGroupInitializer): """3D tensor parallel initialization among input. Args: num_group (int): The number of all tensor groups. depth (int): Depth of 3D parallelism. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group: int, depth: int, *args): super().__init__(*args) self.num_group = num_group self.depth = depth def init_dist_group(self): """Initialize 3D tensor parallel groups among input, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 3D tensor parallelism's information among input in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_3D_OUTPUT_X_WEIGHT env.output_x_weight_group_3d = mode for h in range(self.num_group): for j in range(self.depth): ranks = [ h * self.depth**3 + i + self.depth * (j + self.depth * k) for k in range(self.depth) for i in range(self.depth) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_3D(ProcessGroupInitializer): """Serve as the single entry point to 3D parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args): super().__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.depth = round(math.pow(self.tensor_parallel_size, 1 / 3)) assert self.tensor_parallel_size == self.depth ** 3, \ f'3D depth ({self.depth}) if not cube root of tensor parallel size ({self.tensor_parallel_size})' _check_depth_env_var(self.depth) self.input_initializer = Initializer_3D_Input(self.num_group, self.depth, *args) self.weight_initializer = Initializer_3D_Weight(self.num_group, self.depth, *args) self.output_initializer = Initializer_3D_Output(self.num_group, self.depth, *args) self.input_x_weight_initializer = Initializer_3D_InputxWeight(self.num_group, self.depth, *args) self.output_x_weight_initializer = Initializer_3D_OutputxWeight(self.num_group, self.depth, *args) def init_dist_group(self): """Initialize 3D tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: Whole 3D tensor parallelism's information in a list of tuples. """ parallel_setting = [ self.input_initializer.init_dist_group(), self.weight_initializer.init_dist_group(), self.output_initializer.init_dist_group(), self.input_x_weight_initializer.init_dist_group(), self.output_x_weight_initializer.init_dist_group() ] return parallel_setting
import math import torch.distributed as dist from colossalai.global_variables import tensor_parallel_env as env from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_summa_env_var(summa_dim): # check environment variable for SUMMA env_summa_dim = env.summa_dim if env_summa_dim: assert int(env_summa_dim) == summa_dim, \ 'SUMMA_DIM has been set in the current environment and ' \ 'does not match with the value passed to this initialized' else: env.summa_dim = summa_dim class Initializer_2D_Row(ProcessGroupInitializer): """2d tensor parallel initialization among rows. Args: num_group (int): The number of all tensor groups. summa_dim (int): The dimension of SUMMA. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group, summa_dim, *args, **kwargs): super(Initializer_2D_Row, self).__init__(*args, **kwargs) self.num_group = num_group self.summa_dim = summa_dim def init_dist_group(self): """Initialize 2D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2D tensor row parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2D_ROW for i in range(self.num_group): for j in range(self.summa_dim): ranks = [i * self.tensor_parallel_size + j * self.summa_dim + k for k in range(self.summa_dim)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2D_Col(ProcessGroupInitializer): """2d tensor parallel initialization among cols. Args: num_group (int): The number of all tensor groups. summa_dim (int): The dimension of SUMMA. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, num_group, summa_dim, *args, **kwargs): super(Initializer_2D_Col, self).__init__(*args, **kwargs) self.num_group = num_group self.summa_dim = summa_dim def init_dist_group(self): """Initialize 2D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2D tensor col parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2D_COL for i in range(self.num_group): for j in range(self.summa_dim): ranks = [i * self.tensor_parallel_size + j + k * self.summa_dim for k in range(self.summa_dim)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_2D(ProcessGroupInitializer): """ Serve as the single entry point to 2D parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_group = self.world_size // self.tensor_parallel_size self.summa_dim = int(math.sqrt(self.tensor_parallel_size)) assert self.tensor_parallel_size == self.summa_dim ** 2, \ "2D summa dim should equal to tensor parallel size ^ 0.5" _check_summa_env_var(self.summa_dim) self.col_initializer = Initializer_2D_Col(self.num_group, self.summa_dim, *args, **kwargs) self.row_initializer = Initializer_2D_Row(self.num_group, self.summa_dim, *args, **kwargs) def init_dist_group(self): """Initialize 2D tensor row and col parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: 2D tensor parallelism's information in a list of tuples. """ parallel_setting = [self.row_initializer.init_dist_group(), self.col_initializer.init_dist_group()] return parallel_setting
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.global_variables import tensor_parallel_env as env from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_1D(ProcessGroupInitializer): """A ProcessGroupInitializer for 1d tensor parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_group = self.world_size // self.tensor_parallel_size def init_dist_group(self): """Initialize 1D tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 1D tensor parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_1D env.parallel_input_1d = False for i in range(self.num_group): ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.registry import DIST_GROUP_INITIALIZER from .process_group_initializer import ProcessGroupInitializer from ..parallel_mode import ParallelMode @DIST_GROUP_INITIALIZER.register_module class Initializer_Tensor(ProcessGroupInitializer): """A ProcessGroupInitializer for tensor parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_tensor_parallel_group = self.world_size // self.tensor_parallel_size def init_dist_group(self): """Initialize tensor parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Tensor parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.TENSOR for i in range(self.num_tensor_parallel_group): ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.registry import DIST_GROUP_INITIALIZER from .process_group_initializer import ProcessGroupInitializer from ..parallel_mode import ParallelMode @DIST_GROUP_INITIALIZER.register_module class Initializer_Model(ProcessGroupInitializer): """A ProcessGroupInitializer for model parallelism (model parallel group contains pipeline and tensor parallel groups). Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.model_parallel_size = self.tensor_parallel_size * self.pipeline_parallel_size self.num_group = self.world_size // self.model_parallel_size def init_dist_group(self): """Initialize model parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Model parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.MODEL for i in range(self.num_group): ranks = [i * self.model_parallel_size + j for j in range(self.model_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
from .initializer_1d import Initializer_1D from .initializer_2d import Initializer_2D from .initializer_2p5d import Initializer_2p5D from .initializer_3d import Initializer_3D from .initializer_data import Initializer_Data from .initializer_pipeline import Initializer_Pipeline from .initializer_sequence import Initializer_Sequence from .initializer_tensor import Initializer_Tensor from .initializer_model import Initializer_Model from .process_group_initializer import ProcessGroupInitializer __all__ = [ 'Initializer_Tensor', 'Initializer_Sequence', 'Initializer_Pipeline', 'Initializer_Data', 'Initializer_2p5D', 'Initializer_2D', 'Initializer_3D', 'Initializer_1D', 'ProcessGroupInitializer', 'Initializer_Model' ]
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod from colossalai.context import Config class ProcessGroupInitializer(ABC): """An object, knowing the parallelism configuration, that initializes parallel groups. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, rank: int, world_size: int, config: Config, data_parallel_size: int, pipeline_parallel_size: int, tensor_parallel_size: int): self.rank = rank self.world_size = world_size self.data_parallel_size = data_parallel_size self.config = config self.pipeline_parallel_size = pipeline_parallel_size self.tensor_parallel_size = tensor_parallel_size super().__init__() @abstractmethod def init_dist_group(self): pass
#!/usr/bin/env python # -*- encoding: utf-8 -*- import math import torch.distributed as dist from colossalai.context import Config from colossalai.global_variables import tensor_parallel_env as env from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .process_group_initializer import ProcessGroupInitializer def _check_tesseract_env_var(tesseract_dim: int, tesseract_dep: int): # check global variable for TESSERACT env_tesseract_dim = env.tesseract_dim env_tesseract_dep = env.tesseract_dep if env_tesseract_dim and env_tesseract_dep: assert int(env_tesseract_dim) == tesseract_dim, \ 'TESSERACT_DIM has been set in the current environment and ' \ 'does not match with the value passed to this initialized' assert int(env_tesseract_dep) == tesseract_dep, \ 'TESSERACT_DEP has been set in the current environment and ' \ 'does not match with the value passed to this initialized' else: env.tesseract_dim = tesseract_dim env.tesseract_dep = tesseract_dep # i row j col k dep class Initializer_2p5D_ROW(ProcessGroupInitializer): """2.5d tensor parallel initialization among rows. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_ROW, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim assert self.tensor_parallel_size == self.tesseract_dim ** 2 * self.tesseract_dep, \ "Tensor parallel size should be depth * dim ** 2 in 2.5D parallel" def init_dist_group(self): """Initialize 2.5D tensor row parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor row parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_ROW for h in range(self.num_group): for j in range(self.tesseract_dim): for k in range(self.tesseract_dep): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for i in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2p5D_Col(ProcessGroupInitializer): """2.5d tensor parallel initialization among cols. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_Col, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor col parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor col parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_COL for h in range(self.num_group): for i in range(self.tesseract_dim): for k in range(self.tesseract_dep): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for j in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode class Initializer_2p5D_Dep(ProcessGroupInitializer): """2.5D tensor parallel initialization among depths. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_Dep, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor depth parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor depth parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_DEP for h in range(self.num_group): for i in range(self.tesseract_dim): for j in range(self.tesseract_dim): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for k in range(self.tesseract_dep) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode # i row j col k dep class Initializer_2p5D_XZ(ProcessGroupInitializer): """2.5d tensor parallel initialization among cols times dep. Args: tesseract_dim (int): The dimension of tesseract. tesseract_dep (int): The dimension of depth. rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, tesseract_dim: int, tesseract_dep: int, *args): super(Initializer_2p5D_XZ, self).__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dep = tesseract_dep self.tesseract_dim = tesseract_dim def init_dist_group(self): """Initialize 2.5D tensor colXdepth parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): 2.5D tensor colXdepth parallelism's information in a tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.PARALLEL_2P5D_XZ for h in range(self.num_group): for i in range(self.tesseract_dim): ranks = [ h * self.tensor_parallel_size + i + self.tesseract_dim * (j + self.tesseract_dim * k) for k in range(self.tesseract_dep) for j in range(self.tesseract_dim) ] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_2p5D(ProcessGroupInitializer): """ Serve as the single entry point to Tesseract parallel initialization. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. depth (int): The depth of 2.5d parallel. """ def __init__(self, rank: int, world_size: int, config: Config, data_parallel_size: int, pipeline_parallel_size: int, tensor_parallel_size: int, depth: int): args = (rank, world_size, config, data_parallel_size, pipeline_parallel_size, tensor_parallel_size) super().__init__(*args) self.num_group = self.world_size // self.tensor_parallel_size self.tesseract_dim = int(math.sqrt(self.tensor_parallel_size / depth)) self.tesseract_dep = depth assert self.tensor_parallel_size == self.tesseract_dim ** 2 * self.tesseract_dep, \ "2.5D tesseract dim should equal to (tensor parallel size / tesseract dep) ^ 0.5" _check_tesseract_env_var(self.tesseract_dim, self.tesseract_dep) self.col_initializer = Initializer_2p5D_Col(self.tesseract_dim, self.tesseract_dep, *args) self.row_initializer = Initializer_2p5D_ROW(self.tesseract_dim, self.tesseract_dep, *args) self.dep_initializer = Initializer_2p5D_Dep(self.tesseract_dim, self.tesseract_dep, *args) self.xz_initializer = Initializer_2p5D_XZ(self.tesseract_dim, self.tesseract_dep, *args) def init_dist_group(self): """Initialize 2.5D tensor row, col, depth, and colXdepth parallel groups, and assign local_ranks and groups to each gpu. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: Whole 2.5D tensor parallelism's information in a list of tuples. """ parallel_setting = [ self.col_initializer.init_dist_group(), self.row_initializer.init_dist_group(), self.dep_initializer.init_dist_group(), self.xz_initializer.init_dist_group() ] return parallel_setting
#!/usr/bin/env python # -*- encoding: utf-8 -*- from torch import distributed as dist from colossalai.registry import DIST_GROUP_INITIALIZER from .process_group_initializer import ProcessGroupInitializer from ..parallel_mode import ParallelMode @DIST_GROUP_INITIALIZER.register_module class Initializer_Data(ProcessGroupInitializer): """A ProcessGroupInitializer for data parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.num_data_parallel_group = self.world_size // self.data_parallel_size def init_dist_group(self): """Initialize data parallel groups, and assign local_ranks and groups to each gpu. Returns: Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode): A Data parallelism's information tuple. """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.DATA for i in range(self.num_data_parallel_group): ranks = [i + j * self.num_data_parallel_group for j in range(self.data_parallel_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch.distributed as dist from colossalai.registry import DIST_GROUP_INITIALIZER from ..parallel_mode import ParallelMode from .initializer_tensor import Initializer_Tensor from .process_group_initializer import ProcessGroupInitializer @DIST_GROUP_INITIALIZER.register_module class Initializer_Sequence_DP(ProcessGroupInitializer): """A ProcessGroupInitializer for sequence parallelism all-reduce. In Sequence Parallelism, each GPU holds the full copy of model weights, thus, gradient all-reduce occurs across all processes in the same pipeline stage Args: rank (int): The rank of current process world_size (int): Size of whole communication world config (Config): Running configuration data_parallel_size (int): Size of data parallel pipeline_parallel_size (int): Size of pipeline parallel tensor_parallel_size (int): Size of tensor parallel """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dp_size = self.world_size // self.pipeline_parallel_size self.num_group = self.pipeline_parallel_size def init_dist_group(self): """Initialize Sequence Parallel process groups used for gradient all-reduce. Returns: Tuple: A tuple (local_rank, group_world_size, process_group, ranks_in_group, mode). """ local_rank = None ranks_in_group = None process_group = None cpu_group = None group_world_size = None mode = ParallelMode.SEQUENCE_DP for i in range(self.num_group): ranks = [i * self.dp_size + j for j in range(self.dp_size)] group = dist.new_group(ranks) group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group if self.rank in ranks: local_rank = ranks.index(self.rank) group_world_size = len(ranks) process_group = group cpu_group = group_cpu ranks_in_group = ranks return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode @DIST_GROUP_INITIALIZER.register_module class Initializer_Sequence(ProcessGroupInitializer): """A ProcessGroupInitializer for sequence parallelism. Args: rank (int): The rank of current process. world_size (int): Size of whole communication world. config (Config): Running configuration. data_parallel_size (int): Size of data parallel. pipeline_parallel_size (int): Size of pipeline parallel. tensor_parallel_size (int): Size of tensor parallel. """ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # reuse tensor parallel initializer code self._sequence_initializer = Initializer_Tensor(*args, **kwargs) self._sequence_dp_initializer = Initializer_Sequence_DP(*args, **kwargs) def init_dist_group(self): """Initialize Sequence parallel process groups and assign local_ranks and groups to each gpu. Sequence parallelism requires 2 process groups. The first is for model forward where several processes exchange partial query, key and value embedding to compute self attention values. The second is for all-reduce to synchronize the model parameters. Returns: List[Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode)]: A Sequence parallelism's information in list of tuples. """ parallel_setting = [] local_rank, group_world_size, process_group, cpu_grop, ranks_in_group, mode = \ self._sequence_initializer.init_dist_group() # change mode to sequence mode = ParallelMode.SEQUENCE parallel_setting.append((local_rank, group_world_size, process_group, cpu_grop, ranks_in_group, mode)) parallel_setting.append(self._sequence_dp_initializer.init_dist_group()) return parallel_setting
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch from torch import Tensor from colossalai.context.parallel_mode import ParallelMode class SeedManager: """This class is a manager of all random seeds involved in the system. 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): self._current_mode = None self._seeds = dict() self._seed_states = dict() @property def current_mode(self): return self._current_mode @property def seeds(self): return self._seeds @property def seed_states(self): return self._seed_states def set_state(self, parallel_mode: ParallelMode, state: Tensor): """Sets the state of the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. state (:class:`torch.Tensor`): the state to be set. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not found in the seed manager. """ assert parallel_mode in self._seed_states, f'Parallel mode {parallel_mode} is not found in the seed manager' self._seed_states[parallel_mode] = state def set_mode(self, parallel_mode: ParallelMode): """Sets the current mode of the seed manager. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. """ if self.current_mode: # save the current state for current mode self._seed_states[self._current_mode] = torch.cuda.get_rng_state() # set the new state for new mode self._current_mode = parallel_mode torch.cuda.set_rng_state(self._seed_states[parallel_mode]) def add_seed(self, parallel_mode: ParallelMode, seed: int, overwrtie: bool = False): """Adds a seed to the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. seed (int): The seed to be added. overwrtie (bool, optional): Whether allows to overwrite the seed that has been set already Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode` or the seed for `parallel_mode` has been added. """ assert isinstance(parallel_mode, ParallelMode), 'A valid ParallelMode must be provided' if overwrtie is False: assert parallel_mode not in self._seed_states, f'The seed for {parallel_mode} has been added' elif parallel_mode in self._seed_states: print(f"Warnning: {parallel_mode} seed has been overwritten.", flush=True) current_state = torch.cuda.get_rng_state() torch.cuda.manual_seed(seed) self._seed_states[parallel_mode] = torch.cuda.get_rng_state() self._seeds[parallel_mode] = seed torch.cuda.set_rng_state(current_state) def reset(self): self._current_mode = None self._seeds = dict() self._seed_states = dict()
#!/usr/bin/env python # -*- encoding: utf-8 -*- import functools from contextlib import contextmanager import torch.cuda from torch import Tensor from .seed_manager import SeedManager from ..parallel_mode import ParallelMode _SEED_MANAGER = SeedManager() def get_seeds(): """Returns the seeds of the seed manager. Returns: dict: The seeds of the seed manager. """ return _SEED_MANAGER.seeds def get_states(copy=False): """Returns the seed states of the seed manager. Returns: dict: The seed states of the seed manager. """ states = _SEED_MANAGER.seed_states if copy: new_states = dict() for parallel_mode, state in states.items(): new_states[parallel_mode] = state.clone() return new_states else: return _SEED_MANAGER.seed_states def get_current_mode(): """Returns the current mode of the seed manager. Returns: :class:`torch.ByteTensor`: The current mode of the seed manager. """ return _SEED_MANAGER.current_mode def add_seed(parallel_mode: ParallelMode, seed: int, overwrite: bool = False): """Adds a seed to the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. seed (int): The seed to be added Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not an instance of :class:`colossalai.context.ParallelMode` or the seed for `parallel_mode` has been added. 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>`_. """ _SEED_MANAGER.add_seed(parallel_mode, seed, overwrite) def set_mode(parallel_mode: ParallelMode): """Sets the current mode of the seed manager. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. 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>`_. """ _SEED_MANAGER.set_mode(parallel_mode) def set_seed_states(parallel_mode: ParallelMode, state: Tensor): """Sets the state of the seed manager for `parallel_mode`. Args: parallel_mode (:class:`colossalai.context.ParallelMode`): The chosen parallel mode. state (:class:`torch.Tensor`): the state to be set. Raises: AssertionError: Raises an AssertionError if `parallel_mode` is not found in the seed manager. """ _SEED_MANAGER.set_state(parallel_mode, state) def sync_states(): current_mode = get_current_mode() current_states = torch.cuda.get_rng_state() set_seed_states(current_mode, current_states) @contextmanager def seed(parallel_mode: ParallelMode): """ A context for seed switch Examples: >>> with seed(ParallelMode.DATA): >>> output = F.dropout(input) 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>`_. """ try: # set to new mode current_mode = _SEED_MANAGER.current_mode yield _SEED_MANAGER.set_mode(parallel_mode) finally: # recover _SEED_MANAGER.set_mode(current_mode) def with_seed(func, parallel_mode: ParallelMode): """ A function wrapper which executes the function with a specified seed. Examples: >>> # use with decorator >>> @with_seed(ParallelMode.DATA) >>> def forward(input): >>> return F.dropout(input) >>> out = forward(input) >>> # OR use it inline >>> def forward(input): >>> return F.dropout(input) >>> wrapper_forward = with_seed(forward, ParallelMode.DATA) >>> out = wrapped_forward(input) 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>`_. """ @functools.wraps(func) def wrapper(*args, **kwargs): # switch mode current_mode = _SEED_MANAGER.current_mode _SEED_MANAGER.set_mode(parallel_mode) # exec func out = func(*args, **kwargs) # recover state _SEED_MANAGER.set_mode(current_mode) return out return wrapper def moe_set_seed(seed): if torch.cuda.is_available(): from colossalai.core import global_context as gpc global_rank = gpc.get_global_rank() diff_seed = seed + global_rank add_seed(ParallelMode.TENSOR, diff_seed, True) print(f"moe seed condition: {global_rank} with tensor seed {diff_seed}", flush=True) def reset_seeds(): _SEED_MANAGER.reset()
from ._helper import (seed, set_mode, with_seed, add_seed, get_seeds, get_states, get_current_mode, set_seed_states, sync_states, moe_set_seed, reset_seeds) __all__ = [ 'seed', 'set_mode', 'with_seed', 'add_seed', 'get_seeds', 'get_states', 'get_current_mode', 'set_seed_states', 'sync_states', 'moe_set_seed', 'reset_seeds' ]
from enum import Enum from typing import Optional import torch from typing import Union from colossalai.gemini.gemini_context import GeminiMemoryManager def sizeof_tensor(tensor: torch.Tensor): return tensor.numel() * tensor.element_size() class TensorState(Enum): FREE = 0 HOLD = 1 HOLD_AFTER_FWD = 2 HOLD_AFTER_BWD = 3 COMPUTE = 4 class StatefulTensor(object): """A Structure stores a Torch Tensor and labeled states. Inspired from the paper: PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management https://arxiv.org/abs/2108.05818 """ # Global Stateful Tensor Manager GST_MGR = GeminiMemoryManager(TensorState) def __init__(self, maybe_tensor: Optional[torch.Tensor], state: Optional[TensorState] = TensorState.HOLD) -> None: self._state = state self._payload = None self._payload_size = 0 # byte size of current payload StatefulTensor.GST_MGR.register_new_instance() if self._state == TensorState.FREE: # when the state is free, payload should be None assert maybe_tensor is None, f"payload has to None if state is {self._state}" else: # otherwise, payload should not be None assert maybe_tensor is not None, f"payload can't be None if state is {self._state}" self._payload = maybe_tensor self._payload_size = sizeof_tensor(maybe_tensor) self.__trans_state_update(TensorState.FREE, state) def data_ptr(self): if self._payload is None: return 0 # if a tensor has no storage, 0 should be returned return self._payload.data_ptr() def set_null(self) -> None: # notice that free stateful tensor do not need to become null again if self.state != TensorState.FREE: self.__trans_state_update(self.state, TensorState.FREE) self.__release() def is_null(self) -> bool: if self.state == TensorState.FREE: # check sanity here assert self.payload is None return True return False def trans_state(self, state: TensorState) -> None: if self.state == TensorState.FREE: # free stateful tensor can't change state assert state == TensorState.FREE, "Free stateful tensor can't change to other states" return self.__trans_state_update(self.state, state) if state == TensorState.FREE: self.__release() else: self._state = state def move_to(self, device: Union[torch.device, int]): assert self.state is not TensorState.FREE, "Can't move free stateful tensor" if not isinstance(device, torch.device): to_device = torch.device('cuda', device) else: to_device = device from_device_type = self.device.type if from_device_type == to_device.type: # from device == to device return # update manager's information self.__trans_device_update(from_device_type, to_device.type) self.payload.data = self.payload.data.to(to_device) def payload_copy(self, tensor) -> None: self._payload.view(-1).copy_(tensor.view(-1)) def payload_reset(self, tensor) -> None: assert tensor is not None, "Can't reset None for stateful tensors, please use set_null() instead" if self.payload is not None: # release old payload self.__trans_state_update(self.state, TensorState.FREE) else: # otherwise, set the state to HOLD for new payload self._state = TensorState.HOLD del self._payload self._payload = tensor self._payload_size = sizeof_tensor(tensor) # record new payload self.__trans_state_update(TensorState.FREE, self.state) def payload_relay(self, rhs): # relay the payload of rhs to current stateful tensor # can't support null relay right now assert not rhs.is_null() # now this function only support stateful tensor that has zero-length payload # because it doesn't require memory manager updating # you can extend this function by yourself assert self.payload_size == 0 self._payload = rhs.payload self._payload_size = rhs.payload_size self._state = TensorState.HOLD self.__trans_state_update(rhs.state, TensorState.HOLD) rhs.__release() @property def payload(self) -> Optional[torch.Tensor]: return self._payload @property def payload_size(self) -> int: return self._payload_size @property def state(self) -> TensorState: return self._state @property def device(self) -> torch.device: return self._payload.device @property def dtype(self) -> torch.dtype: return self._payload.dtype @property def shape(self): return self._payload.shape def to(self, device: torch.device): raise RuntimeError("Use move_to(...) instead of call .to() on StatefulTensor") def to_(self, device: torch.device): raise RuntimeError("Use move_to(...) instead of call .to_() on StatefulTensor") def __release(self): # release current payload # shouldn't be visible to users self._state = TensorState.FREE self._payload = None self._payload_size = 0 def __trans_state_update(self, from_state: TensorState, to_state: TensorState): """Update global manager when changing the state of a tensor """ manager = StatefulTensor.GST_MGR size = self.payload_size device_type = self.device.type if from_state != TensorState.FREE: manager.state_mem[device_type][from_state] -= size else: # when from_state is FREE, the tensor is new to manager # we should add its memory manager.total_mem[device_type] += size if to_state != TensorState.FREE: manager.state_mem[device_type][to_state] += size else: # when to_state is FREE, the tensor will be deleted soon # we should sub its memory manager.total_mem[device_type] -= size def __trans_device_update(self, from_type: str, to_type: str): """Update global manager when changing the device of a tensor """ manager = StatefulTensor.GST_MGR size = self.payload_size state = self.state # update aggregated information manager.total_mem[from_type] -= size manager.total_mem[to_type] += size # update the information of each state manager.state_mem[from_type][state] -= size manager.state_mem[to_type][state] += size def __del__(self): self.set_null() StatefulTensor.GST_MGR.delete_instance() del self
from enum import EnumMeta class GeminiMemoryManager(object): def __init__(self, states_cls: EnumMeta): super().__init__() self.states_cls = states_cls self._cnter = 0 # the counter of instances self.total_mem = dict() self.state_mem = dict() self.state_mem['cpu'] = dict() self.state_mem['cuda'] = dict() self.reset() @property def total_number(self): return self._cnter def reset(self): self._cnter = 0 # the counter of instances self.total_mem['cpu'] = 0 # memory occupation of instances in cpu self.total_mem['cuda'] = 0 # memory of occupation of instances in cuda # memory conditions for all states for state in self.states_cls: self.state_mem['cpu'][state] = 0 self.state_mem['cuda'][state] = 0 def register_new_instance(self): self._cnter += 1 def delete_instance(self): self._cnter -= 1 def print_info(self): print(f"Total number: {self.total_number}", f"Total CPU memory occupation: {self.total_mem['cpu']}", f"Total CUDA memory occupation: {self.total_mem['cuda']}\n", sep='\n') for state in self.states_cls: print(f"{state}: CPU memory occupation: {self.state_mem['cpu'][state]}", f"{state}: CUDA memory occupation: {self.state_mem['cuda'][state]}\n", sep='\n')
import functools from abc import ABC, abstractmethod from time import time from typing import Dict, List, Optional, Tuple, Type import torch from colossalai.gemini.chunk import Chunk, ChunkManager from colossalai.gemini.memory_tracer import ChunkMemStatsCollector from colossalai.utils import get_current_device from colossalai.utils.memory import colo_device_memory_capacity class PlacementPolicy(ABC): need_mem_stats: bool = False def __init__(self, chunk_manager: ChunkManager, mem_stats_collector: Optional[ChunkMemStatsCollector] = None) -> None: self.chunk_manager = chunk_manager self.mem_stats_collector: Optional[ChunkMemStatsCollector] = mem_stats_collector @abstractmethod def evict_tensors(self, can_evict_chunks: List[Chunk], **kwargs) -> Tuple[int, float]: raise NotImplementedError @staticmethod def get_default_device() -> torch.device: return torch.device('cpu') class CPUPlacementPolicy(PlacementPolicy): def __init__(self, chunk_manager: ChunkManager, mem_stats_collector: Optional[ChunkMemStatsCollector] = None) -> None: super().__init__(chunk_manager, mem_stats_collector=mem_stats_collector) def evict_tensors(self, can_evict_chunks: List[Chunk], **kwargs) -> Tuple[int, float]: volume = 0 start = time() for chunk in can_evict_chunks: self.chunk_manager.release_chunk(chunk) self.chunk_manager.move_chunk(chunk, torch.device('cpu')) volume += chunk.chunk_mem return volume, time() - start class CUDAPlacementPolicy(PlacementPolicy): def __init__(self, chunk_manager: ChunkManager, mem_stats_collector: Optional[ChunkMemStatsCollector] = None) -> None: assert torch.cuda.is_available(), 'Cannot use CUDATensorPlacementPolicy when CUDA is not available' super().__init__(chunk_manager, mem_stats_collector=mem_stats_collector) def evict_tensors(self, can_evict_chunks: List[Chunk], **kwargs) -> Tuple[int, float]: return 0, 0 @staticmethod def get_default_device() -> torch.device: return get_current_device() class AutoPlacementPolicy(PlacementPolicy): need_mem_stats: bool = True # model data will use 1-_warmup_non_model_data_ratio CUDA memory in warmup phase # you can set them by AutoPlacementPolicy.set_warmup_non_model_data_ratio() # and AutoPlacementPolicy.set_steady_cuda_cap_ratio() _warmup_non_model_data_ratio: float = 0.8 _steady_cuda_cap_ratio: float = 0.9 def __init__(self, chunk_manager: ChunkManager, mem_stats_collector: Optional[ChunkMemStatsCollector] = None) -> None: super().__init__(chunk_manager, mem_stats_collector=mem_stats_collector) def evict_tensors(self, can_evict_chunks: List[Chunk], cuda_demand: int = 0, warmup: bool = True, compute_list: Optional[List[Tuple[Chunk, ...]]] = None, compute_idx: int = 0, **kwargs) -> Tuple[int, float]: """ Evict tensors from CUDA device. Args: can_evict_chunks (List[StatefulTensor]): the list of tensors that can be evicted. cuda_demand (int, optional): the volume of data needed on cuda device. Defaults to 0. warmup (bool, optional): a flag indicates whether in the phase of warmup. Defaults to True. compute_list (List[StatefulTensor], optional): TODO. Defaults to []. compute_idx (int, optional): the idx of computing device. Defaults to 0. Raises: RuntimeError: Returns: int: the volume of memory that is evicted """ start = time() cuda_capacity = colo_device_memory_capacity(get_current_device()) used_cuda_model_data = self.chunk_manager.total_mem['cuda'] if warmup: # We designate a part of CUDA memory for model data in warmup iterations. max_cuda_non_model_data_per_period = cuda_capacity * AutoPlacementPolicy._warmup_non_model_data_ratio else: # max non-model-data cuda memory consumption of this sampling moment and the next sampling moment. max_cuda_non_model_data_per_period = self.mem_stats_collector.next_period_non_model_data_usage('cuda') cuda_capacity *= AutoPlacementPolicy._steady_cuda_cap_ratio total_cuda_model_data = cuda_capacity - max_cuda_non_model_data_per_period avail_cuda_model_data = total_cuda_model_data - used_cuda_model_data freed_cuda_model_data = 0 if avail_cuda_model_data < cuda_demand: # Move cuda_demand - avail_cuda_model_data volume of tensors # to_free_cuda_model_data = cuda_demand - avail_cuda_model_data to_free_cuda_model_data = cuda_demand - avail_cuda_model_data to_free_chunks = can_evict_chunks if not warmup: to_free_chunks = self._sort_can_evict_chunks(tuple(to_free_chunks), compute_idx, tuple(compute_list)) # print(self._sort_can_evict_chunks.cache_info()) for chunk in to_free_chunks: if freed_cuda_model_data >= to_free_cuda_model_data: break self.chunk_manager.release_chunk(chunk) self.chunk_manager.move_chunk(chunk, torch.device('cpu')) freed_cuda_model_data += chunk.chunk_mem if freed_cuda_model_data < to_free_cuda_model_data: raise RuntimeError(f"Adjust layout failed! No enough CUDA memory! " f"Need {to_free_cuda_model_data}, freed {freed_cuda_model_data}") return freed_cuda_model_data, time() - start @staticmethod @functools.lru_cache(maxsize=None) def _sort_can_evict_chunks(can_evict_chunks: tuple, compute_idx: int, compute_list: tuple) -> list: next_compute_idx = {chunk: len(compute_list) for chunk in can_evict_chunks} for i in range(len(compute_list) - 1, compute_idx, -1): for chunk in compute_list[i]: if chunk in next_compute_idx: next_compute_idx[chunk] = i next_compute_idx = sorted(next_compute_idx.items(), key=lambda pair: pair[1], reverse=True) return [t for (t, idx) in next_compute_idx] @staticmethod def set_warmup_non_model_data_ratio(ratio: float) -> None: ratio = float(ratio) assert 0.0 < ratio < 1.0 AutoPlacementPolicy._warmup_non_model_data_ratio = ratio @staticmethod def set_steady_cuda_cap_ratio(ratio: float) -> None: ratio = float(ratio) assert 0.0 < ratio < 1.0 AutoPlacementPolicy._steady_cuda_cap_ratio = ratio class ConstPlacementPolicy(PlacementPolicy): need_mem_stats: bool = False _accessed_memory_boundary = 512 * 1024**2 def __init__(self, chunk_manager: ChunkManager, mem_stats_collector: Optional[ChunkMemStatsCollector] = None) -> None: super().__init__(chunk_manager, mem_stats_collector=mem_stats_collector) def evict_tensors(self, can_evict_chunks: List[Chunk], cuda_demand: int = 0, warmup: bool = True, compute_list: Optional[List[Tuple[Chunk, ...]]] = None, compute_idx: int = 0, **kwargs) -> Tuple[int, float]: """ See the docstrings in the class `AutoPlacementPolicy`. """ start = time() used_accessed_memory = self.chunk_manager.accessed_mem avail_accessed_memory = ConstPlacementPolicy._accessed_memory_boundary - used_accessed_memory freed_accessed_memory = 0 if avail_accessed_memory < cuda_demand: to_free_memory = cuda_demand - avail_accessed_memory to_free_chunks = can_evict_chunks if not warmup: # sort all chunks to_free_chunks = self._sort_can_evict_chunks(tuple(to_free_chunks), compute_idx, tuple(compute_list)) for chunk in to_free_chunks: if freed_accessed_memory >= to_free_memory: break self.chunk_manager.release_chunk(chunk) self.chunk_manager.move_chunk(chunk, torch.device('cpu')) freed_accessed_memory += chunk.chunk_mem if freed_accessed_memory < to_free_memory: raise RuntimeError(f"Adjust layout failed! No enough CUDA memory! " f"Need {to_free_memory}, freed {freed_accessed_memory}") return freed_accessed_memory, time() - start @staticmethod @functools.lru_cache(maxsize=None) def _sort_can_evict_chunks(can_evict_chunks: tuple, compute_idx: int, compute_list: tuple) -> list: next_compute_idx = {chunk: len(compute_list) for chunk in can_evict_chunks} for i in range(len(compute_list) - 1, compute_idx, -1): for chunk in compute_list[i]: if chunk in next_compute_idx: next_compute_idx[chunk] = i next_compute_idx = sorted(next_compute_idx.items(), key=lambda pair: pair[1], reverse=True) return [t for (t, idx) in next_compute_idx] @staticmethod def set_const_memory_boundary(cuda_memory_mb: int) -> None: boundary = int(cuda_memory_mb * 1024**2) assert boundary > 0 ConstPlacementPolicy._accessed_memory_boundary = boundary class PlacementPolicyFactory: policies: Dict[str, Type[PlacementPolicy]] = { 'cpu': CPUPlacementPolicy, 'cuda': CUDAPlacementPolicy, 'auto': AutoPlacementPolicy, 'const': ConstPlacementPolicy } @staticmethod def create(policy_name: str) -> Type[PlacementPolicy]: if policy_name not in PlacementPolicyFactory.policies: raise TypeError(f"Unknown tensor placement policy {policy_name}") return PlacementPolicyFactory.policies[policy_name] @staticmethod def get_policy_names(): return tuple(PlacementPolicyFactory.policies.keys()) @staticmethod def get_default_device(policy_name: str) -> torch.device: policy_cls = PlacementPolicyFactory.create(policy_name) return policy_cls.get_default_device()
import functools from time import time from typing import List, Optional, Tuple import torch from colossalai.gemini.chunk import Chunk, ChunkManager from colossalai.gemini.memory_tracer import MemStats from .memory_tracer import ChunkMemStatsCollector from .placement_policy import PlacementPolicyFactory class GeminiManager: """ Stateful Tensor Manager, inspired from PatrickStar PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management https://arxiv.org/abs/2108.05818 Args: 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. chunk_manager (ChunkManager): A ``ChunkManager`` instance. memstats (MemStats, optional): a mem stats collected by a runtime mem tracer. if None then GeminiManager will collect it during a warmup iteration. """ def __init__(self, placement_policy: str, chunk_manager: ChunkManager, memstats: Optional[MemStats] = None) -> None: assert placement_policy in PlacementPolicyFactory.get_policy_names() self.policy_name = placement_policy policy_cls = PlacementPolicyFactory.create(placement_policy) self._chunk_manager = chunk_manager self._premade_memstats_ = memstats is not None self._memstats = memstats self._mem_stats_collector = ChunkMemStatsCollector(chunk_manager, self._memstats) if policy_cls.need_mem_stats else None self._placement_policy = policy_cls(chunk_manager, self._mem_stats_collector) self._compute_list: List[Tuple[Chunk, ...]] = [] self._compute_idx: int = -1 self._h2d_volume = 0 self._d2h_volume = 0 self._layout_time = 0 self._evict_time = 0 self._warmup = True self._comp_cuda_demand_time = 0 def reset_attributes(self): self._compute_idx = -1 self._h2d_volume = 0 self._d2h_volume = 0 self._layout_time = 0 self._evict_time = 0 self._comp_cuda_demand_time = 0 @property def need_warmup(self) -> bool: return self.policy_name in ('auto', 'const') def is_warmup(self): return self._warmup def memstats(self): """memstats get the memory statistics during training. The stats could be collected by a runtime memory tracer, or collected by the GeminiManager. Note, for the latter, you can not access the memstats before warmup iteration finishes. """ if self._premade_memstats_: return self._memstats else: assert not self._warmup, "Gemini Manager has memstats after warm up! Now is during warmup." return self._mem_stats_collector._memstats def pre_iter(self, *args): if self._mem_stats_collector and self._warmup: self._mem_stats_collector.start_collection() def post_iter(self): """This function must be called when each iteration finishes """ if self._mem_stats_collector and self._warmup: self._mem_stats_collector.finish_collection() self._warmup = False self.reset_attributes() def adjust_layout(self, chunks: Tuple[Chunk, ...]) -> None: """ Adjust the layout of stateful tensors according to the information provided by mem_stats_collector, which should belongs to a Sharded Model. """ # find stateful tensor in state COMPUTE start = time() self._record_chunks_order(chunks) cuda_demand, hold_cuda_tensor_list = self._get_layout_info(self._compute_idx, self._warmup, chunks) self._layout_time += time() - start vol, evict_time = self._placement_policy.evict_tensors(can_evict_chunks=hold_cuda_tensor_list, cuda_demand=cuda_demand, warmup=self._warmup, compute_list=self._compute_list, compute_idx=self._compute_idx) self._d2h_volume += vol self._evict_time += evict_time # move COMPUTE tensors to CUDA self._h2d_volume += cuda_demand @functools.lru_cache(maxsize=None) def _get_layout_info(self, compute_idx: int, warmup: bool, chunks: Tuple[Chunk, ...]): start = time() cuda_demand = 0 for chunk in chunks: if chunk.device_type == 'cuda': if chunk.is_gathered: pass else: cuda_demand += chunk.chunk_mem - chunk.shard_mem elif chunk.device_type == 'cpu': cuda_demand += chunk.chunk_mem else: raise RuntimeError self._comp_cuda_demand_time += time() - start can_evict_chunks = self._chunk_manager.get_cuda_movable_chunks() return cuda_demand, can_evict_chunks def _record_chunks_order(self, chunks: Tuple[Chunk, ...]) -> None: self._compute_idx += 1 if self._warmup and self._placement_policy.need_mem_stats: self._compute_list.append(chunks) @property def default_device(self): return self._placement_policy.get_default_device() def sample_overall_data(self): if self._mem_stats_collector: self._mem_stats_collector.sample_overall_data() def record_model_data_volume(self): if self._mem_stats_collector: self._mem_stats_collector.record_model_data_volume() @property def chunk_manager(self): return self._chunk_manager @property def cuda_margin_mem(self) -> Optional[float]: if self._mem_stats_collector: return self._mem_stats_collector.cuda_margin_mem return None @property def is_cuda_margin_mem_avail(self) -> bool: return self._placement_policy.need_mem_stats @staticmethod def get_default_device(policy_name: str) -> torch.device: return PlacementPolicyFactory.get_default_device(policy_name)
from .chunk import ChunkManager, TensorInfo, TensorState, search_chunk_configuration from .gemini_mgr import GeminiManager from .stateful_tensor_mgr import StatefulTensorMgr from .tensor_placement_policy import TensorPlacementPolicyFactory __all__ = [ 'StatefulTensorMgr', 'TensorPlacementPolicyFactory', 'GeminiManager', 'TensorInfo', 'TensorState', 'ChunkManager', 'search_chunk_configuration' ]
import torch from colossalai.gemini.stateful_tensor import StatefulTensor from typing import Union, Tuple def is_storage_empty(tensor: torch.Tensor) -> bool: return tensor.storage().size() == 0 def free_storage(tensor: torch.Tensor) -> None: if not is_storage_empty(tensor): tensor.storage().resize_(0) def alloc_storage(tensor: torch.Tensor) -> None: if is_storage_empty(tensor): tensor.storage().resize_(tensor.numel()) def colo_tensor_mem_usage(tensor: Union[torch.Tensor, StatefulTensor]) -> Tuple[int, int]: if isinstance(tensor, StatefulTensor): t = tensor.payload elif isinstance(tensor, torch.Tensor): t = tensor else: return 0, 0 cuda_use, cpu_use = 0, 0 mem_use = t.storage().size() * t.element_size() if t.device.type == 'cuda': cuda_use += mem_use elif t.device.type == 'cpu': cpu_use += mem_use return cuda_use, cpu_use def colo_model_data_tensor_move(src_t: Union[StatefulTensor, torch.Tensor], tgt_t: Union[StatefulTensor, torch.Tensor]) -> None: """ A colossal API for model data tensor move. The src and target tensors could be resident on both CPU and GPU. NOTE() The source tensor payload will be removed after this function. The function will record the communication volume between CPU and GPU. Args: src_t (Union[StatefulTensor, torch.Tensor]): source tensor tgt_t (Union[StatefulTensor, torch.Tensor]): target tensor """ if isinstance(src_t, StatefulTensor): src_t_payload = src_t.payload else: src_t_payload = src_t.data src_dev = src_t_payload.device if isinstance(tgt_t, StatefulTensor): tgt_t_payload = tgt_t.payload else: tgt_t_payload = tgt_t.data tgt_t_payload.copy_(src_t_payload) # remove payload of src_t if isinstance(src_t, StatefulTensor): src_t.set_null() else: src_t.data = torch.empty(0, device=src_dev, dtype=src_t_payload.dtype) def colo_model_data_tensor_move_inline(t: Union[StatefulTensor, torch.Tensor], target_device: Union[torch.device, int]) -> None: """ move a tensor to the target_device Args: t (Union[StatefulTensor, torch.Tensor]): the tensor be moved target_device: a traget device, if type is int, it the index of cuda card. """ if not isinstance(target_device, torch.device): target_device = torch.device(f'cuda:{target_device}') if isinstance(t, torch.Tensor): t.data = t.data.to(target_device) elif isinstance(t, StatefulTensor): t.move_to(target_device) else: raise TypeError(f'colo_model_data_tensor_move_inline dose not accept type {type(t)}') def colo_model_data_move_to_cpu(t: Union[StatefulTensor, torch.Tensor]) -> None: """colo_model_data_move_to_cpu move a model data tensor from gpu to cpu Args: t (Union[StatefulTensor, torch.Tensor]): _description_ """ # TODO() optimize the tensor moving with non-blocking if isinstance(t, torch.Tensor): t.data = t.data.cpu() elif isinstance(t, StatefulTensor): t.move_to(torch.device('cpu')) else: raise TypeError(f'colo_model_data_move_to_cpu dose not accept type {type(t)}') def colo_model_tensor_clone(t: Union[StatefulTensor, torch.Tensor], target_device: torch.device) -> torch.Tensor: """ Clone a model data tensor Args: t (Union[StatefulTensor, torch.Tensor]): a model data tensor target_device (torch.device): the target device Returns: torch.Tensor: a cloned torch tensor """ # TODO() rename this function colo_model_data_tensor_move_inline(t, target_device) t_payload = t.payload if isinstance(t, StatefulTensor) else t return t_payload
from abc import ABC, abstractmethod from time import time from typing import List, Optional import torch from colossalai.utils import get_current_device from colossalai.utils.memory import colo_device_memory_capacity from colossalai.gemini.tensor_utils import colo_model_data_tensor_move_inline, colo_tensor_mem_usage from colossalai.gemini.stateful_tensor import StatefulTensor from colossalai.gemini.memory_tracer import MemStatsCollector from typing import Type import functools class TensorPlacementPolicy(ABC): def __init__(self, device: Optional[torch.device], mem_stats_collector: Optional[MemStatsCollector] = None) -> None: self.device: Optional[torch.device] = device self.mem_stats_collector: Optional[MemStatsCollector] = mem_stats_collector @abstractmethod def evict_tensors(self, hold_cuda_tensor_list: List[StatefulTensor], **kwargs) -> None: raise NotImplementedError class CPUTensorPlacementPolicy(TensorPlacementPolicy): def __init__(self, mem_stats_collector: Optional[MemStatsCollector] = None) -> None: super().__init__(torch.device('cpu'), mem_stats_collector=mem_stats_collector) def evict_tensors(self, hold_cuda_tensor_list: List[StatefulTensor], **kwargs) -> int: volume = 0 for t in hold_cuda_tensor_list: colo_model_data_tensor_move_inline(t, self.device) volume += t.payload.numel() * t.payload.element_size() return volume, 0 class CUDATensorPlacementPolicy(TensorPlacementPolicy): def __init__(self, mem_stats_collector: Optional[MemStatsCollector] = None) -> None: assert torch.cuda.is_available(), 'Cannot use CUDATensorPlacementPolicy when CUDA is not available' super().__init__(get_current_device(), mem_stats_collector=mem_stats_collector) def evict_tensors(self, hold_cuda_tensor_list: List[StatefulTensor], **kwargs) -> int: return 0, 0 class AutoTensorPlacementPolicy(TensorPlacementPolicy): def __init__(self, mem_stats_collector: Optional[MemStatsCollector] = None) -> None: super().__init__(None, mem_stats_collector=mem_stats_collector) # model data will use 1-self._warmup_non_model_data_ratio CUDA memory in warmup phase # TODO(ver217): make these args configurable self._warmup_non_model_data_ratio: float = 0.8 self._steady_cuda_cap_ratio: float = 0.9 def evict_tensors(self, hold_cuda_tensor_list: List[StatefulTensor], cuda_demand: int = 0, warmup: bool = True, compute_list: List[StatefulTensor] = [], compute_idx: int = 0, **kwargs) -> int: """ Evict tensors from CUDA device. Args: hold_cuda_tensor_list (List[StatefulTensor]): the list of tensor in state of HOLD-like cuda_demand (int, optional): the volume of data needed on cuda device. Defaults to 0. warmup (bool, optional): a flag indicates whether in the phase of warmup. Defaults to True. compute_list (List[StatefulTensor], optional): TODO. Defaults to []. compute_idx (int, optional): the idx of computing device. Defaults to 0. Raises: RuntimeError: Returns: int: the volume of memory that is evicted """ start = time() cuda_capacity = colo_device_memory_capacity(get_current_device()) used_cuda_model_data = StatefulTensor.GST_MGR.total_mem['cuda'] if warmup: # We designate a part of CUDA memory for model data in warmup iterations. max_cuda_non_model_data_per_period = cuda_capacity * self._warmup_non_model_data_ratio else: # max non-model-data cuda memory consumption of this sampling moment and the next sampling moment. max_cuda_non_model_data_per_period = self.mem_stats_collector.next_period_non_model_data_usage('cuda') cuda_capacity *= self._steady_cuda_cap_ratio total_cuda_model_data = cuda_capacity - max_cuda_non_model_data_per_period avail_cuda_model_data = total_cuda_model_data - used_cuda_model_data freed_cuda_model_data = 0 end = time() if avail_cuda_model_data < cuda_demand: # Move cuda_demand - avail_cuda_model_data volume of tensors # to_free_cuda_model_data = cuda_demand - avail_cuda_model_data to_free_cuda_model_data = cuda_demand - avail_cuda_model_data to_free_tensor_list = hold_cuda_tensor_list if not warmup: to_free_tensor_list = self._sort_hold_cuda_tensors(tuple(hold_cuda_tensor_list), compute_idx, tuple(compute_list)) # print(self._sort_hold_cuda_tensors.cache_info()) end = time() for t in to_free_tensor_list: if freed_cuda_model_data >= to_free_cuda_model_data: break freed_cuda_model_data += t.payload_size colo_model_data_tensor_move_inline(t, torch.device('cpu')) if freed_cuda_model_data < to_free_cuda_model_data: raise RuntimeError( f"Adjust layout failed! No enough CUDA memory! Need {to_free_cuda_model_data}, freed {freed_cuda_model_data}" ) return freed_cuda_model_data, end - start @staticmethod @functools.lru_cache(maxsize=None) def _sort_hold_cuda_tensors(hold_cuda_tensors: tuple, compute_idx: int, compute_list: tuple) -> list: next_compute_idx = {t: len(compute_list) for t in hold_cuda_tensors} for i in range(len(compute_list) - 1, compute_idx, -1): if compute_list[i] in next_compute_idx: next_compute_idx[compute_list[i]] = i next_compute_idx = sorted(next_compute_idx.items(), key=lambda pair: pair[1], reverse=True) return [t for (t, idx) in next_compute_idx] class TensorPlacementPolicyFactory: @staticmethod def create(policy_name: str) -> Type[TensorPlacementPolicy]: if policy_name == 'cpu': return CPUTensorPlacementPolicy elif policy_name == 'cuda': return CUDATensorPlacementPolicy elif policy_name == 'auto': return AutoTensorPlacementPolicy else: raise TypeError(f"Unknown tensor placement policy {policy_name}")
import functools import torch import types from colossalai.utils.cuda import get_current_device from colossalai.gemini.tensor_utils import colo_model_data_tensor_move_inline, colo_tensor_mem_usage from colossalai.gemini.stateful_tensor import StatefulTensor, TensorState from colossalai.gemini.tensor_placement_policy import TensorPlacementPolicy from typing import List from colossalai.logging import get_dist_logger from time import time class StatefulTensorMgr(object): """ Stateful Tensor Manager, inspired from PatrickStar PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management https://arxiv.org/abs/2108.05818 """ def __init__(self, tensor_placement_policy: TensorPlacementPolicy) -> None: self._tensor_placement_policy: TensorPlacementPolicy = tensor_placement_policy self._stateful_tensor_list: List[StatefulTensor] = [] self._compute_list: List[StatefulTensor] = [] self._compute_idx: int = -1 self._cpu_gpu_move_volume = 0 self._layout_time = 0 self._evict_time = 0 self._warmup = True def register_stateful_tensor_list(self, tensor_list: List[StatefulTensor]) -> None: assert self._stateful_tensor_list == [], "Can't register stateful tensors for manager twice" self._stateful_tensor_list = tensor_list for t in self._stateful_tensor_list: assert isinstance(t, StatefulTensor) t.trans_state = types.MethodType(functools.partial(self._trans_state, t.trans_state), t) def start_iter(self): pass def finish_iter(self): """This function must be called when each iteration finishes """ self._warmup = False self._compute_idx = -1 self._cpu_gpu_move_volume = 0 self._layout_time = 0 self._evict_time = 0 def adjust_layout(self) -> None: """ Adjust the layout of statefuil tensor according to the information provided by mem_stats_collector, which should belongs to a Sharded Model. """ # find stateful tensor in state COMPUTE cuda_demand = StatefulTensor.GST_MGR.state_mem['cpu'][TensorState.COMPUTE] start = time() move_to_cuda_tensor_list, hold_cuda_tensor_list = self._get_layout_info(self._compute_idx, self._warmup) self._layout_time += time() - start vol, evict_time = self._tensor_placement_policy.evict_tensors(hold_cuda_tensor_list, cuda_demand=cuda_demand, warmup=self._warmup, compute_list=self._compute_list, compute_idx=self._compute_idx) self._cpu_gpu_move_volume += vol self._evict_time += evict_time # move COMPUTE tensors to CUDA self._cpu_gpu_move_volume += cuda_demand for t in move_to_cuda_tensor_list: colo_model_data_tensor_move_inline(t, get_current_device()) @property def cpu_gpu_move_volume(self): return self._cpu_gpu_move_volume def _trans_state(self, trans_state_func, stateful_tensor, state): trans_state_func(state) if state == TensorState.COMPUTE: self._compute_idx += 1 if self._warmup: self._compute_list.append(stateful_tensor) @functools.lru_cache(maxsize=None) def _get_layout_info(self, compute_idx: int, warmup: bool): move_to_cuda_tensor_list = [] hold_cuda_tensor_list = [] for tensor in self._stateful_tensor_list: if tensor.state == TensorState.FREE: continue if tensor.device.type == 'cuda': if tensor.state in [TensorState.HOLD, TensorState.HOLD_AFTER_BWD, TensorState.HOLD_AFTER_FWD]: hold_cuda_tensor_list.append(tensor) elif tensor.device.type == 'cpu': if tensor.state == TensorState.COMPUTE: move_to_cuda_tensor_list.append(tensor) else: raise RuntimeError return move_to_cuda_tensor_list, hold_cuda_tensor_list
from .chunk import Chunk, ChunkFullError, TensorInfo, TensorState from .manager import ChunkManager from .search_utils import classify_params_by_dp_degree, search_chunk_configuration from .utils import init_chunk_manager __all__ = ['Chunk', 'ChunkManager', 'classify_params_by_dp_degree', 'search_chunk_configuration', 'init_chunk_manager']
from dataclasses import dataclass from enum import Enum from typing import Dict, List, Optional import torch import torch.distributed as dist from colossalai.tensor import ProcessGroup as ColoProcessGroup from colossalai.utils import get_current_device class TensorState(Enum): FREE = 0 COMPUTE = 1 HOLD = 2 HOLD_AFTER_BWD = 3 READY_FOR_REDUCE = 4 STATE_TRANS = ((TensorState.FREE, TensorState.HOLD), (TensorState.FREE, TensorState.COMPUTE), (TensorState.HOLD, TensorState.FREE), (TensorState.HOLD, TensorState.COMPUTE), (TensorState.COMPUTE, TensorState.HOLD), (TensorState.COMPUTE, TensorState.HOLD_AFTER_BWD), (TensorState.HOLD_AFTER_BWD, TensorState.COMPUTE), (TensorState.HOLD_AFTER_BWD, TensorState.READY_FOR_REDUCE), (TensorState.READY_FOR_REDUCE, TensorState.HOLD)) @dataclass class TensorInfo: state: TensorState offset: int end: int class ChunkFullError(Exception): pass def is_storage_empty(tensor: torch.Tensor) -> bool: return tensor.storage().size() == 0 def free_storage(tensor: torch.Tensor) -> None: if not is_storage_empty(tensor): tensor.storage().resize_(0) def alloc_storage(tensor: torch.Tensor) -> None: if is_storage_empty(tensor): tensor.storage().resize_(tensor.numel()) class Chunk: _total_number = 0 def __init__(self, chunk_size: int, process_group: ColoProcessGroup, dtype: torch.dtype, init_device: Optional[torch.device] = None, cpu_shard_init: bool = False, keep_gathered: bool = False, pin_memory: bool = False) -> None: """ Chunk: A container owning a piece of contiguous memory space for tensors Here we use all-gather operation to gather the whole chunk. Currently, Chunk is exclusively used for DDP and ZeRO DDP and it doesn't support unused parameters. It is designed to make the full use of communication and PCIE bandwidth. Args: chunk_size (int): the number of elements in the chunk process_group (ColoProcessGroup): the process group of this chunk dtype (torch.dtype): the data type of the chunk init_device (torch.device): optional, During the chunk construction process, where the tensor is stored. The default value is None, which is the current GPU cpu_shard_init (bool): a flag indicates the local chunk shard is resident on CPU. keep_gathered (bool): optional, if True, this chunk is always gathered in CUDA memory pin_memory (bool): optional, if True, this chunk always has a shard copied in pinned CPU memory """ self.count_id = Chunk._total_number Chunk._total_number += 1 self.chunk_size = chunk_size self.utilized_size = 0 self.torch_pg = process_group.dp_process_group() self.pg_size = dist.get_world_size(self.torch_pg) self.pg_rank = dist.get_rank(self.torch_pg) # the chunk size should be divisible by the dp degree if not keep_gathered: assert chunk_size % self.pg_size == 0 self.shard_size = chunk_size // self.pg_size self.shard_begin = self.shard_size * self.pg_rank self.shard_end = self.shard_begin + self.shard_size self.valid_end = self.shard_size self.dtype = dtype device = init_device or get_current_device() # chunk_temp is a global chunk, which only exists during building the chunks. self.chunk_temp = torch.zeros(chunk_size, dtype=dtype, device=device) # keep all zero self.cuda_global_chunk = None # we force cuda_global_chunk located in CUDA # cuda local chunk, which is sharded on GPUs self.cuda_shard = None # cpu local chunk, which is sharded on CPUs self.cpu_shard = None # is the chunks gathers, which means chunks are duplicated on each process, # and we should use the cuda_global_chunk. self.is_gathered = True # configure the init device of the shard # no-offload default: fp16, fp32 -> CUDA # offload default: fp16, fp32 -> CPU self.shard_device = torch.device("cpu") if cpu_shard_init else get_current_device() self.chunk_mem = self.chunk_size * self.chunk_temp.element_size() self.shard_mem = self.chunk_mem // self.pg_size # each tensor is associated with a TensorInfo to track its meta info # (state, offset, end) self.tensors_info: Dict[torch.Tensor, TensorInfo] = {} # the total number of tensors in the chunk self.num_tensors = 0 # Record the number of tensors in different states self.tensor_state_cnter: Dict[TensorState, int] = dict() for state in TensorState: self.tensor_state_cnter[state] = 0 # If a chunk is kept gathered, # they are treated the same as that of the parameters in DDP during training. self.keep_gathered = keep_gathered if self.keep_gathered: pin_memory = False # since this chunk is gathered, it doesn't need to pin # if pin_memory is True, we allocate a piece of CPU pin-memory # for it all the time self.pin_memory = pin_memory # we introduce the paired chunk here # it refers to another chunk having the same parameters # but with different dtype(such as fp16_chunk.paired_chunk -> fp32_chunk self.paired_chunk = None # if this chunk is synchronized with the optimizer, the flag is True self.optim_sync_flag = True # if the cpu_shard has been visited during the training step, the flag is True self.cpu_vis_flag = False # whether to record l2 norm for the gradient clipping calculation self.l2_norm_flag = False self.l2_norm = None @property def memory_usage(self) -> Dict[str, int]: cuda_memory = 0 cpu_memory = 0 if self.chunk_temp is not None: # this chunk is not closed if self.chunk_temp.device.type == 'cuda': cuda_memory += self.chunk_mem else: cpu_memory += self.chunk_mem else: if self.is_gathered: cuda_memory += self.chunk_mem if self.cuda_shard is not None: cuda_memory += self.shard_mem if self.cpu_shard is not None: cpu_memory += self.shard_mem return dict(cuda=cuda_memory, cpu=cpu_memory) @property def device_type(self) -> str: if self.chunk_temp is not None: return self.chunk_temp.device.type else: if self.is_gathered: return 'cuda' elif self.cuda_shard is not None: return 'cuda' else: return 'cpu' @property def payload(self) -> torch.Tensor: # sanity check assert self.chunk_temp is None if self.is_gathered: return self.cuda_global_chunk elif self.cuda_shard is not None: return self.cuda_shard else: return self.cpu_shard @property def payload_mem(self) -> int: # sanity check assert self.chunk_temp is None if self.is_gathered: return self.chunk_mem else: return self.shard_mem @property def can_move(self) -> bool: return not self.is_gathered @property def can_release(self) -> bool: if self.keep_gathered: return False else: return self.tensor_state_cnter[TensorState.HOLD] + \ self.tensor_state_cnter[TensorState.HOLD_AFTER_BWD] == self.num_tensors @property def can_reduce(self): return self.tensor_state_cnter[TensorState.READY_FOR_REDUCE] == self.num_tensors @property def has_inf_or_nan(self) -> bool: """Check if the chunk has inf or nan values on CUDA. """ if self.is_gathered: valid_tensor = self.cuda_global_chunk[:self.utilized_size] else: assert self.cuda_shard is not None # only check on CUDA valid_tensor = self.cuda_shard[:self.valid_end] return torch.isinf(valid_tensor).any().item() | torch.isnan(valid_tensor).any().item() def set_l2_norm(self) -> None: """Record l2 norm of this chunks on CUDA. """ assert self.l2_norm is None, "you are calculating the l2 norm twice" if self.is_gathered: valid_tensor = self.cuda_global_chunk[:self.utilized_size] else: assert self.cuda_shard is not None # calculate on CUDA valid_tensor = self.cuda_shard[:self.valid_end] chunk_l2_norm = valid_tensor.data.float().norm(2) self.l2_norm = chunk_l2_norm.item()**2 def append_tensor(self, tensor: torch.Tensor): """Add a tensor to the chunk. Args: tensor (torch.Tensor): a tensor to be added to the chunk """ # sanity check assert self.chunk_temp is not None assert tensor.dtype == self.dtype new_utilized_size = self.utilized_size + tensor.numel() # raise exception when the chunk size is exceeded if new_utilized_size > self.chunk_size: raise ChunkFullError self.chunk_temp[self.utilized_size:new_utilized_size].copy_(tensor.data.flatten()) assert type(self.chunk_temp) == torch.Tensor, "copy_tensor_to_chunk_slice must use a torch tensor" tensor.data = self.chunk_temp[self.utilized_size:new_utilized_size].view(tensor.shape) # record all the information about the tensor self.num_tensors += 1 tensor_state = TensorState.HOLD self.tensors_info[tensor] = TensorInfo(tensor_state, self.utilized_size, new_utilized_size) self.tensor_state_cnter[tensor_state] += 1 self.utilized_size = new_utilized_size def close_chunk(self): """Close the chunk. Any tensor can't be appended to a closed chunk later. """ # sanity check assert self.chunk_temp is not None # calculate the valid end for each shard if self.utilized_size <= self.shard_begin: self.valid_end = 0 elif self.utilized_size < self.shard_end: self.valid_end = self.utilized_size - self.shard_begin if self.chunk_temp.device.type == 'cpu': self.cuda_global_chunk = self.chunk_temp.to(get_current_device()) self.__update_tensors_ptr() else: self.cuda_global_chunk = self.chunk_temp self.chunk_temp = None self.__scatter() # gathered chunk never have shard attribute if self.keep_gathered: return if self.pin_memory or self.shard_device.type == 'cpu': self.cpu_shard = torch.empty(self.shard_size, dtype=self.dtype, pin_memory=self.pin_memory) self.cpu_shard.copy_(self.cuda_shard) self.cpu_vis_flag = True # cpu_shard has been visited if self.shard_device.type == 'cpu': self.cuda_shard = None def shard_move(self, device: torch.device, force_copy: bool = False): """Move the shard tensor in the chunk. Args: device: the device to which the shard will move force_copy: if True, copy function is called mandatorily """ # sanity check assert not self.is_gathered # when the current chunk is not synchronized with the optimizer # just use another way for the movement if not self.optim_sync_flag: assert device.type == 'cuda', "each chunk should first be moved to CUDA" self.__paired_shard_move() self.optim_sync_flag = True return if device.type == 'cuda': assert device == get_current_device(), "can't move chunk to another device" if self.cuda_shard: return self.cuda_shard = self.cpu_shard.to(get_current_device()) if not self.pin_memory: self.cpu_shard = None elif device.type == 'cpu': if self.cuda_shard is None: return if self.pin_memory: if force_copy or not self.cpu_vis_flag: self.cpu_shard.copy_(self.cuda_shard) # if cpu_shard has been visited # copy operation is not need else: self.cpu_shard = self.cuda_shard.cpu() self.cpu_vis_flag = True self.cuda_shard = None else: raise NotImplementedError def access_chunk(self): """Make the chunk usable for the parameters inside it. It's an operation done in CUDA. """ # sanity check assert self.chunk_temp is None if not self.is_gathered: self.__gather() self.__update_tensors_ptr() def release_chunk(self): """Release the usable chunk. It's an operation done in CUDA. """ # sanity check assert self.chunk_temp is None if self.is_gathered: self.__scatter() def reduce(self): """Reduce scatter all the gradients. It's an operation done in CUDA. """ # sanity check assert self.is_gathered if self.pg_size == 1: # tricky code here # just move cuda_global_chunk to cuda_shard # the communication is not necessary self.__scatter() elif self.keep_gathered: # we use all-reduce here dist.all_reduce(self.cuda_global_chunk, group=self.torch_pg) else: self.cuda_shard = torch.empty(self.shard_size, dtype=self.dtype, device=get_current_device()) input_list = list(torch.chunk(self.cuda_global_chunk, chunks=self.pg_size, dim=0)) dist.reduce_scatter(self.cuda_shard, input_list, group=self.torch_pg) free_storage(self.cuda_global_chunk) self.is_gathered = False self.__update_tensors_state(TensorState.HOLD) def tensor_trans_state(self, tensor: torch.Tensor, tensor_state: TensorState) -> None: """ Make a transition of the tensor into the next state. Args: tensor (torch.Tensor): a torch Tensor object. tensor_state (TensorState): the target state for transition. """ # As the gradient hook can be triggered either before or after post-backward # tensor's state can be compute -> hold_after_bwd -> ready_for_reduce # or compute -> ready_for_reduce -> hold_after_bwd # the second one is invalid, we just ignore ready_for_reduce -> hold_after_bwd # this function only apply valid state transformation # invalid calls will be ignored and nothing changes if (self.tensors_info[tensor].state, tensor_state) not in STATE_TRANS: return self.__update_one_tensor_info(self.tensors_info[tensor], tensor_state) def copy_tensor_to_chunk_slice(self, tensor: torch.Tensor, data_slice: torch.Tensor) -> None: """ Copy data slice to the memory space indexed by the input tensor in the chunk. Args: tensor (torch.Tensor): the tensor used to retrive meta information data_slice (torch.Tensor): the tensor to be copied to the chunk """ # sanity check assert self.is_gathered tensor_info = self.tensors_info[tensor] self.cuda_global_chunk[tensor_info.offset:tensor_info.end].copy_(data_slice.data.flatten()) tensor.data = self.cuda_global_chunk[tensor_info.offset:tensor_info.end].view(tensor.shape) def get_valid_length(self) -> int: """Get the valid length of the chunk's payload. """ if self.keep_gathered: return self.utilized_size else: return self.valid_end def init_pair(self, friend_chunk: 'Chunk') -> None: """Initialize the paired chunk. """ if self.paired_chunk is None and friend_chunk.paired_chunk is None: self.paired_chunk = friend_chunk friend_chunk.paired_chunk = self else: assert self.paired_chunk is friend_chunk assert friend_chunk.paired_chunk is self def optim_update(self) -> None: """Update the fp16 chunks via their fp32 chunks. It's used by the optimizer. """ # sanity check assert self.paired_chunk is not None friend_chunk = self.paired_chunk if self.is_gathered is True: assert friend_chunk.is_gathered is True self.cuda_global_chunk.copy_(friend_chunk.cuda_global_chunk) self.optim_sync_flag = True elif friend_chunk.device_type == 'cuda' and self.device_type == 'cuda': self.cuda_shard.copy_(friend_chunk.cuda_shard) self.optim_sync_flag = True self.cpu_vis_flag = False else: # optim_sync_flag is set to False # see shard_move function for more details assert friend_chunk.device_type == 'cpu' assert self.device_type == 'cpu' self.optim_sync_flag = False self.cpu_vis_flag = False def get_tensors(self) -> List[torch.Tensor]: return list(self.tensors_info.keys()) def __gather(self): if not self.is_gathered: # sanity check assert self.cuda_shard is not None alloc_storage(self.cuda_global_chunk) gather_list = list(torch.chunk(input=self.cuda_global_chunk, chunks=self.pg_size, dim=0)) dist.all_gather(gather_list, self.cuda_shard, self.torch_pg) self.cuda_shard = None self.is_gathered = True def __scatter(self): if self.keep_gathered: return if self.is_gathered: # sanity check assert self.cuda_shard is None self.cuda_shard = torch.empty(self.shard_size, dtype=self.dtype, device=self.cuda_global_chunk.device) self.cuda_shard.copy_(self.cuda_global_chunk[self.shard_begin:self.shard_end]) free_storage(self.cuda_global_chunk) self.is_gathered = False def __paired_shard_move(self): assert self.paired_chunk is not None, "chunks should be paired before training" optim_chunk = self.paired_chunk assert self.chunk_size == optim_chunk.chunk_size # only be called when optimizer state is in CPU memory # the grad and param should be in the same device assert self.cuda_shard is None temp = optim_chunk.cpu_shard.to(get_current_device()) # avoid to transform FP32 in CPU self.cuda_shard = temp.to(self.dtype) if not self.pin_memory: self.cpu_shard = None def __update_tensors_ptr(self) -> None: # sanity check assert self.is_gathered assert type(self.cuda_global_chunk) == torch.Tensor for tensor, tensor_info in self.tensors_info.items(): tensor.data = self.cuda_global_chunk[tensor_info.offset:tensor_info.end].view(tensor.shape) def __update_one_tensor_info(self, tensor_info: TensorInfo, next_state: TensorState): self.tensor_state_cnter[tensor_info.state] -= 1 tensor_info.state = next_state self.tensor_state_cnter[tensor_info.state] += 1 def __update_tensors_state(self, next_state: TensorState, prev_state: Optional[TensorState] = None): for tensor_info in self.tensors_info.values(): if prev_state is None or tensor_info.state == prev_state: self.__update_one_tensor_info(tensor_info, next_state) def __hash__(self) -> int: return hash(id(self)) def __eq__(self, __o: object) -> bool: return self is __o def __repr__(self, detailed: bool = True): output = [ "Chunk Information:\n", "\tchunk size: {}, chunk dtype: {}, process group size: {}\n".format(self.chunk_size, self.dtype, self.pg_size), "\t# of tensors: {}, utilized size: {}, utilized percentage: {:.2f}\n".format( self.num_tensors, self.utilized_size, self.utilized_size / self.chunk_size) ] def print_tensor(tensor, prefix=''): output.append("{}shape: {}, dtype: {}, device: {}\n".format(prefix, tensor.shape, tensor.dtype, tensor.device)) if self.chunk_temp is not None: output.append("\tchunk temp:\n") print_tensor(tensor=self.chunk_temp, prefix='\t\t') if self.cuda_global_chunk is not None and self.cuda_global_chunk.storage().size() > 0: output.append("\tchunk total:\n") print_tensor(tensor=self.cuda_global_chunk, prefix='\t\t') if self.cuda_shard is not None: output.append("\tcuda shard:\n") print_tensor(tensor=self.cuda_shard, prefix='\t\t') if self.cpu_shard is not None: output.append("\tcpu shard:\n") print_tensor(tensor=self.cpu_shard, prefix='\t\t') memory_info = self.memory_usage output.append("\tmemory usage: cuda {}, cpu {}\n".format(memory_info['cuda'], memory_info['cpu'])) if detailed: output.append("\ttensor state monitor:\n") for st in TensorState: output.append("\t\t# of {}: {}\n".format(st, self.tensor_state_cnter[st])) return ''.join(output)
from time import time from typing import Optional import torch import torch.distributed as dist import torch.nn as nn from colossalai.gemini.chunk import ChunkManager from colossalai.gemini.chunk.search_utils import search_chunk_configuration from colossalai.utils import is_ddp_ignored def safe_div(a, b): if a == 0: return 0 return a / b def init_chunk_manager(model: nn.Module, init_device: Optional[torch.device] = None, hidden_dim: Optional[int] = None, **kwargs) -> ChunkManager: if hidden_dim: search_interval_byte = hidden_dim else: search_interval_byte = 1024 # defaults to 1kb kwargs["search_interval_byte"] = search_interval_byte dist.barrier() begin = time() config_dict, total_size, wasted_size = search_chunk_configuration(model, **kwargs) dist.barrier() end = time() span_s = end - begin mb_size = 1024**2 total_size /= mb_size wasted_size /= mb_size if dist.get_rank() == 0: print("searching chunk configuration is completed in {:.2f} s.\n".format(span_s), "used number: {:.2f} MB, wasted number: {:.2f} MB\n".format(total_size, wasted_size), "total wasted percentage is {:.2f}%".format(100 * safe_div(wasted_size, total_size + wasted_size)), sep='', flush=True) dist.barrier() chunk_manager = ChunkManager(config_dict, init_device) return chunk_manager
from collections import deque from typing import Deque, Dict, Iterable, List, Optional, Set, Tuple import torch from colossalai.gemini.chunk import Chunk, ChunkFullError, TensorState from colossalai.tensor import ColoTensor from colossalai.utils import get_current_device class ChunkManager: """ A manager class to manipulate the tensors in chunks. Args: chunk_configuration (Dict[int, Dict]): the configuration dictionary of this chunk manager. init_device (torch.device): optional, the device on which the chunk is initialized. The default is None. """ def __init__(self, chunk_configuration, init_device: Optional[torch.device] = None) -> None: self.device = init_device or get_current_device() self.dp_degree_chunk_size_dict: Dict[int, int] = dict() self.kwargs_config = chunk_configuration for k, v in self.kwargs_config.items(): self.dp_degree_chunk_size_dict[k] = v.pop('chunk_size') v['init_device'] = self.device self.chunk_groups: Dict[str, Deque] = dict() self.tensor_chunk_map: Dict[torch.Tensor, Chunk] = dict() self.accessed_chunks: Set[Chunk] = set() self.accessed_mem: int = 0 self.total_mem: Dict[str, int] = {'cpu': 0, 'cuda': 0} def register_tensor(self, tensor: ColoTensor, group_type: str, config_key: int, cpu_offload: bool = False, pin_memory: bool = False) -> None: """ Register a tensor to the chunk manager. Then, the tensor should be accessed by `get_chunks`. Args: tensor: the tensor appended to the chunk group_type: the data type of the group. config_key: the key of the group's name, the size of the dp world cpu_offload: if True, the chunk will be closed on CPU pin_memory: whether the chunk is pinned in the cpu memory """ assert tensor not in self.tensor_chunk_map assert isinstance(tensor, ColoTensor), "Please feed ColoTensor to this ChunkManager" assert config_key in self.dp_degree_chunk_size_dict chunk_size = self.dp_degree_chunk_size_dict[config_key] chunk_kwargs = self.kwargs_config[config_key] group_name = "{}_{}".format(group_type, config_key) chunk_group = self.__get_chunk_group(group_name) try: # append the tensor to the last chunk chunk_group[-1].append_tensor(tensor) except (IndexError, ChunkFullError): # the except statement will be triggered when there is no chunk or # the last chunk in the chunk group is full # this will create a new chunk and allocate this chunk to its corresponding process if chunk_group: # the chunk group is not empty # close the last chunk self.__close_one_chunk(chunk_group[-1]) if tensor.numel() > chunk_size: chunk_size = tensor.numel() chunk = Chunk( chunk_size=chunk_size, process_group=tensor.process_group, dtype=tensor.dtype, cpu_shard_init=cpu_offload, pin_memory=pin_memory, **chunk_kwargs, ) chunk_group.append(chunk) chunk.append_tensor(tensor) self.__add_memory_usage(chunk.memory_usage) self.tensor_chunk_map[tensor] = chunk_group[-1] def close_all_groups(self): """Close all the chunks of all groups. """ for group_name in self.chunk_groups: self.__close_one_chunk(self.chunk_groups[group_name][-1]) def access_chunk(self, chunk: Chunk) -> None: """Make the chunk can be used for calculation. """ if chunk in self.accessed_chunks: return self.__sub_memroy_usage(chunk.memory_usage) if chunk.device_type == 'cpu': chunk.shard_move(get_current_device()) self.__add_accessed_chunk(chunk) self.__add_memory_usage(chunk.memory_usage) def release_chunk(self, chunk: Chunk) -> None: """Scatter the chunk in CUDA. """ if chunk not in self.accessed_chunks: return if chunk.can_release: self.__sub_memroy_usage(chunk.memory_usage) self.__sub_accessed_chunk(chunk) self.__add_memory_usage(chunk.memory_usage) def move_chunk(self, chunk: Chunk, device: torch.device, force_copy: bool = False) -> None: """Move the shard of the chunk to the target device. """ if not chunk.can_move or chunk.device_type == device.type: return self.__sub_memroy_usage(chunk.memory_usage) chunk.shard_move(device, force_copy) self.__add_memory_usage(chunk.memory_usage) def trans_tensor_state(self, tensor: torch.Tensor, state: TensorState) -> None: """Transit tensor state according to pre-defined state machine. """ chunk = self.tensor_chunk_map[tensor] chunk.tensor_trans_state(tensor, state) def reduce_chunk(self, chunk: Chunk) -> bool: """Reduce or all reduce the chunk. """ if not chunk.can_reduce: return False self.__sub_memroy_usage(chunk.memory_usage) chunk.reduce() self.__sub_accessed_chunk(chunk) self.__add_memory_usage(chunk.memory_usage) return True def fake_release_chunk(self, chunk: Chunk) -> None: """Release gathered chunk in a fake mode. This function is used for keep-gathered chunk in the inference mode. """ assert chunk.keep_gathered assert chunk.tensor_state_cnter[TensorState.HOLD] == chunk.num_tensors self.__sub_accessed_chunk(chunk) def copy_tensor_to_chunk_slice(self, tensor: torch.Tensor, data: torch.Tensor) -> None: """ Copy data to the chunk. Args: tensor (torch.Tensor): the tensor used to retrive meta information data (torch.Tensor): the tensor to be copied to the chunk """ chunk = self.tensor_chunk_map[tensor] chunk.copy_tensor_to_chunk_slice(tensor, data) def get_chunk(self, tensor: torch.Tensor) -> Chunk: """ Return the chunk owning the tensor. Args: tensor (torch.Tensor): a torch tensor object """ return self.tensor_chunk_map[tensor] def get_cuda_movable_chunks(self) -> List[Chunk]: """ Get all chunks that can be moved. """ chunk_list = [] for chunk in self.accessed_chunks: if chunk.can_release: chunk_list.append(chunk) chunk_list.sort(key=lambda x: x.count_id) return chunk_list def get_chunks(self, tensors: Iterable[torch.Tensor]) -> Tuple[Chunk, ...]: """ Get all chunks owning the input tensors. Args: tensors (Iterable[torch.Tensor]): the tensors used to look for chunks """ chunks = [] for tensor in tensors: chunk = self.get_chunk(tensor) if chunk not in chunks: chunks.append(chunk) return tuple(chunks) def add_extern_static_tensor(self, tensor: torch.Tensor) -> None: """Add extern static tensor to chunk manager. Those tensors won't be managed by chunk manager, but we want to monitor memory usage of them. They are "static", which means their shape, dtype, device never change. Thus, their memory usage never changes. Args: tensor (torch.Tensor): An extern static tensor. E.g. optimizer state. """ assert tensor not in self.tensor_chunk_map self.total_mem[tensor.device.type] += tensor.numel() * tensor.element_size() def __repr__(self) -> str: msg = [ 'Chunk Manager Information:\n', 'Total memory: ' + ', '.join([f'{k}={v}B' for k, v in self.total_mem.items()]) + '\n' ] for group_name, group in self.chunk_groups.items(): msg.append(f'Group {group_name}:\n') for i, chunk in enumerate(group): msg.append(f'[{i}] {chunk}\n') return ''.join(msg) def __get_chunk_group(self, group_name: str) -> Deque: """Register a chunk group. """ if group_name not in self.chunk_groups: self.chunk_groups[group_name] = deque() return self.chunk_groups[group_name] def __close_one_chunk(self, chunk: Chunk): self.__sub_memroy_usage(chunk.memory_usage) chunk.close_chunk() self.__add_memory_usage(chunk.memory_usage) def __sub_memroy_usage(self, usage: Dict[str, int]): for k, v in usage.items(): self.total_mem[k] -= v def __add_memory_usage(self, usage: Dict[str, int]): for k, v in usage.items(): self.total_mem[k] += v def __add_accessed_chunk(self, chunk: Chunk): chunk.access_chunk() self.accessed_chunks.add(chunk) self.accessed_mem += chunk.chunk_mem def __sub_accessed_chunk(self, chunk: Chunk): chunk.release_chunk() self.accessed_chunks.remove(chunk) self.accessed_mem -= chunk.chunk_mem
import math from typing import Dict, List, Optional, Tuple import numpy as np import torch.distributed as dist import torch.nn as nn from colossalai.gemini.memory_tracer import MemStats, OrderedParamGenerator from colossalai.tensor import ColoParameter from colossalai.utils import is_ddp_ignored def _filter_exlarge_params(model: nn.Module, size_dict: Dict[int, List[int]]) -> None: """ Filter those parameters whose size is too large (more than 3x standard deviations) from others. """ agg_size_list = [] for key in size_dict: agg_size_list.extend(size_dict[key]) if len(agg_size_list) == 0: return params_size_arr = np.array(agg_size_list) std = np.std(params_size_arr) mean = np.mean(params_size_arr) upper_limit = mean + 3 * std for key in size_dict: org_list = size_dict[key] size_dict[key] = list(filter(lambda x: x <= upper_limit, org_list)) def _get_unused_byte(size_list: List[int], chunk_size: int) -> int: """Get unused byte for a certain chunk size. """ acc = 0 left = 0 for s in size_list: if s > left: acc += left left = chunk_size left -= s return left + acc def _tensor_numel(local_param: ColoParameter, strict_ddp_flag: bool): if strict_ddp_flag: return local_param.numel_global() else: return local_param.numel() def classify_params_by_dp_degree(param_order: OrderedParamGenerator, strict_ddp_flag: bool = False) -> Dict[int, List[ColoParameter]]: """classify_params_by_dp_degree Classify the parameters by their dp degree Args: param_order (OrderedParamGenerator): the order of param be visied Returns: Dict[int, List[ColoParameter]]: a dict contains the classification results. The keys are dp_degrees and the values are parameters. """ params_dict: Dict[int, List[ColoParameter]] = dict() for param in param_order.generate(): assert isinstance(param, ColoParameter), "please init model in the ColoInitContext" if is_ddp_ignored(param): continue if strict_ddp_flag: param_key = dist.get_world_size() else: param_key = param.process_group.dp_world_size() if param_key not in params_dict: params_dict[param_key] = [] params_dict[param_key].append(param) return params_dict def search_chunk_configuration( model: nn.Module, search_range_mb: float, search_interval_byte: int, # hidden size is the best value for the interval min_chunk_size_mb: float = 32, filter_exlarge_params: bool = True, strict_ddp_flag: bool = False, memstas: Optional[MemStats] = None) -> Tuple[Dict, int, int]: """search_chunk_configuration Args: model (nn.Module): torch module search_range_mb (float): searching range in mega byte. search_interval_byte (int): searching interval in byte. min_chunk_size_mb (float, optional): the minimum size of a distributed chunk. filter_exlarge_params (bool, optional): filter extreme large parameters. Defaults to True. strict_ddp_flag (bool, optional): whether to enable the strict ddp mode. all parameters keep replicated in this mode. Returns: Tuple[Dict, int]: chunk config (a dict of dp_degree -> chunk init args) and its memory chunk waste in byte. """ if memstas is not None: param_order = memstas.param_order() else: # build the param visited order right now param_order = OrderedParamGenerator() for p in model.parameters(): param_order.append(p) search_range_byte = round(search_range_mb * 1024**2) min_chunk_size_byte = round(min_chunk_size_mb * 1024**2) assert search_range_byte >= 0 params_dict = classify_params_by_dp_degree(param_order, strict_ddp_flag) config_dict: Dict[int, Dict] = dict() total_param_size = 0 size_dict: Dict[int, List[int]] = dict() for dp_degree in params_dict: params_list = params_dict[dp_degree] size_list = [_tensor_numel(p, strict_ddp_flag) for p in params_list] group_acc_size = sum(size_list) total_param_size += group_acc_size # let small parameters keep gathered in CUDA all the time if group_acc_size < min_chunk_size_byte: config_dict[dp_degree] = dict(chunk_size=group_acc_size, keep_gathered=True) else: size_dict[dp_degree] = size_list if filter_exlarge_params: _filter_exlarge_params(model, size_dict) max_size = min_chunk_size_byte for key in size_dict: max_size = max(max_size, max(size_dict[key])) start_size = int(math.ceil(max_size / search_interval_byte) * search_interval_byte) min_chunk_waste = float('+inf') best_chunk_size = start_size for chunk_size in range(start_size, start_size + search_range_byte + 1, search_interval_byte): temp_waste = 0 for key in size_dict: temp_waste += _get_unused_byte(size_dict[key], chunk_size) if temp_waste < min_chunk_waste: min_chunk_waste = temp_waste best_chunk_size = chunk_size for dp_degree in params_dict: if dp_degree in config_dict: continue config_dict[dp_degree] = dict(chunk_size=best_chunk_size, keep_gathered=False) return config_dict, total_param_size, min_chunk_waste
import torch.nn from colossalai.gemini.memory_tracer import MemStats from colossalai.gemini.ophooks.runtime_mem_tracer_hook import GradMemStats, GradMemTracerHook, ParamMemTracerHook from colossalai.nn.parallel.data_parallel import _cast_float from colossalai.tensor.param_op_hook import ColoParamOpHookManager __all__ = ['RuntimeMemTracer'] class RuntimeMemTracer(): """RuntimeMemTracer for the module training using ColoParameter. Trace non-model memory usage during fwd+bwd process. It is obtained by using a tensor with the same shape as the training process as the inputs and running an single fwd+bwd to trace the statistics. NOTE() 1. The premise to use this tracer is that the target DNN execute the same operations at each iterations, 2. Module buffers are viewed as non-model data. """ def __init__(self, module: torch.nn.Module, dtype: torch.dtype = torch.half): super().__init__() self.module = module self.dtype = dtype self._gradstat = GradMemStats() self._memstats = MemStats() self.param_op_hook = ParamMemTracerHook(self._memstats, self._gradstat) self.grad_hook = GradMemTracerHook(self._gradstat) self.cpu_param_data_dict = {} for p in module.parameters(): p.data = p.data.to(dtype) self._cast_buffers_to_cuda_dtype() def parameters_in_runtime_order(self): return self._memstats._param_runtime_order.generate() def memstats(self): return self._memstats def __call__(self, *args, **kwargs): return self.forward(*args, **kwargs) def _backup_params(self): """ The function is called before forward. Backup model params on cpu. """ for p in self.module.parameters(): self.cpu_param_data_dict[p] = torch.empty(p.data.shape, dtype=self.dtype, device="cpu") self.cpu_param_data_dict[p].copy_(p.data) def _restore_params(self): """ This function is called after backward. Restore model params. """ for p in self.module.parameters(): p.data = torch.empty(p.data.shape, dtype=self.dtype, device="cpu", requires_grad=p.data.requires_grad) p.data.copy_(self.cpu_param_data_dict[p]) self.cpu_param_data_dict.clear() def _pre_forward(self): self._clear_cuda_mem_info() self._backup_params() self.grad_hook.register_grad_hook(self.module) self.param_op_hook.mem_monitor.start() def forward(self, *args, **kwargs): args, kwargs = _cast_float(args, self.dtype), _cast_float(kwargs, self.dtype) self.module.zero_grad(set_to_none=True) self._pre_forward() with ColoParamOpHookManager.use_hooks(self.param_op_hook): outputs = self.module(*args, **kwargs) return outputs def backward(self, loss): with self.param_op_hook.switch_to_backward(), ColoParamOpHookManager.use_hooks(self.param_op_hook): loss.backward() self._post_backward() def _post_backward(self): cuda_volume = self.param_op_hook.mem_monitor.finish() self._memstats.record_max_cuda_overall_data(cuda_volume) # calc the last Op non model data self._memstats.calc_max_cuda_non_model_data() self.grad_hook.remove_grad_hook() self._restore_params() def _clear_cuda_mem_info(self): self._memstats.clear() self._gradstat.clear() def _cast_buffers_to_cuda_dtype(self): for buffer in self.module.buffers(): buffer.data = buffer.cuda() if torch.is_floating_point(buffer): buffer.data = buffer.data.to(self.dtype)
from typing import Optional import torch import torch.nn as nn from torch.fx import symbolic_trace from colossalai.fx.passes.meta_info_prop import MetaInfoProp from colossalai.fx.profiler import calculate_fwd_out, calculate_fwd_tmp, is_compatible_with_meta from colossalai.gemini.chunk import ChunkManager if is_compatible_with_meta(): from colossalai.fx.profiler import MetaTensor from .chunk_memstats_collector import ChunkMemStatsCollector class ModuleInfos: def __init__(self, module: torch.nn.Module, module_name: str, module_full_name: str, parent_module: torch.nn.Module): self.module = module self.module_name = module_name self.module_full_name = module_full_name self.parent_module = parent_module class StaticMemStatsCollector(ChunkMemStatsCollector): """ A Static Memory statistic collector. """ def __init__(self, module: nn.Module, chunk_manager: ChunkManager) -> None: super().__init__(chunk_manager) self.module = module self.module_info_list = [] def init_mem_stats(self, *inputs): self.register_opnodes_recursively(self.module) self.refactor_module() self.module = self.module.cpu() self.module.train() data = [MetaTensor(torch.rand(inp.shape, device='meta'), fake_device='cpu') for inp in inputs] gm = symbolic_trace(self.module) interp = MetaInfoProp(gm) interp.propagate(*data) total_mem = 0 for inp in inputs: total_mem += inp.numel() * inp.element_size() last_node = None module_name_list = [mInfo.module_full_name for mInfo in self.module_info_list] for node in gm.graph.nodes: total_mem = total_mem + calculate_fwd_tmp(node) + calculate_fwd_out(node) if node.op == "call_module": if node.name.endswith("_0") and node.name[:-2] in module_name_list: self._non_model_data_cuda_list.append(total_mem) last_node = node self._non_model_data_cuda_list.append(total_mem) self._non_model_data_cuda_list = self._non_model_data_cuda_list[1:] cur_module_mem_fwd = 0 cur_module_mem_bwd = 0 grad_module_out = last_node.meta["fwd_mem_out"] for node in gm.graph.nodes.__reversed__(): cur_module_mem_fwd = cur_module_mem_fwd + calculate_fwd_tmp(node) + calculate_fwd_out(node) cur_module_mem_bwd = cur_module_mem_bwd + node.meta["bwd_mem_tmp"] + node.meta["bwd_mem_out"] if node.op == "call_module": if node.name.endswith("_0") and node.name[:-2] in module_name_list: self._non_model_data_cuda_list.append(total_mem + grad_module_out + cur_module_mem_bwd) total_mem = total_mem - cur_module_mem_fwd cur_module_mem_fwd = 0 cur_module_mem_bwd = 0 grad_module_out = node.meta["bwd_mem_out"] self._step_total = len(self._non_model_data_cuda_list) self.recover_module() def refactor_module(self): for modInfo in self.module_info_list: temp_node = nn.Sequential(nn.ReLU(), modInfo.module) modInfo.parent_module.__setattr__(modInfo.module_name, temp_node) def recover_module(self): for modInfo in self.module_info_list: modInfo.parent_module.__setattr__(modInfo.module_name, modInfo.module) def register_opnodes_recursively(self, module: torch.nn.Module, name: str = "", full_name: str = "", parent_module: Optional[torch.nn.Module] = None): assert isinstance(module, torch.nn.Module) for child_name, child in module.named_children(): self.register_opnodes_recursively(child, child_name, full_name + "_" + child_name, module) # Early return on modules with no parameters. if len(list(module.parameters(recurse=False))) == 0: return self.module_info_list.append(ModuleInfos(module, name, full_name[1:], parent_module))
from typing import Optional from colossalai.gemini.chunk import ChunkManager from colossalai.gemini.memory_tracer import MemStats from colossalai.utils import get_current_device from colossalai.utils.memory import colo_device_memory_capacity from .memstats_collector import MemStatsCollector class ChunkMemStatsCollector(MemStatsCollector): def __init__(self, chunk_manager: ChunkManager, memstats: Optional[MemStats] = None) -> None: """ Memory Statistic Collector for Chunks. Args: chunk_manager (ChunkManager): the chunk manager. memstats (Optional[MemStats], optional): memory statistics collected by RMT. Defaults to None. """ super().__init__(memstats) self._chunk_manager = chunk_manager # override def record_model_data_volume(self) -> None: """ record model data volumn on cuda and cpu. """ if self._start_flag and not self.use_outside_memstats: cuda_mem = self._chunk_manager.total_mem['cuda'] self._memstats.record_max_cuda_model_data(cuda_mem) @property def cuda_margin_mem(self) -> float: return colo_device_memory_capacity(get_current_device()) - self._memstats.max_overall_cuda
from .param_runtime_order import OrderedParamGenerator # isort:skip from .memory_stats import MemStats # isort:skip from .memory_monitor import AsyncMemoryMonitor, SyncCudaMemoryMonitor # isort:skip from .memstats_collector import MemStatsCollector # isort:skip from .chunk_memstats_collector import ChunkMemStatsCollector # isort:skip from .static_memstats_collector import StaticMemStatsCollector # isort:skip __all__ = [ 'AsyncMemoryMonitor', 'SyncCudaMemoryMonitor', 'MemStatsCollector', 'ChunkMemStatsCollector', 'StaticMemStatsCollector', 'MemStats', 'OrderedParamGenerator' ]
from abc import ABC import torch class ParamGenerator(ABC): def append(self, param: torch.nn.Parameter): pass def generate(self): pass def clear(self): pass class OrderedParamGenerator(ParamGenerator): """OrderedParamGenerator Contain the order of parameters visited during runtime. """ def __init__(self) -> None: self.param_visited_order = [] def append(self, param: torch.nn.Parameter): self.param_visited_order.append(param) def generate(self): visited_set = set() for p in self.param_visited_order: if p not in visited_set: yield p visited_set.add(p) del visited_set def is_empty(self): return len(self.param_visited_order) == 0 def clear(self): self.param_visited_order = []
from typing import Optional, Tuple import torch def colo_model_optimizer_usage(optim) -> Tuple[int, int]: """Trace the optimizer memory usage Args: optim (ShardedOptimV2): an instance of ShardedOptimver Returns: Tuple[int, int]: cuda/cpu memory usage in Byte """ if optim is None: return 0, 0 assert hasattr(optim, 'get_memory_usage'), f"{type(optim)} has no attr get_memory_usage()" return optim.get_memory_usage() def colo_model_mem_usage(model: torch.nn.Module) -> Tuple[int, int]: """ Trace the model memory usage. Args: model (torch.nn.Module): a torch model Returns: Tuple[int, int]: cuda memory usage in Byte, cpu memory usage in Byte """ if model is None: return 0, 0 def _get_tensor_mem_use(t: Optional[torch.Tensor]): if t is None: return 0, 0 assert isinstance(t, torch.Tensor) _cpu_mem_usage, _cuda_mem_usage = 0, 0 if t.device.type == 'cpu': _cpu_mem_usage += t.numel() * t.element_size() elif t.device.type == 'cuda': _cuda_mem_usage += t.numel() * t.element_size() return _cuda_mem_usage, _cpu_mem_usage cuda_mem_usage = 0 cpu_mem_usage = 0 for param in model.parameters(): if hasattr(param, 'colo_attr'): t_cuda, t_cpu = param.colo_attr.get_memory_usage() cuda_mem_usage += t_cuda cpu_mem_usage += t_cpu else: t_cuda, t_cpu = _get_tensor_mem_use(param.data) cuda_mem_usage += t_cuda cpu_mem_usage += t_cpu t_cuda, t_cpu = _get_tensor_mem_use(param.grad) cuda_mem_usage += t_cuda cpu_mem_usage += t_cpu return cuda_mem_usage, cpu_mem_usage
from typing import Any, Dict, List, Optional import torch from colossalai.gemini.memory_tracer import OrderedParamGenerator class MemStats(object): def __init__(self) -> None: """ Store the non model data statistics used for Gemini and ZeroOptimizer. """ # (preop_step, List[param]) self._step_param_dict = dict() # (param, List[preop_step]) self._param_step_dict = dict() # (preop_step, non_model_data) non model data used during preop_step ~ (preop_step+1) self._step_nmd_dict = dict() self._param_runtime_order = OrderedParamGenerator() self._preop_step = 0 self._prev_overall_cuda = -1 self._max_overall_cuda = 0 self._prev_md_cuda = -1 # old version self._model_data_cuda_list = [] self._model_data_cpu_list = [] self._overall_cuda_list = [] self._overall_cpu_list = [] self._non_model_data_cuda_list = [] self._non_model_data_cpu_list = [] def calc_max_cuda_non_model_data(self): if self._prev_overall_cuda != -1 and self._prev_md_cuda != -1: max_cuda_non_model_data = self._prev_overall_cuda - self._prev_md_cuda self._step_nmd_dict[self._preop_step - 1] = max_cuda_non_model_data # compatibility of the old version. self._non_model_data_cuda_list.append(max_cuda_non_model_data) def record_max_cuda_model_data(self, val): self._prev_md_cuda = val def record_max_cuda_overall_data(self, val): self._prev_overall_cuda = val self._max_overall_cuda = max(self._max_overall_cuda, val) @property def max_overall_cuda(self): return self._max_overall_cuda def increase_preop_step(self, param_list: List[torch.nn.Parameter]): """ the time step is increased. param list is used between current and the next time step. Args: param_list (List[torch.nn.Parameter]): a list of torch paramters. """ for p in param_list: if p not in self._param_step_dict: self._param_step_dict[p] = [self._preop_step] else: self._param_step_dict[p].append(self._preop_step) self._param_runtime_order.append(p) self._step_param_dict[self._preop_step] = param_list self._preop_step += 1 def param_used_step(self, param: torch.nn.Parameter) -> Optional[List[int]]: """param_used_step get the timestep list using the param Args: param (torch.nn.Parameter): a torch param Returns: Optional[List[int]]: a list of int indicates the time step of preop hook. """ if param not in self._param_step_dict: return None else: return self._param_step_dict[param] def param_order(self): if self._param_runtime_order.is_empty(): raise RuntimeError else: return self._param_runtime_order def non_model_data_list(self, device_type: str) -> List[int]: if device_type == 'cuda': return self._non_model_data_cuda_list elif device_type == 'cpu': return self._non_model_data_cpu_list else: raise TypeError def max_non_model_data(self, device_type: str) -> float: if device_type == 'cuda': return max(self._non_model_data_cuda_list) elif device_type == 'cpu': return max(self._non_model_data_cpu_list) else: raise TypeError def clear(self): self._model_data_cuda_list = [] self._overall_cuda_list = [] self._model_data_cpu_list = [] self._overall_cpu_list = [] self._non_model_data_cpu_list = [] self._non_model_data_cuda_list = [] self._param_runtime_order.clear() self._step_param_dict.clear() self._param_step_dict.clear() self._step_nmd_dict.clear() self._preop_step = 0 self._prev_overall_cuda = -1 self._prev_md_cuda = -1
import json from abc import abstractmethod from concurrent.futures import ThreadPoolExecutor from time import sleep, time import torch from colossalai.utils import colo_device_memory_used, get_current_device class MemoryMonitor: """Base class for all types of memory monitor. All monitors should have a list called `time_stamps` and a list called `mem_stats`. """ def __init__(self): self.time_stamps = [] self.mem_stats = [] def __len__(self): return len(self.mem_stats) @abstractmethod def start(self): pass @abstractmethod def finish(self): pass def state_dict(self): return { "time_stamps": self.time_stamps, "mem_stats": self.mem_stats, } def save(self, filename): with open(filename, "w") as f: json.dump(self.state_dict(), f) def clear(self): self.mem_stats.clear() self.time_stamps.clear() class AsyncMemoryMonitor(MemoryMonitor): """ An Async Memory Monitor runing during computing. Sampling memory usage of the current GPU at interval of `1/(10**power)` sec. The idea comes from Runtime Memory Tracer of PatrickStar `PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management`_ Usage:: async_mem_monitor = AsyncMemoryMonitor() input = torch.randn(2, 20).cuda() OP1 = torch.nn.Linear(20, 30).cuda() OP2 = torch.nn.Linear(30, 40).cuda() async_mem_monitor.start() output = OP1(input) async_mem_monitor.finish() async_mem_monitor.start() output = OP2(output) async_mem_monitor.finish() async_mem_monitor.save('log.pkl') Args: power (int, optional): the power of time interva. Defaults to 10. .. _PatrickStar: Parallel Training of Pre-trained Models via Chunk-based Memory Management: https://arxiv.org/abs/2108.05818 """ def __init__(self, power: int = 10): super().__init__() self.keep_measuring = False current_device = get_current_device() def _set_cuda_device(): torch.cuda.set_device(current_device) self.executor = ThreadPoolExecutor(max_workers=1, initializer=_set_cuda_device) self.monitor_thread = None self.interval = 1 / (10**power) def set_interval(self, power: int): self.clear() self.interval = 1 / (10**power) def is_measuring(self): return self.keep_measuring def start(self): self.keep_measuring = True self.monitor_thread = self.executor.submit(self._measure_usage) def finish(self): if self.keep_measuring is False: return 0 self.keep_measuring = False max_usage = self.monitor_thread.result() self.monitor_thread = None self.time_stamps.append(time()) self.mem_stats.append(max_usage) return max_usage def _measure_usage(self): max_usage = 0 while self.keep_measuring: max_usage = max( max_usage, colo_device_memory_used(get_current_device()), ) sleep(self.interval) return max_usage class SyncCudaMemoryMonitor(MemoryMonitor): """ A synchronized cuda memory monitor. It only record the maximum allocated cuda memory from start point to finish point. """ def __init__(self, power: int = 10): super().__init__() def start(self): torch.cuda.synchronize() torch.cuda.reset_peak_memory_stats() def finish(self) -> int: """ return max gpu memory used since latest `start()`. Returns: int: max GPU memory """ torch.cuda.synchronize() self.time_stamps.append(time()) max_usage = torch.cuda.max_memory_allocated() self.mem_stats.append(max_usage) return max_usage
import time from typing import List, Optional import torch from colossalai.gemini.memory_tracer import SyncCudaMemoryMonitor from colossalai.gemini.stateful_tensor import StatefulTensor from colossalai.utils.memory import colo_device_memory_used from .memory_stats import MemStats class MemStatsCollector: """ A Memory statistic collector. It works in two phases. Phase 1. Collection Phase: collect memory usage statistics of CPU and GPU. The first iteration of DNN training. Phase 2. Runtime Phase: use the read-only collected stats The rest iterations of DNN training. It has a Sampling counter which is reset after DNN training iteration. """ def __init__(self, memstats: Optional[MemStats] = None) -> None: self._mem_monitor = SyncCudaMemoryMonitor() self._sampling_time = [] self._start_flag = False self._step_idx = 0 self._step_total = 0 if memstats is not None: self.use_outside_memstats = True self._memstats = memstats else: self.use_outside_memstats = False self._memstats = MemStats() def next_period_non_model_data_usage(self, device_type: str) -> int: """Maximum non model data memory usage during the next Op run Args: device_type (str): device type, can be 'cpu' or 'cuda'. Returns: int: max non model data memory usage of current sampling period """ assert not self._start_flag, 'Cannot get mem stats info during collection phase.' assert self._step_total > 0, 'Cannot get mem stats info before collection phase.' assert len(self._memstats.non_model_data_list(device_type)) > self._step_idx, \ f"{len(self._memstats.non_model_data_list(device_type))} should be > than step idx {self._step_idx}, "\ f"step total {self._step_total}" next_non_model_data = self._memstats.non_model_data_list(device_type)[self._step_idx] self._step_idx = (self._step_idx + 1) % self._step_total return next_non_model_data @property def sampling_time(self): return [t - self._sampling_time[0] for t in self._sampling_time] def start_collection(self): self._start_flag = True self._mem_monitor.start() def finish_collection(self): self.sample_overall_data() # self._step_total = len(self._sampling_time) self._step_total = len(self._memstats.non_model_data_list('cuda')) self._start_flag = False print(f'finish_collection {self._step_total}') # deprecated def record_model_data_volume(self) -> None: """ Sampling model data statistics. """ if self._start_flag and not self.use_outside_memstats: # The following code work for ZeroInitContext, which is deprecated in v0.1.12 cuda_mem = StatefulTensor.GST_MGR.total_mem['cuda'] self._memstats.record_max_cuda_model_data(cuda_mem) def sample_overall_data(self) -> None: """ Sampling overall and non model data cuda memory statistics. """ if self._start_flag and not self.use_outside_memstats: cuda_overall = self._mem_monitor.finish() self._memstats.record_max_cuda_overall_data(cuda_overall) self._memstats.calc_max_cuda_non_model_data() self._mem_monitor.start() if self._start_flag: self._sampling_time.append(time.time()) def clear(self) -> None: self._memstats.clear() self._start_flag = False self._step_idx = 0 self._step_total = 0
from ._param_hookmgr import BaseParamHookMgr __all__ = ["BaseParamHookMgr"]
from typing import Callable, List import torch import functools class BaseParamHookMgr(object): def __init__(self, param_list: List[torch.nn.Parameter]) -> None: r""" register backward hook on every parameters of module """ self._param_list = param_list self._hook_list = [] def register_backward_hooks(self, hook_call: Callable) -> None: r""" The hook_call will be called every time a gradient with respect to the a param in self.param_list is computed. The hook should have the following signature: ``` hook(param, grad) -> Tensor or None ``` """ if not torch.is_grad_enabled(): return # don't register grad hooks if grad isn't enabled for p in self._param_list: if p.requires_grad and not hasattr(p, '_base_param_hook'): handle = p.register_hook(functools.partial(hook_call, p)) p._base_param_hook = handle def remove_hooks(self) -> None: """ Remove hooks from model parameters. """ for p in self._param_list: if p.requires_grad and hasattr(p, '_base_param_hook'): p._base_param_hook.remove()
from .utils import BaseOpHook, register_ophooks_recursively __all__ = ["BaseOpHook", "register_ophooks_recursively"]
from contextlib import contextmanager from enum import Enum from functools import partial from typing import List import torch from colossalai.gemini.memory_tracer import MemStats, SyncCudaMemoryMonitor from colossalai.gemini.tensor_utils import alloc_storage, free_storage from colossalai.tensor.param_op_hook import ColoParamOpHook class TrainingPhase(Enum): FORWARD = 0 BACKWARD = 1 class GradMemStats(): def __init__(self) -> None: self.unreleased_grad_flag = {} self.unreleased_grad_volume = 0 def clear(self): self.unreleased_grad_flag.clear() self.unreleased_grad_volume = 0 class GradMemTracerHook(): def __init__(self, grad_stats: GradMemStats): self.grad_hook_list = [] self._grad_stats = grad_stats def grad_handle(self, p, grad): assert self._grad_stats.unreleased_grad_flag[p] free_storage(grad) self._grad_stats.unreleased_grad_volume -= grad.numel() * grad.element_size() self._grad_stats.unreleased_grad_flag[p] = False def register_grad_hook(self, module: torch.nn.Module): for p in module.parameters(): if p.requires_grad: self.grad_hook_list.append(p.register_hook(partial(self.grad_handle, p))) self._grad_stats.unreleased_grad_flag[p] = False def remove_grad_hook(self): for hook in self.grad_hook_list: hook.remove() class ParamMemTracerHook(ColoParamOpHook): def __init__(self, memstats: MemStats, gradstats: GradMemStats) -> None: super().__init__() self._training_phase = TrainingPhase.FORWARD self._memstats = memstats self._grad_stats = gradstats self.mem_monitor = SyncCudaMemoryMonitor() def _free_cuda_params(self, params): for p in params: if p.data.device.type == "cpu": raise NotImplementedError("Only free cuda memory") free_storage(p.data) def _allocate_params_on_cuda(self, params: List[torch.nn.Parameter]): """ move params to cuda Args: params (List[torch.nn.Parameter]): target params Raises: NotImplementedError: raise error when param has cpu grad """ for p in params: cur_dev = p.data.device.type if cur_dev == "cpu": if p.grad is not None and p.grad.device.type == "cpu": raise NotImplementedError("Only run in forward propagation") p.data = torch.empty(p.data.shape, device="cuda", dtype=p.data.dtype, requires_grad=p.data.requires_grad) elif cur_dev == "cuda": alloc_storage(p.data) def record_model_data_volume(self, params): """ get cuda model data used by params """ data_volume = self._grad_stats.unreleased_grad_volume for p in params: cur_model_data_volume = p.data.numel() * p.data.element_size() data_volume += cur_model_data_volume if self._training_phase == TrainingPhase.BACKWARD and p.requires_grad: # add param.grad, actually param.grad is None in this time data_volume += cur_model_data_volume if not self._grad_stats.unreleased_grad_flag[p]: self._grad_stats.unreleased_grad_volume += cur_model_data_volume self._grad_stats.unreleased_grad_flag[p] = True # record max non model data used for this Op self._memstats.record_max_cuda_model_data(data_volume) def pre_op(self, params): max_cuda_used_pre_op = self.mem_monitor.finish() # record max cuda overall data for prev OP. self._memstats.record_max_cuda_overall_data(max_cuda_used_pre_op) # record max cuda non model data for prev OP. self._memstats.calc_max_cuda_non_model_data() self._allocate_params_on_cuda(params) # record max cuda model data for current OP self.record_model_data_volume(params) self.mem_monitor.start() self._memstats.increase_preop_step(params) def post_op(self, params): self._free_cuda_params(params) 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 typing import List, Callable, Optional from abc import ABC, abstractmethod import torch class BaseOpHook(ABC): """This class allows users to add customized operations before and after the execution of a PyTorch submodule""" def __init__(self): pass @abstractmethod def pre_fwd_exec(self, module: torch.nn.Module, *args): pass @abstractmethod def post_fwd_exec(self, module: torch.nn.Module, *args): pass @abstractmethod def pre_bwd_exec(self, module: torch.nn.Module, input, output): pass @abstractmethod def post_bwd_exec(self, module: torch.nn.Module, input): pass @abstractmethod def post_iter(self): pass # apply torch.autograd.Function that calls a backward_function to tensors in output def _apply_to_tensors_only(module, functional, backward_function, outputs): if type(outputs) is tuple: touched_outputs = [] for output in outputs: touched_output = _apply_to_tensors_only(module, functional, backward_function, output) touched_outputs.append(touched_output) return tuple(touched_outputs) elif type(outputs) is torch.Tensor: return functional.apply(module, backward_function, outputs) else: return outputs class PreBackwardFunction(torch.autograd.Function): @staticmethod def forward(ctx, module, pre_backward_function, outputs): ctx.module = module ctx.pre_backward_function = pre_backward_function module.applied_pre_backward = False outputs = outputs.detach() return outputs @staticmethod def backward(ctx, *args): ctx.pre_backward_function(ctx.module) return (None, None) + args class PostBackwardFunction(torch.autograd.Function): @staticmethod def forward(ctx, module, pre_backward_function, output): ctx.module = module output = output.detach() ctx.pre_backward_function = pre_backward_function return output @staticmethod def backward(ctx, *args): """ Args: activation_grad of the next layer. Returns: grad of the input activation. """ ctx.pre_backward_function(ctx.module) return (None, None) + args def register_ophooks_recursively(module: torch.nn.Module, ophook_list: List[BaseOpHook], name: str = "", filter_fn: Optional[Callable] = None): r"""Recursilvely register pre/post hooks for all submodules in the module in FWD and BWD.""" assert isinstance(module, torch.nn.Module) assert isinstance(ophook_list, (list, tuple)) assert len(ophook_list) > 0, 'expected at least 1 hook in the argument ophook_list but found 0' for hook in ophook_list: assert (isinstance(hook, BaseOpHook)) # Add hooks for submodules for child_name, child in module.named_children(): register_ophooks_recursively(child, ophook_list, name + child_name, filter_fn) # Early return on modules with no parameters. if len(list(module.parameters(recurse=False))) == 0: return # return from flitered module if filter_fn is not None and filter_fn(module): return def _pre_forward_module_hook(submodule, *args): for hook in ophook_list: assert isinstance(submodule, torch.nn.Module) hook.pre_fwd_exec(submodule, *args) def _post_forward_module_hook(submodule, *args): for hook in ophook_list: assert isinstance(submodule, torch.nn.Module) hook.post_fwd_exec(submodule, *args) def _pre_backward_module_hook(submodule, inputs, output): def _run_before_backward_function(submodule): for hook in ophook_list: assert isinstance(submodule, torch.nn.Module) hook.pre_bwd_exec(submodule, inputs, output) return _apply_to_tensors_only(submodule, PreBackwardFunction, _run_before_backward_function, output) def _post_backward_module_hook(submodule, inputs): def _run_after_backward_function(submodule): for hook in ophook_list: assert isinstance(submodule, torch.nn.Module) hook.post_bwd_exec(submodule, inputs) return _apply_to_tensors_only(submodule, PostBackwardFunction, _run_after_backward_function, inputs) module.register_forward_pre_hook(_pre_forward_module_hook) module.register_forward_hook(_post_forward_module_hook) module.register_forward_hook(_pre_backward_module_hook) module.register_forward_pre_hook(_post_backward_module_hook)
import torch from colossalai.registry import OPHOOKS from . import BaseOpHook @OPHOOKS.register_module class ShardParamHook(BaseOpHook): """ A hook to process sharded param before and afther FWD and BWD operator executing. """ def __init__(self): super().__init__() def niter(self): return self._niter def pre_fwd_exec(self, module: torch.nn.Module, *args): for param in module.parameters(): assert hasattr(param, 'ca_attr') param.ca_attr.gather() param.data = param.ca_attr.payload() def post_fwd_exec(self, module: torch.nn.Module, *args): for param in module.parameters(): assert hasattr(param, 'ca_attr') param.ca_attr.shard() param.data = param.ca_attr.payload() def pre_bwd_exec(self, module: torch.nn.Module, input, output): for param in module.parameters(): assert hasattr(param, 'ca_attr') param.ca_attr.gather() param.data = param.ca_attr.payload() def post_bwd_exec(self, module: torch.nn.Module, input): for param in module.parameters(): assert hasattr(param, 'ca_attr') param.ca_attr.shard() param.data = param.ca_attr.payload() def pre_iter(self): pass def post_iter(self): pass
import torch from colossalai.registry import OPHOOKS from . import BaseOpHook @OPHOOKS.register_module class ShardGradMemTracerHook(BaseOpHook): """ A hook to process sharded param before and afther FWD and BWD operator executing. """ def __init__(self): super().__init__() def pre_fwd_exec(self, module: torch.nn.Module, *args): pass def post_fwd_exec(self, module: torch.nn.Module, *args): pass def pre_bwd_exec(self, module: torch.nn.Module, input, output): for param in module.parameters(): assert hasattr(param, '_sharded_grad') param._sharded_grad.setup() def post_bwd_exec(self, module: torch.nn.Module, input): pass def post_iter(self): pass
from ._ops import * from .layer import * from .loss import * from .lr_scheduler import * from .metric import * from .optimizer import *
import math import warnings from torch import Tensor import torch.nn as nn def zeros_(): """Return the initializer filling the input Tensor with the scalar zeros""" def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.zeros_(tensor) return initializer def ones_(): """Return the initializer filling the input Tensor with the scalar ones""" def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.ones_(tensor) return initializer def uniform_(a: float = 0., b: float = 1.): r"""Return the initializer filling the input Tensor with values drawn from the uniform distribution :math:`\mathcal{U}(a, b)`. Args: a (float): the lower bound of the uniform distribution. Defaults 0.0. b (float): the upper bound of the uniform distribution. Defaults 1.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.uniform_(tensor, a, b) return initializer def normal_(mean: float = 0., std: float = 1.): r"""Return the initializer filling the input Tensor with values drawn from the normal distribution .. math:: \mathcal{N}(\text{mean}, \text{std}^2) Args: mean (float): the mean of the normal distribution. Defaults 0.0. std (float): the standard deviation of the normal distribution. Defaults 1.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.normal_(tensor, mean, std) return initializer def trunc_normal_(mean: float = 0., std: float = 1., a: float = -2., b: float = 2.): r"""Return the initializer filling the input Tensor with values drawn from a truncated normal distribution. The values are effectively drawn from the normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)` with values outside :math:`[a, b]` redrawn until they are within the bounds. The method used for generating the random values works best when :math:`a \leq \text{mean} \leq b`. Args: mean (float): the mean of the normal distribution. Defaults 0.0. std (float): the standard deviation of the normal distribution. Defaults 1.0. a (float): the minimum cutoff value. Defaults -2.0. b (float): the maximum cutoff value. Defaults 2.0. """ def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): return nn.init.trunc_normal_(tensor, mean, std, a, b) return initializer def kaiming_uniform_(a=0, mode='fan_in', nonlinearity='leaky_relu'): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification` - He, K. et al. (2015), using a uniform distribution. The resulting tensor will have values sampled from :math:`\mathcal{U}(-\text{bound}, \text{bound})` where .. math:: \text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan_mode}}} Also known as 'He initialization'. Args: a (int): the negative slope of the rectifier used after this layer (only used with ``'leaky_relu'``). mode (str, optional): either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'`` preserves the magnitude of the variance of the weights in the forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the backwards pass. nonlinearity (str, optional): the non-linear function (`nn.functional` name), recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default). """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): if 0 in tensor.shape: warnings.warn("Initializing zero-element tensors is a no-op") return tensor if mode == 'fan_in': assert fan_in is not None, 'Fan_in is not provided.' fan = fan_in elif mode == 'fan_out': assert fan_out is not None, 'Fan_out is not provided.' fan = fan_out else: raise ValueError(f'Invalid initialization mode \'{mode}\'') std = nn.init.calculate_gain(nonlinearity, a) / math.sqrt(fan) bound = math.sqrt(3.) * std return nn.init.uniform_(tensor, -bound, bound) return initializer def kaiming_normal_(a=0, mode='fan_in', nonlinearity='leaky_relu'): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification` - He, K. et al. (2015), using a normal distribution. The resulting tensor will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where .. math:: \text{std} = \frac{\text{gain}}{\sqrt{\text{fan_mode}}} Also known as 'He initialization'. Args: a (int): the negative slope of the rectifier used after this layer (only used with ``'leaky_relu'``). mode (str, optional): either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'`` preserves the magnitude of the variance of the weights in the forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the backwards pass. nonlinearity (str, optional): the non-linear function (`nn.functional` name), recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default). """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): if 0 in tensor.shape: warnings.warn("Initializing zero-element tensors is a no-op") return tensor if mode == 'fan_in': assert fan_in is not None, 'Fan_in is not provided.' fan = fan_in elif mode == 'fan_out': assert fan_out is not None, 'Fan_out is not provided.' fan = fan_out else: raise ValueError(f'Invalid initialization mode \'{mode}\'') std = nn.init.calculate_gain(nonlinearity, a) / math.sqrt(fan) return nn.init.normal_(tensor, 0, std) return initializer def xavier_uniform_(a: float = math.sqrt(3.), scale: float = 2., gain: float = 1.): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Understanding the difficulty of training deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010), using a uniform distribution. The resulting tensor will have values sampled from :math:`\mathcal{U}(-a, a)` where .. math:: a = \text{gain} \times \sqrt{\frac{6}{\text{fan_in} + \text{fan_out}}} Also known as 'Glorot initialization'. Args: a (float, optional): an optional scaling factor used to calculate uniform bounds from standard deviation. Defaults ``math.sqrt(3.)``. scale (float, optional): an optional scaling factor used to calculate standard deviation. Defaults 2.0. gain (float, optional): an optional scaling factor. Defaults 1.0. """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, 'Fan_in is not provided.' fan = fan_in if fan_out is not None: fan += fan_out std = gain * math.sqrt(scale / float(fan)) bound = a * std return nn.init.uniform_(tensor, -bound, bound) return initializer def xavier_normal_(scale: float = 2., gain: float = 1.): r"""Return the initializer filling the input `Tensor` with values according to the method described in `Understanding the difficulty of training deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010), using a normal distribution. The resulting tensor will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where .. math:: \text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan_in} + \text{fan_out}}} Also known as 'Glorot initialization'. Args: scale (float, optional): an optional scaling factor used to calculate standard deviation. Defaults 2.0. gain (float, optional): an optional scaling factor. Defaults 1.0. """ # adapted from torch.nn.init def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, 'Fan_in is not provided.' fan = fan_in if fan_out is not None: fan += fan_out std = gain * math.sqrt(scale / float(fan)) return nn.init.normal_(tensor, 0., std) return initializer def lecun_uniform_(): # adapted from jax.nn.initializers def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, 'Fan_in is not provided.' var = 1.0 / fan_in bound = math.sqrt(3 * var) return nn.init.uniform_(tensor, -bound, bound) return initializer def lecun_normal_(): # adapted from jax.nn.initializers def initializer(tensor: Tensor, fan_in: int = None, fan_out: int = None): assert fan_in is not None, 'Fan_in is not provided.' std = math.sqrt(1.0 / fan_in) return nn.init.trunc_normal_(tensor, std=std / .87962566103423978) return initializer
from typing import Optional import torch from colossalai.gemini.chunk import init_chunk_manager from colossalai.gemini.gemini_mgr import GeminiManager from colossalai.gemini.memory_tracer import MemStats from .data_parallel import ZeroDDP class GeminiDDP(ZeroDDP): def __init__(self, module: torch.nn.Module, device: torch.device, placement_policy: str = "cpu", pin_memory: bool = False, force_outputs_fp32: bool = False, strict_ddp_mode: bool = False, search_range_mb: int = 32, hidden_dim: Optional[int] = None, min_chunk_size_mb: float = 32, memstats: Optional[MemStats] = None) -> None: """ A torch.Module warpper using ZeRO-DP and Genimi. ZeRO is for parallel. Gemini is for memory management. WARNING: The class will modify the module inline! Example: model is initialized under the context of ColoInitContext >>> model = GeminiDDP(model, torch.cuda.current_device(), "cuda") >>> logits = model(x) >>> loss = criterion(logits, labels) >>> model.backward(loss) Args: module (torch.nn.Module): the model to be wrapped. device (torch.device): device to place the model. placement_policy (str, optional): "cpu", "cuda", "auto". Defaults to "cpu". pin_memory (bool, optional): use pin memory on CPU. Defaults to False. force_outputs_fp32 (bool, optional): force outputs are fp32. Defaults to False. search_range_mb (int, optional): chunk size searching range in MegaByte. Defaults to 32. hidden_dim (int, optional): the hidden dimension of DNN. Users can provide this argument to speed up searching. If users do not know this argument before training, it is ok. We will use a default value 1024. min_chunk_size_mb (float, optional): the minimum chunk size in MegaByte. If the aggregate size of parameters is still samller than the minimum chunk size, all parameters will be compacted into one small chunk. memstats (MemStats, optional) the memory statistics collector by a runtime memory tracer. """ # some ugly hotfix for the compatibility with Lightning if search_range_mb is None: search_range_mb = 32 chunk_manager = init_chunk_manager(model=module, init_device=device, hidden_dim=hidden_dim, search_range_mb=search_range_mb, min_chunk_size_mb=min_chunk_size_mb, strict_ddp_flag=strict_ddp_mode) gemini_manager = GeminiManager(placement_policy, chunk_manager, memstats) super().__init__(module, gemini_manager, pin_memory, force_outputs_fp32, strict_ddp_mode)
import itertools from collections import OrderedDict from functools import partial from typing import Dict, Iterable, List, Optional, Set import torch import torch.distributed as dist import torch.nn as nn from colossalai.gemini.chunk import Chunk, ChunkManager, TensorState from colossalai.gemini.gemini_mgr import GeminiManager from colossalai.gemini.memory_tracer import OrderedParamGenerator from colossalai.logging import get_dist_logger from colossalai.nn.parallel.utils import get_temp_total_chunk_on_cuda from colossalai.tensor import ProcessGroup as ColoProcessGroup from colossalai.tensor import ReplicaSpec from colossalai.tensor.colo_parameter import ColoParameter, ColoTensor, ColoTensorSpec from colossalai.tensor.param_op_hook import ColoParamOpHookManager from colossalai.utils import get_current_device, is_ddp_ignored from colossalai.zero.utils.gemini_hook import GeminiZeROHook from .reducer import Reducer from .utils import get_static_torch_model try: from torch.nn.modules.module import _EXTRA_STATE_KEY_SUFFIX, _IncompatibleKeys except ImportError: _EXTRA_STATE_KEY_SUFFIX = '_extra_state' 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) def _cast_float(args, dtype: torch.dtype): if isinstance(args, torch.Tensor) and torch.is_floating_point(args): args = args.to(dtype) elif isinstance(args, (list, tuple)): args = type(args)(_cast_float(t, dtype) for t in args) elif isinstance(args, dict): args = {k: _cast_float(v, dtype) for k, v in args.items()} return args class ColoDDP(torch.nn.Module): """Distributed data parallel for ColoTensor. Nested ColoDDP is not supported now. Example: >>> from colossalai.core import global_context as gpc >>> from colossalai.context import ParallelMode >>> model = torch.nn.Linear(20, 1) >>> pg = ProcessGroup(tp_degree = world_size//2) >>> model = ColoDDP(model, pg) >>> logits = model(x) >>> loss = criterion(logits, labels) >>> model.backward(loss) Args: module (torch.nn.Module): Module to apply DDP. process_group (Optional[dist.ProcessGroup], optional): The process group which DDP uses. If it's None, the default data parallel group will be used. Defaults to None. """ def __init__(self, module: torch.nn.Module, process_group: ColoProcessGroup, bucket_cap_mb: int = 25, rebuild_bucket: bool = True) -> None: assert not isinstance(module, ColoDDP) super().__init__() self.module = module self.comm_stream: torch.cuda.Stream = torch.cuda.Stream() assert process_group self.process_group = process_group self.dp_world_size = self.process_group.dp_world_size() self.reducer = Reducer(bucket_cap_mb) self.rebuild_bucket = rebuild_bucket for p in module.parameters(): if is_ddp_ignored(p): continue if p.requires_grad: p.register_hook(partial(self.grad_handle, p)) def parameters(self, recurse: bool = True): return self.module.parameters(recurse) def named_parameters(self, prefix: str = '', recurse: bool = True): return self.module.named_parameters(prefix, recurse) def named_buffers(self, prefix: str = '', recurse: bool = True): return self.module.named_buffers(prefix, recurse) def named_children(self): return self.module.named_children() def named_modules(self, memo: Optional[Set[torch.nn.Module]] = None, prefix: str = '', remove_duplicate: bool = True): return self.module.named_modules(memo, prefix, remove_duplicate) def forward(self, *args, **kwargs): self.module.zero_grad(set_to_none=True) return self.module(*args, **kwargs) def backward(self, loss: torch.Tensor): loss.backward() with torch.cuda.stream(self.comm_stream): self.reducer.flush() torch.cuda.current_stream().wait_stream(self.comm_stream) if self.rebuild_bucket: self.reducer.free() for p in self.module.parameters(): if is_ddp_ignored(p): continue if p.grad.device.type != "cpu": p.grad = p._saved_grad def grad_handle(self, p, grad): if grad.device.type != "cpu": empty_grad = torch.empty_like(grad) free_storage(empty_grad) if self.dp_world_size > 1: grad = grad / self.dp_world_size self.comm_stream.wait_stream(torch.cuda.current_stream()) with torch.cuda.stream(self.comm_stream): self.reducer.all_reduce_async(grad, group=self.process_group.dp_process_group(), callback_fn=partial(self._save_grad, p)) grad.record_stream(self.comm_stream) else: ColoDDP._save_grad(p, grad) return empty_grad else: # TODO(jiaruifang) fixme self.process_group.set_cpu_groups() dist.all_reduce(grad, group=self.process_group.cpu_dp_process_group()) return grad @staticmethod def _save_grad(p, grad): if hasattr(p, '_saved_grad'): p._saved_grad.add_(grad) else: p._saved_grad = grad def zero_grad(self, set_to_none: bool = False) -> None: self.module.zero_grad(set_to_none=True) for p in self.module.parameters(): if getattr(p, '_saved_grad', None) is not None: if set_to_none: p._saved_grad = None else: if p._saved_grad.grad_fn is not None: p._saved_grad.detach_() else: p._saved_grad.requires_grad_(False) p._saved_grad.zero_() @staticmethod def set_params_to_ignore(params_to_ignore: Iterable[torch.Tensor]) -> None: """Sets parameters to be ignored by DDP. This method must be called before initializing ColoDDP. Example: >>> params_to_ignore = [] >>> for p in module.parameters(): >>> if should_ignore(p): >>> params_to_ignore.append(p) >>> ColoDDP.set_params_to_ignore(params_to_ignore) >>> module = ColoDDP(module) Args: params_to_ignore (Iterable[torch.Tensor]): A list of parameters to be ignored. """ for p in params_to_ignore: p._ddp_to_ignore = True def state_dict(self, destination=None, prefix='', keep_vars=False): return self.module.state_dict(destination=destination, prefix=prefix, keep_vars=keep_vars) def load_state_dict(self, state_dict: 'OrderedDict[str, torch.Tensor]', strict: bool = True): return self.module.load_state_dict(state_dict, strict) class ZeroDDP(ColoDDP): """ZeRO DDP for ColoTensor. Warning: Nested ZeroDDP is not supported now. It is designed to be used with ChunkManager and GeminiManager. For more details, see the API reference of ``ChunkManager`` and ``GeminiManager``. Args: module (torch.nn.Module): Module to apply ZeRO-DP. gemini_manager (GeminiManager): Manages the chunk manager and heterogeneous momery space. For more details, see the API reference of ``GeminiManager``. pin_memory (bool): Chunks on CPU Memory use pin-memory. force_outputs_fp32 (bool): If set to True, outputs will be fp32. Otherwise, outputs will be fp16. Defaults to False. strict_ddp_mode (bool): If set to True, there is no tensor sharding, each tensor is replicated. Defaults to False. Users can set it to True, when they clearly know that they only need DDP. """ def __init__(self, module: torch.nn.Module, gemini_manager: GeminiManager, pin_memory: bool = False, force_outputs_fp32: bool = False, strict_ddp_mode: bool = False) -> None: super().__init__(module, process_group=ColoProcessGroup()) self.gemini_manager = gemini_manager self.chunk_manager: ChunkManager = gemini_manager.chunk_manager self.force_outputs_fp32 = force_outputs_fp32 self.param_op_hook = GeminiZeROHook(gemini_manager) self.fp32_params: List[ColoTensor] = list() self.fp16_params: List[ColoParameter] = list() self.overflow_counter = 0 self.grads_device: Dict[torch.Tensor, torch.device] = dict() self.param2name: Dict[nn.Parameter, str] = dict() self.name2param: Dict[str, nn.Parameter] = dict() self._cast_buffers() self._logger = get_dist_logger() if self.gemini_manager._premade_memstats_: # build chunk in param runtime visited order. param_order = self.gemini_manager.memstats()._param_runtime_order else: # build chunk in param initialized order. # Note: in this way, it can not get filter unused params during runtime. param_order = OrderedParamGenerator() for p in module.parameters(): param_order.append(p) self._init_chunks(param_order=param_order, strict_ddp_mode=strict_ddp_mode, cpu_offload=self.gemini_manager.policy_name != 'cuda', pin_memory=pin_memory) for name, param in module.named_parameters(): self.param2name[param] = name for m_name, m_var in module.named_modules(): for p_name, p_var in m_var.named_parameters(recurse=False): param_name = m_name + '.' + p_name if m_name else p_name self.name2param[param_name] = p_var def _post_forward(self): """This function is only triggered for inference. """ access_list = list(self.chunk_manager.accessed_chunks) # we need to scatter all accessed chunks and move them to their original places for chunk in access_list: if chunk.keep_gathered: self.chunk_manager.fake_release_chunk(chunk) else: assert chunk.can_release self.chunk_manager.release_chunk(chunk) first_param = next(iter(chunk.tensors_info)) self.chunk_manager.move_chunk(chunk, self.grads_device[first_param]) assert self.chunk_manager.accessed_mem == 0 # reset all recorded attributes self.gemini_manager.reset_attributes() def forward(self, *args, **kwargs): # check whether we are in a inference mode grad_flag = torch.is_grad_enabled() if not grad_flag: assert not self.gemini_manager.need_warmup or not self.gemini_manager.is_warmup( ), "You should run a completed iteration as your warmup iter" args, kwargs = _cast_float(args, torch.half), _cast_float(kwargs, torch.half) self.module.zero_grad(set_to_none=True) self.gemini_manager.pre_iter(*args) with ColoParamOpHookManager.use_hooks(self.param_op_hook): outputs = self.module(*args, **kwargs) # scatter chunks in the inference mode if not grad_flag: self._post_forward() if self.force_outputs_fp32: return _cast_float(outputs, torch.float) return outputs def _setup_grads_ptr(self): for p in self.module.parameters(): if is_ddp_ignored(p): continue p.grad = None def _pre_bacward(self): # set a visit label for all parameters # the label is used to check whether the parameter is correctly reduced for param in self.param2name: if not is_ddp_ignored(param): setattr(param, "_gemini_reduced", False) def _post_backward(self): if self.chunk_manager.accessed_mem != 0: error_params = ["Reduction failed at followed parameters:"] for param in self.param2name: if not is_ddp_ignored(param) and not getattr(param, "_gemini_reduced"): error_params.append(self.param2name[param]) error_str = "\n\t".join(error_params) raise RuntimeError("ZERO DDP error: the synchronization of gradients doesn't exit properly.", "The most possible reason is that the model is not compatible with ZeroDDP.\n", f"{error_str}") self._setup_grads_ptr() self._logger.debug( f'comp cuda demand time: {self.gemini_manager._comp_cuda_demand_time}, layout time: {self.gemini_manager._layout_time}, evict time: {self.gemini_manager._evict_time}, CPU->CUDA vol: {self.gemini_manager._h2d_volume}B, CUDA->CPU vol: {self.gemini_manager._d2h_volume}' ) self.gemini_manager.post_iter() def backward(self, loss: torch.Tensor): self._pre_bacward() with self.param_op_hook.switch_to_backward(), ColoParamOpHookManager.use_hooks(self.param_op_hook): loss.backward() self._post_backward() def backward_by_grad(self, tensor, grad): with self.param_op_hook.switch_to_backward(), ColoParamOpHookManager.use_hooks(self.param_op_hook): torch.autograd.backward(tensor, grad) self._post_backward() def grad_handle(self, p, grad): empty_grad = torch.empty_like(grad) free_storage(empty_grad) with torch._C.DisableTorchFunction(): chunk = self.chunk_manager.get_chunk(p) if chunk.tensors_info[p].state != TensorState.HOLD_AFTER_BWD: raise RuntimeError(f"Parameter `{self.param2name[p]}` failed at the gradient reduction. " "Some unsupported torch function is operated upon this parameter.") self.chunk_manager.trans_tensor_state(p, TensorState.READY_FOR_REDUCE) chunk.copy_tensor_to_chunk_slice(p, grad) reduced = self.chunk_manager.reduce_chunk(chunk) if reduced: if chunk.is_gathered: chunk.cuda_global_chunk.div_(chunk.pg_size) else: chunk.cuda_shard.div_(chunk.pg_size) # check overflow elements self.overflow_counter += chunk.has_inf_or_nan # record l2 norm for gradient clipping if chunk.l2_norm_flag: chunk.set_l2_norm() self.chunk_manager.move_chunk(chunk, self.grads_device[p], force_copy=True) return empty_grad def zero_grad(self, set_to_none: bool = False) -> None: self.module.zero_grad(set_to_none=True) def set_chunk_grad_device(self, chunk: Chunk, device: torch.device) -> None: for tensor in chunk.get_tensors(): self.grads_device[tensor] = device def state_dict(self, destination=None, prefix='', keep_vars=False, only_rank_0: bool = True): """Returns a dictionary containing a whole state of the module. Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to ``None`` are not included. Warning: The non strict state dict would ignore the parameters if the tensors of the parameters are shared with other parameters which have been included in the dictionary. When you need to load the state dict, you should set the argument `strict` to False. Returns: dict: a dictionary containing a whole state of the module """ if destination is None: destination = OrderedDict() destination._metadata = OrderedDict() destination._metadata[prefix[:-1]] = local_metadata = dict(version=self._version) self._save_to_state_dict(destination, prefix, keep_vars, only_rank_0) for hook in self._state_dict_hooks.values(): hook_result = hook(self, destination, prefix, local_metadata) if hook_result is not None: destination = hook_result return destination def _get_param_to_save_data(self, param_list: List[torch.nn.Parameter], only_rank_0: bool) -> Dict: """ get param content from chunks. Args: param_list (_type_): a list of torch.nn.Parameters only_rank_0 (_type_): _description_ Returns: Dict: a dict whose key is param name and value is param with correct payload """ # save parameters param_to_save_data = dict() chunk_list = self.chunk_manager.get_chunks(param_list) for chunk in chunk_list: temp_chunk = get_temp_total_chunk_on_cuda(chunk) for tensor, tensor_info in chunk.tensors_info.items(): record_tensor = torch.empty([0]) record_flag = (not only_rank_0) | (dist.get_rank(chunk.torch_pg) == 0) if record_flag: record_tensor = temp_chunk[tensor_info.offset:tensor_info.end].view(tensor.shape).cpu() assert tensor not in param_to_save_data param_to_save_data[tensor] = record_tensor del temp_chunk return param_to_save_data def _save_to_state_dict(self, destination, prefix, keep_vars, only_rank_0=True): r"""Saves module state to `destination` dictionary, containing a state of the module, but not its descendants. This is called on every submodule in :meth:`~torch.nn.Module.state_dict`. In rare cases, subclasses can achieve class-specific behavior by overriding this method with custom logic. Args: destination (dict): a dict where state will be stored prefix (str): the prefix for parameters and buffers used in this module """ assert keep_vars is False, "`state_dict` with parameter, `keep_vars=True`, is not supported now." # get copies of fp32 parameters in CPU param_to_save_data = self._get_param_to_save_data(self.fp32_params, only_rank_0) # get the mapping between copies and fp16 parameters p_mapping = dict() for p, fp32_p in zip(self.fp16_params, self.fp32_params): name = self.param2name[p] assert fp32_p in param_to_save_data, "Parameter '{}' is neglected in the chunk list".format(name) record_parameter = param_to_save_data[fp32_p] p_mapping[p] = record_parameter for name, param in self.name2param.items(): if param is not None: if is_ddp_ignored(param): # deal with ddp ignored parameters destination[prefix + name] = param if keep_vars else param.detach() else: destination[prefix + name] = p_mapping[param] del p_mapping del param_to_save_data # save all buffers for name, buf in self.named_buffers(): if buf is not None and name not in self._non_persistent_buffers_set: destination[prefix + name] = buf if keep_vars else buf.detach() # save extra states extra_state_key = prefix + _EXTRA_STATE_KEY_SUFFIX if getattr(self.__class__, "get_extra_state", torch.nn.Module.get_extra_state) is not torch.nn.Module.get_extra_state: destination[extra_state_key] = self.get_extra_state() def load_state_dict(self, state_dict: 'OrderedDict[str, torch.Tensor]', strict: bool = True): r"""Copies parameters and buffers from :attr:`state_dict` into this module and its descendants. If :attr:`strict` is ``True``, then the keys of :attr:`state_dict` must exactly match the keys returned by this module's :meth:`~torch.nn.Module.state_dict` function. Args: state_dict (dict): a dict containing parameters and persistent buffers. strict (bool, optional): whether to strictly enforce that the keys in :attr:`state_dict` match the keys returned by this module's :meth:`~torch.nn.Module.state_dict` function. Default: ``True`` Returns: ``NamedTuple`` with ``missing_keys`` and ``unexpected_keys`` fields: * **missing_keys** is a list of str containing the missing keys * **unexpected_keys** is a list of str containing the unexpected keys Note: If a parameter or buffer is registered as ``None`` and its corresponding key exists in :attr:`state_dict`, :meth:`load_state_dict` will raise a ``RuntimeError``. """ missing_keys: List[str] = [] unexpected_keys: List[str] = [] error_msgs: List[str] = [] # copy state_dict so _load_from_state_dict can modify it metadata = getattr(state_dict, '_metadata', None) state_dict = state_dict.copy() if metadata is not None: # mypy isn't aware that "_metadata" exists in state_dict state_dict._metadata = metadata # type: ignore[attr-defined] prefix = '' local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {}) self._load_from_state_dict(state_dict, prefix, local_metadata, True, missing_keys, unexpected_keys, error_msgs) if strict: if len(unexpected_keys) > 0: error_msgs.insert( 0, 'Unexpected key(s) in state_dict: {}. '.format(', '.join( '"{}"'.format(k) for k in unexpected_keys))) if len(missing_keys) > 0: error_msgs.insert( 0, 'Missing key(s) in state_dict: {}. '.format(', '.join('"{}"'.format(k) for k in missing_keys))) if len(error_msgs) > 0: raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format( self.__class__.__name__, "\n\t".join(error_msgs))) return _IncompatibleKeys(missing_keys, unexpected_keys) def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs): 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.named_buffers() if k not in self._non_persistent_buffers_set} local_name_params = itertools.chain(self.named_parameters(), persistent_buffers.items()) local_state = {k: v for k, v in local_name_params if v is not None} def load(param_name, dest_tensor, copy_func): state_key = prefix + param_name if state_key in state_dict: input_param = state_dict[state_key] # Backward compatibility: loading 1-dim tensor from 0.3.* to version 0.4+ if len(dest_tensor.shape) == 0 and len(input_param.shape) == 1: input_param = input_param[0] if input_param.shape != dest_tensor.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(state_key, input_param.shape, dest_tensor.shape)) return try: with torch.no_grad(): copy_func(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(state_key, dest_tensor.size(), input_param.size(), ex.args)) elif strict: missing_keys.append(state_key) def load_fp32_parameter(chunk_slice, data): chunk_slice.copy_(data.flatten()) for name, param in self.named_parameters(): if is_ddp_ignored(param): # deal with ddp ignored parameters load(name, param, param.copy_) fp32_to_name = dict() for p, fp32_p in zip(self.fp16_params, self.fp32_params): if p is not None: name = self.param2name[p] fp32_to_name[fp32_p] = name chunk_list = self.chunk_manager.get_chunks(self.fp32_params) for chunk in chunk_list: temp_chunk = get_temp_total_chunk_on_cuda(chunk) for tensor, tensor_info in chunk.tensors_info.items(): parameter_name = fp32_to_name[tensor] parameter_slice = temp_chunk[tensor_info.offset:tensor_info.end] load(parameter_name, tensor, partial(load_fp32_parameter, parameter_slice)) if chunk.is_gathered: chunk.cuda_global_chunk.copy_(temp_chunk) elif chunk.cuda_shard is not None: chunk.cuda_shard.copy_(temp_chunk[chunk.shard_begin:chunk.shard_end]) else: chunk.cpu_shard.copy_(temp_chunk[chunk.shard_begin:chunk.shard_end]) del temp_chunk for chunk_32 in chunk_list: chunk_16 = chunk_32.paired_chunk assert chunk_16 is not None chunk_16.optim_update() for name, buf in persistent_buffers.items(): if buf is not None: load(name, buf, buf.copy_) extra_state_key = prefix + _EXTRA_STATE_KEY_SUFFIX if getattr(self.__class__, "set_extra_state", torch.nn.Module.set_extra_state) is not torch.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):] if input_name not in local_state: unexpected_keys.append(key) def _init_chunks(self, param_order, strict_ddp_mode: bool, cpu_offload: bool, pin_memory: bool): ddp_pg = ColoProcessGroup() for p in param_order.generate(): assert isinstance(p, ColoParameter) # gather sharded parameters in the strict ddp mode if strict_ddp_mode: if not p.is_replicate(): p.set_dist_spec(ReplicaSpec()) p.set_process_group(pg=ddp_pg) # ignore the parameters with no gradient if not p.requires_grad: self.set_params_to_ignore([p]) # move ignored parameters to CUDA if is_ddp_ignored(p): p.data = p.data.to(device=get_current_device(), dtype=torch.float16) continue # create a fp32 parameter fp32_data = p.data.float() fp32_p = ColoTensor(fp32_data, spec=ColoTensorSpec(p.process_group)) # create a fp16 parameter p.data = p.data.half() # register the fp16 parameter and fp32 parameter in the chunk manager dp_world_size = p.process_group.dp_world_size() self.chunk_manager.register_tensor(tensor=p, group_type='fp16_param', config_key=dp_world_size, cpu_offload=cpu_offload, pin_memory=pin_memory) self.chunk_manager.register_tensor(tensor=fp32_p, group_type='fp32_param', config_key=dp_world_size, cpu_offload=cpu_offload, pin_memory=pin_memory) self.fp16_params.append(p) self.fp32_params.append(fp32_p) self.grads_device[p] = self.gemini_manager.default_device self.chunk_manager.close_all_groups() for p, fp32_p in zip(self.fp16_params, self.fp32_params): chunk_16 = self.chunk_manager.get_chunk(p) chunk_32 = self.chunk_manager.get_chunk(fp32_p) chunk_32.init_pair(chunk_16) # keep gathered chunks are in CUDA if chunk_16.keep_gathered: self.grads_device[p] = get_current_device() def _cast_buffers(self): for buffer in self.module.buffers(): buffer.data = buffer.cuda() if torch.is_floating_point(buffer): buffer.data = buffer.half()
from .data_parallel import ColoDDP, ZeroDDP from .gemini_parallel import GeminiDDP from .zero_wrapper import zero_model_wrapper, zero_optim_wrapper __all__ = ['ColoDDP', 'ZeroDDP', 'GeminiDDP', 'zero_model_wrapper', 'zero_optim_wrapper']
# 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 from typing import Callable, Dict, List, Optional, Tuple import torch import torch.distributed as dist from torch import Tensor from torch.distributed import ProcessGroup class Bucket: def __init__(self, size: int, dtype: torch.dtype, device: torch.device, group: ProcessGroup): self.buffer = torch.zeros(size, dtype=dtype, device=device) self.group = group self.offset = 0 self.callbacks: List[Callable] = [] def flush(self) -> None: """Flush content of the bucket.""" if self.offset == 0: assert len(self.callbacks) == 0 return # reduce-scatter bucket dist.all_reduce(self.buffer[:self.offset], 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.offset = 0 self.callbacks.clear() self.buffer = torch.zeros_like(self.buffer) def alloc(self) -> None: if self.buffer.storage().size() == 0: self.buffer.storage().resize_(self.buffer.numel()) def free(self) -> None: assert self.offset == 0 and self.callbacks == [], "Incorrect call of teardown" self.buffer.storage().resize_(0) def append(self, tensor: Tensor, callback_fn: Callable): tensor_size = tensor.numel() offset = self.offset self.buffer[offset:offset + tensor_size].copy_(tensor.flatten()) self.offset += tensor_size # callback will be given the reduced result if callback_fn is not None: result_view = self.buffer[offset:offset + tensor_size].view(tensor.shape) self.callbacks.append(functools.partial(callback_fn, result_view)) @property def avail_size(self) -> int: return self.buffer.size(0) - self.offset class Reducer: 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 all_reduce_async( self, tensor: Tensor, group: ProcessGroup, callback_fn: Optional[Callable] = None, ) -> None: bucket_size = self._get_bucket_size(tensor.element_size()) if tensor.numel() >= bucket_size: dist.all_reduce(tensor, group=group) if callback_fn is not None: callback_fn(tensor) return bucket = self._get_bucket(tensor, group) if tensor.numel() > bucket.avail_size: # not enough space remaining in bucket, flush it now bucket.flush() bucket.append(tensor, callback_fn) @torch.no_grad() def flush(self) -> None: for bucket in self.buckets.values(): bucket.flush() @torch.no_grad() def free(self) -> None: for bucket in self.buckets.values(): bucket.free() @functools.lru_cache() def _get_bucket_size(self, element_size: 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) def _get_bucket(self, tensor: Tensor, group: ProcessGroup) -> Bucket: key = (tensor.dtype, tensor.device, group) if key not in self.buckets: bucket_size = self._get_bucket_size(tensor.element_size()) self.buckets[key] = Bucket(bucket_size, tensor.dtype, tensor.device, group) self.buckets[key].alloc() return self.buckets[key]
from collections import OrderedDict from copy import copy from typing import Optional, Set import torch import torch.distributed as dist import torch.nn as nn from colossalai.gemini.chunk import Chunk from colossalai.utils import get_current_device def get_temp_total_chunk_on_cuda(chunk: Chunk): if chunk.is_gathered: return chunk.cuda_global_chunk if chunk.cuda_shard is not None: shard_temp = chunk.cuda_shard else: shard_temp = chunk.cpu_shard.to(get_current_device()) total_temp = torch.zeros(chunk.chunk_size, dtype=chunk.dtype, device=get_current_device()) gather_list = list(torch.chunk(input=total_temp, chunks=chunk.pg_size, dim=0)) dist.all_gather(tensor_list=gather_list, tensor=shard_temp, group=chunk.torch_pg) return total_temp def _get_dfs_module_list(module: nn.Module, memo: Optional[Set[nn.Module]] = None, prefix: str = ''): """Get a dfs module list of the given module. Its order is same as the order of creations of modules. """ if memo is None: memo = set() if module not in memo: for name, submodule in module._modules.items(): if submodule is None: continue submodule_prefix = prefix + ('.' if prefix else '') + name for m in _get_dfs_module_list(submodule, memo, submodule_prefix): yield m memo.add(module) yield prefix, module def _get_shallow_copy_model(model: nn.Module): """Get a shallow copy of the given model. Each submodule is different from the original submodule. But the new submodule and the old submodule share all attributes. """ old_to_new = dict() for name, module in _get_dfs_module_list(model): new_module = copy(module) new_module._modules = OrderedDict() for subname, submodule in module._modules.items(): if submodule is None: continue setattr(new_module, subname, old_to_new[submodule]) old_to_new[module] = new_module return old_to_new[model] def get_static_torch_model(zero_ddp_model, device=torch.device("cpu"), dtype=torch.float32, only_rank_0=True) -> torch.nn.Module: """Get a static torch.nn.Module model from the given ZeroDDP module. You should notice that the original ZeroDDP model is not modified. Thus, you can use the original model in further training. But you should not use the returned torch model to train, this can cause unexpected errors. Args: zero_ddp_model (ZeroDDP): a zero ddp model device (torch.device): the device of the final torch model dtype (torch.dtype): the dtype of the final torch model only_rank_0 (bool): if True, only rank0 has the coverted torch model Returns: torch.nn.Module: a static torch model used for saving checkpoints or numeric checks """ from colossalai.nn.parallel import ZeroDDP assert isinstance(zero_ddp_model, ZeroDDP) state_dict = zero_ddp_model.state_dict(only_rank_0=only_rank_0) colo_model = zero_ddp_model.module torch_model = _get_shallow_copy_model(colo_model) if not only_rank_0 or dist.get_rank() == 0: for (name, colo_module), (_, torch_module) in \ zip(_get_dfs_module_list(colo_model), _get_dfs_module_list(torch_model)): # clean the parameter list of the new torch module torch_module._parameters = OrderedDict() for sufix_param_name, param in colo_module.named_parameters(recurse=False): # get the full name of the parameter full_param_name = name + ('.' if name else '') + sufix_param_name assert full_param_name in state_dict, \ f"Can not find parameter `{full_param_name}` in the GeminiDDP module" state_param = state_dict[full_param_name] torch_param = torch.nn.Parameter(state_param.data.to(device=device, dtype=dtype)) setattr(torch_module, sufix_param_name, torch_param) dist.barrier() return torch_model
from copy import copy from typing import Dict, Optional import torch import torch.nn as nn from .gemini_parallel import GeminiDDP def zero_model_wrapper(model: nn.Module, zero_stage: int = 1, gemini_config: Optional[Dict] = None): """This wrapper function is used to wrap your training model for ZeRO DDP. Example: >>> with ColoInitContext(): >>> my_model = Bert() >>> my_optim = SGD(my_model.parameters(), lr = 1e-3) >>> zero_model = zero_model_wrapper(my_model, zero_stage=1) >>> zero_optim = zero_optim_wrapper(zero_model, my_optim) Args: model (nn.Module): The model used in ZeRO DDP. zero_stage (int, optional): The stage of ZeRO DDP. You can find more information in ZeRO's paper. https://arxiv.org/abs/1910.02054 gemini_config (dict, optional): The configuration dictionary of `GeminiDDP`. `GeminiDDP` is enabled when the stage is set to 3. You can set the arguemnts of `GeminiDDP` in the gemini_config. Here is an example where we set the device of the model, the placement policy of Gemini, and the size of hidden dimension to help Gemini find out a unified chunk size. Example: >>> config_dict = dict(device=torch.cuda.current_device(), hidden_dim=1024, placement_policy='auto') >>> model = zero_model_wrapper(model, zero_stage=3, gemini_config=config_dict) """ assert zero_stage in [1, 2, 3], "The stage of ZeRO should be 1, 2 or 3" if gemini_config is None: gemini_config = dict() if zero_stage in [1, 2]: wrapped_model = model else: wrapped_model = GeminiDDP(model, **gemini_config) setattr(wrapped_model, "_colo_zero_stage", zero_stage) return wrapped_model def zero_optim_wrapper(model: nn.Module, optimizer: torch.optim.Optimizer, initial_scale: float = 2**16, growth_factor: float = 2, backoff_factor: float = 0.5, growth_interval: int = 1000, hysteresis: int = 2, min_scale: float = 1, max_scale: float = 2**32, max_norm: float = 0.0, norm_type: float = 2.0, optim_config: Optional[Dict] = None): """This wrapper function is used to wrap your training optimizer for ZeRO DDP. Args: model (nn.Module): Your model wrapped by `zero_model_wrapper` optimizer (torch.optim.Optimizer): Your initialized optimizer initial_scale (float, optional): initial_scale used by DynamicGradScaler. min_scale (float, optional): min_scale used by DynamicGradScaler. growth_factor (float, optional): growth_factor used by DynamicGradScaler. backoff_factor (float, optional): backoff_factor used by DynamicGradScaler. growth_interval (float, optional): growth_interval used by DynamicGradScaler. hysteresis (float, optional): hysteresis used by DynamicGradScaler. max_scale (int, optional): max_scale used by DynamicGradScaler. max_norm (float, optional): max_norm used for `clip_grad_norm`. You should notice that you shall not do clip_grad_norm by yourself when using ZeRO DDP. The ZeRO optimizer will take care of clip_grad_norm. norm_type (float, optional): norm_type used for `clip_grad_norm`. optim_config (dict, optinoal): The configuration used for the ZeRO optimizer. Example: >>> zero2_config = dict(reduce_bucket_size=12 * 1024 * 1024, overlap_communication=True) >>> optim = zero_optim_wrapper(model, optim, optim_config=zero2_config) """ assert hasattr(model, "_colo_zero_stage"), "You should use `zero_ddp_wrapper` first" zero_stage = getattr(model, "_colo_zero_stage") assert norm_type == 2.0, "Current ZeRO optimizers only support 'norm_type=2'" if optim_config is None: config_dict = dict() else: config_dict = copy(optim_config) config_dict['initial_scale'] = initial_scale config_dict['growth_factor'] = growth_factor config_dict['backoff_factor'] = backoff_factor config_dict['growth_interval'] = growth_interval config_dict['hysteresis'] = hysteresis config_dict['min_scale'] = min_scale config_dict['max_scale'] = max_scale if zero_stage in [1, 2]: from colossalai.zero.sharded_optim.low_level_optim import LowLevelZeroOptimizer config_dict['partition_grad'] = zero_stage == 2 config_dict['clip_grad_norm'] = max_norm return LowLevelZeroOptimizer(optimizer, **config_dict) else: from colossalai.nn.optimizer.zero_optimizer import ZeroOptimizer config_dict['clipping_norm'] = max_norm return ZeroOptimizer(optimizer, model, **config_dict)
from .colo_module import ColoModule from colossalai.tensor import ComputePattern, distspec, ProcessGroup, ShardSpec class ColoEmbedding(ColoModule): def __init__(self): super(ColoEmbedding, self).__init__() self._register_shard_params(['weight']) def register(self, compute_pattern, pg: ProcessGroup): if not compute_pattern in self._allowed_patterns: if ComputePattern.TP1D == compute_pattern: self._set_TP1D(pg) def _set_TP1D(self, pg: ProcessGroup): # TP1D Row Linear _compute_pattern = ComputePattern.TP1D self._register_allowed_patterns( compute_pattern=_compute_pattern, dist_specs={ 'weight': ShardSpec([0], [pg.tp_world_size()]), }, mode='row', ) # TP1D Col Linear self._register_allowed_patterns( compute_pattern=_compute_pattern, dist_specs={ 'weight': ShardSpec([-1], [pg.tp_world_size()]), }, mode='col', ) self._set_default(compute_pattern=_compute_pattern, target_mode='row')
from .colo_module import ColoModule from colossalai.tensor import ComputePattern, distspec, ProcessGroup, ShardSpec class ColoLinear(ColoModule): def __init__(self): super(ColoLinear, self).__init__() self._register_shard_params(['weight', 'bias']) def register(self, compute_pattern, pg: ProcessGroup): if not compute_pattern in self._allowed_patterns: if ComputePattern.TP1D == compute_pattern: self._set_TP1D(pg) def _set_TP1D(self, pg): # TP1D Row Linear _compute_pattern = ComputePattern.TP1D self._register_allowed_patterns( compute_pattern=_compute_pattern, dist_specs={ 'weight': ShardSpec([-1], [pg.tp_world_size()]), 'bias': None }, mode='row', ) # TP1D Col Linear self._register_allowed_patterns( compute_pattern=_compute_pattern, dist_specs={ 'weight': ShardSpec([0], [pg.tp_world_size()]), 'bias': ShardSpec([0], [pg.tp_world_size()]) }, mode='col', ) self._set_default(compute_pattern=_compute_pattern, target_mode='row')
from .colo_module import ColoModule from .linear import ColoLinear from .embedding import ColoEmbedding from .module_utils import register_colo_module, is_colo_module, get_colo_module, init_colo_module, check_colo_module from .cache_embedding import CachedEmbeddingBag, ParallelCachedEmbeddingBag, CachedParamMgr, LimitBuffIndexCopyer, EvictionStrategy, \ ParallelCachedEmbeddingBagTablewise, TablewiseEmbeddingBagConfig, ParallelCachedEmbeddingBagTablewiseSpiltCache __all__ = [ 'ColoModule', 'register_colo_module', 'is_colo_module', 'get_colo_module', 'init_colo_module', 'check_colo_module', 'ColoLinear', 'ColoEmbedding', 'CachedEmbeddingBag', 'ParallelCachedEmbeddingBag', 'CachedParamMgr', 'LimitBuffIndexCopyer', 'EvictionStrategy', 'ParallelCachedEmbeddingBagTablewise', 'TablewiseEmbeddingBagConfig', 'ParallelCachedEmbeddingBagTablewiseSpiltCache' ]
from typing import Dict from colossalai.tensor import ColoParameter, ComputeSpec, ProcessGroup from colossalai.tensor import distspec from . import ColoModule import torch _COLOSSAL_MODULES: Dict[type, ColoModule] = {} def register_colo_module(module_type: type, colo_module: ColoModule): global _COLOSSAL_MODULES _COLOSSAL_MODULES[module_type] = colo_module def is_colo_module(module: torch.nn.Module): global _COLOSSAL_MODULES for module_type in _COLOSSAL_MODULES.keys(): if isinstance(module, module_type): return True return False def get_colo_module(module: torch.nn.Module): global _COLOSSAL_MODULES if is_colo_module(module): for module_type, colo_module in _COLOSSAL_MODULES.items(): if isinstance(module, module_type): return colo_module else: return None def check_colo_module(module: torch.nn.Module, pg: ProcessGroup, recursive=True): if is_colo_module(module): colo_module = get_colo_module(module) param_names = colo_module.get_param_names() compute_pattern = None for param_name in param_names: param = module.get_parameter(param_name) if not isinstance(param, ColoParameter): raise Exception(f'Invalid ColoParameter spec: {param} in {module} is not a ColoParameter.') if param.has_compute_spec(): cur_compute_pattern = param.compute_spec.compute_pattern if compute_pattern is None: compute_pattern = cur_compute_pattern else: if cur_compute_pattern != compute_pattern: raise Exception( f'Invalid ColoParameter spec: Params in {module} have different compute_pattern.') else: continue if compute_pattern is not None: colo_module.register(compute_pattern, pg) if not colo_module.has_compute_pattern(compute_pattern): raise Exception( f'Invalid ColoParameter spec: ComputePattern {compute_pattern} in {module} is not allowed.') match_specs = False allowed_specs = colo_module.get_dist_specs(compute_pattern) for _, param_specs in allowed_specs.items(): cur_match = True for param_name, dist_spec in param_specs.items(): param = module.get_parameter(param_name) if param.has_compute_spec(): if dist_spec != param.dist_spec: cur_match = False break else: if dist_spec is not None: cur_match = False break if cur_match == True: match_specs = True break if match_specs == False: raise Exception(f'Invalid ColoParameter spec: Params in {module} are incorrectly sharded.') if recursive == True: for submodule in module.children(): check_colo_module(submodule, pg=pg, recursive=True) def init_colo_module(module: torch.nn.Module, compute_spec: ComputeSpec, pg: ProcessGroup, recursive=True, mode='default'): compute_pattern = compute_spec.compute_pattern if is_colo_module(module): # for each param # set its process_group, dist_spec and compute_spec colo_module = get_colo_module(module) colo_module.register(compute_pattern, pg) if not colo_module.has_compute_pattern_with_mode(compute_pattern, mode=mode): raise NotImplementedError # a set for modules which update at least one param in the init process. # these modules need to be checked whether all params still match one of the valid compute pattern. modules_update_param = {module} for param_name, dist_spec in colo_module.get_dist_specs_with_mode(compute_pattern, mode=mode).items(): if dist_spec is None: continue param = module.get_parameter(param_name) if isinstance(param, ColoParameter): param.set_process_group(pg) param.set_dist_spec(dist_spec) param.compute_spec = compute_spec for mod in param.shared_param_modules: modules_update_param.add(mod) for mod in modules_update_param: check_colo_module(mod, pg, recursive=False) if recursive == True: for submodule in module.children(): init_colo_module(submodule, compute_spec, pg=pg, recursive=True, mode=mode)
from colossalai.tensor.distspec import _DistSpec from colossalai.tensor import ComputePattern from typing import List, Dict class ColoModule(object): def __init__(self): self._shard_params: List[str] = [] self._allowed_patterns: Dict[ComputePattern, Dict[str, Dict[str, _DistSpec]]] = {} def _register_shard_params(self, params: List[str]): self._shard_params = params def _register_allowed_patterns(self, compute_pattern: ComputePattern, dist_specs: Dict[str, _DistSpec], mode='default'): assert list( dist_specs.keys()).sort() == self._shard_params.sort(), 'Every registered param should have dist_spec.' if not compute_pattern in self._allowed_patterns: self._allowed_patterns[compute_pattern] = {} self._allowed_patterns[compute_pattern][mode] = dist_specs def _set_default(self, compute_pattern: ComputePattern, target_mode): self._allowed_patterns[compute_pattern]['default'] = self._allowed_patterns[compute_pattern][target_mode] def has_compute_pattern(self, compute_pattern: ComputePattern): return compute_pattern in self._allowed_patterns def get_dist_specs(self, compute_pattern: ComputePattern): assert self.has_compute_pattern(compute_pattern) return self._allowed_patterns[compute_pattern] def has_compute_pattern_with_mode(self, compute_pattern: ComputePattern, mode='default'): return compute_pattern in self._allowed_patterns and mode in self._allowed_patterns[compute_pattern] def get_dist_specs_with_mode(self, compute_pattern: ComputePattern, mode='default'): assert self.has_compute_pattern_with_mode(compute_pattern, mode) return self._allowed_patterns[compute_pattern][mode] def get_param_names(self): return self._shard_params def register(self, compute_pattern, pg): raise NotImplementedError
import numpy as np import torch from torch.profiler import record_function from typing import List, Optional from contexttimer import Timer from .copyer import LimitBuffIndexCopyer from enum import Enum import sys from contextlib import contextmanager class EvictionStrategy(Enum): LFU = 1 # dataset aware eviction strategy DATASET = 2 def _wait_for_data(t, stream: Optional[torch.cuda.streams.Stream]) -> None: if stream is None: return torch.cuda.current_stream().wait_stream(stream) # As mentioned in https://pytorch.org/docs/stable/generated/torch.Tensor.record_stream.html, # PyTorch uses the "caching allocator" for memroy allocation for tensors. When a tensor is # freed, its memory is likely to be reused by newly constructed tenosrs. By default, # this allocator traces whether a tensor is still in use by only the CUDA stream where it # was created. When a tensor is used by additional CUDA streams, we need to call record_stream # to tell the allocator about all these streams. Otherwise, the allocator might free the # underlying memory of the tensor once it is no longer used by the creator stream. This is # a notable programming trick when we write programs using multi CUDA streams. cur_stream = torch.cuda.current_stream() assert isinstance(t, torch.Tensor) t.record_stream(cur_stream) class CachedParamMgr(torch.nn.Module): """ Manage Embedding Weights on CPU and CUDA memory uses a software cache. CPU maintains the entire original weight. CUDA maintains a fraction of the weights used in the upcoming computation. The row number in CUDA is controlled by `cuda_row_num`. During training, GPU needs to transmit embedding rows between CPU and GPU. Args: weight (torch.Tensor): the weight of the Embedding layer. cuda_row_num (int, optional): the number of rows cached in CUDA memory. Defaults to 0. buffer_size (int, optional): the number of rows in a data transmitter buffer. Defaults to 50_000. pin_weight (bool, optional): use pin memory to store the cpu weight. If set `True`, the cpu memory usage will increase largely. Defaults to False. evict_strategy (EvictionStrategy, optional): the eviction strategy. There are two options. `EvictionStrategy.LFU`: use the least frequently used cache. `EvictionStrategy.DATASET`: use the stats collected from the target dataset. It usually leads to less cpu-gpu communication volume. Defaults to EvictionStrategy.DATASET. """ def __init__( self, weight: torch.Tensor, cuda_row_num: int = 0, buffer_size: int = 0, pin_weight: bool = True, evict_strategy: EvictionStrategy = EvictionStrategy.DATASET, async_copy: bool = False, ) -> None: super(CachedParamMgr, self).__init__() self.buffer_size = buffer_size self.num_embeddings, self.embedding_dim = weight.shape self.cuda_row_num = cuda_row_num self._cuda_available_row_num = self.cuda_row_num self.pin_weight = pin_weight self.elem_size_in_byte = weight.element_size() # weight configure self._init_weight(weight) # Perf log self.num_hits_history = [] self.num_miss_history = [] self.num_write_back_history = [] self._evict_strategy = evict_strategy self._async_copy = async_copy if self._async_copy: self._memcpy_stream = torch.cuda.Stream() print('use async copy') if self._evict_strategy == EvictionStrategy.LFU: # cache_row_idx -> frequency, freq of the cache rows. # classic lfu cache. evict the minimal freq value row in cuda cache. self.register_buffer("freq_cnter", torch.empty(self.cuda_row_num, device=torch.cuda.current_device(), dtype=torch.long).fill_(sys.maxsize), persistent=False) self._elapsed_dict = {} self._show_cache_miss = True self._reset_comm_stats() def _reset_comm_stats(self): for k in self._elapsed_dict.keys(): self._elapsed_dict[k] = 0 self._cpu_to_cuda_numel = 0 self._cuda_to_cpu_numel = 0 if self._show_cache_miss: self._cache_miss = 0 self._total_cache = 0 @contextmanager def timer(self, name): with Timer() as t: yield torch.cuda.synchronize() if name not in self._elapsed_dict.keys(): self._elapsed_dict[name] = 0 self._elapsed_dict[name] += t.elapsed def _find_evict_gpu_idxs(self, evict_num: int) -> torch.Tensor: """_find_evict_gpu_idxs Find the gpu idxs to be evicted, according to their freq. Args: evict_num (int): how many rows has to be evicted Returns: torch.Tensor: a list tensor (1D), contains the gpu_row_idxs. """ if self._evict_strategy == EvictionStrategy.LFU: # find the minimal evict_num freq entries in cached_idx_map _, evict_gpu_row_idxs = torch.topk(self.freq_cnter, evict_num, largest=False) return evict_gpu_row_idxs elif self._evict_strategy == EvictionStrategy.DATASET: # cached_idx_map itself implies the priority of eviction. # The value of self.cached_idx_map represents cpu_row_idx. # The larger it is, the less frequently it will appear in the dataset, # and the higher its eviction priority will be. _, evict_gpu_row_idxs = torch.topk(self.cached_idx_map, evict_num, largest=True) return evict_gpu_row_idxs else: raise TypeError def _init_weight(self, weight): if self.cuda_row_num > 0: # Enable cache with introducing auxiliary data structures self.cuda_cached_weight = torch.nn.Parameter( torch.zeros(self.cuda_row_num, self.embedding_dim, device=torch.cuda.current_device(), dtype=weight.dtype)) # pin memory cpu for higher CPU-GPU copy bandwidth self.weight = weight.pin_memory() if self.pin_weight else weight # map original id to new id with respect to frequency # id -> cpu_row_idx self.register_buffer( "idx_map", torch.arange(self.num_embeddings, dtype=torch.long, device=torch.cuda.current_device()), persistent=False, ) # cached_idx_map: gpu_row_idx -> cpu_row_idx self.register_buffer("cached_idx_map", torch.empty(self.cuda_row_num, device=torch.cuda.current_device(), dtype=torch.long).fill_(-1), persistent=False) # cpu_row_id -> gpu_row_idx. # gpu_row_idx as -1 means cpu_row_id not in CUDA. self.register_buffer("inverted_cached_idx", torch.zeros(self.num_embeddings, device=torch.cuda.current_device(), dtype=torch.long).fill_(-1), persistent=False) self.evict_backlist = torch.tensor([], device=torch.cuda.current_device()) # index copy buffer size should less than 10% of cuda weight. if self.buffer_size > 0: self.limit_buff_index_copyer = LimitBuffIndexCopyer(self.buffer_size) else: # Disable cache so that FreqCacheEmbedding is compatible with vanilla EmbeddingBag # self.weight = torch.nn.Parameter(weight) # self.cuda_cached_weight = self.weight raise NotImplementedError() def cpu_weight_data(self, row_idx: int) -> torch.Tensor: """ access a row of CPU weight. Args: row_idx (int): the idx of rows Returns: torch.Tensor: a piece of memory in CPU weight corresponding to row id's payload. The tensor is 1-D. """ return self.weight.data.view(-1).narrow(0, int(row_idx) * self.embedding_dim, self.embedding_dim).view(1, self.embedding_dim) @property def cuda_available_row_num(self): return self._cuda_available_row_num @torch.no_grad() def reorder(self, ids_freq_mapping: Optional[List[int]] = None, warmup_ratio=0.7): """reorder reorder the weight according to ids' frequency in dataset before training. Execute only once before training, also known as warmup phase. Note: If you would like to use the DATASET as the eviction strategy, you must call this function. Note: If you are use the LFU as the eviction strategy, you can skip this function. If you still use this function. It will initialize The frequency in LFU cache using the dataset statistics. Args: ids_freq_mapping (List[int]): a list, whose offset is id number, value is freq. if None then not reorder the cpu weight. warmup_ratio (float): the amount of chunks preloaded in cuda cache """ # reorder phase: reorder the cpu weight according to their freq stats in the target dataset. # reorder only works for DATASET eviction strategy. if ids_freq_mapping is not None and not isinstance(ids_freq_mapping, torch.Tensor): ids_freq_mapping = torch.tensor(ids_freq_mapping) if self._evict_strategy == EvictionStrategy.DATASET: if ids_freq_mapping is not None: tmp_idx = torch.argsort(ids_freq_mapping, descending=True) sorted_idx = torch.argsort(tmp_idx) self.idx_map.data.copy_(sorted_idx) # warmup phase: copy #preload_row_num rows from cpu to gpu. preload_row_num = min(int(np.ceil(self.cuda_row_num * warmup_ratio)), self.num_embeddings) if preload_row_num > 0: with Timer() as timer: # extract rows from cpu weight if self._evict_strategy == EvictionStrategy.LFU and ids_freq_mapping is not None: freq_value, preload_cpu_ids = torch.topk(ids_freq_mapping, preload_row_num, dim=0, largest=True) preload_cuda_row_idxs = torch.arange(preload_row_num).cuda() else: preload_cpu_ids = torch.arange(preload_row_num) preload_cuda_row_idxs = preload_cpu_ids.cuda() if self.buffer_size > 0: self.limit_buff_index_copyer.index_copy(0, src_index=preload_cpu_ids, tgt_index=preload_cuda_row_idxs, src=self.weight.view(self.num_embeddings, -1), tgt=self.cuda_cached_weight.view(self.cuda_row_num, -1)) else: preload_rows = self.weight.view(self.num_embeddings, -1).index_select(0, preload_cpu_ids).cuda() self.cuda_cached_weight.view(self.cuda_row_num, -1).index_copy_(0, preload_cuda_row_idxs, preload_rows) # update auxiliary info self.cached_idx_map[preload_cuda_row_idxs] = preload_cpu_ids.cuda() self.inverted_cached_idx[preload_cpu_ids] = preload_cuda_row_idxs self._cuda_available_row_num -= preload_row_num if self._evict_strategy == EvictionStrategy.LFU: # if the ids_freq_mapping is not None, we initialize the embedding row's freq value in LFU as its freq in dataset. if ids_freq_mapping is None: self.freq_cnter.index_fill_(0, preload_cuda_row_idxs, 0) else: self.freq_cnter[preload_cuda_row_idxs] = freq_value.cuda() print(f'Cache warmup finished cost {timer.elapsed} sec.') def flush(self): """flush all CUDA rows to CPU. The function is usually called after training finished. """ slots = torch.nonzero(self.cached_idx_map > -1).squeeze(1) row_ids = self.cached_idx_map[slots] rows = self.cuda_cached_weight.view(self.cuda_row_num, -1).index_select(0, slots).cpu() self.weight.view(self.num_embeddings, -1).index_copy_(0, row_ids.cpu(), rows) self.cached_idx_map.index_fill_(0, slots, -1) self.inverted_cached_idx.index_fill_(0, row_ids, -1) self._cuda_available_row_num += slots.numel() if self._show_cache_miss: self._cache_miss = 0 self._total_cache = 0 if self._evict_strategy == EvictionStrategy.LFU: self.freq_cnter.fill_(sys.maxsize) assert self._cuda_available_row_num == self.cuda_row_num assert torch.all(self.inverted_cached_idx == -1).item() assert torch.all(self.cached_idx_map == -1).item() def print_comm_stats(self): if self._cuda_to_cpu_numel > 0 and "3_evict_out" in self._elapsed_dict: elapsed = self._elapsed_dict["3_evict_out"] print( f"CUDA->CPU BWD {self._cuda_to_cpu_numel * self.elem_size_in_byte / 1e6 / elapsed} MB/s {self._cuda_to_cpu_numel / 1e6} M elem" ) print(f'cuda_to_cpu_elapse {elapsed} sec') if self._cpu_to_cuda_numel > 0 and "5_evict_in" in self._elapsed_dict: elapsed = self._elapsed_dict["5_evict_in"] print( f"CPU->CUDA BWD {self._cpu_to_cuda_numel * self.elem_size_in_byte / 1e6 / elapsed} MB/s {self._cpu_to_cuda_numel / 1e6} M elem" ) print(f'cpu_to_cuda_elpase {elapsed} sec') for k, v in self._elapsed_dict.items(): print(f'{k}: {v}') print(f'cache miss ratio {self._cache_miss / self._total_cache}') @torch.no_grad() def _id_to_cached_cuda_id(self, ids: torch.Tensor) -> torch.Tensor: """ convert ids to indices in self.cuda_cached_weight. Implemented with parallel operations on GPU. Args: ids (torch.Tensor): ids from the dataset Returns: torch.Tensor: contains indices in self.cuda_cached_weight """ ids = self.idx_map.index_select(0, ids.view(-1)) ret = self.inverted_cached_idx.index_select(0, ids) return ret @torch.no_grad() def prepare_ids(self, ids: torch.Tensor) -> torch.Tensor: """ move the cpu embedding rows w.r.t. ids into CUDA memory Args: ids (torch.Tensor): the ids to be computed Returns: torch.Tensor: indices on the cuda_cached_weight. """ torch.cuda.synchronize() with self.timer("cache_op") as gtimer: # identify cpu rows to cache with self.timer("1_identify_cpu_row_idxs") as timer: with record_function("(cache) get unique indices"): if self._evict_strategy == EvictionStrategy.LFU: cpu_row_idxs, repeat_times = torch.unique(ids, return_counts=True) else: cpu_row_idxs, repeat_times = torch.unique(self.idx_map.index_select(0, ids), return_counts=True) assert len(cpu_row_idxs) <= self.cuda_row_num, \ f"You move {len(cpu_row_idxs)} embedding rows from CPU to CUDA. " \ f"It is larger than the capacity of the cache, which at most contains {self.cuda_row_num} rows, " \ f"Please increase cuda_row_num or decrease the training batch size." self.evict_backlist = cpu_row_idxs tmp = torch.isin(cpu_row_idxs, self.cached_idx_map, invert=True) comm_cpu_row_idxs = cpu_row_idxs[tmp] if self._show_cache_miss: self._cache_miss += torch.sum(repeat_times[tmp]) self._total_cache += ids.numel() self.num_hits_history.append(len(cpu_row_idxs) - len(comm_cpu_row_idxs)) self.num_miss_history.append(len(comm_cpu_row_idxs)) self.num_write_back_history.append(0) # move sure the cuda rows will not be evicted! with record_function("(cache) prepare_rows_on_cuda"): with self.timer("prepare_rows_on_cuda") as timer: self._prepare_rows_on_cuda(comm_cpu_row_idxs) self.evict_backlist = torch.tensor([], device=cpu_row_idxs.device, dtype=cpu_row_idxs.dtype) with self.timer("6_update_cache") as timer: with record_function("6_update_cache"): gpu_row_idxs = self._id_to_cached_cuda_id(ids) # update for LFU. if self._evict_strategy == EvictionStrategy.LFU: unique_gpu_row_idxs = self.inverted_cached_idx[cpu_row_idxs] self.freq_cnter.scatter_add_(0, unique_gpu_row_idxs, repeat_times) return gpu_row_idxs def _row_in_cuda(self, row_id: int) -> bool: return self.inverted_cached_idx[row_id] != -1 @torch.no_grad() def _prepare_rows_on_cuda(self, cpu_row_idxs: torch.Tensor) -> None: """prepare rows in cpu_row_idxs on CUDA memory Args: cpu_row_idxs (torch.Tensor): the rows to be placed on CUDA """ evict_num = cpu_row_idxs.numel() - self.cuda_available_row_num cpu_row_idxs_copy = cpu_row_idxs.cpu() # move evict in rows to gpu if self._async_copy: if self.buffer_size == 0: evict_in_rows_gpu = self.weight.view(self.num_embeddings, -1).index_select(0, cpu_row_idxs_copy).pin_memory() with torch.cuda.stream(self._memcpy_stream): evict_in_rows_gpu = evict_in_rows_gpu.to(torch.cuda.current_device(), non_blocking=True) else: raise NotImplemented if evict_num > 0: with self.timer("2_identify_cuda_row_idxs") as timer: mask_cpu_row_idx = torch.isin(self.cached_idx_map, self.evict_backlist) invalid_idxs = torch.nonzero(mask_cpu_row_idx).squeeze(1) if self._evict_strategy == EvictionStrategy.DATASET: # mask method. # set cached_idx_map[invalid_idxs] to -2. # so those idxs will be sorted to end, therefore not being chosen as victim backup_idxs = self.cached_idx_map[mask_cpu_row_idx].clone() self.cached_idx_map.index_fill_(0, invalid_idxs, -2) with self.timer("2_1_find_evict_gpu_idxs") as timer: evict_gpu_row_idxs = self._find_evict_gpu_idxs(evict_num) # move evict out rows to cpu if self._async_copy: evict_out_rows_gpu = self.cuda_cached_weight.view(self.cuda_row_num, -1).index_select(0, evict_gpu_row_idxs) evict_out_rows_cpu = torch.empty_like(evict_out_rows_gpu, device='cpu', pin_memory=True) with torch.cuda.stream(None): evict_out_rows_cpu.copy_(evict_out_rows_gpu, non_blocking=True) self.cached_idx_map.index_copy_(0, invalid_idxs, backup_idxs) elif self._evict_strategy == EvictionStrategy.LFU: with self.timer("2_1_backup_freqs") as timer: backup_freqs = self.freq_cnter[invalid_idxs].clone() self.freq_cnter.index_fill_(0, invalid_idxs, sys.maxsize) with self.timer("2_2_find_evict_gpu_idxs") as timer: evict_gpu_row_idxs = self._find_evict_gpu_idxs(evict_num) if self._async_copy: evict_out_rows_gpu = self.cuda_cached_weight.view(self.cuda_row_num, -1).index_select(0, evict_gpu_row_idxs) evict_out_rows_cpu = torch.empty_like(evict_out_rows_gpu, device='cpu', pin_memory=True) with torch.cuda.stream(None): evict_out_rows_cpu.copy_(evict_out_rows_gpu, non_blocking=True) with self.timer("2_3_revert_freqs") as timer: self.freq_cnter.index_copy_(0, invalid_idxs, backup_freqs) evict_info = self.cached_idx_map[evict_gpu_row_idxs] with self.timer("3_evict_out") as timer: if self.buffer_size > 0: self.limit_buff_index_copyer.index_copy(0, src_index=evict_gpu_row_idxs, tgt_index=evict_info.cpu(), src=self.cuda_cached_weight.view(self.cuda_row_num, -1), tgt=self.weight.view(self.num_embeddings, -1)) else: # allocate tmp memory on CPU and copy rows on CUDA to CPU. # TODO async gpu -> cpu if self._async_copy: _wait_for_data(evict_out_rows_cpu, None) else: with self.timer("3_1_evict_out_index_select") as timer: evict_out_rows_cpu = self.cuda_cached_weight.view(self.cuda_row_num, -1).index_select(0, evict_gpu_row_idxs) with self.timer("3_2_evict_out_gpu_to_cpu_copy") as timer: evict_out_rows_cpu = evict_out_rows_cpu.cpu() with self.timer("3_2_evict_out_cpu_copy") as timer: self.weight.view(self.num_embeddings, -1).index_copy_(0, evict_info.cpu(), evict_out_rows_cpu) self.cached_idx_map.index_fill_(0, evict_gpu_row_idxs, -1) self.inverted_cached_idx.index_fill_(0, evict_info, -1) # self.freq_cnter.index_fill(0, evict_gpu_row_idxs, sys.maxsize) # unnecessary self._cuda_available_row_num += evict_num weight_size = evict_gpu_row_idxs.numel() * self.embedding_dim self._cuda_to_cpu_numel += weight_size # print(f"evict embedding weight: {weight_size*self.elem_size_in_byte/1e6:.2f} MB") # slots of cuda weight to evict in with self.timer("4_identify_cuda_slot") as timer: slots = torch.nonzero(self.cached_idx_map == -1).squeeze(1)[:cpu_row_idxs.numel()] # TODO wait for optimize with self.timer("5_evict_in") as timer: # Here also allocate extra memory on CUDA. #cpu_row_idxs if self.buffer_size > 0: self.limit_buff_index_copyer.index_copy(0, src_index=cpu_row_idxs_copy, tgt_index=slots, src=self.weight.view(self.num_embeddings, -1), tgt=self.cuda_cached_weight.view(self.cuda_row_num, -1)) else: if self._async_copy: _wait_for_data(evict_in_rows_gpu, self._memcpy_stream) else: with self.timer("5_1_evict_in_index_select") as timer: # narrow index select to a subset of self.weight # tmp = torch.narrow(self.weight.view(self.num_embeddings, -1), 0, min(cpu_row_idxs).cpu(), max(cpu_row_idxs) - min(cpu_row_idxs) + 1) # evict_in_rows_gpu = tmp.index_select(0, cpu_row_idxs_copy - min(cpu_row_idxs).cpu()) evict_in_rows_gpu = self.weight.view(self.num_embeddings, -1).index_select(0, cpu_row_idxs_copy).pin_memory() with self.timer("5_2_evict_in_gpu_to_cpu_copy") as timer: evict_in_rows_gpu = evict_in_rows_gpu.cuda() with self.timer("5_3_evict_in_index_copy") as timer: self.cuda_cached_weight.view(self.cuda_row_num, -1).index_copy_(0, slots, evict_in_rows_gpu) with self.timer("6_update_cache") as timer: self.cached_idx_map[slots] = cpu_row_idxs self.inverted_cached_idx.index_copy_(0, cpu_row_idxs, slots) if self._evict_strategy == EvictionStrategy.LFU: self.freq_cnter.index_fill_(0, slots, 0) self._cuda_available_row_num -= cpu_row_idxs.numel() weight_size = cpu_row_idxs.numel() * self.embedding_dim self._cpu_to_cuda_numel += weight_size # print(f"admit embedding weight: {weight_size*self.elem_size_in_byte/1e6:.2f} MB") def _find_free_cuda_row(self) -> int: if self._cuda_available_row_num == 0: return -1 candidates = torch.nonzero(self.cached_idx_map == -1).squeeze(1) return candidates[0].item() def _evict(self) -> int: """ deprecated evict one row from cuda to cpu. Returns: (int) : the slot id be evicted. """ mask = torch.logical_or(torch.isin(self.cached_idx_map, self.evict_backlist), self.cached_idx_map == -1) buf = self.cached_idx_map[mask].clone() idx = torch.nonzero(mask).squeeze(1) self.cached_idx_map.index_fill_(0, idx, -1) max_row, max_cpu_row_idx = torch.max(self.cached_idx_map, dim=0) max_gpu_row_idx = self.cached_idx_map[max_cpu_row_idx] if max_gpu_row_idx == -1: raise RuntimeError("Can not evict a row") max_gpu_row_idx = max_gpu_row_idx.item() max_offset = self.inverted_cached_idx[max_gpu_row_idx] # recover self.cached_idx_map.index_copy_(0, idx, buf) with Timer() as timer: cuda_tensor = torch.narrow(self.cuda_cached_weight.view(-1), 0, max_offset * self.embedding_dim, self.embedding_dim).view(1, self.embedding_dim) self.cpu_weight_data(max_gpu_row_idx).data.copy_(cuda_tensor) # update inverted_cached_idx, min_slot_id is evicted from cuda self.cached_idx_map[max_cpu_row_idx] = -1 if self._evict_strategy == EvictionStrategy.LFU: self.freq_cnter[max_cpu_row_idx] = sys.maxsize self.inverted_cached_idx[max_gpu_row_idx] = -1 self._cuda_available_row_num += 1 self._cuda_to_cpu_numel += self.embedding_dim # self.num_write_back_history[-1] += 1 return max_cpu_row_idx @torch.no_grad() def _admit(self, row_id: int): """ deprecated move in row_id to CUDA Args: row_id (int): the id of row to be moved in """ # find a free slot in partial cuda weight slot_id = self._find_free_cuda_row() if slot_id == -1: # evict one row slot_id = self._evict() slot_offset = slot_id # copy payload from cpu to cuda with Timer() as timer: cuda_tensor = torch.narrow(self.cuda_cached_weight.view(-1), 0, slot_offset * self.embedding_dim, self.embedding_dim).view(1, self.embedding_dim) cuda_tensor.data.copy_(self.cpu_weight_data(row_id)) # update the inverted_cached_idx self.cached_idx_map[slot_id] = row_id if self._evict_strategy == EvictionStrategy.LFU: self.freq_cnter[slot_id] = 0 self.inverted_cached_idx[row_id] = slot_offset self._cuda_available_row_num -= 1 self._cpu_to_cuda_numel += self.embedding_dim
import torch from torch import LongTensor class LimitBuffIndexCopyer(object): """LimitBuffIndexCopyer Index Copy using limited temp buffer on CUDA. Args: size (int): buffer size """ def __init__(self, size: int) -> None: self._buff_size = size @torch.no_grad() def index_copy(self, dim: int, src_index: LongTensor, tgt_index: LongTensor, src: torch.Tensor, tgt: torch.Tensor): """copy src tensor[src_index] -(index_select)-> tmp -(index_copy_)-> tgt tensor [tgt_index] The valid rows in the src tensor are continous, while rows in tgt tensor is scattered. Args: dim (int): dimension along which to index src_index (int): indices of src tensor to select from tgt_index (int): indices of tgt tensor to select from src (torch.Tensor): the tensor containing values to copy tgt (torch.Tensor): the tensor to be copied """ # tgt.index_copy_(dim, index, src) assert dim == 0, "only support index_copy on dim 0" assert tgt.dim() == 2 assert src.dim() == 2 tgt_device = tgt.device src_device = src.device assert src_index.numel() == tgt_index.numel() dim_size = src_index.numel() src_index = src_index.to(src_device) for begin_pos in range(0, dim_size, self._buff_size): cur_len = min(self._buff_size, dim_size - begin_pos) src_idx_piece = src_index.narrow(0, begin_pos, cur_len) if src_device.type == 'cpu' and tgt_device.type == 'cuda': cpu_tmp_buffer = src.index_select(dim, src_idx_piece).pin_memory() tmp_buffer = torch.empty_like(cpu_tmp_buffer, device=tgt_device) tmp_buffer.copy_(cpu_tmp_buffer) else: tmp_buffer = src.index_select(dim, src_idx_piece).to(tgt_device) tgt_idx_piece = tgt_index.narrow(0, begin_pos, cur_len) tgt.index_copy_(dim, tgt_idx_piece, tmp_buffer)
import torch import torch.nn.functional as F from typing import List, Optional, Iterator, Tuple from .cached_embedding import CachedEmbeddingBag from colossalai.nn._ops._utils import dual_all_to_all from colossalai.tensor import ColoParameter, ShardSpec, ComputePattern, ProcessGroup, ColoTensorSpec, ColoTensor from .cache_mgr import CachedParamMgr, EvictionStrategy def get_partition(embedding_dim, rank, world_size) -> Tuple[int, int, bool]: if world_size == 1: return 0, embedding_dim, True assert embedding_dim >= world_size, \ f"Embedding dimension {embedding_dim} must be larger than the world size " \ f"{world_size} of the process group" chunk_size = embedding_dim // world_size threshold = embedding_dim % world_size # if embedding dim is divisible by world size if threshold == 0: return rank * chunk_size, (rank + 1) * chunk_size, True # align with the split strategy of torch.tensor_split size_list = [chunk_size + 1 if i < threshold else chunk_size for i in range(world_size)] offset = sum(size_list[:rank]) return offset, offset + size_list[rank], False class ParallelCachedEmbeddingBag(CachedEmbeddingBag): def __init__(self, num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2., scale_grad_by_freq=False, sparse=False, _weight=None, mode='mean', include_last_offset=False, dtype=None, device=None, cache_ratio=0.01, ids_freq_mapping=None, warmup_ratio=0.7, buffer_size=50_000, pin_weight=False, evict_strategy: EvictionStrategy = EvictionStrategy.DATASET): self.rank = torch.distributed.get_rank() self.world_size = torch.distributed.get_world_size() self.partition_start_index, self.partition_end_index, divisible = get_partition( embedding_dim, self.rank, self.world_size) self.embedding_dim_per_partition = self.partition_end_index - self.partition_start_index super(ParallelCachedEmbeddingBag, self).__init__(num_embeddings, embedding_dim, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, _weight, mode, include_last_offset, dtype, device, cache_ratio, ids_freq_mapping, warmup_ratio, buffer_size, pin_weight, evict_strategy) self.cache_op = True def _weight_alloc(self, dtype, device): weight = torch.empty(self.num_embeddings, self.embedding_dim_per_partition, device=device, dtype=dtype) with torch.no_grad(): weight.data.uniform_(-1 / self.num_embeddings, 1 / self.num_embeddings) if self.padding_idx is not None: weight[self.padding_idx].fill_(0) colo_tensor_spec = ColoTensorSpec(pg=ProcessGroup(tp_degree=self.world_size), dist_attr=ShardSpec(dims=[-1], num_partitions=[self.world_size]), compute_attr=ComputePattern.TP1D) return ColoTensor.from_torch_tensor(weight, spec=colo_tensor_spec) def forward( self, indices, offsets=None, per_sample_weights=None, shape_hook=None, scatter_dim=0, gather_dim=-1, ): if self.cache_op: with torch.no_grad(): indices = self.cache_weight_mgr.prepare_ids(indices) output_shard = F.embedding_bag(indices.cuda(), self.cache_weight_mgr.cuda_cached_weight, offsets, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.mode, self.sparse, per_sample_weights, self.include_last_offset, self.padding_idx) if shape_hook is not None: output_shard = shape_hook(output_shard) output_full = dual_all_to_all(output_shard, self.weight.get_process_group(), scatter_dim=scatter_dim, gather_dim=gather_dim) return output_full def set_cache_op(self, cache_op: bool = True): self.cache_op = cache_op @classmethod def from_pretrained( cls, embedding: torch.Tensor, freeze: bool = True, padding_idx: Optional[int] = None, max_norm: Optional[float] = None, norm_type: float = 2., scale_grad_by_freq: bool = False, sparse: bool = False, mode: str = 'mean', include_last_offset: bool = False, cuda_row_num: int = 100_000, ids_freq_mapping: Optional[List[int]] = None, warmup_ratio: float = 0.7, buffer_size: int = 0, ) -> 'ParallelCachedEmbeddingBag': rows, cols = embedding.shape embedding_bag = cls(rows, cols, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, embedding, mode, include_last_offset, cuda_row_num=cuda_row_num, ids_freq_mapping=ids_freq_mapping, warmup_ratio=warmup_ratio, buffer_size=buffer_size) embedding_bag.cache_weight_mgr.cuda_cached_weight.requires_grad_ = not freeze return embedding_bag def print_comm_stats_(self): self.cache_weight_mgr.print_comm_stats() def element_size(self): return self.weight.element_size()
import torch class TablewiseEmbeddingBagConfig: ''' example: def prepare_tablewise_config(args, cache_ratio, ...): embedding_bag_config_list: List[TablewiseEmbeddingBagConfig] = [] ... return embedding_bag_config_list ''' def __init__(self, num_embeddings: int, cuda_row_num: int, assigned_rank: int = 0, buffer_size=50_000, ids_freq_mapping=None, initial_weight: torch.tensor = None, name: str = ""): self.num_embeddings = num_embeddings self.cuda_row_num = cuda_row_num self.assigned_rank = assigned_rank self.buffer_size = buffer_size self.ids_freq_mapping = ids_freq_mapping self.initial_weight = initial_weight self.name = name
from .cache_mgr import CachedParamMgr, EvictionStrategy from .copyer import LimitBuffIndexCopyer from .cached_embedding import CachedEmbeddingBag from .parallel_cached_embedding import ParallelCachedEmbeddingBag from .embedding_config import TablewiseEmbeddingBagConfig from .parallel_cached_embedding_tablewise import ParallelCachedEmbeddingBagTablewise from .parallel_cached_embedding_tablewise_split_cache import ParallelCachedEmbeddingBagTablewiseSpiltCache __all__ = [ 'CachedParamMgr', 'LimitBuffIndexCopyer', 'CachedEmbeddingBag', 'ParallelCachedEmbeddingBag', 'EvictionStrategy', 'ParallelCachedEmbeddingBagTablewise', 'TablewiseEmbeddingBagConfig', 'ParallelCachedEmbeddingBagTablewiseSpiltCache' ]
import torch import torch.nn.functional as F from typing import List, Optional, Iterator, Tuple, Union from .base_embedding import BaseEmbeddingBag from .cache_mgr import CachedParamMgr, EvictionStrategy from torch.nn.parameter import Parameter class CachedEmbeddingBag(BaseEmbeddingBag): """CachedEmbeddingBag Cached Embedding. Apply a GPU-based software cache approaches to dynamically manage the embedding table in the CPU and GPU memory space. It can leverage the id's frequency statistics of the target dataset, by passing a frequency list to param `ids_freq_mapping`. You can also apply a navie LFU cache eviction strategy by setting `evict_strategy` as EvictionStrategy.LFU. Args: num_embeddings (int): size of the dictionary of embeddings embedding_dim (int): the size of each embedding vector padding_idx (int, optional): If specified, the entries at padding_idx do not contribute to the gradient; therefore, the embedding vector at padding_idx is not updated during training, i.e. it remains as a fixed “pad”. For a newly constructed EmbeddingBag, the embedding vector at padding_idx will default to all zeros, but can be updated to another value to be used as the padding vector. Note that the embedding vector at padding_idx is excluded from the reduction. max_norm (float, optional): If given, each embedding vector with norm larger than max_norm is renormalized to have norm max_norm norm_type (str, optional): The p of the p-norm to compute for the max_norm option. Defaults to 2.. scale_grad_by_freq (bool, optional): if given, this will scale gradients by the inverse of frequency of the words in the mini-batch. Default False. Note: this option is not supported when mode="max". Defaults to False. sparse (bool, optional): if True, gradient w.r.t. weight matrix will be a sparse tensor. See Notes for more details regarding sparse gradients. Note: this option is not supported when mode="max".. Defaults to False. _weight (torch.Tensor, optional): an embedding weight tensor. Concate multiple tables in a embedding bag as a single one. Defaults to None. mode (str, optional): "sum", "mean" or "max". Specifies the way to reduce the bag. "sum" computes the weighted sum, taking per_sample_weights into consideration. "mean" computes the average of the values in the bag, "max" computes the max value over each bag. Default: "mean". Defaults to 'mean'. include_last_offset (bool, optional): if True, offsets has one additional element, where the last element is equivalent to the size of indices. This matches the CSR format.. Defaults to False. dtype (torch.dtype, optional): data type of the cpu weight initialization. Defaults to None meaning float32. device (torch.device, optional): device type to the cpu weight. Defaults to None meaning cpu. cache_ratio (float, float): cache ratio of the #cuda_weight_row / #cpu_weight_row ids_freq_mapping (Union[List, torch.Tensor], optional): the frequency of each embedding vector occures in dataset. Defaults to None. warmup_ratio (float, optional): the ratio of cuda cache is warmuped with. Defaults to 0.7. buffer_size (int, optional): the max number of vectors in transmitter buffer. If set to 0, the buffer is not used. Defaults to 0. pin_weight (bool, optional): pin the cpu weight. Defaults to False. evict_strategy (EvictionStrategy, optional): evict strategy of the software cache. Defaults to EvictionStrategy.DATASET. """ def __init__(self, num_embeddings: int, embedding_dim: int, padding_idx: int = None, max_norm: float = None, norm_type: float = 2., scale_grad_by_freq: bool = False, sparse: bool = False, _weight: Optional[torch.Tensor] = None, mode: str = 'mean', include_last_offset: bool = False, dtype: Optional[torch.dtype] = None, device: Optional[torch.device] = None, cache_ratio: float = 0.01, ids_freq_mapping: Optional[Union[List, torch.Tensor]] = None, warmup_ratio: float = 0.7, buffer_size: int = 0, pin_weight: bool = False, evict_strategy: EvictionStrategy = EvictionStrategy.LFU): super(CachedEmbeddingBag, self).__init__(num_embeddings, embedding_dim, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, mode, include_last_offset) assert cache_ratio <= 1.0, f"cache ratio {cache_ratio} must less than 1.0" self.evict_strategy = evict_strategy if _weight is None: _weight = self._weight_alloc(dtype, device) cuda_row_num = int(num_embeddings * cache_ratio) # configure weight & cache self._preprocess(_weight, cuda_row_num, ids_freq_mapping, warmup_ratio, buffer_size, pin_weight) self.cache_op = True def set_cache_mgr_async_copy(self, flag): self.cache_weight_mgr._async_copy = flag def _weight_alloc(self, dtype, device): weight = torch.empty(self.num_embeddings, self.embedding_dim, dtype=dtype, device=device) with torch.no_grad(): weight.data.uniform_(-1 / self.num_embeddings, 1 / self.num_embeddings) if self.padding_idx is not None: weight[self.padding_idx].fill_(0) return weight def _preprocess(self, weight, cuda_row_num: int, ids_freq_mapping: Optional[List[int]] = None, warmup_ratio=0.7, buffer_size=50_000, pin_weight=False): """ Called after initialized. Reorder the weight rows according to the ids_freq_mapping. Then, let the weights of the Module be managed by a CachedParamMgr. Args: cuda_row_num (int): number of rows can be hosted in CUDA memory ids_freq_mapping (List[int]): a list, idx is id number, value is freq warmup_ratio (float): the amount of rows preloaded in cuda cache """ self.cache_weight_mgr = CachedParamMgr(weight, cuda_row_num, buffer_size, pin_weight, evict_strategy=self.evict_strategy) self.cache_weight_mgr.reorder(ids_freq_mapping, warmup_ratio) def forward(self, input, offsets=None, per_sample_weights=None, shape_hook=None): if self.cache_op: with torch.no_grad(): input = self.cache_weight_mgr.prepare_ids(input) embeddings = F.embedding_bag(input.cuda(), self.cache_weight_mgr.cuda_cached_weight, offsets, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.mode, self.sparse, per_sample_weights, self.include_last_offset, self.padding_idx) if shape_hook is not None: embeddings = shape_hook(embeddings) return embeddings @property def weight(self): return self.cache_weight_mgr.weight def named_parameters(self, prefix: str = '', recurse: bool = True) -> Iterator[Tuple[str, Parameter]]: yield 'weight', self.cache_weight_mgr.cuda_cached_weight def parameters(self, recurse: bool = True) -> Iterator[Parameter]: yield self.cache_weight_mgr.cuda_cached_weight def set_cache_op(self, cache_op: bool = True): self.cache_op = cache_op ############################# Perf Log ################################### @property def num_hits_history(self): return self.cache_weight_mgr.num_hits_history @property def num_miss_history(self): return self.cache_weight_mgr.num_miss_history @property def num_write_back_history(self): return self.cache_weight_mgr.num_write_back_history @property def swap_in_bandwidth(self): if self.cache_weight_mgr._cpu_to_cuda_numel > 0: return self.cache_weight_mgr._cpu_to_cuda_numel * self.cache_weight_mgr.elem_size_in_byte / 1e6 / \ self.cache_weight_mgr._cpu_to_cuda_elpase else: return 0 @property def swap_out_bandwidth(self): if self.cache_weight_mgr._cuda_to_cpu_numel > 0: return self.cache_weight_mgr._cuda_to_cpu_numel * self.cache_weight_mgr.elem_size_in_byte / 1e6 / \ self.cache_weight_mgr._cuda_to_cpu_elapse return 0
import torch import torch.distributed as dist import torch.nn as nn from torch.profiler import record_function from .cached_embedding import CachedEmbeddingBag from colossalai.tensor import ProcessGroup from colossalai.nn._ops._utils import dual_all_to_all_tablewise from .embedding_config import TablewiseEmbeddingBagConfig from .cache_mgr import EvictionStrategy from typing import List import abc class ParallelCachedEmbeddingBagTablewiseSpiltCache(abc.ABC, nn.Module): """ every table assigned to this class instance is managed by a CachedEmbeddingBag. """ def __init__(self, embedding_bag_config_list: List[TablewiseEmbeddingBagConfig], embedding_dim: int, padding_idx=None, max_norm=None, norm_type=2., scale_grad_by_freq=False, sparse=False, mode='mean', include_last_offset=False, dtype=None, device=None, warmup_ratio=0.7, pin_weight=False, evict_strategy: EvictionStrategy = EvictionStrategy.LFU): super(ParallelCachedEmbeddingBagTablewiseSpiltCache, self).__init__() self.rank = dist.get_rank() self.world_size = dist.get_world_size() self.rank_of_tables = [config.assigned_rank for config in embedding_bag_config_list] self.global_table_num_embeddings_list = [config.num_embeddings for config in embedding_bag_config_list] self.global_tables_num = len(embedding_bag_config_list) self.global_tables_offsets = torch.cumsum(torch.tensor([0] + self.global_table_num_embeddings_list), 0).cuda() self.assigned_table_list: List[int] = [] for i, rank in enumerate(self.rank_of_tables): if rank == self.rank: self.assigned_table_list.append(i) self.include_last_offset = include_last_offset self.pg = ProcessGroup(tp_degree=self.world_size) # prepare CachedEmbeddingBag list self.cached_embedding_bag_list: nn.ModuleList = nn.ModuleList() for config in embedding_bag_config_list: if config.assigned_rank != self.rank: continue self.cached_embedding_bag_list.append( CachedEmbeddingBag(num_embeddings=config.num_embeddings, embedding_dim=embedding_dim, padding_idx=padding_idx, max_norm=max_norm, norm_type=norm_type, scale_grad_by_freq=scale_grad_by_freq, sparse=sparse, _weight=config.initial_weight, mode=mode, include_last_offset=include_last_offset, dtype=dtype, device=device, cuda_row_num=config.cuda_row_num, ids_freq_mapping=config.ids_freq_mapping, warmup_ratio=warmup_ratio, buffer_size=config.buffer_size, pin_weight=pin_weight, evict_strategy=evict_strategy)) # prepare list shape for all_to_all output self.embedding_dim_per_rank = [0 for i in range(self.world_size)] for rank in self.rank_of_tables: self.embedding_dim_per_rank[rank] += embedding_dim def forward(self, indices: torch.Tensor, offsets: torch.Tensor = None, per_sample_weights=None, shape_hook=None): # determine indices to handle batch_size = (offsets.shape[0]) // self.global_tables_num local_output_list = [] for i, handle_table in enumerate(self.assigned_table_list): with record_function("(tablewise) prepare indices and offsets"): with record_function("part 1"): indices_start_position = offsets[batch_size * handle_table] if (not self.include_last_offset) and (batch_size * (handle_table + 1) >= indices.shape[0]): # till the end special case indices_end_position = indices.shape[0] else: indices_end_position = offsets[batch_size * (handle_table + 1)] with record_function("part 2"): # local_indices = indices[indices_start_position:indices_end_position] - self.global_tables_offsets[handle_table] local_indices = indices.narrow(0, indices_start_position, indices_end_position - indices_start_position).sub(self.global_tables_offsets[handle_table]) if self.include_last_offset: # local_offsets = offsets[batch_size * handle_table:batch_size * (handle_table + 1) + 1] - offsets[batch_size * (handle_table)] local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size + 1).sub(offsets[batch_size * (handle_table)]) else: # local_offsets = offsets[batch_size * handle_table:batch_size * (handle_table + 1)] - offsets[batch_size * (handle_table)] local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size).sub(offsets[batch_size * (handle_table)]) local_per_sample_weights = None if per_sample_weights != None: local_per_sample_weights = per_sample_weights[indices_start_position:indices_end_position] with record_function("(tablewise) tablewise forward"): local_output_list.append(self.cached_embedding_bag_list[i](local_indices, local_offsets, local_per_sample_weights)) # get result of shape = (batch_size, (len(assigned_table_list)*embedding_dim)) local_output = torch.cat(local_output_list, 1) # then concatenate those local_output on the second demension. # use all_to_all remains = batch_size % self.world_size scatter_strides = [batch_size // self.world_size + int(i < remains) for i in range(self.world_size)] output_full = dual_all_to_all_tablewise(local_output, self.pg, scatter_strides, self.embedding_dim_per_rank) if shape_hook is not None: output_full = shape_hook(output_full) return output_full def element_size(self): if len(self.assigned_table_list) == 0: return 0 return self.cached_embedding_bag_list[0].cache_weight_mgr.weight.element_size() def print_comm_stats_(self): cuda_to_cpu_elem_num = 0 cpu_to_cuda_elem_num = 0 for cached_embedding_bag in self.cached_embedding_bag_list: cuda_to_cpu_elem_num += cached_embedding_bag.cache_weight_mgr._cuda_to_cpu_numel cpu_to_cuda_elem_num += cached_embedding_bag.cache_weight_mgr._cpu_to_cuda_numel print(f"CUDA->CPU num: {cuda_to_cpu_elem_num / 1e6} M elem") print(f"CPU->CUDA num: {cpu_to_cuda_elem_num / 1e6} M elem")
import abc import torch.nn as nn class BaseEmbeddingBag(abc.ABC, nn.Module): def __init__( self, num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2., scale_grad_by_freq=False, sparse=False, mode='mean', include_last_offset=False, ): super(BaseEmbeddingBag, self).__init__() self.num_embeddings = num_embeddings self.embedding_dim = embedding_dim if padding_idx is not None: if padding_idx > 0: assert padding_idx < self.num_embeddings, 'Padding_idx must be within num_embeddings' elif padding_idx < 0: assert padding_idx >= -self.num_embeddings, 'Padding_idx must be within num_embeddings' padding_idx = self.num_embeddings + padding_idx self.padding_idx = padding_idx self.max_norm = max_norm self.norm_type = norm_type self.scale_grad_by_freq = scale_grad_by_freq self.sparse = sparse # Specific to embedding bag self.mode = mode self.include_last_offset = include_last_offset
import torch import torch.distributed as dist import torch.nn.functional as F from .cached_embedding import CachedEmbeddingBag from .cache_mgr import EvictionStrategy from .embedding_config import TablewiseEmbeddingBagConfig from colossalai.tensor import ProcessGroup from colossalai.nn._ops._utils import dual_all_to_all_tablewise from typing import List import time class ParallelCachedEmbeddingBagTablewise(CachedEmbeddingBag): """ all tables assigned to this class instance are managed by a single CachedEmbeddingBag. Those parameters in TablewiseEmbeddingBagConfig are ignored: cuda_row_num, buffer_size, initial_weight. """ def __init__(self, embedding_bag_config_list: List[TablewiseEmbeddingBagConfig], embedding_dim: int, padding_idx=None, max_norm=None, norm_type=2., scale_grad_by_freq=False, sparse=False, _weight=None, mode='mean', include_last_offset=False, dtype=None, device=None, cache_ratio=0.01, warmup_ratio=0.7, buffer_size=50_000, pin_weight=False, evict_strategy: EvictionStrategy = EvictionStrategy.LFU): self.rank = dist.get_rank() self.world_size = dist.get_world_size() self.rank_of_tables = [config.assigned_rank for config in embedding_bag_config_list] self.global_table_num_embeddings_list = [config.num_embeddings for config in embedding_bag_config_list] self.global_tables_num = len(embedding_bag_config_list) self.global_tables_offsets = torch.cumsum(torch.tensor([0] + self.global_table_num_embeddings_list), 0).cuda() self.assigned_table_list: List[int] = [] self.pg = ProcessGroup(tp_degree=self.world_size) self.num_embeddings = 0 for i, rank in enumerate(self.rank_of_tables): if rank == self.rank: self.assigned_table_list.append(i) self.num_embeddings += self.global_table_num_embeddings_list[i] self.include_last_offset = include_last_offset ids_freq_mapping = [] for config in embedding_bag_config_list: if config.assigned_rank == self.rank: if config.ids_freq_mapping != None: ids_freq_mapping.extend(config.ids_freq_mapping) else: ids_freq_mapping = None break self.cache_ratio = cache_ratio # table-associate cache cuda_row_num = int(cache_ratio * self.num_embeddings) super(ParallelCachedEmbeddingBagTablewise, self).__init__(self.num_embeddings, embedding_dim, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse, _weight, mode, include_last_offset, dtype, device, cache_ratio, ids_freq_mapping, warmup_ratio, buffer_size, pin_weight, evict_strategy) # for assigned tables reconnection: self.idx_offset_list = [] offset_cumsum = 0 for table_i, table_num_embeddings in enumerate(self.global_table_num_embeddings_list): if self.rank_of_tables[table_i] == self.rank: self.idx_offset_list.append(offset_cumsum) else: offset_cumsum += table_num_embeddings # prepare list shape for all_to_all output self.embedding_dim_per_rank = [0 for i in range(self.world_size)] for rank in self.rank_of_tables: self.embedding_dim_per_rank[rank] += embedding_dim self.cache_op = True def forward( self, indices: torch.Tensor, offsets: torch.Tensor = None, per_sample_weights=None, shape_hook=None, already_split_along_rank=True, ): if not already_split_along_rank: # not recommanded. it takes time. batch_size = (offsets.shape[0]) // self.global_tables_num local_indices, local_offsets, local_per_sample_weights = self.split_along_rank( batch_size, indices, offsets, per_sample_weights) else: # recommanded. batch_size = (offsets.shape[0]) // len(self.assigned_table_list) local_indices, local_offsets, local_per_sample_weights = indices, offsets, per_sample_weights if self.cache_op: with torch.no_grad(): indices = self.cache_weight_mgr.prepare_ids(local_indices) local_output = F.embedding_bag(indices.cuda(), self.cache_weight_mgr.cuda_cached_weight, local_offsets, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.mode, self.sparse, local_per_sample_weights, self.include_last_offset, self.padding_idx) local_output = torch.cat(local_output.split(batch_size), 1) remains = batch_size % self.world_size scatter_strides = [batch_size // self.world_size + int(i < remains) for i in range(self.world_size)] output_full = dual_all_to_all_tablewise(local_output, self.pg, scatter_strides, self.embedding_dim_per_rank) if shape_hook is not None: output_full = shape_hook(output_full) return output_full def split_along_rank(self, batch_size, indices: torch.Tensor, offsets: torch.Tensor = None, per_sample_weights=None): ''' if input indices and offsets haven't been splitted along assigned rank, this function will do it. it takes time. please consider splitting data during batch loading. ''' local_indices_list: List(torch.Tensor) = [] local_offsets_list: List(torch.Tensor) = [] if per_sample_weights != None: local_per_sample_weights_list: List(torch.Tensor) = [] offset_pre_end = 0 # local_offsets trick for i, handle_table in enumerate(self.assigned_table_list): indices_start_position = offsets[batch_size * handle_table] if (not self.include_last_offset) and (batch_size * (handle_table + 1) >= indices.shape[0]): # till-the-end special case indices_end_position = indices.shape[0] else: indices_end_position = offsets[batch_size * (handle_table + 1)] # alternative approach: reduce malloc ''' # 1. local_indices_list: local_indices = indices.narrow(0, indices_start_position, indices_end_position - indices_start_position) torch.sub(local_indices, self.idx_offset_list[i], out=local_indices) local_indices_list.append(local_indices) # 2. local_offsets_list: if i + 1 == len(self.assigned_table_list): # till-the-end special case if not self.include_last_offset: local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size) else: local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size + 1) torch.add(local_offsets, offset_pre_end - offsets[batch_size * handle_table], out=local_offsets) local_offsets_list.append(local_offsets) else: temp_holder = offsets[batch_size * handle_table].item() local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size) torch.add(local_offsets, offset_pre_end - offsets[batch_size * handle_table], out=local_offsets) offset_pre_end = offsets[batch_size * (handle_table + 1)] + offset_pre_end - temp_holder local_offsets_list.append(local_offsets) ''' # 1. local_indices_list: local_indices_list.append( indices.narrow(0, indices_start_position, indices_end_position - indices_start_position).sub(self.idx_offset_list[i])) # 2. local_offsets_list: if i + 1 == len(self.assigned_table_list): # till-the-end special case if not self.include_last_offset: local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size).add(offset_pre_end - offsets[batch_size * (handle_table)]) else: local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size + 1).add(offset_pre_end - offsets[batch_size * (handle_table)]) local_offsets_list.append(local_offsets) else: local_offsets = offsets.narrow(0, batch_size * handle_table, batch_size + 1).add(offset_pre_end - offsets[batch_size * (handle_table)]) offset_pre_end = local_offsets[-1] local_offsets_list.append(local_offsets[:-1]) # 3. local_per_sample_weights_list: if per_sample_weights != None: local_per_sample_weights_list.append(per_sample_weights[indices_start_position:indices_end_position]) local_indices = torch.cat(local_indices_list, 0) local_offsets = torch.cat(local_offsets_list, 0) local_per_sample_weights = None if per_sample_weights != None: local_per_sample_weights = torch.cat(local_per_sample_weights_list, 0) return local_indices, local_offsets, local_per_sample_weights def set_cache_op(self, cache_op: bool = True): self.cache_op = cache_op def print_comm_stats_(self): self.cache_weight_mgr.print_comm_stats() def element_size(self): return self.weight.element_size()
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_adam.py import torch from colossalai.registry import OPTIMIZERS from colossalai.utils import multi_tensor_applier @OPTIMIZERS.register_module class FusedAdam(torch.optim.Optimizer): """Implements Adam algorithm. `FusedAdam` requires CUDA extensions which can be built during installation or runtime. This version of fused Adam implements 2 fusions. * Fusion of the Adam update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` :class:`colossalai.nn.optimizer.FusedAdam` may be used with or without Amp. Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED in FusedAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) set_grad_none (bool, optional): whether set grad to None when zero_grad() method is called. (default: True) .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__(self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, adamw_mode=True, weight_decay=0., amsgrad=False, set_grad_none=True): if amsgrad: raise RuntimeError('FusedAdam does not support the AMSGrad variant.') defaults = dict(lr=lr, bias_correction=bias_correction, betas=betas, eps=eps, weight_decay=weight_decay) super(FusedAdam, self).__init__(params, defaults) self.adamw_mode = 1 if adamw_mode else 0 self.set_grad_none = set_grad_none if multi_tensor_applier.available: from colossalai.kernel.op_builder import FusedOptimBuilder fused_optim = FusedOptimBuilder().load() # Skip buffer self._dummy_overflow_buf = torch.cuda.IntTensor([0]) self.multi_tensor_adam = fused_optim.multi_tensor_adam else: raise RuntimeError('FusedAdam requires cuda extensions') def zero_grad(self, set_to_none=False): if set_to_none: for group in self.param_groups: for p in group['params']: p.grad = None else: super(FusedAdam, self).zero_grad() def step(self, closure=None, grads=None, output_params=None, scale=None, grad_norms=None, div_scale: float = -1): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. The remaining arguments are deprecated, and are only retained (for the moment) for error-checking purposes. """ if any(p is not None for p in [grads, output_params, scale, grad_norms]): raise RuntimeError( 'FusedAdam has been updated. Simply initialize it identically to torch.optim.Adam, and call step() with no arguments.' ) loss = None if closure is not None: loss = closure() for group in self.param_groups: bias_correction = 1 if group['bias_correction'] else 0 beta1, beta2 = group['betas'] # assume same step across group now to simplify things # per parameter step can be easily support by making it tensor, or pass list into kernel if 'step' in group: group['step'] += 1 else: group['step'] = 1 # create lists for multi-tensor apply g_l, p_l, m_l, v_l = [], [], [], [] for p in group['params']: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError( 'FusedAdam does not support sparse gradients, please consider SparseAdam instead') state = self.state[p] # State initialization if len(state) == 0: # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p) if p.dtype not in [torch.float16, torch.float32]: raise RuntimeError('FusedAdam only support fp16 and fp32.') g_l.append(p.grad.data) p_l.append(p.data) m_l.append(state['exp_avg']) v_l.append(state['exp_avg_sq']) multi_tensor_applier(self.multi_tensor_adam, self._dummy_overflow_buf, [g_l, p_l, m_l, v_l], group['lr'], beta1, beta2, group['eps'], group['step'], self.adamw_mode, bias_correction, group['weight_decay'], div_scale) return loss
""" Adapted from the pytorch-lamb library at https://github.com/cybertronai/pytorch-lamb """ import torch from torch.optim import Optimizer from colossalai.registry import OPTIMIZERS @OPTIMIZERS.register_module class Lamb(Optimizer): r"""Implements Lamb algorithm. It has been proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) adam (bool, optional): always use trust ratio = 1, which turns this into Adam. Useful for comparison purposes. .. _Large Batch Optimization for Deep Learning\: Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 """ def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-6, weight_decay=0, adam=False): if not 0.0 <= lr: raise ValueError("Invalid learning rate: {}".format(lr)) if not 0.0 <= eps: raise ValueError("Invalid epsilon value: {}".format(eps)) if not 0.0 <= betas[0] < 1.0: raise ValueError("Invalid beta parameter at index 0: {}".format(betas[0])) if not 0.0 <= betas[1] < 1.0: raise ValueError("Invalid beta parameter at index 1: {}".format(betas[1])) defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay) self.adam = adam super(Lamb, self).__init__(params, defaults) def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: for p in group['params']: if p.grad is None: continue grad = p.grad.data if grad.is_sparse: raise RuntimeError('Lamb does not support sparse gradients, consider SparseAdam instad.') state = self.state[p] # State initialization if len(state) == 0: state['step'] = 0 # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p) # Exponential moving average of squared gradient values state['exp_avg_sq'] = torch.zeros_like(p) exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq'] beta1, beta2 = group['betas'] state['step'] += 1 # Decay the first and second moment running average coefficient # m_t exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) # v_t exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) # Paper v3 does not use debiasing. # bias_correction1 = 1 - beta1 ** state['step'] # bias_correction2 = 1 - beta2 ** state['step'] # Apply bias to lr to avoid broadcast. # * math.sqrt(bias_correction2) / bias_correction1 step_size = group['lr'] weight_norm = p.data.pow(2).sum().sqrt() adam_step = exp_avg / exp_avg_sq.sqrt().add(group['eps']) if group['weight_decay'] != 0: adam_step.add_(p.data, alpha=group['weight_decay']) adam_norm = adam_step.pow(2).sum().sqrt() if weight_norm == 0 or adam_norm == 0: trust_ratio = 1 else: trust_ratio = weight_norm / adam_norm state['weight_norm'] = weight_norm state['adam_norm'] = adam_norm state['trust_ratio'] = trust_ratio if self.adam: trust_ratio = 1 p.data.add_(adam_step, alpha=-step_size * trust_ratio) return loss
import math import warnings from enum import Enum from typing import Any, Dict, Set, Tuple import torch import torch.distributed as dist from torch.nn import Parameter from torch.optim import Optimizer from colossalai.amp.naive_amp.grad_scaler import DynamicGradScaler from colossalai.gemini.chunk import Chunk, ChunkManager from colossalai.logging import get_dist_logger from colossalai.nn.optimizer import ColossalaiOptimizer, CPUAdam, FusedAdam, HybridAdam from colossalai.nn.parallel.data_parallel import ZeroDDP from colossalai.utils import disposable, get_current_device, is_ddp_ignored _AVAIL_OPTIM_LIST = {FusedAdam, CPUAdam, HybridAdam} class OptimState(Enum): SCALED = 0 UNSCALED = 1 class ZeroOptimizer(ColossalaiOptimizer): """A wrapper for optimizer. ``ZeroDDP`` and ``ZeroOptimizer`` implement Zero Redundancy Optimizer (ZeRO state-3). Note: You must use ``ZeroDDP`` with ``ZeroOptimizer``. Note: Make sure you set ``placement_policy`` of ``GeminiManager`` to `"auto"`, if you set ``gpu_margin_mem_ratio > 0``. Args: optim (Optimizer): An Optimizer instance. module (ZeroDDP): A ``ZeroDDP`` 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 `placement_policy` of `GeminiManager` 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. """ def __init__(self, optim: Optimizer, module: ZeroDDP, 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, clipping_norm: float = 0.0, norm_type: float = 2.0, **defaults: Any): super().__init__(optim) assert isinstance(module, ZeroDDP) assert type(optim) in _AVAIL_OPTIM_LIST, "You should use an optimizer in the available list:\n" \ f"{_AVAIL_OPTIM_LIST}" self.module = module self.gemini_manager = module.gemini_manager self.chunk_manager: ChunkManager = self.gemini_manager.chunk_manager self.optim_state = OptimState.UNSCALED self.param_to_range: Dict[Parameter, Tuple[int, int]] = dict() self.param_to_chunk32: Dict[Parameter, Chunk] = dict() self.chunk16_set: Set[Chunk] = set() self.clipping_flag = clipping_norm > 0.0 self.max_norm = clipping_norm if self.clipping_flag: assert norm_type == 2.0, "ZeroOptimizer only supports L2 norm now" ddp_param_list = [] for name, param in module.named_parameters(): if is_ddp_ignored(param): if param.requires_grad: warnings.warn(f"Parameter `{name}` is ignored by DDP but requires gradient! " "You should handle its optimizer update by yourself!") else: ddp_param_list.append(param) for p, fp32_p in zip(ddp_param_list, module.fp32_params): chunk_16 = self.chunk_manager.get_chunk(p) if chunk_16 not in self.chunk16_set: chunk_16.l2_norm_flag = self.clipping_flag self.chunk16_set.add(chunk_16) self.__init__optimizer() # 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: torch.Tensor = torch.zeros(1, dtype=torch.int64, device=get_current_device()) self._logger = get_dist_logger() 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_params_h2d: bool = self.gemini_manager.is_cuda_margin_mem_avail and self.gpu_margin_mem_ratio > 0.0 and getattr( optim, 'num_fp32_shards_per_param', 0) >= 2 if self.gpu_margin_mem_ratio > 0.0 and not self.gemini_manager.is_cuda_margin_mem_avail: self._logger.warning(f'gpu_margin_mem_ratio is meaningless when placement_policy is not "auto"', ranks=[0]) self._register_states = disposable(self._register_states_) def _set_grad_ptr(self): for group in self.param_groups: for fake_param in group['params']: chunk32 = self.param_to_chunk32[fake_param] begin, end = self.param_to_range[fake_param] chunk16 = chunk32.paired_chunk fake_param.data = chunk16.payload[begin:end] fake_param.grad = fake_param.data fake_param.data = chunk32.payload[begin:end] def _update_fp16_params(self): none_tensor = torch.empty([0]) for group in self.param_groups: for fake_param in group['params']: assert fake_param.grad is None fake_param.data = none_tensor for chunk16 in self.chunk16_set: chunk16.optim_update() def _check_overflow(self): # clear previous overflow record self._found_overflow.fill_(self.module.overflow_counter) # all-reduce across global group dist.all_reduce(self._found_overflow) return self._found_overflow.item() > 0 def _clear_global_norm(self) -> None: for c16 in self.chunk16_set: c16.l2_norm = None def _calc_global_norm(self) -> float: norm_sqr: float = 0.0 group_to_norm = dict() for c16 in self.chunk16_set: assert c16.l2_norm is not None if c16.is_gathered: norm_sqr += c16.l2_norm else: # this chunk is sharded, use communication to collect total norm if c16.torch_pg not in group_to_norm: group_to_norm[c16.torch_pg] = 0.0 group_to_norm[c16.torch_pg] += c16.l2_norm c16.l2_norm = None # clear l2 norm comm_buffer = torch.zeros(1, dtype=torch.float, device=get_current_device()) for group, part_norm in group_to_norm.items(): comm_buffer.fill_(part_norm) dist.all_reduce(comm_buffer, group=group) norm_sqr += comm_buffer.item() global_norm = math.sqrt(norm_sqr) return global_norm def _get_combined_scale(self): loss_scale = 1 if self.optim_state == OptimState.SCALED: loss_scale = self.loss_scale self.optim_state = OptimState.UNSCALED combined_scale = loss_scale if self.clipping_flag: total_norm = self._calc_global_norm() clip = ((total_norm / loss_scale) + 1e-6) / self.max_norm if clip > 1: combined_scale = clip * loss_scale if combined_scale == 1: return -1 else: return combined_scale @property def loss_scale(self): return self.grad_scaler.scale.item() def zero_grad(self, *args, **kwargs): self.module.overflow_counter = 0 return self.optim.zero_grad(set_to_none=True) def step(self, *args, **kwargs): self._maybe_move_fp32_params() self._set_grad_ptr() found_inf = self._check_overflow() if found_inf: self.optim_state = OptimState.UNSCALED # no need to unscale grad self.grad_scaler.update(found_inf) # update gradient scaler self._logger.info(f'Found overflow. Skip step') self._clear_global_norm() # clear recorded norm self.zero_grad() # reset all gradients self._update_fp16_params() return # get combined scale. combined scale = loss scale * clipping norm # so that gradient = gradient / combined scale combined_scale = self._get_combined_scale() self.grad_scaler.update(found_inf) ret = self.optim.step(div_scale=combined_scale, *args, **kwargs) self._register_states() self.zero_grad() self._update_fp16_params() return ret def clip_grad_norm(self, model: torch.nn.Module, max_norm: float, norm_type: float = 2.0): raise NotImplementedError def backward(self, loss: torch.Tensor): loss = self.loss_scale * loss self.optim_state = OptimState.SCALED self.module.backward(loss) def backward_by_grad(self, tensor: torch.Tensor, grad: torch.Tensor): # 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.module.backward_by_grad(tensor, grad) def _maybe_move_fp32_params(self): if self._should_move_fp32_params_h2d: self._should_move_fp32_params_h2d = False available_cuda_margin_mem = self.gemini_manager.cuda_margin_mem * self.gpu_margin_mem_ratio fp32_params_available_cuda_margin_mem = available_cuda_margin_mem / self.optim.num_fp32_shards_per_param fp32_params_used_cuda_margin_mem = 0 for group in self.param_groups: for fake_param in group['params']: chunk32 = self.param_to_chunk32[fake_param] chunk16 = chunk32.paired_chunk if chunk32.device_type == 'cuda': continue if fp32_params_used_cuda_margin_mem + chunk32.payload_mem < fp32_params_available_cuda_margin_mem: self.chunk_manager.move_chunk(chunk32, get_current_device()) # stores grad now self.chunk_manager.move_chunk(chunk16, get_current_device()) self.module.set_chunk_grad_device(chunk16, get_current_device()) fp32_params_used_cuda_margin_mem += chunk32.payload_mem for group in self.param_groups: for fake_param in group['params']: chunk32 = self.param_to_chunk32[fake_param] if chunk32.device_type == 'cuda': state = self.optim.state[fake_param] for k, v in state.items(): if isinstance(v, torch.Tensor): state[k] = v.to(get_current_device()) def _register_states_(self): for group in self.optim.param_groups: for p in group['params']: state = self.optim.state[p] for val in state.values(): if isinstance(val, torch.Tensor): self.chunk_manager.add_extern_static_tensor(val) def __init__optimizer(self): def get_range_pair(local_chunk: Chunk, local_param: Parameter): param_info = local_chunk.tensors_info[local_param] if local_chunk.keep_gathered: return param_info.offset, param_info.end begin = max(0, param_info.offset - local_chunk.shard_begin) end = min(local_chunk.shard_size, param_info.end - local_chunk.shard_begin) return begin, end for group in self.optim.param_groups: fake_params_list = list() for param in group['params']: if is_ddp_ignored(param): continue chunk16 = self.chunk_manager.get_chunk(param) range_pair = get_range_pair(chunk16, param) if range_pair[0] >= range_pair[1]: continue fake_param = torch.nn.Parameter(torch.empty([0])) self.param_to_chunk32[fake_param] = chunk16.paired_chunk self.param_to_range[fake_param] = range_pair fake_params_list.append(fake_param) group['params'] = fake_params_list
"""Adapted from https://github.com/NUS-HPC-AI-Lab/LARS-ImageNet-PyTorch/blob/main/lars.py""" from typing import Iterable import torch from torch.optim import Optimizer from colossalai.registry import OPTIMIZERS @OPTIMIZERS.register_module class Lars(Optimizer): r"""Implements the LARS optimizer from `"Large batch training of convolutional networks" <https://arxiv.org/pdf/1708.03888.pdf>`_. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float, optional): learning rate (default: 1e-3) momentum (float, optional): momentum factor (default: 0) eeta (float, optional): LARS coefficient as used in the paper (default: 1e-3) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) """ def __init__( self, params: Iterable[torch.nn.Parameter], lr=1e-3, momentum=0, eeta=1e-3, weight_decay=0, epsilon=0.0 ) -> None: if not isinstance(lr, float) or lr < 0.0: raise ValueError("Invalid learning rate: {}".format(lr)) if momentum < 0.0: raise ValueError("Invalid momentum value: {}".format(momentum)) if weight_decay < 0.0: raise ValueError( "Invalid weight_decay value: {}".format(weight_decay)) if eeta <= 0 or eeta > 1: raise ValueError("Invalid eeta value: {}".format(eeta)) if epsilon < 0: raise ValueError("Invalid epsilon value: {}".format(epsilon)) defaults = dict(lr=lr, momentum=momentum, weight_decay=weight_decay, eeta=eeta, epsilon=epsilon, lars=True) super().__init__(params, defaults) @torch.no_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: with torch.enable_grad(): loss = closure() for group in self.param_groups: weight_decay = group['weight_decay'] momentum = group['momentum'] eeta = group['eeta'] lr = group['lr'] lars = group['lars'] eps = group['epsilon'] for p in group['params']: if p.grad is None: continue decayed_grad = p.grad scaled_lr = lr if lars: w_norm = torch.norm(p) g_norm = torch.norm(p.grad) trust_ratio = torch.where( w_norm > 0 and g_norm > 0, eeta * w_norm / (g_norm + weight_decay * w_norm + eps), torch.ones_like(w_norm) ) trust_ratio.clamp_(0.0, 50) scaled_lr *= trust_ratio.item() if weight_decay != 0: decayed_grad = decayed_grad.add(p, alpha=weight_decay) decayed_grad = torch.clamp(decayed_grad, -10.0, 10.0) if momentum != 0: param_state = self.state[p] if 'momentum_buffer' not in param_state: buf = param_state['momentum_buffer'] = torch.clone( decayed_grad).detach() else: buf = param_state['momentum_buffer'] buf.mul_(momentum).add_(decayed_grad) decayed_grad = buf p.add_(decayed_grad, alpha=-scaled_lr) return loss
from .colossalai_optimizer import ColossalaiOptimizer from .fused_adam import FusedAdam from .fused_lamb import FusedLAMB from .fused_sgd import FusedSGD from .lamb import Lamb from .lars import Lars from .cpu_adam import CPUAdam from .hybrid_adam import HybridAdam __all__ = ['ColossalaiOptimizer', 'FusedLAMB', 'FusedAdam', 'FusedSGD', 'Lamb', 'Lars', 'CPUAdam', 'HybridAdam']
import math from typing import Optional import torch from colossalai.kernel.op_builder import CPUAdamBuilder from colossalai.registry import OPTIMIZERS from .nvme_optimizer import NVMeOptimizer @OPTIMIZERS.register_module class CPUAdam(NVMeOptimizer): """Implements Adam algorithm. Supports parameters updating on both GPU and CPU, depanding on the device of paramters. But the parameters and gradients should on the same device: * Parameters on CPU and gradients on CPU is allowed. * Parameters on GPU and gradients on GPU is allowed. * Parameters on GPU and gradients on CPU is **not** allowed. `CPUAdam` requires CUDA extensions which can be built during installation or runtime. This version of CPU Adam accelates parameters updating on CPU with SIMD. Support of AVX2 or AVX512 is required. The GPU part is implemented in an naive way. CPU Adam also supports the hybrid precision calculation, eg. fp32 parameters and fp16 gradients. :class:`colossalai.nn.optimizer.CPUAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: model_params (iterable): iterable of parameters of dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED yet in CPUAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) simd_log (boolean, optional): whether to show if you are using SIMD to accelerate. (default: False) nvme_offload_fraction (float, optional): Fraction of optimizer states to be offloaded to NVMe. Defaults to 0.0. nvme_offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ # Number of fp32 shards for per parameter # Param weight, grad, momentum and variance num_fp32_shards_per_param = 4 def __init__(self, model_params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, adamw_mode=True, nvme_offload_fraction: float = 0.0, nvme_offload_dir: Optional[str] = None): default_args = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, bias_correction=bias_correction) super(CPUAdam, self).__init__(model_params, default_args, nvme_offload_fraction, nvme_offload_dir) self.adamw_mode = adamw_mode cpu_adam = CPUAdamBuilder().load() self.cpu_adam_op = cpu_adam.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay, adamw_mode) def torch_adam_update(self, data, grad, exp_avg, exp_avg_sq, lr, beta1, beta2, eps, weight_decay, bias_correction1, bias_correction2, use_adamw=False): # FIXME(ver217): remove the below line when replace torch adam with fused adam grad = grad.float() if weight_decay != 0: if use_adamw: data.mul_(1 - lr * weight_decay) else: grad = grad.add(data, alpha=weight_decay) # Decay the first and second moment running average coefficient exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1) exp_avg_sq.mul_(beta2).addcmul_(grad, grad, value=1 - beta2) # TODO(jiaruifang) dose not support amsgrad denom = (exp_avg_sq.sqrt() / math.sqrt(bias_correction2)).add_(eps) step_size = lr / bias_correction1 data.addcdiv_(exp_avg, denom, value=-step_size) @torch.no_grad() def step(self, closure=None, div_scale: float = -1): loss = None if closure is not None: with torch.enable_grad(): loss = closure() self._pre_step('exp_avg', 'exp_avg_sq') for _, group in enumerate(self.param_groups): for _, p in enumerate(group['params']): if p.grad is None: continue state = self.state[p] target_device = p.device if len(state) == 0: state['step'] = 0 # gradient momentums state['exp_avg'] = torch.zeros_like(p, dtype=torch.float, device=target_device) # gradient variances state['exp_avg_sq'] = torch.zeros_like(p, dtype=torch.float, device=target_device) self._post_state_init(p) state['step'] += 1 beta1, beta2 = group['betas'] if target_device.type == 'cpu': assert p.data.numel() == p.grad.data.numel(), "parameter and gradient should have the same size" assert state['exp_avg'].device.type == 'cpu', "exp_avg should stay on cpu" assert state['exp_avg_sq'].device.type == 'cpu', "exp_avg should stay on cpu" self._pre_update(p, 'exp_avg', 'exp_avg_sq') self.cpu_adam_op.step(state['step'], group['lr'], beta1, beta2, group['eps'], group['weight_decay'], group['bias_correction'], p.data, p.grad.data, state['exp_avg'], state['exp_avg_sq'], div_scale) self._post_update(p, 'exp_avg', 'exp_avg_sq') elif target_device.type == 'cuda': assert div_scale == -1, "div_scale should remain default" assert state['exp_avg'].device.type == 'cuda', "exp_avg should stay on cuda" assert state['exp_avg_sq'].device.type == 'cuda', "exp_avg should stay on cuda" bias_correction1 = 1 - beta1**state['step'] bias_correction2 = 1 - beta2**state['step'] # adam on cuda self.torch_adam_update(p.data, p.grad.data, state['exp_avg'], state['exp_avg_sq'], group['lr'], beta1, beta2, group['eps'], group['weight_decay'], bias_correction1, bias_correction2, self.adamw_mode) else: raise RuntimeError self._post_step() return loss
import torch import torch.nn as nn from torch import Tensor from torch.optim import Optimizer from colossalai.utils import clip_grad_norm_fp32 class ColossalaiOptimizer(Optimizer): def __init__(self, optim: Optimizer): self.optim = optim @property def param_groups(self): return self.optim.param_groups @property def defaults(self): return self.optim.defaults def add_param_group(self, *args, **kwargs): return self.optim.add_param_group(*args, **kwargs) def step(self, *args, **kwargs): return self.optim.step(*args, **kwargs) def zero_grad(self, *args, **kwargs): self.optim.zero_grad(*args, **kwargs) def load_state_dict(self, *args, **kwargs): self.optim.load_state_dict(*args, **kwargs) def state_dict(self): return self.optim.state_dict() def backward(self, loss: Tensor): loss.backward() def backward_by_grad(self, tensor: Tensor, grad: Tensor): torch.autograd.backward(tensors=tensor, grad_tensors=grad) def clip_grad_norm(self, model: nn.Module, max_norm: float): if max_norm > 0.0: clip_grad_norm_fp32(model.parameters(), max_norm)
from typing import Any import torch from colossalai.nn.optimizer import HybridAdam from colossalai.nn.optimizer.zero_optimizer import ZeroOptimizer __all__ = ['GeminiAdamOptimizer'] class GeminiAdamOptimizer(ZeroOptimizer): def __init__(self, model: torch.nn.Module, **defaults: Any) -> None: optimizer = HybridAdam(model.parameters(), **defaults) super().__init__(optimizer, model, **defaults)
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_lamb.py import torch from colossalai.registry import OPTIMIZERS from colossalai.utils import multi_tensor_applier @OPTIMIZERS.register_module class FusedLAMB(torch.optim.Optimizer): """Implements LAMB algorithm. `FusedLAMB` requires CUDA extensions which can be built during installation or runtime. This version of fused LAMB implements 2 fusions. * Fusion of the LAMB update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedLAMB`'s usage is identical to any ordinary Pytorch optimizer :class:`colossalai.nn.optimizer.FusedLAMB` may be used with or without Amp. LAMB was proposed in `Large Batch Optimization for Deep Learning: Training BERT in 76 minutes`_. Arguments: params (iterable): iterable of parameters to optimize or dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its norm. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-6) weight_decay (float, optional): weight decay (L2 penalty) (default: 0.01) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ NOT SUPPORTED now! (default: False) adam_w_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) grad_averaging (bool, optional): whether apply (1-beta2) to grad when calculating running averages of gradient. (default: True) set_grad_none (bool, optional): whether set grad to None when zero_grad() method is called. (default: True) max_grad_norm (float, optional): value used to clip global grad norm (default: 1.0) use_nvlamb (boolean, optional): Apply adaptive learning rate to 0.0 weight decay parameter (default: False) .. _Large Batch Optimization for Deep Learning: Training BERT in 76 minutes: https://arxiv.org/abs/1904.00962 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ def __init__(self, params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-6, weight_decay=0.01, amsgrad=False, adam_w_mode=True, grad_averaging=True, set_grad_none=True, max_grad_norm=1.0, use_nvlamb=False): if amsgrad: raise RuntimeError('FusedLAMB does not support the AMSGrad variant.') defaults = dict(lr=lr, bias_correction=bias_correction, betas=betas, eps=eps, weight_decay=weight_decay, grad_averaging=grad_averaging, max_grad_norm=max_grad_norm) super(FusedLAMB, self).__init__(params, defaults) if multi_tensor_applier.available: from colossalai.kernel.op_builder import FusedOptimBuilder fused_optim = FusedOptimBuilder().load() self.multi_tensor_l2norm = fused_optim.multi_tensor_l2norm # Skip buffer self._dummy_overflow_buf = torch.tensor([0], dtype=torch.int, device=self.param_groups[0]["params"][0].device) self.multi_tensor_lamb = fused_optim.multi_tensor_lamb else: raise RuntimeError('FusedLAMB requires cuda extensions') self.adam_w_mode = 1 if adam_w_mode else 0 self.set_grad_none = set_grad_none self.use_nvlamb = use_nvlamb def zero_grad(self): if self.set_grad_none: for group in self.param_groups: for p in group['params']: p.grad = None else: super(FusedLAMB, self).zero_grad() def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() # create separate grad lists for fp32 and fp16 params g_all_32, g_all_16 = [], [] for group in self.param_groups: for p in group['params']: if p.grad is None: continue if p.dtype == torch.float32: g_all_32.append(p.grad.data) elif p.dtype == torch.float16: g_all_16.append(p.grad.data) else: raise RuntimeError('FusedLAMB only support fp16 and fp32.') device = self.param_groups[0]["params"][0].device g_norm_32, g_norm_16 = torch.zeros(1, device=device), torch.zeros(1, device=device) # compute grad norm for two lists if len(g_all_32) > 0: g_norm_32 = multi_tensor_applier(self.multi_tensor_l2norm, self._dummy_overflow_buf, [g_all_32], False)[0] if len(g_all_16) > 0: g_norm_16 = multi_tensor_applier(self.multi_tensor_l2norm, self._dummy_overflow_buf, [g_all_16], False)[0] # blend two grad norms to get global grad norm global_grad_norm = multi_tensor_applier(self.multi_tensor_l2norm, self._dummy_overflow_buf, [[g_norm_32, g_norm_16]], False)[0] max_grad_norm = self.defaults['max_grad_norm'] for group in self.param_groups: bias_correction = 1 if group['bias_correction'] else 0 beta1, beta2 = group['betas'] grad_averaging = 1 if group['grad_averaging'] else 0 # assume same step across group now to simplify things # per parameter step can be easily support by making it tensor, or pass list into kernel if 'step' in group: group['step'] += 1 else: group['step'] = 1 # create lists for multi-tensor apply g_16, p_16, m_16, v_16 = [], [], [], [] g_32, p_32, m_32, v_32 = [], [], [], [] for p in group['params']: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError( 'FusedLAMB does not support sparse gradients, please consider SparseAdam instead') state = self.state[p] # State initialization if len(state) == 0: # Exponential moving average of gradient values state['exp_avg'] = torch.zeros_like(p) # Exponential moving average of gradient values state['exp_avg_sq'] = torch.zeros_like(p) if p.dtype == torch.float16: g_16.append(p.grad.data) p_16.append(p.data) m_16.append(state['exp_avg']) v_16.append(state['exp_avg_sq']) elif p.dtype == torch.float32: g_32.append(p.grad.data) p_32.append(p.data) m_32.append(state['exp_avg']) v_32.append(state['exp_avg_sq']) else: raise RuntimeError('FusedLAMB only support fp16 and fp32.') if (len(g_16) > 0): multi_tensor_applier(self.multi_tensor_lamb, self._dummy_overflow_buf, [g_16, p_16, m_16, v_16], group['lr'], beta1, beta2, group['eps'], group['step'], bias_correction, group['weight_decay'], grad_averaging, self.adam_w_mode, global_grad_norm, max_grad_norm, self.use_nvlamb) if (len(g_32) > 0): multi_tensor_applier(self.multi_tensor_lamb, self._dummy_overflow_buf, [g_32, p_32, m_32, v_32], group['lr'], beta1, beta2, group['eps'], group['step'], bias_correction, group['weight_decay'], grad_averaging, self.adam_w_mode, global_grad_norm, max_grad_norm, self.use_nvlamb) return loss
import torch import os import tempfile import math from torch.nn.parameter import Parameter from typing import Optional, List, Dict, Callable class NVMeOptimizer(torch.optim.Optimizer): """A base class for offloading optimizer states. Args: params: parameters defaults (dict): default dict nvme_offload_fraction (float, optional): Fraction of params to be offloaded to NVMe. Defaults to 0.0. offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. Raises: ImportError: Raise if ``tensornvme`` is not installed. """ def __init__(self, params, defaults: dict, nvme_offload_fraction: float = 0.0, offload_dir: Optional[str] = None) -> None: assert 0.0 <= nvme_offload_fraction <= 1.0 super().__init__(params, defaults) self.nvme_offload_fraction = float(nvme_offload_fraction) if self.nvme_offload_fraction > 0.0: try: from tensornvme import DiskOffloader from tensornvme._C import get_backends except ModuleNotFoundError: raise ModuleNotFoundError('Please install tensornvme to use NVMeOptimizer') self.offload_dir = offload_dir or tempfile.mkdtemp() backend = 'uring' if 'uring' in get_backends() else 'aio' self.offloader = DiskOffloader(self.offload_dir, 8, backend=backend) else: self.offload_dir = None self.offloader = None self.is_on_nvme: Dict[Parameter, bool] = {} self.offloaded_numel: int = 0 self.total_numel: int = self._get_numel() self.can_offload_numel = math.floor(self.total_numel * self.nvme_offload_fraction) self.prefetch_params: List[Parameter] = [] self.param_to_prefetch_idx: Dict[Parameter, int] = {} def _get_numel(self) -> int: numel = 0 for group in self.param_groups: for p in group['params']: numel += p.storage().size() return numel def _post_state_init(self, param: Parameter) -> None: numel = param.storage().size() if self.offloader is not None and param.device.type == 'cpu' and numel + self.offloaded_numel <= self.can_offload_numel: self.is_on_nvme[param] = True self.offloaded_numel += numel else: self.is_on_nvme[param] = False def _setup_prefetch_params(self) -> List[Parameter]: if self.offloader is None: return assert len(self.prefetch_params) == 0 and len(self.param_to_prefetch_idx) == 0 for group in self.param_groups: for p in group['params']: if p.grad is None: continue if len(self.state[p]) > 0 and self.is_on_nvme[p]: assert p.device.type == 'cpu' self.param_to_prefetch_idx[p] = len(self.prefetch_params) self.prefetch_params.append(p) def _pre_step(self, *state_keys: str) -> None: self._setup_prefetch_params() if self.offloader is None or len(self.prefetch_params) == 0: return state = self.state[self.prefetch_params[0]] for key in state_keys: self.offloader.async_read(state[key]) def _pre_update(self, param: Parameter, *state_keys: str) -> None: if self.offloader is None or param not in self.param_to_prefetch_idx: return self.offloader.sync_read_events() idx = self.param_to_prefetch_idx[param] if idx + 1 < len(self.prefetch_params): state = self.state[self.prefetch_params[idx + 1]] for key in state_keys: self.offloader.async_read(state[key]) def _post_update(self, param: Parameter, *state_keys: str) -> None: if self.offloader is None: return self.offloader.sync_write_events() if self.is_on_nvme[param]: state = self.state[param] for key in state_keys: self.offloader.async_write(state[key]) def _post_step(self) -> None: if self.offloader is not None: self.offloader.synchronize() self.prefetch_params.clear() self.param_to_prefetch_idx.clear() def step(self, closure: Optional[Callable[[], float]] = ...) -> Optional[float]: """Performs a single optimization step (parameter update). Example: >>> self._pre_step('exp_avg', 'exp_avg_sq') >>> for group in self.param_groups: >>> for p in group['params']: >>> if p.grad is None: >>> continue >>> state = self.state[p] >>> if len(state) == 0: >>> state['exp_avg'] = ... >>> state['exp_avg_sq'] = ... >>> self._post_state_init(p) >>> if p.device.type == 'cpu': >>> self._pre_update(p, 'exp_avg', 'exp_avg_sq') >>> adam() >>> self._post_update(p, 'exp_avg', 'exp_avg_sq') >>> else: >>> ... >>> self._post_step() Args: closure (Optional[Callable[[], float]], optional): A closure that reevaluates the model and returns the loss. Optional for most optimizers. """ raise NotImplementedError def state_dict(self) -> dict: # TODO(ver217): design a new method to save state_dict. When using NVMe offload, this method may lead to OOM. if self.offloader is not None: raise NotImplementedError return super().state_dict() def load_state_dict(self, state_dict: dict) -> None: # TODO(ver217): design a new method to load state_dict. When using NVMe offload, whole state_dict may not be able to fit in memory. if self.offloader is not None: raise NotImplementedError super().load_state_dict(state_dict) def __del__(self) -> None: if getattr(self, 'offloader', None) is not None: del self.offloader if os.path.exists(self.offload_dir): try: os.rmdir(self.offload_dir) except OSError: pass
from typing import Any, Optional import torch from colossalai.kernel.op_builder import CPUAdamBuilder, FusedOptimBuilder from colossalai.registry import OPTIMIZERS from colossalai.utils import multi_tensor_applier from .nvme_optimizer import NVMeOptimizer @OPTIMIZERS.register_module class HybridAdam(NVMeOptimizer): """Implements Adam algorithm. Supports parameters updating on both GPU and CPU, depanding on the device of paramters. But the parameters and gradients should on the same device: * Parameters on CPU and gradients on CPU is allowed. * Parameters on GPU and gradients on GPU is allowed. * Parameters on GPU and gradients on CPU is **not** allowed. `HybriadAdam` requires CUDA extensions which can be built during installation or runtime. This version of Hybrid Adam is an hybrid of CPUAdam and FusedAdam. * For parameters updating on CPU, it uses CPUAdam. * For parameters updating on GPU, it uses FusedAdam. * Hybird precision calculation of fp16 and fp32 is supported, eg fp32 parameters and fp16 gradients. :class:`colossalai.nn.optimizer.HybridAdam` may be used as a drop-in replacement for ``torch.optim.AdamW``, or ``torch.optim.Adam`` with ``adamw_mode=False`` Adam was been proposed in `Adam: A Method for Stochastic Optimization`_. Arguments: model_params (iterable): iterable of parameters of dicts defining parameter groups. lr (float, optional): learning rate. (default: 1e-3) betas (Tuple[float, float], optional): coefficients used for computing running averages of gradient and its square. (default: (0.9, 0.999)) eps (float, optional): term added to the denominator to improve numerical stability. (default: 1e-8) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) amsgrad (boolean, optional): whether to use the AMSGrad variant of this algorithm from the paper `On the Convergence of Adam and Beyond`_ (default: False) NOT SUPPORTED yet in CPUAdam! adamw_mode (boolean, optional): Apply L2 regularization or weight decay True for decoupled weight decay(also known as AdamW) (default: True) simd_log (boolean, optional): whether to show if you are using SIMD to accelerate. (default: False) nvme_offload_fraction (float, optional): Fraction of optimizer states to be offloaded to NVMe. Defaults to 0.0. nvme_offload_dir (Optional[str], optional): Directory to save NVMe offload files. If it's ``None``, a random temporary directory will be used. Defaults to None. .. _Adam\: A Method for Stochastic Optimization: https://arxiv.org/abs/1412.6980 .. _On the Convergence of Adam and Beyond: https://openreview.net/forum?id=ryQu7f-RZ """ # Number of fp32 shards for per parameter # Param weight, grad, momentum and variance num_fp32_shards_per_param = 4 def __init__(self, model_params, lr=1e-3, bias_correction=True, betas=(0.9, 0.999), eps=1e-8, weight_decay=0, adamw_mode=True, nvme_offload_fraction: float = 0.0, nvme_offload_dir: Optional[str] = None, **defaults: Any): default_args = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay, bias_correction=bias_correction) super(HybridAdam, self).__init__(model_params, default_args, nvme_offload_fraction, nvme_offload_dir) self.adamw_mode = adamw_mode # build during runtime if not found cpu_optim = CPUAdamBuilder().load() fused_optim = FusedOptimBuilder().load() self.cpu_adam_op = cpu_optim.CPUAdamOptimizer(lr, betas[0], betas[1], eps, weight_decay, adamw_mode) self.gpu_adam_op = fused_optim.multi_tensor_adam self._dummy_overflow_buf = torch.cuda.IntTensor([0]) @torch.no_grad() def step(self, closure=None, div_scale: float = -1): loss = None if closure is not None: with torch.enable_grad(): loss = closure() self._pre_step('exp_avg', 'exp_avg_sq') for _, group in enumerate(self.param_groups): g_l, p_l, m_l, v_l = [], [], [], [] group_step = 0 for _, p in enumerate(group['params']): if p.grad is None: continue state = self.state[p] target_device = p.device if len(state) == 0: state['step'] = 0 # gradient momentums state['exp_avg'] = torch.zeros_like(p, dtype=torch.float, device=target_device) # gradient variances state['exp_avg_sq'] = torch.zeros_like(p, dtype=torch.float, device=target_device) self._post_state_init(p) state['step'] += 1 group_step = state['step'] beta1, beta2 = group['betas'] if target_device.type == 'cpu': assert state['exp_avg'].device.type == 'cpu', "exp_avg should stay on cpu" assert state['exp_avg_sq'].device.type == 'cpu', "exp_avg should stay on cpu" self._pre_update(p, 'exp_avg', 'exp_avg_sq') self.cpu_adam_op.step(state['step'], group['lr'], beta1, beta2, group['eps'], group['weight_decay'], group['bias_correction'], p.data, p.grad.data, state['exp_avg'], state['exp_avg_sq'], div_scale) self._post_update(p, 'exp_avg', 'exp_avg_sq') elif target_device.type == 'cuda': assert state['exp_avg'].device.type == 'cuda', "exp_avg should stay on cuda" assert state['exp_avg_sq'].device.type == 'cuda', "exp_avg should stay on cuda" # record the state by gruop and update at once g_l.append(p.grad.data) p_l.append(p.data) m_l.append(state['exp_avg']) v_l.append(state['exp_avg_sq']) else: raise RuntimeError if len(g_l) > 0: adamw_mode = 1 if self.adamw_mode else 0 bias_correction = 1 if group['bias_correction'] else 0 multi_tensor_applier(self.gpu_adam_op, self._dummy_overflow_buf, [g_l, p_l, m_l, v_l], group['lr'], group['betas'][0], group['betas'][1], group['eps'], group_step, adamw_mode, bias_correction, group['weight_decay'], div_scale) self._post_step() return loss
# modified from https://github.com/NVIDIA/apex/blob/master/apex/optimizers/fused_sgd.py import torch from torch.optim.optimizer import Optimizer, required from colossalai.registry import OPTIMIZERS from colossalai.utils import multi_tensor_applier @OPTIMIZERS.register_module class FusedSGD(Optimizer): r"""Implements stochastic gradient descent (optionally with momentum). `FusedSGD` requires CUDA extensions which can be built during installation or runtime. This version of fused SGD implements 2 fusions. * Fusion of the SGD update's elementwise operations * A multi-tensor apply launch that batches the elementwise updates applied to all the model's parameters into one or a few kernel launches. :class:`colossalai.nn.optimizer.FusedSGD` may be used as a drop-in replacement for ``torch.optim.SGD`` :class:`colossalai.nn.optimizer.FusedSGD` may be used with or without Amp. Nesterov momentum is based on the formula from `On the importance of initialization and momentum in deep learning`__. Args: params (iterable): iterable of parameters to optimize or dicts defining parameter groups lr (float): learning rate momentum (float, optional): momentum factor (default: 0) weight_decay (float, optional): weight decay (L2 penalty) (default: 0) dampening (float, optional): dampening for momentum (default: 0) nesterov (bool, optional): enables Nesterov momentum (default: False) __ http://www.cs.toronto.edu/%7Ehinton/absps/momentum.pdf .. note:: The implementation of SGD with Momentum/Nesterov subtly differs from Sutskever et. al. and implementations in some other frameworks. Considering the specific case of Momentum, the update can be written as .. math:: v = \rho * v + g \\ p = p - lr * v where p, g, v and :math:`\rho` denote the parameters, gradient, velocity, and momentum respectively. This is in contrast to Sutskever et. al. and other frameworks which employ an update of the form .. math:: v = \rho * v + lr * g \\ p = p - v The Nesterov version is analogously modified. """ def __init__(self, params, lr=required, momentum=0, dampening=0, weight_decay=0, nesterov=False, wd_after_momentum=False): if lr is not required and lr < 0.0: raise ValueError("Invalid learning rate: {}".format(lr)) if momentum < 0.0: raise ValueError("Invalid momentum value: {}".format(momentum)) if weight_decay < 0.0: raise ValueError("Invalid weight_decay value: {}".format(weight_decay)) defaults = dict(lr=lr, momentum=momentum, dampening=dampening, weight_decay=weight_decay, nesterov=nesterov) if nesterov and (momentum <= 0 or dampening != 0): raise ValueError("Nesterov momentum requires a momentum and zero dampening") super(FusedSGD, self).__init__(params, defaults) self.wd_after_momentum = wd_after_momentum if multi_tensor_applier.available: from colossalai.kernel.op_builder import FusedOptimBuilder fused_optim = FusedOptimBuilder().load() # Skip buffer self._dummy_overflow_buf = torch.tensor([0], dtype=torch.int, device=self.param_groups[0]["params"][0].device) self.multi_tensor_sgd = fused_optim.multi_tensor_sgd else: raise RuntimeError('FusedSGD requires cuda extensions') def __setstate__(self, state): super(FusedSGD, self).__setstate__(state) for group in self.param_groups: group.setdefault('nesterov', False) def get_momentums(self, params): momentums = [] first_run = True for p in params: param_state = self.state[p] # torch.optim.SGD initializes momentum in the main loop, we have # to do it here, and track whether or not we've done so, so that # momentum application can be skipped in the main kernel. if 'momentum_buffer' not in param_state: first_run = True buf = param_state['momentum_buffer'] = torch.zeros_like(p) momentums.append(buf) else: first_run = False momentums.append(param_state['momentum_buffer']) return momentums, first_run def step(self, closure=None): """Performs a single optimization step. Arguments: closure (callable, optional): A closure that reevaluates the model and returns the loss. """ loss = None if closure is not None: loss = closure() for group in self.param_groups: weight_decay = group['weight_decay'] momentum = group['momentum'] dampening = group['dampening'] nesterov = group['nesterov'] # For each group, there are 3 possible combinations we need to consider: # grad_type, param_to_update_type, momentum_type # 1. fp16, fp16, fp16 # 2. fp32, fp32, fp32 # 3. fp16, fp32, fp32 g_l, p_l = [], [] for p in group['params']: if p.grad is None: continue if p.grad.data.is_sparse: raise RuntimeError('FusedSGD does not support sparse gradients') g_l.append(p.grad) p_l.append(p) m_l, first_run = self.get_momentums(p_l) multi_tensor_applier(self.multi_tensor_sgd, self._dummy_overflow_buf, [g_l, p_l, m_l], weight_decay, momentum, dampening, group['lr'], nesterov, first_run, self.wd_after_momentum, 1.0) return loss
import torch.nn as nn from colossalai.registry import LOSSES from torch.nn.modules.loss import _Loss from colossalai.context.moe_context import MOE_CONTEXT @LOSSES.register_module class MoeCrossEntropyLoss(_Loss): r"""torch.nn.CrossEntropyLoss added with auxiliary loss. Args: input (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). target (:class:`torch.tensor`): Ground truth class indices or class probabilities. aux_weight (float, optional): Weight of auxiliary loss in total loss.Defaults 0.01. The ``args`` and ``kwargs`` should include parameters below: :: weight (Tensor, optional) size_average (bool, optional) ignore_index (int, optional) reduce (bool, optional) reduction (str, optional) label_smoothing (float, optional) More details about ``args``, ``kwargs`` and ``torch.nn.functional.cross_entropy`` could be found in `Cross_entropy <https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html#torch.nn.functional.cross_entropy>`_. """ def __init__(self, aux_weight: float = 0.01, *args, **kwargs): super().__init__() self.loss = nn.CrossEntropyLoss(*args, **kwargs) self.aux_weight = aux_weight def forward(self, *args): """ The ``args`` should at least include parameters below: :: input (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). target (:class:`torch.tensor`): Ground truth class indices or class probabilities. More details about ``args``, ``kwargs`` and ``torch.nn.functional.cross_entropy`` could be found in `Cross_entropy <https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html#torch.nn.functional.cross_entropy>`_. """ main_loss = self.loss(*args) aux_loss = MOE_CONTEXT.get_loss() return main_loss + self.aux_weight * aux_loss @LOSSES.register_module class MoeLoss(_Loss): """A wrapper class for any loss module to add with auxiliary loss. Args: aux_weight (float): Weight of auxiliary loss in total loss. loss_fn (``Callable``): Loss function. args (list): Args in loss function. kwargs (dict): Kwargs in loss function """ def __init__(self, aux_weight: float, loss_fn, *args, **kwargs): super().__init__() self.loss_fn = loss_fn(*args, **kwargs) self.aux_weight = aux_weight def forward(self, *args, **kwargs): """ The ``args`` and ``kwargs`` should at least include parameters below: :: input (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). target (:class:`torch.tensor`): Ground truth class indices or class probabilities. Note: The ``args`` and ``kwargs`` may include different parameters varying with different loss function. """ main_loss = self.loss_fn(*args, **kwargs) aux_loss = MOE_CONTEXT.get_loss() return main_loss + self.aux_weight * aux_loss
from colossalai.global_variables import tensor_parallel_env as env from colossalai.nn.layer.utils import get_tensor_parallel_mode from torch import nn from torch.nn.modules.loss import * from torch.nn.modules.loss import _Loss from .loss_1d import VocabParallelCrossEntropyLoss1D from .loss_2d import CrossEntropyLoss2D, VocabParallelCrossEntropyLoss2D from .loss_2p5d import CrossEntropyLoss2p5D, VocabParallelCrossEntropyLoss2p5D from .loss_3d import CrossEntropyLoss3D, VocabParallelCrossEntropyLoss3D from .loss_moe import MoeCrossEntropyLoss, MoeLoss _parallel_cross_entropy = { '2d': CrossEntropyLoss2D, '2.5d': CrossEntropyLoss2p5D, '3d': CrossEntropyLoss3D, } _vocab_parallel_cross_entropy = { '1d': VocabParallelCrossEntropyLoss1D, '2d': VocabParallelCrossEntropyLoss2D, '2.5d': VocabParallelCrossEntropyLoss2p5D, '3d': VocabParallelCrossEntropyLoss3D, } class CrossEntropyLoss(_Loss): def __init__(self, reduction: bool = True, *args, **kwargs): super().__init__() tensor_parallel = get_tensor_parallel_mode() if tensor_parallel is not None and env.vocab_parallel: self.loss = _vocab_parallel_cross_entropy[tensor_parallel](reduction=reduction, *args, **kwargs) elif tensor_parallel is None or tensor_parallel == '1d': reduction = 'mean' if reduction else 'none' self.loss = nn.CrossEntropyLoss(reduction=reduction, *args, **kwargs) else: self.loss = _parallel_cross_entropy[tensor_parallel](reduction=reduction, *args, **kwargs) def forward(self, *args): return self.loss(*args)
import torch import torch.distributed as dist from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.registry import LOSSES from torch.cuda.amp import custom_bwd, custom_fwd from torch.nn.modules.loss import _Loss class _VocabParallelCrossEntropy1D(torch.autograd.Function): @staticmethod @custom_fwd(cast_inputs=torch.float32) def forward(ctx, vocab_parallel_logits, targets, process_group): if process_group is None: process_group = gpc.get_group(ParallelMode.PARALLEL_1D) # Maximum value along vocab dimension across all GPUs. logits_max = torch.max(vocab_parallel_logits, dim=-1)[0] torch.distributed.all_reduce(logits_max, op=torch.distributed.ReduceOp.MAX, group=process_group) # Subtract the maximum value. vocab_parallel_logits.sub_(logits_max.unsqueeze(dim=-1)) # Get the partition's vocab indecies partition_vocab_size = vocab_parallel_logits.size()[-1] rank = dist.get_rank(process_group) vocab_start_index = partition_vocab_size * rank vocab_end_index = vocab_start_index + partition_vocab_size # Create a mask of valid vocab ids (1 means it needs to be masked). target_mask = (targets < vocab_start_index) | (targets >= vocab_end_index) masked_target = targets.clone() - vocab_start_index masked_target[target_mask] = 0 # Get predicted-logits = logits[target]. # For Simplicity, we convert logits to a 2-D tensor with size # [*, partition-vocab-size] and target to a 1-D tensor of size [*]. logits_2d = vocab_parallel_logits.view(-1, partition_vocab_size) masked_target_1d = masked_target.view(-1) arange_1d = torch.arange(start=0, end=logits_2d.size()[0], device=logits_2d.device) predicted_logits_1d = logits_2d[arange_1d, masked_target_1d] predicted_logits_1d = predicted_logits_1d.clone().contiguous() predicted_logits = predicted_logits_1d.view_as(targets) predicted_logits[target_mask] = 0.0 # All reduce is needed to get the chunks from other GPUs. torch.distributed.all_reduce(predicted_logits, op=torch.distributed.ReduceOp.SUM, group=process_group) # Sum of exponential of logits along vocab dimension across all GPUs. exp_logits = torch.exp(vocab_parallel_logits) sum_exp_logits = exp_logits.sum(dim=-1) torch.distributed.all_reduce(sum_exp_logits, op=torch.distributed.ReduceOp.SUM, group=process_group) # Loss = log(sum(exp(logits))) - predicted-logit. loss = torch.log(sum_exp_logits) - predicted_logits # Store softmax, target-mask and masked-target for backward pass. exp_logits.div_(sum_exp_logits.unsqueeze(dim=-1)) ctx.save_for_backward(exp_logits, target_mask, masked_target_1d) return loss @staticmethod @custom_bwd def backward(ctx, grad_output): # Retreive tensors from the forward path. softmax, target_mask, masked_target_1d = ctx.saved_tensors # All the inputs have softmax as thier gradient. grad_input = softmax # For simplicity, work with the 2D gradient. partition_vocab_size = softmax.size()[-1] grad_2d = grad_input.view(-1, partition_vocab_size) # Add the gradient from matching classes. arange_1d = torch.arange(start=0, end=grad_2d.size()[0], device=grad_2d.device) grad_2d[arange_1d, masked_target_1d] -= (1.0 - target_mask.view(-1).float()) # Finally elementwise multiplication with the output gradients. grad_input.mul_(grad_output.unsqueeze(dim=-1)) return grad_input, None, None @LOSSES.register_module class VocabParallelCrossEntropyLoss1D(_Loss): """Vocab parallel cross entropy loss for 1D parallelism. Args: reduction (bool, optional): whether to average the loss, defaults to True. """ def __init__(self, reduction=True): super().__init__() self.reduction_mean = reduction def forward(self, logits, targets, process_group=None): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ loss = _VocabParallelCrossEntropy1D.apply(logits, targets, process_group) if self.reduction_mean: loss = loss.mean() return loss
import torch import torch.distributed as dist from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.nn.layer.parallel_2d import reduce_by_batch_2d, split_batch_2d from colossalai.nn.layer.parallel_2d._utils import assert_summa_initialization from colossalai.registry import LOSSES from colossalai.utils import get_current_device from torch.cuda.amp import custom_bwd, custom_fwd from torch.nn.functional import cross_entropy from torch.nn.modules.loss import _Loss @LOSSES.register_module class CrossEntropyLoss2D(_Loss): r"""Cross entropy loss for 2D parallelism Args: reduction (bool, optional): whether to average the loss, defaults to True. The ``args`` and ``kwargs`` should include parameters below: :: weight (Tensor, optional) size_average (bool, optional) ignore_index (int, optional) reduce (bool, optional) label_smoothing (float, optional) More details about ``args``, ``kwargs`` and ``torch.nn.functional.cross_entropy`` could be found in `Cross_entropy <https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html#torch.nn.functional.cross_entropy>`_. """ def __init__(self, reduction=True, *args, **kwargs): super().__init__() assert_summa_initialization() self.reduction_mean = reduction self.loss_args = args self.loss_kwargs = kwargs def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. Returns: float: the loss between logits and targets. """ targets = split_batch_2d(targets) loss = cross_entropy(logits, targets, reduction='none', *self.loss_args, **self.loss_kwargs) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_2d(loss, True) return loss class _VocabParallelCrossEntropy2D(torch.autograd.Function): ### Modified based on megatron.mpu.cross_entropy ### @staticmethod @custom_fwd(cast_inputs=torch.float32) def forward(ctx, logits, targets): # logits: [b/q, h/q] # labels: [b/q] # loss: [b/q] # vocab_parallel_logits: [b/q, s, v/q] # target: [b/q, s] logits_max = torch.max(logits, dim=-1)[0] torch.distributed.all_reduce(logits_max, op=torch.distributed.ReduceOp.MAX, group=gpc.get_group(ParallelMode.PARALLEL_2D_ROW)) # Subtract the maximum value. # vocab_parallel_logits.sub_(logits_max.unsqueeze(dim=-1)) logits = logits - logits_max.unsqueeze(dim=-1) vocab_size = logits.size(-1) rank = gpc.get_local_rank(ParallelMode.PARALLEL_2D_ROW) vocab_start = rank * (vocab_size) vocab_end = (rank + 1) * (vocab_size) - 1 target_mask = (targets < vocab_start) | (targets > vocab_end) masked_target = targets.clone() - vocab_start masked_target[target_mask] = 0 arange_1d = torch.arange( start=0, end=logits.size()[0], ) predicted_logits = logits[arange_1d, masked_target] predicted_logits[target_mask] = 0. dist.all_reduce(predicted_logits, group=gpc.get_group(ParallelMode.PARALLEL_2D_ROW)) exp_logits = torch.exp(logits) sum_exp_logits = exp_logits.sum(dim=1) dist.all_reduce(sum_exp_logits, group=gpc.get_group(ParallelMode.PARALLEL_2D_ROW)) loss = torch.log(sum_exp_logits) - predicted_logits exp_logits.div_(sum_exp_logits.unsqueeze(dim=-1)) ctx.save_for_backward(exp_logits, target_mask, masked_target) return loss @staticmethod @custom_bwd def backward(ctx, output_grad): # Retreive tensors from the forward path. softmax, target_mask, masked_target = ctx.saved_tensors # All the inputs have softmax as their gradient. grad_input = softmax # For simplicity, work with the 2D gradient. partition_vocab_size = softmax.size()[-1] grad_2d = grad_input.view(-1, partition_vocab_size) # Add the gradient from matching classes. arange_1d = torch.arange(start=0, end=grad_2d.size()[0], device=get_current_device()) grad_2d[arange_1d, masked_target] -= (1.0 - target_mask.view(-1).float()) # Finally elementwise multiplication with the output gradients. grad_input.mul_(output_grad.unsqueeze(dim=-1)) return grad_input, None @LOSSES.register_module class VocabParallelCrossEntropyLoss2D(_Loss): """Vocab parallel cross entropy loss for 2D parallelism. Args: reduction (bool, optional): whether to average the loss, defaults to True. """ def __init__(self, reduction=True): super().__init__() self.reduction_mean = reduction def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ targets = split_batch_2d(targets) loss = _VocabParallelCrossEntropy2D.apply( logits, targets, ) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_2d(loss, True) return loss
import torch import torch.distributed as dist from colossalai.constants import INPUT_GROUP_3D, WEIGHT_GROUP_3D, OUTPUT_GROUP_3D from colossalai.core import global_context as gpc from colossalai.nn.layer.parallel_3d import reduce_by_batch_3d, split_tensor_3d from colossalai.nn.layer.parallel_3d._utils import get_parallel_mode_from_env from colossalai.registry import LOSSES from colossalai.utils import get_current_device from torch.cuda.amp import custom_bwd, custom_fwd from torch.nn.functional import cross_entropy from torch.nn.modules.loss import _Loss @LOSSES.register_module class CrossEntropyLoss3D(_Loss): r"""Cross entropy loss for 3D parallelism. Args: reduction (bool, optional): whether to average the loss, defaults to True. The ``args`` and ``kwargs`` should include parameters below: :: weight (Tensor, optional) size_average (bool, optional) ignore_index (int, optional) reduce (bool, optional) label_smoothing (float, optional) More details about ``args``, ``kwargs`` and ``torch.nn.functional.cross_entropy`` could be found in `Cross_entropy <https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html#torch.nn.functional.cross_entropy>`_. """ def __init__(self, reduction=True, *args, **kwargs): super().__init__() self.input_parallel_mode = get_parallel_mode_from_env(INPUT_GROUP_3D) self.weight_parallel_mode = get_parallel_mode_from_env(WEIGHT_GROUP_3D) self.reduction_mean = reduction self.loss_args = args self.loss_kwargs = kwargs def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ targets = split_tensor_3d(targets, 0, self.weight_parallel_mode) targets = split_tensor_3d(targets, 0, self.input_parallel_mode) loss = cross_entropy(logits, targets, reduction='none', *self.loss_args, **self.loss_kwargs) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_3d(loss, self.input_parallel_mode, self.weight_parallel_mode, True) return loss class _VocabParallelCrossEntropy3D(torch.autograd.Function): # Adapted from megatron.mpu.cross_entropy # loss[i] = -logits[i][targets] + log(sum(exp(logits[i]))) @staticmethod @custom_fwd(cast_inputs=torch.float32) def forward(ctx, logits, targets, output_parallel_mode): # logits: [b/q^2, c/q] # labels: [b/q^2] # loss: [b/q^2] logits_max = torch.max(logits, dim=-1)[0] dist.all_reduce(logits_max, op=torch.distributed.ReduceOp.MAX, group=gpc.get_group(output_parallel_mode)) # Subtract the maximum value. logits = logits - logits_max.unsqueeze(dim=-1) vocab_size_per_partition = logits.size()[-1] rank = gpc.get_local_rank(output_parallel_mode) vocab_start = rank * vocab_size_per_partition vocab_end = (rank + 1) * vocab_size_per_partition - 1 # loss[i] = 0 if targets[i] < vocab_start or targets[i] > vocab_end target_mask = (targets < vocab_start) | (targets > vocab_end) masked_target = targets.clone() - vocab_start masked_target[target_mask] = 0 arange_1d = torch.arange(start=0, end=logits.size()[0], device=get_current_device()) predicted_logits = logits[arange_1d, masked_target] predicted_logits = predicted_logits.clone().contiguous().view_as(targets) predicted_logits[target_mask] = 0. dist.all_reduce(predicted_logits, group=gpc.get_group(output_parallel_mode)) # Loss = log(sum(exp(logits))) - predicted-logit. exp_logits = torch.exp(logits) sum_exp_logits = exp_logits.sum(dim=-1) dist.all_reduce(sum_exp_logits, group=gpc.get_group(output_parallel_mode)) loss = torch.log(sum_exp_logits) - predicted_logits exp_logits.div_(sum_exp_logits.unsqueeze(dim=-1)) ctx.save_for_backward(exp_logits, target_mask, masked_target) return loss @staticmethod @custom_bwd def backward(ctx, output_grad): # Retreive tensors from the forward path. softmax, target_mask, masked_target = ctx.saved_tensors # All the inputs have softmax as thier gradient. input_grad = softmax # For simplicity, work with the 2D gradient. partition_vocab_size = softmax.size()[-1] grad_2d = input_grad.view(-1, partition_vocab_size) # Add the gradient from matching classes. arange_1d = torch.arange(start=0, end=grad_2d.size()[0], device=get_current_device()) grad_2d[arange_1d, masked_target] -= (1.0 - target_mask.view(-1).float()) input_grad.mul_(output_grad.unsqueeze(dim=-1)) return input_grad, None, None, None @LOSSES.register_module class VocabParallelCrossEntropyLoss3D(_Loss): """Vocab parallel cross entropy loss for 2D parallelism. Args: reduction (bool, optional): whether to average the loss, defaults to True. """ def __init__(self, reduction=True): super().__init__() self.input_parallel_mode = get_parallel_mode_from_env(INPUT_GROUP_3D) self.weight_parallel_mode = get_parallel_mode_from_env(WEIGHT_GROUP_3D) self.output_parallel_mode = get_parallel_mode_from_env(OUTPUT_GROUP_3D) self.reduction_mean = reduction def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ targets = split_tensor_3d(targets, 0, self.weight_parallel_mode) targets = split_tensor_3d(targets, 0, self.input_parallel_mode) loss = _VocabParallelCrossEntropy3D.apply(logits, targets, self.output_parallel_mode) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_3d(loss, self.input_parallel_mode, self.weight_parallel_mode, True) return loss
import torch import torch.distributed as dist from colossalai.context import ParallelMode from colossalai.core import global_context as gpc from colossalai.nn.layer.parallel_2p5d import reduce_by_batch_2p5d, split_batch_2p5d from colossalai.nn.layer.parallel_2p5d._utils import assert_tesseract_initialization from colossalai.registry import LOSSES from colossalai.utils import get_current_device from torch.cuda.amp import custom_bwd, custom_fwd from torch.nn.functional import cross_entropy from torch.nn.modules.loss import _Loss @LOSSES.register_module class CrossEntropyLoss2p5D(_Loss): r"""Cross entropy loss for 2.5D parallelism Args: reduction (bool, optional): whether to average the loss, defaults to True. The ``args`` and ``kwargs`` should include parameters below: :: weight (Tensor, optional) size_average (bool, optional) ignore_index (int, optional) reduce (bool, optional) label_smoothing (float, optional) More details about ``args``, ``kwargs`` and ``torch.nn.functional.cross_entropy`` could be found in `Cross_entropy <https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html#torch.nn.functional.cross_entropy>`_. """ def __init__(self, reduction=True, *args, **kwargs): super().__init__() assert_tesseract_initialization() self.reduction_mean = reduction self.loss_args = args self.loss_kwargs = kwargs def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ targets = split_batch_2p5d(targets) loss = cross_entropy(logits, targets, reduction='none', *self.loss_args, **self.loss_kwargs) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_2p5d(loss, True) return loss class _VocabParallelCrossEntropy2p5D(torch.autograd.Function): ### Modified based on megatron.mpu.cross_entropy ### @staticmethod @custom_fwd(cast_inputs=torch.float32) def forward(ctx, logits, targets): # logits: [b/dq, h/q] # loss: [b/dq] # targets: [b/dq, h/q] logits_max = torch.max(logits, dim=-1)[0] torch.distributed.all_reduce(logits_max, op=torch.distributed.ReduceOp.MAX, group=gpc.get_group(ParallelMode.PARALLEL_2P5D_ROW)) # Subtract the maximum value. logits = logits - logits_max.unsqueeze(dim=-1) vocab_size = logits.size(-1) rank = gpc.get_local_rank(ParallelMode.PARALLEL_2P5D_ROW) vocab_start = rank * (vocab_size) vocab_end = (rank + 1) * (vocab_size) - 1 target_mask = (targets < vocab_start) | (targets > vocab_end) masked_target = targets.clone() - vocab_start masked_target[target_mask] = 0 arange_1d = torch.arange( start=0, end=logits.size()[0], ) predicted_logits = logits[arange_1d, masked_target] predicted_logits[target_mask] = 0. dist.all_reduce(predicted_logits, group=gpc.get_group(ParallelMode.PARALLEL_2P5D_ROW)) exp_logits = torch.exp(logits) sum_exp_logits = exp_logits.sum(dim=1) dist.all_reduce(sum_exp_logits, group=gpc.get_group(ParallelMode.PARALLEL_2P5D_ROW)) loss = torch.log(sum_exp_logits) - predicted_logits exp_logits.div_(sum_exp_logits.unsqueeze(dim=-1)) ctx.save_for_backward(exp_logits, target_mask, masked_target) return loss @staticmethod @custom_bwd def backward(ctx, output_grad): # Retreive tensors from the forward path. softmax, target_mask, masked_target = ctx.saved_tensors # All the inputs have softmax as their gradient. grad_input = softmax # For simplicity, work with the 2D gradient. partition_vocab_size = softmax.size()[-1] grad_2d = grad_input.view(-1, partition_vocab_size) # Add the gradient from matching classes. arange_1d = torch.arange(start=0, end=grad_2d.size()[0], device=get_current_device()) grad_2d[arange_1d, masked_target] -= (1.0 - target_mask.view(-1).float()) # Finally elementwise multiplication with the output gradients. grad_input.mul_(output_grad.unsqueeze(dim=-1)) return grad_input, None @LOSSES.register_module class VocabParallelCrossEntropyLoss2p5D(_Loss): """ Vocab parallel cross entropy loss for 2.5D parallelism Args: reduction (bool, optional): whether to average the loss, defaults to True. """ def __init__(self, reduction=True): super().__init__() self.reduction_mean = reduction def forward(self, logits, targets): """Calculate loss between logits and targets. Args: logits (:class:`torch.tensor`): Predicted unnormalized scores (often referred to as logits). targets (:class:`torch.tensor`): Ground truth class indices or class probabilities. """ targets = split_batch_2p5d(targets) loss = _VocabParallelCrossEntropy2p5D.apply(logits, targets) if self.reduction_mean: loss = loss.mean() loss = reduce_by_batch_2p5d(loss, True) return loss