mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-03 10:06:44 +00:00
* init
* rename and remove useless func
* basic chunk
* add evoformer
* align evoformer
* add meta
* basic chunk
* basic memory
* finish basic inference memory estimation
* finish memory estimation
* fix bug
* finish memory estimation
* add part of index tracer
* finish basic index tracer
* add doc string
* add doc str
* polish code
* polish code
* update active log
* polish code
* add possible region search
* finish region search loop
* finish chunk define
* support new op
* rename index tracer
* finishi codegen on msa
* redesign index tracer, add source and change compute
* pass outproduct mean
* code format
* code format
* work with outerproductmean and msa
* code style
* code style
* code style
* code style
* change threshold
* support check_index_duplicate
* support index dupilictae and update loop
* support output
* update memory estimate
* optimise search
* fix layernorm
* move flow tracer
* refactor flow tracer
* format code
* refactor flow search
* code style
* adapt codegen to prepose node
* code style
* remove abandoned function
* remove flow tracer
* code style
* code style
* reorder nodes
* finish node reorder
* update run
* code style
* add chunk select class
* add chunk select
* code style
* add chunksize in emit, fix bug in reassgin shape
* code style
* turn off print mem
* add evoformer openfold init
* init openfold
* add benchmark
* add print
* code style
* code style
* init openfold
* update openfold
* align openfold
* use max_mem to control stratge
* update source add
* add reorder in mem estimator
* improve reorder efficeincy
* support ones_like, add prompt if fit mode search fail
* fix a bug in ones like, dont gen chunk if dim size is 1
* fix bug again
* update min memory stratege, reduce mem usage by 30%
* last version of benchmark
* refactor structure
* restruct dir
* update test
* rename
* take apart chunk code gen
* close mem and code print
* code format
* rename ambiguous variable
* seperate flow tracer
* seperate input node dim search
* seperate prepose_nodes
* seperate non chunk input
* seperate reorder
* rename
* ad reorder graph
* seperate trace flow
* code style
* code style
* fix typo
* set benchmark
* rename test
* update codegen test
* Fix state_dict key missing issue of the ZeroDDP (#2363)
* Fix state_dict output for ZeroDDP duplicated parameters
* Rewrite state_dict based on get_static_torch_model
* Modify get_static_torch_model to be compatible with the lower version (ZeroDDP)
* update codegen test
* update codegen test
* add chunk search test
* code style
* add available
* [hotfix] fix gpt gemini example (#2404)
* [hotfix] fix gpt gemini example
* [example] add new assertions
* remove autochunk_available
* [workflow] added nightly release to pypi (#2403)
* add comments
* code style
* add doc for search chunk
* [doc] updated readme regarding pypi installation (#2406)
* add doc for search
* [doc] updated kernel-related optimisers' docstring (#2385)
* [doc] updated kernel-related optimisers' docstring
* polish doc
* rename trace_index to trace_indice
* rename function from index to indice
* rename
* rename in doc
* [polish] polish code for get_static_torch_model (#2405)
* [gemini] polish code
* [testing] remove code
* [gemini] make more robust
* rename
* rename
* remove useless function
* [worfklow] added coverage test (#2399)
* [worfklow] added coverage test
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* add doc for trace indice
* [docker] updated Dockerfile and release workflow (#2410)
* add doc
* update doc
* add available
* change imports
* add test in import
* [workflow] refactored the example check workflow (#2411)
* [workflow] refactored the example check workflow
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* Update parallel_context.py (#2408)
* [hotfix] add DISTPAN argument for benchmark (#2412)
* change the benchmark config file
* change config
* revert config file
* rename distpan to distplan
* [workflow] added precommit check for code consistency (#2401)
* [workflow] added precommit check for code consistency
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* polish code
* adapt new fx
* [workflow] added translation for non-english comments (#2414)
* [setup] refactored setup.py for dependency graph (#2413)
* change import
* update doc
* [workflow] auto comment if precommit check fails (#2417)
* [hotfix] add norm clearing for the overflow step (#2416)
* [examples] adding tflops to PaLM (#2365)
* [workflow]auto comment with test coverage report (#2419)
* [workflow]auto comment with test coverage report
* polish code
* polish yaml
* [doc] added documentation for CI/CD (#2420)
* [doc] added documentation for CI/CD
* polish markdown
* polish markdown
* polish markdown
* [example] removed duplicated stable diffusion example (#2424)
* [zero] add inference mode and its unit test (#2418)
* [workflow] report test coverage even if below threshold (#2431)
* [example] improved the clarity yof the example readme (#2427)
* [example] improved the clarity yof the example readme
* polish workflow
* polish workflow
* polish workflow
* polish workflow
* polish workflow
* polish workflow
* [ddp] add is_ddp_ignored (#2434)
[ddp] rename to is_ddp_ignored
* [workflow] make test coverage report collapsable (#2436)
* [autoparallel] add shard option (#2423)
* [fx] allow native ckpt trace and codegen. (#2438)
* [cli] provided more details if colossalai run fail (#2442)
* [autoparallel] integrate device mesh initialization into autoparallelize (#2393)
* [autoparallel] integrate device mesh initialization into autoparallelize
* add megatron solution
* update gpt autoparallel examples with latest api
* adapt beta value to fit the current computation cost
* [zero] fix state_dict and load_state_dict for ddp ignored parameters (#2443)
* [ddp] add is_ddp_ignored
[ddp] rename to is_ddp_ignored
* [zero] fix state_dict and load_state_dict
* fix bugs
* [zero] update unit test for ZeroDDP
* [example] updated the hybrid parallel tutorial (#2444)
* [example] updated the hybrid parallel tutorial
* polish code
* [zero] add warning for ignored parameters (#2446)
* [example] updated large-batch optimizer tutorial (#2448)
* [example] updated large-batch optimizer tutorial
* polish code
* polish code
* [example] fixed seed error in train_dreambooth_colossalai.py (#2445)
* [workflow] fixed the on-merge condition check (#2452)
* [workflow] automated the compatiblity test (#2453)
* [workflow] automated the compatiblity test
* polish code
* [autoparallel] update binary elementwise handler (#2451)
* [autoparallel] update binary elementwise handler
* polish
* [workflow] automated bdist wheel build (#2459)
* [workflow] automated bdist wheel build
* polish workflow
* polish readme
* polish readme
* Fix False warning in initialize.py (#2456)
* Update initialize.py
* pre-commit run check
* [examples] update autoparallel tutorial demo (#2449)
* [examples] update autoparallel tutorial demo
* add test_ci.sh
* polish
* add conda yaml
* [cli] fixed hostname mismatch error (#2465)
* [example] integrate autoparallel demo with CI (#2466)
* [example] integrate autoparallel demo with CI
* polish code
* polish code
* polish code
* polish code
* [zero] low level optim supports ProcessGroup (#2464)
* [example] update vit ci script (#2469)
* [example] update vit ci script
* [example] update requirements
* [example] update requirements
* [example] integrate seq-parallel tutorial with CI (#2463)
* [zero] polish low level optimizer (#2473)
* polish pp middleware (#2476)
Co-authored-by: Ziyue Jiang <ziyue.jiang@gmail.com>
* [example] update gpt gemini example ci test (#2477)
* [zero] add unit test for low-level zero init (#2474)
* [workflow] fixed the skip condition of example weekly check workflow (#2481)
* [example] stable diffusion add roadmap
* add dummy test_ci.sh
* [example] stable diffusion add roadmap (#2482)
* [CI] add test_ci.sh for palm, opt and gpt (#2475)
* polish code
* [example] titans for gpt
* polish readme
* remove license
* polish code
* update readme
* [example] titans for gpt (#2484)
* [autoparallel] support origin activation ckpt on autoprallel system (#2468)
* [autochunk] support evoformer tracer (#2485)
support full evoformer tracer, which is a main module of alphafold. previously we just support a simplifed version of it.
1. support some evoformer's op in fx
2. support evoformer test
3. add repos for test code
* [example] fix requirements (#2488)
* [zero] add unit testings for hybrid parallelism (#2486)
* [hotfix] gpt example titans bug #2493
* polish code and fix dataloader bugs
* [hotfix] gpt example titans bug #2493 (#2494)
* [fx] allow control of ckpt_codegen init (#2498)
* [fx] allow control of ckpt_codegen init
Currently in ColoGraphModule, ActivationCheckpointCodeGen will be set automatically in __init__. But other codegen can't be set if so.
So I add an arg to control whether to set ActivationCheckpointCodeGen in __init__.
* code style
* [example] dreambooth example
* add test_ci.sh to dreambooth
* [autochunk] support autochunk on evoformer (#2497)
* Revert "Update parallel_context.py (#2408)"
This reverts commit 7d5640b9db
.
* add avg partition (#2483)
Co-authored-by: Ziyue Jiang <ziyue.jiang@gmail.com>
* [auto-chunk] support extramsa (#3) (#2504)
* [utils] lazy init. (#2148)
* [utils] lazy init.
* [utils] remove description.
* [utils] complete.
* [utils] finalize.
* [utils] fix names.
* [autochunk] support parsing blocks (#2506)
* [zero] add strict ddp mode (#2508)
* [zero] add strict ddp mode
* [polish] add comments for strict ddp mode
* [zero] fix test error
* [doc] update opt and tutorial links (#2509)
* [workflow] fixed changed file detection (#2515)
Co-authored-by: oahzxl <xuanlei.zhao@gmail.com>
Co-authored-by: eric8607242 <e0928021388@gmail.com>
Co-authored-by: HELSON <c2h214748@gmail.com>
Co-authored-by: Frank Lee <somerlee.9@gmail.com>
Co-authored-by: Haofan Wang <haofanwang.ai@gmail.com>
Co-authored-by: Jiarui Fang <fangjiarui123@gmail.com>
Co-authored-by: ZijianYY <119492445+ZijianYY@users.noreply.github.com>
Co-authored-by: YuliangLiu0306 <72588413+YuliangLiu0306@users.noreply.github.com>
Co-authored-by: Super Daniel <78588128+super-dainiu@users.noreply.github.com>
Co-authored-by: ver217 <lhx0217@gmail.com>
Co-authored-by: Ziyue Jiang <ziyue.jiang97@gmail.com>
Co-authored-by: Ziyue Jiang <ziyue.jiang@gmail.com>
Co-authored-by: oahzxl <43881818+oahzxl@users.noreply.github.com>
Co-authored-by: binmakeswell <binmakeswell@gmail.com>
Co-authored-by: Fazzie-Maqianli <55798671+Fazziekey@users.noreply.github.com>
Co-authored-by: アマデウス <kurisusnowdeng@users.noreply.github.com>
524 lines
21 KiB
Python
524 lines
21 KiB
Python
from typing import Any, Dict, Iterable, List, Tuple
|
|
|
|
import torch
|
|
|
|
import colossalai
|
|
from colossalai.fx.codegen.activation_checkpoint_codegen import CODEGEN_AVAILABLE
|
|
|
|
if CODEGEN_AVAILABLE:
|
|
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
|
|
|
|
from .search_chunk import SearchChunk
|
|
from .utils import delete_free_var_from_last_use, find_idx_by_name, get_logger, get_node_shape
|
|
|
|
|
|
def _gen_chunk_slice_dim(chunk_dim: int, chunk_indice_name: str, shape: List) -> str:
|
|
"""
|
|
Generate chunk slice string, eg. [:, :, chunk_idx_name:chunk_idx_name + chunk_size, :]
|
|
|
|
Args:
|
|
chunk_dim (int)
|
|
chunk_indice_name (str): chunk indice name
|
|
shape (List): node shape
|
|
|
|
Returns:
|
|
new_shape (str): return slice
|
|
"""
|
|
new_shape = "["
|
|
for idx, _ in enumerate(shape):
|
|
if idx == chunk_dim:
|
|
new_shape += "%s:%s + chunk_size" % (chunk_indice_name, chunk_indice_name)
|
|
else:
|
|
new_shape += ":"
|
|
new_shape += ", "
|
|
new_shape = new_shape[:-2] + "]"
|
|
return new_shape
|
|
|
|
|
|
def _gen_loop_start(chunk_input: List[Node], chunk_output: Node, chunk_ouput_dim: int, chunk_size=2) -> str:
|
|
"""
|
|
Generate chunk loop start
|
|
|
|
eg. chunk_result = torch.empty([100, 100], dtype=input_node.dtype, device=input_node.device)
|
|
chunk_size = 32
|
|
for chunk_idx in range(0, 100, 32):
|
|
......
|
|
|
|
Args:
|
|
chunk_input (List[Node]): chunk input node
|
|
chunk_output (Node): chunk output node
|
|
chunk_ouput_dim (int): chunk output node chunk dim
|
|
chunk_size (int): chunk size. Defaults to 2.
|
|
|
|
Returns:
|
|
context (str): generated str
|
|
"""
|
|
input_node = chunk_input[0]
|
|
out_shape = get_node_shape(chunk_output)
|
|
out_str = str(list(out_shape))
|
|
context = (
|
|
"chunk_result = torch.empty(%s, dtype=%s.dtype, device=%s.device); chunk_size = %d\nfor chunk_idx in range" %
|
|
(out_str, input_node.name, input_node.name, chunk_size))
|
|
context += "(0, %d, chunk_size):\n" % (out_shape[chunk_ouput_dim])
|
|
return context
|
|
|
|
|
|
def _gen_loop_end(
|
|
chunk_inputs: List[Node],
|
|
chunk_non_compute_inputs: List[Node],
|
|
chunk_outputs: Node,
|
|
chunk_outputs_dim: int,
|
|
node_list: List[Node],
|
|
) -> str:
|
|
"""
|
|
Generate chunk loop end
|
|
|
|
eg. chunk_result[chunk_idx:chunk_idx + chunk_size] = output_node
|
|
output_node = chunk_result; xx = None; xx = None
|
|
|
|
Args:
|
|
chunk_inputs (List[Node]): chunk input node
|
|
chunk_non_compute_inputs (List[Node]): input node without chunk
|
|
chunk_outputs (Node): chunk output node
|
|
chunk_outputs_dim (int): chunk output node chunk dim
|
|
node_list (List)
|
|
|
|
Returns:
|
|
context (str): generated str
|
|
"""
|
|
chunk_outputs_name = chunk_outputs.name
|
|
chunk_outputs_idx = find_idx_by_name(chunk_outputs_name, node_list)
|
|
chunk_output_shape = chunk_outputs.meta["tensor_meta"].shape
|
|
chunk_slice = _gen_chunk_slice_dim(chunk_outputs_dim, "chunk_idx", chunk_output_shape)
|
|
context = " chunk_result%s = %s; %s = None\n" % (
|
|
chunk_slice,
|
|
chunk_outputs_name,
|
|
chunk_outputs_name,
|
|
)
|
|
context += (chunk_outputs_name + " = chunk_result; chunk_result = None; chunk_size = None")
|
|
|
|
# determine if its the last use for chunk input
|
|
for chunk_input in chunk_inputs + chunk_non_compute_inputs:
|
|
if all([find_idx_by_name(user.name, node_list) <= chunk_outputs_idx for user in chunk_input.users.keys()]):
|
|
context += "; %s = None" % chunk_input.name
|
|
|
|
context += "\n"
|
|
return context
|
|
|
|
|
|
def _replace_name(context: str, name_from: str, name_to: str) -> str:
|
|
"""
|
|
replace node name
|
|
"""
|
|
patterns = [(" ", " "), (" ", "."), (" ", ","), ("(", ")"), ("(", ","), (" ", ")"), (" ", ""), ("", " ")]
|
|
for p in patterns:
|
|
source = p[0] + name_from + p[1]
|
|
target = p[0] + name_to + p[1]
|
|
if source in context:
|
|
context = context.replace(source, target)
|
|
break
|
|
return context
|
|
|
|
|
|
def _replace_reshape_size(context: str, node_name: str, reshape_size_dict: Dict) -> str:
|
|
"""
|
|
replace reshape size, some may have changed due to chunk
|
|
"""
|
|
if node_name not in reshape_size_dict:
|
|
return context
|
|
context = context.replace(reshape_size_dict[node_name][0], reshape_size_dict[node_name][1])
|
|
return context
|
|
|
|
|
|
def _replace_ones_like(
|
|
search_chunk: SearchChunk,
|
|
chunk_infos: List[Dict],
|
|
region_idx: int,
|
|
node_idx: int,
|
|
node: Node,
|
|
body: List[str],
|
|
) -> List[str]:
|
|
"""
|
|
add chunk slice for new tensor op such as ones like
|
|
"""
|
|
if "ones_like" in node.name:
|
|
meta_node = search_chunk.trace_indice.node_list[node_idx]
|
|
chunk_dim = chunk_infos[region_idx]["node_chunk_dim"][meta_node]["chunk_dim"]
|
|
if get_node_shape(meta_node)[chunk_dim] != 1:
|
|
source_node = meta_node.args[0].args[0]
|
|
if (source_node not in chunk_infos[region_idx]["node_chunk_dim"]
|
|
or chunk_infos[region_idx]["node_chunk_dim"][source_node]["chunk_dim"] is None):
|
|
chunk_slice = _gen_chunk_slice_dim(chunk_dim, "chunk_idx", get_node_shape(node))
|
|
body[-1] = _replace_name(body[-1], node.args[0].name, node.args[0].name + chunk_slice)
|
|
return body
|
|
|
|
|
|
def _replace_input_node(
|
|
chunk_inputs: List[Node],
|
|
region_idx: int,
|
|
chunk_inputs_dim: Dict,
|
|
node_idx: int,
|
|
body: List[str],
|
|
) -> List[str]:
|
|
"""
|
|
add chunk slice for input nodes
|
|
"""
|
|
for input_node_idx, input_node in enumerate(chunk_inputs[region_idx]):
|
|
for idx, dim in chunk_inputs_dim[region_idx][input_node_idx].items():
|
|
if idx == node_idx:
|
|
chunk_slice = _gen_chunk_slice_dim(dim[0], "chunk_idx", get_node_shape(input_node))
|
|
body[-1] = _replace_name(body[-1], input_node.name, input_node.name + chunk_slice)
|
|
return body
|
|
|
|
|
|
def emit_code_with_chunk(
|
|
body: List[str],
|
|
nodes: Iterable[Node],
|
|
emit_node_func,
|
|
delete_unused_value_func,
|
|
search_chunk: SearchChunk,
|
|
chunk_infos: List,
|
|
):
|
|
"""
|
|
Emit code with chunk according to chunk_infos.
|
|
|
|
It will generate a for loop in chunk regions, and
|
|
replace inputs and outputs of regions with chunked variables.
|
|
|
|
Args:
|
|
body: forward code
|
|
nodes: graph.nodes
|
|
emit_node_func: function to emit node
|
|
delete_unused_value_func: function to remove the unused value
|
|
search_chunk: the class to search all chunks
|
|
chunk_infos: store all information about all chunks.
|
|
"""
|
|
node_list = list(nodes)
|
|
|
|
# chunk region
|
|
chunk_starts = [i["region"][0] for i in chunk_infos]
|
|
chunk_ends = [i["region"][1] for i in chunk_infos]
|
|
|
|
# chunk inputs
|
|
chunk_inputs = [i["inputs"] for i in chunk_infos] # input with chunk
|
|
chunk_inputs_non_chunk = [i["inputs_non_chunk"] for i in chunk_infos] # input without chunk
|
|
chunk_inputs_dim = [i["inputs_dim"] for i in chunk_infos] # input chunk dim
|
|
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
|
|
chunk_outputs = [i["outputs"][0] for i in chunk_infos]
|
|
chunk_outputs_dim = [i["outputs_dim"] for i in chunk_infos]
|
|
|
|
node_list = search_chunk.reorder_graph.reorder_node_list(node_list)
|
|
node_idx = 0
|
|
region_idx = 0
|
|
within_chunk_region = False
|
|
|
|
while node_idx < len(node_list):
|
|
node = node_list[node_idx]
|
|
|
|
# if is chunk start, generate for loop start
|
|
if node_idx in chunk_starts:
|
|
within_chunk_region = True
|
|
region_idx = chunk_starts.index(node_idx)
|
|
body.append(
|
|
_gen_loop_start(
|
|
chunk_inputs[region_idx],
|
|
chunk_outputs[region_idx],
|
|
chunk_outputs_dim[region_idx],
|
|
chunk_infos[region_idx]["chunk_size"],
|
|
))
|
|
|
|
if within_chunk_region:
|
|
emit_node_func(node, body)
|
|
# replace input var with chunk var
|
|
body = _replace_input_node(chunk_inputs, region_idx, chunk_inputs_dim, node_idx, body)
|
|
# ones like
|
|
body = _replace_ones_like(search_chunk, chunk_infos, region_idx, node_idx, node, body)
|
|
# reassgin reshape size
|
|
body[-1] = _replace_reshape_size(body[-1], node.name, chunk_infos[region_idx]["reshape_size"])
|
|
body[-1] = " " + body[-1]
|
|
delete_unused_value_func(node, body, chunk_inputs_names)
|
|
else:
|
|
emit_node_func(node, body)
|
|
if node_idx not in chunk_inputs:
|
|
delete_unused_value_func(node, body, chunk_inputs_names)
|
|
|
|
# generate chunk region end
|
|
if node_idx in chunk_ends:
|
|
body.append(
|
|
_gen_loop_end(
|
|
chunk_inputs[region_idx],
|
|
chunk_inputs_non_chunk[region_idx],
|
|
chunk_outputs[region_idx],
|
|
chunk_outputs_dim[region_idx],
|
|
node_list,
|
|
))
|
|
within_chunk_region = False
|
|
|
|
node_idx += 1
|
|
|
|
|
|
if CODEGEN_AVAILABLE:
|
|
|
|
class AutoChunkCodeGen(CodeGen):
|
|
|
|
def __init__(self,
|
|
meta_graph,
|
|
max_memory: int = None,
|
|
print_mem: bool = False,
|
|
print_progress: bool = False) -> None:
|
|
super().__init__()
|
|
# find the chunk regions
|
|
self.search_chunk = SearchChunk(meta_graph, max_memory, print_mem, print_progress)
|
|
self.chunk_infos = self.search_chunk.search_region()
|
|
if print_progress:
|
|
get_logger().info("AutoChunk start 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))
|
|
|
|
delete_free_var_from_last_use(user_to_last_uses)
|
|
|
|
# NOTE: we add a variable to distinguish body and ckpt_func
|
|
def delete_unused_values(user: Node, body, to_keep=[]):
|
|
"""
|
|
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, [])
|
|
nodes_to_delete = [i for i in nodes_to_delete if i.name not in to_keep]
|
|
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
|
|
emit_code_with_chunk(
|
|
body,
|
|
nodes,
|
|
emit_node,
|
|
delete_unused_values,
|
|
self.search_chunk,
|
|
self.chunk_infos,
|
|
)
|
|
|
|
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}"""
|
|
# print(fn_code)
|
|
return PythonCode(fn_code, globals_)
|