!7437 auto parallel support dynamic
Merge pull request !7437 from yao_yf/auto_parallel_support_dynamic_shapepull/7437/MERGE
commit
d4142d682d
@ -0,0 +1,192 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "frontend/parallel/ops_info/unique_info.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <memory>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "ir/value.h"
|
||||||
|
#include "frontend/parallel/device_matrix.h"
|
||||||
|
#include "frontend/parallel/strategy.h"
|
||||||
|
#include "frontend/parallel/context.h"
|
||||||
|
#include "frontend/parallel/tensor_layout/tensor_redistribution.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace parallel {
|
||||||
|
/*
|
||||||
|
* unique has one input, two outputs. Currently, unique cannot be split.
|
||||||
|
*/
|
||||||
|
Status UniqueInfo::InferTensorMap() {
|
||||||
|
MS_EXCEPTION_IF_NULL(ParallelContext::GetInstance());
|
||||||
|
for (auto shp : inputs_shape_) {
|
||||||
|
TensorMap out_tensor_map;
|
||||||
|
TensorMap in_tensor_map;
|
||||||
|
for (size_t i = 0; i < shp.size(); ++i) {
|
||||||
|
in_tensor_map.push_back(MAP_NONE);
|
||||||
|
out_tensor_map.push_back(MAP_NONE);
|
||||||
|
}
|
||||||
|
inputs_tensor_map_.push_back(in_tensor_map);
|
||||||
|
outputs_tensor_map_.push_back(out_tensor_map);
|
||||||
|
outputs_tensor_map_.push_back(out_tensor_map);
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::InferTensorLayout(TensorLayouts *inputs_layout, TensorLayouts *outputs_layout) {
|
||||||
|
if (inputs_layout == nullptr || outputs_layout == nullptr) {
|
||||||
|
MS_LOG(ERROR) << name_ << " : The layout is null.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
TensorLayout input_layout;
|
||||||
|
TensorLayout output_layout;
|
||||||
|
TensorLayout index_layout;
|
||||||
|
if ((input_layout.InitFromVector(dev_matrix_shape_, inputs_tensor_map_[0], inputs_shape_[0]) != SUCCESS) ||
|
||||||
|
(output_layout.InitFromVector(dev_matrix_shape_, outputs_tensor_map_[0], outputs_shape_[0]) != SUCCESS) ||
|
||||||
|
(index_layout.InitFromVector(dev_matrix_shape_, outputs_tensor_map_[1], outputs_shape_[1]) != SUCCESS)) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
inputs_layout->push_back(input_layout);
|
||||||
|
outputs_layout->push_back(output_layout);
|
||||||
|
outputs_layout->push_back(index_layout);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::InferTensorInfo() {
|
||||||
|
TensorLayouts inputs_layout;
|
||||||
|
TensorLayouts outputs_layout;
|
||||||
|
if (InferTensorLayout(&inputs_layout, &outputs_layout) != SUCCESS) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < inputs_layout.size(); ++i) {
|
||||||
|
TensorInfo input_tensor_info(inputs_layout[i]);
|
||||||
|
inputs_tensor_info_.push_back(input_tensor_info);
|
||||||
|
}
|
||||||
|
for (size_t i = 0; i < outputs_layout.size(); ++i) {
|
||||||
|
TensorInfo output_tensor_info(outputs_layout[i]);
|
||||||
|
outputs_tensor_info_.push_back(output_tensor_info);
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::InferDevMatrixShape() {
|
||||||
|
dev_matrix_shape_.push_back(dev_num_);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::Init(const StrategyPtr &strategy) {
|
||||||
|
if (InitWithAutoRepeatCalc(strategy) != SUCCESS) {
|
||||||
|
MS_LOG(ERROR) << name_ << " : Init failed";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << name_ << " : Init success";
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::CheckStrategy(const StrategyPtr &strategy) {
|
||||||
|
Strategys stras = strategy->GetInputDim();
|
||||||
|
if (CheckStrategyValue(strategy, inputs_shape_) != SUCCESS) {
|
||||||
|
MS_LOG(ERROR) << name_ << ": Invalid strategy.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
for (Dimensions stra : stras) {
|
||||||
|
if (stra.size() != UNIQUE_INPUT_SIZE) {
|
||||||
|
MS_LOG(ERROR) << name_ << " : Invalid strategy.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int32_t stage = strategy->GetInputStage();
|
||||||
|
int32_t dev_num = SizeToInt(g_device_manager->GetDeviceListByStageId(stage).size());
|
||||||
|
dev_num_ = dev_num;
|
||||||
|
if (stras[0][0] != 1) {
|
||||||
|
MS_LOG(ERROR) << "Currently, unique only support repeat calculate in all devices";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::GetAttrs() {
|
||||||
|
if ((inputs_shape_.size() != UNIQUE_INPUTS_SIZE) || (outputs_shape_.size() != UNIQUE_OUTPUTS_SIZE)) {
|
||||||
|
MS_LOG(ERROR) << name_ << ": Inputs shape size " << inputs_shape_.size() << " or outputs shape size "
|
||||||
|
<< outputs_shape_.size() << " is wrong.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::InferMirrorOps() {
|
||||||
|
mirror_ops_.clear();
|
||||||
|
|
||||||
|
Shape tensor_map = inputs_tensor_map_[0];
|
||||||
|
std::vector<Group> group;
|
||||||
|
if (CreateGroupByTensorMap(tensor_map, &group) != SUCCESS) {
|
||||||
|
MS_LOG(ERROR) << name_ << " : Create group failed.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
OperatorVector mirror_op;
|
||||||
|
if (group.empty()) {
|
||||||
|
MS_LOG(INFO) << name_ << " : The mirror ops is empty.";
|
||||||
|
return SUCCESS;
|
||||||
|
} else {
|
||||||
|
mirror_op = CreateMirrorOps(group[0].name(), group[0].GetDevNum());
|
||||||
|
mirror_ops_.push_back(mirror_op);
|
||||||
|
std::string group_name = group[0].name();
|
||||||
|
MS_LOG(INFO) << name_ << " : Create the mirror ops success, the group name is " << group_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::InitForCostModel(const StrategyPtr &strategy) {
|
||||||
|
if (InitForCostModelWithAutoRepeatCalc(strategy) != SUCCESS) {
|
||||||
|
MS_LOG(ERROR) << name_ << " : Init for cost model failed.";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
MS_LOG(INFO) << name_ << " : Init for cost model success.";
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status UniqueInfo::SetCostUnderStrategy(const StrategyPtr &strategy) { return SetCostUnderStrategyBase(strategy); }
|
||||||
|
|
||||||
|
Status UniqueInfo::GenerateStrategies(int32_t stage_id) {
|
||||||
|
if (inputs_shape_.size() != UNIQUE_INPUTS_SIZE) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
if (inputs_shape_[0].size() != UNIQUE_INPUT_SIZE) {
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
Shape input0_split;
|
||||||
|
input0_split.emplace_back(0);
|
||||||
|
Shapes splittable_inputs = {input0_split};
|
||||||
|
std::vector<StrategyPtr> sp_vector;
|
||||||
|
if (GenerateStrategiesForIndependentInputs(stage_id, inputs_shape_, splittable_inputs, &sp_vector) != SUCCESS) {
|
||||||
|
MS_LOG(ERROR) << name_ << ": GenerateStrategiesForIndependentInputs failed";
|
||||||
|
return FAILED;
|
||||||
|
}
|
||||||
|
size_t success = 0;
|
||||||
|
for (auto &sp : sp_vector) {
|
||||||
|
if (SetCostUnderStrategy(sp) == SUCCESS) {
|
||||||
|
success++;
|
||||||
|
MS_LOG(INFO) << name_ << ": Successfully generated " << success << " strategy.";
|
||||||
|
PrintStrategy(sp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
} // namespace parallel
|
||||||
|
} // namespace mindspore
|
@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_UNIQUE_INFO_H_
|
||||||
|
#define MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_UNIQUE_INFO_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "frontend/parallel/auto_parallel/operator_costmodel.h"
|
||||||
|
#include "frontend/parallel/ops_info/operator_info.h"
|
||||||
|
#include "frontend/parallel/strategy.h"
|
||||||
|
|
||||||
|
namespace mindspore {
|
||||||
|
namespace parallel {
|
||||||
|
class UniqueInfo : public OperatorInfo {
|
||||||
|
public:
|
||||||
|
UniqueInfo(const std::string &operator_name, const Shapes &inputs_shape, const Shapes &outputs_shape,
|
||||||
|
const PrimitiveAttrs &attrs)
|
||||||
|
: OperatorInfo(operator_name, inputs_shape, outputs_shape, attrs, std::make_shared<GetNextCost>(false)) {}
|
||||||
|
~UniqueInfo() override = default;
|
||||||
|
|
||||||
|
Status Init(const StrategyPtr &strategy) override;
|
||||||
|
Status SetCostUnderStrategy(const StrategyPtr &strategy) override;
|
||||||
|
Status InitForCostModel(const StrategyPtr &strategy) override;
|
||||||
|
Status GenerateStrategies(int32_t stage_id) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Status CheckStrategy(const StrategyPtr &strategy) override;
|
||||||
|
Status GetAttrs() override;
|
||||||
|
Status InferTensorMap() override;
|
||||||
|
Status InferTensorLayout(TensorLayouts *inputs_layout, TensorLayouts *outputs_layout);
|
||||||
|
Status InferTensorInfo() override;
|
||||||
|
Status InferDevMatrixShape() override;
|
||||||
|
Status InferMirrorOps() override;
|
||||||
|
Status InferForwardCommunication() override { return SUCCESS; }
|
||||||
|
Status InferAsLossDivisor() override { return SUCCESS; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int32_t dev_num_ = 1;
|
||||||
|
};
|
||||||
|
} // namespace parallel
|
||||||
|
} // namespace mindspore
|
||||||
|
|
||||||
|
#endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_UNIQUE_INFO_H_
|
@ -0,0 +1,118 @@
|
|||||||
|
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
import mindspore as ms
|
||||||
|
import mindspore.nn as nn
|
||||||
|
from mindspore import Tensor
|
||||||
|
from mindspore import context
|
||||||
|
from mindspore.common.api import _executor
|
||||||
|
from mindspore.common.parameter import Parameter
|
||||||
|
from mindspore.ops import composite as C
|
||||||
|
from mindspore.ops import operations as P
|
||||||
|
from mindspore.common.initializer import initializer
|
||||||
|
from mindspore.nn import TrainOneStepCell, Momentum
|
||||||
|
from tests.ut.python.ops.test_math_ops import VirtualLoss
|
||||||
|
|
||||||
|
|
||||||
|
grad_all = C.GradOperation(get_all=True)
|
||||||
|
|
||||||
|
|
||||||
|
class NetWithLoss(nn.Cell):
|
||||||
|
def __init__(self, network):
|
||||||
|
super(NetWithLoss, self).__init__()
|
||||||
|
self.loss = VirtualLoss()
|
||||||
|
self.network = network
|
||||||
|
|
||||||
|
def construct(self, x):
|
||||||
|
predict = self.network(x)
|
||||||
|
return self.loss(predict)
|
||||||
|
|
||||||
|
|
||||||
|
class GradWrap(nn.Cell):
|
||||||
|
def __init__(self, network):
|
||||||
|
super(GradWrap, self).__init__()
|
||||||
|
self.network = network
|
||||||
|
|
||||||
|
def construct(self, x):
|
||||||
|
return grad_all(self.network)(x)
|
||||||
|
|
||||||
|
def test_unique_column_split():
|
||||||
|
class Net(nn.Cell):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.unique = P.Unique().shard(((1,),))
|
||||||
|
self.relu = P.ReLU()
|
||||||
|
self.mul = P.Mul()
|
||||||
|
self.embedding_lookp = P.GatherV2().shard(((1, 8), (1,)))
|
||||||
|
self.embedding_table = Parameter(initializer('normal', [2000, 128]),
|
||||||
|
name='embedding_table')
|
||||||
|
self.gatherv2 = P.GatherV2().shard(((1, 8), (1,)))
|
||||||
|
self.reshape = P.Reshape()
|
||||||
|
self.matmul = P.MatMul()
|
||||||
|
self.mul_weight = Parameter(Tensor(np.full([32, 64, 1], 0.5, dtype=np.float32)), name="mul_weight")
|
||||||
|
|
||||||
|
def construct(self, indices):
|
||||||
|
indices_flatten = self.reshape(indices, (-1,))
|
||||||
|
unique_id, unique_idx = self.unique(indices_flatten)
|
||||||
|
unique_id_weight = self.embedding_lookp(self.embedding_table, unique_id, 0)
|
||||||
|
weight_flatten = self.gatherv2(unique_id_weight, unique_idx, 0)
|
||||||
|
weight = self.reshape(weight_flatten, (32, 64, 128))
|
||||||
|
vx = self.mul(weight, self.mul_weight)
|
||||||
|
return vx
|
||||||
|
|
||||||
|
size = 8
|
||||||
|
context.set_auto_parallel_context(device_num=size, global_rank=0, parallel_mode="auto_parallel")
|
||||||
|
x = Tensor(np.ones([32, 64]), dtype=ms.int32)
|
||||||
|
net = Net()
|
||||||
|
optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
|
||||||
|
train_net = TrainOneStepCell(net, optimizer)
|
||||||
|
train_net.set_auto_parallel()
|
||||||
|
train_net.set_train()
|
||||||
|
_executor.compile(train_net, x)
|
||||||
|
|
||||||
|
def test_unique_row_split():
|
||||||
|
class Net(nn.Cell):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self.unique = P.Unique().shard(((1,),))
|
||||||
|
self.relu = P.ReLU()
|
||||||
|
self.mul = P.Mul()
|
||||||
|
self.embedding_lookp = P.GatherV2().shard(((8, 1), (1,)))
|
||||||
|
self.embedding_table = Parameter(initializer('normal', [2000, 128]),
|
||||||
|
name='embedding_table')
|
||||||
|
self.gatherv2 = P.GatherV2().shard(((1, 1), (8,)))
|
||||||
|
self.reshape = P.Reshape()
|
||||||
|
self.matmul = P.MatMul()
|
||||||
|
self.mul_weight = Parameter(Tensor(np.full([32, 64, 1], 0.5, dtype=np.float32)), name="mul_weight")
|
||||||
|
|
||||||
|
def construct(self, indices):
|
||||||
|
indices_flatten = self.reshape(indices, (-1,))
|
||||||
|
unique_id, unique_idx = self.unique(indices_flatten)
|
||||||
|
unique_id_weight = self.embedding_lookp(self.embedding_table, unique_id, 0)
|
||||||
|
weight_flatten = self.gatherv2(unique_id_weight, unique_idx, 0)
|
||||||
|
weight = self.reshape(weight_flatten, (32, 64, 128))
|
||||||
|
vx = self.mul(weight, self.mul_weight)
|
||||||
|
return vx
|
||||||
|
|
||||||
|
size = 8
|
||||||
|
context.set_auto_parallel_context(device_num=size, global_rank=0, parallel_mode="stand_alone")
|
||||||
|
x = Tensor(np.ones([32, 64]), dtype=ms.int32)
|
||||||
|
net = Net()
|
||||||
|
optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
|
||||||
|
train_net = TrainOneStepCell(net, optimizer)
|
||||||
|
train_net.set_auto_parallel()
|
||||||
|
train_net.set_train()
|
||||||
|
_executor.compile(train_net, x)
|
Loading…
Reference in new issue