python_code
stringlengths
0
456k
import torch import torch.nn.functional as F from ...registry import bias_addition_module from .bias_addition_module import BiasAdditionModule @bias_addition_module.register(torch.nn.Linear) class BiasAdditionLinear(BiasAdditionModule): def extract_kwargs_from_mod(self): return {} def generate(self): non_bias_linear_func_proxy = self.create_non_bias_func_proxy() bias_addition_proxy = self.create_bias_addition_proxy(non_bias_linear_func_proxy, self.bias_proxy) return bias_addition_proxy
from .bias_addition_module import * from .conv import * from .linear import *
import torch import torch.nn.functional as F from torch.nn.modules.utils import _pair, _reverse_repeat_tuple, _single, _triple from ...registry import bias_addition_module from .bias_addition_module import BiasAdditionModule @bias_addition_module.register(torch.nn.Conv1d) @bias_addition_module.register(torch.nn.Conv2d) @bias_addition_module.register(torch.nn.Conv3d) class BiasAdditionConv(BiasAdditionModule): def extract_kwargs_from_mod(self): root = self.tracer.root conv_module = root.get_submodule(self.target) kwarg_attributes = ['groups', 'dilation', 'stride'] non_bias_kwargs = {} for attr_name in kwarg_attributes: if hasattr(conv_module, attr_name): non_bias_kwargs[attr_name] = getattr(conv_module, attr_name) if conv_module.padding_mode != "zeros": #TODO: non zeros mode requires some extra processing for input conv_type = type(conv_module) if conv_type == "torch.nn.Conv1d": padding_element = _single(0) elif conv_type == "torch.nn.Conv2d": padding_element = _pair(0) elif conv_type == "torch.nn.Conv3d": padding_element = _triple(0) non_bias_kwargs['padding'] = padding_element else: non_bias_kwargs['padding'] = getattr(conv_module, 'padding') return non_bias_kwargs def create_bias_reshape_proxy(self, dimensions): """ This method is used to reshape the bias node in order to make bias and output of non-bias convolution broadcastable. """ bias_shape = [1] * (dimensions - 1) bias_shape[0] = -1 bias_reshape_node_kind = 'call_method' bias_reshape_node_target = 'view' bias_reshape_node_args = (self.bias_proxy, torch.Size(bias_shape)) bias_reshape_proxy = self.tracer.create_proxy(bias_reshape_node_kind, bias_reshape_node_target, bias_reshape_node_args, {}) return bias_reshape_proxy def generate(self): non_bias_conv_func_proxy = self.create_non_bias_func_proxy() output_dims = non_bias_conv_func_proxy.meta_data.dim() bias_reshape_proxy = self.create_bias_reshape_proxy(output_dims) bias_addition_proxy = self.create_bias_addition_proxy(non_bias_conv_func_proxy, bias_reshape_proxy) return bias_addition_proxy
import torch from torch.fx.graph_module import GraphModule from typing import Callable, List, Dict, Any, Optional from torch.fx._compatibility import compatibility from packaging import version import inspect @compatibility(is_backward_compatible=True) class Partition: """ Adapted from https://github.com/pytorch/pytorch/blob/master/torch/fx/passes/split_module.py """ def __init__(self, name: str): self.name: str = name self.node_names: List[str] = [] self.inputs: Dict[str, None] = {} self.outputs: Dict[str, None] = {} self.partitions_dependent_on: Dict[str, None] = {} self.partition_dependents: Dict[str, None] = {} self.graph: torch.fx.graph.Graph = torch.fx.graph.Graph() self.environment: Dict[torch.fx.node.Node, torch.fx.node.Node] = {} self.targets: Dict[str, Any] = {} def __repr__(self) -> str: return f"name: {self.name},\n" \ f" nodes: {self.node_names},\n" \ f" inputs: {self.inputs},\n" \ f" outputs: {self.outputs},\n" \ f" partitions depenent on: {self.partitions_dependent_on},\n" \ f" parition dependents: {self.partition_dependents}" # Creates subgraphs out of main graph @compatibility(is_backward_compatible=True) def split_module( m: GraphModule, root_m: torch.nn.Module, split_callback: Callable[[torch.fx.node.Node], int], merge_output = False, ): """ Adapted from https://github.com/pytorch/pytorch/blob/master/torch/fx/passes/split_module.py Creates subgraphs out of main graph Args: m (GraphModule): Graph module to split root_m (torch.nn.Module): root nn module. Not currently used. Included because the root nn module is usually transformed via torch.fx._symbolic_trace.symbolic_trace (see example below) split_callback (Callable[[torch.fx.node.Node], int]): Callable function that maps a given Node instance to a numeric partition identifier. split_module will use this function as the policy for which operations appear in which partitions in the output Module. Returns: GraphModule: the module after split. Example: This is a sample setup: import torch from torch.fx.symbolic_trace import symbolic_trace from torch.fx.graph_module import GraphModule from torch.fx.node import Node from colossalai.fx.passes.split_module import split_module class MyModule(torch.nn.Module): def __init__(self): super().__init__() self.param = torch.nn.Parameter(torch.rand(3, 4)) self.linear = torch.nn.Linear(4, 5) def forward(self, x, y): z = self.linear(x + self.param).clamp(min=0.0, max=1.0) w = self.linear(y).clamp(min=0.0, max=1.0) return z + w # symbolically trace model my_module = MyModule() my_module_traced = symbolic_trace(my_module) # random mod partitioning partition_counter = 0 NPARTITIONS = 3 def mod_partition(node: Node): global partition_counter partition = partition_counter % NPARTITIONS partition_counter = (partition_counter + 1) % NPARTITIONS return partition # split module in module with submodules module_with_submodules = split_module( my_module_traced, my_module, mod_partition ) Output looks like this. Original graph is broken into partitions > print(module_with_submodules) GraphModule( (submod_0): GraphModule( (linear): Linear(in_features=4, out_features=5, bias=True) ) (submod_1): GraphModule( (linear): Linear(in_features=4, out_features=5, bias=True) ) (submod_2): GraphModule() ) def forward(self, x, y): param = self.param submod_0 = self.submod_0(x, param, y); x = param = y = None getitem = submod_0[0] getitem_1 = submod_0[1]; submod_0 = None submod_1 = self.submod_1(getitem, getitem_1); getitem = getitem_1 = None getitem_2 = submod_1[0] getitem_3 = submod_1[1]; submod_1 = None submod_2 = self.submod_2(getitem_2, getitem_3); getitem_2 = getitem_3 = None return submod_2 Output of split module is the same as output of input traced module. This is an example within a test setting: > orig_out = my_module_traced(x, y) > submodules_out = module_with_submodules(x, y) > self.assertEqual(orig_out, submodules_out) True """ partitions: Dict[str, Partition] = {} orig_nodes: Dict[str, torch.fx.node.Node] = {} def record_cross_partition_use(def_node: torch.fx.node.Node, use_node: Optional[torch.fx.node.Node]): # noqa: B950 def_partition_name = getattr(def_node, '_fx_partition', None) use_partition_name = getattr(use_node, '_fx_partition', None) if def_partition_name != use_partition_name: if def_partition_name is not None: def_partition = partitions[def_partition_name] def_partition.outputs.setdefault(def_node.name) if use_partition_name is not None: def_partition.partition_dependents.setdefault(use_partition_name) if use_partition_name is not None: use_partition = partitions[use_partition_name] use_partition.inputs.setdefault(def_node.name) if def_partition_name is not None: use_partition.partitions_dependent_on.setdefault(def_partition_name) def record_output( def_node: torch.fx.node.Node, use_node: Optional[torch.fx.node.Node] ): # noqa: B950 def_partition_name = getattr(def_node, "_fx_partition", None) use_partition_name = getattr(use_node, "_fx_partition", None) if def_partition_name != use_partition_name: if def_partition_name is not None: def_partition = partitions[def_partition_name] def_partition.outputs.setdefault(def_node.name) if use_partition_name is not None: def_partition.partition_dependents.setdefault(use_partition_name) if use_partition_name is not None: use_partition = partitions[use_partition_name] use_partition.inputs.setdefault(def_node.name) if def_partition_name is not None: use_partition.partitions_dependent_on.setdefault(def_partition_name) use_partition.outputs.setdefault(def_node.name) else: if use_partition_name is not None: use_partition = partitions[use_partition_name] use_partition.outputs.setdefault(def_node.name) # split nodes into parititons for node in m.graph.nodes: orig_nodes[node.name] = node if node.op in ["placeholder"]: continue if node.op == 'output': if merge_output: torch.fx.graph.map_arg(node.args[0], lambda n: record_output(n, node.prev)) else: torch.fx.graph.map_arg(node.args[0], lambda n: record_cross_partition_use(n, None)) continue partition_name = str(split_callback(node)) # add node to partitions partition = partitions.get(partition_name) if partition is None: partitions[partition_name] = partition = Partition(partition_name) partition.node_names.append(node.name) node._fx_partition = partition_name torch.fx.graph.map_arg(node.args, lambda def_node: record_cross_partition_use(def_node, node)) torch.fx.graph.map_arg(node.kwargs, lambda def_node: record_cross_partition_use(def_node, node)) # noqa: B950 # find partitions with no dependencies root_partitions: List[str] = [] for partition_name, partition in partitions.items(): if not len(partition.partitions_dependent_on): root_partitions.append(partition_name) # check partitions for circular dependencies and create topological partition ordering sorted_partitions: List[str] = [] while root_partitions: root_partition = root_partitions.pop() sorted_partitions.append(root_partition) for dependent in partitions[root_partition].partition_dependents: partitions[dependent].partitions_dependent_on.pop(root_partition) if not partitions[dependent].partitions_dependent_on: root_partitions.append(dependent) if len(sorted_partitions) != len(partitions): raise RuntimeError("cycle exists between partitions!") # add placeholders to parititons for partition_name in sorted_partitions: partition = partitions[partition_name] for input in partition.inputs: placeholder = partition.graph.placeholder(input) placeholder.meta = orig_nodes[input].meta.copy() partition.environment[orig_nodes[input]] = placeholder # Transform nodes and collect targets for partition's submodule for node in m.graph.nodes: if hasattr(node, '_fx_partition'): partition = partitions[node._fx_partition] # swap out old graph nodes in kw/args with references to new nodes in this submodule environment = partition.environment gathered_args = torch.fx.graph.map_arg(node.args, lambda n: environment[n]) gathered_kwargs = torch.fx.graph.map_arg(node.kwargs, lambda n: environment[n]) if node.op not in ['call_module', 'get_attr']: target = node.target else: target_atoms = node.target.split('.') target_attr = m for atom in target_atoms: if not hasattr(target_attr, atom): raise RuntimeError(f'Operator target {node.target} not found!') target_attr = getattr(target_attr, atom) # target = target_atoms[-1] target = '_'.join(target_atoms) partition.targets[target] = target_attr assert isinstance(gathered_args, tuple) assert isinstance(gathered_kwargs, dict) new_node = partition.graph.create_node(op=node.op, target=target, args=gathered_args, kwargs=gathered_kwargs) new_node.meta = node.meta.copy() partition.environment[node] = new_node # Set up values to construct base module base_mod_env: Dict[str, torch.fx.node.Node] = {} base_mod_graph: torch.fx.graph.Graph = torch.fx.graph.Graph() base_mod_attrs: Dict[str, torch.fx.graph_module.GraphModule] = {} for node in m.graph.nodes: if node.op == 'placeholder': if version.parse(torch.__version__) < version.parse('1.11.0'): base_mod_env[node.name] = base_mod_graph.placeholder(node.target, type_expr=node.type) else: default_value = node.args[0] if len(node.args) > 0 else inspect.Signature.empty base_mod_env[node.name] = base_mod_graph.placeholder(node.target, type_expr=node.type, default_value=default_value) base_mod_env[node.name].meta = node.meta.copy() # Do some things iterating over the partitions in topological order again: # 1) Finish off submodule Graphs by setting corresponding outputs # 2) Construct GraphModules for each submodule # 3) Construct the base graph by emitting calls to those submodules in # topological order for partition_name in sorted_partitions: partition = partitions[partition_name] # Set correct output values output_vals = tuple(partition.environment[orig_nodes[name]] for name in partition.outputs) output_vals = output_vals[0] if len(output_vals) == 1 else output_vals # type: ignore[assignment] partition.graph.output(output_vals) # Construct GraphModule for this partition submod_name = f'submod_{partition_name}' base_mod_attrs[submod_name] = torch.fx.graph_module.GraphModule(partition.targets, partition.graph) # noqa: B950 # Emit call in base graph to this submodule output_val = base_mod_graph.call_module(submod_name, tuple(base_mod_env[name] for name in partition.inputs)) if len(partition.outputs) > 1: # Unpack multiple return values from submodule output_val_proxy = torch.fx.proxy.Proxy(output_val) for i, output_name in enumerate(partition.outputs): base_mod_env[output_name] = output_val_proxy[i].node # type: ignore[index] else: if not partition.outputs: continue base_mod_env[list(partition.outputs)[0]] = output_val for node in m.graph.nodes: if node.op == 'output': base_mod_graph.output(torch.fx.graph.map_arg(node.args[0], lambda n: base_mod_env[n.name])) # noqa: B950 for partition_name in sorted_partitions: partition = partitions[partition_name] new_gm = torch.fx.graph_module.GraphModule(base_mod_attrs, base_mod_graph) return new_gm
from dataclasses import asdict from typing import Any, Dict, List, NamedTuple, Tuple import torch import torch.fx from torch.fx.node import Argument, Node, Target from torch.utils._pytree import tree_map from colossalai.fx._compatibility import compatibility, is_compatible_with_meta from colossalai.fx.profiler import ( GraphInfo, activation_size, calculate_fwd_in, calculate_fwd_out, calculate_fwd_tmp, profile_function, profile_method, profile_module, ) @compatibility(is_backward_compatible=True) class TensorMetadata(NamedTuple): # TensorMetadata is a structure containing pertinent information # about a tensor within a PyTorch program. shape: torch.Size dtype: torch.dtype requires_grad: bool stride: Tuple[int] numel: int is_tensor: bool # TODO: we can add a list of sharding spec here, and record the sharding # behaviour by appending sharding spec into list. def _extract_tensor_metadata(result: torch.Tensor) -> TensorMetadata: """ Extract a TensorMetadata NamedTuple describing `result`. """ shape = result.shape dtype = result.dtype requires_grad = result.requires_grad stride = result.stride() numel = result.numel() is_tensor = True return TensorMetadata(shape, dtype, requires_grad, stride, numel, is_tensor) @compatibility(is_backward_compatible=True) class MetaInfoProp(torch.fx.Interpreter): """ Execute an FX graph Node-by-Node with meta tensor and record the memory usage, FLOPs, and type of the result into the corresponding node. Usage: BATCH_SIZE = 2 DIM_IN = 4 DIM_HIDDEN = 16 DIM_OUT = 16 model = torch.nn.Sequential( torch.nn.Linear(DIM_IN, DIM_HIDDEN), torch.nn.Linear(DIM_HIDDEN, DIM_OUT), ) input_sample = torch.rand(BATCH_SIZE, DIM_IN) gm = symbolic_trace(model) interp = MetaInfoProp(gm) interp.run(input_sample) print(interp.summary(format='kb')) # don't panic if some statistics are 0.00 MB # output of above code is Op type Op Forward FLOPs Backward FLOPs FWD_OUT FWD_TMP BWD_OUT BWD_TMP ----------- ------- --------------- ---------------- --------- --------- --------- --------- placeholder input_1 0 FLOPs 0 FLOPs 0.00 KB 0.00 KB 0.00 KB 0.00 KB call_module _0 128 FLOPs 288 FLOPs 0.12 KB 0.00 KB 0.34 KB 0.00 KB call_module _1 512 FLOPs 1,056 FLOPs 0.12 KB 0.00 KB 1.19 KB 0.00 KB output output 0 FLOPs 0 FLOPs 0.00 KB 0.00 KB 0.00 KB 0.00 KB Args: module (GraphModule): The module to be executed """ _is_proped: bool = False @compatibility(is_backward_compatible=True) def run_node(self, n: Node) -> Any: """ Run a specific node ``n`` and return the result. Calls into placeholder, get_attr, call_function, call_method, call_module, or output depending on ``node.op`` Args: n (Node): The Node to execute Returns: Any: The result of executing ``n`` """ self._is_proped = True result, meta_info = super().run_node(n) def extract_tensor_meta(obj): if isinstance(obj, torch.Tensor): return _extract_tensor_metadata(obj) else: return TensorMetadata(None, None, False, None, 0, False) tensor_meta = tree_map(extract_tensor_meta, result) n.meta['tensor_meta'] = tensor_meta n.meta = {**n.meta, **asdict(meta_info)} # extend MetaInfo to `n.meta` # TODO: the attribute node_size should be removed in the future setattr(n, 'node_size', activation_size(n.meta.get('fwd_out', 0)) + activation_size(n.meta.get('fwd_tmp', 0))) setattr(n, 'fwd_flop', n.meta.get('fwd_flop', 0)) n.meta['type'] = type(result) # retain the autograd graph for param in self.module.parameters(): param.grad = None return result # Main Node running APIs @compatibility(is_backward_compatible=True) def placeholder(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``placeholder`` node. Note that this is stateful: ``Interpreter`` maintains an internal iterator over arguments passed to ``run`` and this method returns next() on that iterator. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Returns: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ return super().placeholder(target, args, kwargs), GraphInfo() @compatibility(is_backward_compatible=True) def get_attr(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``get_attr`` node. Will retrieve an attribute value from the ``Module`` hierarchy of ``self.module``. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ return super().get_attr(target, args, kwargs), GraphInfo() @compatibility(is_backward_compatible=True) def call_function(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_function`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ assert not isinstance(target, str) return profile_function(target)(*args, **kwargs) @compatibility(is_backward_compatible=True) def call_method(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_method`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ return profile_method(target)(*args, **kwargs) @compatibility(is_backward_compatible=True) def call_module(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_module`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ # Retrieve executed args and kwargs values from the environment # Execute the method and return the result assert isinstance(target, str) submod = self.fetch_attr(target) return profile_module(submod)(*args, **kwargs) @compatibility(is_backward_compatible=True) def output(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute an ``output`` node. This really just retrieves the value referenced by the ``output`` node and returns it. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ if hasattr(args[0], '_tensor'): return args[0], GraphInfo(fwd_in=[args[0]._tensor]) return args[0], GraphInfo(save_fwd_in=True) def propagate(self, *args): """ Run `module` via interpretation and return the result and record the shape and type of each node. Args: *args (Tensor): the sample input. Returns: Any: The value returned from executing the Module """ return super().run(*args) def summary(self, unit: str = 'MB') -> str: """ Summarizes the memory and FLOPs statistics of the `GraphModule` in tabular format. Note that this API requires the ``tabulate`` module to be installed. """ # https://github.com/pytorch/pytorch/blob/master/torch/fx/graph.py try: from tabulate import tabulate except ImportError: print("`summary` relies on the library `tabulate`, " "which could not be found on this machine. Run `pip " "install tabulate` to install the library.") assert self._is_proped, "Please call `interp.run(input)` before calling `interp.summary()`." # Build up a list of summary information for each node node_summaries: List[List[Any]] = [] def mem_repr(mem: int) -> str: unit_divisor_map = { 'kb': 1024, 'mb': 1024**2, 'gb': 1024**3, 'tb': 1024**4, } return f"{mem / unit_divisor_map[unit.lower()]:.2f} {unit.upper()}" def flops_repr(flop: int) -> str: return f"{flop:,} FLOPs" for node in self.module.graph.nodes: node: Node node_summaries.append([ node.op, str(node), flops_repr(node.meta['fwd_flop']), flops_repr(node.meta['bwd_flop']), mem_repr(calculate_fwd_in(node)), mem_repr(calculate_fwd_out(node)), mem_repr(calculate_fwd_tmp(node)), mem_repr(node.meta['bwd_mem_out']), mem_repr(node.meta['bwd_mem_tmp']), ]) # Use the ``tabulate`` library to create a well-formatted table # presenting our summary information headers: List[str] = [ 'Op type', 'Op', 'Forward FLOPs', 'Backward FLOPs', 'FWD_IN', 'FWD_OUT', 'FWD_TMP', 'BWD_OUT', 'BWD_TMP', ] return tabulate(node_summaries, headers=headers, stralign='right') def metainfo_trace(gm: torch.fx.GraphModule, *args, verbose: bool = False, unit: str = "MB", **kwargs) -> None: """ MetaInfo tracing API Given a ``GraphModule`` and a sample input, this API will trace the MetaInfo of a single training cycle, and annotate them on ``gm.graph``. Uses: >>> model = ... >>> gm = symbolic_trace(model) >>> args = ... # sample input to the ``GraphModule`` >>> metainfo_trace(gm, *args) Args: gm (torch.fx.GraphModule): The ``GraphModule`` to be annotated with MetaInfo. verbose (bool, optional): Whether to show ``MetaInfoProp.summary()`. Defaults to False. unit (str, optional): The unit of memory. Defaults to "MB". Returns: torch.fx.GraphModule: The ``GraphModule`` annotated with MetaInfo. """ device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu') interp = MetaInfoProp(gm.to(device)) if is_compatible_with_meta(): from colossalai.fx.profiler import MetaTensor args = tree_map(lambda x: MetaTensor(x, fake_device=device), args) kwargs = tree_map(lambda x: MetaTensor(x, fake_device=device), kwargs) interp.propagate(*args, **kwargs) if verbose: interp.summary(unit) gm.to('cpu') del interp return gm
from .adding_split_node_pass import balanced_split_pass, split_with_split_nodes_pass from .concrete_info_prop import ConcreteInfoProp from .meta_info_prop import MetaInfoProp, metainfo_trace from .shard_1d_pass import column_shard_linear_pass, row_shard_linear_pass
import torch from typing import Dict from torch.fx.node import Node, map_arg from torch.fx.graph import Graph def get_comm_size(prev_partition, next_partition): """ Given two partitions (parent and child), calculate the communication size between the two. """ # Keep tracking the communication size between parent and child comm_size = 0 # Keep tracking all the counted node visited_nodes = set() # Go through all nodes in the child partition # If a node has input nodes from the parent partition, # the output size of those input nodes will be counted # and added to comm_size parent_node_names = [n.name for n in prev_partition.graph.nodes] for node in next_partition.graph.nodes: input_nodes: Dict[Node, None] = {} map_arg(node.args, lambda n: input_nodes.setdefault(n)) map_arg(node.kwargs, lambda n: input_nodes.setdefault(n)) for n in input_nodes: if n.name in parent_node_names and n not in visited_nodes: comm_size += n.meta['tensor_meta'].numel visited_nodes.add(n) return comm_size def get_leaf(graph: Graph): """ Given a graph, return leaf nodes of this graph. Note: If we remove ``root`` nodes, ``placeholder`` nodes, and ``output`` nodes from fx graph, we will get a normal DAG. Leaf nodes in this context means leaf nodes in that DAG. """ input_nodes: Dict[Node, None] = {} for node in graph.nodes: if node.op == 'output': map_arg(node.args, lambda n: input_nodes.setdefault(n)) map_arg(node.kwargs, lambda n: input_nodes.setdefault(n)) placeholder_nodes = [] for node in input_nodes.keys(): if node.op == 'placeholder': placeholder_nodes.append(node) for node in placeholder_nodes: input_nodes.pop(node) return list(input_nodes.keys()) def is_leaf(graph: Graph, node: Node): return node in get_leaf(graph) def get_top(graph: Graph): """ Given a graph, return top nodes of this graph. Note: If we remove ``root`` nodes, ``placeholder`` nodes, and ``output`` nodes from fx graph, we will get a normal DAG. Top nodes in this context means nodes with BFS level 0 in that DAG. """ top_node_list = set() for node in graph.nodes: if node.op == 'output': continue is_top = False def _get_top(node): nonlocal is_top if node.op == 'placeholder': is_top = True map_arg(node.args, lambda n: _get_top(n)) map_arg(node.kwargs, lambda n: _get_top(n)) if is_top: top_node_list.add(node) return list(top_node_list) def is_top(graph: Graph, node: Node): return node in get_top(graph) def get_all_consumers(graph: Graph, node: Node): """ Given a graph and a node of this graph, return all consumers of the node. Returns: List of ``Nodes`` that node appear in these nodes ``args`` and ``kwargs``. """ consumer_list = [] for n in graph.nodes: if node in n.all_input_nodes: consumer_list.append(n) return consumer_list def assign_bfs_level_to_nodes(graph: Graph): """ Give a graph, assign bfs level to each node of this graph excluding ``placeholder`` and ``output`` nodes. Example: class MLP(torch.nn.Module): def __init__(self, dim: int): super().__init__() self.linear1 = torch.nn.Linear(dim, dim) self.linear2 = torch.nn.Linear(dim, dim) self.linear3 = torch.nn.Linear(dim, dim) self.linear4 = torch.nn.Linear(dim, dim) self.linear5 = torch.nn.Linear(dim, dim) def forward(self, x): l1 = self.linear1(x) l2 = self.linear2(x) l3 = self.linear3(l1) l4 = self.linear4(l2) l5 = self.linear5(l3) return l4, l5 model = MLP(4) gm = symbolic_trace(model) print(gm.graph) assign_bfs_level_to_nodes(gm.graph) for node in gm.graph.nodes: if hasattr(node, 'bfs_level'): print(node.name, node.bfs_level) Output: graph(): %x : [#users=2] = placeholder[target=x] %linear1 : [#users=1] = call_module[target=linear1](args = (%x,), kwargs = {}) %linear2 : [#users=1] = call_module[target=linear2](args = (%x,), kwargs = {}) %linear3 : [#users=1] = call_module[target=linear3](args = (%linear1,), kwargs = {}) %linear4 : [#users=1] = call_module[target=linear4](args = (%linear2,), kwargs = {}) %linear5 : [#users=1] = call_module[target=linear5](args = (%linear3,), kwargs = {}) return (linear4, linear5) linear1 0 linear2 0 linear3 1 linear4 1 linear5 2 """ current_level = 0 nodes_to_process = [] top_nodes = get_top(graph) for node in top_nodes: node.bfs_level = current_level nodes_to_process.extend(get_all_consumers(graph, node)) current_level += 1 while nodes_to_process: new_process_list = [] for node in nodes_to_process: if node.op == 'output': continue node.bfs_level = current_level new_process_list.extend(get_all_consumers(graph, node)) nodes_to_process = new_process_list current_level += 1 def get_node_module(node) -> torch.nn.Module: """ Find the module associated with the given node. Args: node (torch.fx.Node): a torch.fx.Node object in the fx computation graph Returns: torch.nn.Module: the module associated with the given node """ assert node.graph.owning_module is not None, 'Cannot find the owning_module for node.graph, please make sure the graph is associated with a GraphModule object' assert node.op == 'call_module', f'Expected node.op to be call_module, but found {node.op}' module = node.graph.owning_module.get_submodule(node.target) return module
from dataclasses import asdict from typing import Any, Dict, List, NamedTuple, Optional, Tuple import torch import torch.fx from torch.fx.node import Argument, Node, Target from torch.utils._pytree import tree_flatten from colossalai.fx._compatibility import compatibility from colossalai.fx.profiler import GraphInfo, profile_function, profile_method, profile_module @compatibility(is_backward_compatible=True) class ConcreteInfoProp(torch.fx.Interpreter): """ Execute an FX graph Node-by-Node with concrete tensor and record the memory usage, execution time of forward and backward, and type of the result into the corresponding node. Usage: BATCH_SIZE = 2 DIM_IN = 4 DIM_HIDDEN = 16 DIM_OUT = 16 model = torch.nn.Sequential( torch.nn.Linear(DIM_IN, DIM_HIDDEN), torch.nn.Linear(DIM_HIDDEN, DIM_OUT), ).cuda() input_sample = torch.rand(BATCH_SIZE, DIM_IN, device="cuda") gm = symbolic_trace(model) interp = ConcreteInfoProp(gm) interp.run(input_sample) print(interp.summary(unit='kb')) output of above code is Op type Op Forward time Backward time SAVE_FWD_IN FWD_OUT FWD_TMP BWD_OUT BWD_TMP ----------- ------- ----------------------- ------------------------ ------------- --------- --------- --------- --------- placeholder input_1 0.0 s 0.0 s False 0.00 KB 0.00 KB 0.00 KB 0.00 KB call_module _0 0.0003993511199951172 s 0.00706791877746582 s False 0.50 KB 0.00 KB 0.03 KB 0.66 KB call_module _1 6.29425048828125e-05 s 0.00018286705017089844 s False 0.50 KB 0.00 KB 0.12 KB 0.81 KB output output 0.0 s 0.0 s True 0.00 KB 0.00 KB 0.00 KB 0.00 KB Args: module (GraphModule): The module to be executed """ _is_proped: bool = False def run(self, *args, initial_env: Optional[Dict[Node, Any]] = None, enable_io_processing: bool = True) -> Any: """Customized run for ConcreteInfoProp We need to store the device in self.device Args: *args: The arguments to the Module to run, in positional order initial_env (Optional[Dict[Node, Any]]): An optional starting environment for execution. This is a dict mapping `Node` to any value. This can be used, for example, to pre-populate results for certain `Nodes` so as to do only partial evaluation within the interpreter. enable_io_processing (bool): If true, we process the inputs and outputs with graph's process_inputs and process_outputs function first before using them. Returns: Any: The value returned from executing the Module """ flatten_args, _ = tree_flatten(args) self.device = next(item for item in flatten_args if hasattr(item, "device")).device return super().run(*args, initial_env, enable_io_processing) @compatibility(is_backward_compatible=True) def run_node(self, n: Node) -> Any: """ Run a specific node ``n`` and return the result. Calls into placeholder, get_attr, call_function, call_method, call_module, or output depending on ``node.op`` Args: n (Node): The Node to execute Returns: Any: The result of executing ``n`` """ self._is_proped = True result, meta_info = super().run_node(n) n.meta = {**n.meta, **asdict(meta_info)} # extend MetaInfo to `n.meta` # TODO: the attribute node_size should be removed in the future setattr(n, 'node_size', n.meta.get('fwd_mem_tmp', 0) + n.meta.get('fwd_mem_out', 0)) n.meta['type'] = type(result) # retain the autograd graph for param in self.module.parameters(): param.grad = None return result # Main Node running APIs @compatibility(is_backward_compatible=True) def placeholder(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``placeholder`` node. Note that this is stateful: ``Interpreter`` maintains an internal iterator over arguments passed to ``run`` and this method returns next() on that iterator. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Returns: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and forward & backward time. """ return super().placeholder(target, args, kwargs), GraphInfo() @compatibility(is_backward_compatible=True) def get_attr(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``get_attr`` node. Will retrieve an attribute value from the ``Module`` hierarchy of ``self.module``. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ return super().get_attr(target, args, kwargs), GraphInfo() @compatibility(is_backward_compatible=True) def call_function(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_function`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and forward & backward time. """ assert not isinstance(target, str) return profile_function(target, self.device)(*args, **kwargs) @compatibility(is_backward_compatible=True) def call_method(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_method`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and forward & backward time. """ return profile_method(target, self.device)(*args, **kwargs) @compatibility(is_backward_compatible=True) def call_module(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute a ``call_module`` node with meta tensor and return the result and its meta profile. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and forward & backward time. """ # Retrieve executed args and kwargs values from the environment # Execute the method and return the result assert isinstance(target, str) submod = self.fetch_attr(target) return profile_module(submod, self.device)(*args, **kwargs) @compatibility(is_backward_compatible=True) def output(self, target: 'Target', args: Tuple[Argument, ...], kwargs: Dict[str, Any]) -> Any: """ Execute an ``output`` node. This really just retrieves the value referenced by the ``output`` node and returns it. Args: target (Target): The call target for this node. See `Node <https://pytorch.org/docs/master/fx.html#torch.fx.Node>`__ for details on semantics args (Tuple): Tuple of positional args for this invocation kwargs (Dict): Dict of keyword arguments for this invocation Return: result (Any): The argument value that was retrieved meta_info (MetaInfo): The memory cost and forward & backward time. """ return args[0], GraphInfo(save_fwd_in=True) def propagate(self, *args): """ Run `module` via interpretation and return the result and record the shape and type of each node. Args: *args (Tensor): the sample input. Returns: Any: The value returned from executing the Module """ return super().run(*args) def summary(self, unit: str = 'MB') -> str: """ Summarizes the memory and FLOPs statistics of the `GraphModule` in tabular format. Note that this API requires the ``tabulate`` module to be installed. """ # https://github.com/pytorch/pytorch/blob/master/torch/fx/graph.py try: from tabulate import tabulate except ImportError: print("`summary` relies on the library `tabulate`, " "which could not be found on this machine. Run `pip " "install tabulate` to install the library.") assert self._is_proped, "Please call `interp.run(input)` before calling `interp.summary()`." # Build up a list of summary information for each node node_summaries: List[List[Any]] = [] def mem_repr(mem: int) -> str: unit_divisor_map = { 'kb': 1024, 'mb': 1024**2, 'gb': 1024**3, 'tb': 1024**4, } return f"{mem / unit_divisor_map[unit.lower()]:.2f} {unit.upper()}" def time_repr(time: float): return f"{time:,} s" for node in self.module.graph.nodes: node: Node node_summaries.append([ node.op, str(node), time_repr(node.meta['fwd_time']), time_repr(node.meta['bwd_time']), node.meta['save_fwd_in'], mem_repr(node.meta['fwd_mem_out']), mem_repr(node.meta['fwd_mem_tmp']), mem_repr(node.meta['bwd_mem_out']), mem_repr(node.meta['bwd_mem_tmp']), ]) # Use the ``tabulate`` library to create a well-formatted table # presenting our summary information headers: List[str] = [ 'Op type', 'Op', 'Forward time', 'Backward time', 'SAVE_FWD_IN', 'FWD_OUT', 'FWD_TMP', 'BWD_OUT', 'BWD_TMP', ] return tabulate(node_summaries, headers=headers, stralign='right')
import torch import torch.nn as nn import operator from colossalai.tensor import ProcessGroup from colossalai.tensor.distspec import ShardSpec from colossalai.tensor.compute_spec import ComputePattern, ComputeSpec ELEMENTWISE_MODULE_OP = [torch.nn.Dropout, torch.nn.ReLU] ELEMENTWISE_FUNC_OP = [ torch.add, operator.add, torch.abs, torch.cos, torch.exp, torch.mul, operator.mul, operator.floordiv, operator.truediv, operator.neg, torch.multiply, torch.nn.functional.relu, torch.nn.functional.dropout ] def weight_split(weight: torch.nn.parameter.Parameter, dim: int, col_normal: bool) -> torch.nn.parameter.Parameter: """weight_split split a nn.Parameter Args: weight (torch.nn.parameter.Parameter): a torch Parameter instance dim (int): the dimension to be sharded along with col_normal(bool): col shard with gather or not Returns: _type_: _description_ """ if col_normal: setattr(weight, "fx_attr", (dim, "SHARD", "TP", "col_normal")) else: setattr(weight, "fx_attr", (dim, "SHARD", "TP", "col_needs_many_outputs")) return weight def column_shard_linear_pass(gm: torch.fx.GraphModule): # Split all the linear module with column shard. Currently for testing only. mod_graph = gm.graph for node in mod_graph.nodes: if node.op == "call_module": target_module = node.graph.owning_module.get_submodule(node.target) if isinstance(target_module, torch.nn.Linear): target_module.weight = weight_split(target_module.weight, dim=0, col_normal=False) if target_module.bias is not None: target_module.bias.data = weight_split(target_module.bias.data, dim=0, col_normal=False) gm.recompile() return gm def row_shard_linear_pass(gm: torch.fx.GraphModule): # Split all the linear module with row shard. Currently for testing only. mod_graph = gm.graph for node in mod_graph.nodes: if node.op == "call_module": target_module = node.graph.owning_module.get_submodule(node.target) if isinstance(target_module, torch.nn.Linear): target_module.weight = weight_split(target_module.weight, dim=-1, col_normal=False) gm.recompile() return gm def transformer_mlp_pass(graph_module: torch.fx.GraphModule, process_group: ProcessGroup): """ This IR pass checks for transformer MLP like structure and annotate column and row sharding to the linear layers. """ #TODO: Needs to handle special cases, like x = linear(x) + linear(x) graph = graph_module.graph world_size = process_group.world_size() def _traverse_and_annotate(node, start_tracking, annotation_record, world_size): # traverse the graph to look for consecutive linear layers is_linear_module = False if node.op == 'call_module': # look for the linear layer module = node.graph.owning_module.get_submodule(node.target) if isinstance(module, nn.Linear): is_linear_module = True if start_tracking: # when start_tracking = True # it means the first linear has been found and the current module # is the second linear # set the current linear module to be row-sharded annotation_record['row'] = module for shard_type, module in annotation_record.items(): # add row sharding spec if shard_type == 'row': dist_spec = ShardSpec(dims=[-1], num_partitions=[world_size]) comp_spec = ComputeSpec(ComputePattern.TP1D) setattr(module.weight, 'pg', process_group) setattr(module.weight, 'dist_spec', dist_spec) setattr(module.weight, 'comp_spec', comp_spec) elif shard_type == 'col': weight_dist_spec = ShardSpec(dims=[0], num_partitions=[world_size]) weight_comp_spec = ComputeSpec(ComputePattern.TP1D) weight_comp_spec.output_replicate = False setattr(module.weight, 'pg', process_group) setattr(module.weight, 'dist_spec', weight_dist_spec) setattr(module.weight, 'comp_spec', weight_comp_spec) if module.bias is not None: bias_dist_spec = ShardSpec(dims=[0], num_partitions=[world_size]) bias_comp_spec = ComputeSpec(ComputePattern.TP1D) bias_comp_spec.output_replicate = False setattr(module.bias, 'pg', process_group) setattr(module.bias, 'dist_spec', bias_dist_spec) setattr(module.bias, 'comp_spec', bias_comp_spec) start_tracking = False annotation_record.clear() else: # when start tracking = False # it means the current layer is the first linear # set the linear layer to be col-sharded start_tracking = True annotation_record['col'] = module if start_tracking and not is_linear_module: # check against the white list # if non-element wise op is found, we reset the tracking if node.op == 'call_module': module = node.graph.owning_module.get_submodule(node.target) if module.__class__ not in ELEMENTWISE_MODULE_OP: start_tracking = False elif node.op == 'call_function' or node.op == 'call_method': if node.target not in ELEMENTWISE_FUNC_OP: start_tracking = False elif len(node.users.keys()) > 1: start_tracking = False if not start_tracking: annotation_record.clear() # stop tracking for consecutive linear when branch is found # e.g. # out1 = self.linear1(x) # out2 = self.linear2(x) # return out1+out2 next_nodes = list(node.users.keys()) if len(next_nodes) > 1: start_tracking = False annotation_record.clear() # traverse for node in next_nodes: _traverse_and_annotate(node, start_tracking, annotation_record, world_size) placeholder_node = list(graph.nodes)[0] annotate_record = {} _traverse_and_annotate(placeholder_node, False, annotate_record, world_size) return graph_module
import torch from torch.fx.graph_module import GraphModule from typing import Callable, List, Dict, Any, Optional from torch.fx._compatibility import compatibility from packaging import version from colossalai.fx.passes.meta_info_prop import TensorMetadata import inspect from typing import List from colossalai.fx.passes.split_module import Partition from colossalai.fx.passes.adding_split_node_pass import pipe_split, balanced_split_pass from torch.fx.node import Node def customized_split_pass_for_gpt2(gm: torch.fx.GraphModule, pp_size: int, partition_list: List[int]): ''' This pass is only used to do the gpt2 performance test, it may move into adding_split_node_pass.py, and will be deprecated in future. ''' mod_graph = gm.graph valid_children_size = 0 valid_children = [] for node in mod_graph.nodes: if node.op == "call_module": valid_children_size += 1 valid_children.append(node.target) if valid_children_size < pp_size: # If valid children is not enough to shard, we will use balanced policy instead of uniform policy. return balanced_split_pass(gm, pp_size) accumulate_layer_amount = 0 list_of_part = partition_list part_index = 0 for node in mod_graph.nodes: if pp_size <= 1: break if node.op == "call_module": if node.target in valid_children: accumulate_layer_amount += 1 if accumulate_layer_amount == list_of_part[part_index]: part_index += 1 pp_size -= 1 with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def split_with_split_nodes_pass_for_gp2_test(annotated_gm: torch.fx.GraphModule): ''' This pass will be used in gpt2 test, only a part of changes may be added into split_with_split_nodes_pass, and it will be deprecated in future. ''' part_idx = 0 def eliminate_unused_placeholders(gm): for node in gm.graph.nodes: if node.op == 'placeholder': if not len(node.users): gm.graph.erase_node(node) gm.recompile() return gm def refill_outputs_and_placeholders(gm, next_partition_placeholders): ''' This method is used to eliminate the outputs in previous partition which is unused in next partition. In split module pass, it treats partitions as a DAG, but we need treat them as a single direction linked list in pipeline parallel. The difference is if a output from partition 0 is an input argument of partition 3, the DAG will not transfer it to partition 1 and partition 2. However, in single direction linked list, we need to do so. ''' output_type = None output_args = [] non_output_list = [] new_placeholder_list = [] for node in gm.graph.nodes: if node.op == 'output': if isinstance(node.args[0], (tuple, list)): output_type = node.args[0].__class__ output_args.extend([n.name for n in node.args[0]]) else: output_args.append(node.args[0].name) rm_list = [] for name in output_args: if next_partition_placeholders and name not in next_partition_placeholders: rm_list.append(name) for name in rm_list: output_args.remove(name) gm.graph.erase_node(node) else: non_output_list.append(node.name) for name in next_partition_placeholders: if name not in output_args: output_args.append(name) for name in output_args: if name not in non_output_list: gm.graph.placeholder(name) # convert name to node for output_args for index, name in enumerate(output_args): for n in gm.graph.nodes: if n.name == name: output_args[index] = n continue # reorder the output args to make sure # output args has same order as next partition placeholder reorder_output_args = [] if next_partition_placeholders: for name in next_partition_placeholders: for node in output_args: if node.name == name: reorder_output_args.append(node) continue for node in gm.graph.nodes: if node.op == 'placeholder': new_placeholder_list.append(node.name) if output_type is not None: gm.graph.output(output_type(output_args)) else: gm.graph.output(output_args) gm.recompile() return gm, new_placeholder_list def split_callback(n: torch.fx.Node): nonlocal part_idx if (n.op, n.target) == ('call_function', pipe_split): part_idx += 1 return part_idx split_mod = split_module_for_gpt2_test(annotated_gm, None, split_callback) split_submodules = [] for name, submodule in split_mod.named_modules(): if isinstance(submodule, torch.fx.GraphModule): for node in submodule.graph.nodes: if (node.op, node.target) == ('call_function', pipe_split): submodule.graph.erase_node(node) submodule.recompile() split_submodules.append(submodule) submodules = list(split_mod.children()) placeholder_dict = {} for submodule in submodules: submodule = eliminate_unused_placeholders(submodule) placeholder_dict[submodule] = [] submodules.reverse() for index, submodule in enumerate(submodules): if index == 0: placeholder_list = [] else: placeholder_list = placeholder_dict[submodules[index - 1]] submodule, placeholder_dict[submodule] = refill_outputs_and_placeholders(submodule, placeholder_list) submodule.recompile() split_mod.recompile() return split_mod, split_submodules @compatibility(is_backward_compatible=True) def split_module_for_gpt2_test( m: GraphModule, root_m: torch.nn.Module, split_callback: Callable[[torch.fx.node.Node], int], ): """ This pass will be used in gpt2 pp performance test, only a part of changes may be added into split_module, and it will be deprecated in future. """ partitions: Dict[str, Partition] = {} orig_nodes: Dict[str, torch.fx.node.Node] = {} def _node_with_all_tensor_element(node_metadata: Any) -> int: """ return whether node contains non-tensor element. """ all_tensor_node = True if isinstance(node_metadata, TensorMetadata): all_tensor_node = node_metadata.is_tensor and all_tensor_node elif isinstance(node_metadata, dict): value_list = [v for _, v in node_metadata.items()] all_tensor_node += _node_with_all_tensor_element(value_list) else: for element in node_metadata: all_tensor_node += _node_with_all_tensor_element(element) return all_tensor_node def _move_all_ancestors_into_partition(node, partition_name): all_ancestors = set() def _gen_all_ancestors_set(node): all_ancestors.add(node) for n in node.all_input_nodes: if n in all_ancestors: continue _gen_all_ancestors_set(n) _gen_all_ancestors_set(node) for n in list(all_ancestors): if n.op != 'placeholder' and n._fx_partition > partition_name: n._fx_partition = partition_name def record_cross_partition_use(def_node: torch.fx.node.Node, use_node: Optional[torch.fx.node.Node]): # noqa: B950 def_partition_name = getattr(def_node, '_fx_partition', None) use_partition_name = getattr(use_node, '_fx_partition', None) if def_partition_name != use_partition_name: # if 'tensor_meta' in def_node.meta: # if not _node_with_all_tensor_element(def_node.meta['tensor_meta']): # _move_all_ancestors_into_partition(use_node, def_partition_name) # node_process_list.extend(use_node.all_input_nodes) # node_process_list.extend(list(use_node.users)) # node_process_list.append(use_node) # return if def_partition_name is not None: def_partition = partitions[def_partition_name] def_partition.outputs.setdefault(def_node.name) if use_partition_name is not None: def_partition.partition_dependents.setdefault(use_partition_name) if use_partition_name is not None: use_partition = partitions[use_partition_name] use_partition.inputs.setdefault(def_node.name) if def_partition_name is not None: use_partition.partitions_dependent_on.setdefault(def_partition_name) node_process_list = list(m.graph.nodes) # split nodes into parititons while node_process_list: node = node_process_list.pop(0) orig_nodes[node.name] = node if node.op in ["placeholder"]: continue if node.op == 'output': # partition_name = str(split_callback(node)) # def _set_output_args_partition(n, partition_name): # n._fx_partition = partition_name # torch.fx.graph.map_arg(node.args[0], lambda n: _set_output_args_partition(n, partition_name)) torch.fx.graph.map_arg(node.args[0], lambda n: record_cross_partition_use(n, None)) continue partition_name = str(split_callback(node)) # add node to partitions partition = partitions.get(partition_name) if partition is None: partitions[partition_name] = partition = Partition(partition_name) partition.node_names.append(node.name) origin_partition_name = getattr(node, '_fx_partition', None) if origin_partition_name is None: node._fx_partition = partition_name torch.fx.graph.map_arg(node.args, lambda def_node: record_cross_partition_use(def_node, node)) torch.fx.graph.map_arg(node.kwargs, lambda def_node: record_cross_partition_use(def_node, node)) # noqa: B950 # find partitions with no dependencies root_partitions: List[str] = [] for partition_name, partition in partitions.items(): if not len(partition.partitions_dependent_on): root_partitions.append(partition_name) # check partitions for circular dependencies and create topological partition ordering sorted_partitions: List[str] = [] while root_partitions: root_partition = root_partitions.pop() sorted_partitions.append(root_partition) for dependent in partitions[root_partition].partition_dependents: partitions[dependent].partitions_dependent_on.pop(root_partition) if not partitions[dependent].partitions_dependent_on: root_partitions.append(dependent) if len(sorted_partitions) != len(partitions): raise RuntimeError("cycle exists between partitions!") # add placeholders to parititons for partition_name in sorted_partitions: partition = partitions[partition_name] for input in partition.inputs: placeholder = partition.graph.placeholder(input) placeholder.meta = orig_nodes[input].meta.copy() partition.environment[orig_nodes[input]] = placeholder # Transform nodes and collect targets for partition's submodule for node in m.graph.nodes: if hasattr(node, '_fx_partition'): partition = partitions[node._fx_partition] # swap out old graph nodes in kw/args with references to new nodes in this submodule environment = partition.environment gathered_args = torch.fx.graph.map_arg(node.args, lambda n: environment[n]) gathered_kwargs = torch.fx.graph.map_arg(node.kwargs, lambda n: environment[n]) if node.op not in ['call_module', 'get_attr']: target = node.target else: target_atoms = node.target.split('.') target_attr = m for atom in target_atoms: if not hasattr(target_attr, atom): raise RuntimeError(f'Operator target {node.target} not found!') target_attr = getattr(target_attr, atom) # target = target_atoms[-1] target = '_'.join(target_atoms) partition.targets[target] = target_attr assert isinstance(gathered_args, tuple) assert isinstance(gathered_kwargs, dict) new_node = partition.graph.create_node(op=node.op, target=target, args=gathered_args, kwargs=gathered_kwargs, name=node.name) new_node.meta = node.meta.copy() partition.environment[node] = new_node # Set up values to construct base module base_mod_env: Dict[str, torch.fx.node.Node] = {} base_mod_graph: torch.fx.graph.Graph = torch.fx.graph.Graph() base_mod_attrs: Dict[str, torch.fx.graph_module.GraphModule] = {} for node in m.graph.nodes: if node.op == 'placeholder': if version.parse(torch.__version__) < version.parse('1.11.0'): base_mod_env[node.name] = base_mod_graph.placeholder(node.name, type_expr=node.type) else: default_value = node.args[0] if len(node.args) > 0 else inspect.Signature.empty base_mod_env[node.name] = base_mod_graph.placeholder(node.name, type_expr=node.type, default_value=default_value) base_mod_env[node.name].meta = node.meta.copy() # Do some things iterating over the partitions in topological order again: # 1) Finish off submodule Graphs by setting corresponding outputs # 2) Construct GraphModules for each submodule # 3) Construct the base graph by emitting calls to those submodules in # topological order for partition_name in sorted_partitions: partition = partitions[partition_name] # Set correct output values output_vals = tuple(partition.environment[orig_nodes[name]] for name in partition.outputs) output_vals = output_vals[0] if len(output_vals) == 1 else output_vals # type: ignore[assignment] partition.graph.output(output_vals) # Construct GraphModule for this partition submod_name = f'submod_{partition_name}' base_mod_attrs[submod_name] = torch.fx.graph_module.GraphModule(partition.targets, partition.graph) # noqa: B950 # Emit call in base graph to this submodule output_val = base_mod_graph.call_module(submod_name, tuple(base_mod_env[name] for name in partition.inputs)) if len(partition.outputs) > 1: # Unpack multiple return values from submodule output_val_proxy = torch.fx.proxy.Proxy(output_val) for i, output_name in enumerate(partition.outputs): base_mod_env[output_name] = output_val_proxy[i].node # type: ignore[index] else: if not partition.outputs: continue base_mod_env[list(partition.outputs)[0]] = output_val for node in m.graph.nodes: if node.op == 'output': base_mod_graph.output(torch.fx.graph.map_arg(node.args[0], lambda n: base_mod_env[n.name])) # noqa: B950 return torch.fx.graph_module.GraphModule(base_mod_attrs, base_mod_graph)
import torch from torch.fx import symbolic_trace from torch.fx.node import Node from colossalai.fx.passes.split_module import split_module def pipe_split(): pass def avgcompute_split_pass(gm: torch.fx.GraphModule, pp_size: int): """ In avgcompute_split_pass, we split module by the fwd flops. """ mod_graph = gm.graph # To use avgcompute_split_pass, we need run meta_info_prop interpreter first. # If nodes don't have meta info, this pass will fall back to normal balanced split pass. check_node = list(mod_graph.nodes)[0] if 'tensor_meta' not in check_node.meta: return balanced_split_pass(gm, pp_size) total_fwd_flop = 0 for node in mod_graph.nodes: total_fwd_flop += node.fwd_flop partition_flop = total_fwd_flop // pp_size accumulate_fwd_flop = 0 for node in mod_graph.nodes: if pp_size <= 1: break if 'pipe_split' in node.name: continue accumulate_fwd_flop += node.fwd_flop if accumulate_fwd_flop >= partition_flop: total_fwd_flop = total_fwd_flop - accumulate_fwd_flop accumulate_fwd_flop = 0 pp_size -= 1 partition_flop = total_fwd_flop // pp_size with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def avgnode_split_pass(gm: torch.fx.GraphModule, pp_size: int): """ In avgnode_split_pass, simpliy split graph by node number. """ mod_graph = gm.graph avg_num_node = len(mod_graph.nodes) // pp_size accumulate_num_node = 0 for node in mod_graph.nodes: if pp_size <= 1: break accumulate_num_node += 1 if accumulate_num_node >= avg_num_node: accumulate_num_node = 0 pp_size -= 1 if node.next.op == 'output': with mod_graph.inserting_before(node): split_node = mod_graph.create_node('call_function', pipe_split) else: with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def balanced_split_pass(gm: torch.fx.GraphModule, pp_size: int): """ In balanced_split_pass, we split module by the size of parameters(weights+bias). """ mod_graph = gm.graph total_param_amount = 0 for param in mod_graph.owning_module.parameters(): total_param_amount += param.numel() params_per_partition = total_param_amount // pp_size accumulate_param_amount = 0 for node in mod_graph.nodes: if pp_size <= 1: break if node.op == "call_module": target_module = node.graph.owning_module.get_submodule(node.target) for param in target_module.parameters(): accumulate_param_amount += param.numel() if accumulate_param_amount >= params_per_partition: accumulate_param_amount = 0 pp_size -= 1 # If the next node is output node, we will insert split annotation before # node to make sure there is at least one node in last partition. if node.next.op == 'output': with mod_graph.inserting_before(node): split_node = mod_graph.create_node('call_function', pipe_split) else: with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) if pp_size > 1: node_counter = 0 for node in mod_graph.nodes: if pp_size <= 1: break if node.op == 'placeholder': continue elif node_counter == 0: node_counter += 1 else: pp_size -= 1 node_counter = 0 with mod_graph.inserting_before(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def balanced_split_pass_v2(gm: torch.fx.GraphModule, pp_size: int): """ In balanced_split_pass_v12, we split module by the size of nodes(weights+bias+outputs). """ mod_graph = gm.graph # To use balanced_split_pass_v2, we need run meta_info_prop interpreter first. # If nodes don't have meta info, this pass will fall back to normal balanced split pass. check_node = list(mod_graph.nodes)[0] if 'tensor_meta' not in check_node.meta: return balanced_split_pass(gm, pp_size) total_element_size = 0 for node in mod_graph.nodes: total_element_size += node.node_size partition_size = total_element_size // pp_size accumulate_node_size = 0 for node in mod_graph.nodes: if pp_size <= 1: break if 'pipe_split' in node.name: continue accumulate_node_size += node.node_size if accumulate_node_size >= partition_size: total_element_size = total_element_size - accumulate_node_size accumulate_node_size = 0 pp_size -= 1 partition_size = total_element_size // pp_size with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def uniform_split_pass(gm: torch.fx.GraphModule, pp_size: int): mod_graph = gm.graph valid_children_size = 0 valid_children = [] for module in mod_graph.owning_module.children(): valid_children_size += 1 valid_children.append(module) if valid_children_size < pp_size: # If valid children is not enough to shard, we will use balanced policy instead of uniform policy. return balanced_split_pass(gm, pp_size) layers_per_partition = valid_children_size // pp_size accumulate_layer_amount = 0 for node in mod_graph.nodes: if pp_size <= 1: break if node.op == "call_module": target_module = node.graph.owning_module.get_submodule(node.target) if target_module in valid_children: accumulate_layer_amount += 1 if accumulate_layer_amount == layers_per_partition: accumulate_layer_amount = 0 pp_size -= 1 with mod_graph.inserting_after(node): split_node = mod_graph.create_node('call_function', pipe_split) gm.recompile() return gm def split_with_split_nodes_pass(annotated_gm: torch.fx.GraphModule, merge_output=False): # TODO(lyl): use partition IR to assign partition ID to each node. # Currently: analyzing graph -> annotate graph by inserting split node -> use split module pass to split graph # In future: graph to partitions -> analyzing partition IR -> recombining partitions to get best performance -> assign partition ID to each node part_idx = 0 def split_callback(n: torch.fx.Node): nonlocal part_idx if (n.op, n.target) == ('call_function', pipe_split): part_idx += 1 return part_idx split_mod = split_module(annotated_gm, None, split_callback, merge_output) split_submodules = [] for name, submodule in split_mod.named_modules(): if isinstance(submodule, torch.fx.GraphModule): for node in submodule.graph.nodes: if (node.op, node.target) == ('call_function', pipe_split): submodule.graph.erase_node(node) submodule.recompile() split_submodules.append(submodule) return split_mod, split_submodules
import torch from typing import List from torch.fx import symbolic_trace from torch.fx.node import Node from colossalai.fx.passes.split_module import split_module from colossalai.tensor.shape_consistency import ShapeConsistencyManager from colossalai.device.device_mesh import DeviceMesh from colossalai.tensor.sharding_spec import ShardingSpec, _DimSpec import builtins import operator from copy import deepcopy def apply(*args, **kwargs): shape_consistency_manager = ShapeConsistencyManager() return shape_consistency_manager.apply(*args, **kwargs) def solution_annotatation_pass(gm: torch.fx.GraphModule, solution: List[int], device_mesh): mod_graph = gm.graph nodes = tuple(mod_graph.nodes) # the dict to get origin sharding spec of node origin_node_sharding_spec_dict = {} for node_index, (node, strategy_index) in enumerate(zip(nodes, solution)): strategies_vector = node.strategies_vector setattr(node, 'best_strategy', strategies_vector[strategy_index]) setattr(node, 'sharding_spec', strategies_vector[strategy_index].output_sharding_spec) origin_node_sharding_spec_dict[node_index] = strategies_vector[strategy_index].output_sharding_spec # apply the sharding spec of parameters for node in nodes: if node.op == 'call_module': target_module = node.graph.owning_module.get_submodule(node.target) origin_sharding_spec = ShardingSpec(device_mesh, target_module.weight.shape, {}) setattr(target_module.weight, 'sharding_spec', origin_sharding_spec) target_weight_sharding_spec = node.best_strategy.input_shardings[1] target_module.weight.data = target_module.weight.data.permute((1, 0, 2, 3)) apply(target_module.weight, target_weight_sharding_spec) target_module.weight.data = target_module.weight.data.permute((1, 0, 2, 3)) # the dict to get input sharding specs of user node sharding_spec_convert_dict = {} for index, node in enumerate(nodes): target_sharding_specs = [] for user_node in node.strategies_vector.successor_nodes: node_index = user_node.strategies_vector.predecessor_nodes.index(node) target_sharding_spec = user_node.best_strategy.input_shardings[node_index] target_sharding_specs.append(target_sharding_spec) sharding_spec_convert_dict[index] = target_sharding_specs # add above dicts into graph for node in nodes: if node.op != 'placeholder': with mod_graph.inserting_before(node): input_specs_node = mod_graph.create_node('placeholder', target='sharding_spec_convert_dict') origin_specs_node = mod_graph.create_node('placeholder', target='origin_node_sharding_spec_dict') break return sharding_spec_convert_dict, origin_node_sharding_spec_dict def shape_consistency_pass(gm: torch.fx.GraphModule): mod_graph = gm.graph nodes = tuple(mod_graph.nodes) input_dict_node = None origin_dict_node = None # mapping the node into the origin graph index node_to_index_dict = {} index = 0 for node in nodes: if node.target == 'sharding_spec_convert_dict': input_dict_node = node continue if node.target == 'origin_node_sharding_spec_dict': origin_dict_node = node continue if not hasattr(node, 'best_strategy'): continue node_to_index_dict[node] = index index += 1 assert input_dict_node is not None # add shape consistency apply function into graph for node in nodes: if not hasattr(node, 'best_strategy'): continue with mod_graph.inserting_after(node): origin_spec_node = mod_graph.create_node('call_function', operator.getitem, args=(origin_dict_node, node_to_index_dict[node])) with mod_graph.inserting_after(origin_spec_node): set_sharding_spec_node = mod_graph.create_node('call_function', builtins.setattr, args=(node, 'sharding_spec', origin_spec_node)) for user_node in node.strategies_vector.successor_nodes: node_index = user_node.strategies_vector.predecessor_nodes.index(node) with mod_graph.inserting_before(user_node): input_specs_node = mod_graph.create_node('call_function', operator.getitem, args=(input_dict_node, node_to_index_dict[node])) with mod_graph.inserting_before(user_node): sharding_spec_node = mod_graph.create_node('call_function', operator.getitem, args=(input_specs_node, node_index)) with mod_graph.inserting_before(user_node): shape_consistency_node = mod_graph.create_node('call_function', apply, args=(node, sharding_spec_node)) return gm
import copy import math from typing import List, Tuple import torch from colossalai.fx import is_compatible_with_meta from colossalai.fx.codegen.activation_checkpoint_codegen import \ _find_nested_ckpt_regions from colossalai.fx.graph_module import ColoGraphModule from colossalai.fx.passes.algorithms.ckpt_solver_rotor import (_compute_table, _construct_chain, _rec) from colossalai.fx.passes.meta_info_prop import MetaInfoProp from colossalai.fx.profiler import parameter_size from torch.fx import GraphModule, Node from .linearize import linearize from .operation import (Backward, Chain, ForwardCheck, ForwardEnable, ForwardNograd, Function, Loss, Offload, Prefetch, Sequence) INF = float("inf") def _normalize_flops(chain: Chain, flops) -> Chain: """ Normalize flops """ for i in range(chain.length): chain.fweight[i] /= flops chain.bweight[i] /= flops return chain class PofoTable: """PofoTable The PofoTable contains the necessary components to store intermediate results of dynamic programming and the operations alone the way. """ def __init__(self, chain_length: int, mem_slots: int): """Init pofo table The pofo table contains two tables, opt and what, indicating values and operations. Args: chain_length (int): chain length mem_slots (int): number of memory slots """ self.length = chain_length self.mem_slots = mem_slots # initializing tables # the first bool indicates whether the input has bar # opt table is for value, opt[True/False][i][A][(df, db)] = OCx(i, A, df, db) # what table is for decision, what[True/False][i][A][(df, db)] = (is_enable, is_offload, index) # where is_enable indicates whether we enable the gradient, is_offload indicates whether we # offload the input, index indicates the end of F_\empty sequence if is_enable = False self.opt = { False: [[{} for _ in range(mem_slots + 1)] for _ in range(self.length + 1)], True: [[{} for _ in range(mem_slots + 1)] for _ in range(self.length + 1)] } self.what = { False: [[{} for _ in range(mem_slots + 1)] for _ in range(self.length + 1)], True: [[{} for _ in range(mem_slots + 1)] for _ in range(self.length + 1)] } def _get_value(self, state, table, default): i, act_size, df, db, input_has_bar = state if act_size + df > self.mem_slots or act_size + db > self.mem_slots: return default try: return table[input_has_bar][i][act_size][(df, db)] except KeyError: print(f"state not found {state}") def get_opt(self, state): return self._get_value(state, self.opt, INF) def get_what(self, state): return self._get_value(state, self.what, INF) def set_value(self, state, opt, what): i, act_size, df, db, input_has_bar = state self.opt[input_has_bar][i][act_size][(df, db)] = opt self.what[input_has_bar][i][act_size][(df, db)] = what class PofoSolver: """PofoSolver that executes algorithm mentioned in https://proceedings.neurips.cc/paper/2021/hash/c8461bf13fca8a2b9912ab2eb1668e4b-Abstract.html The new pofo solver is based on paper Efficient Combination of Rematerialization and Offloading for Training DNNs and it's code given in the supplemental. Currently we doesn't use the whole set up in the original paper and reuse rotor solver for the backward sequence as suggested in supplemental. The solver now is able to find strategy with offload. """ def __init__(self, chain: Chain, max_memory: int, bandwidth, mem_slots: int) -> None: self.chain = chain self.length = chain.length self.max_memory = max_memory self.mem_slots = mem_slots self.mem_unit = max_memory / mem_slots self.bandwidth = bandwidth self.disc_chain = copy.deepcopy(self.chain) self.disc_chain._discretize(self.mem_unit) self.rotor_table = _compute_table(self.disc_chain, mem_slots) self._compute_pofo_table() def _discretize(self, *values) -> Tuple: return tuple(math.ceil(value / self.mem_unit) for value in values) def _undiscretize(self, *discrete_values) -> Tuple: if len(discrete_values) == 1: return discrete_values[0] * self.mem_unit else: return tuple(d * self.mem_unit for d in discrete_values) def _mmax_all(self, idx: int): """ Calculate the maximum memory usage of Fi_all """ return self.chain.cbweight[idx + 1] + self.chain.fwd_mem_tmp[idx] def _mmax_b(self, idx: int): """ Calculate the maximum memory usage of Bi """ return self.chain.cbweight[idx + 1] + self.chain.cweight[idx + 1] + self.chain.cweight[idx] + self.chain.bwd_mem_tmp[idx] def _mmax_ng(self, i: int, j: int): """ Calculate the maximum memory usage of CF_i, F_i+1\empty, ... F_j\empty """ res = self.chain.cweight[j + 1] + self.chain.fwd_mem_tmp[j] if j > i: res += self.chain.cweight[j] return res def _rotor_estimated_bwd(self, i, j, m, delta): compute = self.rotor_table[0][math.floor((m - self.chain.cweight[i]) / self.mem_unit)][i][j] comm = delta / self.bandwidth return (max(compute, comm) + compute + comm) / 2 def _rotor_estimated_bwd_sequence(self, i, j, m, delta): return _rec(self.disc_chain, i, j, math.floor((m - self.chain.cweight[i]) / self.mem_unit), self.rotor_table) def _common_values_enable(self, state: Tuple): idx, act_size, df, db, input_has_bar = state input_size = self.chain.cbweight[idx] if input_has_bar else self.chain.cweight[idx] mf = act_size + df + input_size mb = act_size + db + input_size mem_avail = self.max_memory - act_size - input_size f_usage = self._mmax_all(idx) b_usage = self._mmax_b(idx) # infeasible if f_usage > mem_avail or b_usage > mem_avail: return None # calculate idle time eps_f_beta = max(0, f_usage - self.max_memory + mf) eps_b_beta = max(0, b_usage - self.max_memory + mb) idle_time = (eps_f_beta + eps_b_beta) / self.bandwidth # calculate offload and prefetch data offload_data = self.chain.fweight[idx] * self.bandwidth + eps_f_beta prefetch_data = self.chain.bweight[idx] * self.bandwidth + eps_b_beta # total_time total_time = self.chain.fweight[idx] + self.chain.bweight[idx] + idle_time return (offload_data, prefetch_data, total_time, idle_time) def _common_values_nograd(self, state: Tuple, j: int, iterative: bool = False): i, act_size, df, db, input_has_bar = state # compute new epsilon_tmp and sum_fwds if iterative: self.epsilon_tmp = max(self.epsilon_tmp, self._mmax_ng(i, j) - self.bandwidth * self.sum_fwds) self.sum_fwds += self.chain.fweight[j] else: self.epsilon_tmp = max( self._mmax_ng(i, k) - self.bandwidth * sum(self.chain.fweight[i:k]) for k in range(i, j + 1)) self.sum_fwds = sum(self.chain.fweight[i:j + 1]) input_size = self.chain.cbweight[i] if input_has_bar else self.chain.cweight[i] mf = act_size + df + input_size mem_avail = self.max_memory - act_size - input_size # if infeasible if max(self._mmax_ng(i, k) for k in range(i, self.length)) > mem_avail: return None eps_f_beta = max(0, self.epsilon_tmp - self.max_memory + mf) offload_data = self.sum_fwds * self.bandwidth + eps_f_beta # TODO: Implement the precise backward recompute sequence mentioned in the paper # currently we will use an approximate way to get the backward time time_backward = self._rotor_estimated_bwd(i, j, mem_avail, db) prefetch_data = time_backward * self.bandwidth idle_time = eps_f_beta / self.bandwidth total_time = self.sum_fwds + idle_time + time_backward return (offload_data, prefetch_data, total_time, idle_time) def _new_values(self, state: Tuple, do_offload: bool, common_values: Tuple) -> Tuple: """Generate new values for next state Args: state (Tuple): undiscretized states do_offload (bool): bool type indicates whether we need to do offload common_values (Tuple): common values (offload_data, prefetch_data, total_time, idle_time) Returns: Tuple: (new_act_size, new_df, new_db) """ idx, act_size, df, db, input_has_bar = state offload_data, prefetch_data, *_ = common_values input_size = self.chain.cbweight[idx] if input_has_bar else self.chain.cweight[idx] if do_offload: new_act_size = act_size new_df = max(0, df + input_size - offload_data) new_db = max(0, db - prefetch_data) + input_size else: new_act_size = act_size + input_size new_df = max(0, df - offload_data) new_db = max(0, db - prefetch_data) return (new_act_size, new_df, new_db) def _compute_pofo_table(self): self.table = PofoTable(self.length, self.mem_slots) # initializing the loss for act_size in range(self.mem_slots + 1): for df in range(self.mem_slots - act_size + 1): for db in range(self.mem_slots - act_size + 1): # undiscretize for idle time calculation origin_values = self._undiscretize(act_size, df, db) for input_has_bar in (False, True): disc_state = (self.length, act_size, df, db, input_has_bar) state = (self.length, *origin_values, input_has_bar) common_values = self._common_values_enable(state) # if no feasible choice if common_values is None: self.table.set_value(disc_state, INF, None) continue # if there is feasible choice new_act_size, new_df, new_db = self._new_values(state, False, common_values) eps_g = (new_df + new_db) / self.bandwidth total_time = common_values[2] + eps_g self.table.set_value(disc_state, total_time, (True, False)) # main loop for i in reversed(range(self.length)): for act_size in range(self.mem_slots + 1): for df in range(self.mem_slots - act_size + 1): for db in range(self.mem_slots - act_size + 1): # undiscretize for idle time calculation origin_values = self._undiscretize(act_size, df, db) for input_has_bar in (False, True): best_result = INF best_choice = None disc_state = (i, act_size, df, db, input_has_bar) state = (i, *origin_values, input_has_bar) # case 1: start with F_all vals_enable = self._common_values_enable(state) if vals_enable is not None: for do_offload in (True, False): new_state = self._new_values(state, do_offload, vals_enable) new_state = (i + 1, *self._discretize(*new_state), True) total_time = vals_enable[2] results_all = self.table.get_opt(new_state) + total_time if results_all < best_result: best_result = results_all best_choice = (True, do_offload) # case 2: start with F_ck self.sum_fwds = 0 self.epsilon_tmp = 0 for j in range(i, self.length): vals_nograd = self._common_values_nograd(state, j, True) # if infeasible if vals_nograd is None: continue for do_offload in (True, False): new_state = self._new_values(state, do_offload, vals_nograd) new_state = (j + 1, *self._discretize(*new_state), False) total_time = vals_nograd[2] result_nograd = total_time + self.table.get_opt(new_state) if result_nograd < best_result: best_result = result_nograd best_choice = (False, do_offload, j) self.table.set_value(disc_state, best_result, best_choice) def pofo_rec(self, disc_state): i, act_size, df, db, input_has_bar = disc_state result = Sequence(Function("pofo", *disc_state)) what = self.table.get_what(disc_state) state = self._undiscretize(act_size, df, db) state = (i, *state, input_has_bar) i, act_size, df, db, input_has_bar = state if what is None: return None # if loss if i == self.length: result.insert(Loss()) return result if what[0]: do_offload = what[1] values = self._common_values_enable(state) new_state = self._discretize(*self._new_values(state, do_offload, values)) new_state = (i + 1, *new_state, True) if do_offload: result.insert(Offload(i, input_has_bar)) result.insert(ForwardEnable(i)) result.insert_sequence(self.pofo_rec(new_state)) if do_offload: result.insert(Prefetch(i, input_has_bar)) result.insert(Backward(i)) else: _, do_offload, j = what values = self._common_values_nograd(state, j) new_state = self._discretize(*self._new_values(state, do_offload, values)) new_state = (j + 1, *new_state, False) if do_offload: result.insert(Offload(i, input_has_bar)) result.insert(ForwardCheck(i)) for k in range(i + 1, j + 1): result.insert(ForwardNograd(k)) result.insert_sequence(self.pofo_rec(new_state)) if do_offload: result.insert(Prefetch(i, input_has_bar)) m = self.max_memory - act_size - (self.chain.cbweight[i] if input_has_bar else self.chain.cweight[i]) #TODO: Implement the precise backward recompute sequence mentioned in the paper result.insert_sequence(self._rotor_estimated_bwd_sequence(i, j, m, db)) return result def _annotate_from_pofo_sequence(sequence: Sequence, node_list: List[List[Node]]): op_list = sequence.list_operations() loss_op = next(op for op in op_list if isinstance(op, Loss)) fwd_list = op_list[:op_list.index(loss_op)] bwd_list = op_list[op_list.index(loss_op) + 1:] ckpt_idx = 0 in_ckpt = False ckpt_region = [] # forward annotation for op in fwd_list: if in_ckpt: if isinstance(op, ForwardNograd): ckpt_region.append(op.index) elif isinstance(op, ForwardEnable): in_ckpt = False for node_idx in ckpt_region: for n in node_list[node_idx]: setattr(n, "activation_checkpoint", [ckpt_idx]) ckpt_idx += 1 ckpt_region = [] elif isinstance(op, ForwardCheck): for node_idx in ckpt_region: for n in node_list[node_idx]: setattr(n, "activation_checkpoint", [ckpt_idx]) ckpt_idx += 1 ckpt_region = [op.index] else: if isinstance(op, ForwardCheck): in_ckpt = True ckpt_region.append(op.index) # annotate the backward if there is any nested activation checkpoint in_recompute = False for op in bwd_list: if in_recompute: if isinstance(op, ForwardNograd): ckpt_region.append(op.index) elif isinstance(op, ForwardEnable): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) ckpt_idx += 1 ckpt_region = [] elif isinstance(op, ForwardCheck): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) ckpt_idx += 1 ckpt_region = [op.index] elif isinstance(op, Backward): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) in_recompute = False else: if not isinstance(op, Backward): in_recompute = True ckpt_idx = 0 ckpt_region = [] if isinstance(op, ForwardCheck): ckpt_region.append(op.index) # postprocess, make sure every activation checkpoint label in the # same activation checkpoint region (level = 0) has the same length op_list = [] for node in node_list: op_list += node ckpt_regions = _find_nested_ckpt_regions(op_list) for (start_idx, end_idx) in ckpt_regions: nested_length = max(len(op_list[idx].activation_checkpoint) for idx in range(start_idx, end_idx + 1)) for idx in range(start_idx, end_idx + 1): op_list[idx].activation_checkpoint += [None] * (nested_length - len(op_list[idx].activation_checkpoint)) # annotate the offload offload_idx = 0 for idx, op in enumerate(fwd_list): if isinstance(op, Offload): # corner case: offload input if op.index == 0: if isinstance(fwd_list[idx + 1], ForwardCheck): for n in node_list[op.index]: setattr(n, "activation_offload", True) else: for n in node_list[op.index]: setattr(n, "activation_offload", (offload_idx, True, False)) offload_idx += 1 else: if op.has_bar: # annotate previous node if hasattr(node_list[op.index - 1][0], "activation_offload"): for n in node_list[op.index - 1]: n.activation_offload[-1] = True else: for n in node_list[op.index - 1]: setattr(n, "activation_offload", [offload_idx, False, True]) offload_idx += 1 # annotate this node if isinstance(fwd_list[idx + 1], ForwardCheck): for n in node_list[op.index]: setattr(n, "activation_offload", True) else: for n in node_list[op.index]: setattr(n, "activation_offload", [offload_idx, True, False]) offload_idx += 1 def solver_pofo(gm: ColoGraphModule, data, bandwidth, flops, mem_limit: int, mem_slots: int = 50, cnode: List[str] = None, eps: float = 0.0) -> ColoGraphModule: """Solver that combine offload and activation checkpoint Reference: https://proceedings.neurips.cc/paper/2021/hash/c8461bf13fca8a2b9912ab2eb1668e4b-Abstract.html Args: gm (ColoGraphModule): ColoGraphModule derived from tracer data: input of the model bandwidth: offload bandwidth, unit Byte/s flops: FLOPS of device, unit FLOPs/s mem_limit (int): memory limit, unit Byte mem_slots (int, optional): number of memory slots. Defaults to 500. cnode (List[str], optional): common node for linearize. Defaults to None. eps (float, optional): epsilon for memory decay. Defaults to 0.02. Returns: ColoGraphModule: annotated graph module """ node_list = linearize(gm, cnode) mem_limit -= parameter_size(gm) # prepare data if is_compatible_with_meta(): from colossalai.fx.profiler import MetaTensor data = MetaTensor(data, fake_device=next(gm.parameters()).device) MetaInfoProp(gm).run(data) chain: Chain = _construct_chain(node_list, data) chain = _normalize_flops(chain, flops) # currently we view loss as an op without expense chain.cbweight.append(0) chain.cweight.append(0) chain.fwd_mem_tmp.append(0) chain.bwd_mem_tmp.append(0) chain.fweight.append(0) chain.bweight.append(0) solver = PofoSolver(chain, mem_limit, bandwidth, mem_slots) first_state = (0, 0, 0, 0, False) sequence = solver.pofo_rec(first_state) if sequence == None: raise ValueError(f"Cannot solve sequence with {mem_limit} Bytes memory") _annotate_from_pofo_sequence(sequence, node_list) setattr(gm, "__sequence__", sequence) return gm
from .ckpt_solver_chen import chen_greedy from .linearize import linearize from .ckpt_solver_rotor import solver_rotor from .ckpt_solver_pofo import solver_pofo
import math def _discretize(mem_unit, values): return [math.ceil(value / mem_unit) for value in values] class Chain: def __init__(self, fw, bw, cw, cbw, ftmp, btmp, check=True): self.fweight = fw self.bweight = bw self.cweight = cw self.cbweight = cbw self.fwd_mem_tmp = ftmp self.bwd_mem_tmp = btmp self.length = len(fw) if check and not self.check_lengths(): raise AttributeError("In Chain, input lists do not have consistent lengths") def check_lengths(self): return ((len(self.fweight) == self.length) and (len(self.bweight) == self.length + 1) and (len(self.cweight) == self.length + 1) and (len(self.fwd_mem_tmp) == self.length) and (len(self.bwd_mem_tmp) == self.length + 1) and (len(self.cbweight) == self.length + 1)) def __repr__(self): chain_list = [] for i in range(self.length): chain_list.append((self.fweight[i], self.bweight[i], self.cweight[i], self.cbweight[i], self.fwd_mem_tmp[i], self.bwd_mem_tmp[i])) i = self.length chain_list.append((None, self.bweight[i], self.cweight[i], self.cbweight[i], None, self.bwd_mem_tmp[i])) return chain_list.__repr__() def _discretize(self, mem_unit): self.cweight = _discretize(mem_unit, self.cweight) self.cbweight = _discretize(mem_unit, self.cbweight) self.fwd_mem_tmp = _discretize(mem_unit, self.fwd_mem_tmp) self.bwd_mem_tmp = _discretize(mem_unit, self.bwd_mem_tmp) class Operation: def shift(self, value): if type(self.index) is tuple: self.index = tuple(x + value for x in self.index) else: self.index += value class Offload(Operation): def __init__(self, index, has_bar=False) -> None: super().__init__() self.index = index self.name = "Off" self.has_bar = has_bar if self.has_bar: self.name += "wBar" def __repr__(self): return f"{self.name}_{self.index}" class Prefetch(Operation): def __init__(self, index, has_bar=False) -> None: super().__init__() self.index = index self.name = "Pre" self.has_bar = has_bar if self.has_bar: self.name += "wBar" def __repr__(self): return f"{self.name}_{self.index}" class Forward(Operation): def __init__(self, index): self.index = index self.name = "F" def __repr__(self): return "{n}_{i}".format(n=self.name, i=self.index) def cost(self, chain: Chain): if chain is not None: return chain.fweight[self.index] else: return 1 class ForwardEnable(Forward): def __init__(self, index): super().__init__(index) self.name = "Fe" class ForwardNograd(Forward): def __init__(self, index): super().__init__(index) self.name = "Fn" class ForwardCheck(Forward): def __init__(self, index): super().__init__(index) self.name = "CF" class Forwards(Operation): def __init__(self, start, end): self.index = (start, end) def __repr__(self): return "F_{i}->{j}".format(i=self.index[0], j=self.index[1]) def cost(self, chain: Chain): if chain is not None: return sum(chain.fweight[self.index[0]:self.index[1] + 1]) else: return (self.index[1] - self.index[0] + 1) def isForward(op): return type(op) is Forward or type(op) is Forwards class Backward(Operation): def __init__(self, index): self.index = index def __repr__(self): return "B_{i}".format(i=self.index) def cost(self, chain: Chain): if chain is not None: return chain.bweight[self.index] else: return 1 class Loss(Operation): def __init__(self): pass def __repr__(self): return "L" def cost(self, chain): return 0 class MemoryAccess(Operation): def __init__(self, index): self.index = index def __repr__(self): return "{n}_{i}".format(n=self.name, i=self.index) def cost(self, chain: Chain): return 0 class WriteMemory(MemoryAccess): def __init__(self, index): super().__init__(index) self.name = "WM" class ReadMemory(MemoryAccess): def __init__(self, index): super().__init__(index) self.name = "RM" class DiscardMemory(MemoryAccess): def __init__(self, index): super().__init__(index) self.name = "DM" class Function: def __init__(self, name, *args): self.name = name self.args = args self.str_args = ','.join(str(v) for v in self.args) def __repr__(self): return "{n}({args})".format(n=self.name, args=self.str_args) class Sequence: def __init__(self, function): self.sequence = [] #List of Operation and Sequence self.function = function #Description the function (name and parameters) def __repr__(self): return repr(self.list_operations()) def list_operations(self): op_list = [] for x in self.sequence: if isinstance(x, Operation): op_list.append(x) else: assert isinstance(x, Sequence) op_list += x.list_operations() return op_list def insert(self, operation): self.sequence.append(operation) def remove(self, operation_index): del self.sequence[operation_index] def insert_sequence(self, sequence): self.sequence.append(sequence) def shift(self, value): for x in self.sequence: x.shift(value) return self def remove_useless_write(self): if self.sequence: if isinstance(self.sequence[0], WriteMemory): self.remove(0) return self def get_makespan(self, chain): return sum(op.cost(chain) for op in self.list_operations()) def without_suffix(self): ops = self.list_operations() end_of_first_phase = [i for i in range(len(ops)) if type(ops[i]) is Loss][0] try: last_idx = max(i for i in range(end_of_first_phase) if not type(ops[i]) is ForwardEnable) except ValueError: last_idx = -1 if last_idx == end_of_first_phase - 1: return (self, None) chain_length = ops[end_of_first_phase - 1].index ## Some assumption here about the sequence (finishes with Forward_L start_of_fwd_enable_chain = ops[last_idx + 1].index ## And starts with B_L), but should be fine in practice result = Sequence(Function("Strip", self.function.name, *self.function.args, start_of_fwd_enable_chain)) for i in range(last_idx + 1): result.insert(ops[i]) result.insert(Loss()) for i in range(chain_length, start_of_fwd_enable_chain - 1, -1): position = end_of_first_phase + 1 + (chain_length - i) assert type(ops[position]) is Backward assert ops[position].index == i for i in range(end_of_first_phase + 1 + 1 + chain_length - start_of_fwd_enable_chain, len(ops)): result.insert(ops[i]) return (result, start_of_fwd_enable_chain)
import math import sys from typing import List, Tuple from torch.fx import Node from colossalai.fx.codegen.activation_checkpoint_codegen import _find_nested_ckpt_regions from colossalai.fx.graph_module import ColoGraphModule from colossalai.fx.profiler import activation_size, calculate_fwd_out, calculate_fwd_tmp, parameter_size from colossalai.logging import get_dist_logger from .linearize import linearize from .operation import Backward, Chain, ForwardCheck, ForwardEnable, ForwardNograd, Function, Loss, Sequence # global vairable to indicate whether the solver is failed SOLVER_FAILED = False # this is the python compute table code from rotor # https://gitlab.inria.fr/hiepacs/rotor # paper link: https://hal.inria.fr/hal-02352969 def _compute_table(chain: Chain, mmax) -> Tuple: """Returns the optimal table: a tuple containing: Opt[m][lmin][lmax] with lmin = 0...chain.length and lmax = lmin...chain.length (lmax is not included) and m = 0...mmax what[m][lmin][lmax] is (True,) if the optimal choice is a chain checkpoint (False, j) if the optimal choice is a leaf checkpoint of length j The computation uses dynamic programming""" fw = chain.fweight + [0] ## forward time bw = chain.bweight ## backward time, not used cw = chain.cweight + [0] ## size of x (and of y) cbw = chain.cbweight + [0] ## size of xbar fwd_mem_tmp = chain.fwd_mem_tmp + [0] bwd_mem_tmp = chain.bwd_mem_tmp + [0] # Build table opt = [[{} for _ in range(chain.length + 1)] for _ in range(mmax + 1)] what = [[{} for _ in range(chain.length + 1)] for _ in range(mmax + 1)] # Last one is a dict because its indices go from i to l. Renumbering will wait for C implementation # Initialize borders of the tables for lmax-lmin = 0 for m in range(mmax + 1): for i in range(chain.length + 1): #lmax-lmin = 0 limit = max(cw[i + 1] + cbw[i + 1] + fwd_mem_tmp[i], cw[i + 1] + cbw[i + 1] + bwd_mem_tmp[i]) if m >= limit: ## Equation (1) opt[m][i][i] = fw[i] + bw[i] else: opt[m][i][i] = float("inf") # Compute everything for m in range(mmax + 1): for d in range(1, chain.length + 1): for i in range(chain.length + 1 - d): # for idx in range(i+1, chain.length + 1): idx = i + d mmin = cw[idx + 1] + cw[i + 1] + fwd_mem_tmp[i] if idx > i + 1: mmin = max(mmin, cw[idx + 1] + max(cw[j] + cw[j + 1] + fwd_mem_tmp[j] for j in range(i + 1, idx))) if m < mmin: opt[m][i][idx] = float("inf") else: leaf_checkpoints = [(j, sum(fw[i:j]) + opt[m - cw[j]][j][idx] + opt[m][i][j - 1]) for j in range(i + 1, idx + 1) if m >= cw[j]] if leaf_checkpoints: best_leaf = min(leaf_checkpoints, key=lambda t: t[1]) else: best_leaf = None if m >= cbw[i + 1]: chain_checkpoint = opt[m][i][i] + opt[m - cbw[i + 1]][i + 1][idx] else: chain_checkpoint = float("inf") if best_leaf and best_leaf[1] <= chain_checkpoint: opt[m][i][idx] = best_leaf[1] what[m][i][idx] = (False, best_leaf[0]) else: opt[m][i][idx] = chain_checkpoint what[m][i][idx] = (True,) return (opt, what) def _rec(chain: Chain, lmin, lmax, cmem, opt_table): """ chain : the class describing the AC graph lmin : index of the first forward to execute lmax : upper bound index of the last forward to execute (not included) cmem : number of available memory slots Return the optimal sequence of makespan Opt_hete[cmem][lmin][lmax-lmin]""" if cmem <= 0: raise ValueError("Can not process a chain with negative memory {cmem}".format(cmem=cmem)) opt, what = opt_table sequence = Sequence(Function("Persistent", lmax - lmin, cmem)) if opt[cmem][lmin][lmax] == float("inf"): # using logger to annonce that the solver is failed logger = get_dist_logger() logger.info("Can not process this chain from index {lmin} to {lmax} with memory {cmem}".format(lmin=lmin, lmax=lmax, cmem=cmem)) # set global indicater SOLVER_FAILED to True global SOLVER_FAILED SOLVER_FAILED = True return sequence if lmin == lmax: if lmin == chain.length: sequence.insert(Loss()) else: sequence.insert(ForwardEnable(lmin)) sequence.insert(Backward(lmin)) return sequence if what[cmem][lmin][lmax][0]: sequence.insert(ForwardEnable(lmin)) sequence.insert_sequence(_rec(chain, lmin + 1, lmax, cmem - chain.cbweight[lmin + 1], opt_table)) sequence.insert(Backward(lmin)) else: j = what[cmem][lmin][lmax][1] sequence.insert(ForwardCheck(lmin)) for k in range(lmin + 1, j): sequence.insert(ForwardNograd(k)) sequence.insert_sequence(_rec(chain, j, lmax, cmem - chain.cweight[j], opt_table)) sequence.insert_sequence(_rec(chain, lmin, j - 1, cmem, opt_table)) return sequence def _fwd_xbar(node: List[Node]) -> int: """Get the forward xbar of a node Args: node (List[Node]): List of torch.fx Node, indicates a node in linearized graph Returns: int: xbar size, unit Byte """ xbar = 0 for n in node: xbar += calculate_fwd_tmp(n) + calculate_fwd_out(n) return xbar def _fwd_time(node: List[Node]) -> int: """Get the foward time of a node Args: node (List[Node]): List of torch.fx Node, indicates a node in linearized graph Returns: int: foward time, extimated by flops count """ fwd_time = 0 for n in node: # minimum flop count is needed fwd_time += max(n.meta['fwd_flop'], 1) return fwd_time def _bwd_time(node: List[Node]) -> int: """Get the backward time of a node Args: node (List[Node]): List of torch.fx Node, indicates a node in linearized graph Returns: int: backward time, extimated by flops count """ bwd_time = 0 for n in node: # minimum flop count is needed bwd_time += max(n.meta['bwd_flop'], 1) return bwd_time def _get_fwd_mem_tmp(node: List[Node]) -> int: """Get the forward temp memory of a node This could be done by subtracting the saved activation from all output of a node Args: node (List[Node]): List of torch.fx Node, indicates a node in linearized graph Returns: int: forward temp memory, unit Byte """ n = node[-1] return activation_size(n.meta['fwd_out']) - calculate_fwd_out(n) def _get_bwd_mem_tmp(node: List[Node]) -> int: """Get the backward temp memory of a node Args: node (List[Node]): List of torch.fx Node, indicates a node in linearized graph Returns: int: backward temp memory, unit Byte """ def _get_deps_size(): deps_size = 0 for k, v in deps.items(): k: Node if v > 0: deps_size += k.meta['bwd_mem_out'] if v == float('-inf'): deps_size -= calculate_fwd_tmp(k) + calculate_fwd_out(k) return deps_size bwd_mem_tmp = 0 deps = {} for n in reversed(node): deps[n] = len(n.all_input_nodes) bwd_mem_tmp = max(bwd_mem_tmp, _get_deps_size() + n.meta['bwd_mem_tmp']) for child in n.users: if child in deps: deps[child] -= 1 if deps[child] <= 0: deps[child] = float('-inf') # free return bwd_mem_tmp def _construct_chain(node_list: List[List[Node]], input) -> Chain: fwd_time = [] bwd_time = [] xbar_sizes = [activation_size(input)] x_sizes = [activation_size(input)] tmp_fwd = [] tmp_bwd = [] for idx, node in enumerate(node_list): fwd_time.append(_fwd_time(node)) bwd_time.append(_bwd_time(node)) x_sizes.append(calculate_fwd_out(node[-1])) xbar_sizes.append(max(x_sizes[-1], _fwd_xbar(node))) tmp_fwd.append(_get_fwd_mem_tmp(node)) tmp_bwd.append(_get_bwd_mem_tmp(node)) bwd_time.append(0) # currently we view loss backward temp as zero tmp_bwd.append(0) return Chain(fwd_time, bwd_time, x_sizes, xbar_sizes, tmp_fwd, tmp_bwd) def _annotate_from_sequence(sequence: Sequence, node_list: List[List[Node]]): op_list = sequence.list_operations() loss_op = next(op for op in op_list if isinstance(op, Loss)) fwd_list = op_list[:op_list.index(loss_op)] bwd_list = op_list[op_list.index(loss_op) + 1:] ckpt_idx = 0 in_ckpt = False ckpt_region = [] # forward annotation for idx, op in enumerate(fwd_list, 0): if in_ckpt: if isinstance(op, ForwardNograd): ckpt_region.append(idx) elif isinstance(op, ForwardEnable): in_ckpt = False for node_idx in ckpt_region: for n in node_list[node_idx]: setattr(n, "activation_checkpoint", [ckpt_idx]) ckpt_idx += 1 ckpt_region = [] elif isinstance(op, ForwardCheck): for node_idx in ckpt_region: for n in node_list[node_idx]: setattr(n, "activation_checkpoint", [ckpt_idx]) ckpt_idx += 1 ckpt_region = [idx] else: if isinstance(op, ForwardCheck): in_ckpt = True ckpt_region.append(idx) # annotate the backward if there is any nested activation checkpoint in_recompute = False for op in bwd_list: if in_recompute: if isinstance(op, ForwardNograd): ckpt_region.append(op.index) elif isinstance(op, ForwardEnable): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) ckpt_idx += 1 ckpt_region = [] elif isinstance(op, ForwardCheck): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) ckpt_idx += 1 ckpt_region = [op.index] elif isinstance(op, Backward): for node_idx in ckpt_region: for n in node_list[node_idx]: n.activation_checkpoint.append(ckpt_idx) in_recompute = False else: if not isinstance(op, Backward): in_recompute = True ckpt_idx = 0 ckpt_region = [] if isinstance(op, ForwardCheck): ckpt_region.append(op.index) # postprocess, make sure every activation checkpoint label in the # same activation checkpoint region (level = 0) has the same length op_list = [] for node in node_list: op_list += node ckpt_regions = _find_nested_ckpt_regions(op_list) for (start_idx, end_idx) in ckpt_regions: nested_length = max(len(op_list[idx].activation_checkpoint) for idx in range(start_idx, end_idx + 1)) for idx in range(start_idx, end_idx + 1): op_list[idx].activation_checkpoint += [None] * (nested_length - len(op_list[idx].activation_checkpoint)) def solver_rotor(gm: ColoGraphModule, data, mem_limit: int, mem_slots: int = 500, cnode: List[str] = None, eps: float = 0.0, force_python: bool = False) -> ColoGraphModule: """solver that automatically find activation checkpoint in rotor's manner Args: gm (ColoGraphModule): ColoGraphModule generated by tracing model and MetaInfoProp. data (torch.Tensor): input data. mem_limit (int): memory budget in Byte. mem_slots (int, optional): number of slots for discretizing memory budget. Defaults to 500. cnode (List[Node], optional): common node list for linearize. Defaults to None. eps (float): epsilon for memory decay. Defaults to 0.0 force_python (bool): force to use python version of dynamic programs Returns: ColoGraphModule: annotated ColoGraphModuled with __sequence__ attribute """ # try to import C version solver if force_python is not set logger = get_dist_logger() if not force_python: try: from .dynamic_programs_C_version import persistent_compute_table CVERSION = True # build module if module not found except ModuleNotFoundError: import os import subprocess logger.info("dynamic_programs_C_version hasn't been built! Building library...", ranks=[0]) this_dir = os.path.dirname(os.path.abspath(__file__)) result = subprocess.Popen( [ f"{sys.executable}", f"{os.path.join(this_dir, 'build_c_ext.py')}", "build_ext", f"--build-lib={this_dir}" ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) if result.wait() == 0: logger.info("dynamic_programs_C_version has been built!", ranks=[0]) from .dynamic_programs_C_version import persistent_compute_table CVERSION = True else: logger.info("dynamic_programs_C_version built failed! Using python version!", ranks=[0]) CVERSION = False else: CVERSION = False # check if metainfoprop is done if any(len(node.meta) == 0 for node in gm.graph.nodes): raise RuntimeError( "Nodes meta information hasn't been prepared! Please run MetaInfoProp before calling solver!") # linearize the graph node_list = linearize(gm, cnode) # construct chain mem_unit = mem_limit * (1.0 - eps) // mem_slots chain: Chain = _construct_chain(node_list, data) chain._discretize(mem_unit) # use C version if possible if CVERSION and not force_python: logger.info("Using C version rotor solver!", ranks=[0]) opt_table = persistent_compute_table(chain, mem_slots) else: opt_table = _compute_table(chain, mem_slots) logger.info("Using python version rotor solver!", ranks=[0]) # found sequence sequence = _rec(chain, 0, chain.length, mem_slots - chain.cweight[0], opt_table) # if solver failed, we don't need to annotate the graph if not SOLVER_FAILED: _annotate_from_sequence(sequence, node_list) # set __sequence__ attribute to GraphModule if SOLVER_FAILED: setattr(gm, "__sequence__", None) else: setattr(gm, "__sequence__", sequence) # set __opttable__ attribute to GraphModule setattr(gm, "__opttable__", opt_table[0]) gm.recompile() return gm
import math from typing import List, Set, Tuple import torch from torch.fx import GraphModule, Node from colossalai.fx.profiler import calculate_fwd_in, calculate_fwd_tmp __all__ = ['chen_greedy'] CKPT_OP = ['call_module', 'call_method', 'call_function', 'get_attr'] def _all_potential_ckpt_nodes(gm: GraphModule) -> List: """ In most existing frameworks of activation checkpoint, the forward graph is assumed to be linearized. """ def is_sink(): """ If we can free all memories when executing a certain node, it is a sink. """ return not sum((v for k, v in deps.items())) deps = {} ckpt_nodes = [] for n in gm.graph.nodes: for n_par in n._input_nodes: deps[n_par] -= 1 # free memory and dependencies # We can only put act_ckpt on these nodes if n.op in CKPT_OP and is_sink(): ckpt_nodes.append(n) deps[n] = len(n.users) # add dependencies for future executions return ckpt_nodes def chen_greedy(gm: GraphModule) -> GraphModule: """ This is the simple implementation of Algorithm 3 in https://arxiv.org/abs/1604.06174. Note that this algorithm targets at memory optimization only, using techniques in appendix A. Usage: model = resnet18() input_sample = torch.rand(4, 3, 224, 224) gm = symbolic_trace(model) MetaInfoProp(gm).run(input_sample) gm = chen_greedy(gm) Args: gm (GraphModule): The module to add checkpoints """ def grid_search(num_grids: int = 6) -> Set: """ Search ckpt strategy with b = 0, then run the allocation algorithm again with b = √xy. Grid search over [√2/2 b, √2 b] for ckpt_opt over num_grids as in appendix A. """ _, b_approx = run_chen_greedy(0) b_min, b_max = math.floor(b_approx / math.sqrt(2)), math.ceil(b_approx * math.sqrt(2)) b_opt = math.inf for b in range(b_min, b_max, (b_max - b_min) // num_grids): ckpt_intv, b_approx = run_chen_greedy(b) if b_approx < b_opt: b_opt = b_approx ckpt_opt = ckpt_intv return ckpt_opt def run_chen_greedy(b: int = 0) -> Tuple[Set, int]: """ This is the simple implementation of Algorithm 3 in https://arxiv.org/abs/1604.06174. """ ckpt_nodes = _all_potential_ckpt_nodes(gm) ckpt_intv = [] temp = 0 x = 0 y = 0 prev_idx = 2 for (idx, n) in enumerate(gm.graph.nodes): n: Node temp += calculate_fwd_in(n) + calculate_fwd_tmp(n) y = max(y, temp) if temp > b and n in ckpt_nodes: x += calculate_fwd_in(n) temp = 0 ckpt_intv.append((prev_idx, idx + 1)) prev_idx = idx + 1 return ckpt_intv, math.floor(math.sqrt(x * y)) gm.graph.lint() # make sure nodes are in topological order ckpt = grid_search(num_grids=6) node_list = list(gm.graph.nodes) for i, seg in enumerate(ckpt): for idx in range(*seg): n = node_list[idx] if n.op in CKPT_OP: setattr(n, 'activation_checkpoint', i) gm.recompile() return gm
from typing import List, Any from torch.fx import GraphModule, Node from colossalai.fx.profiler import is_inplace # Common nodes are type of nodes that could be seen as attributes and remain # unchanged throughout the whole model, it will be used several times by # different blocks of model, so that it is hard for us to linearize the graph # when we encounter those kinds of nodes. We let users to annotate some of the # input as common node, such as attention mask, and the followings are some of # the ops that could actually be seen as common nodes. With our common node prop, # we could find some of the "real" common nodes (e.g. the real attention mask # used in BERT and GPT), the rule is simple, for node who's parents are all common # nodes or it's op belongs to the following operations, we view this node as a # newly born common node. # List of target name that could be seen as common node COPS = ["getattr", "getitem", "size"] def _is_cop(target: Any) -> bool: """Check if an op could be seen as common node Args: target (Any): node target Returns: bool """ if isinstance(target, str): return target in COPS else: return target.__name__ in COPS def linearize(gm: GraphModule, cnode: List[str] = None) -> List[List[Node]]: """Linearizing the graph Args: gm (GraphModule): GraphModule derived by tracing cnode (List[str], optional): common node List, should be the subset of input. Default to None. Returns: List[List[Node]]: List of list, each inside list of Node presents the actual 'node' in linearized manner. Remarks: We merge the inplace ops into the previous node. """ def _is_sink() -> bool: """Check if we can free all dependencies Returns: bool """ return not sum([v for _, v in deps.items()]) and not any(map(is_inplace, n.users)) # make sure that item in cnode is valid if cnode: for name in cnode: try: assert next(node for node in gm.graph.nodes if node.name == name).op == "placeholder", \ f"common node {name} is not an input of the model" except StopIteration: raise ValueError(f"common node name {name} not in graph") else: cnode = [] deps = {} linearized_nodes = [] region = [] for n in gm.graph.nodes: if n.op != "placeholder" and n.op != "output": for n_par in n._input_nodes: if n_par.op != "placeholder" and n_par.name not in cnode: deps[n_par] -= 1 region.append(n) # if the node could free all dependencies in graph # we could begin a new node if _is_sink(): linearized_nodes.append(region) region = [] # propagate common node attr if possible if len(n._input_nodes) == len([node for node in n._input_nodes if node.name in cnode]) or _is_cop(n.target): cnode.append(n.name) else: deps[n] = len([user for user in n.users if user.op != "output"]) return linearized_nodes
from setuptools import setup, Extension import os this_dir = os.path.dirname(os.path.abspath(__file__)) ext_modules = [Extension( 'dynamic_programs_C_version', sources=[os.path.join(this_dir, 'dynamic_programs.c')], )] setup( name='rotor c extension', version='0.1', description='rotor c extension for faster dp computing', ext_modules=ext_modules, )
from .activation_checkpoint_codegen import *
from typing import Any, Callable, Dict, Iterable, List, Tuple import torch import colossalai try: from torch.fx.graph import ( CodeGen, PythonCode, _custom_builtins, _CustomBuiltin, _format_target, _is_from_torch, _Namespace, _origin_type_map, inplace_methods, magic_methods, ) from torch.fx.node import Argument, Node, _get_qualified_name, _type_repr, map_arg CODEGEN_AVAILABLE = True except: from torch.fx.graph import ( PythonCode, _custom_builtins, _CustomBuiltin, _format_args, _format_target, _is_from_torch, _Namespace, _origin_type_map, magic_methods, ) from torch.fx.node import Argument, Node, _get_qualified_name, _type_repr, map_arg CODEGEN_AVAILABLE = False if CODEGEN_AVAILABLE: __all__ = ['ActivationCheckpointCodeGen'] else: __all__ = ['python_code_with_activation_checkpoint'] def _gen_saved_tensors_hooks(): """ Generate saved tensors hooks """ pack_hook = """def pack_hook_input(self, x): if getattr(x, "offload", False): return (x.device, x.cpu()) else: return x def pack_hook_no_input(self, x): if getattr(x, "offload", True): return (x.device, x.cpu()) else: return x """ unpack_hook = """def unpack_hook(self, packed): if isinstance(packed, tuple): device, tensor = packed return tensor.to(device) else: return packed """ return pack_hook, unpack_hook def _gen_save_tensors_hooks_context(offload_input=True) -> str: """Generate customized saved_tensors_hooks Args: offload_input (bool, optional): whether we need offload input, if offload_input=False, we will use self.pack_hook_no_input instead. Defaults to True. Returns: str: generated context """ if offload_input: context = "with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_input, self.unpack_hook):\n" else: context = "with torch.autograd.graph.saved_tensors_hooks(self.pack_hook_no_input, self.unpack_hook):\n" return context def _gen_save_on_cpu_context(): """ Generate save on cpu context """ context = "with torch.autograd.graph.save_on_cpu(pin_memory=True):\n" return context def _find_input_and_output_nodes(nodes: List[Node]): """ Find the input and output node names which are not found in the given list of nodes. """ input_nodes = [] output_nodes = [] # if a node has an input node which is not in the node list # we treat that input node as the input of the checkpoint function for node in nodes: for input_node in node._input_nodes.keys(): node_repr = repr(input_node) if input_node not in nodes and node_repr not in input_nodes: input_nodes.append(node_repr) # if a node has a user node which is not in the node list # we treat that user node as the node receiving the current node output for node in nodes: for output_node in node.users.keys(): node_repr = repr(node) if output_node not in nodes and node_repr not in output_nodes: output_nodes.append(node_repr) return input_nodes, output_nodes def _find_ckpt_regions(nodes: List[Node]): """ Find the checkpoint regions given a list of consecutive nodes. The outputs will be list of tuples, each tuple is in the form of (start_index, end_index). """ ckpt_nodes = [] ckpt_regions = [] start = -1 end = -1 current_region = None for idx, node in enumerate(nodes): if 'activation_checkpoint' in node.meta: act_ckpt_label = node.meta['activation_checkpoint'] # this activation checkpoint label is not set yet # meaning this is the first node of the activation ckpt region if current_region is None: current_region = act_ckpt_label start = idx # if activation checkpoint has changed # we restart the tracking # e.g. node ckpt states = [ckpt1, ckpt2, ckpt2, ckpt2] if act_ckpt_label != current_region: assert start != -1 ckpt_regions.append((start, idx - 1)) current_region = act_ckpt_label start = idx end = -1 elif current_region is not None and not 'activation_checkpoint' in node.meta: # used to check the case below # node ckpt states = [ckpt, ckpt, non-ckpt] end = idx - 1 assert start != -1 and end != -1 ckpt_regions.append((start, end)) start = end = -1 current_region = None else: pass return ckpt_regions def _find_offload_regions(nodes: List[Node]): """This function is to find the offload regions In pofo algorithm, during annotation, we will annotate the offload region with the list in the form of [idx, offload_input, offload_bar]. idx indicates the offload region's index, offload_input is a bool type indicates whether we need to offload the input, offload_bar is a bool type indicates whether we need to offload all the intermediate x_bars of this region. """ offload_regions = [] offload_labels = [] start = -1 end = -1 current_region = None for idx, node in enumerate(nodes): if 'activation_offload' in node.meta and isinstance(node.meta['activation_offload'], Iterable): act_offload_label = node.meta['activation_offload'] if current_region == None: current_region = act_offload_label start = idx offload_labels.append(act_offload_label) if act_offload_label != current_region: assert start != -1 offload_regions.append((start, idx - 1)) offload_labels.append(act_offload_label) current_region = act_offload_label start = idx end = -1 else: if current_region is not None: end = idx - 1 assert start != -1 and end != -1 offload_regions.append((start, end)) start = end = -1 current_region = None else: pass return offload_regions, offload_labels def _gen_ckpt_fn_def(label, free_vars: List[str]) -> str: """ Generate the checkpoint function definition """ return f"def checkpoint_{label}({', '.join(['self'] + free_vars)}):" def _gen_ckpt_output(output_vars: List[str]) -> str: """ Generate the return statement for checkpoint region """ return f"return {', '.join(output_vars)}" def _gen_ckpt_usage(label, activation_offload, input_vars, output_vars, use_reentrant=True): """ Generate the checkpoint function call code text """ outputs = ', '.join(output_vars) inputs = ', '.join(input_vars) return f'{outputs} = colossalai.utils.activation_checkpoint.checkpoint(self.checkpoint_{label}, {activation_offload}, {inputs}, use_reentrant={use_reentrant})' def _end_of_ckpt(node: Node, check_idx: int) -> bool: """Check if the node could end the ckpt region Args: node (Node): torch.fx.Node check_idx (int): the index of checkpoint level for nested checkpoint Returns: bool """ if 'activation_checkpoint' in node.meta: if isinstance(node.meta['activation_checkpoint'], list): return node.meta['activation_checkpoint'][check_idx] == None else: return False else: return True def _find_nested_ckpt_regions(nodes, check_idx=0): """ Find the nested checkpoint regions given a list of consecutive nodes. The outputs will be list of tuples, each tuple is in the form of (start_index, end_index). """ ckpt_regions = [] start = -1 end = -1 current_region = None for idx, node in enumerate(nodes): if 'activation_checkpoint' in node.meta: if isinstance(node.meta['activation_checkpoint'], int): act_ckpt_label = node.meta['activation_checkpoint'] else: act_ckpt_label = node.meta['activation_checkpoint'][check_idx] # this activation checkpoint label is not set yet # meaning this is the first node of the activation ckpt region if current_region is None: current_region = act_ckpt_label start = idx # if activation checkpoint has changed # we restart the tracking # e.g. node ckpt states = [ckpt1, ckpt2, ckpt2, ckpt2] if act_ckpt_label != current_region: assert start != -1 ckpt_regions.append((start, idx - 1)) current_region = act_ckpt_label start = idx end = -1 elif current_region is not None and _end_of_ckpt(node, check_idx): # used to check the case below # node ckpt states = [ckpt, ckpt, non-ckpt] end = idx - 1 assert start != -1 and end != -1 ckpt_regions.append((start, end)) start = end = -1 current_region = None else: pass if current_region is not None: end = len(nodes) - 1 ckpt_regions.append((start, end)) return ckpt_regions def emit_ckpt_func(body, ckpt_func, node_list: List[Node], emit_node_func, delete_unused_value_func, level=0, in_ckpt=False): """Emit ckpt fuction in nested way Args: body: forward code, in recursive calls, this part will be checkpoint functions code ckpt_func: checkpoint functions code, in recursive calls, this part will be a buffer node_list (List[Node]): list of torch.fx.Node emit_node_func: function to emit a node delete_unused_value_func: function to delete unused value level (int, optional): checkpoint level. Defaults to 0. in_ckpt (bool, optional): indicates wether the func is in recursive call. Defaults to False. """ inputs, outputs = _find_input_and_output_nodes(node_list) # if the current checkpoint function use int as label, using old generation method if isinstance(node_list[0].meta['activation_checkpoint'], int): label = node_list[0].meta['activation_checkpoint'] ckpt_fn_def = _gen_ckpt_fn_def(label, inputs) ckpt_func.append(f'{ckpt_fn_def}\n') for node in node_list: emit_node_func(node, ckpt_func) ckpt_func[-1] = ' ' + ckpt_func[-1] delete_unused_value_func(node, ckpt_func) ckpt_func.append(' ' + _gen_ckpt_output(outputs) + '\n\n') activation_offload = node_list[0].meta.get('activation_offload', False) usage = _gen_ckpt_usage(label, activation_offload, inputs, outputs, False) usage += "\n" body.append(usage) # use nested ckpt function codegen else: # label given by each layer, e.g. if you are currently at level [0, 1, 1] # the label will be '0_1_1' label = "_".join([str(idx) for idx in node_list[0].meta['activation_checkpoint'][:level + 1]]) ckpt_fn_def = _gen_ckpt_fn_def(label, inputs) ckpt_func.append(f'{ckpt_fn_def}\n') # if there is more level to fetch if level + 1 < len(node_list[0].meta['activation_checkpoint']): ckpt_regions = _find_nested_ckpt_regions(node_list, level + 1) start_idx = [item[0] for item in ckpt_regions] end_idx = [item[1] for item in ckpt_regions] # use ckpt_func_buffer to store nested checkpoint functions ckpt_func_buffer = [] node_idx = 0 while 1: if node_idx >= len(node_list): break if node_idx in start_idx: ckpt_node_list = node_list[node_idx:end_idx[start_idx.index(node_idx)] + 1] emit_ckpt_func(ckpt_func, ckpt_func_buffer, ckpt_node_list, emit_node_func, delete_unused_value_func, level + 1, True) node_idx += len(ckpt_node_list) else: node = node_list[node_idx] emit_node_func(node, ckpt_func) ckpt_func[-1] = ' ' + ckpt_func[-1] delete_unused_value_func(node, ckpt_func) node_idx += 1 ckpt_func.append(' ' + _gen_ckpt_output(outputs) + '\n\n') ckpt_func += ckpt_func_buffer activation_offload = node_list[0].meta.get('activation_offload', False) usage = _gen_ckpt_usage(label, activation_offload, inputs, outputs, False) + '\n' if in_ckpt: usage = ' ' + usage body.append(usage) # last level else: for node in node_list: emit_node_func(node, ckpt_func) ckpt_func[-1] = ' ' + ckpt_func[-1] delete_unused_value_func(node, ckpt_func) ckpt_func.append(' ' + _gen_ckpt_output(outputs) + '\n\n') activation_offload = node_list[0].meta.get('activation_offload', False) usage = _gen_ckpt_usage(label, activation_offload, inputs, outputs, False) + '\n' if in_ckpt: usage = ' ' + usage body.append(usage) def emit_code_with_nested_activation_checkpoint(body, ckpt_func, nodes, emit_node_func, delete_unused_value_func): """Emit code with nested activation checkpoint When we detect some of the node.activation_checkpoint is a List, we will use this function to emit the activation checkpoint codes. Args: body: forward code ckpt_func: checkpoint functions code nodes: graph.nodes emit_node_func: function to emit node delete_unused_value_func: function to remove the unused value """ ckpt_regions = _find_nested_ckpt_regions(nodes, 0) start_idx = [item[0] for item in ckpt_regions] end_idx = [item[1] for item in ckpt_regions] # find the offload regions offload_regions, offload_labels = _find_offload_regions(nodes) offload_starts = [item[0] for item in offload_regions] offload_ends = [item[1] for item in offload_regions] offload_inputs = [] offload_outputs = [] within_offload_region = False node_list = list(nodes) # find the input and output var names for each offload region for idx, (start, end) in enumerate(offload_regions): offload_node_list = node_list[start:end + 1] inputs, outputs = _find_input_and_output_nodes(offload_node_list) offload_inputs.append(inputs) offload_outputs.append(outputs) # this flag is to prevent repeated insert of save tensors # hooks definition in ckpt_func is_hook_inserted = False node_idx = 0 while 1: # break if we finish the processing all the nodes if node_idx >= len(node_list): break # process ckpt_regions if node_idx in start_idx: ckpt_node_list = node_list[node_idx:end_idx[start_idx.index(node_idx)] + 1] emit_ckpt_func(body, ckpt_func, ckpt_node_list, emit_node_func, delete_unused_value_func) node_idx += len(ckpt_node_list) # process node in forward function else: node = node_list[node_idx] if node_idx in offload_starts: offload_label = offload_labels[offload_starts.index(node_idx)] _, offload_input, offload_bar = offload_label within_offload_region = True # insert hook functions if needed if not is_hook_inserted: pack_hook, unpack_hook = _gen_saved_tensors_hooks() ckpt_func.insert(0, "\n".join([pack_hook, unpack_hook]) + "\n") is_hook_inserted = True if offload_input and offload_bar: body.append(_gen_save_on_cpu_context()) elif offload_input: for par in offload_inputs[offload_label[0]]: body.append(f"setattr({par}, 'offload', True)\n") body.append(_gen_save_tensors_hooks_context(offload_input=True)) else: for par in offload_inputs[offload_label[0]]: body.append(f"setattr({par}, 'offload', False)\n") body.append(_gen_save_tensors_hooks_context(offload_input=False)) if within_offload_region: emit_node_func(node, body) body[-1] = ' ' + body[-1] delete_unused_value_func(node, body) else: emit_node_func(node, body) delete_unused_value_func(node, body) if node_idx in offload_ends: within_offload_region = False node_idx += 1 def emit_code_with_activation_checkpoint(body, ckpt_func, nodes, emit_node_func, delete_unused_value_func): # find the activation checkpoint regions ckpt_regions = _find_ckpt_regions(nodes) start_idx = [item[0] for item in ckpt_regions] end_idx = [item[1] for item in ckpt_regions] input_vars = [] output_vars = [] within_ckpt_region = False # find the offload regions offload_regions, offload_labels = _find_offload_regions(nodes) offload_starts = [item[0] for item in offload_regions] offload_ends = [item[1] for item in offload_regions] offload_inputs = [] offload_outputs = [] within_offload_region = False node_list = list(nodes) # use this variable to avoid inserting hook functions # to ckpt_func repeatedly is_hook_inserted = False # find the input and output var names for each region for idx, (start, end) in enumerate(ckpt_regions): ckpt_node_list = node_list[start:end + 1] inputs, outputs = _find_input_and_output_nodes(ckpt_node_list) input_vars.append(inputs) output_vars.append(outputs) # find the input and output var names for each offload region for idx, (start, end) in enumerate(offload_regions): offload_node_list = node_list[start:end + 1] inputs, outputs = _find_input_and_output_nodes(offload_node_list) offload_inputs.append(inputs) offload_outputs.append(outputs) # append code text to body for idx, node in enumerate(node_list): # if this is the first node of the ckpt region # append the ckpt function defition if idx in start_idx: label = start_idx.index(idx) ckpt_fn_def = _gen_ckpt_fn_def(label, input_vars[label]) ckpt_func.append(f'{ckpt_fn_def}\n') within_ckpt_region = True if idx in offload_starts: offload_label = offload_labels[offload_starts.index(idx)] _, offload_input, offload_bar = offload_label within_offload_region = True # insert hook functions if needed if not is_hook_inserted: pack_hook, unpack_hook = _gen_saved_tensors_hooks() ckpt_func.insert(0, "\n".join([pack_hook, unpack_hook]) + "\n") is_hook_inserted = True if offload_input and offload_bar: body.append(_gen_save_on_cpu_context()) elif offload_input: for par in offload_inputs[offload_label[0]]: body.append(f"setattr({par}, 'offload', True)\n") body.append(_gen_save_tensors_hooks_context(offload_input=True)) else: for par in offload_inputs[offload_label[0]]: body.append(f"setattr({par}, 'offload', False)\n") body.append(_gen_save_tensors_hooks_context(offload_input=False)) # NOTE: emit_node does not emit a string with newline. It depends # on delete_unused_values to append one # NOTE: currently we separate body and ckpt_func definition if within_ckpt_region: emit_node_func(node, ckpt_func) ckpt_func[-1] = ' ' + ckpt_func[-1] delete_unused_value_func(node, ckpt_func) elif within_offload_region: emit_node_func(node, body) body[-1] = ' ' + body[-1] delete_unused_value_func(node, body) else: emit_node_func(node, body) delete_unused_value_func(node, body) if idx in end_idx: # if this is the last node of the ckpt region # generate return statement label = end_idx.index(idx) return_statement = _gen_ckpt_output(output_vars[label]) return_statement = f' {return_statement}\n\n' ckpt_func.append(return_statement) # we need to check if the checkpoint need to offload the input start_node_idx = start_idx[label] if 'activation_offload' in node_list[start_node_idx].meta: activation_offload = node_list[start_node_idx].meta['activation_offload'] else: activation_offload = False # we need to check if the checkpoint need use_reentrant=False use_reentrant = True non_leaf_input = 0 for var in input_vars[label]: input_node = next(item for item in node_list if item.name == var) if input_node.op != "placeholder": non_leaf_input = 1 for user in input_node.users: if 'activation_checkpoint' in user.meta: if user.meta['activation_checkpoint'] == label: if user.op == "call_module": if hasattr(user.graph.owning_module.get_submodule(user.target), "inplace"): use_reentrant = not user.graph.owning_module.get_submodule(user.target).inplace elif user.op == "call_function": if "inplace" in user.kwargs: use_reentrant = not user.kwargs["inplace"] # if all the inputs are leaf nodes, we need to set use_reentrant = False if not non_leaf_input: use_reentrant = False # generate checkpoint function call in a new line usage = _gen_ckpt_usage(label, activation_offload, input_vars[label], output_vars[label], use_reentrant) usage += '\n' body.append(usage) within_ckpt_region = False if idx in offload_ends: within_offload_region = False if CODEGEN_AVAILABLE: class ActivationCheckpointCodeGen(CodeGen): def _gen_python_code(self, nodes, root_module: str, namespace: _Namespace) -> PythonCode: free_vars: List[str] = [] body: List[str] = [] globals_: Dict[str, Any] = {} wrapped_fns: Dict[str, None] = {} # Wrap string in list to pass by reference maybe_return_annotation: List[str] = [''] def add_global(name_hint: str, obj: Any): """Add an obj to be tracked as a global. We call this for names that reference objects external to the Graph, like functions or types. Returns: the global name that should be used to reference 'obj' in generated source. """ if _is_from_torch(obj) and obj != torch.device: # to support registering torch.device # HACK: workaround for how torch custom ops are registered. We # can't import them like normal modules so they must retain their # fully qualified name. return _get_qualified_name(obj) # normalize the name hint to get a proper identifier global_name = namespace.create_name(name_hint, obj) if global_name in globals_: assert globals_[global_name] is obj return global_name globals_[global_name] = obj return global_name # set _custom_builtins here so that we needn't import colossalai in forward _custom_builtins["colossalai"] = _CustomBuiltin("import colossalai", colossalai) # Pre-fill the globals table with registered builtins. for name, (_, obj) in _custom_builtins.items(): add_global(name, obj) def type_repr(o: Any): if o == (): # Empty tuple is used for empty tuple type annotation Tuple[()] return '()' typename = _type_repr(o) if hasattr(o, '__origin__'): # This is a generic type, e.g. typing.List[torch.Tensor] origin_type = _origin_type_map.get(o.__origin__, o.__origin__) origin_typename = add_global(_type_repr(origin_type), origin_type) if hasattr(o, '__args__'): # Assign global names for each of the inner type variables. args = [type_repr(arg) for arg in o.__args__] if len(args) == 0: # Bare type, such as `typing.Tuple` with no subscript # This code-path used in Python < 3.9 return origin_typename return f'{origin_typename}[{",".join(args)}]' else: # Bare type, such as `typing.Tuple` with no subscript # This code-path used in Python 3.9+ return origin_typename # Common case: this is a regular module name like 'foo.bar.baz' return add_global(typename, o) def _format_args(args: Tuple[Argument, ...], kwargs: Dict[str, Argument]) -> str: def _get_repr(arg): # Handle NamedTuples (if it has `_fields`) via add_global. if isinstance(arg, tuple) and hasattr(arg, '_fields'): qualified_name = _get_qualified_name(type(arg)) global_name = add_global(qualified_name, type(arg)) return f"{global_name}{repr(tuple(arg))}" return repr(arg) args_s = ', '.join(_get_repr(a) for a in args) kwargs_s = ', '.join(f'{k} = {_get_repr(v)}' for k, v in kwargs.items()) if args_s and kwargs_s: return f'{args_s}, {kwargs_s}' return args_s or kwargs_s # Run through reverse nodes and record the first instance of a use # of a given node. This represents the *last* use of the node in the # execution order of the program, which we will use to free unused # values node_to_last_use: Dict[Node, Node] = {} user_to_last_uses: Dict[Node, List[Node]] = {} def register_last_uses(n: Node, user: Node): if n not in node_to_last_use: node_to_last_use[n] = user user_to_last_uses.setdefault(user, []).append(n) for node in reversed(nodes): map_arg(node.args, lambda n: register_last_uses(n, node)) map_arg(node.kwargs, lambda n: register_last_uses(n, node)) # NOTE: we add a variable to distinguish body and ckpt_func def delete_unused_values(user: Node, body): """ Delete values after their last use. This ensures that values that are not used in the remainder of the code are freed and the memory usage of the code is optimal. """ if user.op == 'placeholder': return if user.op == 'output': body.append('\n') return nodes_to_delete = user_to_last_uses.get(user, []) if len(nodes_to_delete): to_delete_str = ' = '.join([repr(n) for n in nodes_to_delete] + ['None']) body.append(f'; {to_delete_str}\n') else: body.append('\n') # NOTE: we add a variable to distinguish body and ckpt_func def emit_node(node: Node, body): maybe_type_annotation = '' if node.type is None else f' : {type_repr(node.type)}' if node.op == 'placeholder': assert isinstance(node.target, str) maybe_default_arg = '' if not node.args else f' = {repr(node.args[0])}' free_vars.append(f'{node.target}{maybe_type_annotation}{maybe_default_arg}') raw_name = node.target.replace('*', '') if raw_name != repr(node): body.append(f'{repr(node)} = {raw_name}\n') return elif node.op == 'call_method': assert isinstance(node.target, str) body.append( f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]), node.target)}' f'({_format_args(node.args[1:], node.kwargs)})') return elif node.op == 'call_function': assert callable(node.target) # pretty print operators if node.target.__module__ == '_operator' and node.target.__name__ in magic_methods: assert isinstance(node.args, tuple) body.append(f'{repr(node)}{maybe_type_annotation} = ' f'{magic_methods[node.target.__name__].format(*(repr(a) for a in node.args))}') return # pretty print inplace operators; required for jit.script to work properly # not currently supported in normal FX graphs, but generated by torchdynamo if node.target.__module__ == '_operator' and node.target.__name__ in inplace_methods: body.append(f'{inplace_methods[node.target.__name__].format(*(repr(a) for a in node.args))}; ' f'{repr(node)}{maybe_type_annotation} = {repr(node.args[0])}') return qualified_name = _get_qualified_name(node.target) global_name = add_global(qualified_name, node.target) # special case for getattr: node.args could be 2-argument or 3-argument # 2-argument: attribute access; 3-argument: fall through to attrib function call with default value if global_name == 'getattr' and \ isinstance(node.args, tuple) and \ isinstance(node.args[1], str) and \ node.args[1].isidentifier() and \ len(node.args) == 2: body.append( f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]), node.args[1])}') return body.append( f'{repr(node)}{maybe_type_annotation} = {global_name}({_format_args(node.args, node.kwargs)})') if node.meta.get('is_wrapped', False): wrapped_fns.setdefault(global_name) return elif node.op == 'call_module': assert isinstance(node.target, str) body.append(f'{repr(node)}{maybe_type_annotation} = ' f'{_format_target(root_module, node.target)}({_format_args(node.args, node.kwargs)})') return elif node.op == 'get_attr': assert isinstance(node.target, str) body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(root_module, node.target)}') return elif node.op == 'output': if node.type is not None: maybe_return_annotation[0] = f" -> {type_repr(node.type)}" body.append(self.generate_output(node.args[0])) return raise NotImplementedError(f'node: {node.op} {node.target}') # Modified for activation checkpointing ckpt_func = [] # if any node has a list of labels for activation_checkpoint, we # will use nested type of activation checkpoint codegen if any(isinstance(node.meta.get('activation_checkpoint', None), Iterable) for node in nodes): emit_code_with_nested_activation_checkpoint(body, ckpt_func, nodes, emit_node, delete_unused_values) else: emit_code_with_activation_checkpoint(body, ckpt_func, nodes, emit_node, delete_unused_values) if len(body) == 0: # If the Graph has no non-placeholder nodes, no lines for the body # have been emitted. To continue to have valid Python code, emit a # single pass statement body.append('pass\n') if len(wrapped_fns) > 0: wrap_name = add_global('wrap', torch.fx.wrap) wrap_stmts = '\n'.join([f'{wrap_name}("{name}")' for name in wrapped_fns]) else: wrap_stmts = '' if self._body_transformer: body = self._body_transformer(body) for name, value in self.additional_globals(): add_global(name, value) # as we need colossalai.utils.checkpoint, we need to import colossalai # in forward function prologue = self.gen_fn_def(free_vars, maybe_return_annotation[0]) prologue = ''.join(ckpt_func) + prologue prologue = prologue code = ''.join(body) code = '\n'.join(' ' + line for line in code.split('\n')) fn_code = f""" {wrap_stmts} {prologue} {code}""" return PythonCode(fn_code, globals_) else: def python_code_with_activation_checkpoint(self, root_module: str, namespace: _Namespace) -> PythonCode: """ This method is copied from the _python_code of torch.fx.graph.Graph. Modifications are made so that it can generate code for activation checkpoint. """ free_vars: List[str] = [] body: List[str] = [] globals_: Dict[str, Any] = {} wrapped_fns: Dict[str, None] = {} # Wrap string in list to pass by reference maybe_return_annotation: List[str] = [''] def add_global(name_hint: str, obj: Any): """Add an obj to be tracked as a global. We call this for names that reference objects external to the Graph, like functions or types. Returns: the global name that should be used to reference 'obj' in generated source. """ if _is_from_torch(obj) and obj != torch.device: # to support registering torch.device # HACK: workaround for how torch custom ops are registered. We # can't import them like normal modules so they must retain their # fully qualified name. return _get_qualified_name(obj) # normalize the name hint to get a proper identifier global_name = namespace.create_name(name_hint, obj) if global_name in globals_: assert globals_[global_name] is obj return global_name globals_[global_name] = obj return global_name # set _custom_builtins here so that we needn't import colossalai in forward _custom_builtins["colossalai"] = _CustomBuiltin("import colossalai", colossalai) # Pre-fill the globals table with registered builtins. for name, (_, obj) in _custom_builtins.items(): add_global(name, obj) def type_repr(o: Any): if o == (): # Empty tuple is used for empty tuple type annotation Tuple[()] return '()' typename = _type_repr(o) # This is a generic type, e.g. typing.List[torch.Tensor] if hasattr(o, '__origin__'): origin_type = _origin_type_map.get(o.__origin__, o.__origin__) origin_typename = add_global(_type_repr(origin_type), origin_type) # Assign global names for each of the inner type variables. args = [type_repr(arg) for arg in o.__args__] return f'{origin_typename}[{",".join(args)}]' # Common case: this is a regular module name like 'foo.bar.baz' return add_global(typename, o) # Run through reverse nodes and record the first instance of a use # of a given node. This represents the *last* use of the node in the # execution order of the program, which we will use to free unused # values node_to_last_use: Dict[Node, Node] = {} user_to_last_uses: Dict[Node, List[Node]] = {} def register_last_uses(n: Node, user: Node): if n not in node_to_last_use: node_to_last_use[n] = user user_to_last_uses.setdefault(user, []).append(n) for node in reversed(self.nodes): map_arg(node.args, lambda n: register_last_uses(n, node)) map_arg(node.kwargs, lambda n: register_last_uses(n, node)) # NOTE: we add a variable to distinguish body and ckpt_func def delete_unused_values(user: Node, body): """ Delete values after their last use. This ensures that values that are not used in the remainder of the code are freed and the memory usage of the code is optimal. """ if user.op == 'placeholder': return if user.op == 'output': body.append('\n') return nodes_to_delete = user_to_last_uses.get(user, []) if len(nodes_to_delete): to_delete_str = ' = '.join([repr(n) for n in nodes_to_delete] + ['None']) body.append(f'; {to_delete_str}\n') else: body.append('\n') # NOTE: we add a variable to distinguish body and ckpt_func def emit_node(node: Node, body): maybe_type_annotation = '' if node.type is None else f' : {type_repr(node.type)}' if node.op == 'placeholder': assert isinstance(node.target, str) maybe_default_arg = '' if not node.args else f' = {repr(node.args[0])}' free_vars.append(f'{node.target}{maybe_type_annotation}{maybe_default_arg}') raw_name = node.target.replace('*', '') if raw_name != repr(node): body.append(f'{repr(node)} = {raw_name}\n') return elif node.op == 'call_method': assert isinstance(node.target, str) body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]), node.target)}' f'({_format_args(node.args[1:], node.kwargs)})') return elif node.op == 'call_function': assert callable(node.target) # pretty print operators if node.target.__module__ == '_operator' and node.target.__name__ in magic_methods: assert isinstance(node.args, tuple) body.append(f'{repr(node)}{maybe_type_annotation} = ' f'{magic_methods[node.target.__name__].format(*(repr(a) for a in node.args))}') return qualified_name = _get_qualified_name(node.target) global_name = add_global(qualified_name, node.target) # special case for getattr: node.args could be 2-argument or 3-argument # 2-argument: attribute access; 3-argument: fall through to attrib function call with default value if global_name == 'getattr' and \ isinstance(node.args, tuple) and \ isinstance(node.args[1], str) and \ node.args[1].isidentifier() and \ len(node.args) == 2: body.append( f'{repr(node)}{maybe_type_annotation} = {_format_target(repr(node.args[0]), node.args[1])}') return body.append( f'{repr(node)}{maybe_type_annotation} = {global_name}({_format_args(node.args, node.kwargs)})') if node.meta.get('is_wrapped', False): wrapped_fns.setdefault(global_name) return elif node.op == 'call_module': assert isinstance(node.target, str) body.append(f'{repr(node)}{maybe_type_annotation} = ' f'{_format_target(root_module, node.target)}({_format_args(node.args, node.kwargs)})') return elif node.op == 'get_attr': assert isinstance(node.target, str) body.append(f'{repr(node)}{maybe_type_annotation} = {_format_target(root_module, node.target)}') return elif node.op == 'output': if node.type is not None: maybe_return_annotation[0] = f" -> {type_repr(node.type)}" if self._pytree_info is None: body.append(f'return {repr(node.args[0])}') else: body.append(f'return pytree.tree_unflatten({repr(node.args[0])}, self._out_spec)') return raise NotImplementedError(f'node: {node.op} {node.target}') # Modified for activation checkpointing ckpt_func = [] # if any node has a list of labels for activation_checkpoint, we # will use nested type of activation checkpoint codegen if any(isinstance(node.meta.get('activation_checkpoint', None), Iterable) for node in self.nodes): emit_code_with_nested_activation_checkpoint(body, ckpt_func, self.nodes, emit_node, delete_unused_values) else: emit_code_with_activation_checkpoint(body, ckpt_func, self.nodes, emit_node, delete_unused_values) if len(body) == 0: # If the Graph has no non-placeholder nodes, no lines for the body # have been emitted. To continue to have valid Python code, emit a # single pass statement body.append('pass\n') if self._pytree_info is not None: orig_args = self._pytree_info.orig_args has_orig_self = (orig_args[0] == 'self') if has_orig_self: free_vars.insert(0, 'self') if len(free_vars) > 0: # pytree has placeholders in it body.insert( 0, f"{', '.join(free_vars)}, = fx_pytree.tree_flatten_spec([{', '.join(orig_args)}], self._in_spec)\n") else: orig_args = free_vars if len(wrapped_fns) > 0: wrap_name = add_global('wrap', torch.fx.wrap) wrap_stmts = '\n'.join([f'{wrap_name}("{name}")' for name in wrapped_fns]) else: wrap_stmts = '' ckpt_func = ''.join(ckpt_func) # If the original function didn't have self as its first argument, we # would have added it. if len(orig_args) == 0 or orig_args[0] != 'self': orig_args.insert(0, 'self') code = ''.join(body) code = '\n'.join(' ' + line for line in code.split('\n')) # as we need colossalai.utils.checkpoint, we need to import colossalai # in forward function fn_code = f""" {wrap_stmts} {ckpt_func} def forward({', '.join(orig_args)}){maybe_return_annotation[0]}: {code}""" return PythonCode(fn_code, globals_)
import torch __all__ = ['ALIAS_ATEN', 'INPLACE_NEW', 'INPLACE_MATH_ATEN', 'CLONE_ATEN', 'RELU_LIKE_OPS', 'RELU_LIKE_MOD'] aten = torch.ops.aten ALIAS_ATEN = [ aten.detach.default, aten.t.default, aten.transpose.int, aten.view.default, aten._unsafe_view.default, aten._reshape_alias.default, ] INPLACE_NEW = [ aten.empty_like.default, aten.new_empty_strided.default, ] INPLACE_MATH_ATEN = [ aten.add_.Tensor, aten.sub_.Tensor, aten.div_.Tensor, aten.div_.Scalar, aten.mul_.Tensor, aten.bernoulli_.float, ] CLONE_ATEN = [ aten.clone.default, ] # See illustrations in # https://github.com/hpcaitech/ColossalAI/blob/main/colossalai/fx/profiler/constants.py OUTPUT_SAVED_OPS = [ torch.nn.functional.relu, torch.nn.functional.softmax, ] OUTPUT_SAVED_MOD = [ torch.nn.ReLU, torch.nn.Softmax, ]
from .._compatibility import is_compatible_with_meta if is_compatible_with_meta(): from .opcount import flop_mapping from .profiler import profile_function, profile_method, profile_module from .shard_utils import ( calculate_bwd_time, calculate_fwd_in, calculate_fwd_out, calculate_fwd_time, calculate_fwd_tmp, ) from .tensor import MetaTensor else: from .experimental import meta_profiler_function, meta_profiler_module, profile_function, profile_method, profile_module, calculate_fwd_in, calculate_fwd_tmp, calculate_fwd_out from .dataflow import GraphInfo from .memory_utils import activation_size, is_inplace, parameter_size
import uuid import torch from torch.types import _bool, _device, _dtype from torch.utils._pytree import tree_flatten, tree_map from .._compatibility import compatibility from .constants import ALIAS_ATEN __all__ = ['MetaTensor'] def set_data_ptr(x): if isinstance(x, torch.Tensor): if not x.data_ptr(): data_ptr = uuid.uuid4() x.data_ptr = lambda: data_ptr @compatibility(is_backward_compatible=False) class MetaTensor(torch.Tensor): """ A wrapping tensor that hacks `torch.autograd` without patching more `torch.ops.aten` ops. `fake_device` is the device that `MetaTensor` is supposed to run on. """ _tensor: torch.Tensor @staticmethod def __new__(cls, elem, fake_device=None): # Avoid multiple wrapping if isinstance(elem, MetaTensor): fake_device = elem.device if fake_device is None else fake_device elem = elem._tensor # The wrapping tensor (MetaTensor) shouldn't hold any # memory for the class in question, but it should still # advertise the same device as before r = torch.Tensor._make_wrapper_subclass( cls, elem.size(), strides=elem.stride(), storage_offset=elem.storage_offset(), dtype=elem.dtype, layout=elem.layout, device=fake_device or (elem.device if elem.device.type != 'meta' else torch.device('cpu')), requires_grad=elem.requires_grad) # deceive the frontend for aten selections r._tensor = elem # ...the real tensor is held as an element on the tensor. if not r._tensor.is_meta: r._tensor = r._tensor.to(torch.device('meta')) # only tensor not on `meta` should be copied to `meta` set_data_ptr(r._tensor) return r def __repr__(self): if self.grad_fn: return f"MetaTensor(..., size={tuple(self.shape)}, device='{self.device}', dtype={self.dtype}, grad_fn={self.grad_fn})" return f"MetaTensor(..., size={tuple(self.shape)}, device='{self.device}', dtype={self.dtype})" @classmethod def __torch_dispatch__(cls, func, types, args=(), kwargs=None): fake_device = None def unwrap(x): nonlocal fake_device if isinstance(x, MetaTensor): fake_device = x.device x = x._tensor elif isinstance(x, torch.Tensor): fake_device = x.device x = x.to(torch.device('meta')) return x args = tree_map(unwrap, args) kwargs = tree_map(unwrap, kwargs) if 'device' in kwargs: fake_device = kwargs['device'] kwargs['device'] = torch.device('meta') # run aten for backend=CPU but actually on backend=Meta out = func(*args, **kwargs) # here we keep the uuid of input because ALIAS_ATEN do not generate a physical copy # of the input if func in ALIAS_ATEN: out.data_ptr = args[0].data_ptr # Now, we want to continue propagating this tensor, so we rewrap Tensors in # our custom tensor subclass def wrap(x): if isinstance(x, torch.Tensor): nonlocal fake_device if not x.is_meta: x = x.to(torch.device('meta')) return MetaTensor(x, fake_device=fake_device) if isinstance(x, torch.Tensor) else x return tree_map(wrap, out) def to(self, *args, **kwargs) -> torch.Tensor: """An extension of `torch.Tensor.to()` to MetaTensor Returns: result (MetaTensor): MetaTensor Usage: >>> tensor = MetaTensor(torch.rand(10), fake_device='cuda:100') >>> tensor.to(torch.uint8) MetaTensor(tensor(..., device='meta', size=(10,), dtype=torch.uint8), fake_device='cuda:100') >>> tensor.to(torch.device('cuda:42')) MetaTensor(tensor(..., device='meta', size=(10,)), fake_device='cuda:42') >>> tensor.to('vulkan') MetaTensor(tensor(..., device='meta', size=(10,)), fake_device='vulkan') """ # this imitates c++ function in the way of @overload fake_device = None def replace(x): nonlocal fake_device if isinstance(x, str) or isinstance(x, _device): fake_device = x return 'meta' return x elem = self._tensor.to(*tree_map(replace, args), **tree_map(replace, kwargs)) return MetaTensor(elem, fake_device=fake_device) def cpu(self, *args, **kwargs): if self.device.type == 'cpu': return self.to(*args, **kwargs) return self.to(*args, device='cpu', **kwargs) def cuda(self, device=None, non_blocking=False): if device is not None: return self.to(device=device, non_blocking=non_blocking) return self.to(device='cuda:0', non_blocking=non_blocking)
# adopted from https://github.com/facebookresearch/fvcore/blob/main/fvcore/nn/jit_handles.py # ideas from https://pastebin.com/AkvAyJBw import operator from functools import partial, reduce from numbers import Number from typing import Any, Callable, List import torch from packaging import version aten = torch.ops.aten def matmul_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for matmul. """ # Inputs should be a list of length 2. # Inputs contains the shapes of two matrices. input_shapes = [v.shape for v in inputs] assert len(input_shapes) == 2, input_shapes # There are three cases: 1) gemm, 2) gemv, 3) dot if all(len(shape) == 2 for shape in input_shapes): # gemm assert input_shapes[0][-1] == input_shapes[1][-2], input_shapes elif all(len(shape) == 1 for shape in input_shapes): # dot assert input_shapes[0][0] == input_shapes[1][0], input_shapes # expand shape input_shapes[0] = torch.Size([1, input_shapes[0][0]]) input_shapes[1] = torch.Size([input_shapes[1][0], 1]) else: # gemv if len(input_shapes[0]) == 1: assert input_shapes[0][0] == input_shapes[1][-2], input_shapes input_shapes.reverse() else: assert input_shapes[1][0] == input_shapes[0][-1], input_shapes # expand the shape of the vector to [batch size, 1] input_shapes[-1] = torch.Size([input_shapes[-1][-1], 1]) flops = reduce(operator.mul, input_shapes[0]) * input_shapes[-1][-1] return flops def addmm_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for fully connected layers. """ # Count flop for nn.Linear # inputs is a list of length 3. input_shapes = [v.shape for v in inputs[1:3]] # input_shapes[0]: [batch size, input feature dimension] # input_shapes[1]: [input feature dimension, output feature dimension] assert len(input_shapes[0]) == 2, input_shapes[0] assert len(input_shapes[1]) == 2, input_shapes[1] batch_size, input_dim = input_shapes[0] output_dim = input_shapes[1][1] flops = batch_size * input_dim * output_dim return flops def linear_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for the aten::linear operator. """ # Inputs is a list of length 3; unlike aten::addmm, it is the first # two elements that are relevant. input_shapes = [v.shape for v in inputs[0:2]] # input_shapes[0]: [dim0, dim1, ..., input_feature_dim] # input_shapes[1]: [output_feature_dim, input_feature_dim] assert input_shapes[0][-1] == input_shapes[1][-1] flops = reduce(operator.mul, input_shapes[0]) * input_shapes[1][0] return flops def bmm_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for the bmm operation. """ # Inputs should be a list of length 2. # Inputs contains the shapes of two tensor. assert len(inputs) == 2, len(inputs) input_shapes = [v.shape for v in inputs] n, c, t = input_shapes[0] d = input_shapes[-1][-1] flops = n * c * t * d return flops def baddbmm_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for the baddbmm(batch add and batch matmul) operation. """ # Inputs = [input, batch1, batch2] # out = input + batch1 x batch2 assert len(inputs) == 3, len(inputs) n, c, t = inputs[1].shape d = inputs[2].shape[-1] flops = n * c * t * d return flops def conv_flop_count( x_shape: List[int], w_shape: List[int], out_shape: List[int], transposed: bool = False, ) -> Number: """ Count flops for convolution. Note only multiplication is counted. Computation for addition and bias is ignored. Flops for a transposed convolution are calculated as flops = (x_shape[2:] * prod(w_shape) * batch_size). Args: x_shape (list(int)): The input shape before convolution. w_shape (list(int)): The filter shape. out_shape (list(int)): The output shape after convolution. transposed (bool): is the convolution transposed Returns: int: the number of flops """ batch_size = x_shape[0] conv_shape = (x_shape if transposed else out_shape)[2:] flops = batch_size * reduce(operator.mul, w_shape) * reduce(operator.mul, conv_shape) return flops def conv_flop_jit(inputs: List[Any], outputs: List[Any]): """ Count flops for convolution. """ x, w = inputs[:2] x_shape, w_shape, out_shape = (x.shape, w.shape, outputs[0].shape) transposed = inputs[6] return conv_flop_count(x_shape, w_shape, out_shape, transposed=transposed) def transpose_shape(shape): return [shape[1], shape[0]] + list(shape[2:]) def conv_backward_flop_jit(inputs: List[Any], outputs: List[Any]): grad_out_shape, x_shape, w_shape = [i.shape for i in inputs[:3]] output_mask = inputs[-1] fwd_transposed = inputs[7] flop_count = 0 if output_mask[0]: grad_input_shape = outputs[0].shape flop_count += conv_flop_count(grad_out_shape, w_shape, grad_input_shape, not fwd_transposed) if output_mask[1]: grad_weight_shape = outputs[1].shape flop_count += conv_flop_count(transpose_shape(x_shape), grad_out_shape, grad_weight_shape, fwd_transposed) return flop_count def norm_flop_counter(affine_arg_index: int, input_arg_index: int) -> Callable: """ Args: affine_arg_index: index of the affine argument in inputs """ def norm_flop_jit(inputs: List[Any], outputs: List[Any]) -> Number: """ Count flops for norm layers. """ # Inputs[0] contains the shape of the input. input_shape = inputs[input_arg_index].shape has_affine = inputs[affine_arg_index].shape is not None if hasattr(inputs[affine_arg_index], 'shape') else inputs[affine_arg_index] assert 2 <= len(input_shape) <= 5, input_shape # 5 is just a rough estimate flop = reduce(operator.mul, input_shape) * (5 if has_affine else 4) return flop return norm_flop_jit def batchnorm_flop_jit(inputs: List[Any], outputs: List[Any], training: bool = None) -> Number: if training is None: training = inputs[-3] assert isinstance(training, bool), "Signature of aten::batch_norm has changed!" if training: return norm_flop_counter(1, 0)(inputs, outputs) # pyre-ignore has_affine = inputs[1].shape is not None input_shape = reduce(operator.mul, inputs[0].shape) return input_shape * (2 if has_affine else 1) def elementwise_flop_counter(input_scale: float = 1, output_scale: float = 0) -> Callable: """ Count flops by input_tensor.numel() * input_scale + output_tensor.numel() * output_scale Args: input_scale: scale of the input tensor (first argument) output_scale: scale of the output tensor (first element in outputs) """ def elementwise_flop(inputs: List[Any], outputs: List[Any]) -> Number: ret = 0 if input_scale != 0: shape = inputs[0].shape ret += input_scale * reduce(operator.mul, shape) if shape else 0 if output_scale != 0: shape = outputs[0].shape ret += output_scale * reduce(operator.mul, shape) if shape else 0 return ret return elementwise_flop def zero_flop_jit(*args): """ Count flops for zero flop layers. """ return 0 if version.parse(torch.__version__) >= version.parse('1.12.0'): flop_mapping = { # gemm, gemv and dot aten.mm.default: matmul_flop_jit, aten.mv.default: matmul_flop_jit, aten.dot.default: matmul_flop_jit, aten.matmul.default: matmul_flop_jit, aten.addmm.default: addmm_flop_jit, aten.bmm.default: bmm_flop_jit, aten.baddbmm.default: baddbmm_flop_jit, # convolution aten.convolution.default: conv_flop_jit, aten._convolution.default: conv_flop_jit, aten.convolution_backward.default: conv_backward_flop_jit, # normalization aten.native_batch_norm.default: batchnorm_flop_jit, aten.native_batch_norm_backward.default: batchnorm_flop_jit, aten.cudnn_batch_norm.default: batchnorm_flop_jit, aten.cudnn_batch_norm_backward.default: partial(batchnorm_flop_jit, training=True), aten.native_layer_norm.default: norm_flop_counter(2, 0), aten.native_layer_norm_backward.default: norm_flop_counter(2, 0), aten.native_group_norm.default: norm_flop_counter(2, 0), aten.native_group_norm_backward.default: norm_flop_counter(2, 0), # pooling aten.avg_pool1d.default: elementwise_flop_counter(1, 0), aten.avg_pool2d.default: elementwise_flop_counter(1, 0), aten.avg_pool2d_backward.default: elementwise_flop_counter(0, 1), aten.avg_pool3d.default: elementwise_flop_counter(1, 0), aten.avg_pool3d_backward.default: elementwise_flop_counter(0, 1), aten.max_pool1d.default: elementwise_flop_counter(1, 0), aten.max_pool2d.default: elementwise_flop_counter(1, 0), aten.max_pool3d.default: elementwise_flop_counter(1, 0), aten.max_pool1d_with_indices.default: elementwise_flop_counter(1, 0), aten.max_pool2d_with_indices.default: elementwise_flop_counter(1, 0), aten.max_pool2d_with_indices_backward.default: elementwise_flop_counter(0, 1), aten.max_pool3d_with_indices.default: elementwise_flop_counter(1, 0), aten.max_pool3d_with_indices_backward.default: elementwise_flop_counter(0, 1), aten._adaptive_avg_pool2d.default: elementwise_flop_counter(1, 0), aten._adaptive_avg_pool2d_backward.default: elementwise_flop_counter(0, 1), aten._adaptive_avg_pool3d.default: elementwise_flop_counter(1, 0), aten._adaptive_avg_pool3d_backward.default: elementwise_flop_counter(0, 1), aten.embedding_dense_backward.default: elementwise_flop_counter(0, 1), aten.embedding.default: elementwise_flop_counter(1, 0), aten.upsample_nearest2d.vec: elementwise_flop_counter(0, 1), aten.upsample_nearest2d_backward.vec: elementwise_flop_counter(0, 1), } elementwise_flop_aten = [ # basic op aten.add.Tensor, aten.add_.Tensor, aten.div.Tensor, aten.div_.Tensor, aten.div.Scalar, aten.div_.Scalar, aten.mul.Tensor, aten.mul.Scalar, aten.mul_.Tensor, aten.neg.default, aten.pow.Tensor_Scalar, aten.rsub.Scalar, aten.sum.default, aten.sum.dim_IntList, aten.mean.dim, aten.sub.Tensor, aten.sub_.Tensor, aten.exp.default, aten.sin.default, aten.cos.default, # activation op aten.hardswish.default, aten.hardswish_.default, aten.hardswish_backward.default, aten.hardtanh.default, aten.hardtanh_.default, aten.hardtanh_backward.default, aten.hardsigmoid_backward.default, aten.hardsigmoid.default, aten.gelu.default, aten.gelu_backward.default, aten.silu.default, aten.silu_.default, aten.silu_backward.default, aten.sigmoid.default, aten.sigmoid_backward.default, aten._softmax.default, aten._softmax_backward_data.default, aten.relu_.default, aten.relu.default, aten.tanh.default, aten.tanh_backward.default, aten.threshold_backward.default, # dropout aten.native_dropout.default, aten.native_dropout_backward.default, ] for op in elementwise_flop_aten: flop_mapping[op] = elementwise_flop_counter(1, 0) # TODO: this will be removed in future zero_flop_aten = [ aten.as_strided.default, aten.as_strided_.default, aten.bernoulli_.float, aten.cat.default, aten.clone.default, aten.copy_.default, aten.detach.default, aten.expand.default, aten.empty_like.default, aten.new_empty.default, aten.new_empty_strided.default, aten.ones_like.default, aten._reshape_alias.default, aten.select.int, aten.select_backward.default, aten.squeeze.dim, aten.slice.Tensor, aten.slice_backward.default, aten.split.Tensor, aten.permute.default, aten.t.default, aten.transpose.int, aten._to_copy.default, aten.unsqueeze.default, aten.unbind.int, aten._unsafe_view.default, aten.view.default, aten.where.self, aten.zero_.default, aten.zeros_like.default, aten.fill_.Scalar ] # yapf: disable for op in zero_flop_aten: flop_mapping[op] = zero_flop_jit else: flop_mapping = {} elementwise_flop_aten = {} zero_flop_aten = {}
from dataclasses import dataclass, field from enum import Enum from functools import partial from typing import Dict, List from torch.fx import Graph, Node from .._compatibility import compatibility from .memory_utils import activation_size, is_inplace class Phase(Enum): FORWARD = 0 BACKWARD = 1 PLACEHOLDER = 2 @compatibility(is_backward_compatible=True) @dataclass class GraphInfo: """ GraphInfo is a dataclass for MetaInfo, which measures the execution memory cost and FLOPs with `MetaTensor`. The dataflow analysis is conducted on a single node of the FX graph. ============================================================================ ------------------------------- | Node | [fwd_in] are ---> | [fwd_in] [bwd_out] | <----- [bwd_out] is marks the memory for `grad_out`. placeholders saved for | | \__________ | | backward. | | \ | | | [fwd_tmp] ------> [bwd_tmp] | <----- | | \_________ | | [bwd_tmp] marks the peak memory | / \ \ | | in backward pass. [x] is not counted ---> | [x] [fwd_tmp] -> [bwd_tmp] | <----- in [fwd_tmp] because | | \_____ | | it is not saved for | | \ | | backward. | [fwd_out] \ | | <----- [fwd_out] is [fwd_in] for the next node. ------------------------------- ============================================================================ Attributes: fwd_flop (int): The forward FLOPs of a certain node. fwd_time (float): The real forward time (s) of a certain node. bwd_flop (int): The backward FLOPs of a certain node. bwd_time (float): The real backward time (s) of a certain node. save_fwd_in (bool): The decision variable of whether to save the fwd_mem_out of parent nodes. fwd_in (List): See the above illustration. fwd_tmp (List): See the above illustration. fwd_out (List): See the above illustration. fwd_mem_tmp (int): See the above illustration. fwd_mem_out (int): See the above illustration. bwd_mem_tmp (int): See the above illustration. bwd_mem_out (int): See the above illustration. """ # TODO(super-dainiu): removed redundant items, currently all of them are necessary for development fwd_flop: int = 0 fwd_time: float = 0.0 bwd_flop: int = 0 bwd_time: float = 0.0 save_fwd_in: bool = False fwd_in: List = field(default_factory=list) fwd_tmp: List = field(default_factory=list) fwd_out: List = field(default_factory=list) fwd_mem_tmp: int = 0 fwd_mem_out: int = 0 bwd_mem_tmp: int = 0 bwd_mem_out: int = 0 def is_phase(n: Node, phase: Phase) -> bool: assert 'phase' in n.meta, f'Node meta of {n} has no key `phase`!' return n.meta['phase'] == phase @compatibility(is_backward_compatible=False) def autograd_graph_analysis(graph: Graph) -> GraphInfo: """Analyze the autograd node dependencies and find out the memory usage. Basically the input graph should have all nodes marked for keyword `phase`. Nodes should have attribute `out` indicating the output of each node. ============================================================================ Placeholder ----> p o <---- We need to keep track of grad out |\________ | ↓ ↘| f --------> b |\ \_____ ↑ | \ ↘ / f f ----> b <---- Not every forward result needs to be saved for backward | \____ ↑ ↘ ↘| f ----> b <---- Backward can be freed as soon as it is required no more. ↘ ↗ l ============================================================================= Args: graph (Graph): The autograd graph with nodes marked for keyword `phase`. Returns: graph_info (GraphInfo): Meta information for the dataflow. """ def _peak_memory(deps: Dict[Node, int]): peak_mem = 0 for k, v in deps.items(): if v > 0 and is_phase(k, Phase.BACKWARD) and not all(map(is_inplace, k.users)) and not is_inplace(k): peak_mem += activation_size(k.meta['saved_tensor']) if v <= float('-inf') and is_phase(k, Phase.FORWARD): peak_mem -= activation_size(k.meta['saved_tensor']) return peak_mem # deps is used to track all the memory dependencies of the graph. deps = {} graph_info = GraphInfo() for n in graph.nodes: n: Node deps[n] = len(n.users) # A forward tensor who is marked `save` but is also # an input to `Phase.FORWARD` should be saved during forward. # If the tensor is a placeholder, then it belongs to `fwd_mem_in`. # Any `fwd_mem_in` should be kept in memory even this function # is checkpointed. # Otherwise, the tensor belongs to `fwd_mem_tmp`. If we checkpoint # the node, `fwd_mem_tmp` can be freed. if is_phase(n, Phase.PLACEHOLDER): graph_info.fwd_in += n.meta['saved_tensor'] if is_phase(n, Phase.FORWARD): graph_info.fwd_tmp += n.meta['saved_tensor'] elif is_phase(n, Phase.BACKWARD): if len(n.users): graph_info.bwd_mem_tmp = max(graph_info.bwd_mem_tmp, _peak_memory(deps)) else: # TODO: some of the bwd_mem_out might be model parameters. # basically a backward node without user is a `grad_out` node graph_info.bwd_mem_out += activation_size(n.meta['saved_tensor']) for input_n in n.all_input_nodes: if input_n in deps: deps[input_n] -= 1 if deps[input_n] <= 0: deps[input_n] = float('-inf') return graph_info
import time from functools import partial from typing import Any, Callable, Dict, Tuple import torch from torch.fx import Graph, Node from torch.fx.node import Argument, Target from torch.nn.parameter import Parameter from torch.utils._pytree import tree_map from .._compatibility import compatibility from .constants import ALIAS_ATEN, OUTPUT_SAVED_MOD, OUTPUT_SAVED_OPS from .dataflow import GraphInfo, Phase, autograd_graph_analysis, is_phase from .memory_utils import activation_size, parameter_size from .opcount import flop_mapping from .tensor import MetaTensor __all__ = ['profile_function', 'profile_module', 'profile_method'] # super-dainiu: this cache should be global, otherwise it cannot # track duplicated tensors between nodes cache = set() # a global identifier for inplace ops do_not_cache = False def normalize_tuple(x): if not isinstance(x, tuple): return (x,) return x def is_autogradable(x): return isinstance(x, torch.Tensor) and x.is_floating_point() def detach_variables(x): if isinstance(x, torch.Tensor): requires_grad = x.requires_grad x = x.detach() x.requires_grad = requires_grad return x @compatibility(is_backward_compatible=True) def _profile_concrete(target: Callable, *args, **kwargs) -> Tuple[Tuple[Any, ...], GraphInfo]: """Profile a Callable function with args and kwargs on concrete devices by https://github.com/Cypher30 To profile the actual forward memory, we first run target in the context torch.no_grad() to get the fwd_mem_out, then we run target with grad enable to found the extra memory stored in the memory by memory allocated minus the fwd_mem_out. To profile the actual backward memory, we first make dummy gradient for torch.autograd.backward, then find the bwd_mem_tmp with memory peak during the process minus bwd_mem_out(it is actually equal to size of args and kwargs). We also add time stamps to profile the real forward and backward time. Args: target (Callable): A Callable function args (Any): Arguments kwargs (Any): Arguments Returns: Tuple[Tuple[Any, ...], GraphInfo]: Output for next node & memory cost and real forward and backward time. """ graphinfo = GraphInfo() # detach input from the graph args = tree_map(detach_variables, args) kwargs = tree_map(detach_variables, kwargs) if isinstance(target, str): # args[0] is the `self` object for this method call self_obj, *args_tail = args # calculate fwd_mem_out mem_stamp0 = torch.cuda.memory_allocated() with torch.no_grad(): out = getattr(self_obj, target)(*args_tail, **kwargs) mem_stamp1 = torch.cuda.memory_allocated() graphinfo.fwd_mem_out = mem_stamp1 - mem_stamp0 del out # calculate fwd_mem_tmp & fwd_time mem_stamp0 = torch.cuda.memory_allocated() fwd_time0 = time.time() out = getattr(self_obj, target)(*args_tail, **kwargs) fwd_time1 = time.time() graphinfo.fwd_time = fwd_time1 - fwd_time0 mem_stamp1 = torch.cuda.memory_allocated() graphinfo.fwd_mem_tmp = mem_stamp1 - mem_stamp0 - graphinfo.fwd_mem_out # calculate bwd_mem_tmp & bwd_time grad_tensors = tree_map(lambda x: torch.ones_like(x) if isinstance(x, torch.Tensor) else None, out) torch.cuda.reset_peak_memory_stats() mem_stamp0 = torch.cuda.memory_allocated() bwd_time0 = time.time() torch.autograd.backward(out, grad_tensors=grad_tensors) bwd_time1 = time.time() graphinfo.bwd_time = bwd_time1 - bwd_time0 mem_stamp1 = torch.cuda.max_memory_allocated() # calculate bwd memory stats # NOTE: the module should add param to bwd_mem_out for bwd_mem_tmp calculation graphinfo.bwd_mem_out = activation_size(args) + activation_size(kwargs) graphinfo.bwd_mem_out += parameter_size(target.__self__) if hasattr(target.__self__, "parameters") else 0 graphinfo.bwd_mem_tmp = mem_stamp1 - mem_stamp0 - graphinfo.bwd_mem_out else: # calculate fwd_mem_out mem_stamp0 = torch.cuda.memory_allocated() with torch.no_grad(): out = target(*args, **kwargs) mem_stamp1 = torch.cuda.memory_allocated() graphinfo.fwd_mem_out = mem_stamp1 - mem_stamp0 del out # calculate fwd_mem_tmp & fwd_time mem_stamp0 = torch.cuda.memory_allocated() fwd_time0 = time.time() out = target(*args, **kwargs) fwd_time1 = time.time() graphinfo.fwd_time = fwd_time1 - fwd_time0 mem_stamp1 = torch.cuda.memory_allocated() graphinfo.fwd_mem_tmp = mem_stamp1 - mem_stamp0 - graphinfo.fwd_mem_out # calculate bwd_mem_tmp & bwd_time grad_tensors = tree_map(lambda x: torch.ones_like(x) if isinstance(x, torch.Tensor) else None, out) torch.cuda.reset_peak_memory_stats() mem_stamp0 = torch.cuda.memory_allocated() bwd_time0 = time.time() torch.autograd.backward(out, grad_tensors=grad_tensors) bwd_time1 = time.time() graphinfo.bwd_time = bwd_time1 - bwd_time0 mem_stamp1 = torch.cuda.max_memory_allocated() # calculate bwd memory stats # NOTE: the module should add param to bwd_mem_out for bwd_mem_tmp calculation graphinfo.bwd_mem_out = activation_size(args) + activation_size(kwargs) graphinfo.bwd_mem_out += parameter_size(target.__self__) if hasattr(target.__self__, "parameters") else 0 graphinfo.bwd_mem_tmp = mem_stamp1 - mem_stamp0 - graphinfo.bwd_mem_out return tree_map(detach_variables, out), graphinfo @compatibility(is_backward_compatible=False) def _profile_meta(target: Callable, *args, **kwargs) -> Tuple[Tuple[Any, ...], GraphInfo]: """ Profile a Callable function with args and kwargs on meta devices. Args: target (Callable): A Callable function args (Any): Argument kwargs (Any): Argument Returns: out (Tuple[Any, ...]): The argument value that was retrieved. meta_info (GraphInfo): The memory cost and FLOPs estimated with `MetaTensor`. """ # This subgraph traces aten level ops inside one node. subgraph = Graph() # `flop_count`` serves as a global dictionary to store results. flop_count = { Phase.FORWARD: 0, Phase.BACKWARD: 0, } # FlopTensor not only get the flop statistics of a single node, # it also build a full autograd graph for this node. # This makes sure we can analyze the dependencies of memory, and # decide which forward intermediate results should be kept until # backward is executed. # Hopefully, this attempt will provide a better estimation of memory. class FlopTensor(MetaTensor): _node: Node = None def __repr__(self): if self.grad_fn: return f"FlopTensor({self._tensor}, fake_device='{self.device}', size={tuple(self.shape)}, grad_fn={self.grad_fn})" return f"FlopTensor({self._tensor}, fake_device='{self.device}', size={tuple(self.shape)}, requires_grad={self.requires_grad})" @classmethod def __torch_dispatch__(cls, func, types, args=(), kwargs=None): args_node = tree_map(lambda x: x._node if isinstance(x, FlopTensor) else None, args) kwargs_node = tree_map(lambda x: x._node if isinstance(x, FlopTensor) else None, kwargs) node = subgraph.create_node('call_function', func, args_node, kwargs_node) out = super().__torch_dispatch__(func, types, args, kwargs) flop_count[phase] += flop_mapping[func](args, normalize_tuple(out)) node.meta['phase'] = phase # super-dainiu: in `nn.MultiheadAttention` this weird thing occurs, # i.e. `Phase.PLACEHOLDER` tensors are aliased and saved during # `Phase.FORWARD` if phase == Phase.FORWARD: if all(map(partial(is_phase, phase=Phase.PLACEHOLDER), node.all_input_nodes)) and func in ALIAS_ATEN: node.meta['phase'] = Phase.PLACEHOLDER # TODO(yby): specify `saved_tensors` for backward memory estimation node.meta['saved_tensor'] = [] if phase == Phase.BACKWARD: node.meta['saved_tensor'] = normalize_tuple(out) def wrap(x): if isinstance(x, MetaTensor): x = FlopTensor(x) x._node = node return x out = tree_map(wrap, out) return out def wrap(x): if isinstance(x, torch.Tensor): x = FlopTensor(x) if is_autogradable(x): x.requires_grad_(True) x._node = subgraph.create_node('placeholder', 'placeholder', (subgraph._root,), name=subgraph._graph_namespace.create_name('input', x._tensor)) x._node.meta['phase'] = Phase.PLACEHOLDER x._node.meta['saved_tensor'] = [] return x # Basically, we need to detach the args and kwargs from the outer graph. args = tree_map(wrap, args) kwargs = tree_map(wrap, kwargs) def pack(x): global cache, do_not_cache if isinstance(x, FlopTensor) and not x._tensor.data_ptr() in cache: tensor = x._tensor.detach() tensor.data_ptr = x._tensor.data_ptr x._node.meta['saved_tensor'] += [tensor] if not do_not_cache: cache.add(x._tensor.data_ptr()) return x def unpack(x): return x # `phase` will mark the phase of autograd from outside scope. phase = Phase.FORWARD # mark saved tensors with saved_tensors_hooks with torch.autograd.graph.saved_tensors_hooks(pack, unpack): if isinstance(target, str): # args[0] is the `self` object for this method call self_obj, *args_tail = args out = getattr(self_obj, target)(*args_tail, **kwargs) else: out = target(*args, **kwargs) # If the output is not a floating point `torch.Tensor` or it does not # requires grad, then we should not run backward for this node. if all(map(lambda x: is_autogradable(x) and x.requires_grad, normalize_tuple(out))): grad_out = [torch.zeros_like(t) for t in normalize_tuple(out)] phase = Phase.BACKWARD torch.autograd.backward( out, grad_out, ) graph_info = autograd_graph_analysis(subgraph) graph_info.fwd_flop, graph_info.bwd_flop = flop_count[Phase.FORWARD], flop_count[Phase.BACKWARD] def extract_tensor(x: Any): if isinstance(x, MetaTensor): tensor = x._tensor.detach() tensor.data_ptr = x._tensor.data_ptr return tensor if not isinstance(x, torch.finfo): return x graph_info.fwd_out = list(map(extract_tensor, normalize_tuple(out))) def unwrap(x): return MetaTensor(x) if isinstance(x, torch.Tensor) else x return tree_map(unwrap, out), graph_info @compatibility(is_backward_compatible=True) def profile_function(target: 'Target', device: str = 'meta') -> Callable: """ Wrap a `call_function` node or `torch.nn.functional` in order to record the memory cost and FLOPs of the execution. Warnings: You may only use tensors with `device=meta` for this wrapped function. Only original `torch.nn.functional` are available. Examples: >>> input = torch.rand(100, 100, 100, 100, device='meta') >>> func = torch.nn.functional.relu >>> output, meta_info = profile_function(func)(input) """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: # find the grad for parameter in args and kwargs param_size = 0 def get_param_size(x): nonlocal param_size if isinstance(x, Parameter): param_size += activation_size(x) tree_map(get_param_size, args) tree_map(get_param_size, kwargs) # If there is an argument that this `call_function` is inplace, we should # still run the profiling but discard some results regarding `target` global do_not_cache inplace = kwargs.get('inplace', False) if target in OUTPUT_SAVED_OPS: do_not_cache = True if inplace: do_not_cache = True kwargs['inplace'] = False if device == 'meta': out, meta = _profile_meta(func, *args, **kwargs) else: out, meta = _profile_concrete(func, *args, **kwargs) if inplace: kwargs['inplace'] = True meta.bwd_mem_tmp = 0 meta.bwd_mem_out = 0 do_not_cache = False meta.bwd_mem_out -= param_size return out, meta f.__name__ = target.__name__ func = target return f @compatibility(is_backward_compatible=True) def profile_method(target: 'Target', device: str = 'meta') -> Callable: """ Wrap a `call_method` node record the memory cost and FLOPs of the execution. """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: # execute the method and return the result assert isinstance(target, str), f'{target} instance is not str.' if device == 'meta': out, meta = _profile_meta(target, *args, **kwargs) else: out, meta = _profile_concrete(target, *args, **kwargs) return out, meta return f @compatibility(is_backward_compatible=True) def profile_module(module: torch.nn.Module, device: str = 'meta') -> Callable: """ Wrap a `call_module` node or `torch.nn` in order to record the memory cost and FLOPs of the execution. Warnings: You may only use tensors with `device=meta` for this wrapped function. Only original `torch.nn` are available. Example: >>> input = torch.rand(4, 3, 224, 224, device='meta') >>> mod = torch.nn.Conv2d(3, 128, 3) >>> output, meta_info = profile_module(mod)(input) """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: # calculate parameter size param_size = parameter_size(module) # If there is an argument that this `call_module` is inplace, we should # still run the profiling but discard some results regarding `module`. global do_not_cache inplace = getattr(module, 'inplace', False) if type(module) in OUTPUT_SAVED_MOD: do_not_cache = True if inplace: do_not_cache = True module.inplace = False if device == 'meta': out, meta = _profile_meta(func, *args, **kwargs) else: out, meta = _profile_concrete(func, *args, **kwargs) if inplace: module.inplace = True meta.bwd_mem_tmp = 0 meta.bwd_mem_out = 0 do_not_cache = False # grad for param will not be counted meta.bwd_mem_out -= param_size return out, meta f.__name__ = module.__class__.__name__ func = module.forward return f
import torch from torch.fx import Node from .._compatibility import compatibility, is_compatible_with_meta from .memory_utils import activation_size if is_compatible_with_meta(): from .constants import OUTPUT_SAVED_MOD, OUTPUT_SAVED_OPS __all__ = ["calculate_fwd_in", "calculate_fwd_tmp", "calculate_fwd_out"] @compatibility(is_backward_compatible=False) def calculate_fwd_in(n: Node) -> int: """A helper function to calculate `fwd_in` (with sharding spec) Args: n (Node): a node from the graph Returns: fwd_in (int): the result of `fwd_in` """ # TODO(super-dainiu): should divide the memory by sharding spec return activation_size(n.meta["fwd_in"]) @compatibility(is_backward_compatible=False) def calculate_fwd_tmp(n: Node) -> int: """A helper function to calculate `fwd_tmp` (with sharding spec) Currently, `torch.nn.ReLU` behaves weirdly, so we have to patch it for accuracy. Args: n (Node): a node from the graph Returns: fwd_tmp (int): the result of `fwd_tmp` """ # TODO(super-dainiu): should divide the memory by sharding spec def is_relu_like_node(n: Node) -> bool: """Check if a node is a ReLU-like node. ReLU-like nodes have the following properties: - They are either `call_function` or `call_module` - Their output tensors are directly saved for backward - Their input tensors are not saved for backward An example is `torch.nn.functional.softmax` which has (forward + backward): def forward(self, input_2): _softmax_default = torch.ops.aten._softmax.default(input_2, None, None); input_2 = None zeros_like_default = torch.ops.aten.zeros_like.default(_softmax_default, dtype = None, layout = None, device = None, pin_memory = None) detach_default = torch.ops.aten.detach.default(_softmax_default); _softmax_default = None _softmax_backward_data_default = torch.ops.aten._softmax_backward_data.default(zeros_like_default, detach_default, None, None); zeros_like_default = detach_default = None detach_default_1 = torch.ops.aten.detach.default(_softmax_backward_data_default); _softmax_backward_data_default = None detach_default_2 = torch.ops.aten.detach.default(detach_default_1); detach_default_1 = None Args: n (Node): A node from the graph Returns: bool: Whether the node is a ReLU-like node """ if n.op == 'call_function': return n.target in OUTPUT_SAVED_OPS elif n.op == 'call_module': return type(n.graph.owning_module.get_submodule(n.target)) in OUTPUT_SAVED_MOD return False if not is_relu_like_node(n): return activation_size(n.meta["fwd_tmp"]) return 0 @compatibility(is_backward_compatible=False) def calculate_fwd_out(n: Node) -> int: """A helper function to calculate `fwd_out` (with sharding spec) Args: n (Node): a node from the graph Returns: fwd_out (int): the result of `fwd_out` """ # TODO(super-dainiu): should divide the memory by sharding spec def intersect(a, b): return {k: a[k] for k in a if k in b} fwd_in = dict() for u in n.users: fwd_in.update({x.data_ptr(): x for x in u.meta["fwd_in"] if isinstance(x, torch.Tensor)}) fwd_out = {x.data_ptr(): x for x in n.meta["fwd_out"] if isinstance(x, torch.Tensor)} return activation_size(intersect(fwd_in, fwd_out)) def calculate_fwd_time(n: Node) -> float: """A helper function to calculate `fwd_time` (with sharding spec) Args: n (Node): a node from the graph Returns: fwd_time (float): the result of `fwd_time` """ # TODO(super-dainiu): should divide the time by the number of GPUs as well as TFLOPs return n.meta["fwd_time"] def calculate_bwd_time(n: Node) -> float: """A helper function to calculate `bwd_time` (with sharding spec) Args: n (Node): a node from the graph Returns: bwd_time (float): the result of `bwd_time` """ # TODO(super-dainiu): should divide the time by the number of GPUs as well as TFLOPs return n.meta["bwd_time"]
from typing import Dict, List, Tuple, Union import torch from torch.fx import GraphModule, Node from .._compatibility import compatibility, is_compatible_with_meta __all__ = ['activation_size', 'parameter_size', 'is_inplace'] @compatibility(is_backward_compatible=True) def activation_size(out: Union[torch.Tensor, Dict, List, Tuple, int]) -> int: """Calculate activation size of a node. Args: activation (Union[torch.Tensor, Dict, List, Tuple, int]): The activation of a `torch.nn.Module` or `torch.nn.functional`. Returns: int: The activation size, unit is byte. """ act_size = 0 if isinstance(out, torch.Tensor): if out.is_quantized: act_size += out.numel() * torch._empty_affine_quantized([], dtype=out.dtype).element_size() else: act_size += out.numel() * torch.tensor([], dtype=out.dtype).element_size() elif isinstance(out, dict): value_list = [v for _, v in out.items()] act_size += activation_size(value_list) elif isinstance(out, tuple) or isinstance(out, list) or isinstance(out, set): for element in out: act_size += activation_size(element) return act_size @compatibility(is_backward_compatible=True) def parameter_size(mod: torch.nn.Module) -> int: """Calculate parameter size of a node. Args: mod (torch.nn.Module): The target `torch.nn.Module`. Returns: int: The parameter size, unit is byte. """ param_size = 0 for param in mod.parameters(): param_size += param.numel() * torch.tensor([], dtype=param.dtype).element_size() return param_size def is_inplace(n: Node): """Get the inplace argument from torch.fx.Node Args: node (Node): torch.fx.Node Returns: bool: indicates whether this op is inplace """ inplace = False if n.op == "call_function": inplace = n.kwargs.get("inplace", False) if is_compatible_with_meta(): from .constants import ALIAS_ATEN if n.target in ALIAS_ATEN: inplace = True elif n.op == "call_module": inplace = getattr(n.graph.owning_module.get_submodule(n.target), "inplace", False) return inplace
class ProfilerRegistry: def __init__(self, name): self.name = name self.store = {} def register(self, source): def wrapper(func): self.store[source] = func return func return wrapper def get(self, source): assert source in self.store target = self.store[source] return target def has(self, source): return source in self.store meta_profiler_function = ProfilerRegistry(name='patched_functions_for_meta_profile') meta_profiler_module = ProfilerRegistry(name='patched_modules_for_meta_profile')
from operator import add, floordiv, getitem, mul, neg, pos, setitem, sub import torch __all__ = ['INPLACE_OPS', 'INPLACE_METHOD', 'NON_INPLACE_METHOD'] # TODO fill out the inplace ops INPLACE_OPS = [ add, sub, mul, floordiv, neg, pos, getitem, setitem, getattr, torch.Tensor.cpu, ] # TODO: list all call_methods that are inplace here INPLACE_METHOD = [ 'transpose', 'permute', # TODO: reshape may return a copy of the data if the data is not contiguous 'reshape', 'dim', 'flatten', 'size', 'view', 'unsqueeze', 'to', 'type', 'flatten', ] # TODO: list all call_methods that are not inplace here NON_INPLACE_METHOD = [ 'chunk', 'contiguous', 'expand', 'mean', 'split', ]
from .profiler import profile_function, profile_method, profile_module from .profiler_function import * from .profiler_module import * from .registry import meta_profiler_function, meta_profiler_module from .shard_utils import calculate_fwd_in, calculate_fwd_out, calculate_fwd_tmp
from dataclasses import dataclass from typing import Any, Callable, Dict, Tuple import torch from torch.fx.node import Argument, Target from ..._compatibility import compatibility from ..memory_utils import activation_size from .constants import INPLACE_METHOD, INPLACE_OPS, NON_INPLACE_METHOD from .registry import meta_profiler_function, meta_profiler_module __all__ = ['profile_function', 'profile_module', 'profile_method'] # this is for compatibility use @compatibility(is_backward_compatible=True) @dataclass class GraphInfo: """ GraphInfo is a dataclass for MetaInfo, which measures the execution memory cost and FLOPs with `MetaTensor`. The dataflow analysis is conducted on a single node of the FX graph. ============================================================================ ------------------------------- | Node | [fwd_in] are ---> | [fwd_in] [bwd_out] | <----- [bwd_out] is marks the memory for `grad_out` placeholders saved for | | \__________ | | backward. | | \ | | | [fwd_tmp] ------> [bwd_tmp] | <----- | | \_________ | | [bwd_tmp] marks the peak memory | / \ \ | | in backward pass. [x] is not counted ---> | [x] [fwd_tmp] -> [bwd_tmp] | <----- in [fwd_tmp] because | | | \_____ | | it is not saved for | | | \ | | backward. ------------------------------- ============================================================================ Attributes: fwd_flop (int): The forward FLOPs of a certain node bwd_flop (int): The backward FLOPs of a certain node. fwd_mem_in (int): See the above illustration. fwd_mem_tmp (int): See the above illustration. bwd_mem_tmp (int): See the above illustration. bwd_mem_out (int): See the above illustration. """ fwd_flop: int = 0 bwd_flop: int = 0 fwd_mem_in: int = 0 fwd_mem_tmp: int = 0 bwd_mem_tmp: int = 0 bwd_mem_out: int = 0 CALL_FUNCTION_MSG = \ """ Colossal-AI hasn't supported profiling for {}, you might manually patch it with the following code.\n from colossalai.fx.profiler.experimental import meta_profiler_function @meta_profiler_function.register(YOUR_FUNCTION) def profile_YOUR_FUNCTION(input: torch.Tensor, *args) -> Tuple[int, int]: flops = ... macs = ... return flops, macs """ CALL_METHOD_MSG = 'Please check if {} is an inplace method. If so, add target to INPLACE_METHOD={}. Otherwise, add target to NON_INPLACE_METHOD={}' CALL_MODULE_MSG = \ """ Colossal-AI hasn't supported profiling for {}, you might manually patch it with the following code.\n from colossalai.fx.profiler.experimental import meta_profiler_module @meta_profiler_module.register(YOUR_MODULE) def profile_YOUR_MODULE(self: torch.nn.Module, input: torch.Tensor) -> Tuple[int, int]: flops = ... macs = ... return flops, macs """ @compatibility(is_backward_compatible=True) def profile_function(target: 'Target') -> Callable: """ Wrap a `call_function` node or `torch.nn.functional` in order to record the memory cost and FLOPs of the execution. Unfortunately, backward memory cost and FLOPs are estimated results. Warnings: You may only use tensors with `device=meta` for this wrapped function. Only original `torch.nn.functional` are available. Examples: >>> input = torch.rand(100, 100, 100, 100, device='meta') >>> func = torch.nn.functional.relu >>> output, (fwd_flop, bwd_flop), (fwd_tmp, fwd_out, bwd_tmp, bwd_out) = profile_function(func)(input, inplace=False) """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: assert meta_profiler_function.has(target) or meta_profiler_function.has( target.__name__), CALL_FUNCTION_MSG.format(target) fwd_tmp = 0 fwd_out = 0 out = func(*args, **kwargs) if target not in INPLACE_OPS and not kwargs.get('inplace', False): fwd_out = activation_size(out) if meta_profiler_function.has(target): profiler = meta_profiler_function.get(target) else: profiler = meta_profiler_function.get(target.__name__) fwd_flop, _ = profiler(*args, **kwargs) return out, GraphInfo(fwd_flop, fwd_flop * 2, fwd_tmp, fwd_out, fwd_tmp + fwd_out, 0) f.__name__ = target.__name__ func = target return f @compatibility(is_backward_compatible=True) def profile_method(target: 'Target') -> Callable: """ Wrap a `call_method` node record the memory cost and FLOPs of the execution. Warnings: This is not fully implemented and you may follow the error message to debug. """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: # args[0] is the `self` object for this method call self_obj, *args_tail = args # execute the method and return the result assert isinstance(target, str), f'{target} instance is not str.' out = getattr(self_obj, target)(*args_tail, **kwargs) assert target in INPLACE_METHOD + NON_INPLACE_METHOD, CALL_METHOD_MSG.format( target, INPLACE_METHOD, NON_INPLACE_METHOD) # call_method has no parameters and are MOSTLY(?) inplace, and has no FLOPs or MACs. fwd_tmp = 0 if target in INPLACE_METHOD else activation_size(out) fwd_out = 0 if target not in INPLACE_METHOD else activation_size(out) return out, GraphInfo(0, 0, fwd_tmp, fwd_out, fwd_tmp + fwd_out, 0) return f @compatibility(is_backward_compatible=True) def profile_module(module: torch.nn.Module) -> Callable: """ Wrap a `call_module` node or `torch.nn` in order to record the memory cost and FLOPs of the execution. Warnings: You may only use tensors with `device=meta` for this wrapped function. Only original `torch.nn` are available. Example: >>> input = torch.rand(4, 3, 224, 224, device='meta') >>> mod = torch.nn.Conv2d(3, 128, 3) >>> output, (fwd_flop, bwd_flop), (fwd_tmp, fwd_out, bwd_tmp, bwd_out) = profile_module(mod)(input) """ def f(*args: Tuple[Argument, ...], **kwargs: Dict[str, Any]) -> Any: assert meta_profiler_module.has(type(module)), CALL_MODULE_MSG.format(type(module)) fwd_tmp = 0 fwd_out = 0 out = func(*args, **kwargs) if getattr(module, 'inplace', False): fwd_out = activation_size(out) profiler = meta_profiler_module.get(type(module)) fwd_flop, _ = profiler(module, *args, **kwargs) return out, GraphInfo(fwd_flop, fwd_flop * 2, fwd_tmp, fwd_out, fwd_tmp + fwd_out, 0) f.__name__ = module.__class__.__name__ func = module.forward return f
# for PyTorch 1.11 compatibility uses from typing import Dict, List, Tuple, Union import torch from torch.fx import GraphModule, Node from ..._compatibility import compatibility __all__ = ["calculate_fwd_in", "calculate_fwd_tmp", "calculate_fwd_out"] @compatibility(is_backward_compatible=True) def calculate_fwd_in(n: Node) -> bool: """A helper function to calculate `fwd_in` Args: n (Node): a node from the graph Returns: save_fwd_in (bool): the result of `save_fwd_in` """ return n.meta['save_fwd_in'] @compatibility(is_backward_compatible=True) def calculate_fwd_tmp(n: Node) -> int: """A helper function to calculate `fwd_tmp` Args: n (Node): a node from the graph Returns: fwd_tmp (int): the result of `fwd_tmp` """ return n.meta["fwd_mem_tmp"] @compatibility(is_backward_compatible=True) def calculate_fwd_out(n: Node) -> int: """A helper function to calculate `fwd_out` Args: n (Node): a node from the graph Returns: fwd_out (int): the result of `fwd_out` """ return n.meta['fwd_mem_out']
from typing import Optional, Tuple import torch from ..registry import meta_profiler_module # TODO: This is hard to compute memory cost @meta_profiler_module.register(torch.nn.MultiheadAttention) def torch_nn_msa(self: torch.nn.MultiheadAttention, query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, key_padding_mask: Optional[torch.Tensor] = None, need_weights: bool = True, attn_mask: Optional[torch.Tensor] = None, average_attn_weights: bool = True) -> Tuple[int, int]: if getattr(self, 'batch_first', False): batch_size = query.shape[0] len_idx = 1 else: batch_size = query.shape[1] len_idx = 0 dim_idx = 2 qdim = query.shape[dim_idx] kdim = key.shape[dim_idx] vdim = value.shape[dim_idx] qlen = query.shape[len_idx] klen = key.shape[len_idx] vlen = value.shape[len_idx] num_heads = self.num_heads assert qdim == self.embed_dim if self.kdim is None: assert kdim == qdim if self.vdim is None: assert vdim == qdim flops = 0 macs = 0 # Q scaling flops += qlen * qdim # Initial projections flops += 2 * ((qlen * qdim * qdim) # QW + (klen * kdim * kdim) # KW + (vlen * vdim * vdim) # VW ) macs += ((qlen * qdim * qdim) # QW + (klen * kdim * kdim) # KW + (vlen * vdim * vdim) # VW ) if self.in_proj_bias is not None: flops += (qlen + klen + vlen) * qdim # attention heads: scale, matmul, softmax, matmul qk_head_dim = qdim // num_heads v_head_dim = vdim // num_heads head_flops = ( 2 * (qlen * klen * qk_head_dim) # QK^T + (qlen * klen) # softmax + 2 * (qlen * klen * v_head_dim) # AV ) head_macs = ((qlen * klen * qk_head_dim) # QK^T + 2 * (qlen * klen * v_head_dim) # AV ) flops += num_heads * head_flops macs += num_heads * head_flops # final projection, bias is always enabled flops += qlen * vdim * (vdim + 1) flops *= batch_size macs *= batch_size return flops, macs
import operator from functools import reduce import math from typing import Tuple import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.Conv1d) def torch_nn_conv1d(self: torch.nn.Conv1d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.Conv1d.html c_in, l_in = input.shape[-2:] c_out = self.out_channels l_out = math.floor((l_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1) result_shape = input.shape[:-2] + ( c_out, l_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce(operator.mul, result_shape) macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += num_elem return flops, macs @meta_profiler_module.register(torch.nn.Conv2d) def torch_nn_conv2d(self: torch.nn.Conv2d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html c_in, h_in, w_in = input.shape[-3:] c_out = self.out_channels h_out = math.floor((h_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1) w_out = math.floor((w_in + 2 * self.padding[1] - self.dilation[1] * (self.kernel_size[1] - 1) - 1) / self.stride[1] + 1) result_shape = input.shape[:-3] + ( c_out, h_out, w_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce(operator.mul, result_shape) macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += num_elem return flops, macs @meta_profiler_module.register(torch.nn.Conv3d) def torch_nn_conv3d(self: torch.nn.Conv3d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.Conv3d.html c_in, d_in, h_in, w_in = input.shape[-4:] c_out = self.out_channels d_out = math.floor((d_in + 2 * self.padding[0] - self.dilation[0] * (self.kernel_size[0] - 1) - 1) / self.stride[0] + 1) h_out = math.floor((h_in + 2 * self.padding[1] - self.dilation[1] * (self.kernel_size[1] - 1) - 1) / self.stride[1] + 1) w_out = math.floor((w_in + 2 * self.padding[2] - self.dilation[2] * (self.kernel_size[2] - 1) - 1) / self.stride[2] + 1) result_shape = input.shape[:-4] + ( c_out, d_out, h_out, w_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce(operator.mul, result_shape) macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += num_elem return flops, macs @meta_profiler_module.register(torch.nn.ConvTranspose1d) def torch_nn_convtranspose1d(self: torch.nn.ConvTranspose1d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose1d.html c_in, l_in = input.shape[-2:] c_out = self.out_channels l_out = math.floor((l_in - 1) * self.stride[0] - 2 * self.padding[0] + self.dilation[0] * (self.kernel_size[0] - 1) + self.output_padding[0] + 1) result_shape = input.shape[:-2] + ( c_out, l_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce( operator.mul, input.shape ) # see https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler/profiler.py#L604 macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += reduce(operator.mul, result_shape) return flops, macs @meta_profiler_module.register(torch.nn.ConvTranspose2d) def torch_nn_convtranspose2d(self: torch.nn.ConvTranspose2d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose2d.html c_in, h_in, w_in = input.shape[-3:] c_out = self.out_channels h_out = math.floor((h_in - 1) * self.stride[0] - 2 * self.padding[0] + self.dilation[0] * (self.kernel_size[0] - 1) + self.output_padding[0] + 1) w_out = math.floor((w_in - 1) * self.stride[1] - 2 * self.padding[1] + self.dilation[1] * (self.kernel_size[1] - 1) + self.output_padding[1] + 1) result_shape = input.shape[:-3] + ( c_out, h_out, w_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce(operator.mul, input.shape) macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += reduce(operator.mul, result_shape) return flops, macs @meta_profiler_module.register(torch.nn.ConvTranspose3d) def torch_nn_convtranspose3d(self: torch.nn.ConvTranspose3d, input: torch.Tensor) -> Tuple[int, int]: # the output shape is calculated using the formula stated # at https://pytorch.org/docs/stable/generated/torch.nn.ConvTranspose3d.html c_in, d_in, h_in, w_in = input.shape[-4:] c_out = self.out_channels d_out = math.floor((d_in - 1) * self.stride[0] - 2 * self.padding[0] + self.dilation[0] * (self.kernel_size[0] - 1) + self.output_padding[0] + 1) h_out = math.floor((h_in - 1) * self.stride[1] - 2 * self.padding[1] + self.dilation[1] * (self.kernel_size[1] - 1) + self.output_padding[1] + 1) w_out = math.floor((w_in - 1) * self.stride[2] - 2 * self.padding[2] + self.dilation[2] * (self.kernel_size[2] - 1) + self.output_padding[2] + 1) result_shape = input.shape[:-4] + ( c_out, d_out, h_out, w_out, ) macs_per_elem = reduce(operator.mul, self.kernel_size) * c_in // self.groups num_elem = reduce(operator.mul, input.shape) macs = macs_per_elem * num_elem flops = 2 * macs if self.bias is not None: flops += reduce(operator.mul, result_shape) return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.Embedding) def torch_nn_embedding(self: torch.nn.Embedding, input: torch.Tensor) -> Tuple[int, int]: # nn.Embedding is a dictionary lookup, so technically it has 0 FLOPs. (https://discuss.pytorch.org/t/correct-way-to-calculate-flops-in-model/67198/6) flops = 0 macs = 0 return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.Linear) @meta_profiler_module.register(torch.nn.modules.linear.NonDynamicallyQuantizableLinear) def torch_nn_linear(self: torch.nn.Linear, input: torch.Tensor) -> Tuple[int, int]: out_features = self.weight.shape[0] macs = input.numel() * out_features flops = 2 * macs if self.bias is not None: flops += self.bias.numel() return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.AvgPool1d) @meta_profiler_module.register(torch.nn.AvgPool2d) @meta_profiler_module.register(torch.nn.AvgPool3d) @meta_profiler_module.register(torch.nn.MaxPool1d) @meta_profiler_module.register(torch.nn.MaxPool2d) @meta_profiler_module.register(torch.nn.MaxPool3d) @meta_profiler_module.register(torch.nn.AdaptiveAvgPool1d) @meta_profiler_module.register(torch.nn.AdaptiveMaxPool1d) @meta_profiler_module.register(torch.nn.AdaptiveAvgPool2d) @meta_profiler_module.register(torch.nn.AdaptiveMaxPool2d) @meta_profiler_module.register(torch.nn.AdaptiveAvgPool3d) @meta_profiler_module.register(torch.nn.AdaptiveMaxPool3d) def torch_nn_pooling(self: torch.nn.Module, input: torch.Tensor) -> Tuple[int, int]: # all pooling could be considered as going over each input element only once (https://stackoverflow.com/a/67301217) flops = input.numel() macs = 0 return flops, macs
from .activation_function import * from .attention import * from .convolution import * from .dropout import * from .embedding import * from .linear import * from .normalization import * from .pooling import * from .rnn import * from .torch_op import *
import operator import torch from ..registry import meta_profiler_module from typing import Optional, Tuple, Union @meta_profiler_module.register(torch.nn.Flatten) def torch_nn_flatten(self: torch.nn.Flatten, input: torch.Tensor) -> Tuple[int, int]: flops = 0 macs = 0 return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.Dropout) def torch_nn_dropout(self: torch.nn.Module, input: torch.Tensor) -> Tuple[int, int]: # nn.Embedding is a dictionary lookup, so technically it has 0 FLOPs. (https://discuss.pytorch.org/t/correct-way-to-calculate-flops-in-model/67198/6) flops = 0 macs = 0 return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_module # TODO: different activation has different FLOPs count, currently unused. _multiplier = { torch.nn.ReLU: 1, torch.nn.PReLU: 4, torch.nn.Sigmoid: 4, torch.nn.Tanh: 5, torch.nn.LeakyReLU: 3, torch.nn.ELU: 4, torch.nn.ReLU6: 2, torch.nn.GELU: 9, torch.nn.Hardswish: 5, torch.nn.Hardsigmoid: 4, } @meta_profiler_module.register(torch.nn.ELU) @meta_profiler_module.register(torch.nn.LeakyReLU) @meta_profiler_module.register(torch.nn.ReLU) @meta_profiler_module.register(torch.nn.GELU) @meta_profiler_module.register(torch.nn.Sigmoid) @meta_profiler_module.register(torch.nn.Tanh) @meta_profiler_module.register(torch.nn.ReLU6) @meta_profiler_module.register(torch.nn.PReLU) @meta_profiler_module.register(torch.nn.Hardswish) @meta_profiler_module.register(torch.nn.Hardsigmoid) def torch_nn_non_linear_act(self: torch.nn.Module, input: torch.Tensor) -> Tuple[int, int]: flops = input.numel() macs = 0 return flops, macs
from typing import Tuple, Union import torch from ..registry import meta_profiler_module @meta_profiler_module.register(torch.nn.InstanceNorm1d) @meta_profiler_module.register(torch.nn.InstanceNorm2d) @meta_profiler_module.register(torch.nn.InstanceNorm3d) @meta_profiler_module.register(torch.nn.LayerNorm) @meta_profiler_module.register(torch.nn.GroupNorm) @meta_profiler_module.register(torch.nn.BatchNorm1d) @meta_profiler_module.register(torch.nn.BatchNorm2d) @meta_profiler_module.register(torch.nn.BatchNorm3d) def torch_nn_normalize(self: Union[torch.nn.LayerNorm, torch.nn.GroupNorm, torch.nn.BatchNorm1d, torch.nn.BatchNorm2d, torch.nn.BatchNorm3d], input: torch.Tensor) -> Tuple[int, int]: # adopted from https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler/profiler.py#L615 has_affine = self.weight is not None if self.training: flops = input.numel() * (2 if has_affine else 1) else: flops = input.numel() * (5 if has_affine else 4) macs = 0 return flops, macs try: import apex meta_profiler_module.register(apex.normalization.FusedLayerNorm)(torch_nn_normalize) meta_profiler_module.register(apex.normalization.FusedRMSNorm)(torch_nn_normalize) meta_profiler_module.register(apex.normalization.MixedFusedLayerNorm)(torch_nn_normalize) meta_profiler_module.register(apex.normalization.MixedFusedRMSNorm)(torch_nn_normalize) except (ImportError, AttributeError): pass
from functools import reduce import operator import torch from ..registry import meta_profiler_module from typing import Optional, Tuple, Union def _rnn_flops(flops: int, macs: int, module: torch.nn.RNNBase, w_ih: torch.Tensor, w_hh: torch.Tensor) -> Tuple[int, int]: # copied from https://github.com/sovrasov/flops-counter.pytorch/blob/master/ptflops/pytorch_ops.py # matrix matrix mult ih state and internal state macs += reduce(operator.mul, w_ih.shape) flops += 2 * reduce(operator.mul, w_ih.shape) # matrix matrix mult hh state and internal state macs += reduce(operator.mul, w_hh.shape) flops += 2 * reduce(operator.mul, w_hh.shape) if isinstance(module, (torch.nn.RNN, torch.nn.RNNCell)): # add both operations flops += module.hidden_size elif isinstance(module, (torch.nn.GRU, torch.nn.GRUCell)): # hadamard of r flops += module.hidden_size # adding operations from both states flops += module.hidden_size * 3 # last two hadamard product and add flops += module.hidden_size * 3 elif isinstance(module, (torch.nn.LSTM, torch.nn.LSTMCell)): # adding operations from both states flops += module.hidden_size * 4 # two hadamard product and add for C state flops += module.hidden_size * 3 # final hadamard flops += module.hidden_size * 3 return flops, macs @meta_profiler_module.register(torch.nn.LSTM) @meta_profiler_module.register(torch.nn.GRU) @meta_profiler_module.register(torch.nn.RNN) def torch_nn_rnn(self: torch.nn.RNNBase, input: torch.Tensor, hx: Optional[torch.Tensor] = None) -> Tuple[int, int]: flops = 0 macs = 0 for i in range(self.num_layers): w_ih = self.__getattr__('weight_ih_l' + str(i)) w_hh = self.__getattr__('weight_hh_l' + str(i)) flops, macs = _rnn_flops(flops, macs, self, w_ih, w_hh) if self.bias: b_ih = self.__getattr__('bias_ih_l' + str(i)) b_hh = self.__getattr__('bias_hh_l' + str(i)) flops += reduce(operator.mul, b_ih) + reduce(operator.mul, b_hh) flops *= reduce(operator.mul, input.shape[:2]) macs *= reduce(operator.mul, input.shape[:2]) if self.bidirectional: flops *= 2 macs *= 2 return flops, macs @meta_profiler_module.register(torch.nn.LSTMCell) @meta_profiler_module.register(torch.nn.GRUCell) @meta_profiler_module.register(torch.nn.RNNCell) def torch_nn_rnn(self: torch.nn.RNNCellBase, input: torch.Tensor, hx: Optional[torch.Tensor] = None) -> Tuple[int, int]: flops = 0 macs = 0 w_ih = self.__getattr__('weight_ih_l') w_hh = self.__getattr__('weight_hh_l') flops, macs = _rnn_flops(flops, macs, self, w_ih, w_hh) if self.bias: b_ih = self.__getattr__('bias_ih_l') b_hh = self.__getattr__('bias_hh_l') flops += reduce(operator.mul, b_ih) + reduce(operator.mul, b_hh) flops *= input.shape[0] macs *= input.shape[0] return flops, macs
import operator from functools import reduce from typing import Any, Optional, Tuple, Union import torch from ..registry import meta_profiler_function def _elementwise_flops_compute(input, other): # copied from https://github.com/microsoft/DeepSpeed/blob/master/deepspeed/profiling/flops_profiler/profiler.py#L763 if not torch.is_tensor(input): if torch.is_tensor(other): return reduce(operator.mul, other.shape), 0 else: return 1, 0 elif not torch.is_tensor(other): return reduce(operator.mul, input.shape), 0 else: dim_input = len(input.shape) dim_other = len(other.shape) max_dim = max(dim_input, dim_other) final_shape = [] for i in range(max_dim): in_i = input.shape[i] if i < dim_input else 1 ot_i = other.shape[i] if i < dim_other else 1 if in_i > ot_i: final_shape.append(in_i) else: final_shape.append(ot_i) flops = reduce(operator.mul, final_shape) return flops, 0 @meta_profiler_function.register(torch.add) @meta_profiler_function.register(torch.eq) @meta_profiler_function.register(torch.sub) @meta_profiler_function.register(torch.mul) @meta_profiler_function.register(torch.floor_divide) @meta_profiler_function.register('add') # for built-in op + @meta_profiler_function.register('iadd') # for built-in op += @meta_profiler_function.register('eq') # for built-in op = @meta_profiler_function.register('sub') # for built-in op - @meta_profiler_function.register('isub') # for built-in op -= @meta_profiler_function.register('mul') # for built-in op * @meta_profiler_function.register('imul') # for built-in op *= @meta_profiler_function.register('floordiv') # for built-in op // @meta_profiler_function.register('ifloordiv') # for built-in op //= def torch_add_like_ops(input: Any, other: Any, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: return _elementwise_flops_compute(input, other) @meta_profiler_function.register(torch.abs) def torch_elementwise_op(input: torch.Tensor, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: flops = input.numel() macs = 0 return flops, macs @meta_profiler_function.register(torch.matmul) @meta_profiler_function.register('matmul') # for built-in op @ @meta_profiler_function.register(torch.Tensor.matmul) def torch_matmul(input: torch.Tensor, other: torch.Tensor, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: macs = reduce(operator.mul, input.shape) * other.shape[-1] flops = 2 * macs return flops, macs @meta_profiler_function.register(torch.bmm) def torch_bmm(input: torch.Tensor, other: torch.Tensor, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: macs = reduce(operator.mul, input.shape) * other.shape[-1] flops = 2 * macs return flops, macs @meta_profiler_function.register(torch.var_mean) def torch_var_mean(input: torch.Tensor, dim: Union[int, Tuple[int, ...]], unbiased: Optional[bool] = True, keepdim: Optional[bool] = False, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: assert out is None, 'saving to out is not supported yet' flops = input.numel() * 3 macs = 0 return flops, macs
import torch from typing import Optional from ..registry import meta_profiler_function @meta_profiler_function.register(torch.nn.functional.embedding) def torch_nn_functional_embedding( input: torch.Tensor, weight: torch.Tensor, padding_idx: Optional[int] = None, max_norm: Optional[float] = None, norm_type: float = 2.0, scale_grad_by_freq: bool = False, sparse: bool = False, ) -> torch.Tensor: # F.embedding is a dictionary lookup, so technically it has 0 FLOPs. (https://discuss.pytorch.org/t/correct-way-to-calculate-flops-in-model/67198/6) flops = 0 macs = 0 return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_function @meta_profiler_function.register(torch.nn.functional.linear) def torch_nn_linear(input: torch.Tensor, weight: torch.Tensor, bias: torch.Tensor = None) -> Tuple[int, int]: out_features = weight.shape[0] macs = torch.numel(input) * out_features flops = 2 * macs if bias is not None: flops += bias.numel() return flops, macs
from typing import Tuple, Union import torch from ..registry import meta_profiler_function @meta_profiler_function.register(torch.nn.functional.avg_pool1d) @meta_profiler_function.register(torch.nn.functional.avg_pool2d) @meta_profiler_function.register(torch.nn.functional.avg_pool3d) @meta_profiler_function.register(torch.nn.functional.max_pool1d) @meta_profiler_function.register(torch.nn.functional.max_pool2d) @meta_profiler_function.register(torch.nn.functional.max_pool3d) @meta_profiler_function.register(torch.nn.functional.adaptive_avg_pool1d) @meta_profiler_function.register(torch.nn.functional.adaptive_avg_pool2d) @meta_profiler_function.register(torch.nn.functional.adaptive_avg_pool3d) @meta_profiler_function.register(torch.nn.functional.adaptive_max_pool1d) @meta_profiler_function.register(torch.nn.functional.adaptive_max_pool2d) @meta_profiler_function.register(torch.nn.functional.adaptive_max_pool3d) def torch_nn_func_pooling(input: torch.Tensor, *args, **kwargs) -> Tuple[int, int]: # all pooling could be considered as going over each input element only once (https://stackoverflow.com/a/67301217) flops = input.numel() macs = 0 return flops, macs
from .activation_function import * from .arithmetic import * from .embedding import * from .linear import * from .normalization import * from .pooling import * from .python_ops import * from .torch_ops import *
import operator from typing import Any, Tuple import torch from ..registry import meta_profiler_function @meta_profiler_function.register(operator.getitem) def operator_getitem(a: Any, b: Any) -> Tuple[int, int]: flops = 0 macs = 0 return flops, macs @meta_profiler_function.register(getattr) def python_getattr(a: Any, b: Any) -> Tuple[int, int]: flops = 0 macs = 0 return flops, macs
from functools import reduce import operator from typing import Any, Optional, Tuple import torch from ..registry import meta_profiler_function @meta_profiler_function.register(torch.arange) @meta_profiler_function.register(torch.finfo) @meta_profiler_function.register(torch.permute) @meta_profiler_function.register(torch.Tensor.permute) @meta_profiler_function.register(torch.Tensor.repeat) @meta_profiler_function.register(torch.index_select) @meta_profiler_function.register(torch.Tensor.index_select) @meta_profiler_function.register(torch.squeeze) @meta_profiler_function.register(torch.Tensor.squeeze) @meta_profiler_function.register(torch.unsqueeze) @meta_profiler_function.register(torch.Tensor.unsqueeze) @meta_profiler_function.register(torch.cat) @meta_profiler_function.register(torch.concat) @meta_profiler_function.register(torch.repeat_interleave) @meta_profiler_function.register(torch.Tensor.repeat_interleave) @meta_profiler_function.register(torch.flatten) @meta_profiler_function.register(torch.Tensor.flatten) @meta_profiler_function.register(torch.roll) @meta_profiler_function.register(torch.full) @meta_profiler_function.register(torch.Tensor.cpu) @meta_profiler_function.register(torch.Tensor.cuda) @meta_profiler_function.register(torch._assert) def torch_zero_flops_op(*args, **kwargs) -> Tuple[int, int]: flops = 0 macs = 0 return flops, macs @meta_profiler_function.register(torch.where) def torch_where(condition: torch.Tensor, x: Any, y: Any) -> Tuple[int, int]: # torch.where returns the broadcasted tensor of condition, x, and y, # so hack it by using addition flops = condition.numel() macs = 0 return flops, macs @meta_profiler_function.register(torch.max) def torch_max(input: torch.Tensor, dim: int = None, keepdim: bool = False, *, out: Optional[torch.Tensor] = None) -> Tuple[int, int]: macs = 0 assert out is None, 'assigning value to out is not supported yet' if dim is not None: shape = list(input.shape) shape.pop(int(dim)) flops = reduce(operator.mul, shape), macs return flops, macs else: flops = input.numel() return flops, macs
from typing import Tuple import torch from ..registry import meta_profiler_function # TODO: different activation has different FLOPs count, currently unused. _multiplier = { torch.nn.functional.relu: 1, torch.nn.functional.prelu: 4, torch.nn.functional.sigmoid: 4, torch.nn.functional.tanh: 5, torch.nn.functional.leaky_relu: 3, torch.nn.functional.elu: 4, torch.nn.functional.relu6: 2, torch.nn.functional.gelu: 9, torch.nn.functional.hardswish: 5, torch.nn.functional.hardsigmoid: 4, } @meta_profiler_function.register(torch.nn.functional.leaky_relu) @meta_profiler_function.register(torch.nn.functional.elu) @meta_profiler_function.register(torch.nn.functional.gelu) @meta_profiler_function.register(torch.nn.functional.relu6) @meta_profiler_function.register(torch.nn.functional.prelu) @meta_profiler_function.register(torch.nn.functional.relu) @meta_profiler_function.register(torch.nn.functional.sigmoid) @meta_profiler_function.register(torch.nn.functional.tanh) @meta_profiler_function.register(torch.nn.functional.hardswish) @meta_profiler_function.register(torch.nn.functional.hardsigmoid) def torch_nn_func_non_linear_act(input: torch.Tensor, inplace: bool = False) -> Tuple[int, int]: flops = input.numel() macs = 0 return flops, macs
from typing import List, Optional, Tuple import torch from ..registry import meta_profiler_function @meta_profiler_function.register(torch.nn.functional.instance_norm) def torch_nn_func_instancenorm( input: torch.Tensor, running_mean: Optional[torch.Tensor] = None, running_var: Optional[torch.Tensor] = None, weight: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None, use_input_stats: bool = True, momentum: float = 0.1, eps: float = 1e-5, ): has_affine = weight is not None flops = input.numel() * (5 if has_affine else 4) macs = 0 return flops, macs @meta_profiler_function.register(torch.nn.functional.group_norm) def torch_nn_func_groupnorm(input: torch.Tensor, num_groups: int, weight: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None, eps: float = 1e-5) -> Tuple[int, int]: has_affine = weight is not None flops = input.numel() * (5 if has_affine else 4) macs = 0 return flops, macs @meta_profiler_function.register(torch.nn.functional.layer_norm) def torch_nn_func_layernorm( input: torch.Tensor, normalized_shape: List[int], weight: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None, eps: float = 1e-5, ) -> Tuple[int, int]: has_affine = weight is not None flops = input.numel() * (5 if has_affine else 4) macs = 0 return flops, macs @meta_profiler_function.register(torch.nn.functional.batch_norm) def torch_nn_func_batchnorm( input: torch.Tensor, running_mean: Optional[torch.Tensor], running_var: Optional[torch.Tensor], weight: Optional[torch.Tensor] = None, bias: Optional[torch.Tensor] = None, training: bool = False, momentum: float = 0.1, eps: float = 1e-5, ) -> Tuple[int, int]: has_affine = weight is not None if training: flops = input.numel() * (2 if has_affine else 1) else: flops = input.numel() * (5 if has_affine else 4) macs = 0 return flops, macs
import torch import gc import psutil from collections import namedtuple from colossalai.context.parallel_mode import ParallelMode from colossalai.utils import get_current_device from colossalai.core import global_context as gpc from colossalai.context.parallel_mode import ParallelMode from colossalai.logging import get_dist_logger from packaging import version _GLOBAL_CUDA_MEM_FRACTION = 1.0 _GLOBAL_CPU_MEM_CAPACITY = -1 def _bytes_to_MB(val, decimal=2): """A byte-to-Megabyte converter, default using binary notation. :param val: X bytes to convert :return: X' MB """ return round(val / (1024 * 1024), decimal) # copy from PatrickStar def _get_cpu_memory_info(): ps_mem_info = namedtuple("ps_mem_info", ["total", "free", "cached", "buffers", "used"]) try: # psutil reads the memory info from /proc/memory_info, # which results in returning the host memory instead of # that of container. # Here we try to read the container memory with method in: # https://stackoverflow.com/a/46213331/5163915 mems = {} with open("/sys/fs/cgroup/memory/memory.meminfo", "rb") as f: for line in f: fields = line.split() mems[fields[0]] = int(fields[1]) * 1024 total = mems[b"MemTotal:"] free = mems[b"MemFree:"] cached = mems[b"Cached:"] buffers = mems[b"Buffers:"] used = total - free - cached - buffers if used < 0: used = total - free mem_info = ps_mem_info(total=total, free=free, cached=cached, buffers=buffers, used=used) except FileNotFoundError: mems = psutil.virtual_memory() mem_info = ps_mem_info( total=mems.total, free=mems.free, cached=mems.cached, buffers=mems.buffers, used=mems.used, ) return mem_info def report_memory_usage(message, logger=None, report_cpu=False): """Calculate and print RAM usage (in GB) Args: message (str): A prefix message to add in the log. logger (:class:`colossalai.logging.DistributedLogger`): The logger used to record memory information. report_cpu (bool, optional): Whether to report CPU memory. Raises: EnvironmentError: Raise error if no distributed environment has been initialized. """ if not gpc.is_initialized(ParallelMode.GLOBAL): raise EnvironmentError("No distributed environment is initialized") gpu_allocated = _bytes_to_MB(torch.cuda.memory_allocated()) gpu_max_allocated = _bytes_to_MB(torch.cuda.max_memory_allocated()) gpu_cached = _bytes_to_MB(torch.cuda.memory_reserved()) gpu_max_cached = _bytes_to_MB(torch.cuda.max_memory_reserved()) full_log = f"{message}: GPU: allocated {gpu_allocated} MB, max allocated {gpu_max_allocated} MB, " \ + f"cached: {gpu_cached} MB, max cached: {gpu_max_cached} MB" if report_cpu: # python doesn't do real-time garbage collection so do it explicitly to get the correct RAM reports gc.collect() vm_stats = psutil.virtual_memory() vm_used = _bytes_to_MB(vm_stats.total - vm_stats.available) full_log += f", CPU Virtual Memory: used = {vm_used} MB, percent = {vm_stats.percent}%" if logger is None: logger = get_dist_logger() logger.info(full_log) # get the peak memory to report correct data, so reset the counter for the next call if hasattr(torch.cuda, "reset_peak_memory_stats"): # pytorch 1.4+ torch.cuda.reset_peak_memory_stats() def colo_device_memory_capacity(device: torch.device) -> int: """ Get the capacity of the memory of the device Args: device (torch.device): a device Returns: int: size in byte """ assert isinstance(device, torch.device) if device.type == 'cpu': # In the context of 1-CPU-N-GPU, the memory capacity of the current process is 1/N overall CPU memory. return colo_get_cpu_memory_capacity() / gpc.num_processes_on_current_node if device.type == 'cuda': return torch.cuda.get_device_properties(get_current_device()).total_memory * _GLOBAL_CUDA_MEM_FRACTION def colo_device_memory_used(device: torch.device) -> int: """ Get the device memory on device belonging to the current process. Args: device (torch.device): a device Returns: int: memory size in bytes """ if device.type == 'cpu': mem_info = _get_cpu_memory_info() # In the context of 1-CPU-N-GPU, the memory usage of the current process is 1/N CPU memory used. # Each process consumes the same amount of memory. ret = mem_info.used / gpc.num_processes_on_current_node return ret elif device.type == 'cuda': ret: int = torch.cuda.memory_allocated(device) # get the peak memory to report correct data, so reset the counter for the next call if hasattr(torch.cuda, "reset_peak_memory_stats"): # pytorch 1.4+ torch.cuda.reset_peak_memory_stats(device) return ret def colo_set_process_memory_fraction(ratio: float) -> None: """colo_set_process_memory_fraction set how much cuda memory used on the gpu belonging to the current process. Args: ratio (float): a ratio between 0. ~ 1. """ if version.parse(torch.__version__) < version.parse('1.8'): logger = get_dist_logger('colo_set_process_memory_fraction') logger.warning('colo_set_process_memory_fraction failed because torch version is less than 1.8') return global _GLOBAL_CUDA_MEM_FRACTION _GLOBAL_CUDA_MEM_FRACTION = ratio torch.cuda.set_per_process_memory_fraction(_GLOBAL_CUDA_MEM_FRACTION, get_current_device()) def colo_set_cpu_memory_capacity(size: int) -> None: global _GLOBAL_CPU_MEM_CAPACITY mem_info = _get_cpu_memory_info() total_size = mem_info.total if size <= total_size: _GLOBAL_CPU_MEM_CAPACITY = size else: _GLOBAL_CPU_MEM_CAPACITY = total_size def colo_get_cpu_memory_capacity() -> int: """ Get the cpu memory capacity. We may not use all of it. Returns: int: _description_ """ global _GLOBAL_CPU_MEM_CAPACITY if _GLOBAL_CPU_MEM_CAPACITY == -1: mem_info = _get_cpu_memory_info() return mem_info.total else: return _GLOBAL_CPU_MEM_CAPACITY
#!/usr/bin/env python # -*- encoding: utf-8 -*- import time from typing import Tuple from .cuda import synchronize class Timer: """A timer object which helps to log the execution times, and provides different tools to assess the times. """ def __init__(self): self._started = False self._start_time = time.time() self._elapsed = 0 self._history = [] @property def has_history(self): return len(self._history) != 0 @property def current_time(self) -> float: synchronize() return time.time() def start(self): """Firstly synchronize cuda, reset the clock and then start the timer. """ self._elapsed = 0 synchronize() self._start_time = time.time() self._started = True def lap(self): """lap time and return elapsed time """ return self.current_time - self._start_time def stop(self, keep_in_history: bool = False): """Stop the timer and record the start-stop time interval. Args: keep_in_history (bool, optional): Whether does it record into history each start-stop interval, defaults to False. Returns: int: Start-stop interval. """ synchronize() end_time = time.time() elapsed = end_time - self._start_time if keep_in_history: self._history.append(elapsed) self._elapsed = elapsed self._started = False return elapsed def get_history_mean(self): """Mean of all history start-stop time intervals. Returns: int: Mean of time intervals """ return sum(self._history) / len(self._history) def get_history_sum(self): """Add up all the start-stop time intervals. Returns: int: Sum of time intervals. """ return sum(self._history) def get_elapsed_time(self): """Return the last start-stop time interval. Returns: int: The last time interval. Note: Use it only when timer is not in progress """ assert not self._started, 'Timer is still in progress' return self._elapsed def reset(self): """Clear up the timer and its history """ self._history = [] self._started = False self._elapsed = 0 class MultiTimer: """An object contains multiple timers. Args: on (bool, optional): Whether the timer is enabled. Default is True. """ def __init__(self, on: bool = True): self._on = on self._timers = dict() def start(self, name: str): """Start namely one of the timers. Args: name (str): Timer's key. """ if self._on: if name not in self._timers: self._timers[name] = Timer() return self._timers[name].start() def stop(self, name: str, keep_in_history: bool): """Stop namely one of the timers. Args: name (str): Timer's key. keep_in_history (bool): Whether does it record into history each start-stop interval. """ if self._on: return self._timers[name].stop(keep_in_history) else: return None def get_timer(self, name): """Get timer by its name (from multitimer) Args: name (str): Timer's key. Returns: :class:`colossalai.utils.Timer`: Timer with the name you give correctly. """ return self._timers[name] def reset(self, name=None): """Reset timers. Args: name (str, optional): If name is designated, the named timer will be reset and others will not, defaults to None. """ if self._on: if name is not None: self._timers[name].reset() else: for timer in self._timers: timer.reset() def is_on(self): return self._on def set_status(self, mode: bool): self._on = mode def __iter__(self) -> Tuple[str, Timer]: for name, timer in self._timers.items(): yield name, timer
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch from torch.utils.checkpoint import check_backward_validity, detach_variable from colossalai.context.random import get_states, get_current_mode, set_seed_states, set_mode, sync_states from .cuda import get_current_device import weakref def copy_to_device(obj, device): if torch.is_tensor(obj): # Notice: # When in no_grad context, requires_gard is False after movement ret = obj.to(device).detach() ret.requires_grad = obj.requires_grad return ret elif isinstance(obj, list): return [copy_to_device(i, device) for i in obj] elif isinstance(obj, tuple): return tuple([copy_to_device(v, device) for v in obj]) elif isinstance(obj, dict): return {k: copy_to_device(v, device) for k, v in obj.items()} else: return obj class CheckpointFunction(torch.autograd.Function): @staticmethod def forward(ctx, run_function, activation_offload=False, *args): check_backward_validity(args) ctx.run_function = run_function ctx.activation_offload = activation_offload ctx.device = get_current_device() # preserve rng states ctx.fwd_cpu_rng_state = torch.get_rng_state() sync_states() ctx.fwd_seed_states = get_states(copy=True) ctx.fwd_current_mode = get_current_mode() if hasattr(torch, 'is_autocast_enabled'): ctx.had_autocast_in_fwd = torch.is_autocast_enabled() else: ctx.had_autocast_in_fwd = False if activation_offload: inputs_cuda = copy_to_device(args, ctx.device) else: inputs_cuda = args with torch.no_grad(): outputs = run_function(*inputs_cuda) # Save non-tensor inputs in ctx, keep a placeholder None for tensors # to be filled out during the backward. ctx.inputs = [] ctx.tensor_indices = [] tensor_inputs = [] for i, arg in enumerate(args): if torch.is_tensor(arg): if activation_offload: tensor_inputs.append(copy_to_device(arg, 'cpu')) else: tensor_inputs.append(arg) ctx.tensor_indices.append(i) ctx.inputs.append(None) else: ctx.inputs.append(arg) if activation_offload: ctx.tensor_inputs = tensor_inputs else: ctx.save_for_backward(*tensor_inputs) return outputs @staticmethod def backward(ctx, *args): if not torch.autograd._is_checkpoint_valid(): raise RuntimeError("Checkpointing is not compatible with .grad() or when an `inputs` parameter is " "passed to .backward(). Please use .backward() and do not pass its `inputs` argument.") # Copy the list to avoid modifying original list. inputs = list(ctx.inputs) tensor_indices = ctx.tensor_indices if ctx.activation_offload: tensors = ctx.tensor_inputs else: tensors = ctx.saved_tensors # store the current states bwd_cpu_rng_state = torch.get_rng_state() sync_states() bwd_seed_states = get_states(copy=True) bwd_current_mode = get_current_mode() # set the states to what it used to be torch.set_rng_state(ctx.fwd_cpu_rng_state) for parallel_mode, state in ctx.fwd_seed_states.items(): set_seed_states(parallel_mode, state) set_mode(ctx.fwd_current_mode) if ctx.activation_offload: tensors = copy_to_device(tensors, ctx.device) # Fill in inputs with appropriate saved tensors. for i, idx in enumerate(tensor_indices): inputs[idx] = tensors[i] detached_inputs = detach_variable(tuple(inputs)) if ctx.had_autocast_in_fwd: with torch.enable_grad(), torch.cuda.amp.autocast(): outputs = ctx.run_function(*detached_inputs) else: with torch.enable_grad(): outputs = ctx.run_function(*detached_inputs) if isinstance(outputs, torch.Tensor): outputs = (outputs,) # recover the rng states torch.set_rng_state(bwd_cpu_rng_state) for parallel_mode, state in bwd_seed_states.items(): set_seed_states(parallel_mode, state) set_mode(bwd_current_mode) # run backward() with only tensor that requires grad outputs_with_grad = [] args_with_grad = [] for i in range(len(outputs)): if torch.is_tensor(outputs[i]) and outputs[i].requires_grad: outputs_with_grad.append(outputs[i]) args_with_grad.append(args[i]) if len(outputs_with_grad) == 0: raise RuntimeError("none of output has requires_grad=True," " this checkpoint() is not necessary") torch.autograd.backward(outputs_with_grad, args_with_grad) grads = tuple(inp.grad if isinstance(inp, torch.Tensor) else None for inp in detached_inputs) return (None, None) + grads def checkpoint(function, activation_offload, *args, use_reentrant: bool = True): """Checkpoint the computation while preserve the rng states, modified from Pytorch torch.utils.checkpoint. Args: function: Describe the forward pass function. It should know how to handle the input tuples. activation_offload: The variable to check whether we should offload activation to cpu args (list): Tuple containing the parameters of the function use_reentrant: Bool type to check if we need to use_reentrant, if use_reentrant=False, there might be more flexibility for user to define there checkpoint function Returns: Output of running function with provided args. """ if use_reentrant: return CheckpointFunction.apply(function, activation_offload, *args) else: return _checkpoint_without_reentrant( function, activation_offload, *args, ) def _checkpoint_without_reentrant(function, activation_offload=False, *args): # store rng_state fwd_cpu_state = torch.get_rng_state() sync_states() fwd_seed_states = get_states(copy=True) fwd_current_mode = get_current_mode() # check if use autocast if hasattr(torch, 'is_autocast_enabled'): has_autocast_in_fwd = torch.is_autocast_enabled() else: has_autocast_in_fwd = False # using WeakKeyDictionary to store all the activation the first time we call unpack storage: weakref.WeakKeyDictionary = weakref.WeakKeyDictionary() weak_holder_list = [] # class for weakref.ref class Holder(): pass # return a Holder object for later unpack process def pack(x): res = Holder() weak_holder_list.append(weakref.ref(res)) return res # unpack hook def unpack(x): unpack_counter = 0 # re-compute all the activation inside the function when we first call unpack if len(storage) == 0: def inner_pack(inner): nonlocal unpack_counter unpack_counter += 1 # If the holder went out of scope, the SavedVariable is dead and so # the value will never be read from the storage. Skip filling it. if weak_holder_list[unpack_counter - 1]() is None: return # Use detach here to ensure we don't keep the temporary autograd # graph created during the second forward storage[weak_holder_list[unpack_counter - 1]()] = inner.detach() return def inner_unpack(packed): raise RuntimeError("You are calling backwards on a tensor that is never exposed. Please open an issue.") # restore rng state torch.set_rng_state(fwd_cpu_state) for parallel_mode, state in fwd_seed_states.items(): set_seed_states(parallel_mode, state) set_mode(fwd_current_mode) # reload arg into device if needed if activation_offload: for arg in args: if torch.is_tensor(arg): arg = arg.to(device=device) # rerun forward, the inner_pack will store all the activations in storage if has_autocast_in_fwd: with torch.enable_grad(), \ torch.cuda.amp.autocast(), \ torch.autograd.graph.saved_tensors_hooks(inner_pack, inner_unpack): _unused = function(*args) else: with torch.enable_grad(), \ torch.autograd.graph.saved_tensors_hooks(inner_pack, inner_unpack): _unused = function(*args) if x not in storage: raise RuntimeError("Attempt to retrieve a tensor saved by autograd multiple times without checkpoint" " recomputation being triggered in between, this is not currently supported. Please" " open an issue with details on your use case so that we can prioritize adding this.") return storage[x] # get device if we need to offload the activation if activation_offload: device = get_current_device() # run function with pack and unpack as saved_tensors_hooks with torch.autograd.graph.saved_tensors_hooks(pack, unpack): output = function(*args) # offload activation if needed if activation_offload: for arg in args: if torch.is_tensor(arg): arg = arg.to(device="cpu") return output
from .activation_checkpoint import checkpoint from .checkpointing import load_checkpoint, save_checkpoint from .common import ( clip_grad_norm_fp32, conditional_context, copy_tensor_parallel_attributes, count_zeros_fp32, disposable, ensure_path_exists, free_port, is_ddp_ignored, is_dp_rank_0, is_model_parallel_parameter, is_no_pp_or_last_stage, is_tp_rank_0, is_using_ddp, is_using_pp, is_using_sequence, multi_tensor_applier, param_is_not_tensor_parallel_duplicate, print_rank_0, switch_virtual_pipeline_parallel_rank, sync_model_param, ) from .cuda import empty_cache, get_current_device, set_to_cuda, synchronize from .data_sampler import DataParallelSampler, get_dataloader from .memory import ( colo_device_memory_capacity, colo_device_memory_used, colo_get_cpu_memory_capacity, colo_set_cpu_memory_capacity, colo_set_process_memory_fraction, report_memory_usage, ) from .tensor_detector import TensorDetector from .timer import MultiTimer, Timer __all__ = [ 'checkpoint', 'free_port', 'print_rank_0', 'sync_model_param', 'is_ddp_ignored', 'is_dp_rank_0', 'is_tp_rank_0', 'is_no_pp_or_last_stage', 'is_using_ddp', 'is_using_pp', 'is_using_sequence', 'conditional_context', 'is_model_parallel_parameter', 'clip_grad_norm_fp32', 'count_zeros_fp32', 'copy_tensor_parallel_attributes', 'param_is_not_tensor_parallel_duplicate', 'get_current_device', 'synchronize', 'empty_cache', 'set_to_cuda', 'report_memory_usage', 'colo_device_memory_capacity', 'colo_device_memory_used', 'colo_set_process_memory_fraction', 'Timer', 'MultiTimer', 'multi_tensor_applier', 'DataParallelSampler', 'get_dataloader', 'switch_virtual_pipeline_parallel_rank', 'TensorDetector', 'load_checkpoint', 'save_checkpoint', 'ensure_path_exists', 'disposable', 'colo_set_cpu_memory_capacity', 'colo_get_cpu_memory_capacity', ]
#!/usr/bin/env python # -*- encoding: utf-8 -*- import functools import os import random import socket from collections import defaultdict from contextlib import contextmanager from pathlib import Path from typing import Callable, Dict, List, Optional, Union import torch import torch.distributed as dist from torch._six import inf from torch.nn.parameter import Parameter from colossalai.constants import IS_TENSOR_PARALLEL, NUM_PARTITIONS, TENSOR_PARALLEL_ATTRIBUTES from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.global_variables import tensor_parallel_env as env from colossalai.tensor import ColoParameter, ProcessGroup from .multi_tensor_apply import multi_tensor_applier try: from colossalai._C import fused_optim except: fused_optim = None def print_rank_0(msg: str, logger=None): """Print messages and save logs(optional). This is executed only if you are the rank-0 gpu. Args: msg (str): A string message to output. logger (:class:`colossalai.logging.DistributedLogger`, optional): The logger to record the message, defaults to None. """ if gpc.get_global_rank() == 0: if logger is None: print(msg, flush=True) else: logger.info(msg) def ensure_path_exists(filename: str): # ensure the path exists dirpath = os.path.dirname(filename) if not os.path.exists(dirpath): Path(dirpath).mkdir(parents=True, exist_ok=True) def free_port(): while True: try: sock = socket.socket() sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) port = random.randint(20000, 65000) sock.bind(('localhost', port)) sock.close() return port except Exception: continue def sync_model_param(model, parallel_mode): r"""Make sure data parameters are consistent during Data Parallel Mode. Args: model (:class:`torch.nn.Module`): A pyTorch model on whose parameters you check the consistency. parallel_mode (:class:`colossalai.context.ParallelMode`): Parallel mode to be checked. 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>`_ """ if gpc.is_initialized(parallel_mode) and gpc.get_world_size(parallel_mode) > 1: for param in model.parameters(): ranks = gpc.get_ranks_in_group(parallel_mode) dist.broadcast(param, src=ranks[0], group=gpc.get_group(parallel_mode)) def is_dp_rank_0(): return not gpc.is_initialized(ParallelMode.DATA) or gpc.is_first_rank(ParallelMode.DATA) def is_tp_rank_0(): return not gpc.is_initialized(ParallelMode.TENSOR) or gpc.is_first_rank(ParallelMode.TENSOR) def is_no_pp_or_last_stage(): return not gpc.is_initialized(ParallelMode.PIPELINE) or gpc.is_last_rank(ParallelMode.PIPELINE) def is_using_ddp(): return gpc.is_initialized(ParallelMode.DATA) and gpc.get_world_size(ParallelMode.DATA) > 1 def is_using_pp(): return gpc.is_initialized(ParallelMode.PIPELINE) and gpc.get_world_size(ParallelMode.PIPELINE) > 1 def is_using_sequence(): return gpc.is_initialized(ParallelMode.SEQUENCE) and gpc.get_world_size(ParallelMode.SEQUENCE) > 1 @contextmanager def conditional_context(context_manager, enable=True): if enable: with context_manager: yield else: yield class model_branch_context(object): def __enter__(self): self.env_status = env.save() def __exit__(self, *exc_info): env.load(**self.env_status) def is_model_parallel_parameter(p): return hasattr(p, IS_TENSOR_PARALLEL) and getattr(p, IS_TENSOR_PARALLEL) def is_ddp_ignored(p): return getattr(p, '_ddp_to_ignore', False) def _calc_l2_norm(grads): # we should not global fused_optim if fused_optim is None: from colossalai.kernel.op_builder import FusedOptimBuilder fused_optim = FusedOptimBuilder().load() norm = 0.0 if len(grads) > 0: dummy_overflow_buf = torch.cuda.IntTensor([0]) norm, _ = multi_tensor_applier( fused_optim.multi_tensor_l2norm, dummy_overflow_buf, [grads], False # no per-parameter norm ) return norm def _calc_lp(grads, norm_type): norm = 0.0 for grad in grads: grad_norm = torch.norm(grad, norm_type) norm += grad_norm**norm_type return norm def _move_norm_to_cuda(norm: Union[float, torch.Tensor]) -> Union[float, torch.Tensor]: if torch.is_tensor(norm) and norm.device.type != 'cuda': norm = norm.to(torch.cuda.current_device()) return norm def _get_tensor_norm(norm: Union[float, torch.Tensor], move_to_cuda) -> torch.Tensor: if isinstance(norm, float): norm = torch.Tensor([norm]) if move_to_cuda: norm = norm.to(torch.cuda.current_device()) return norm # ======== Gradient Clipping ========= def _compute_local_lp(params: List[ColoParameter], norm_type: float) -> float: if len(params) == 0: return 0.0 grads = [p.grad for p in params] use_cuda_kernel = grads[0].device.type == 'cuda' if norm_type == inf: local_lp = max([g.abs().max() for g in grads]) elif norm_type == 2.0 and use_cuda_kernel: local_lp = _calc_l2_norm(grads)**norm_type else: local_lp = _calc_lp(grads, norm_type) if isinstance(local_lp, torch.Tensor): return local_lp.item() return local_lp def _compute_buckets_lp(params: List[ColoParameter], norm_type: float) -> float: if len(params) == 0: return 0.0 buckets: Dict[Optional[ProcessGroup], List[ColoParameter]] = defaultdict(list) for p in params: if p.is_replicate(): buckets[None].append(p) else: buckets[p.get_process_group().tp_process_group()].append(p) total_lp = 0.0 for group, bucket in buckets.items(): local_lp = _compute_local_lp(bucket, norm_type) if group is not None: local_lp_tensor = torch.tensor([local_lp], device=torch.cuda.current_device()) if norm_type == inf: dist.all_reduce(local_lp_tensor, op=dist.ReduceOp.MAX, group=group) else: dist.all_reduce(local_lp_tensor, group=group) local_lp = local_lp_tensor.item() if norm_type == inf: total_lp = max(total_lp, local_lp) else: total_lp += local_lp return total_lp def _compute_pp_grad_lp(total_lp: float, norm_type: float) -> float: if gpc.is_initialized(ParallelMode.PIPELINE) and gpc.get_world_size(ParallelMode.PIPELINE) > 1: total_lp_tensor = torch.tensor([total_lp], device=torch.cuda.current_device()) if norm_type == inf: dist.all_reduce(total_lp_tensor, op=dist.ReduceOp.MAX, group=gpc.get_group(ParallelMode.PIPELINE)) else: dist.all_reduce(total_lp_tensor, group=gpc.get_group(ParallelMode.PIPELINE)) total_lp = total_lp_tensor.item() return total_lp def _compute_grad_lp(parameters, norm_type: float = 2.0) -> float: if isinstance(parameters, torch.Tensor): parameters = [parameters] grad_dtype = None cpu_grad_params: List[ColoParameter] = [] cuda_grad_params: List[ColoParameter] = [] for p in parameters: if p.grad is None: continue assert isinstance(p, ColoParameter) if grad_dtype is None: grad_dtype = p.grad.dtype assert p.grad.dtype == grad_dtype, f'Expected all grads are {grad_dtype}, got {p.grad.dtype}' if p.grad.device.type == 'cuda': cuda_grad_params.append(p) else: cpu_grad_params.append(p) norm_type = float(norm_type) cpu_lp = _compute_buckets_lp(cpu_grad_params, norm_type) cuda_lp = _compute_buckets_lp(cuda_grad_params, norm_type) if norm_type == inf: total_lp = max(cpu_lp, cuda_lp) else: total_lp = cpu_lp + cuda_lp return _compute_pp_grad_lp(total_lp, norm_type) def compute_grad_norm(parameters, norm_type: float = 2.0) -> float: norm_type = float(norm_type) total_norm = _compute_grad_lp(parameters, norm_type) if norm_type != inf: total_norm = total_norm**(1 / norm_type) return total_norm def _clip_grad_norm(parameters, max_norm: float, total_norm: float) -> None: clip_coef = max_norm / (total_norm + 1e-6) if clip_coef < 1.0: cuda_grads: List[torch.Tensor] = [] cpu_grads: List[torch.Tensor] = [] if isinstance(parameters, torch.Tensor): parameters = [parameters] for p in parameters: if p.grad is None: continue if p.grad.device.type == 'cuda': cuda_grads.append(p.grad.detach()) else: cpu_grads.append(p.grad.detach()) if len(cuda_grads) > 0: dummy_overflow_buf = torch.cuda.IntTensor([0]) multi_tensor_applier(fused_optim.multi_tensor_scale, dummy_overflow_buf, [cuda_grads, cuda_grads], clip_coef) for g in cpu_grads: g.mul_(clip_coef) def clip_grad_norm(parameters, max_norm: float, norm_type: float = 2.0) -> float: total_norm = compute_grad_norm(parameters, norm_type) _clip_grad_norm(parameters, max_norm, total_norm) return total_norm def clip_grad_norm_fp32(parameters, max_norm, norm_type=2): """Clips gradient norm of an iterable of parameters whose gradients are in fp32. This is adapted from :func:`torch.nn.utils.clip_grad.clip_grad_norm_` and added functionality to handle model parallel parameters. Note: the gradients are modified in place. Args: parameters (Iterable[:class:`torch.tensor`] or :class:`torch.tensor`): An iterable of Tensors or a single Tensor that will have gradients normalized. max_norm (Union[float, int]): Max norm of the gradients. norm_type (Union[float, int, 'inf']): Type of the used p-norm. Can be ``'inf'`` for infinity norm. Returns: float: Total norm of the parameters. """ if isinstance(parameters, torch.Tensor): parameters = [parameters] # Filter parameters based on: # - grad should not be none # - parameter should not be shared # - should not be a replica due to tensor model parallelism params: List[Parameter] = [] has_zero_shared_param: bool = False for param in parameters: if param.grad is not None: # Make sure the grads are in fp32 assert param.grad.dtype == torch.float, \ f'expected gradient to be dtype torch.float, but got {param.grad.type()}' if hasattr(param, 'colo_attr') and param.colo_attr.sharded_data_tensor.is_sharded: has_zero_shared_param = True params.append(param) if len(params) == 0: enable_cuda_kernels = False else: enable_cuda_kernels = params[0].grad.device.type == 'cuda' # Norm parameters. max_norm = float(max_norm) norm_type = float(norm_type) # Parameters can be on CPU or CUDA # If parameters are on CPU, disable CUDA kernerls # Calculate norm. if norm_type == inf: total_norm = max(p.grad.data.abs().max() for p in params) total_norm_cuda = torch.cuda.FloatTensor([float(total_norm)]) # Take max across all model-parallel GPUs. if gpc.is_initialized(ParallelMode.MODEL) and gpc.get_world_size(ParallelMode.MODEL) > 1: dist.all_reduce(total_norm_cuda, op=dist.ReduceOp.MAX, group=gpc.get_group(ParallelMode.MODEL), async_op=False) if has_zero_shared_param: dist.all_reduce(total_norm_cuda, op=dist.ReduceOp.MAX, group=gpc.get_group(ParallelMode.DATA), async_op=False) total_norm = total_norm_cuda[0].item() else: tensor_parallel_grads = [] no_tensor_parallel_grads = [] zero_sharded_grads = [] for p in params: if is_model_parallel_parameter(p): reductor = (gpc.get_world_size(ParallelMode.TENSOR) / getattr(p, NUM_PARTITIONS))**(1 / norm_type) tensor_parallel_grads.append(p.grad.data / reductor) elif hasattr(p, 'colo_attr') and p.colo_attr.sharded_data_tensor.is_sharded: zero_sharded_grads.append(p.grad.data) else: no_tensor_parallel_grads.append(p.grad.data) if norm_type == 2.0 and enable_cuda_kernels: tensor_parallel_norm = _calc_l2_norm(tensor_parallel_grads)**norm_type no_tensor_parallel_norm = _calc_l2_norm(no_tensor_parallel_grads)**norm_type zero_sharded_norm = _calc_l2_norm(zero_sharded_grads)**norm_type else: tensor_parallel_norm = _calc_lp(tensor_parallel_grads, norm_type) no_tensor_parallel_norm = _calc_lp(no_tensor_parallel_grads, norm_type) zero_sharded_norm = _calc_lp(zero_sharded_grads, norm_type) # If norm is type of float, then we convert them into torch.Tensor. tensor_parallel_norm = _get_tensor_norm(tensor_parallel_norm, enable_cuda_kernels) no_tensor_parallel_norm = _get_tensor_norm(no_tensor_parallel_norm, enable_cuda_kernels) zero_sharded_norm = _get_tensor_norm(zero_sharded_norm, enable_cuda_kernels) # If grads are on CPU, the norms is also on CPU. Cast them to CUDA tensors if not enable_cuda_kernels: tensor_parallel_norm = _move_norm_to_cuda(tensor_parallel_norm) no_tensor_parallel_norm = _move_norm_to_cuda(no_tensor_parallel_norm) zero_sharded_norm = _move_norm_to_cuda(zero_sharded_norm) # Sum across all model-parallel GPUs. if gpc.is_initialized(ParallelMode.TENSOR) and len(tensor_parallel_grads) > 0: dist.all_reduce(tensor_parallel_norm, op=dist.ReduceOp.SUM, group=gpc.get_group(ParallelMode.TENSOR)) # Sum across all zero sharded GPUs if len(zero_sharded_grads) > 0: dist.all_reduce(zero_sharded_norm, group=gpc.get_group(ParallelMode.DATA)) no_tensor_parallel_norm += zero_sharded_norm total_norm = tensor_parallel_norm + no_tensor_parallel_norm if gpc.is_initialized(ParallelMode.PIPELINE) and gpc.get_world_size(ParallelMode.PIPELINE) > 1: dist.all_reduce(total_norm, op=dist.ReduceOp.SUM, group=gpc.get_group(ParallelMode.PIPELINE)) total_norm = total_norm**(1.0 / norm_type) if torch.is_tensor(total_norm): total_norm = total_norm.item() # Scale. clip_coeff = max_norm / (total_norm + 1.0e-6) if clip_coeff < 1.0: if enable_cuda_kernels: grads = [p.grad.detach() for p in params] dummy_overflow_buf = torch.cuda.IntTensor([0]) multi_tensor_applier(fused_optim.multi_tensor_scale, dummy_overflow_buf, [grads, grads], clip_coeff) else: for p in params: p.grad.detach().mul_(clip_coeff) return total_norm def count_zeros_fp32(parameters): if isinstance(parameters, torch.Tensor): parameters = [parameters] # Filter parameters based on: # - grad should not be none # - parameter should not be shared # - should not be a replica due to tensor model parallelism total_num_zeros = 0.0 for param in parameters: grad_not_none = param.grad is not None is_not_tp_duplicate = param_is_not_tensor_parallel_duplicate(param) if grad_not_none and is_not_tp_duplicate: grad = param.grad.detach() num_zeros = grad.numel() - torch.count_nonzero(grad) total_num_zeros = num_zeros + total_num_zeros total_num_zeros = torch.IntTensor([int(total_num_zeros)]).cuda() # Sum across all model-parallel GPUs. ops = [] ops.append( dist.all_reduce(total_num_zeros, op=dist.ReduceOp.SUM, group=gpc.get_group(ParallelMode.TENSOR), async_op=True)) if gpc.is_initialized(ParallelMode.PIPELINE): ops.append( dist.all_reduce(total_num_zeros, op=dist.ReduceOp.SUM, group=gpc.get_group(ParallelMode.PIPELINE), async_op=True)) for req in ops: req.wait() total_num_zeros = total_num_zeros.item() return total_num_zeros def copy_tensor_parallel_attributes(src_tensor, dst_tensor): for attr in TENSOR_PARALLEL_ATTRIBUTES: if hasattr(src_tensor, attr): val = getattr(src_tensor, attr) setattr(dst_tensor, attr, val) def param_is_not_tensor_parallel_duplicate(param): return (hasattr(param, IS_TENSOR_PARALLEL) and getattr(param, IS_TENSOR_PARALLEL)) or (gpc.get_local_rank( ParallelMode.TENSOR) == 0) @contextmanager def switch_virtual_pipeline_parallel_rank(rank): prev_rank = gpc.virtual_pipeline_parallel_rank try: gpc.set_virtual_pipeline_parallel_rank(rank) yield finally: gpc.set_virtual_pipeline_parallel_rank(prev_rank) def disposable(func: Callable) -> Callable: executed = False @functools.wraps(func) def wrapper(*args, **kwargs): nonlocal executed if not executed: executed = True return func(*args, **kwargs) return wrapper
#!/usr/bin/env python # -*- encoding: utf-8 -*- import torch def set_to_cuda(models): """Send model to gpu. :param models: nn.module or a list of module """ if isinstance(models, list) and len(models) > 1: ret = [] for model in models: ret.append(model.to(get_current_device())) return ret elif isinstance(models, list): return models[0].to(get_current_device()) else: return models.to(get_current_device()) def get_current_device() -> torch.device: """ Returns currently selected device (gpu/cpu). If cuda available, return gpu, otherwise return cpu. """ if torch.cuda.is_available(): return torch.device(f'cuda:{torch.cuda.current_device()}') else: return torch.device('cpu') def synchronize(): """Similar to cuda.synchronize(). Waits for all kernels in all streams on a CUDA device to complete. """ if torch.cuda.is_available(): torch.cuda.synchronize() def empty_cache(): """Similar to cuda.empty_cache() Releases all unoccupied cached memory currently held by the caching allocator. """ if torch.cuda.is_available(): torch.cuda.empty_cache()
import torch.nn as nn import torch.distributed as dist from colossalai.core import global_context as gpc from colossalai.context.moe_context import MOE_CONTEXT from colossalai.context import ParallelMode from .common import is_using_ddp from typing import Dict, List def get_moe_epsize_param_dict(model: nn.Module) -> Dict[int, List[nn.Parameter]]: """Returns a parameter dictionary, the key of which is the expert parallel size of every parameter. Since the parameters in data parallelism is replicated in each GPU, we set their ep_size to 1. Args: model (:class:`torch.nn.Module`): A pyTorch `nn.Module` from which we get dict. """ epsize_param_dict = dict() for param in model.parameters(): if not hasattr(param, 'moe_info'): ep_size = 1 # set ep_size to 1 for dp parameters else: ep_size = param.moe_info.ep_size if ep_size not in epsize_param_dict: epsize_param_dict[ep_size] = [] epsize_param_dict[ep_size].append(param) return epsize_param_dict def sync_moe_model_param(model: nn.Module): """Make sure model parameters are consistent in MoE parallel context. Args: model (:class:`torch.nn.Module`): A pyTorch model on whose parameters you check the consistency. """ if is_using_ddp(): param_dict = get_moe_epsize_param_dict(model) # synchrosize the parameters whose dp_group is the whole world if 1 in param_dict: src_rank = gpc.get_ranks_in_group(ParallelMode.DATA)[0] for param in param_dict[1]: dist.broadcast(param, src=src_rank, group=gpc.get_group(ParallelMode.DATA)) for ep_size in param_dict: # When ep_size = world_size, communication is not needed if ep_size != 1 and ep_size != MOE_CONTEXT.world_size: src_rank = dist.get_rank(MOE_CONTEXT.parallel_info_dict[ep_size].ep_group) for param in param_dict[ep_size]: dist.broadcast(param, src=src_rank, group=param.moe_info.dp_group)
from collections import OrderedDict from itertools import chain import torch import torch.distributed as dist from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.constants import IS_TENSOR_PARALLEL try: from torch.nn.modules.module import _EXTRA_STATE_KEY_SUFFIX except ImportError: _EXTRA_STATE_KEY_SUFFIX = '_extra_state' from .common import is_using_pp __all__ = ["save_checkpoint", "load_checkpoint"] def broadcast_state_dict(state_dict, parallel_mode): state_dict = [state_dict.copy() if isinstance(state_dict, dict) else state_dict] src_rank = gpc.get_ranks_in_group(parallel_mode)[0] dist.broadcast_object_list(state_dict, src=src_rank, group=gpc.get_cpu_group(parallel_mode)) return state_dict[0] def partition_tensor_parallel_state_dict(state_dict: OrderedDict, parallel_mode: ParallelMode, dims: dict = dict(), partition_states: dict = dict()): src_rank = gpc.get_ranks_in_group(parallel_mode)[0] depth = gpc.get_world_size(parallel_mode) group = gpc.get_cpu_group(parallel_mode) is_rank0 = gpc.get_local_rank(parallel_mode) == 0 partition_info = [None] if is_rank0: partition_info_dict = OrderedDict() for key, param in state_dict.items(): dim = dims[key] is_partitioned = partition_states[key] shape = list(param.shape) if is_partitioned: shape[dim] = shape[dim] // depth partition_info_dict[key] = (is_partitioned, param.dtype, shape, dim) partition_info[0] = partition_info_dict dist.broadcast_object_list(partition_info, src_rank, group=group) partitioned_state = OrderedDict() for key, (is_partitioned, dtype, shape, dim) in partition_info[0].items(): if is_partitioned: output = torch.empty(shape, dtype=dtype) if is_rank0: scatter_list = [t.contiguous() for t in state_dict[key].chunk(depth, dim)] else: scatter_list = None dist.scatter(output, scatter_list, src_rank, group=group) else: if is_rank0: output = state_dict[key] else: output = torch.empty(shape, dtype=dtype) dist.broadcast(output, src_rank, group=group) partitioned_state[key] = output return partitioned_state def gather_tensor_parallel_state_dict( state_dict: OrderedDict, parallel_mode: ParallelMode, dims: dict = dict(), partition_states: dict = dict(), keep_vars: bool = False, ): dst_rank = gpc.get_ranks_in_group(parallel_mode)[0] depth = gpc.get_world_size(parallel_mode) for key in list(state_dict.keys()): param = state_dict.pop(key) param = param if keep_vars else param.detach() dim = dims.get(key, 0) do_partition = partition_states.get(key, True) if do_partition: temp = param.transpose(0, dim).contiguous() gather_list = None if gpc.get_local_rank(parallel_mode) == 0: shape = list(param.shape) shape[0], shape[dim] = shape[dim], shape[0] shape[0] *= depth param = torch.empty(shape, dtype=param.dtype, device=param.device) gather_list = list(torch.chunk(param, depth, dim=0)) dist.gather(temp, gather_list, dst=dst_rank, group=gpc.get_cpu_group(parallel_mode)) param = torch.transpose(param, 0, dim) # update params in state_dict only on local rank 0 if gpc.get_local_rank(parallel_mode) == 0: state_dict[key] = param return state_dict def _send_state_dict(state_dict, dst, parallel_mode): state_tensor, state_size = dist.distributed_c10d._object_to_tensor(state_dict) dist.send(state_size, dst, group=gpc.get_cpu_group(parallel_mode)) dist.send(state_tensor, dst, group=gpc.get_cpu_group(parallel_mode)) def _recv_state_dict(src, parallel_mode): state_size = torch.tensor([0], dtype=torch.long) dist.recv(state_size, src, group=gpc.get_cpu_group(parallel_mode)) state_tensor = torch.empty(state_size.item(), dtype=torch.uint8) dist.recv(state_tensor, src, group=gpc.get_cpu_group(parallel_mode)) state_dict = dist.distributed_c10d._tensor_to_object(state_tensor, state_size) return state_dict def partition_pipeline_parallel_state_dict(model, state_dict): pipeline_state = OrderedDict() if gpc.get_local_rank(ParallelMode.TENSOR) == 0: # receive all states from prev stage if not gpc.is_first_rank(ParallelMode.PIPELINE): state_dict = _recv_state_dict(gpc.get_prev_global_rank(ParallelMode.PIPELINE), ParallelMode.PIPELINE) # move states to output for name, _ in model.named_parameters(recurse=True): if name in state_dict: pipeline_state[name] = state_dict.pop(name) for name, _ in model.named_buffers(recurse=True): if name in state_dict: pipeline_state[name] = state_dict.pop(name) for name, _ in model.named_modules(): extra_state_key = name + "." + _EXTRA_STATE_KEY_SUFFIX if extra_state_key in state_dict: pipeline_state[extra_state_key] = state_dict.pop(extra_state_key) # send rest states to next stage if not gpc.is_last_rank(ParallelMode.PIPELINE): _send_state_dict(state_dict, gpc.get_next_global_rank(ParallelMode.PIPELINE), ParallelMode.PIPELINE) return pipeline_state def gather_pipeline_parallel_state_dict(state_dict): gathered_states = ([None for _ in range(gpc.get_world_size(ParallelMode.PIPELINE))] if gpc.get_local_rank(ParallelMode.PIPELINE) == 0 else None) dist.gather_object( state_dict, gathered_states, dst=gpc.get_ranks_in_group(ParallelMode.PIPELINE)[0], group=gpc.get_cpu_group(ParallelMode.PIPELINE), ) state_dict = (OrderedDict(chain.from_iterable(state.items() for state in gathered_states)) if gpc.get_local_rank(ParallelMode.PIPELINE) == 0 else OrderedDict()) return state_dict def save_checkpoint(file, epoch: int, model: torch.nn.Module, optimizer: torch.optim.Optimizer = None, lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None, **kwargs): """Stores the checkpoint to disk. Saves all the training components' parameters or buffers, such as model, optimizer, lr_scheduler etc. into a checkpoint dictionary. Args: file: a file-like object (has to implement write and flush) or a string or os.PathLike object containing a file name. epoch (int): Epoch number (indicates how many epochs have you trained this model). model (:class:`torch.nn.Module`): Model to be saved. optimizer (Union[:class:`torch.optim.Optimizer`, :class:`colossalai.nn.optimizer`]): Optimizer to be saved. lr_scheduler (Union[:class:`torch.optim.lr_scheduler`, :class:`colossalai.nn.lr_scheduler`], optional): lr_scheduler to be saved, defaults to None. pickle_module: module used for pickling metadata and objects pickle_protocol: can be specified to override the default protocol """ # ckpt container checkpoint = {"epoch": epoch} model_state = model.state_dict() if is_using_pp() and gpc.get_local_rank(ParallelMode.TENSOR) == 0: model_state = gather_pipeline_parallel_state_dict(model_state) if gpc.get_global_rank() == 0: checkpoint["model"] = model_state # if optimizer is not None: # checkpoint['optimizer'] = optimizer.state_dict() # if lr_scheduler is not None: # checkpoint['lr_scheduler'] = lr_scheduler.state_dict() torch.save(checkpoint, file, **kwargs) def broadcast_model(model: torch.nn.Module): src_rank = gpc.get_ranks_in_group(ParallelMode.TENSOR)[0] for p in model.parameters(): if not getattr(p, IS_TENSOR_PARALLEL, False) and p.storage().size() > 0: group = gpc.get_group(ParallelMode.TENSOR) if p.device.type == 'cuda' else gpc.get_cpu_group( ParallelMode.TENSOR) dist.broadcast(p, src_rank, group=group) def load_checkpoint( file, model: torch.nn.Module, optimizer: torch.optim.Optimizer = None, lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None, strict: bool = True, ): """Loads training states from a checkpoint file. Args: file: a file-like object (has to implement read(), readline(), tell(), and seek()), or a string or os.PathLike object containing a file name. model (:class:`torch.nn.Module`): Model to load saved weights and buffers. optimizer (Union[:class:`torch.optim.Optimizer`, :class:`colossalai.nn.optimizer`]): Optimizer to recuperate. lr_scheduler (:class:`torch.optim.lr_scheduler._LRScheduler`, optional): lr_scheduler to recuperate, defaults to None. strict (bool, optional): Whether to strictly enforce that the keys in :attr:`state_dict` of the checkpoint match the names of parameters and buffers in model, defaults to True. Returns: int: The saved epoch number. Raises: RuntimeError: Raise error if the model/optimizer cannot successfully be recuperated """ state_dict = (torch.load(file, map_location=torch.device("cpu")) if gpc.get_local_rank(ParallelMode.MODEL) == 0 else None) # model states model_state = state_dict.pop("model") if state_dict is not None else dict() # pipeline if is_using_pp(): model_state = partition_pipeline_parallel_state_dict(model, model_state) try: model.load_state_dict(model_state, strict=strict) broadcast_model(model) except RuntimeError as e: error_msgs = str(e) if error_msgs.startswith("Error(s) in loading state_dict for "): error_msgs = error_msgs.split("\n\t")[1:] dst_rank = gpc.get_ranks_in_group(ParallelMode.MODEL)[0] all_error_msgs = [None for _ in range(gpc.get_world_size(ParallelMode.MODEL))] dist.gather_object(error_msgs, all_error_msgs, dst=dst_rank, group=gpc.get_cpu_group(ParallelMode.MODEL)) if gpc.get_global_rank() == 0: all_error_msgs = list(chain.from_iterable(all_error_msgs)) raise RuntimeError("Error(s) in loading state_dict for {}:\n\t{}".format( model.__class__.__name__, "\n\t".join(all_error_msgs))) else: raise e # broadcast the rest states state_dict = broadcast_state_dict(state_dict, ParallelMode.MODEL) # # optimizer states # if optimizer is not None and 'optimizer' in state_dict: # optimizer.load_state_dict(state_dict['optimizer']) # # lr scheduler states # if lr_scheduler is not None and 'lr_scheduler' in state_dict: # lr_scheduler.load_state_dict(state_dict['lr_scheduler']) # last epoch last_epoch = state_dict.pop("epoch", -1) return last_epoch
import torch import torch.distributed as dist from colossalai.tensor import ColoTensor from colossalai.nn.optimizer import ColossalaiOptimizer from colossalai.utils.checkpoint.utils import gather_tensor, scatter_tensor from typing import Optional, Dict def save_checkpoint(path: str, epoch: int, model: torch.nn.Module, optimizer: Optional[ColossalaiOptimizer] = None, lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None, *args, **kwargs): """save_checkpoint save a model, whose parameters are `ColoTensor`s. Args: path (str): directory to save the checkpoint files. epoch (int): the number of epoch model (torch.nn.Module): a torch module initialized by ColoInitContext optimizer (ColossalaiOptimizer, optional): optimizers. Defaults to None. lr_scheduler (torch.optim.lr_scheduler._LRScheduler, optional): lr schedule. Defaults to None. """ rank = dist.get_rank() model_state = model.state_dict() # save the dist context about the tensors in a new dict, while still maintain the original dict. for k, v in model_state.items(): if isinstance(v, ColoTensor): gather_tensor(v) # gather shared tensors to rank0 # don't recover tensors in rank0, since the dict is only a copy of model if rank == 0: # sanity check for k, v in model_state.items(): if isinstance(v, ColoTensor): assert v.save_ready assert v.is_replicate() delattr(v, 'save_ready') # model saving save_state = {'epoch': epoch, 'model': model_state} torch.save(save_state, path + '/epoch_{}_model.pth'.format(epoch), *args, **kwargs) # delete old dicts del model_state # synchronize all the processes dist.barrier() if optimizer is not None: mapping = dict() optim_state = optimizer.state_dict() for k, v in optim_state['state'].items(): for n, t in v.items(): if isinstance(t, ColoTensor): mapping[(k, n)] = t.dist_spec gather_tensor(t) if rank == 0: save_state = {'epoch': epoch, 'optim': optim_state} torch.save(save_state, path + '/epoch_{}_optim.pth'.format(epoch), *args, **kwargs) # recover colo tensors in rank0 for k, v in optimizer.state_dict()['state'].items(): for n, t in v.items(): if isinstance(t, ColoTensor): assert hasattr(t, 'save_ready') t.set_dist_spec(mapping[(k, n)]) delattr(t, 'save_ready') del optim_state del mapping dist.barrier() def load_checkpoint(path: str, epoch: int, model: torch.nn.Module, optimizer: Optional[ColossalaiOptimizer] = None, lr_scheduler: torch.optim.lr_scheduler._LRScheduler = None, torch_load_kwargs: Optional[Dict] = None, load_state_dict_kwargs: Optional[Dict] = None): """load_checkpoint load a model, whose parameters are `ColoTensor`s. Args: path (str): directory to save the checkpoint files. epoch (int): the number of epoch model (torch.nn.Module): a torch module initialized by ColoInitContext optimizer (ColossalaiOptimizer, optional): optimizers. Defaults to None. lr_scheduler (torch.optim.lr_scheduler._LRScheduler, optional): lr schedule. Defaults to None. torch_load_kwargs: (dict, optional): The kwargs of torch.load inside the function load_state_dict_kwargs (dict, optional): The kwargs of load_state_dict inside the function """ # initialize the default paramters if not torch_load_kwargs: torch_load_kwargs = dict() if not load_state_dict_kwargs: load_state_dict_kwargs = dict() rank = dist.get_rank() mapping = dict() for n, p in model.named_parameters(): if isinstance(p, ColoTensor): mapping[n] = p.dist_spec gather_tensor(p) if rank == 0: load_state = torch.load(path + '/epoch_{}_model.pth'.format(epoch), **torch_load_kwargs) model.load_state_dict(load_state['model'], **load_state_dict_kwargs) dist.barrier() # scatter loaded parameters for n, p in model.named_parameters(): if isinstance(p, ColoTensor): scatter_tensor(p, mapping[n]) if rank == 0: assert hasattr(p, 'save_ready') delattr(p, 'save_ready') del mapping if optimizer is not None: mapping = dict() for k, v in optimizer.state_dict()['state'].items(): for n, t in v.items(): if isinstance(t, ColoTensor): mapping[(k, n)] = t.dist_spec gather_tensor(t) if rank == 0: colo_checkpoint = torch.load(path + '/epoch_{}_optim.pth'.format(epoch), **torch_load_kwargs) optimizer.load_state_dict(colo_checkpoint['optim'], **load_state_dict_kwargs) dist.barrier() for k, v in optimizer.state_dict()['state'].items(): for n, t in v.items(): if isinstance(t, ColoTensor): scatter_tensor(t, mapping[(k, n)]) del mapping
from .module_checkpoint import save_checkpoint, load_checkpoint __all__ = ['save_checkpoint', 'load_checkpoint']
import torch import torch.distributed as dist from colossalai.tensor import ColoTensor, ColoTensorSpec from colossalai.tensor.distspec import _DistSpec, DistPlacementPattern def robust_broadcast(tensor): with torch.no_grad(): is_cpu_ten = tensor.device.type == 'cpu' if is_cpu_ten: b_data = tensor.cuda() else: b_data = tensor dist.broadcast(b_data, 0) if is_cpu_ten: tensor.copy_(b_data) def gather_tensor(colo_tensor: ColoTensor) -> None: """Make colo_tensor replicated when the rank is 0 """ if not colo_tensor.is_replicate(): pg = colo_tensor.get_process_group() # for the group which contains rank 0 if pg.dp_local_rank() == 0: old_dist_spec = colo_tensor.dist_spec colo_tensor.to_replicate_() if dist.get_rank() != 0: colo_tensor.set_dist_spec(old_dist_spec) # synchronize all processes for unexpected problems dist.barrier() if dist.get_rank() == 0: setattr(colo_tensor, 'save_ready', True) # set saving signitrue def scatter_tensor(colo_tensor: ColoTensor, dist_spec: _DistSpec) -> None: """Reversal operation of `gather_tensor`. """ if dist_spec.placement == DistPlacementPattern.REPLICATE: robust_broadcast(colo_tensor.data) else: global_size = colo_tensor.size_global() if dist.get_rank() == 0: entire_data = colo_tensor.data else: entire_data = torch.empty(global_size, device=colo_tensor.device) robust_broadcast(entire_data) if dist.get_rank() == 0: colo_tensor.set_dist_spec(dist_spec) else: rep_tensor = ColoTensor( entire_data, ColoTensorSpec(pg=colo_tensor.get_process_group(), compute_attr=colo_tensor.compute_spec)) rep_tensor.set_dist_spec(dist_spec) with torch.no_grad(): colo_tensor.data.copy_(rep_tensor.data) # synchronize all processes for unexpected problems dist.barrier()
from colossalai.utils.rank_recorder.rank_recorder import recorder __all__ = ["recorder"]
import time from typing import List, Dict import json import os import time import shutil import atexit import torch import torch.distributed as dist import json import matplotlib.pyplot as plt import matplotlib.colors as mcolors cmap = list(mcolors.TABLEAU_COLORS.values()) LOG_FOLDER = "record.log" MAX_WAIT_TIME = 20 class Event: def __init__(self, start: int, end: int, name: str, rank: int) -> None: self.start = start self.end = end self.name = name self.rank = rank class Recorder: def __init__(self) -> None: self.rank_to_history: Dict[int, List[Event]] = {} self.base_time = time.time() self.temp_event = None self.export_format = 'png' self.export_name = 'test' self.dpi = 500 self.theme = 'dark_background' self.figure_width = 30 self.figure_height = 10 self.legend_fontsize = 16 self.device_fontsize = 20 self.bar_height = 0.2 if not os.path.exists(LOG_FOLDER): os.makedirs(LOG_FOLDER) def start(self, name: str, rank: int): # TODO : add lock to prevent conflict torch.cuda.synchronize() start_time = time.time() self.temp_event = Event(start_time, None, name, rank) def end(self): assert self.temp_event is not None, "`start` before `end`" torch.cuda.synchronize() end_time = time.time() self.temp_event.end = end_time rank = self.temp_event.rank if rank not in self.rank_to_history: self.rank_to_history[rank] = [] self.rank_to_history[rank].append(self.temp_event) self.temp_event = None def get_history(self): return self.history def __call__(self, name: str, rank: str): self.temp_name = name self.temp_rank = rank return self def __enter__(self): name = self.temp_name rank = self.temp_rank self.start(name, rank) def __exit__(self, *args): self.end() def dump_record(self): rank = dist.get_rank() rank_to_history = self.rank_to_history records = {'base_time': self.base_time, 'content': {}} for record_rank in rank_to_history: history = rank_to_history[record_rank] recs = [] for event in history: rec = {'start': event.start, 'end': event.end, 'name': event.name} recs.append(rec) records['content'][record_rank] = recs dump_name = f'{rank}.json' dump_path = os.path.join(LOG_FOLDER, dump_name) with open(dump_path, 'w', encoding='utf-8') as f: json.dump(records, f, ensure_ascii=False) def merge_recode(self): base_time = self.base_time world_size = dist.get_world_size() wait_time = 0 while True: time.sleep(0.1) log_num = len(os.listdir(LOG_FOLDER)) if log_num == world_size: break wait_time += 1 if wait_time >= MAX_WAIT_TIME: break # merge logs_path = [os.path.join(LOG_FOLDER, file) for file in os.listdir(LOG_FOLDER)] recoders = {} for path in logs_path: with open(path, 'r', encoding='utf-8') as f: recs = json.load(f) for record_rank in recs['content']: history = recs['content'][record_rank] recoders[record_rank] = [] for rec in history: recoders[record_rank].append({ 'start': rec['start'] - base_time, 'end': rec['end'] - base_time, 'name': rec['name'] }) shutil.rmtree(LOG_FOLDER) with open(self.export_name + '.json', 'w', encoding='utf-8') as f: json.dump(recoders, f, ensure_ascii=False) def visualise_record(self): with open(self.export_name + '.json', 'r', encoding='utf-8') as f: records = json.load(f) records = dict(records) ranks = list(sorted(records.keys())) name_list = {} plots = {} plt.figure(dpi=self.dpi, figsize=[self.figure_width, self.figure_height]) plt.style.use(self.theme) for rank in ranks: rank_records = records[rank] for rec in rank_records: s = rec['start'] e = rec['end'] name = rec['name'] if name not in name_list: name_list[name] = len(name_list) bar = plt.barh(rank, width=e - s, height=self.bar_height, left=s, color=cmap[name_list[name]]) if name not in plots: plots[name] = bar plt.legend(list(plots.values()), list(plots.keys()), loc="upper left", fontsize=self.legend_fontsize) plt.yticks(ticks=ranks, labels=[f'Device:{rank}' for rank in ranks], fontsize=self.device_fontsize) plt.grid(axis='x') plt.savefig("{}.{}".format(self.export_name, self.export_format)) def exit_worker(self): if len(self.rank_to_history) == 0: return self.dump_record() # if this is rank 0, wait for merge rank = dist.get_rank() if rank == 1: # take the base time of rank 0 as standard self.merge_recode() self.visualise_record() recorder = Recorder() atexit.register(recorder.exit_worker)
from .multi_tensor_apply import MultiTensorApply multi_tensor_applier = MultiTensorApply(2048 * 32)
# modified from https://github.com/NVIDIA/apex/blob/master/apex/multi_tensor_apply/multi_tensor_apply.py class MultiTensorApply(object): """ Apply an operation to a list of tensors efficiently. Args: chunk_size (int): Size of a chunk. """ available = False warned = False def __init__(self, chunk_size): try: MultiTensorApply.available = True self.chunk_size = chunk_size except ImportError as err: MultiTensorApply.available = False MultiTensorApply.import_err = err def check_avail(self): if not MultiTensorApply.available: raise RuntimeError( "Attempted to call MultiTensorApply method, but MultiTensorApply " "is not available, possibly because Apex was installed without " "--cpp_ext --cuda_ext. Original import error message:", MultiTensorApply.import_err) def __call__(self, op, noop_flag_buffer, tensor_lists, *args): self.check_avail() return op(self.chunk_size, noop_flag_buffer, tensor_lists, *args)
#!/usr/bin/env python # -*- encoding: utf-8 -*- from abc import ABC, abstractmethod class BaseSampler(ABC): def __init__(self, dataset, batch_size): self.dataset = dataset self.batch_size = batch_size @abstractmethod def __len__(self): pass @abstractmethod def __iter__(self): pass
from .base_sampler import BaseSampler from .data_parallel_sampler import DataParallelSampler, get_dataloader __all__ = ['BaseSampler', 'DataParallelSampler', 'get_dataloader']
#!/usr/bin/env python # -*- encoding: utf-8 -*- # adpated from torch.utils.data.DistributedSampler import math import random import numpy as np from typing import TypeVar, Iterator import torch from torch.utils.data import Sampler, Dataset, DataLoader from colossalai.context.parallel_mode import ParallelMode from colossalai.core import global_context as gpc from colossalai.registry import DATA_SAMPLERS T_co = TypeVar('T_co', covariant=True) @DATA_SAMPLERS.register_module class DataParallelSampler(Sampler): """A data sampler for distributed data parallelism. Args: dataset (:class:`torch.utils.data.Dataset`): The Dataset for sampling. shuffle (bool, optional): Whether to shuffle data, defaults to False. seed (int, optional): The random seed used for sampling, defaults to 0. drop_last (bool, optional): Set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller, defaults to False. """ def __init__(self, dataset: Dataset, shuffle: bool = False, seed: int = 0, drop_last: bool = False) -> None: self.dataset = dataset self.num_replicas = gpc.get_world_size(ParallelMode.DATA) self.rank = gpc.get_local_rank(ParallelMode.DATA) self.epoch = 0 self.drop_last = drop_last # If the dataset length is evenly divisible by # of replicas, then there # is no need to drop any data, since the dataset will be split equally. # type: ignore[arg-type] if self.drop_last and len(self.dataset) % self.num_replicas != 0: # Split to nearest available length that is evenly divisible. # This is to ensure each rank receives the same amount of data when # using this Sampler. self.num_samples = math.ceil( # `type:ignore` is required because Dataset cannot provide a default __len__ # see NOTE in pytorch/torch/utils/data/sampler.py (len(self.dataset) - self.num_replicas) / \ self.num_replicas # type: ignore[arg-type] ) else: self.num_samples = math.ceil( len(self.dataset) / self.num_replicas) # type: ignore[arg-type] self.total_size = self.num_samples * self.num_replicas self.shuffle = shuffle self.seed = seed def __iter__(self) -> Iterator[T_co]: if self.shuffle: # deterministically shuffle based on epoch and seed g = torch.Generator() g.manual_seed(self.seed + self.epoch) # type: ignore[arg-type] indices = torch.randperm(len(self.dataset), generator=g).tolist() # update for next epoch so that there is no need to call # set_epoch manually self.epoch += 1 else: indices = list(range(len(self.dataset))) # type: ignore[arg-type] if not self.drop_last: # add extra samples to make it evenly divisible padding_size = self.total_size - len(indices) if padding_size <= len(indices): indices += indices[:padding_size] else: indices += (indices * math.ceil(padding_size / len(indices)))[:padding_size] else: # remove tail of data to make it evenly divisible. indices = indices[:self.total_size] assert len(indices) == self.total_size # subsample indices = indices[self.rank:self.total_size:self.num_replicas] assert len(indices) == self.num_samples return iter(indices) def __len__(self) -> int: return self.num_samples def set_epoch(self, epoch: int) -> None: r"""Sets the epoch for this sampler. When :attr:`shuffle=True`, this ensures all replicas use a different random ordering for each epoch. Otherwise, the next iteration of this sampler will yield the same ordering. Args: epoch (int): Epoch number. """ self.epoch = epoch def get_dataloader(dataset, shuffle=False, seed=1024, add_sampler=True, drop_last=False, pin_memory=False, num_workers=0, **kwargs): r"""Set up a deterministic dataloader (also configure seed workers, samplers and whether shuffle or not) Note: When pipeline parallel is enabled, shuffle cannot be True as it will result in mismatch between input data on the 1st stage and label on the last stage. Args: dataset (:class:`torch.utils.data.Dataset`): The dataset to be loaded. shuffle (bool, optional): Whether to shuffle the dataset. Defaults to False. seed (int, optional): Random worker seed for sampling, defaults to 1024. add_sampler: Whether to add ``DistributedDataParallelSampler`` to the dataset. Defaults to True. drop_last (bool, optional): Set to True to drop the last incomplete batch, if the dataset size is not divisible by the batch size. If False and the size of dataset is not divisible by the batch size, then the last batch will be smaller, defaults to False. pin_memory (bool, optional): Whether to pin memory address in CPU memory. Defaults to False. num_workers (int, optional): Number of worker threads for this dataloader. Defaults to 0. kwargs (dict): optional parameters for ``torch.utils.data.DataLoader``, more details could be found in `DataLoader <https://pytorch.org/docs/stable/_modules/torch/utils/data/dataloader.html#DataLoader>`_. Returns: :class:`torch.utils.data.DataLoader`: A DataLoader used for training or testing. """ _kwargs = kwargs.copy() if add_sampler and gpc.is_initialized(ParallelMode.DATA) and gpc.get_world_size(ParallelMode.DATA) > 1: sampler = DataParallelSampler(dataset, shuffle=shuffle) else: sampler = None # Deterministic dataloader def seed_worker(worker_id): worker_seed = seed np.random.seed(worker_seed) torch.manual_seed(worker_seed) random.seed(worker_seed) if sampler is None: return DataLoader(dataset, worker_init_fn=seed_worker, shuffle=shuffle, drop_last=drop_last, pin_memory=pin_memory, num_workers=num_workers, **_kwargs) else: return DataLoader(dataset, sampler=sampler, worker_init_fn=seed_worker, drop_last=drop_last, pin_memory=pin_memory, num_workers=num_workers, **_kwargs)
import gc import inspect import torch import torch.nn as nn from typing import Optional from collections import defaultdict LINE_WIDTH = 108 LINE = '-' * LINE_WIDTH + '\n' class TensorDetector(): def __init__(self, show_info: bool = True, log: str = None, include_cpu: bool = False, module: Optional[nn.Module] = None): """This class is a detector to detect tensor on different devices. Args: show_info (bool, optional): whether to print the info on screen, default True. log (str, optional): the file name to save the log. Defaults to None. include_cpu (bool, optional): whether to detect tensor on cpu, default False. module (Optional[:class:`nn.Module`]): when sending an ``nn.Module`` object, the detector can name the tensors detected better. """ self.show_info = show_info self.log = log self.include_cpu = include_cpu self.tensor_info = defaultdict(list) self.saved_tensor_info = defaultdict(list) self.order = [] self.detected = [] self.devices = [] self.info = "" self.module = module if isinstance(module, nn.Module): # if module is an instance of nn.Module, we can name the parameter with its real name for name, param in module.named_parameters(): self.tensor_info[id(param)].append(name) self.tensor_info[id(param)].append(param.device) self.tensor_info[id(param)].append(param.shape) self.tensor_info[id(param)].append(param.requires_grad) self.tensor_info[id(param)].append(param.dtype) self.tensor_info[id(param)].append(self.get_tensor_mem(param)) def get_tensor_mem(self, tensor): # calculate the memory occupied by a tensor memory_size = tensor.element_size() * tensor.storage().size() if (tensor.is_leaf or tensor.retains_grad) and tensor.grad is not None: grad_memory_size = tensor.grad.element_size() * tensor.grad.storage().size() memory_size += grad_memory_size return self.mem_format(memory_size) def mem_format(self, real_memory_size): # format the tensor memory into a reasonal magnitude if real_memory_size >= 2**30: return str(real_memory_size / (2**30)) + ' GB' if real_memory_size >= 2**20: return str(real_memory_size / (2**20)) + ' MB' if real_memory_size >= 2**10: return str(real_memory_size / (2**10)) + ' KB' return str(real_memory_size) + ' B' def collect_tensors_state(self): for obj in gc.get_objects(): if torch.is_tensor(obj): # skip cpu tensor when include_cpu is false and the tensor we have collected before if (not self.include_cpu) and obj.device == torch.device('cpu'): continue self.detected.append(id(obj)) # skip paramters we had added in __init__ when module is an instance of nn.Module for the first epoch if id(obj) not in self.tensor_info: name = type(obj).__name__ # after backward, we want to update the records, to show you the change if isinstance(self.module, nn.Module) and name == 'Parameter': if obj.grad is not None: # with grad attached for par_name, param in self.module.named_parameters(): if param.requires_grad and param.grad.equal(obj.grad): name = par_name + ' (with grad)' else: # with no grad attached # there will be no new paramters created during running # so it must be in saved_tensor_info continue # we can also marked common tensors as tensor(with grad) if name == 'Tensor' and (obj.is_leaf or obj.retains_grad): if obj.grad is not None: name = name + ' (with grad)' # in fact, common tensor have no grad # unless you set retain_grad() if id(obj) in self.saved_tensor_info.keys() and name == self.saved_tensor_info[id(obj)][0]: continue self.tensor_info[id(obj)].append(name) self.tensor_info[id(obj)].append(obj.device) self.tensor_info[id(obj)].append(obj.shape) self.tensor_info[id(obj)].append(obj.requires_grad) self.tensor_info[id(obj)].append(obj.dtype) self.tensor_info[id(obj)].append(self.get_tensor_mem(obj)) # recorded the order we got the tensor # by this we can guess the tensor easily # it will record every tensor updated this turn self.order.append(id(obj)) # recorded all different devices if obj.device not in self.devices: self.devices.append(obj.device) def print_tensors_state(self): template_format = '{:3s}{:<30s}{:>10s}{:>20s}{:>10s}{:>20s}{:>15s}' self.info += LINE self.info += template_format.format(' ', 'Tensor', 'device', 'shape', 'grad', 'dtype', 'Mem') self.info += '\n' self.info += LINE # if a tensor updates this turn, and was recorded before # it should be updated in the saved_tensor_info as well outdated = [x for x in self.saved_tensor_info.keys() if x in self.order] minus = [x for x in self.saved_tensor_info.keys() if x not in self.detected] minus = outdated + minus if len(self.order) > 0: for tensor_id in self.order: self.info += template_format.format('+', str(self.tensor_info[tensor_id][0]), str(self.tensor_info[tensor_id][1]), str(tuple(self.tensor_info[tensor_id][2])), str(self.tensor_info[tensor_id][3]), str(self.tensor_info[tensor_id][4]), str(self.tensor_info[tensor_id][5])) self.info += '\n' if len(self.order) > 0 and len(minus) > 0: self.info += '\n' if len(minus) > 0: for tensor_id in minus: self.info += template_format.format('-', str(self.saved_tensor_info[tensor_id][0]), str(self.saved_tensor_info[tensor_id][1]), str(tuple(self.saved_tensor_info[tensor_id][2])), str(self.saved_tensor_info[tensor_id][3]), str(self.saved_tensor_info[tensor_id][4]), str(self.saved_tensor_info[tensor_id][5])) self.info += '\n' # deleted the updated tensor self.saved_tensor_info.pop(tensor_id) # trace where is the detect() locate_info = inspect.stack()[2] locate_msg = '"' + locate_info.filename + '" line ' + str(locate_info.lineno) self.info += LINE self.info += f"Detect Location: {locate_msg}\n" for device in self.devices: if device == torch.device('cpu'): continue gpu_mem_alloc = self.mem_format(torch.cuda.memory_allocated(device)) self.info += f"Totle GPU Memery Allocated on {device} is {gpu_mem_alloc}\n" self.info += LINE self.info += '\n\n' if self.show_info: print(self.info) if self.log is not None: with open(self.log + '.log', 'a') as f: f.write(self.info) def detect(self, include_cpu=False): self.include_cpu = include_cpu self.collect_tensors_state() self.print_tensors_state() self.saved_tensor_info.update(self.tensor_info) self.tensor_info.clear() self.order = [] self.detected = [] self.info = "" def close(self): self.saved_tensor_info.clear() self.module = None
from .tensor_detector import TensorDetector
#!/usr/bin/env python # coding: utf-8 import inspect import types from typing import Callable, List import torch import torch.nn as nn from colossalai.tensor import ColoParameter, ColoTensor from colossalai.utils.model.utils import substitute_init_recursively class LazyInitContext(): """ A context to allow for lazy weight initialization of PyTorch modules. It intercepts the tensor initialization functions for lazy initialization Note: This API is only experimental and subject to future changes. Usage: with LazyInitContext() as ctx: model = nn.Linear(10, 10) model.weight.zero_() # make sure the weight is a meta tensor assert model.weight.is_meta # initialize weights ctx.lazy_init_parameters(model) # make sure the weight is not a meta tensor # and initialized correctly assert not model.weight.is_meta and torch.all(model.weight == 0) Args: to_meta (bool): optional, whether to initialize the model with meta tensors, default is True. This argument exists for now because some corner cases such as self.weight = torch.zeros(...) cannot be captured yet. extra_torch_tensor_func (List[str]): extra torch tensor functions related to value setting, such as `zero_` and `triu_`. `zero_` is pre-added by default. """ tensor_set_value_func = ['zero_', 'fill_'] def __init__(self, to_meta: bool = True, extra_torch_tensor_func: List[str] = None): # TODO: hijack the torch constructor functions as well self._to_meta = to_meta self._intercepted_nn_init_func_cache = {} self._nn_init_methods = self._get_nn_init_methods() self._torch_mod_cls = torch.nn.modules.module.Module if extra_torch_tensor_func: # use tuple to remove duplicates self._torch_tensor_funcs = tuple(self.tensor_set_value_func + extra_torch_tensor_func) else: self._torch_tensor_funcs = self.tensor_set_value_func @property def to_meta(self): return self._to_meta def _cache_init_func(self, func): """ This method wraps the ``torch.nn.init`` method and torch tensor value-setting functions so that the function call is cached instead of being executed. """ def wrapped_init_func(tensor, *args, **kwargs): if tensor not in self._intercepted_nn_init_func_cache: self._intercepted_nn_init_func_cache[tensor] = [] self._intercepted_nn_init_func_cache[tensor].append((func, args, kwargs)) return wrapped_init_func def _get_nn_init_methods(self): """ This method looks for all available functions in the ``torch.nn.init`` module. """ nn_init_method_names = dir(torch.nn.init) nn_init_methods = [] # look for all methods in ``torch.nn.init`` module for name in nn_init_method_names: nn_init_methods.append((name, getattr(torch.nn.init, name))) def _is_init_method(item): name, func = item if (not isinstance(func, types.FunctionType) or name.startswith('_') or not name.endswith('_')): return False else: return True # remove methods which are not init functions nn_init_methods = list(filter(_is_init_method, nn_init_methods)) return nn_init_methods def _wrap_module_init(self, func): """ This method wraps the calls to the `__init__` of ``torch.nn.Module`` and replaces the argument device with value 'meta' so that all modules are created as meta tensors. """ has_device = 'device' in inspect.signature(func).parameters def layer_lazy_init(module, *args, **kwargs): # if this module contains device argument # we set it to meta to initialize as meta backend if has_device: kwargs['device'] = 'meta' func(module, *args, **kwargs) # if device is not found, we intialize it and convert to meta if not has_device: module.to('meta') return layer_lazy_init def _get_tmp_origin_func_ref(self, name): """ Generate a function name for consistency during caching and retrieving. """ return f'_orig_{name}' def _patch_nn_init_funcs(self): # patch nn.init functions for name, func in self._nn_init_methods: setattr(torch.nn.init, name, self._cache_init_func(func)) def _unpatch_nn_init_funcs(self): # unpatch nn.init functions for name, func in self._nn_init_methods: setattr(torch.nn.init, name, func) def _patch_submodule_init(self): # patch classes __init__ methods def _activate_wrap_init(cls): cls.__orig_init__ = cls.__init__ cls.__init__ = self._wrap_module_init(cls.__init__) substitute_init_recursively(self._torch_mod_cls, _activate_wrap_init, set()) def _unpatch_submodule_init(self): def _recover_orig_init(cls): cls.__init__ = cls.__orig_init__ substitute_init_recursively(self._torch_mod_cls, _recover_orig_init, set()) def _patch_torch_tensor_funcs(self): # patch tensor value-setting functions for func_name in self._torch_tensor_funcs: origin_func_name = self._get_tmp_origin_func_ref(func_name) origin_func = getattr(torch.Tensor, func_name) setattr(torch.Tensor, origin_func_name, origin_func) setattr(torch.Tensor, func_name, self._cache_init_func(origin_func)) def _unpatch_torch_tensor_funcs(self): for func_name in self._torch_tensor_funcs: origin_func_name = self._get_tmp_origin_func_ref(func_name) origin_func = getattr(torch.Tensor, origin_func_name) setattr(torch.Tensor, func_name, origin_func) def __enter__(self): self._patch_torch_tensor_funcs() self._patch_nn_init_funcs() if self._to_meta: self._patch_submodule_init() return self def __exit__(self, *args, **kwargs): if self._to_meta: self._unpatch_submodule_init() self._unpatch_nn_init_funcs() self._unpatch_torch_tensor_funcs() def lazy_init_parameters(self, model: torch.nn.Module, device='cpu'): """ Initialize the weights of the meta-tensor model. Args: model (`torch.nn.Module`): the model instantiated under the context. device (str): the device on which weights are initialized """ def _init_recursively(module: nn.Module): # recursively initialize the module for mod in module.children(): _init_recursively(mod) # initialize and shard tensors directly attached to the current module for name, param in module.named_parameters(recurse=False): _init_and_shard(module, name, param) for name, buf in module.named_buffers(recurse=False): _init_and_shard(module, name, buf) @torch.no_grad() def _init_and_shard(module, name, tensor): # check whether the tensor is a buffer or parameter is_param = isinstance(tensor, nn.parameter.Parameter) # get sharding spec dist_spec = getattr(tensor, 'dist_spec', None) pg = getattr(tensor, 'pg', None) comp_spec = getattr(tensor, 'comp_spec', None) # convert the tensor from meta to materialized one if tensor.is_meta: materialized_tensor = torch.empty_like(tensor, device=device) # if this tensor is a meta tensor, it must have an init function assert tensor in self._intercepted_nn_init_func_cache else: materialized_tensor = tensor # apply init function if tensor in self._intercepted_nn_init_func_cache: init_func, args, kwargs = self._intercepted_nn_init_func_cache[tensor][-1] init_func(materialized_tensor, *args, **kwargs) # convert it to ColoTensor or ColoParameter if is_param: tensor = ColoParameter.from_torch_tensor(materialized_tensor, requires_grad=tensor.requires_grad) else: tensor = ColoTensor.from_torch_tensor(materialized_tensor) # override the original tensor with torch.no_grad(): setattr(module, name, tensor) # apply sharding if dist_spec: tensor.process_group = pg tensor.set_tensor_spec(dist_spec, comp_spec) _init_recursively(model) return model
import contextlib import copy import gc import pprint from typing import Callable, List, Optional, Union import torch import torch.nn as nn from torch.utils._pytree import tree_map from colossalai.device.device_mesh import DeviceMesh from colossalai.fx.profiler import MetaTensor from colossalai.tensor.shape_consistency import ShapeConsistencyManager from colossalai.tensor.sharding_spec import ShardingSpec # reference: https://pytorch.org/cppdocs/notes/tensor_creation.html _TorchFactoryMethod = [ "arange", "empty", "eye", "full", "linspace", "logspace", "ones", "rand", "randn", "randint", "randperm", "zeros", "tensor", ] orig_empty = torch.empty # avoid override scm = ShapeConsistencyManager() class LazyTensor(torch.Tensor): """A naive implementation of LazyTensor (https://arxiv.org/pdf/2102.13267.pdf). Usage: 1. Use ``LazyTensor`` instead of ``torch.Tensor``. >>> x = LazyTensor(torch.zeros, 2, 3) >>> x += 1 >>> y = x * x >>> y = y.cuda().half() >>> y[0, 0] = 0 >>> y = y.materialize() # materialize the tensor >>> print(y) tensor([[0., 1., 1.], [1., 1., 1.]], device='cuda:0', dtype=torch.float16) 2. Generate ``MetaTensor`` from ``LazyTensor`` >>> x = LazyTensor(torch.zeros, 2, 3) >>> x.reshape(3, 2) >>> x = x.traceable() # generate ``MetaTensor`` >>> print(x) MetaTensor(..., size=(3, 2), device=cpu, dtype=torch.float32) 3. Use ``LazyTensor`` to generate sharded ``nn.Parameter``. >>> x = LazyTensor(torch.zeros, 2, 3) >>> x.spec = ... # some ``ShardingSpec`` >>> x.distribute() # distribute the tensor according to the ``ShardingSpec`` Warnings: 1. Cases that ``LazyTensor`` can't deal with. >>> x = LazyTensor(torch.ones, 2, 3) >>> x[0, 0] = -x[0, 0] # this will cause infinite recursion 2. ``LazyTensor.materialize()`` can't be called multiple times. >>> x = LazyTensor(torch.ones, 2, 3) >>> x.materialize() >>> x.materialize() # this is disallowed """ _repr = True _meta_data: Optional[MetaTensor] = None # shape, dtype, device _cached_data: Optional[torch.Tensor] = None # materialized data @staticmethod def __new__(cls, func, *args, dtype=None, device=None, **kwargs): elem = func(*args, dtype=dtype, device='meta', **kwargs) r = torch.Tensor._make_wrapper_subclass(cls, elem.size(), strides=elem.stride(), storage_offset=elem.storage_offset(), dtype=elem.dtype, layout=elem.layout, device=device if device is not None else torch.device('cpu'), requires_grad=elem.requires_grad) r._meta_data = MetaTensor(elem, fake_device=device) return r def __init__(self, func, *args, dtype=None, device=None, **kwargs): self._factory_method = (func, args, {'dtype': dtype, 'device': device, **kwargs}) # (func, args, kwargs) self._cached_buffer = list() # (func, args, kwargs) self._spec = None self._data = self def __repr__(self): if self._repr: # avoid recursive representation self.__class__._repr = False s = f'LazyTensor(..., size={tuple(self._meta_data.shape)}, device={self._meta_data.device}, dtype={self._meta_data.dtype})\n'\ f'factory method: {self._factory_method}\n'\ f'cached: {pprint.pformat(self._cached_buffer) if self._cached_data is None else self._cached_data}\n'\ f'spec: {self._spec}' self.__class__._repr = True return s else: return 'LazyTensor(...)' def materialize(self) -> torch.Tensor: """Materialize the ``LazyTensor`` to ``torch.Tensor``. Warnings: Calling ``self.materialize()`` will clear all cached sequence and factory method, because we don't allow materialize the same ``LazyTensor`` twice. This is mentioned in the paper: https://arxiv.org/pdf/2102.13267.pdf (Part 4.3). Returns: torch.Tensor: The materialized tensor. """ target = self._data._realize_cached_data() if isinstance(self, nn.Parameter): target = nn.Parameter(target, requires_grad=self.requires_grad) self._clear_all() return target def traceable(self) -> MetaTensor: """Generate ``MetaTensor`` from ``LazyTensor``. (Mostly for tracing) Returns: MetaTensor: The generated ``MetaTensor``. """ if isinstance(self, nn.Parameter): return nn.Parameter(self._meta_data, requires_grad=self.requires_grad) else: return self._meta_data def distribute(self) -> torch.Tensor: """Distribute the ``LazyTensor`` according to the ``ShardingSpec``. Returns: torch.Tensor: The sharded tensor. """ if self._spec is None: raise RuntimeError('ShardingSpec is not set for\n{self}') spec, device_mesh = self._spec, self._spec.device_mesh target = self.materialize() # TODO(some man): better not be coupled with auto-parallel target.data = scm.apply_for_autoparallel_runtime(target.data, ShardingSpec(device_mesh, target.shape, {}), spec).detach().clone() return target def _realize_cached_data(self) -> torch.Tensor: # self._cached_data should be generated after the first call of this function if self._cached_data is None: if self._factory_method is not None: # apply factory method func, args, kwargs = self._factory_method # apply cached sequence self._cached_data = self._apply_cache_buffer(func(*args, **kwargs)) else: # apply cached sequence only self._cached_data = self._apply_cache_buffer() return self._cached_data def _apply_cache_buffer(self, target=None) -> torch.Tensor: # dump all cached sequence # super-dainiu: support methods for single Tensor only def replace(x): if x is self: return target elif isinstance(x, LazyTensor): return x._realize_cached_data() return x packed = None for (func, args, kwargs) in self._cached_buffer: if func == torch.Tensor.requires_grad_: packed = func, args, kwargs # requires grad should be set at last else: o = func(*tree_map(replace, args), **tree_map(replace, kwargs)) target = o if isinstance(o, torch.Tensor) else target # if func returns non-Tensor, discard the value # super-dainiu: set requires_grad after all inplace-ops are done if packed is not None: func, args, kwargs = packed func(*tree_map(replace, args), **tree_map(replace, kwargs)) return target # clear all means: # 1. clear factory method # 2. clear cached sequence # 3. clear cached data def _clear_all(self): self._cached_data = None self._cached_buffer = None self._data = None gc.collect() # avoid memory leak # cache everything with __torch_function__ @classmethod def __torch_function__(cls, func, types, args=(), kwargs=None): if kwargs is None: kwargs = {} target = None if isinstance(func, torch._C.ScriptMethod): def unwrap(x): if isinstance(x, LazyTensor): return x._meta_data return x target: LazyTensor = args[0].clone() target._cached_buffer.append((func, args, kwargs)) target._meta_data = getattr(target._meta_data, func.name)(*tree_map(unwrap, args[1:]), **tree_map(unwrap, kwargs)) else: def unwrap(x): nonlocal target if isinstance(x, LazyTensor): target = x if (func.__name__.endswith('_') and not (func.__name__.endswith('__')) or func.__name__ == "__setitem__") else x.clone() target._cached_buffer.append((func, args, kwargs)) return x._meta_data return x args = tree_map(unwrap, args) kwargs = tree_map(unwrap, kwargs) o = func(*args, **kwargs) if isinstance(o, MetaTensor): target._meta_data = o return target else: return o @classmethod def __torch_dispatch__(cls, func, types, args=(), kwargs=None): pass # skip def clone(self) -> "LazyTensor": """Create a new ``LazyTensor`` with same cached sequence and factory method. Returns: LazyTensor: the new ``LazyTensor`` """ target = LazyTensor(orig_empty, 0, dtype=self._meta_data.dtype, device=self._meta_data.device) target._factory_method = None target._cached_buffer = list() target._meta_data = self._meta_data.clone() target._cached_data = self._cached_data.clone() if self._cached_data is not None else None target._spec = copy.deepcopy(self._spec) return target def detach(self) -> "LazyTensor": target = self.clone() target._cached_buffer.append((torch.Tensor.detach_, (self,), {})) return target @property def spec(self) -> ShardingSpec: return self._spec @spec.setter def spec(self, other: ShardingSpec): self._spec = other @property def data(self) -> "LazyTensor": return self._data.detach() @data.setter def data(self, other: "LazyTensor") -> "LazyTensor": """This avoid the following infinite recursion, which is very common in ``nn.Module`` initialization. Usage: >>> a = LazyTensor(torch.empty, 0, dtype=torch.float32, device='cpu') >>> b = a.cuda() >>> a.data = b """ self._data = other class LazyInitContext(): """Context manager for lazy initialization. Enables initializing the model without allocating real memory. Usage: 1. The model is initialized, but no real memory is allocated. >>> ctx = LazyInitContext() >>> with ctx: >>> model = MyModel().cuda() 2. The model is initialized with ``MetaTensor`` as weights, but still no real memory is allocated. >>> with ctx.traceable(model): >>> gm = symbolic_trace(model, meta_args=meta_args) >>> # Solve the execution strategy and apply the strategy to the model >>> strategy = StrategyAndSpec() 3. The model is initialized with ``torch.Tensor`` as weights, and real memory is allocated. (single device) >>> model = ctx.materialize(model) 3. The model is initialized with sharded ``torch.Tensor`` as weights, and real memory is allocated. (distributed scenario) >>> model = apply_strategy_to_all_params(model, strategy) >>> model = ctx.distribute(model) Warnings: This API is still experimental and further modifications can be made to it. For example: 1. Quantization strategies can be applied before allocating real memory. 2. Lazy initialization seems slower than normal initialization. """ def __init__(self): self.overrides = {} def __enter__(self): def wrap_factory_method(target): # factory functions (eg. torch.empty()) def wrapper(*args, **kwargs): return LazyTensor(target, *args, **kwargs) return wrapper, target def wrap_factory_like_method(orig_target, target): # factory_like functions (eg. torch.empty_like()) def wrapper(*args, **kwargs): orig_t = args[0] return LazyTensor(orig_target, *args[1:], device=orig_t.device, dtype=orig_t.dtype, **kwargs) return wrapper, target self.overrides = { target: wrap_factory_method(getattr(torch, target)) for target in _TorchFactoryMethod if callable(getattr(torch, target, None)) } self.overrides.update({ target + '_like': wrap_factory_like_method(getattr(torch, target), getattr(torch, target + '_like')) for target in _TorchFactoryMethod if callable(getattr(torch, target + '_like', None)) }) for name, (wrapper, orig) in self.overrides.items(): setattr(torch, name, wrapper) def __exit__(self, exc_type, exc_val, exc_tb): for name, (wrapper, orig) in self.overrides.items(): setattr(torch, name, orig) @staticmethod def materialize(module: torch.nn.Module): """Initialize all ``nn.Parameter`` from ``LazyTensor``. Args: module (torch.nn.Module): Target ``nn.Module`` """ @torch.no_grad() def init_recursively(module: nn.Module): # recursively initialize the module for mod in module.children(): init_recursively(mod) # initialize tensors directly attached to the current module for name, param in module.named_parameters(recurse=False): setattr(module, name, param.materialize()) for name, buf in module.named_buffers(recurse=False): setattr(module, name, buf.materialize()) init_recursively(module) return module @staticmethod def distribute(module: torch.nn.Module): """Initialize and shard all ``nn.Parameter`` from ``LazyTensor``. Args: module (torch.nn.Module): Sharded target ``nn.Module`` """ @torch.no_grad() def init_recursively(module: nn.Module): # recursively initialize the module for mod in module.children(): init_recursively(mod) # initialize tensors directly attached to the current module for name, param in module.named_parameters(recurse=False): setattr(module, name, param.distribute()) for name, buf in module.named_buffers(recurse=False): setattr(module, name, buf.distribute()) init_recursively(module) return module @staticmethod @contextlib.contextmanager def traceable(module: torch.nn.Module): """Initialize all ``nn.Parameters`` as ``MetaTensor``. This enables ``ColoTracer`` with control flow. Args: module (torch.nn.Module): Traceable ``nn.Module`` with ``MetaTensor`` as parameters. """ orig_val = dict() def init_recursively(module: nn.Module): # recursively initialize the module for mod in module.children(): init_recursively(mod) # initialize tensors directly attached to the current module for name, param in module.named_parameters(recurse=False): setattr(module, name, param.traceable()) orig_val[(module, name)] = param for name, buf in module.named_buffers(recurse=False): setattr(module, name, buf.traceable()) orig_val[(module, name)] = buf init_recursively(module) yield # restore original values for (module, name), val in orig_val.items(): setattr(module, name, val)
from typing import Any, Dict, Iterator, Optional, Tuple, Union import torch from torch import nn from colossalai.nn.parallel.layers import ColoEmbedding, ColoLinear, register_colo_module from colossalai.tensor import ColoParameter, ColoTensor, ProcessGroup from .utils import InsertPostInitMethodToModuleSubClasses # find named_params includes replica def _named_params_with_replica( module: nn.Module, prefix: str = '', recurse: bool = True, ) -> Iterator[Tuple[str, Union[nn.Parameter, ColoTensor]]]: modules = module.named_modules(prefix=prefix) if recurse else [(prefix, module)] for mod_prefix, mod in modules: for name, val in mod._parameters.items(): if val is None: continue name = mod_prefix + ('.' if mod_prefix else '') + name yield name, val def _convert_to_coloparam(param: torch.nn.Parameter, device: torch.device, dtype=torch.float, default_pg: Optional[ProcessGroup] = None, default_dist_spec: Optional[Any] = None) -> ColoParameter: if type(param) is ColoParameter: return param # detaching tensor is necessary for optimizers. requires_grad = param.requires_grad # param is the global tensor. if param.device.type == "meta": colo_param = ColoParameter(param, requires_grad=requires_grad) else: colo_param = ColoParameter(param.to(device=device, dtype=dtype), requires_grad=requires_grad) # if default_shard_plan exists, shard the param during initialization. # This can reduce the model size after initialization. # NOTE() embedding usually can not be correctly sharded. So I use except to handle # the param that can not be sharded by the default plan if default_pg is not None: colo_param.set_process_group(default_pg) if default_dist_spec is not None: try: colo_param.set_dist_spec(default_dist_spec) except: pass return colo_param def ColoModulize(module): """ Replacing the parameters() and named_parameters() with our customized ones """ module._colo_visited = True class ColoInitContext(InsertPostInitMethodToModuleSubClasses): def __init__(self, device: torch.device = torch.device('cpu'), dtype: torch.dtype = torch.float, default_pg: Optional[ProcessGroup] = None, default_dist_spec=None): """ Args: device (torch.device): the device where parameters initialized are resident. Defaults to torch.device('cpu'). dtype (torch.dtype): the dtype of parameters initialized. Defults to torch.float. default_pg (ProcessGroup): the default process group for all initialized parameters. default_dist_spec: the default distributed specifications. """ super().__init__() self._device = device self._dtype = dtype self._register_colo_modules() self._default_pg = default_pg self._default_dist_spec = default_dist_spec def _register_colo_modules(self): register_colo_module(torch.nn.Linear, ColoLinear()) register_colo_module(torch.nn.Embedding, ColoEmbedding()) def _pre_context_exec(self): pass def _post_init_method(self, module: torch.nn.Module, *args, **kwargs): """ The function to call at the end of the constructor of each module. FIXME(fjr) The module may be passed to this function multiple times? """ name_list = [] for name, param in _named_params_with_replica(module): if type(param) is ColoParameter: continue split = name.rfind('.') if split >= 0: # param in submodule module_name = name[:split] param_name = name[split + 1:] else: module_name = '' # param in current module param_name = name name_list.append((module_name, param_name)) replaced_tensors = dict( ) # record mapping between (torch.Tensor, ColoTensor) to distinguish the same reference for module_name, param_name in name_list: submodule = module.get_submodule(module_name) param = submodule.get_parameter(param_name) if param in replaced_tensors: colo_param = replaced_tensors[param] else: colo_param = _convert_to_coloparam(param, self._device, self._dtype, self._default_pg, self._default_dist_spec) replaced_tensors[param] = colo_param delattr(submodule, param_name) setattr(submodule, param_name, colo_param) colo_param.shared_param_modules.append(submodule) param_number = 0 meta_param_number = 0 buffer_number = 0 meta_buffer_number = 0 for param in module.parameters(): param_number += 1 meta_param_number += (param.device.type == 'meta') for buffer in module.buffers(): buffer_number += 1 meta_buffer_number += (buffer.device.type == 'meta') if meta_param_number > 0 and meta_param_number != param_number: raise ValueError("Meta parameters and valued parameters can not be in the same model") if meta_buffer_number > 0 and meta_buffer_number != buffer_number: raise ValueError("Meta buffers and valued buffers can not be in the same model") if meta_buffer_number == 0: for buffer in module.buffers(): buffer.data = buffer.data.to(device=self._device) def post_process_colo_init_ctx(model: torch.nn.Module, device: torch.device = torch.device('cpu'), dtype: torch.dtype = torch.float, default_pg: Optional[ProcessGroup] = None, default_dist_spec=None): """post_process_colo_init_ctx This function is called after `ColoInitContext`. Args: model (torch.nn.module): the model device (torch.device, optional): device type of the model params. Defaults to torch.device('cpu'). dtype (torch.dtype, optional): dtype of the model params. Defaults to torch.float. default_pg (Optional[ProcessGroup], optional): default process group. Defaults to None. Inidicates a DP-only process group. default_dist_spec (Any, optional): default dist spec of params. Defaults to None. Raises: RuntimeError: raise error if """ torch_params = [] for n, p in model.named_parameters(): if not isinstance(p, ColoParameter): # print(f"{n} is not a ColoParameter. We are going to converting it to ColoParameter") torch_params.append((n, p)) for (n, param) in torch_params: name_list = n.split('.') module = model for i in range(len(name_list) - 1): module = module._modules[name_list[i]] delattr(module, name_list[-1]) setattr(module, name_list[-1], _convert_to_coloparam(param, device, dtype, default_pg, default_dist_spec)) del torch_params for n, p in model.named_parameters(): if not isinstance(p, ColoTensor): raise RuntimeError
import torch import functools from typing import Optional def substitute_init_recursively(cls, func, visited: set): for subcls in cls.__subclasses__(): substitute_init_recursively(subcls, func, visited) if subcls not in visited: func(subcls) visited.add(subcls) def call_to_str(base, *args, **kwargs): """Construct a string representation of a call. Args: base (str): name of the call args (tuple, optional): args to ``base`` kwargs (dict, optional): kwargs supplied to ``base`` Returns: str: A string representation of base(*args, **kwargs) """ name = f'{base}(' if args: name += ', '.join(repr(arg) for arg in args) if kwargs: name += ', ' if kwargs: name += ', '.join(f'{key}={repr(arg)}' for key, arg in kwargs.items()) name += ')' return name class InsertPostInitMethodToModuleSubClasses(object): def __init__(self, default_dtype: Optional[torch.dtype] = None): self._old_default_dtype = None self._default_dtype = default_dtype def __enter__(self): r""" Enter the context scope. """ if self._default_dtype is not None: self._old_default_dtype = torch.get_default_dtype() torch.set_default_dtype(self._default_dtype) def preprocess_after(f): @functools.wraps(f) def wrapper(module: torch.nn.Module, *args, **kwargs): f(module, *args, **kwargs) self._post_init_method(module, *args, **kwargs) return wrapper def _enable_class(cls): cls._old_init = cls.__init__ cls.__init__ = preprocess_after(cls.__init__) # The function is called during init subclass. def _init_subclass(cls, **kwargs): cls.__init__ = preprocess_after(cls.__init__) # Replace .__init__() for all existing subclasses of torch.nn.Module # Excution self._post_init_method after the default init function. substitute_init_recursively(torch.nn.modules.module.Module, _enable_class, set()) # holding on to the current __init__subclass__ for exit torch.nn.modules.module.Module._old_init_subclass = (torch.nn.modules.module.Module.__init_subclass__) # Replace .__init__() for future subclasses of torch.nn.Module torch.nn.modules.module.Module.__init_subclass__ = classmethod(_init_subclass) self._pre_context_exec() return self def __exit__(self, exc_type, exc_value, traceback): if self._default_dtype is not None: torch.set_default_dtype(self._old_default_dtype) def _disable_class(cls): if not hasattr(cls, '_old_init'): raise AttributeError( f"_old_init is not found in the {cls.__name__}, please make sure that you have imported {cls.__name__} before entering the context." ) cls.__init__ = cls._old_init # Replace .__init__() for all existing subclasses of torch.nn.Module substitute_init_recursively(torch.nn.modules.module.Module, _disable_class, set()) # Replace .__init__() for future subclasses of torch.nn.Module torch.nn.modules.module.Module.__init_subclass__ = (torch.nn.modules.module.Module._old_init_subclass) self._post_context_exec() # Now that we cleaned up the metaclass injection, raise the exception. if exc_type is not None: return False # To be implemented by inheriting classes def _post_init_method(self, module, *args, **kwargs): pass def _pre_context_exec(self): pass def _post_context_exec(self): pass
import os import threading import time import torch from enum import Enum from typing import List from colossalai.gemini.stateful_tensor import StatefulTensor from colossalai.gemini.ophooks import BaseOpHook from colossalai.engine import Engine from colossalai.utils.profiler.extention import ProfilerExtension class DeviceType(Enum): CPU = 0 CUDA = 1 def get_timestamp_us(): return int(time.time() * 1e6) def generic_instant_event(name, pid, tid, timestamp, args): return {'ph': 'i', 's': 't', 'name': name, 'pid': pid, 'tid': tid, 'ts': timestamp, 'args': args} class StatefulTensorMemoryEvent: EVENT_NAME = '[statefulTensorMemory]' def __init__(self, timestamp: int, device_type: DeviceType, bytes_: int) -> None: self.pid = os.getpid() self.tid = threading.get_ident() self.timestamp = timestamp self.device_type = device_type self.device_id = torch.cuda.current_device() if device_type == DeviceType.CUDA else -1 self.bytes = bytes_ def state_dict(self): return generic_instant_event(StatefulTensorMemoryEvent.EVENT_NAME, self.pid, self.tid, self.timestamp, { 'Device Type': self.device_type.value, 'Device Id': self.device_id, 'Bytes': self.bytes }) class StatefulTensorMemoryTracer: def __init__(self) -> None: self.events: List[StatefulTensorMemoryEvent] = [] self._tracing = False def sample(self): cuda_mem = StatefulTensor.GST_MGR.total_mem['cuda'] cpu_mem = StatefulTensor.GST_MGR.total_mem['cpu'] timestamp = get_timestamp_us() if self._tracing: self.events.append(StatefulTensorMemoryEvent(timestamp, DeviceType.CUDA, cuda_mem)) self.events.append(StatefulTensorMemoryEvent(timestamp, DeviceType.CPU, cpu_mem)) def start_trace(self): self.events.clear() self._tracing = True def stop_trace(self): self._tracing = False def state_dict(self): return [event.state_dict() for event in self.events] class StatefulTensorMemoryTracerHook(BaseOpHook): def __init__(self, tracer: StatefulTensorMemoryTracer): super().__init__() self.tracer = tracer self._enable = False def pre_fwd_exec(self, module: torch.nn.Module, *args): if self._enable: self.tracer.sample() def post_fwd_exec(self, module: torch.nn.Module, *args): if self._enable: self.tracer.sample() def pre_bwd_exec(self, module: torch.nn.Module, input_, output): if self._enable: self.tracer.sample() def post_bwd_exec(self, module: torch.nn.Module, input_): if self._enable: self.tracer.sample() def post_iter(self): if self._enable: self.tracer.sample() def enable(self): self._enable = True def disable(self): self._enable = False class StatefulTensorMemoryProfilerExtention(ProfilerExtension): def __init__(self, engine: Engine) -> None: self.engine = engine self.tracer = StatefulTensorMemoryTracer() self.hook = StatefulTensorMemoryTracerHook(self.tracer) self.hook_registered = False def prepare_trace(self): self.hook.enable() if not self.hook_registered: self.engine.add_hook(self.hook) self.hook_registered = True def start_trace(self): self.prepare_trace() self.tracer.start_trace() def stop_trace(self): self.tracer.stop_trace() self.hook.disable() if self.hook_registered: self.engine.remove_hook(self.hook) # remove_hook is not implemented now # FIXME(ver217): uncomment below line when remove_hook is implemented # self.hook_registered = False def extend_chrome_trace(self, trace: dict) -> dict: trace['traceEvents'].extend(self.tracer.state_dict()) return trace
from .legacy import * from .profiler import profile
from abc import ABC, abstractmethod class ProfilerExtension(ABC): @abstractmethod def prepare_trace(self): pass @abstractmethod def start_trace(self): pass @abstractmethod def stop_trace(self): pass @abstractmethod def extend_chrome_trace(self, trace: dict) -> dict: pass
import os from typing import List from colossalai.engine import Engine from torch.profiler import profile as torch_profile from torch.profiler.profiler import ProfilerAction from typing import Any, Callable, Iterable, Optional from torch.autograd import ProfilerActivity import json import os import tempfile import gzip from colossalai.utils.profiler.extention import ProfilerExtension from colossalai.utils.profiler.stateful_tensor_mem_extention import StatefulTensorMemoryProfilerExtention from colossalai.logging import get_dist_logger class profile(torch_profile): """Profiler context manager. Args: activities (iterable): list of activity groups (CPU, CUDA) to use in profiling, supported values: ``torch.profiler.ProfilerActivity.CPU``, ``torch.profiler.ProfilerActivity.CUDA``. Default value: ProfilerActivity.CPU and (when available) ProfilerActivity.CUDA. schedule (callable): callable that takes step (int) as a single parameter and returns ``ProfilerAction`` value that specifies the profiler action to perform at each step. on_trace_ready (callable): callable that is called at each step when ``schedule`` returns ``ProfilerAction.RECORD_AND_SAVE`` during the profiling. engine (Optional[Engine], optional): An ``Engine`` instance. Defaults to None. record_shapes (bool): save information about operator's input shapes. profile_memory (bool): track tensor memory allocation/deallocation. with_stack (bool): record source information (file and line number) for the ops. with_flops (bool): use formula to estimate the FLOPs (floating point operations) of specific operators (matrix multiplication and 2D convolution). with_modules (bool): record module hierarchy (including function names) corresponding to the callstack of the op. e.g. If module A's forward call's module B's forward which contains an aten::add op, then aten::add's module hierarchy is A.B Note that this support exist, at the moment, only for TorchScript models and not eager mode models. profile_stateful_tensor_memory (bool): track stateful tensor memory usage. ``engine`` must not be None if you enable this. .. note:: Use :func:`~torch.profiler.schedule` to generate the callable schedule. Non-default schedules are useful when profiling long training jobs and allow the user to obtain multiple traces at the different iterations of the training process. The default schedule simply records all the events continuously for the duration of the context manager. .. note:: Use :func:`~torch.profiler.tensorboard_trace_handler` to generate result files for TensorBoard: ``on_trace_ready=torch.profiler.tensorboard_trace_handler(dir_name)`` After profiling, result files can be found in the specified directory. Use the command: ``tensorboard --logdir dir_name`` to see the results in TensorBoard. For more information, see `PyTorch Profiler TensorBoard Plugin <https://github.com/pytorch/kineto/tree/master/tb_plugin>`__ .. note:: Enabling shape and stack tracing results in additional overhead. When record_shapes=True is specified, profiler will temporarily hold references to the tensors; that may further prevent certain optimizations that depend on the reference count and introduce extra tensor copies. Examples: .. code-block:: python with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ] ) as p: code_to_profile() print(p.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) Using the profiler's ``schedule``, ``on_trace_ready`` and ``step`` functions: .. code-block:: python # Non-default profiler schedule allows user to turn profiler on and off # on different iterations of the training loop; # trace_handler is called every time a new trace becomes available def trace_handler(prof): print(prof.key_averages().table( sort_by="self_cuda_time_total", row_limit=-1)) # prof.export_chrome_trace("/tmp/test_trace_" + str(prof.step_num) + ".json") with torch.profiler.profile( activities=[ torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA, ], # In this example with wait=1, warmup=1, active=2, # profiler will skip the first step/iteration, # start warming up on the second, record # the third and the forth iterations, # after which the trace will become available # and on_trace_ready (when set) is called; # the cycle repeats starting with the next step schedule=torch.profiler.schedule( wait=1, warmup=1, active=2), on_trace_ready=trace_handler # on_trace_ready=torch.profiler.tensorboard_trace_handler('./log') # used when outputting for tensorboard ) as p: for iter in range(N): code_iteration_to_profile(iter) # send a signal to the profiler that the next iteration has started p.step() """ def __init__(self, *, activities: Optional[Iterable[ProfilerActivity]] = None, schedule: Optional[Callable[[int], ProfilerAction]] = None, on_trace_ready: Optional[Callable[..., Any]] = None, engine: Optional[Engine] = None, record_shapes: bool = False, profile_memory: bool = False, with_stack: bool = False, with_flops: bool = False, with_modules: bool = False, profile_stateful_tensor_memory: bool = False) -> None: super().__init__(activities=activities, schedule=schedule, on_trace_ready=on_trace_ready, record_shapes=record_shapes, profile_memory=profile_memory, with_stack=with_stack, with_flops=with_flops, with_modules=with_modules) self._logger = get_dist_logger() self.extentions: List[ProfilerExtension] = [] if profile_stateful_tensor_memory: if engine is None: self._logger.warning('Ignore "profile_model_data" since engine is None', ranks=[0]) else: self.extentions.append(StatefulTensorMemoryProfilerExtention(engine)) def prepare_trace(self) -> None: if hasattr(super(), 'prepare_trace'): super().prepare_trace() elif hasattr(super(), '_start_warmup'): super()._start_warmup() for ext in self.extentions: ext.prepare_trace() def _start_warmup(self): self.prepare_trace() def start_trace(self): if hasattr(super(), '_start_trace'): super()._start_trace() elif hasattr(super(), 'start_trace'): super().start_trace() for ext in self.extentions: ext.start_trace() def _start_trace(self): self.start_trace() def stop_trace(self): if hasattr(super(), '_stop_trace'): super()._stop_trace() elif hasattr(super(), 'stop_trace'): super().stop_trace() for ext in self.extentions: ext.stop_trace() def _stop_trace(self): self.stop_trace() def export_chrome_trace(self, path: str): """ Exports the collected trace in Chrome JSON format. """ assert self.profiler fp = tempfile.NamedTemporaryFile('w+t', suffix='.json', delete=False) fp.close() retvalue = self.profiler.export_chrome_trace(fp.name) with open(fp.name) as fin: trace = json.load(fin) for ext in self.extentions: trace = ext.extend_chrome_trace(trace) open_func = gzip.open if path.endswith('.gz') else open with open_func(path, 'wt') as fout: json.dump(trace, fout) os.remove(fp.name) return retvalue
from pathlib import Path from torch.autograd.profiler import profile from .prof_utils import BaseProfiler, _format_time, _format_memory, _format_bandwidth from typing import List def _get_size(dtype: str): if dtype == "fp16": return 2 elif dtype == "fp32": return 4 else: raise NotImplementedError def _get_numel(my_list: List[int]) -> int: from functools import reduce from operator import mul return reduce(mul, my_list) def _reduce_location(locations: List[str]) -> str: ret = [] for lo in locations: ret.append(lo) ret.append("\n") ret = ret[:-1] return ''.join(ret) class PcieEvent(object): """Pcie Event. """ def __init__(self, count: int = 0, pcie_vol: int = 0, cuda_time: int = 0): self.count = count self.pcie_vol = pcie_vol self.cuda_time = cuda_time def add(self, rhs): self.count += rhs.count self.pcie_vol += rhs.pcie_vol self.cuda_time += rhs.cuda_time class PcieProfiler(BaseProfiler): """Pcie profiler. Records all data transmission between CPU and GPU. TODO: Merge pcie profiler into communication profiler """ def __init__(self, dtype: str = "fp32", depth: int = 1): super().__init__(profiler_name="Pcie", priority=10) self.depth = depth self.data_size = _get_size(dtype) self.h2d_count = 0 self.h2d_time = 0 self.d2h_count = 0 self.d2h_time = 0 self.ops_record = dict() self.profiler = None def reset(self): self.h2d_count = 0 self.h2d_time = 0 self.d2h_count = 0 self.d2h_time = 0 self.ops_record = dict() self.profiler = None def enable(self): self.profiler = profile(enabled=True, use_cuda=True, use_cpu=True, use_kineto=True, record_shapes=True, with_stack=True) self.profiler.__enter__() def disable(self): self.profiler.__exit__(None, None, None) if self.profiler.enabled: events = self.profiler.function_events for event in events: if event.name == "aten::copy_": t_shape = event.input_shapes[0] if len(t_shape) == 0 or event.cuda_time_total == 0 or len(event.stack) == 0: continue current_comm_event = PcieEvent(1, self.data_size * _get_numel(t_shape), event.cuda_time_total) code_location = _reduce_location(event.stack[:self.depth]) if code_location in self.ops_record: self.ops_record[code_location].add(current_comm_event) else: self.ops_record[code_location] = current_comm_event elif 'Memcpy HtoD' in event.name: self.h2d_count += 1 self.h2d_time += event.cuda_time_total elif 'Memcpy DtoH' in event.name: self.d2h_count += 1 self.d2h_time += event.cuda_time_total self.profiler = None def to_tensorboard(self, writer): writer.add_text(tag="Data Transmission", text_string=self.result_str("\n\n")) def to_file(self, filename: Path): with open(filename, "w") as f: f.write(self.result_str()) def show(self): print(self.result_str()) def result_str(self, sep: str = "\n"): res = [] def append(s: str = None): if s is not None: res.append(s) res.append(sep) append("Pcie profiling result:") append("time of data transmission (CPU -> GPU): {}".format(_format_time(self.h2d_time))) append("number of transmission (CPU -> GPU): {}".format(self.h2d_count)) append("time of data transmission (GPU -> CPU): {}".format(_format_time(self.d2h_time))) append("number of transmission (GPU -> CPU): {}".format(self.d2h_count)) append("Possible data transmission events in PCIE:") seperation = '-' * 62 row_format = '{:^10}' + '{:^12}' + '{:^16}' + '{:^12}' * 2 append(seperation) append(row_format.format('Location', 'GPU time', 'Trans volume', 'Bandwidth', 'Num of calls')) append(seperation) show_list = sorted(self.ops_record.items(), key=lambda kv: -kv[1].cuda_time) for location, event in show_list: append(location) append( row_format.format('', _format_time(event.cuda_time), _format_memory(event.pcie_vol), _format_bandwidth(event.pcie_vol, event.cuda_time), event.count)) append() return ''.join(res)
import inspect from pathlib import Path from functools import partial import torch from torch.autograd.profiler import profile import torch.distributed as dist from torch.distributed import ReduceOp from colossalai.utils import get_current_device from .prof_utils import BaseProfiler, _format_time, _format_memory, _format_bandwidth from typing import List, Optional def _get_code_location(depth: int): ret = [] length = min(len(inspect.stack()), depth + 1) for i in range(3, length): upper_frame = inspect.stack()[i] function_name = inspect.stack()[i - 1].function ret.append(upper_frame.filename) ret.append('(') ret.append(str(upper_frame.lineno)) ret.append('): ') ret.append(function_name) if i != length - 1: ret.append('\n') return ''.join(ret) torch_all_reduce = dist.all_reduce torch_all_gather = dist.all_gather torch_reduce_scatter = dist.reduce_scatter torch_broadcast = dist.broadcast torch_reduce = dist.reduce class CommEvent(object): """Communication Event. Used for communication time and communication volume recording. """ def __init__(self, count: int = 0, comm_vol: float = 0., cuda_time: int = 0): self.self_count = count self.self_comm_vol = comm_vol self.self_cuda_time = cuda_time def add(self, rhs): self.self_count += rhs.self_count self.self_comm_vol += rhs.self_comm_vol self.self_cuda_time += rhs.self_cuda_time class CommProfiler(BaseProfiler): """Communication profiler. Records all communication events. """ def __init__(self, depth: int = 0, total_count: int = 0, total_comm_vol: float = 0, total_cuda_time: int = 0): super().__init__(profiler_name="Collective_Communication", priority=0) self.depth = 3 + depth self.total_count = total_count self.total_comm_vol = total_comm_vol self.total_cuda_time = total_cuda_time self.ops_record = dict() self.profiler = None self.pending_op = None self.pending_metadata = None self.warn_flag = False def reset(self): self.total_count = 0 self.total_comm_vol = 0 self.total_cuda_time = 0 self.ops_record = dict() self.profiler = None self.pending_op = None self.pending_metadata = None self.warn_flag = False def enable(self): dist.all_reduce = partial(all_reduce, profiler=self) dist.all_gather = partial(all_gather, profiler=self) dist.reduce_scatter = partial(reduce_scatter, profiler=self) dist.broadcast = partial(broadcast, profiler=self) dist.reduce = partial(reduce, profiler=self) def disable(self): dist.all_reduce = torch_all_reduce dist.all_gather = torch_all_gather dist.reduce_scatter = torch_reduce_scatter dist.broadcast = torch_broadcast dist.reduce = torch_reduce def to_tensorboard(self, writer): writer.add_text(tag="Collective Communication", text_string=self.result_str("\n\n")) def to_file(self, filename: Path): with open(filename, "w") as f: f.write(self.result_str()) def show(self): print(self.result_str()) def result_str(self, sep: str = "\n"): res = [] def append(s: str = None): if s is not None: res.append(s) res.append(sep) if self.warn_flag: append("Warnning: there exists multiple communication operations in the same time. As a result, " "the profiling result is not accurate.") if self.total_cuda_time == 0: return "No collective communication has been called yet!" append("Collective communication profiling result:") append("total cuda time: {}".format(_format_time(self.total_cuda_time))) append("average bandwidth: {}".format(_format_bandwidth(self.total_comm_vol, self.total_cuda_time))) append("total number of calls: {}".format(self.total_count)) append("All events:") seperation = '-' * 74 row_format = '{:^10}' + '{:^12}' * 2 + '{:^16}' + '{:^12}' * 2 append(seperation) append(row_format.format('Location', 'GPU time', 'Percentage', 'Comm volume', 'Bandwidth', 'Num of calls')) append(seperation) show_list = sorted(self.ops_record.items(), key=lambda kv: -kv[1].self_cuda_time) for location, event in show_list: append(location) append( row_format.format('', _format_time(event.self_cuda_time), '{:.1f}%'.format(event.self_cuda_time / self.total_cuda_time * 100.0), _format_memory(event.self_comm_vol), _format_bandwidth(event.self_comm_vol, event.self_cuda_time), event.self_count)) append() return ''.join(res) @property def has_aync_op(self): return self.pending_op is not None def activate_profiler(self, kn: str, vol: float): self.pending_metadata = (kn, _get_code_location(self.depth), vol) self.profiler = profile(enabled=True, use_cuda=True, use_cpu=True, use_kineto=True) self.profiler.__enter__() def close_profiler(self, group=None): assert self.profiler is not None, "There is no running dist op" kernel_name, code_location, vol = self.pending_metadata self.profiler.__exit__(None, None, None) if self.profiler.enabled and dist.get_world_size(group) > 1: assert_flag = 0 current_comm_event = None events = self.profiler.function_events for event in events: if kernel_name in event.name: assert assert_flag == 0, "Multiple dist ops has been called " current_comm_event = CommEvent(1, vol, event.self_cuda_time_total) assert_flag += 1 assert current_comm_event is not None, "dist op has not been found" buffer = torch.tensor([current_comm_event.self_cuda_time], device=get_current_device()) torch_all_reduce(buffer, op=ReduceOp.MIN, group=group) current_comm_event.self_cuda_time = buffer.item() self.total_count += current_comm_event.self_count self.total_comm_vol += current_comm_event.self_comm_vol self.total_cuda_time += current_comm_event.self_cuda_time if code_location in self.ops_record: self.ops_record[code_location].add(current_comm_event) else: self.ops_record[code_location] = current_comm_event self.profiler = None self.pending_op = None self.pending_metadata = None def wait_async_op(self): if self.pending_op is not None: op = self.pending_op op.wait() self.close_profiler() class CommHandler(object): """Communication handler. A dummy handler to wait aync operations. """ def __init__(self, profiler: CommProfiler): super().__init__() self.prof = profiler def wait(self): self.prof.wait_async_op() def async_check(profiler: CommProfiler): if profiler.pending_op is not None: profiler.warn_flag = True profiler.wait_async_op() def all_reduce(tensor: torch.Tensor, op: ReduceOp = ReduceOp.SUM, group=None, async_op: bool = False, profiler: CommProfiler = None) -> Optional[CommHandler]: async_check(profiler) comm_size = dist.get_world_size(group) correction = 2 * (comm_size - 1) / comm_size comm_vol = correction * tensor.element_size() * tensor.numel() profiler.activate_profiler("ncclKernel_AllReduce_", comm_vol) profiler.pending_op = torch_all_reduce(tensor, op, group, async_op) if async_op: return CommHandler(profiler) profiler.close_profiler(group) def reduce_scatter(output: torch.Tensor, input_list: List[torch.Tensor], op: ReduceOp = ReduceOp.SUM, group=None, async_op: bool = False, profiler: CommProfiler = None) -> Optional[CommHandler]: async_check(profiler) comm_size = dist.get_world_size(group) correction = (comm_size - 1) / comm_size comm_vol = 0 for tensor in input_list: comm_vol += tensor.element_size() * tensor.numel() comm_vol *= correction profiler.activate_profiler("ncclKernel_ReduceScatter_", comm_vol) profiler.pending_op = torch_reduce_scatter(output, input_list, op, group, async_op) if async_op: return CommHandler(profiler) profiler.close_profiler(group) def all_gather(tensor_list: List[torch.Tensor], tensor: torch.Tensor, group=None, async_op: bool = False, profiler: CommProfiler = None) -> Optional[CommHandler]: async_check(profiler) comm_size = dist.get_world_size(group) correction = (comm_size - 1) / comm_size comm_vol = 0 for ten in tensor_list: comm_vol += ten.element_size() * ten.numel() comm_vol *= correction profiler.activate_profiler("ncclKernel_AllGather_", comm_vol) profiler.pending_op = torch_all_gather(tensor_list, tensor, group, async_op) if async_op: return CommHandler(profiler) profiler.close_profiler(group) def broadcast(tensor: torch.Tensor, src: int, group=None, async_op: bool = False, profiler: CommProfiler = None) -> Optional[CommHandler]: async_check(profiler) comm_vol = 1.0 * tensor.element_size() * tensor.numel() profiler.activate_profiler("ncclKernel_Broadcast_", comm_vol) profiler.pending_op = torch_broadcast(tensor, src, group, async_op) if async_op: return CommHandler(profiler) profiler.close_profiler(group) def reduce(tensor: torch.Tensor, dst: int, op: ReduceOp = ReduceOp.SUM, group=None, async_op: bool = False, profiler: CommProfiler = None) -> Optional[CommHandler]: async_check(profiler) comm_vol = 1.0 * tensor.element_size() * tensor.numel() profiler.activate_profiler("ncclKernel_Reduce_", comm_vol) profiler.pending_op = torch_reduce(tensor, dst, op, group, async_op) if async_op: return CommHandler(profiler) profiler.close_profiler(group)
from .comm_profiler import CommProfiler from .pcie_profiler import PcieProfiler from .prof_utils import ProfilerContext, BaseProfiler from .mem_profiler import MemProfiler __all__ = ['BaseProfiler', 'CommProfiler', 'PcieProfiler', 'MemProfiler', 'ProfilerContext']
from abc import ABC, abstractmethod from pathlib import Path from typing import Union, List from colossalai.core import global_context as gpc # copied from high version pytorch to support low version def _format_time(time_us): """Defines how to format time in FunctionEvent""" US_IN_SECOND = 1000.0 * 1000.0 US_IN_MS = 1000.0 if time_us >= US_IN_SECOND: return '{:.3f}s'.format(time_us / US_IN_SECOND) if time_us >= US_IN_MS: return '{:.3f}ms'.format(time_us / US_IN_MS) return '{:.3f}us'.format(time_us) # copied from high version pytorch to support low version def _format_memory(nbytes): """Returns a formatted memory size string""" KB = 1024 MB = 1024 * KB GB = 1024 * MB if (abs(nbytes) >= GB): return '{:.2f} GB'.format(nbytes * 1.0 / GB) elif (abs(nbytes) >= MB): return '{:.2f} MB'.format(nbytes * 1.0 / MB) elif (abs(nbytes) >= KB): return '{:.2f} KB'.format(nbytes * 1.0 / KB) else: return str(nbytes) + ' B' def _format_bandwidth(volme: float or int, time_us: int): sec_div_mb = (1000.0 / 1024.0)**2 mb_per_sec = volme / time_us * sec_div_mb if mb_per_sec >= 1024.0: return '{:.3f} GB/s'.format(mb_per_sec / 1024.0) else: return '{:.3f} MB/s'.format(mb_per_sec) class BaseProfiler(ABC): def __init__(self, profiler_name: str, priority: int): self.name = profiler_name self.priority = priority @abstractmethod def enable(self): pass @abstractmethod def disable(self): pass @abstractmethod def to_tensorboard(self, writer): pass @abstractmethod def to_file(self, filename: Path): pass @abstractmethod def show(self): pass class ProfilerContext(object): """Profiler context manager Usage:: world_size = 4 inputs = torch.randn(10, 10, dtype=torch.float32, device=get_current_device()) outputs = torch.empty(world_size, 10, 10, dtype=torch.float32, device=get_current_device()) outputs_list = list(torch.chunk(outputs, chunks=world_size, dim=0)) cc_prof = CommProfiler() with ProfilerContext([cc_prof]) as prof: op = dist.all_reduce(inputs, async_op=True) dist.all_gather(outputs_list, inputs) op.wait() dist.reduce_scatter(inputs, outputs_list) dist.broadcast(inputs, 0) dist.reduce(inputs, 0) prof.show() """ def __init__(self, profilers: List[BaseProfiler] = None, enable: bool = True): self.enable = enable self.profilers = sorted(profilers, key=lambda prof: prof.priority) def __enter__(self): if self.enable: for prof in self.profilers: prof.enable() return self def __exit__(self, exc_type, exc_val, exc_tb): if self.enable: for prof in self.profilers: prof.disable() def to_tensorboard(self, writer): from torch.utils.tensorboard import SummaryWriter assert isinstance(writer, SummaryWriter), \ f'torch.utils.tensorboard.SummaryWriter is required, but found {type(writer)}.' for prof in self.profilers: prof.to_tensorboard(writer) def to_file(self, log_dir: Union[str, Path]): if isinstance(log_dir, str): log_dir = Path(log_dir) if not log_dir.exists(): log_dir.mkdir(parents=True, exist_ok=True) for prof in self.profilers: log_file = log_dir.joinpath(f'{prof.name}_rank_{gpc.get_global_rank()}.log') prof.to_file(log_file) def show(self): for prof in self.profilers: prof.show()
import shutil import tempfile from abc import ABC, abstractmethod from typing import Dict, List, Type from .reader import CheckpointReader, DiskCheckpointReader from .writer import CheckpointWriter, DiskCheckpointWriter _backends: Dict[str, Type['CheckpointIOBackend']] = {} def register(name: str): assert name not in _backends, f'"{name}" is registered' def wrapper(cls): _backends[name] = cls return cls return wrapper def get_backend(name: str) -> 'CheckpointIOBackend': assert name in _backends, f'Unsupported backend "{name}"' return _backends[name]() class CheckpointIOBackend(ABC): def __init__(self) -> None: super().__init__() self.temps: List[str] = [] @abstractmethod def get_writer(self, base_name: str, overwrite: bool = False, rank: int = 0, world_size: int = 1) -> CheckpointWriter: pass @abstractmethod def get_reader(self, base_name: str) -> CheckpointReader: pass @abstractmethod def get_temp(self, base_name: str) -> str: pass @abstractmethod def clean_temp(self) -> None: pass @register('disk') class CheckpointDiskIO(CheckpointIOBackend): def get_writer(self, base_name: str, overwrite: bool = False, rank: int = 0, world_size: int = 1) -> CheckpointWriter: return DiskCheckpointWriter(base_name, overwrite, rank=rank, world_size=world_size) def get_reader(self, base_name: str) -> CheckpointReader: return DiskCheckpointReader(base_name) def get_temp(self, base_name: str) -> str: temp_dir_name = tempfile.mkdtemp(dir=base_name) self.temps.append(temp_dir_name) return temp_dir_name def clean_temp(self) -> None: for temp_dir_name in self.temps: shutil.rmtree(temp_dir_name)
import warnings from typing import Any, Callable, Dict, Generator, List, Optional, Tuple import torch.distributed as dist from torch.nn import Module from torch.optim import Optimizer from .backend import get_backend from .convertor import (CheckpointConvertor, ModelCheckpointMerger, ModelCheckpointRedistor, OptimizerCheckpointMerger, OptimizerCheckpointRedistor) from .meta import ParamDistMeta, RedistMeta from .utils import build_checkpoints, optimizer_load_state_dict def save(path: str, model: Module, optimizer: Optional[Optimizer] = None, param_to_os: Optional[Dict[str, int]] = None, dist_meta: Optional[Dict[str, ParamDistMeta]] = None, max_shard_size_gb: float = 0.0, overwrite: bool = False, backend: str = 'disk', **kwargs: Any) -> None: io_backend = get_backend(backend) if dist.is_initialized(): rank = dist.get_rank() world_size = dist.get_world_size() else: rank = 0 world_size = 1 if world_size == 1: # global doesn't need dist_meta dist_meta = None else: assert dist_meta is not None max_shard_size = int(max_shard_size_gb * 1024**3) model_checkpoints, optimizer_checkpoints, meta_checkpoint = build_checkpoints(max_shard_size, model, optimizer, param_to_os, dist_meta) writer = io_backend.get_writer(path, overwrite, rank, world_size) writer.save_others(kwargs) for model_checkpoint in model_checkpoints: writer.save_model(model_checkpoint) for optimizer_checkpoint in optimizer_checkpoints: writer.save_optimizer(optimizer_checkpoint) writer.save_meta(meta_checkpoint) def merge(path: str, output_path: str, max_shard_size_gb: float = 0.0, overwrite: bool = False, backend: str = 'disk') -> bool: io_backend = get_backend(backend) if dist.is_initialized() and dist.get_rank() != 0: return False reader = io_backend.get_reader(path) if len(reader.meta_list) == 1: # already global warnings.warn(f'Checkpoint at "{path}" is already global, nothing to do.') return False dist_meta_list, param_count, param_to_os, paired_os = reader.load_meta() writer = io_backend.get_writer(output_path, overwrite=overwrite) writer.save_others(reader.load_others()) max_shard_size = int(max_shard_size_gb * 1024**3) _convert_shards(ModelCheckpointMerger(max_shard_size, writer.save_model, param_count), reader.load_models(), dist_meta_list) _convert_shards( OptimizerCheckpointMerger(max_shard_size, writer.save_optimizer, param_count, param_to_os, paired_os), reader.load_optimizers(), dist_meta_list) meta_checkpoint = {'dist_meta': None, 'params': list(param_count.keys())} if param_to_os is not None: meta_checkpoint['param_to_os'] = param_to_os meta_checkpoint['paired_os'] = paired_os writer.save_meta(meta_checkpoint) return True def redist(path: str, output_path: str, redist_meta: RedistMeta, dist_metas: List[Dict[str, ParamDistMeta]], max_shard_size_gb: float = 0.0, overwrite: bool = False, backend: str = 'disk') -> bool: io_backend = get_backend(backend) if dist.is_initialized() and dist.get_rank() != 0: return False nprocs = len(dist_metas) reader = io_backend.get_reader(path) dist_meta_list, param_count, param_to_os, paired_os = reader.load_meta() do_redist: bool = False if len(dist_meta_list) == nprocs: for a, b in zip(dist_metas, dist_meta_list): if a != b: do_redist = True break else: do_redist = True if not do_redist: warnings.warn(f'Checkpoint at "{path}" is not required to redist, nothing to do.') return False writers = [io_backend.get_writer(output_path, overwrite, rank, nprocs) for rank in range(nprocs)] writers[0].save_others(reader.load_others()) max_shard_size = int(max_shard_size_gb * 1024**3) _convert_shards( ModelCheckpointRedistor(max_shard_size, [writer.save_model for writer in writers], param_count, redist_meta), reader.load_models(), dist_meta_list) _convert_shards( OptimizerCheckpointRedistor(max_shard_size, [writer.save_optimizer for writer in writers], param_count, param_to_os, paired_os, redist_meta), reader.load_optimizers(), dist_meta_list) for writer, dist_meta in zip(writers, dist_metas): meta_checkpoint = {'dist_meta': dist_meta, 'params': list(param_count.keys())} if param_to_os is not None: meta_checkpoint['param_to_os'] = param_to_os meta_checkpoint['paired_os'] = paired_os writer.save_meta(meta_checkpoint) return True def _convert_shards(convertor: CheckpointConvertor, shard_generator: Generator[dict, None, None], dist_meta_list: List[Optional[Dict[str, ParamDistMeta]]]) -> None: for shard_dict in shard_generator: convertor.append(shard_dict, dist_meta_list) convertor.complete() def load(path: str, model: Module, optimizer: Optional[Optimizer] = None, redist_meta: Optional[RedistMeta] = None, dist_metas: Optional[List[Dict[str, ParamDistMeta]]] = None, max_shard_size_gb: float = 0.0, backend: str = 'disk') -> dict: is_global: bool = not dist.is_initialized() or dist.get_world_size() == 1 rank: int = dist.get_rank() if dist.is_initialized() else 0 is_main_process: bool = rank == 0 # validate args if redist_meta is None or dist_metas is None: assert is_global io_backend = get_backend(backend) read_path: str = path if is_main_process: # pre-process checkpoints temp_path = io_backend.get_temp(path) if is_global: wrote = merge(path, temp_path, max_shard_size_gb, backend=backend) else: wrote = redist(path, temp_path, redist_meta, dist_metas, max_shard_size_gb, backend=backend) if wrote: read_path = temp_path if not is_global: bcast_list = [read_path] if is_main_process else [None] dist.broadcast_object_list(bcast_list) read_path = bcast_list[0] reader = io_backend.get_reader(read_path) # load model for shard in reader.load_model(rank): model.load_state_dict(shard, strict=False) if optimizer is not None: for shard in reader.load_optimizer(rank): # optimizer.load_state_dict(shard) optimizer_load_state_dict(optimizer, shard) others_dict = reader.load_others() if not is_global: dist.barrier() # clean up temp if is_main_process: io_backend.clean_temp() return others_dict
from .io import load, merge, redist, save from .meta import (ParamDistMeta, ParamRedistMeta, PipelineRedistMeta, RankRedistMeta, RedistMeta)
import os from abc import ABC, abstractmethod from collections import Counter from typing import Dict, Generator, List, Optional, Tuple import torch from .constant import GLOBAL_META_FILE_NAME, OTHER_CKPT_FILE_NAME from .meta import ParamDistMeta from .utils import is_duplicated_list class CheckpointReader(ABC): def __init__(self, base_name: str) -> None: super().__init__() self.base_name = base_name self.meta_list = [] @abstractmethod def read(self, name: str) -> dict: pass @abstractmethod def load_meta( self) -> Tuple[List[Optional[Dict[str, ParamDistMeta]]], Dict[str, int], Optional[dict], Optional[dict]]: pass @abstractmethod def load_model(self, rank: int) -> Generator[dict, None, None]: pass @abstractmethod def load_models(self) -> Generator[Dict[int, dict], None, None]: pass @abstractmethod def load_optimizer(self, rank: int) -> Generator[dict, None, None]: pass @abstractmethod def load_optimizers(self) -> Generator[Dict[int, dict], None, None]: pass @abstractmethod def load_others(self) -> dict: pass class DiskCheckpointReader(CheckpointReader): def __init__(self, base_name: str) -> None: super().__init__(base_name) assert os.path.isdir(base_name), f'"{base_name}" is not a directory' global_meta = self.read(GLOBAL_META_FILE_NAME) for meta_file_name in global_meta['meta']: meta = self.read(meta_file_name) if meta.get('dist_meta', None) is None: # only global checkpoint can have empty dist_meta assert len(global_meta['meta']) == 1 self.meta_list.append(meta) def read(self, name: str) -> dict: return torch.load(os.path.join(self.base_name, name)) def load_meta( self) -> Tuple[List[Optional[Dict[str, ParamDistMeta]]], Dict[str, int], Optional[dict], Optional[dict]]: meta_infos = [(meta.get('dist_meta', None), meta['params'], meta.get('param_to_os', None), meta.get('paired_os', None)) for meta in self.meta_list] dist_meta_list, params_list, param_to_os_list, paired_os_list = zip(*meta_infos) # reduce param_count param_count = Counter(p for params in params_list for p in params) # validate param_to_os assert is_duplicated_list(param_to_os_list) assert is_duplicated_list(paired_os_list) return list(dist_meta_list), param_count, param_to_os_list[0], paired_os_list[0] def _load_shard(self, shard_type: str, rank: int) -> Generator[dict, None, None]: meta = self.meta_list[rank] checkpoint_names = meta.get(shard_type, []) for name in checkpoint_names: yield self.read(name) def load_model(self, rank: int) -> Generator[dict, None, None]: return self._load_shard('model', rank) def load_models(self) -> Generator[Dict[int, dict], None, None]: indices = [0] * len(self.meta_list) while True: shards = {} for i, meta in enumerate(self.meta_list): model_checkpoint_names = meta.get('model', []) if indices[i] < len(model_checkpoint_names): shards[i] = self.read(model_checkpoint_names[indices[i]]) indices[i] += 1 if len(shards) > 0: yield shards else: break def load_optimizer(self, rank: int) -> Generator[dict, None, None]: param_groups = None for shard in self._load_shard('optimizer', rank): if param_groups is None: param_groups = shard['param_groups'] else: shard['param_groups'] = param_groups yield shard def load_optimizers(self) -> Generator[Dict[int, dict], None, None]: indices = [0] * len(self.meta_list) param_groups = [] while True: shards = {} for i, meta in enumerate(self.meta_list): optimizer_checkpoint_names = meta.get('optimizer', []) if indices[i] < len(optimizer_checkpoint_names): shards[i] = self.read(optimizer_checkpoint_names[indices[i]]) if indices[i] == 0: param_groups.append(shards[i]['param_groups']) else: shards[i]['param_groups'] = param_groups[i] indices[i] += 1 if len(shards) > 0: yield shards else: break def load_others(self) -> dict: return self.read(OTHER_CKPT_FILE_NAME)
import torch from numpy import prod from torch import Tensor from typing import List, Optional, Tuple from collections import defaultdict from .meta import ParamDistMeta, ParamRedistMeta def unflatten_zero_param(tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> Tensor: assert len(tensors) > 0 and len(dist_metas) > 0 and len(tensors) == len(dist_metas) for dist_meta in dist_metas[1:]: assert dist_meta.zero_meta == dist_metas[0].zero_meta, 'Expect all params have the same zero meta.' if not dist_metas[0].used_zero: # tensors are replicate return tensors[0] numel = dist_metas[0].zero_numel orig_shape = dist_metas[0].zero_orig_shape tensors = [t[1] for t in sorted(zip(dist_metas, tensors), key=lambda tp: tp[0].dp_rank)] assert numel == sum(t.numel() for t in tensors), 'Expect numel of all params is equal to zero_numel.' return torch.cat(tensors).reshape(orig_shape) def gather_tp_param(tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> Tensor: assert len(tensors) > 0 and len(dist_metas) > 0 and len(tensors) == len(dist_metas) for dist_meta in dist_metas[1:]: assert dist_meta.tp_meta == dist_metas[0].tp_meta, 'Expect all params have the same tp meta.' for t in tensors[1:]: assert t.shape == tensors[0].shape, 'Expect all params have the same shape.' if not dist_metas[0].used_tp: # tensors are replicate return tensors[0] total_parts = prod(dist_meta.tp_num_parts) assert dist_meta.tp_world_size == total_parts, \ f'Expect prod(tp_num_parts) == tp_world_size, got {total_parts} and {dist_meta.tp_world_size}.' shard_info = sorted(zip(dist_meta.tp_shard_dims, dist_meta.tp_num_parts), key=lambda t: t[0], reverse=True) for dim, num_parts in shard_info: buffer = [] for start in range(0, len(tensors), num_parts): buffer.append(torch.cat(tensors[start:start + num_parts], dim)) tensors = buffer assert len(tensors) == 1 return tensors[0] def validate_parallel_info(dist_metas: List[ParamDistMeta]) -> None: assert len(dist_metas) > 0 # check world size for dist_meta in dist_metas[1:]: assert dist_meta.dp_world_size == dist_metas[ 0].dp_world_size, 'Expect all dist meta have the same dp_world_size' assert dist_meta.tp_world_size == dist_metas[ 0].tp_world_size, 'Expect all dist meta have the same tp_world_size' def deduplicate_params(tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> Tuple[List[Tensor], List[ParamDistMeta]]: unique_dist_meta = [] unique_idx = [] for i, dist_meta in enumerate(dist_metas): if dist_meta not in unique_dist_meta: unique_dist_meta.append(dist_meta) unique_idx.append(i) return [tensors[i] for i in unique_idx], [dist_metas[i] for i in unique_idx] def merge_param(tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> Tensor: assert len(tensors) > 0 and len(dist_metas) > 0 and len(tensors) == len(dist_metas) # validate parallel info validate_parallel_info(dist_metas) tensors, dist_metas = deduplicate_params(tensors, dist_metas) unflattened_tensors = [] # group zero params by tp rank tensor_dict = defaultdict(list) dist_meta_dict = defaultdict(list) for t, dist_meta in zip(tensors, dist_metas): tensor_dict[dist_meta.tp_rank].append(t) dist_meta_dict[dist_meta.tp_rank].append(dist_meta) assert len(tensor_dict ) == dist_metas[0].tp_world_size, f'Expect {dist_metas[0].tp_world_size} ranks, got {len(tensor_dict)}' for tp_rank in tensor_dict.keys(): unflattened_tensors.append(unflatten_zero_param(tensor_dict[tp_rank], dist_meta_dict[tp_rank])) return gather_tp_param(unflattened_tensors, [dist_meta_list[0] for dist_meta_list in dist_meta_dict.values()]) def split_tp_param(tensor: Tensor, redist_meta: ParamRedistMeta) -> List[Tensor]: if not redist_meta.used_tp: assert redist_meta.tp_world_size == 1, 'Expect tp_world_size == 1, when no tp meta provided.' return [tensor] total_parts = prod(redist_meta.tp_num_parts) assert redist_meta.tp_world_size == total_parts, f'Expect prod(tp_num_parts) == tp_world_size, got {total_parts} and {redist_meta.tp_world_size}.' shard_info = sorted(zip(redist_meta.tp_shard_dims, redist_meta.tp_num_parts), key=lambda t: t[0]) tensors = [tensor] for dim, num_parts in shard_info: buffer = [] for t in tensors: assert t.size(dim) % num_parts == 0, \ f'Expect dim{dim} of tensor({tensor.shape}) is divisible by {num_parts}.' chunks = [chunk.contiguous() for chunk in t.chunk(num_parts, dim)] buffer.extend(chunks) tensors = buffer assert len(tensors) == redist_meta.tp_world_size return tensors def flatten_zero_param(tensor: Tensor, redist_meta: ParamRedistMeta) -> List[Tensor]: if not redist_meta.used_zero: return [tensor] * redist_meta.dp_world_size tensors: List[Optional[Tensor]] = [ torch.empty(0, dtype=tensor.dtype, device=tensor.device) for _ in range(redist_meta.zero_start_dp_rank) ] offsets = redist_meta.zero_offsets + [tensor.numel()] for i, offset in enumerate(offsets[:-1]): end = offsets[i + 1] tensors.append(tensor.view(-1)[offset:end]) if len(tensors) < redist_meta.dp_world_size: tensors.extend([ torch.empty(0, dtype=tensor.dtype, device=tensor.device) for _ in range(redist_meta.dp_world_size - len(tensors)) ]) assert len(tensors) == redist_meta.dp_world_size return tensors def unmerge_param(tensor: Tensor, redist_meta: ParamRedistMeta) -> List[List[Tensor]]: tensors = split_tp_param(tensor, redist_meta) tensors = [flatten_zero_param(t, redist_meta) for t in tensors] return tensors
import warnings from copy import deepcopy from itertools import chain from typing import Any, Callable, Dict, List, Optional, Tuple from torch import Tensor from torch.nn import Module from torch.nn.parameter import Parameter from torch.optim import Optimizer from .meta import ParamDistMeta def run_if_not_none(fn: Callable[[Any], Any], arg: Any) -> Any: if arg is not None: return fn(arg) def get_param_to_os(model: Module, optimizer: Optimizer) -> Dict[str, int]: # ensure all params in optimizer are in model state dict params_set = set(id(p) for p in model.parameters()) for group in optimizer.param_groups: for p in group['params']: assert id(p) in params_set param_mappings = {} start_index = 0 def get_group_mapping(group): nonlocal start_index param_mappings.update( {id(p): i for i, p in enumerate(group['params'], start_index) if id(p) not in param_mappings}) start_index += len(group['params']) for g in optimizer.param_groups: get_group_mapping(g) return {k: param_mappings[id(p)] for k, p in model.named_parameters()} def compute_optimizer_state_size(state: Dict[str, Any]) -> int: size = 0 for v in state.values(): if isinstance(v, Tensor): size += v.numel() * v.element_size() return size class ModelCheckpointSharder: def __init__(self, max_shard_size: int) -> None: self.max_shard_size = max_shard_size self.buffer: Dict[str, Tensor] = {} self.buffer_size: int = 0 def append(self, key: str, tensor: Tensor) -> Optional[dict]: retval = None if self.max_shard_size > 0 and self.buffer_size >= self.max_shard_size: retval = self.buffer self.buffer = {} self.buffer_size = 0 self.buffer[key] = tensor self.buffer_size += tensor.numel() * tensor.element_size() return retval def extend(self, state_dict: Dict[str, Tensor]) -> List[dict]: shards = [] for key, tensor in state_dict.items(): shard = self.append(key, tensor) run_if_not_none(shards.append, shard) return shards def complete(self) -> Optional[dict]: return self.buffer if len(self.buffer) > 0 else None class OptimizerCheckpointSharder: def __init__(self, max_shard_size: int, param_groups: dict) -> None: self.max_shard_size = max_shard_size self.buffer: Dict[str, dict] = {'state': {}, 'param_groups': param_groups} self.buffer_size: int = 0 self.returned_first: bool = False def append(self, key: int, state: dict) -> Optional[dict]: retval = None if self.max_shard_size > 0 and self.buffer_size >= self.max_shard_size: retval = self.buffer self.buffer = {'state': {}} self.buffer_size = 0 self.buffer['state'][key] = state self.buffer_size += compute_optimizer_state_size(state) return retval def extend(self, state_dict: Dict[str, dict]) -> List[dict]: shards = [] for key, state in state_dict['state'].items(): shard = self.append(key, state) run_if_not_none(shards.append, shard) return shards def complete(self) -> Optional[dict]: return self.buffer if len(self.buffer['state']) > 0 else None def shard_checkpoint(max_shard_size: int, model_state_dict: Dict[str, Tensor], optimizer_state_dict: Optional[dict] = None, param_to_os: Optional[dict] = None) -> Tuple[List[dict], List[dict]]: has_optimizer: bool = False if optimizer_state_dict is not None: assert param_to_os is not None os_to_param = {v: k for k, v in param_to_os.items()} for os_key in optimizer_state_dict['state'].keys(): assert os_key in os_to_param assert os_to_param[os_key] in model_state_dict has_optimizer = True model_sharder = ModelCheckpointSharder(max_shard_size) model_shards = model_sharder.extend(model_state_dict) run_if_not_none(model_shards.append, model_sharder.complete()) if not has_optimizer: return model_shards, [] optimizer_sharder = OptimizerCheckpointSharder(max_shard_size, optimizer_state_dict['param_groups']) optimizer_shards = optimizer_sharder.extend(optimizer_state_dict) run_if_not_none(optimizer_shards.append, optimizer_sharder.complete()) return model_shards, optimizer_shards def get_paired_os(model_state_dict: Dict[str, Tensor], optimizer_state_dict: dict, param_to_os: Dict[str, int]) -> dict: os_to_param = {v: k for k, v in param_to_os.items()} paired_os = {} for idx, state in optimizer_state_dict['state'].items(): paired_os[idx] = {} p = model_state_dict[os_to_param[idx]] for k, v in state.items(): if isinstance(v, Tensor) and v.shape == p.shape: paired_os[idx][k] = True else: paired_os[idx][k] = False return paired_os def build_checkpoints(max_size: int, model: Module, optimizer: Optional[Optimizer] = None, param_to_os: Optional[Dict[str, int]] = None, dist_meta: Optional[Dict[str, ParamDistMeta]] = None, eliminate_replica: bool = False) -> Tuple[List[dict], List[dict], dict]: save_global = dist_meta is None model_state_dict = model.state_dict() optimizer_state_dict = optimizer.state_dict() if optimizer else None meta = {'dist_meta': dist_meta} if optimizer: param_to_os = param_to_os or get_param_to_os(model, optimizer) paired_os = get_paired_os(model_state_dict, optimizer_state_dict, param_to_os) meta['param_to_os'] = param_to_os meta['paired_os'] = paired_os if not save_global and eliminate_replica: # filter dp replicated params model_state_dict = { k: v for k, v in model_state_dict.items() if dist_meta[k].used_zero or dist_meta[k].dp_rank == 0 } if optimizer: optimizer_state_dict['state'] = { param_to_os[k]: optimizer_state_dict['state'][param_to_os[k]] for k in model_state_dict.keys() if dist_meta[k].used_zero or dist_meta[k].dp_rank == 0 } meta['params'] = list(model_state_dict.keys()) if len(model_state_dict) == 0: warnings.warn('model state dict is empty, checkpoint is not saved') return [], [], meta model_checkpoints, optimizer_checkpoints = shard_checkpoint(max_size, model_state_dict, optimizer_state_dict, param_to_os) return model_checkpoints, optimizer_checkpoints, meta def is_duplicated_list(list_: List[Any]) -> bool: if len(list_) == 0: return True elem = list_[0] for x in list_[1:]: if x != elem: return False return True def copy_optimizer_state(src_state: dict, dest_state: dict) -> None: for k, v in src_state.items(): if k in dest_state: old_v = dest_state[k] if isinstance(old_v, Tensor): old_v.copy_(v) else: dest_state[k] = v def optimizer_load_state_dict(optimizer: Optimizer, state_dict: dict, strict: bool = False) -> None: assert optimizer.state_dict()['param_groups'] == state_dict['param_groups'] state_dict = deepcopy(state_dict) groups = optimizer.param_groups saved_groups = state_dict['param_groups'] idx_to_p: Dict[str, Parameter] = { old_id: p for old_id, p in zip(chain.from_iterable((g['params'] for g in saved_groups )), chain.from_iterable((g['params'] for g in groups))) } missing_keys = list(set(idx_to_p.keys()) - set(state_dict['state'].keys())) unexpected_keys = [] error_msgs = [] for idx, state in state_dict['state'].items(): if idx in idx_to_p: old_state = optimizer.state[idx_to_p[idx]] copy_optimizer_state(state, old_state) else: unexpected_keys.append(idx) 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(optimizer.__class__.__name__, "\n\t".join(error_msgs)))
from abc import ABC, abstractmethod from collections import defaultdict from typing import Any, Callable, Dict, List, Optional from torch import Tensor from .distributed import merge_param, unmerge_param from .meta import ParamDistMeta, RedistMeta from .utils import (ModelCheckpointSharder, OptimizerCheckpointSharder, run_if_not_none) class CheckpointConvertor(ABC): @abstractmethod def append(self, shard_dict: Dict[int, dict], dist_meta_list: List[Optional[Dict[str, ParamDistMeta]]]) -> None: pass @abstractmethod def complete(self) -> None: pass class ModelCheckpointConvertor(CheckpointConvertor): def __init__(self, param_count: Dict[str, int]) -> None: super().__init__() self.param_count = param_count self.buffer: Dict[str, Dict[int, Tensor]] = defaultdict(dict) @abstractmethod def convert_tensors(self, key: str, tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> None: pass def append(self, shard_dict: Dict[int, dict], dist_meta_list: List[Optional[Dict[str, ParamDistMeta]]]) -> None: for rank, state_dict in shard_dict.items(): for k, tensor in state_dict.items(): self.buffer[k][rank] = tensor converted_keys = set() for k, rank_dict in self.buffer.items(): if len(rank_dict) == self.param_count[k]: tensors = [] dist_metas = [] for rank, tensor in rank_dict.items(): tensors.append(tensor) if dist_meta_list[rank] is not None: dist_metas.append(dist_meta_list[rank][k]) self.convert_tensors(k, tensors, dist_metas) converted_keys.add(k) for k in converted_keys: del self.buffer[k] def complete(self) -> None: assert len(self.buffer) == 0 class ModelCheckpointMerger(ModelCheckpointConvertor): def __init__(self, max_shard_size: int, save_fn: Callable[[dict], Any], param_count: Dict[str, int]) -> None: super().__init__(param_count) self.sharder = ModelCheckpointSharder(max_shard_size) self.save_fn = save_fn def convert_tensors(self, key: str, tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> None: assert len(dist_metas) == len(tensors) tensor = merge_param(tensors, dist_metas) shard = self.sharder.append(key, tensor) run_if_not_none(self.save_fn, shard) def complete(self) -> None: super().complete() run_if_not_none(self.save_fn, self.sharder.complete()) class ModelCheckpointRedistor(ModelCheckpointConvertor): def __init__(self, max_shard_size: int, save_fns: List[Callable[[dict], Any]], param_count: Dict[str, int], redist_meta: RedistMeta) -> None: super().__init__(param_count) self.save_fns = save_fns self.redist_meta = redist_meta nprocs = len(save_fns) self.sharders = [ModelCheckpointSharder(max_shard_size) for _ in range(nprocs)] self.rank_map = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) for k, rank_meta in redist_meta.rank_meta.items(): for rank, rank_info in rank_meta.items(): self.rank_map[k][rank_info.tp_rank][rank_info.dp_rank].append(rank) def convert_tensors(self, key: str, tensors: List[Tensor], dist_metas: List[ParamDistMeta]) -> None: if len(dist_metas) == 0: # already global tensor = tensors[0] else: assert len(dist_metas) == len(tensors) tensor = merge_param(tensors, dist_metas) for tp_rank, tensor_list in enumerate(unmerge_param(tensor, self.redist_meta.param_meta[key])): for dp_rank, t in enumerate(tensor_list): for rank in self.rank_map[key][tp_rank][dp_rank]: shard = self.sharders[rank].append(key, t) run_if_not_none(self.save_fns[rank], shard) def complete(self) -> None: super().complete() for rank, save_fn in enumerate(self.save_fns): run_if_not_none(save_fn, self.sharders[rank].complete()) class OptimizerCheckpointConvertor(CheckpointConvertor): def __init__(self, param_count: Dict[str, int], param_to_os: Optional[Dict[str, int]], paired_os: Optional[Dict[int, dict]]) -> None: super().__init__() self.param_count = param_count self.param_to_os = param_to_os self.paired_os = paired_os self.buffer: Dict[int, Dict[int, dict]] = defaultdict(dict) self.os_to_param = {v: k for k, v in param_to_os.items()} @abstractmethod def setup(self, param_groups: dict) -> None: pass @abstractmethod def convert_states(self, idx: int, states: List[dict], dist_metas: List[ParamDistMeta]) -> None: pass def append(self, shard_dict: Dict[int, dict], dist_meta_list: List[Optional[Dict[str, ParamDistMeta]]]) -> None: for rank, state_dict in shard_dict.items(): self.setup(state_dict['param_groups']) for idx, state in state_dict['state'].items(): self.buffer[idx][rank] = state converted_indices = set() for idx, rank_dict in self.buffer.items(): if len(rank_dict) == self.param_count[self.os_to_param[idx]]: states = [] dist_metas = [] for rank, state in rank_dict.items(): states.append(state) if dist_meta_list[rank] is not None: dist_metas.append(dist_meta_list[rank][self.os_to_param[idx]]) self.convert_states(idx, states, dist_metas) converted_indices.add(idx) for idx in converted_indices: del self.buffer[idx] def complete(self) -> None: assert len(self.buffer) == 0 class OptimizerCheckpointMerger(OptimizerCheckpointConvertor): def __init__(self, max_shard_size: int, save_fn: Callable[[dict], Any], param_count: Dict[str, int], param_to_os: Optional[Dict[str, int]], paired_os: Optional[Dict[int, dict]]) -> None: super().__init__(param_count, param_to_os, paired_os) self.max_shard_size = max_shard_size self.save_fn = save_fn self.sharder = None def setup(self, param_groups: dict) -> None: if self.sharder is None: self.sharder = OptimizerCheckpointSharder(self.max_shard_size, param_groups) def convert_states(self, idx: int, states: List[dict], dist_metas: List[ParamDistMeta]) -> None: assert len(dist_metas) == len(states) new_state = {} for state_key, state_tensor in states[0].items(): if self.paired_os[idx][state_key]: new_state[state_key] = merge_param([state[state_key] for state in states], dist_metas) else: new_state[state_key] = state_tensor shard = self.sharder.append(idx, new_state) run_if_not_none(self.save_fn, shard) def complete(self) -> None: super().complete() run_if_not_none(self.save_fn, self.sharder.complete()) class OptimizerCheckpointRedistor(OptimizerCheckpointConvertor): def __init__(self, max_shard_size: int, save_fns: List[Callable[[dict], Any]], param_count: Dict[str, int], param_to_os: Optional[Dict[str, int]], paired_os: Optional[Dict[int, dict]], redist_meta: RedistMeta) -> None: super().__init__(param_count, param_to_os, paired_os) self.max_shard_size = max_shard_size self.save_fns = save_fns self.redist_meta = redist_meta self.sharders: List[OptimizerCheckpointSharder] = [] self.rank_map = defaultdict(lambda: defaultdict(lambda: defaultdict(list))) for k, rank_meta in redist_meta.rank_meta.items(): for rank, rank_info in rank_meta.items(): self.rank_map[k][rank_info.tp_rank][rank_info.dp_rank].append(rank) def setup(self, param_groups: dict) -> None: if len(self.sharders) == 0: nprocs = len(self.save_fns) for _ in range(nprocs): self.sharders.append(OptimizerCheckpointSharder(self.max_shard_size, param_groups)) def convert_states(self, idx: int, states: List[dict], dist_metas: List[ParamDistMeta]) -> None: need_merge: bool = True if len(dist_metas) == 0: need_merge = False else: assert len(dist_metas) == len(states) new_states = [{} for _ in range(len(self.save_fns))] for state_key, state_tensor in states[0].items(): if self.paired_os[idx][state_key]: if need_merge: tensor = merge_param([state[state_key] for state in states], dist_metas) else: tensor = state_tensor for tp_rank, tensor_list in enumerate( unmerge_param(tensor, self.redist_meta.param_meta[self.os_to_param[idx]])): for dp_rank, t in enumerate(tensor_list): for rank in self.rank_map[self.os_to_param[idx]][tp_rank][dp_rank]: new_states[rank][state_key] = t else: for new_state in new_states: new_state[state_key] = state_tensor for rank, new_state in enumerate(new_states): shard = self.sharders[rank].append(idx, new_state) run_if_not_none(self.save_fns[rank], shard) def complete(self) -> None: super().complete() for rank, save_fn in enumerate(self.save_fns): run_if_not_none(save_fn, self.sharders[rank].complete())
from abc import ABC, abstractmethod from typing import Optional from .constant import MODEL_CKPT_FILE_NAME, OPTIM_CKPT_FILE_NAME, META_CKPT_FILE_NAME, OTHER_CKPT_FILE_NAME, GLOBAL_META_FILE_NAME import torch import os class CheckpointWriter(ABC): def __init__(self, base_name: str, overwrite: bool = False, rank: int = 0, world_size: int = 1) -> None: super().__init__() self.base_name = base_name self.overwrite = overwrite self.rank = rank self.world_size = world_size self.is_distributed = world_size > 1 self.is_main_process = rank == 0 @abstractmethod def write(self, name: str, state_dict: dict) -> None: pass @abstractmethod def save_model(self, model_checkpoint: dict) -> None: pass @abstractmethod def save_optimizer(self, optimizer_checkpoint: dict) -> None: pass @abstractmethod def save_meta(self, meta_checkpoint: dict) -> None: pass @abstractmethod def save_others(self, kwargs: dict) -> None: pass class DiskCheckpointWriter(CheckpointWriter): def __init__(self, base_name: str, overwrite: bool = False, rank: int = 0, world_size: int = 1) -> None: super().__init__(base_name, overwrite, rank, world_size) if not os.path.exists(base_name): os.makedirs(base_name) assert os.path.isdir(base_name), f'"{base_name}" is not a directory' self.model_checkpoint_names = [] self.optimizer_checkpoint_names = [] self.is_meta_saved: bool = False self._save_global_meta() def write(self, name: str, state_dict: dict) -> None: path = os.path.join(self.base_name, name) if os.path.exists(path) and not self.overwrite: raise RuntimeError(f'Save error: Checkpoint "{path}" exists. (overwrite = False)') torch.save(state_dict, path) def _save_global_meta(self) -> None: if self.is_main_process: global_meta = {'meta': []} if self.is_distributed: for i in range(self.world_size): global_meta['meta'].append(META_CKPT_FILE_NAME.replace('.bin', f'-rank{i}.bin')) else: global_meta['meta'].append(META_CKPT_FILE_NAME) self.write(GLOBAL_META_FILE_NAME, global_meta) def _get_checkpoint_name(self, base_name: str, shard_idx: Optional[int] = None) -> str: checkpoint_name = base_name if self.is_distributed: checkpoint_name = checkpoint_name.replace('.bin', f'-rank{self.rank}.bin') if shard_idx is not None: checkpoint_name = checkpoint_name.replace('.bin', f'-shard{shard_idx}.bin') return checkpoint_name def save_model(self, model_checkpoint: dict) -> None: assert not self.is_meta_saved, 'Cannot save model after saving meta' name = self._get_checkpoint_name(MODEL_CKPT_FILE_NAME, len(self.model_checkpoint_names)) self.write(name, model_checkpoint) self.model_checkpoint_names.append(name) def save_optimizer(self, optimizer_checkpoint: dict) -> None: assert not self.is_meta_saved, 'Cannot save optimizer after saving meta' name = self._get_checkpoint_name(OPTIM_CKPT_FILE_NAME, len(self.optimizer_checkpoint_names)) self.write(name, optimizer_checkpoint) self.optimizer_checkpoint_names.append(name) def save_meta(self, meta_checkpoint: dict) -> None: if len(self.model_checkpoint_names) > 0: meta_checkpoint['model'] = self.model_checkpoint_names if len(self.optimizer_checkpoint_names) > 0: meta_checkpoint['optimizer'] = self.optimizer_checkpoint_names self.write(self._get_checkpoint_name(META_CKPT_FILE_NAME), meta_checkpoint) self.is_meta_saved = True def save_others(self, kwargs: dict) -> None: if self.is_main_process: self.write(OTHER_CKPT_FILE_NAME, kwargs)
import re GLOBAL_META_FILE_NAME = 'global_meta.bin' MODEL_CKPT_FILE_NAME = 'model.bin' OPTIM_CKPT_FILE_NAME = 'optim.bin' META_CKPT_FILE_NAME = 'meta.bin' OTHER_CKPT_FILE_NAME = 'other.bin' CKPT_PAT = re.compile(r'global_meta|model|optim|meta|other')
from dataclasses import dataclass from typing import List, Optional, Set, Dict @dataclass class ParamDistMeta: # parallel info dp_rank: int dp_world_size: int tp_rank: int tp_world_size: int # tp info tp_shard_dims: Optional[List[int]] = None tp_num_parts: Optional[List[int]] = None # zero info zero_numel: Optional[int] = None zero_orig_shape: Optional[List[int]] = None @property def used_tp(self) -> bool: return self.tp_shard_dims is not None and self.tp_num_parts is not None @property def used_zero(self) -> bool: return self.zero_numel is not None and self.zero_orig_shape is not None @property def parallel_meta(self) -> tuple: return self.dp_rank, self.dp_world_size, self.tp_rank, self.tp_world_size @property def tp_meta(self) -> tuple: return self.tp_shard_dims, self.tp_num_parts @property def zero_meta(self) -> tuple: return self.zero_numel, self.zero_orig_shape @staticmethod def from_dict(d: dict) -> 'ParamDistMeta': return ParamDistMeta(**d) @dataclass class ParamRedistMeta: # parallel info dp_world_size: int tp_world_size: int # tp info tp_shard_dims: Optional[List[int]] = None tp_num_parts: Optional[List[int]] = None # zero info zero_start_dp_rank: Optional[int] = None zero_offsets: Optional[List[int]] = None @property def used_tp(self) -> bool: return self.tp_shard_dims is not None and self.tp_num_parts is not None @property def used_zero(self) -> bool: return self.zero_start_dp_rank is not None and self.zero_offsets is not None @dataclass class RankRedistMeta: dp_rank: int tp_rank: int pp_rank: int @dataclass class PipelineRedistMeta: params: Set[str] @dataclass class RedistMeta: rank_meta: Dict[str, Dict[int, RankRedistMeta]] pipeline_meta: List[PipelineRedistMeta] param_meta: Dict[str, ParamRedistMeta]
import copy from typing import Dict, List, Tuple from torch.fx.node import Node from .estimate_memory import EstimateMemory from .reorder_graph import ReorderGraph from .select_chunk import SelectChunk from .trace_flow import TraceFlow from .trace_indice import TraceIndice from .utils import NodeMgr, get_logger, get_node_shape, is_non_compute_node, is_non_compute_node_except_placeholder class SearchChunk(object): """ This is the core class for AutoChunk. It defines the framework of the strategy of AutoChunk. Chunks will be selected one by one utill search stops. The chunk search is as follows: 1. find the peak memory node 2. find the max chunk region according to the peak memory node 3. find all possible chunk regions in the max chunk region 4. find the best chunk region for current status 5. goto 1 Attributes: gm: graph model print_mem (bool): print estimated memory trace_index: trace the flow of every dim of every node to find all free dims trace_flow: determine the region chunk strategy reorder_graph: reorder nodes to improve chunk efficiency estimate_memory: estimate memory with chunk select_chunk: select the best chunk region Args: gm: graph model max_memory (int): max memory in MB print_mem (bool): print estimated memory """ def __init__(self, gm, max_memory=None, print_mem=False, print_progress=False) -> None: self.print_mem = print_mem self.print_progress = print_progress self.node_mgr = NodeMgr(gm) self.trace_indice = TraceIndice(self.node_mgr) self.estimate_memory = EstimateMemory(self.node_mgr) self._init_trace() self.trace_flow = TraceFlow(self.trace_indice, self.node_mgr) self.reorder_graph = ReorderGraph(self.trace_indice, self.node_mgr) self.select_chunk = SelectChunk( self.trace_indice, self.estimate_memory, self.reorder_graph, self.node_mgr, max_memory=max_memory, ) def _init_trace(self) -> None: """ find the max trace range for every node reduce the computation complexity of trace_indice """ # find all max ranges active_nodes = self.estimate_memory.get_active_nodes(self.node_mgr.get_node_list()) cur_node_idx = len(self._get_free_var_idx()) max_chunk_region_list = [] while True: max_chunk_region = self._search_max_chunk_region(active_nodes, cur_node_idx) cur_node_idx = max_chunk_region[1] + 1 if cur_node_idx >= len(active_nodes) - 1: break max_chunk_region_list.append(max_chunk_region) # nothing to limit for the first range max_chunk_region_list = max_chunk_region_list[1:] max_chunk_region_list[0] = (0, max_chunk_region_list[0][1]) # set trace range and do the trace if self.print_progress: get_logger().info("AutoChunk start tracing indice") self.trace_indice.set_trace_range(max_chunk_region_list, active_nodes) self.trace_indice.trace_indice() def _find_peak_node(self, mem_peak: List) -> int: max_value = max(mem_peak) max_idx = mem_peak.index(max_value) return max_idx def _get_free_var_idx(self) -> List: """ Get free var index Returns: free_var_idx (List): all indexs of free vars """ free_var_idx = [] for idx, n in enumerate(self.node_mgr.get_node_list()): if n.op == "placeholder" and get_node_shape(n) is not None: free_var_idx.append(idx) return free_var_idx def _search_max_chunk_region(self, active_node: List, peak_node_idx: int, chunk_regions: List = None) -> Tuple: """ Search max chunk region according to peak memory node Chunk region starts extending from the peak node, stops where free var num is min Args: active_node (List): active node status for every node peak_node_idx (int): peak memory node idx chunk_regions (List): chunk region infos Returns: chunk_region_start (int) chunk_region_end (int) """ # check if peak node already in chunkinfo if chunk_regions is not None: for i in chunk_regions: if i["region"][0] < peak_node_idx <= i["region"][1]: return None free_vars = self._get_free_var_idx() free_var_num = len(free_vars) active_node_num = [len(i) for i in active_node] min_active_node_num = min(active_node_num[free_var_num:]) threshold = max(free_var_num, min_active_node_num) # normal search # from peak_node to free_var inside_flag = False chunk_region_start = free_var_num for i in range(peak_node_idx, -1, -1): if active_node_num[i] <= threshold: inside_flag = True if inside_flag and active_node_num[i] > threshold: chunk_region_start = i + 1 break # from peak_node to len-2 inside_flag = False chunk_region_end = len(active_node) - 1 for i in range(peak_node_idx, len(active_node)): if active_node_num[i] <= threshold: inside_flag = True if inside_flag and active_node_num[i] > threshold: chunk_region_end = i break # if normal search fails, use approximate search if (chunk_region_end - chunk_region_start) > 250: window_size = 100 # search min for start min_num = 1e3 for i in range(max(peak_node_idx - window_size, 0), peak_node_idx + 1): if active_node_num[i] < min_num: min_num = active_node_num[i] chunk_region_start = i # search min for end min_num = 1e3 for i in range(min(peak_node_idx + window_size, len(active_node_num) - 1), peak_node_idx - 1, -1): if active_node_num[i] < min_num: min_num = active_node_num[i] chunk_region_end = i # avoid chunk regions overlap if chunk_regions is not None: for i in chunk_regions: region = i["region"] if chunk_region_start >= region[0] and chunk_region_end <= region[1]: return None elif (region[0] <= chunk_region_start <= region[1] and chunk_region_end > region[1]): chunk_region_start = region[1] + 1 elif (region[0] <= chunk_region_end <= region[1] and chunk_region_start < region[0]): chunk_region_end = region[0] - 1 return chunk_region_start, chunk_region_end def _find_chunk_info(self, input_trace, output_trace, start_idx, end_idx) -> List: """ Find chunk info for a region. We are given the region start and region end, and need to find out all chunk info for it. We first loop every dim of start node and end node, to see if we can find dim pair, which is linked in a flow and not computed. If found, we then search flow in the whole region to find out all chunk infos. Args: input_trace (List): node's input trace in region output_trace (List): node's output trace in region start_idx (int): region start node index end_idx (int): region end node index Returns: chunk_infos: possible regions found """ start_traces = input_trace[start_idx] if len(start_traces) > 1: # TODO need to be removed return [] end_trace = output_trace[end_idx] end_node = self.node_mgr.get_node_by_idx(end_idx) chunk_infos = [] for end_dim, _ in enumerate(end_trace["indice"]): for start_node, start_trace in start_traces.items(): for start_dim, _ in enumerate(start_trace["indice"]): if not self.trace_flow.check_region_start_end(start_node, start_dim, start_idx, end_node, end_dim, end_idx): continue # flow search chunk_info = self.trace_flow.flow_search(start_idx, start_dim, end_idx, end_dim) if chunk_info is None: continue chunk_infos.append(chunk_info) return chunk_infos def _search_possible_chunk_regions(self, max_chunk_region: Tuple, peak_node: Node) -> List: """ Search every possible region within the max chunk region. Args: max_chunk_region (Tuple) peak_node (Node): peak memory node Returns: possible_chunk_region (List) """ possible_chunk_region = [] output_trace = copy.deepcopy(self.trace_indice.indice_trace_list) input_trace = [] # trace of a node's input nodes for _, n in enumerate(self.node_mgr.get_node_list()): cur_trace = {} for arg in n.args: if type(arg) == type(n) and not is_non_compute_node_except_placeholder(arg): cur_trace[arg] = self.trace_indice._find_trace_from_node(arg) input_trace.append(cur_trace) for start_idx in range(max_chunk_region[0], peak_node + 1): for end_idx in range(peak_node, max_chunk_region[1] + 1): # skip non compute nodes if is_non_compute_node(self.node_mgr.get_node_by_idx(start_idx)) or is_non_compute_node( self.node_mgr.get_node_by_idx(end_idx)): continue # select free dim chunk_info = self._find_chunk_info(input_trace, output_trace, start_idx, end_idx) if len(chunk_info) > 0: possible_chunk_region.extend(chunk_info) return possible_chunk_region def _step_search( self, mem_peak: List[float], active_node: List[List[Node]], chunk_infos: List[Dict], ) -> Dict: """ Find one chunk region The chunk search is as follows: 1. find the peak memory node 2. find the max chunk region according to the peak memory node 3. find all possible chunk regions in the max chunk region 4. find the best chunk region for current status Args: mem_peak (List): peak memory for every node active_node (List[List[Node]]): active node for every node chunk_infos (List[Dict]): all chunk info Returns: best_chunk_region (Dict) """ peak_node = self._find_peak_node(mem_peak) max_chunk_region = self._search_max_chunk_region(active_node, peak_node, chunk_infos) if max_chunk_region == None: return None possible_chunk_regions = self._search_possible_chunk_regions(max_chunk_region, peak_node) best_chunk_region = self.select_chunk._select_best_chunk_region(possible_chunk_regions, chunk_infos, peak_node, max_chunk_region, mem_peak) best_chunk_region = self.reorder_graph.reorder_all(best_chunk_region) return best_chunk_region def search_region(self) -> Dict: """ Search all chunk regions: 1. Estimate current memory 2. Find best chunk for current memory 3. goto 1 Returns: chunk_infos (Dict) """ if self.print_progress: get_logger().info("AutoChunk start searching chunk regions") chunk_infos = [] init_mem_peak, _, active_node = self.estimate_memory.estimate_chunk_inference_mem(self.node_mgr.get_node_list()) mem_peak = init_mem_peak while True: chunk_info = self._step_search(mem_peak, active_node, chunk_infos) if chunk_info is None: break chunk_infos.append(chunk_info) mem_peak, _, active_node = self.estimate_memory.estimate_chunk_inference_mem( self.node_mgr.get_node_list(), chunk_infos) if self.print_progress: get_logger().info("AutoChunk find chunk region %d = (%d, %d)" % (len(chunk_infos), chunk_info["region"][0], chunk_info["region"][1])) if self.print_mem: self.print_mem = False self.estimate_memory.estimate_chunk_inference_mem(self.node_mgr.get_node_list(), chunk_infos, print_mem=True) return chunk_infos
import copy from typing import Any, Callable, Dict, Iterable, List, Tuple import torch from torch.fx.node import Node, map_arg from colossalai.fx.profiler import activation_size, parameter_size from .utils import NodeMgr, delete_free_var_from_last_use, get_node_shape, is_non_memory_node class EstimateMemory(object): """ Estimate memory with chunk """ def __init__(self, node_mgr: NodeMgr) -> None: self.node_mgr = node_mgr def _get_meta_node_size(self, x): x = x.meta["tensor_meta"] x = x.numel * torch.tensor([], dtype=x.dtype).element_size() return x def _get_output_node(self, n): out_size = activation_size(n.meta["fwd_out"]) out_node = [n.name] if out_size > 0 else [] return out_size, out_node def _get_output_node_size(self, n): return self._get_output_node(n)[0] def _add_active_node(self, n, active_list): new_active = self._get_output_node(n)[1] if n.op == "placeholder" and get_node_shape(n) is not None: new_active.append(n.name) for i in new_active: if i not in active_list and get_node_shape(n) is not None: active_list.append(i) def _get_delete_node(self, user, user_to_last_uses, to_keep=None): delete_size = 0 delete_node = [] if user.op not in ("output",): nodes_to_delete = user_to_last_uses.get(user, []) if len(user.users) == 0: nodes_to_delete.append(user) if to_keep is not None: keep_list = [] for n in nodes_to_delete: if n.name in to_keep: keep_list.append(n) for n in keep_list: if n in nodes_to_delete: nodes_to_delete.remove(n) if len(nodes_to_delete): out_node = [self._get_output_node(i) for i in nodes_to_delete] delete_size = sum([i[0] for i in out_node]) for i in range(len(out_node)): if out_node[i][0] > 0: delete_node.append(out_node[i][1][0]) elif nodes_to_delete[i].op == "placeholder": delete_node.append(nodes_to_delete[i].name) # elif any(j in nodes_to_delete[i].name for j in ['transpose', 'permute', 'view']): # delete_node.append(nodes_to_delete[i].name) return delete_size, delete_node def _get_delete_node_size(self, user, user_to_last_uses, to_keep): return self._get_delete_node(user, user_to_last_uses, to_keep)[0] def _remove_deactive_node(self, user, user_to_last_uses, active_list): delete_node = self._get_delete_node(user, user_to_last_uses)[1] for i in delete_node: if i in active_list: active_list.remove(i) def _get_chunk_inputs_size(self, chunk_inputs, chunk_inputs_non_chunk, node_list, chunk_end_idx): nodes_to_delete = [] for chunk_input in chunk_inputs + chunk_inputs_non_chunk: chunk_input_users = chunk_input.users.keys() chunk_input_users_idx = [self.node_mgr.find_node_idx(i) for i in chunk_input_users] if all(i <= chunk_end_idx for i in chunk_input_users_idx): if chunk_input not in nodes_to_delete: nodes_to_delete.append(chunk_input) out_node = [self._get_output_node(i) for i in nodes_to_delete] delete_size = sum([i[0] for i in out_node]) return delete_size def _get_last_usr(self, nodes): node_to_last_use: Dict[Node, Node] = {} user_to_last_uses: Dict[Node, List[Node]] = {} def register_last_uses(n: Node, user: Node): if n not in node_to_last_use: node_to_last_use[n] = user user_to_last_uses.setdefault(user, []).append(n) for node in reversed(nodes): map_arg(node.args, lambda n: register_last_uses(n, node)) map_arg(node.kwargs, lambda n: register_last_uses(n, node)) return user_to_last_uses def _get_contiguous_memory(self, node, not_contiguous_list, delete=False): mem = 0 not_contiguous_ops = ["permute"] inherit_contiguous_ops = ["transpose", "view"] if node.op == "call_function" and any(n in node.name for n in ["matmul", "reshape"]): for n in node.args: if n in not_contiguous_list: # matmul won't change origin tensor, but create a tmp copy mem += self._get_output_node_size(n) elif node.op == "call_module": for n in node.args: if n in not_contiguous_list: # module will just make origin tensor to contiguous if delete: not_contiguous_list.remove(n) elif node.op == "call_method" and any(i in node.name for i in not_contiguous_ops): if node not in not_contiguous_list: not_contiguous_list.append(node) return mem def _get_chunk_ratio(self, node, chunk_node_dim, chunk_size): if node not in chunk_node_dim: return 1.0 node_shape = get_node_shape(node) chunk_dim = chunk_node_dim[node]["chunk_dim"] if chunk_dim is None: return 1.0 else: return float(chunk_size) / node_shape[chunk_dim] def _get_chunk_delete_node_size(self, user, user_to_last_uses, chunk_ratio, chunk_inputs_names): # if any(j in user.name for j in ['transpose', 'permute', 'view']): # return 0 if user.op in ("placeholder", "output"): return 0 nodes_to_delete = user_to_last_uses.get(user, []) if len(user.users) == 0: nodes_to_delete.append(user) delete_size = 0 for n in nodes_to_delete: if n.name in chunk_inputs_names: continue delete_size += self._get_output_node_size(n) * chunk_ratio return delete_size def _print_mem_log(self, log, nodes, title=None): if title: print(title) for idx, (l, n) in enumerate(zip(log, nodes)): print("%s:%.2f \t" % (n.name, l), end="") if (idx + 1) % 3 == 0: print("") print("\n") def _print_compute_op_mem_log(self, log, nodes, title=None): if title: print(title) for idx, (l, n) in enumerate(zip(log, nodes)): if n.op in ["placeholder", "get_attr", "output"]: continue if any(i in n.name for i in ["getitem", "getattr"]): continue print("%s:%.2f \t" % (n.name, l), end="") if (idx + 1) % 3 == 0: print("") print("\n") def estimate_chunk_inference_mem( self, node_list: List, chunk_infos=None, print_mem=False, ): """ Estimate inference memory with chunk Args: node_list (List): _description_ chunk_infos (Dict): Chunk information. Defaults to None. print_mem (bool): Wether to print peak memory of every node. Defaults to False. Returns: act_memory_peak_log (List): peak memory of every node act_memory_after_node_log (List): memory after excuting every node active_node_list_log (List): active nodes of every node. active nodes refer to nodes generated but not deleted. """ act_memory = 0.0 act_memory_peak_log = [] act_memory_after_node_log = [] active_node_list = [] active_node_list_log = [] not_contiguous_list = [] user_to_last_uses = self._get_last_usr(node_list) user_to_last_uses_no_free_var = self._get_last_usr(node_list) delete_free_var_from_last_use(user_to_last_uses_no_free_var) use_chunk = True if chunk_infos is not None else False chunk_within = False chunk_region_idx = None chunk_ratio = 1 # use it to estimate chunk mem chunk_inputs_names = [] if use_chunk: chunk_regions = [i["region"] for i in chunk_infos] chunk_starts = [i[0] for i in chunk_regions] chunk_ends = [i[1] for i in chunk_regions] chunk_inputs = [i["inputs"] for i in chunk_infos] chunk_inputs_non_chunk = [i["inputs_non_chunk"] for i in chunk_infos] chunk_inputs_names = [j.name for i in chunk_inputs for j in i ] + [j.name for i in chunk_inputs_non_chunk for j in i] chunk_outputs = [i["outputs"] for i in chunk_infos] chunk_node_dim = [i["node_chunk_dim"] for i in chunk_infos] chunk_sizes = [i["chunk_size"] if "chunk_size" in i else 1 for i in chunk_infos] for idx, node in enumerate(node_list): # if node in chunk start nodes, change chunk ratio and add chunk_tensor if use_chunk and idx in chunk_starts: chunk_within = True chunk_region_idx = chunk_starts.index(idx) act_memory += sum(self._get_output_node_size(i) for i in chunk_outputs[chunk_region_idx]) / (1024**2) # determine chunk ratio for current node if chunk_within: chunk_ratio = self._get_chunk_ratio( node, chunk_node_dim[chunk_region_idx], chunk_sizes[chunk_region_idx], ) # if node is placeholder, just add the size of the node if node.op == "placeholder": act_memory += self._get_meta_node_size(node) * chunk_ratio / (1024**2) act_memory_peak_log.append(act_memory) # skip output elif node.op == "output": continue # no change for non compute node elif is_non_memory_node(node): act_memory_peak_log.append(act_memory) # node is a compute op # calculate tmp, output node and delete node memory else: # forward memory # TODO: contiguous_memory still not accurate for matmul, view, reshape and transpose act_memory += (self._get_contiguous_memory(node, not_contiguous_list) * chunk_ratio / (1024**2)) act_memory += (self._get_output_node_size(node) * chunk_ratio / (1024**2)) # record max act memory act_memory_peak_log.append(act_memory) # delete useless memory act_memory -= (self._get_contiguous_memory(node, not_contiguous_list, delete=True) * chunk_ratio / (1024**2)) # delete unused vars not in chunk_input_list # we can't delete input nodes until chunk ends if chunk_within: act_memory -= self._get_chunk_delete_node_size( node, user_to_last_uses_no_free_var, chunk_ratio, chunk_inputs_names, ) / (1024**2) else: act_memory -= self._get_delete_node_size(node, user_to_last_uses_no_free_var, chunk_inputs_names) / (1024**2) # log active node, only effective without chunk self._add_active_node(node, active_node_list) self._remove_deactive_node(node, user_to_last_uses, active_node_list) # if node in chunk end nodes, restore chunk settings if use_chunk and idx in chunk_ends: act_memory -= (self._get_output_node_size(node) * chunk_ratio / (1024**2)) act_memory -= self._get_chunk_inputs_size( chunk_inputs[chunk_region_idx], chunk_inputs_non_chunk[chunk_region_idx], node_list, chunk_regions[chunk_region_idx][1], ) / (1024**2) chunk_within = False chunk_ratio = 1 chunk_region_idx = None act_memory_after_node_log.append(act_memory) active_node_list_log.append(copy.deepcopy(active_node_list)) if print_mem: print("with chunk" if use_chunk else "without chunk") # self._print_mem_log(act_memory_peak_log, node_list, "peak") # self._print_mem_log(act_memory_after_node_log, node_list, "after") self._print_compute_op_mem_log(act_memory_peak_log, node_list, "peak") # self._print_compute_op_mem_log( # act_memory_after_node_log, node_list, "after" # ) # param_memory = parameter_size(gm) # all_memory = act_memory + param_memory return act_memory_peak_log, act_memory_after_node_log, active_node_list_log def get_active_nodes(self, node_list: List) -> List: """ Get active nodes for every node Args: node_list (List): _description_ Returns: active_node_list_log (List): active nodes of every node. active nodes refer to nodes generated but not deleted. """ active_node_list = [] active_node_list_log = [] user_to_last_uses = self._get_last_usr(node_list) user_to_last_uses_no_free_var = self._get_last_usr(node_list) delete_free_var_from_last_use(user_to_last_uses_no_free_var) for _, node in enumerate(node_list): # log active node, only effective without chunk self._add_active_node(node, active_node_list) self._remove_deactive_node(node, user_to_last_uses, active_node_list) active_node_list_log.append(copy.deepcopy(active_node_list)) return active_node_list_log
from .trace_indice import TraceIndice from .utils import NodeMgr class ReorderGraph(object): """ Reorder node list and indice trace list """ def __init__(self, trace_indice: TraceIndice, node_mgr: NodeMgr) -> None: self.trace_indice = trace_indice self.node_mgr = node_mgr self.all_reorder_map = {i: i for i in range(len(self.node_mgr.get_node_list()))} def _get_reorder_map(self, chunk_info): reorder_map = {i: i for i in range(len(self.node_mgr.get_node_list()))} chunk_region_start = chunk_info["region"][0] chunk_region_end = chunk_info["region"][1] chunk_prepose_nodes = chunk_info["args"]["prepose_nodes"] chunk_prepose_nodes_idx = [self.node_mgr.find_node_idx(i) for i in chunk_prepose_nodes] # put prepose nodes ahead for idx, n in enumerate(chunk_prepose_nodes): n_idx = chunk_prepose_nodes_idx[idx] reorder_map[n_idx] = chunk_region_start + idx # put other nodes after prepose nodes for n in self.node_mgr.get_node_slice_by_idx(chunk_region_start, chunk_region_end + 1): if n in chunk_prepose_nodes: continue n_idx = self.node_mgr.find_node_idx(n) pos = sum([n_idx < i for i in chunk_prepose_nodes_idx]) reorder_map[n_idx] = n_idx + pos return reorder_map def _reorder_chunk_info(self, chunk_info, reorder_map): # update chunk info chunk_info["region"] = ( chunk_info["region"][0] + len(chunk_info["args"]["prepose_nodes"]), chunk_info["region"][1], ) new_inputs_dim = [] for _, input_dim in enumerate(chunk_info["inputs_dim"]): new_input_dim = {} for k, v in input_dim.items(): new_input_dim[reorder_map[k]] = v new_inputs_dim.append(new_input_dim) chunk_info["inputs_dim"] = new_inputs_dim return chunk_info def _update_all_reorder_map(self, reorder_map): for origin_idx, map_idx in self.all_reorder_map.items(): self.all_reorder_map[origin_idx] = reorder_map[map_idx] def _reorder_self_node_list(self, reorder_map): new_node_list = [None for _ in range(len(self.node_mgr.get_node_list()))] for old_idx, new_idx in reorder_map.items(): new_node_list[new_idx] = self.node_mgr.get_node_by_idx(old_idx) self.node_mgr.update_node_list(new_node_list) def _reorder_idx_trace(self, reorder_map): # reorder list new_idx_trace_list = [None for _ in range(len(self.trace_indice.indice_trace_list))] for old_idx, new_idx in reorder_map.items(): new_idx_trace_list[new_idx] = self.trace_indice.indice_trace_list[old_idx] self.trace_indice.indice_trace_list = new_idx_trace_list # update compute for idx_trace in self.trace_indice.indice_trace_list: compute = idx_trace["compute"] for dim_compute in compute: for idx, i in enumerate(dim_compute): dim_compute[idx] = reorder_map[i] # update source for idx_trace in self.trace_indice.indice_trace_list: source = idx_trace["source"] for dim_idx, dim_source in enumerate(source): new_dim_source = {} for k, v in dim_source.items(): new_dim_source[reorder_map[k]] = v source[dim_idx] = new_dim_source def reorder_all(self, chunk_info): if chunk_info is None: return chunk_info if len(chunk_info["args"]["prepose_nodes"]) == 0: return chunk_info reorder_map = self._get_reorder_map(chunk_info) self._update_all_reorder_map(reorder_map) self._reorder_idx_trace(reorder_map) self._reorder_self_node_list(reorder_map) chunk_info = self._reorder_chunk_info(chunk_info, reorder_map) return chunk_info def reorder_node_list(self, node_list): new_node_list = [None for _ in range(len(node_list))] for old_idx, new_idx in self.all_reorder_map.items(): new_node_list[new_idx] = node_list[old_idx] return new_node_list def tmp_reorder(self, node_list, chunk_info): if len(chunk_info["args"]["prepose_nodes"]) == 0: return node_list, chunk_info reorder_map = self._get_reorder_map(chunk_info) # new tmp node list new_node_list = [None for _ in range(len(node_list))] for old_idx, new_idx in reorder_map.items(): new_node_list[new_idx] = node_list[old_idx] chunk_info = self._reorder_chunk_info(chunk_info, reorder_map) return new_node_list, chunk_info