Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[autoparallel] add rotor C version #1658

Merged
merged 52 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
04e5272
Merge pull request #1 from hpcaitech/main
Cypher30 Jul 14, 2022
75618b3
Merge pull request #2 from hpcaitech/main
Cypher30 Jul 15, 2022
3e4620c
Merge pull request #3 from hpcaitech/main
Cypher30 Jul 20, 2022
cf24049
Merge remote-tracking branch 'upstream/main' into main
Jul 20, 2022
3d223b6
Merge remote-tracking branch 'upstream/main' into main
Jul 21, 2022
644115c
Merge branch 'hpcaitech:main' into main
Cypher30 Jul 22, 2022
d995ade
Merge branch 'hpcaitech:main' into main
Cypher30 Jul 25, 2022
bba2dbe
Merge branch 'hpcaitech:main' into main
Cypher30 Jul 26, 2022
05ca628
Merge branch 'hpcaitech:main' into main
Cypher30 Jul 26, 2022
0a967da
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 6, 2022
0637c0d
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 8, 2022
74a6227
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 10, 2022
e550490
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 10, 2022
2d7f5d9
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 11, 2022
b62e870
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 12, 2022
b4b0974
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 15, 2022
65c20de
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 16, 2022
1660bfc
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 17, 2022
6eb0ad0
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 20, 2022
56df059
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 26, 2022
480e932
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 30, 2022
0fa66ee
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 30, 2022
1d013b0
Merge branch 'hpcaitech:main' into main
Cypher30 Aug 31, 2022
5774db2
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 5, 2022
e8ff699
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 6, 2022
855c728
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 7, 2022
2c113ea
Merge branch 'main' of github.com:Cypher30/ColossalAI into main
Sep 8, 2022
838ba70
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 13, 2022
cacec2b
Merge branch 'main' of github.com:Cypher30/ColossalAI into main
Sep 13, 2022
5ed6ef0
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 14, 2022
668af30
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 14, 2022
df79772
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 15, 2022
7b6a0fc
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 20, 2022
c30022e
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 23, 2022
df20f4d
Merge branch 'hpcaitech:main' into main
Cypher30 Sep 26, 2022
a78d4cd
[autoparallel] add rotor c version
Sep 27, 2022
ebf4f4d
[autoparallel] fix conflict
Sep 27, 2022
6274266
Merge branch 'hpcaitech:main' into feature/add_rotor_c_version
Cypher30 Sep 27, 2022
cfab103
[fx] remove metainfoprop in rotor solver
Sep 27, 2022
af4d41e
[autoparallel] modify C
Sep 27, 2022
96228ef
Merge branch 'hpcaitech:main' into feature/add_rotor_c_version
Cypher30 Sep 27, 2022
5464deb
[autoparallel] remove build.py
Sep 27, 2022
58fcb85
Merge branch 'feature/add_rotor_c_version' of github.com:Cypher30/Col…
Sep 27, 2022
f2f9dd2
[autoparallel] fix C extension build
Sep 27, 2022
1640d13
[autoparallel] add C solver consistency test
Sep 28, 2022
846d729
[autoparallel] remove some unused imports
Sep 29, 2022
038b833
[autoparallel] refactor rotor solver code
Sep 29, 2022
067df11
Merge branch 'hpcaitech:main' into feature/add_rotor_c_version
Cypher30 Sep 29, 2022
5904994
[autoparallel] replace print with colossalai logger
Sep 29, 2022
73d35a5
Merge branch 'feature/add_rotor_c_version' of github.com:Cypher30/Col…
Sep 29, 2022
61bec6f
[autoparallel] ranks fixed
Sep 29, 2022
832b18a
Merge branch 'hpcaitech:main' into feature/add_rotor_c_version
Cypher30 Sep 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions colossalai/fx/passes/algorithms/build_c_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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,
)
56 changes: 46 additions & 10 deletions colossalai/fx/passes/algorithms/ckpt_solver_rotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import math
from .linearize import linearize
from .operation import ForwardCheck, ForwardEnable, ForwardNograd, Backward, Loss, Chain, Sequence, Function
from colossalai.fx.passes.meta_info_prop import MetaInfoProp
from colossalai.fx.codegen.activation_checkpoint_codegen import _find_nested_ckpt_regions
from colossalai import META_COMPATIBILITY


# this is the python compute table code from rotor
Expand Down Expand Up @@ -323,34 +321,72 @@ def solver_rotor(gm: ColoGraphModule,
mem_limit: int,
mem_slots: int = 500,
cnode: List[str] = None,
eps: float = 0.0) -> ColoGraphModule:
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.
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
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 subprocess
import os
print("dynamic_programs_C_version hasn't been built! Building library...")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can replace print with colossalai logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

this_dir = os.path.dirname(os.path.abspath(__file__))
result = subprocess.Popen(
f'python {os.path.join(this_dir, "build_c_ext.py")} build_ext --build-lib={this_dir}',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
if result.wait() == 0:
print("dynamic_programs_C_version has been built!")
from .dynamic_programs_C_version import persistent_compute_table
CVERSION = True
else:
print("dynamic_programs_C_version built failed! Using python version!")
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)
mem_unit = mem_limit * (1.0 - eps) // mem_slots
if META_COMPATIBILITY:
from colossalai.fx.profiler import MetaTensor
data = MetaTensor(data, fake_device=next(gm.parameters()).device)
MetaInfoProp(gm).run(data)

# construct chain
mem_unit = mem_limit * (1.0 - eps) // mem_slots
chain: Chain = _construct_chain(node_list, data)
chain._discretize(mem_unit)
opt_table = _compute_table(chain, mem_slots)

# use C version if possible
if CVERSION and not force_python:
opt_table = persistent_compute_table(chain, mem_slots)
else:
opt_table = _compute_table(chain, mem_slots)

# found sequence
sequence = _rec(chain, 0, chain.length, mem_slots - chain.cweight[0], opt_table)
_annotate_from_sequence(sequence, node_list)

# set __sequence__ attribute to GraphModule
setattr(gm, "__sequence__", sequence)
gm.recompile()
return gm