|
8 | 8 |
|
9 | 9 | import logging
|
10 | 10 | import operator
|
11 |
| -from typing import Any, Callable, Dict, List, Mapping, Tuple, Union |
| 11 | +import re |
| 12 | +from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union |
12 | 13 |
|
13 | 14 | import torch
|
14 | 15 | import torch.nn as nn
|
15 |
| -from torch.fx import Node |
16 |
| -from torch.fx.graph_module import GraphModule |
| 16 | +from torch.fx import GraphModule, Node |
| 17 | +from torch.fx.passes.split_utils import split_by_tags |
17 | 18 |
|
| 19 | +from composer.algorithms.stochastic_depth.stochastic_layers import BlockStochasticModule |
18 | 20 | from composer.utils import ensure_tuple
|
19 | 21 |
|
20 | 22 | log = logging.getLogger(__name__)
|
21 | 23 |
|
22 |
| -__all__ = ['count_op_instances', 'replace_op', 'fuse_parallel_linears'] |
| 24 | +__all__ = ['count_op_instances', 'replace_op', 'fuse_parallel_linears', 'apply_stochastic_residual'] |
23 | 25 |
|
24 | 26 |
|
25 | 27 | def count_op_instances(gm: GraphModule, ops: Union[Callable, str, List[Union[Callable, str]]]) -> int:
|
@@ -111,28 +113,138 @@ def replace_op(gm: GraphModule, src_ops: Union[Callable, str, List[Union[Callabl
|
111 | 113 | return gm
|
112 | 114 |
|
113 | 115 |
|
114 |
| -def detect_residual_pattern(gm: GraphModule): |
115 |
| - """Search and replace the pattern with another. |
| 116 | +def _get_ancestors(node: Node) -> List[Node]: |
| 117 | + ancestorNodes = [] |
| 118 | + while node.op != 'placeholder': |
| 119 | + ancestorNodes.append(node) |
| 120 | + node = node.all_input_nodes[0] |
| 121 | + return ancestorNodes |
| 122 | + |
| 123 | + |
| 124 | +def _get_residual_block_nodes(nodeLHS: Node, nodeRHS: Node) -> Tuple[List[Node], List[Node]]: |
| 125 | + """Walk backwards from nodeLHS and nodeRSH to the root and construct lists of their parents. |
116 | 126 |
|
117 | 127 | Arguments:
|
118 |
| - gm (GraphModule): The source FX-traced graph. |
| 128 | + nodeLHS (Node): left-hand side node for a binary operator |
| 129 | + nodeRHS (Node): right-hand side node for a binary operator |
119 | 130 |
|
120 | 131 | Returns:
|
121 |
| - GraphModule: Modified GraphModule. |
| 132 | + (lhsAncestors, rhsAncestors): Two lists of nodes containing ancestors for ``nodeLHS`` and ``nodeRHS`` with |
| 133 | + their common ancestors removed. |
122 | 134 | """
|
123 |
| - raise NotImplementedError('detect_residual_pattern is currently not implemented.') |
| 135 | + lhsAncestors = _get_ancestors(nodeLHS) |
| 136 | + rhsAncestors = _get_ancestors(nodeRHS) |
| 137 | + |
| 138 | + # Iterate from back and eliminate common nodes |
| 139 | + while lhsAncestors and rhsAncestors and lhsAncestors[-1] == rhsAncestors[-1]: |
| 140 | + lhsAncestors.pop() |
| 141 | + rhsAncestors.pop() |
| 142 | + lhsAncestors.reverse() |
| 143 | + rhsAncestors.reverse() |
| 144 | + return lhsAncestors, rhsAncestors |
124 | 145 |
|
125 | 146 |
|
126 |
| -def replace_residual_with_stochastic(gm: GraphModule): |
127 |
| - """Replaces residual pattern with their stoachstic equivalent. |
| 147 | +def _attach_tag(nodes: List[Node], tag: str): |
| 148 | + """Attach tag to the given nodes for the splitter.""" |
| 149 | + for node in nodes: |
| 150 | + node.tag = tag # type: ignore[attr-defined] |
| 151 | + |
| 152 | + |
| 153 | +def _tag_residual_nodes(gm: GraphModule) -> Tuple[List[str], int]: |
| 154 | + """Tag nodes for splitting.""" |
| 155 | + # all nodes that are not a part of the residual blocks are tagged with "mainN_{count}". |
| 156 | + # a tag is required for all nodes by split_by_tags |
| 157 | + # Also an earlier tag can be repeated for later nodes. |
| 158 | + count = 0 |
| 159 | + all_tags = [] |
| 160 | + # In this pass over all nodes, we just tag them |
| 161 | + for node in gm.graph.nodes: |
| 162 | + default_tag = f'mainN_{count}' |
| 163 | + node.tag = default_tag |
| 164 | + if default_tag not in all_tags: |
| 165 | + all_tags.append(default_tag) |
| 166 | + if node.op == 'call_function' and node.target in [torch.add, operator.add]: |
| 167 | + assert len(node.all_input_nodes) == 2 |
| 168 | + node0, node1 = node.all_input_nodes[0], node.all_input_nodes[1] |
| 169 | + lhs_nodes, rhs_nodes = _get_residual_block_nodes(node0, node1) |
| 170 | + if lhs_nodes or rhs_nodes: |
| 171 | + if len(lhs_nodes): |
| 172 | + _attach_tag(lhs_nodes, f'non_res_{count}') |
| 173 | + all_tags.append(f'non_res_{count}') |
| 174 | + if len(rhs_nodes): |
| 175 | + _attach_tag(rhs_nodes, f'residual_{count}') |
| 176 | + all_tags.append(f'residual_{count}') |
| 177 | + add_tag = f'addN_{count}' |
| 178 | + if add_tag not in all_tags: |
| 179 | + all_tags.append(add_tag) |
| 180 | + node.tag = add_tag |
| 181 | + count += 1 |
| 182 | + return all_tags, count |
| 183 | + |
| 184 | + |
| 185 | +def _get_residual_modules(gm: GraphModule, node: Node) -> Tuple[Optional[GraphModule], Optional[GraphModule], int]: |
| 186 | + """Returns GraphModules for the main and residual branches. |
| 187 | +
|
| 188 | + node.op is assumed to be a call_module |
| 189 | + """ |
| 190 | + pattern = re.compile(r'non_res_(\d+)|residual_(\d+)') |
| 191 | + matches = pattern.match(str(node.target)) |
| 192 | + if matches: |
| 193 | + idx = int(matches[1]) if matches[1] else int(matches[2]) |
| 194 | + main_submod = getattr(gm, f'non_res_{idx}') |
| 195 | + residual_submod = getattr(gm, f'residual_{idx}', None) |
| 196 | + return main_submod, residual_submod, idx |
| 197 | + else: |
| 198 | + return None, None, 0 |
| 199 | + |
| 200 | + |
| 201 | +def _replace_residual_pattern(gm: GraphModule, |
| 202 | + original_node: Node, |
| 203 | + replacement_module: str, |
| 204 | + has_residual_ops: bool = False) -> None: |
| 205 | + """Replaces main, residual and add_node with the ``replacement_module``. |
| 206 | +
|
| 207 | + ``replacement_module`` is already added to the gm. |
| 208 | + """ |
| 209 | + insert_node = original_node.prev |
| 210 | + add_node = original_node.next |
| 211 | + if has_residual_ops: |
| 212 | + add_node = original_node.next.next |
| 213 | + with gm.graph.inserting_after(insert_node): |
| 214 | + new_node = gm.graph.call_module(replacement_module, args=(insert_node,)) # type: ignore |
| 215 | + add_node.replace_all_uses_with(new_node) |
| 216 | + gm.graph.erase_node(add_node) |
| 217 | + if has_residual_ops: |
| 218 | + gm.graph.erase_node(original_node.next) |
| 219 | + gm.graph.erase_node(original_node) |
| 220 | + gm.graph.lint() |
| 221 | + |
| 222 | + |
| 223 | +def apply_stochastic_residual(gm: GraphModule, drop_rate: float = 0.2) -> Tuple[GraphModule, int]: |
| 224 | + """Detect and replace residual pattern with their stochastic equivalent. |
128 | 225 |
|
129 | 226 | Arguments:
|
130 |
| - gm (GraphModule): The source FX-traced graph. |
| 227 | + gm (GraphModule): The source FX-traced graph. It can be the whole model symbolically traced. |
131 | 228 |
|
132 | 229 | Returns:
|
133 |
| - GraphModule: Modified GraphModule. |
| 230 | + GraphModule: Modified GraphModule that has stochastic residual connections. |
134 | 231 | """
|
135 |
| - raise NotImplementedError('replace_residual_with_stochastic is currently not implemented.') |
| 232 | + if not isinstance(gm, GraphModule): |
| 233 | + raise ValueError( |
| 234 | + f'Input to apply_stochastic_residual should be an instance of GraphModule. Received {type(gm)}') |
| 235 | + all_tags, count = _tag_residual_nodes(gm) |
| 236 | + split_gm = split_by_tags(gm, all_tags) |
| 237 | + for node in split_gm.graph.nodes: |
| 238 | + if node.op != 'call_module': |
| 239 | + continue |
| 240 | + |
| 241 | + main_submod, residual_submod, idx = _get_residual_modules(split_gm, node) |
| 242 | + if main_submod: |
| 243 | + residual_st_instance = BlockStochasticModule(main_submod, residual_submod, drop_rate) |
| 244 | + split_gm.add_submodule(f'resi_st_{idx}', residual_st_instance) # type: ignore |
| 245 | + _replace_residual_pattern(split_gm, node, f'resi_st_{idx}', residual_submod is not None) |
| 246 | + split_gm.recompile() |
| 247 | + return split_gm, count |
136 | 248 |
|
137 | 249 |
|
138 | 250 | def _can_linears_be_fused(linear_nodes: List[Node], all_modules: Mapping[str, nn.Module]) -> bool:
|
|
0 commit comments