mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-01 09:07:51 +00:00
[autoparallel] add experimental permute handler (#2029)
This commit is contained in:
@@ -37,30 +37,6 @@ def _solution_annotatation(gm: torch.fx.GraphModule, solution: List[int]):
|
||||
origin_node_sharding_spec_dict[node_index] = strategies_vector[strategy_index].get_sharding_spec_by_name(
|
||||
str(node))
|
||||
|
||||
# experimental pass for torch.Tensor.view
|
||||
# Arguments of view op will be divided in the sharded dimensions.
|
||||
for node in nodes:
|
||||
if node.op == 'call_method' and getattr(node.args[0]._meta_data.__class__, node.target) in (torch.Tensor.view,):
|
||||
output_dim_partition_dict = node.sharding_spec.dim_partition_dict
|
||||
device_mesh = node.sharding_spec.device_mesh
|
||||
new_args = []
|
||||
for arg in node.args:
|
||||
if isinstance(arg, Node):
|
||||
if isinstance(arg._meta_data, int):
|
||||
new_args.append(arg._meta_data)
|
||||
else:
|
||||
new_args.append(arg)
|
||||
else:
|
||||
assert isinstance(arg, int), 'The argument in view node should be either type of Node or int.'
|
||||
new_args.append(arg)
|
||||
|
||||
for dim, shard_dims in output_dim_partition_dict.items():
|
||||
total_shard_size = 1
|
||||
for shard_dim in shard_dims:
|
||||
total_shard_size *= device_mesh.shape[shard_dim]
|
||||
new_args[dim + 1] //= total_shard_size
|
||||
node.args = tuple(new_args)
|
||||
|
||||
# the dict to get input sharding specs of user node
|
||||
sharding_spec_convert_dict = {}
|
||||
# the dict to record comm actions of nodes
|
||||
@@ -113,7 +89,74 @@ def _solution_annotatation(gm: torch.fx.GraphModule, solution: List[int]):
|
||||
return gm, sharding_spec_convert_dict, origin_node_sharding_spec_dict, comm_actions_dict
|
||||
|
||||
|
||||
def _module_params_sharding(gm: torch.fx.GraphModule, device_mesh):
|
||||
def _node_args_converting(gm: torch.fx.GraphModule, device_mesh: DeviceMesh):
|
||||
"""
|
||||
This pass will process node args to adapt the distributed tensor layout.
|
||||
"""
|
||||
mod_graph = gm.graph
|
||||
nodes = tuple(mod_graph.nodes)
|
||||
|
||||
for node in nodes:
|
||||
# skip the placeholder node added in _solution_annotation pass
|
||||
if not hasattr(node, 'sharding_spec'):
|
||||
continue
|
||||
output_dim_partition_dict = node.sharding_spec.dim_partition_dict
|
||||
device_mesh = node.sharding_spec.device_mesh
|
||||
new_args = []
|
||||
|
||||
if node.op == 'call_method':
|
||||
method = getattr(node.args[0]._meta_data.__class__, node.target)
|
||||
# process the node with (input, *shape) style args
|
||||
if method in (torch.Tensor.view, torch.Tensor.reshape):
|
||||
for arg in node.args:
|
||||
if isinstance(arg, Node):
|
||||
if isinstance(arg._meta_data, int):
|
||||
new_args.append(arg._meta_data)
|
||||
else:
|
||||
new_args.append(arg)
|
||||
else:
|
||||
assert isinstance(arg, int), 'The argument in view node should be either type of Node or int.'
|
||||
new_args.append(arg)
|
||||
|
||||
for dim, shard_dims in output_dim_partition_dict.items():
|
||||
# we will skip the dim with -1 value
|
||||
if new_args[dim + 1] == -1:
|
||||
continue
|
||||
total_shard_size = 1
|
||||
for shard_dim in shard_dims:
|
||||
total_shard_size *= device_mesh.shape[shard_dim]
|
||||
new_args[dim + 1] //= total_shard_size
|
||||
node.args = tuple(new_args)
|
||||
|
||||
elif node.op == 'call_function':
|
||||
target = node.target
|
||||
# process the node with (input, torch.Size) style args
|
||||
if target in (torch.reshape,):
|
||||
for arg in node.args:
|
||||
if isinstance(arg, Node):
|
||||
if isinstance(arg._meta_data, (tuple, list)):
|
||||
new_args.append(list(arg._meta_data))
|
||||
else:
|
||||
new_args.append(arg)
|
||||
else:
|
||||
assert isinstance(
|
||||
arg, (tuple, list)), 'The argument in reshape node should be either type of Node or tuple.'
|
||||
new_args.append(list(arg))
|
||||
|
||||
for dim, shard_dims in output_dim_partition_dict.items():
|
||||
# we will skip the dim with -1 value
|
||||
if new_args[1][dim] == -1:
|
||||
continue
|
||||
total_shard_size = 1
|
||||
for shard_dim in shard_dims:
|
||||
total_shard_size *= device_mesh.shape[shard_dim]
|
||||
new_args[1][dim] //= total_shard_size
|
||||
node.args = tuple(new_args)
|
||||
|
||||
return gm
|
||||
|
||||
|
||||
def _module_params_sharding(gm: torch.fx.GraphModule, device_mesh: DeviceMesh):
|
||||
"""
|
||||
Apply the sharding action to the module parameters and buffers following the
|
||||
instructions of solver solution.
|
||||
@@ -216,6 +259,7 @@ def implicit_comm_action_apply(gm: torch.fx.GraphModule):
|
||||
def runtime_preparation_pass(gm: torch.fx.GraphModule, solution: List[int], device_mesh: DeviceMesh):
|
||||
gm, sharding_spec_convert_dict, origin_node_sharding_spec_dict, comm_actions_dict = _solution_annotatation(
|
||||
gm, solution)
|
||||
gm = _node_args_converting(gm, device_mesh)
|
||||
# TODO: the pass below should be uncommented after the implementation of implicit_comm_action_apply_pass completed.
|
||||
# gm = implicit_comm_action_apply(gm)
|
||||
gm = _module_params_sharding(gm, device_mesh)
|
||||
|
@@ -3,6 +3,7 @@ from .batch_norm_handler import BatchNormModuleHandler
|
||||
from .binary_elementwise_handler import BinaryElementwiseHandler
|
||||
from .bmm_handler import AddBMMFunctionHandler, BMMFunctionHandler
|
||||
from .conv_handler import ConvFunctionHandler, ConvModuleHandler
|
||||
from .experimental import PermuteHandler, ViewHandler
|
||||
from .getatrr_handler import GetattrHandler
|
||||
from .getitem_handler import GetItemHandler
|
||||
from .layer_norm_handler import LayerNormModuleHandler
|
||||
@@ -21,5 +22,5 @@ __all__ = [
|
||||
'LayerNormModuleHandler', 'BatchNormModuleHandler', 'ConvModuleHandler', 'ConvFunctionHandler',
|
||||
'UnaryElementwiseHandler', 'ReshapeHandler', 'PlacehodlerHandler', 'OuputHandler', 'WhereHandler',
|
||||
'NormPoolingHandler', 'BinaryElementwiseHandler', 'MatMulHandler', 'operator_registry', 'ADDMMFunctionHandler',
|
||||
'GetItemHandler', 'GetattrHandler'
|
||||
'GetItemHandler', 'GetattrHandler', 'ViewHandler', 'PermuteHandler'
|
||||
]
|
||||
|
@@ -1,4 +1,8 @@
|
||||
from .view_generator import ViewGenerator
|
||||
from .permute_handler import PermuteHandler
|
||||
from .reshape_generator import PermuteGenerator, TransposeGenerator, ViewGenerator
|
||||
from .transpose_handler import TransposeHandler
|
||||
from .view_handler import ViewHandler
|
||||
|
||||
__all__ = ['ViewGenerator', 'ViewHandler']
|
||||
__all__ = [
|
||||
'ViewGenerator', 'ViewHandler', 'PermuteGenerator', 'PermuteHandler', 'TransposeGenerator', 'TransposeGenerator'
|
||||
]
|
||||
|
@@ -0,0 +1,76 @@
|
||||
from typing import Dict, List
|
||||
|
||||
import torch
|
||||
|
||||
from ...sharding_strategy import OperationData, OperationDataType
|
||||
from ..node_handler import NodeHandler
|
||||
from ..registry import operator_registry
|
||||
from ..strategy import StrategyGenerator
|
||||
from .reshape_generator import PermuteGenerator
|
||||
|
||||
__all__ = ['PermuteHandler']
|
||||
|
||||
|
||||
@operator_registry.register(torch.Tensor.permute)
|
||||
@operator_registry.register(torch.permute)
|
||||
class PermuteHandler(NodeHandler):
|
||||
"""
|
||||
A PermuteHandler which deals with the sharding strategies for torch.permute or torch.transpose.
|
||||
"""
|
||||
|
||||
def get_strategy_generator(self) -> List[StrategyGenerator]:
|
||||
op_data_mapping = self.get_operation_data_mapping()
|
||||
generators = []
|
||||
generators.append(PermuteGenerator(op_data_mapping, self.device_mesh, self.node.args[0]))
|
||||
return generators
|
||||
|
||||
def get_operation_data_mapping(self) -> Dict[str, OperationData]:
|
||||
# check if the input operand is a parameter
|
||||
if isinstance(self.node.args[0]._meta_data, torch.nn.parameter.Parameter):
|
||||
data_type = OperationDataType.PARAM
|
||||
else:
|
||||
data_type = OperationDataType.ARG
|
||||
|
||||
input_data = self.node.args[0]._meta_data
|
||||
physical_input_operand = OperationData(name=str(self.node.args[0]), type=data_type, data=input_data)
|
||||
|
||||
permute_dims = []
|
||||
if self.node.op == 'call_method':
|
||||
# torch.Tensor.permute (input, *dims)
|
||||
for arg in self.node.args:
|
||||
if isinstance(arg, torch.fx.Node):
|
||||
if isinstance(arg._meta_data, int):
|
||||
permute_dims.append(arg._meta_data)
|
||||
else:
|
||||
assert isinstance(arg, int), 'The argument in permute node should be either type of Node or int.'
|
||||
permute_dims.append(arg)
|
||||
else:
|
||||
# torch.permute (input, dims)
|
||||
for arg in self.node.args:
|
||||
if isinstance(arg, torch.fx.Node):
|
||||
if isinstance(arg._meta_data, (tuple, list)):
|
||||
permute_dims.extend(arg._meta_data)
|
||||
else:
|
||||
assert isinstance(
|
||||
arg,
|
||||
(tuple, list)), 'The argument in permute node should be type of Node, Tuple[int] or List[int].'
|
||||
permute_dims.extend(arg)
|
||||
|
||||
num_dims = self.node._meta_data.dim()
|
||||
for i in range(num_dims):
|
||||
# recover negative value to positive
|
||||
if permute_dims[i] < 0:
|
||||
permute_dims[i] += num_dims
|
||||
|
||||
physical_shape_operand = OperationData(name='permute_dims', type=OperationDataType.ARG, data=list(permute_dims))
|
||||
|
||||
output_data = self.node._meta_data
|
||||
physical_output_operand = OperationData(name=str(self.node), type=OperationDataType.OUTPUT, data=output_data)
|
||||
|
||||
mapping = {
|
||||
"input": physical_input_operand,
|
||||
"permute_dims": physical_shape_operand,
|
||||
"output": physical_output_operand
|
||||
}
|
||||
|
||||
return mapping
|
@@ -17,12 +17,12 @@ from colossalai.auto_parallel.tensor_shard.utils import (
|
||||
from colossalai.tensor.shape_consistency import CollectiveCommPattern
|
||||
from colossalai.tensor.sharding_spec import ShardingSpec
|
||||
|
||||
__all__ = ['ViewGenerator']
|
||||
__all__ = ['ReshapeGenerator', 'ViewGenerator', 'PermuteGenerator', 'TransposeGenerator']
|
||||
|
||||
|
||||
class ViewGenerator(FollowingStrategyGenerator):
|
||||
class ReshapeGenerator(FollowingStrategyGenerator):
|
||||
"""
|
||||
ViewGenerator which deals with the sharding strategies of view op.
|
||||
ReshapeGenerator is the base class for all the reshape operation.
|
||||
"""
|
||||
|
||||
def validate(self) -> bool:
|
||||
@@ -61,6 +61,15 @@ class ViewGenerator(FollowingStrategyGenerator):
|
||||
memory_cost = TrainCycleItem(fwd=fwd_mem_cost, bwd=bwd_mem_cost, total=total_mem_cost)
|
||||
strategy.memory_cost = memory_cost
|
||||
|
||||
def collate_strategies(self) -> List[ShardingStrategy]:
|
||||
return super().collate_strategies()
|
||||
|
||||
|
||||
class ViewGenerator(ReshapeGenerator):
|
||||
"""
|
||||
ViewGenerator deals with the sharding strategies of view op.
|
||||
"""
|
||||
|
||||
def collate_strategies(self) -> List[ShardingStrategy]:
|
||||
strategy_list = []
|
||||
for index, strategy in enumerate(self.predecessor_node.strategies_vector):
|
||||
@@ -136,3 +145,85 @@ class ViewGenerator(FollowingStrategyGenerator):
|
||||
strategy_list.append(strategy)
|
||||
|
||||
return strategy_list
|
||||
|
||||
|
||||
class PermuteGenerator(ReshapeGenerator):
|
||||
"""
|
||||
PermuteGenerator deals with the sharding strategies of permute op.
|
||||
"""
|
||||
|
||||
def collate_strategies(self) -> List[ShardingStrategy]:
|
||||
strategy_list = []
|
||||
for index, strategy in enumerate(self.predecessor_node.strategies_vector):
|
||||
dim_partition_dict_mapping = {}
|
||||
communication_action_mapping = {}
|
||||
input_sharding_spec = strategy.output_sharding_specs[self.op_data["input"]]
|
||||
|
||||
permute_dims = self.op_data['permute_dims'].data
|
||||
dim_partition_dict_for_input = input_sharding_spec.dim_partition_dict
|
||||
dim_partition_dict_for_output = {}
|
||||
for dim_index, permute_dim in enumerate(permute_dims):
|
||||
if permute_dim in dim_partition_dict_for_input:
|
||||
dim_partition_dict_for_output[dim_index] = dim_partition_dict_for_input[permute_dim]
|
||||
|
||||
dim_partition_dict_mapping = {
|
||||
"input": dim_partition_dict_for_input,
|
||||
"output": dim_partition_dict_for_output,
|
||||
}
|
||||
sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping)
|
||||
|
||||
# add index into name to pass the duplicated check
|
||||
# we keep same strategies with different name for node merging, and it will not increase the searching space,
|
||||
# because in solver, this node will be merged into other nodes, and solver will not create a new variable for this node.
|
||||
name = f'{sharding_spec_mapping["input"].sharding_sequence} -> {sharding_spec_mapping["output"].sharding_sequence}_{index}'
|
||||
|
||||
strategy = self.get_sharding_strategy(name=name,
|
||||
sharding_spec_mapping=sharding_spec_mapping,
|
||||
communication_action_mapping=communication_action_mapping)
|
||||
strategy_list.append(strategy)
|
||||
|
||||
return strategy_list
|
||||
|
||||
|
||||
class TransposeGenerator(ReshapeGenerator):
|
||||
"""
|
||||
TransposeGenerator deals with the sharding strategies of permute op.
|
||||
"""
|
||||
|
||||
def collate_strategies(self) -> List[ShardingStrategy]:
|
||||
strategy_list = []
|
||||
for index, strategy in enumerate(self.predecessor_node.strategies_vector):
|
||||
dim_partition_dict_mapping = {}
|
||||
communication_action_mapping = {}
|
||||
input_sharding_spec = strategy.output_sharding_specs[self.op_data["input"]]
|
||||
dim_partition_dict_for_input = input_sharding_spec.dim_partition_dict
|
||||
dim_partition_dict_for_output = {}
|
||||
|
||||
transpose_dims = self.op_data['transpose_dims'].data
|
||||
dim_0 = transpose_dims[0]
|
||||
dim_1 = transpose_dims[1]
|
||||
for dim, sharded_dims in dim_partition_dict_for_input.items():
|
||||
if dim == dim_0:
|
||||
dim_partition_dict_for_output[dim_1] = dim_partition_dict_for_input[dim_0]
|
||||
elif dim == dim_1:
|
||||
dim_partition_dict_for_output[dim_0] = dim_partition_dict_for_input[dim_1]
|
||||
else:
|
||||
dim_partition_dict_for_output[dim] = sharded_dims
|
||||
|
||||
dim_partition_dict_mapping = {
|
||||
"input": dim_partition_dict_for_input,
|
||||
"output": dim_partition_dict_for_output,
|
||||
}
|
||||
sharding_spec_mapping = self.to_sharding_spec_mapping(dim_partition_dict_mapping)
|
||||
|
||||
# add index into name to pass the duplicated check
|
||||
# we keep same strategies with different name for node merging, and it will not increase the searching space,
|
||||
# because in solver, this node will be merged into other nodes, and solver will not create a new variable for this node.
|
||||
name = f'{sharding_spec_mapping["input"].sharding_sequence} -> {sharding_spec_mapping["output"].sharding_sequence}_{index}'
|
||||
|
||||
strategy = self.get_sharding_strategy(name=name,
|
||||
sharding_spec_mapping=sharding_spec_mapping,
|
||||
communication_action_mapping=communication_action_mapping)
|
||||
strategy_list.append(strategy)
|
||||
|
||||
return strategy_list
|
@@ -0,0 +1,65 @@
|
||||
from typing import Dict, List
|
||||
|
||||
import torch
|
||||
|
||||
from ...sharding_strategy import OperationData, OperationDataType
|
||||
from ..node_handler import NodeHandler
|
||||
from ..registry import operator_registry
|
||||
from ..strategy import StrategyGenerator
|
||||
from .reshape_generator import TransposeGenerator
|
||||
|
||||
__all__ = ['TransposeHandler']
|
||||
|
||||
|
||||
@operator_registry.register(torch.Tensor.transpose)
|
||||
@operator_registry.register(torch.transpose)
|
||||
class TransposeHandler(NodeHandler):
|
||||
"""
|
||||
A TransposeHandler which deals with the sharding strategies for torch.permute or torch.transpose.
|
||||
"""
|
||||
|
||||
def get_strategy_generator(self) -> List[StrategyGenerator]:
|
||||
op_data_mapping = self.get_operation_data_mapping()
|
||||
generators = []
|
||||
generators.append(TransposeGenerator(op_data_mapping, self.device_mesh, self.node.args[0]))
|
||||
return generators
|
||||
|
||||
def get_operation_data_mapping(self) -> Dict[str, OperationData]:
|
||||
# check if the input operand is a parameter
|
||||
if isinstance(self.node.args[0]._meta_data, torch.nn.parameter.Parameter):
|
||||
data_type = OperationDataType.PARAM
|
||||
else:
|
||||
data_type = OperationDataType.ARG
|
||||
|
||||
input_data = self.node.args[0]._meta_data
|
||||
physical_input_operand = OperationData(name=str(self.node.args[0]), type=data_type, data=input_data)
|
||||
|
||||
transpose_dims = []
|
||||
# torch.transpose (input, dim0, dim1)
|
||||
for arg in self.node.args:
|
||||
if isinstance(arg, torch.fx.Node):
|
||||
if isinstance(arg._meta_data, int):
|
||||
transpose_dims.append(arg._meta_data)
|
||||
else:
|
||||
transpose_dims.append(arg)
|
||||
|
||||
num_dims = self.node._meta_data.dim()
|
||||
for i in range(2):
|
||||
# recover negative value to positive
|
||||
if transpose_dims[i] < 0:
|
||||
transpose_dims[i] += num_dims
|
||||
|
||||
physical_shape_operand = OperationData(name='transpose_dims',
|
||||
type=OperationDataType.ARG,
|
||||
data=list(transpose_dims))
|
||||
|
||||
output_data = self.node._meta_data
|
||||
physical_output_operand = OperationData(name=str(self.node), type=OperationDataType.OUTPUT, data=output_data)
|
||||
|
||||
mapping = {
|
||||
"input": physical_input_operand,
|
||||
"transpose_dims": physical_shape_operand,
|
||||
"output": physical_output_operand
|
||||
}
|
||||
|
||||
return mapping
|
@@ -6,11 +6,13 @@ from ...sharding_strategy import OperationData, OperationDataType
|
||||
from ..node_handler import NodeHandler
|
||||
from ..registry import operator_registry
|
||||
from ..strategy import StrategyGenerator
|
||||
from .view_generator import ViewGenerator
|
||||
from .reshape_generator import ViewGenerator
|
||||
|
||||
__all__ = ['ViewHandler']
|
||||
|
||||
|
||||
@operator_registry.register(torch.Tensor.reshape)
|
||||
@operator_registry.register(torch.reshape)
|
||||
@operator_registry.register(torch.Tensor.view)
|
||||
class ViewHandler(NodeHandler):
|
||||
"""
|
||||
|
@@ -10,13 +10,9 @@ from .strategy import ReshapeGenerator, StrategyGenerator
|
||||
__all__ = ['ReshapeHandler']
|
||||
|
||||
|
||||
@operator_registry.register(torch.reshape)
|
||||
@operator_registry.register(torch.Tensor.split)
|
||||
@operator_registry.register(torch.split)
|
||||
@operator_registry.register(torch.flatten)
|
||||
@operator_registry.register(torch.Tensor.transpose)
|
||||
@operator_registry.register(torch.Tensor.permute)
|
||||
@operator_registry.register(torch.Tensor.view)
|
||||
@operator_registry.register(torch.nn.AdaptiveAvgPool2d)
|
||||
class ReshapeHandler(NodeHandler):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user