-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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] added new linear module handler #1616
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
139 changes: 139 additions & 0 deletions
139
colossalai/auto_parallel/solver/op_handler/dot_handler_v2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from .node_handler import ModuleHandler, NodeHandler | ||
from ..sharding_strategy import ShardingStrategy_V2, StrategyGenerator_V2, OperationDataType, OperationData | ||
from typing import List, Dict | ||
from .registry import operator_registry | ||
|
||
__all__ = ['LinearModuleHandler'] | ||
|
||
|
||
class DotProductStrategyGenerator(StrategyGenerator_V2): | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
|
||
class MatVecStrategyGenerator(StrategyGenerator_V2): | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
|
||
class LinearProjectionStrategyGenerator(StrategyGenerator_V2): | ||
|
||
def update_compute_cost(self, strategy: ShardingStrategy_V2) -> ShardingStrategy_V2: | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
def update_memory_cost(self, strategy: ShardingStrategy_V2) -> ShardingStrategy_V2: | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
def generate(self, operand_mapping: Dict[str, OperationData]) -> List[ShardingStrategy_V2]: | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
def validate(self, *args, **kwargs) -> bool: | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
|
||
class BatchedMatMulStrategyGenerator(StrategyGenerator_V2): | ||
"""TODO: to be implemented""" | ||
pass | ||
|
||
|
||
@operator_registry.register(torch.nn.Linear) | ||
class LinearModuleHandler(ModuleHandler): | ||
""" | ||
A LinearModuleHandler which deals with the sharding strategies for nn.Linear module. | ||
""" | ||
|
||
def register_strategy_generator(self) -> List[StrategyGenerator_V2]: | ||
generators = [] | ||
generators.append(LinearProjectionStrategyGenerator(self.device_mesh)) | ||
return generators | ||
|
||
def get_operation_data_mapping(self) -> Dict[str, OperationData]: | ||
# use transposed shape for strategies | ||
# the strategies will be transformed back to its original shape in self.post_process | ||
physical_input_operand = OperationData(name=str(self.node.args[0]), | ||
type=OperationDataType.ARG, | ||
data=self.node.args[0]._meta_data) | ||
physical_other_operand = OperationData(name="weight", | ||
type=OperationDataType.PARAM, | ||
data=self.named_parameters['weight'], | ||
logical_shape=self.named_parameters['weight'].shape[::-1]) | ||
physical_output = OperationData(name=str(self.node), type=OperationDataType.OUTPUT, data=self.node._meta_data) | ||
|
||
mapping = {"input": physical_input_operand, "other": physical_other_operand, "output": physical_output} | ||
|
||
if self.named_parameters['bias'] is not None: | ||
physical_bias_operand = OperationData(name="bias", | ||
type=OperationDataType.PARAM, | ||
data=self.named_parameters['bias']) | ||
mapping['bias'] = physical_bias_operand | ||
return mapping | ||
|
||
def post_process(self, strategy: ShardingStrategy_V2): | ||
""" | ||
Convert the sharding spec of the weight parameter back to its original shape. | ||
""" | ||
for op_data, sharding_spec in strategy.input_sharding_specs.items(): | ||
if op_data.name == "weight": | ||
assert op_data.logical_shape != op_data.data.shape | ||
dim_partition_dict = sharding_spec.dim_partition_dict | ||
# switch first and last dim of the linear module weight | ||
dim_partition_dict[0], dim_partition_dict[-1] = dim_partition_dict[-1], dim_partition_dict[0] | ||
|
||
# re-init the sharding spec | ||
sharding_spec.__init__(sharding_spec.device_mesh, sharding_spec.entire_shape, dim_partition_dict) | ||
return strategy | ||
|
||
|
||
@operator_registry.register(F.linear) | ||
class LinearFunctionHandler(NodeHandler): | ||
""" | ||
A LinearModuleHandler which deals with the sharding strategies for nn.Linear module. | ||
""" | ||
|
||
def register_strategy_generator(self) -> List[StrategyGenerator_V2]: | ||
generators = [] | ||
generators.append(LinearProjectionStrategyGenerator(self.device_mesh)) | ||
return generators | ||
|
||
def get_operation_data_mapping(self) -> Dict[str, OperationData]: | ||
# use transposed shape for strategies | ||
# the strategies will be transformed back to its original shape in self.post_process | ||
physical_input_operand = OperationData(name=str(self.node.args[0]), | ||
type=OperationDataType.ARG, | ||
data=self.node.args[0]._meta_data) | ||
physical_other_operand = OperationData(name=str(self.node.args[1]), | ||
type=OperationDataType.ARG, | ||
data=self.node.args[1]._meta_data, | ||
logical_shape=self.node.args[1]._meta_data.shape[::-1]) | ||
physical_output = OperationData(name=str(self.node), type=OperationDataType.OUTPUT, data=self.node._meta_data) | ||
|
||
mapping = {"input": physical_input_operand, "other": physical_other_operand, "output": physical_output} | ||
|
||
if self.node.args[2] is not None: | ||
physical_bias_operand = OperationData(name=str(self.node.args[2]), | ||
type=OperationDataType.ARG, | ||
data=self.node.args[2]._meta_data) | ||
mapping['bias'] = physical_bias_operand | ||
return mapping | ||
|
||
def post_process(self, strategy: ShardingStrategy_V2): | ||
""" | ||
Convert the sharding spec of the weight parameter back to its original shape. | ||
""" | ||
for op_data, sharding_spec in strategy.input_sharding_specs.items(): | ||
if op_data.name == str(self.node.args[1]): | ||
assert op_data.logical_shape != op_data.data.shape | ||
dim_partition_dict = sharding_spec.dim_partition_dict | ||
# switch first and last dim of the linear module weight | ||
dim_partition_dict[0], dim_partition_dict[-1] = dim_partition_dict[-1], dim_partition_dict[0] | ||
|
||
# re-init the sharding spec | ||
sharding_spec.__init__(sharding_spec.device_mesh, sharding_spec.entire_shape, dim_partition_dict) | ||
return strategy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could unify all the communication related operation to CommSpec, which could give communication cost information using get_comm_cost api, and support runtime application with convert_spec_to_action. The resharding costs is estimated by ShapeConsistencyManager which uses CommSpec internally.