mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-06 19:40:28 +00:00
[misc] update pre-commit and run all files (#4752)
* [misc] update pre-commit * [misc] run pre-commit * [misc] remove useless configuration files * [misc] ignore cuda for clang-format
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
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
|
||||
from typing import List
|
||||
|
||||
import torch
|
||||
|
||||
from colossalai.tensor.shape_consistency import ShapeConsistencyManager
|
||||
from colossalai.tensor.sharding_spec import ShardingSpec
|
||||
|
||||
|
||||
def apply(*args, **kwargs):
|
||||
@@ -24,16 +21,16 @@ def solution_annotation_pass(gm: torch.fx.GraphModule, solution: List[int], devi
|
||||
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)
|
||||
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':
|
||||
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)
|
||||
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)
|
||||
@@ -51,10 +48,10 @@ def solution_annotation_pass(gm: torch.fx.GraphModule, solution: List[int], devi
|
||||
|
||||
# add above dicts into graph
|
||||
for node in nodes:
|
||||
if node.op != 'placeholder':
|
||||
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')
|
||||
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
|
||||
@@ -70,13 +67,13 @@ def shape_consistency_pass(gm: torch.fx.GraphModule):
|
||||
node_to_index_dict = {}
|
||||
index = 0
|
||||
for node in nodes:
|
||||
if node.target == 'sharding_spec_convert_dict':
|
||||
if node.target == "sharding_spec_convert_dict":
|
||||
input_dict_node = node
|
||||
continue
|
||||
if node.target == 'origin_node_sharding_spec_dict':
|
||||
if node.target == "origin_node_sharding_spec_dict":
|
||||
origin_dict_node = node
|
||||
continue
|
||||
if not hasattr(node, 'best_strategy'):
|
||||
if not hasattr(node, "best_strategy"):
|
||||
continue
|
||||
node_to_index_dict[node] = index
|
||||
index += 1
|
||||
@@ -84,28 +81,28 @@ def shape_consistency_pass(gm: torch.fx.GraphModule):
|
||||
|
||||
# add shape consistency apply function into graph
|
||||
for node in nodes:
|
||||
if not hasattr(node, 'best_strategy'):
|
||||
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]))
|
||||
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))
|
||||
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]))
|
||||
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))
|
||||
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))
|
||||
shape_consistency_node = mod_graph.create_node("call_function", apply, args=(node, sharding_spec_node))
|
||||
|
||||
return gm
|
||||
|
Reference in New Issue
Block a user