parent
bcc6e1ca28
commit
8129806475
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,69 @@
|
||||
/**
|
||||
* 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_SLICE_INFO_H_
|
||||
#define MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_SLICE_INFO_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "ir/value.h"
|
||||
#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 SliceInfo : public OperatorInfo {
|
||||
public:
|
||||
SliceInfo(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<SliceCost>(false)),
|
||||
slice_axis_(-1) {}
|
||||
~SliceInfo() override = default;
|
||||
|
||||
Status Init(const StrategyPtr &strategy) override;
|
||||
Status InitForCostModel(const StrategyPtr &strategy) override;
|
||||
Status GenerateStrategies(int64_t) override;
|
||||
Status SetCostUnderStrategy(const StrategyPtr &) override;
|
||||
std::shared_ptr<Strategys> GenerateBatchStrategies() override;
|
||||
|
||||
protected:
|
||||
Status GetAttrs() override;
|
||||
Status CheckStrategy(const StrategyPtr &strategy) override;
|
||||
Status InferMirrorOps() override;
|
||||
Status InferForwardCommunication() override { return SUCCESS; }
|
||||
Status InferTensorInfo() override;
|
||||
Status InferDevMatrixShape() override;
|
||||
Status InferTensorMap() override;
|
||||
ReplaceGraphPtr replace_graph(const CNodePtr &cnode) override;
|
||||
|
||||
private:
|
||||
Status GetInput(const ValuePtr &input_value, std::vector<int64_t> *input);
|
||||
Status ComputeReplaceGraph(const CNodePtr &cnode);
|
||||
std::vector<int64_t> begin_;
|
||||
std::vector<int64_t> size_;
|
||||
int64_t slice_axis_;
|
||||
};
|
||||
|
||||
using SliceInfoPtr = std::shared_ptr<SliceInfo>;
|
||||
} // namespace parallel
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_OPS_INFO_SLICE_INFO_H_
|
@ -0,0 +1,135 @@
|
||||
# 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 pytest
|
||||
|
||||
import mindspore as ms
|
||||
from mindspore import context, Tensor, Parameter
|
||||
from mindspore.common.api import _executor
|
||||
from mindspore.nn import Cell, TrainOneStepCell, Momentum
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
|
||||
class Net(Cell):
|
||||
def __init__(self, weight, w2, begin, end, strategy1=None, strategy2=None, is_parameter=True):
|
||||
super().__init__()
|
||||
self.mul = P.Mul().shard(strategy1)
|
||||
self.slice = P.Slice().shard(strategy2)
|
||||
if is_parameter:
|
||||
self.weight = Parameter(weight, "w1")
|
||||
else:
|
||||
self.weight = weight
|
||||
self.mul2 = P.Mul()
|
||||
self.weight2 = Parameter(w2, "w2")
|
||||
self.begin = begin
|
||||
self.end = end
|
||||
|
||||
def construct(self, x, b):
|
||||
out = self.slice(self.weight, self.begin, self.end)
|
||||
out = self.mul(x, out)
|
||||
out = self.mul2(out, self.weight2)
|
||||
return out
|
||||
|
||||
|
||||
class Net2(Cell):
|
||||
def __init__(self, weight2, begin, end, strategy1=None, strategy2=None):
|
||||
super().__init__()
|
||||
self.mul = P.Mul().shard(strategy1)
|
||||
self.slice = P.Slice().shard(strategy2)
|
||||
self.weight2 = Parameter(weight2, "w2")
|
||||
self.begin = begin
|
||||
self.end = end
|
||||
|
||||
def construct(self, x, b):
|
||||
out = self.mul(x, self.weight2)
|
||||
out = self.slice(out, self.begin, self.end)
|
||||
return out
|
||||
|
||||
|
||||
_x = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
|
||||
_w1 = Tensor(np.ones([256, 64, 32]), dtype=ms.float32)
|
||||
_w2 = Tensor(np.ones([128, 64, 1]), dtype=ms.float32)
|
||||
_b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
|
||||
|
||||
|
||||
def compile_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, _b)
|
||||
context.reset_auto_parallel_context()
|
||||
|
||||
|
||||
def test_slice_no_fully_fetch_split_error():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((2, 2, 2), (2, 2, 2))
|
||||
strategy2 = ((2, 2, 2),)
|
||||
net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=True)
|
||||
with pytest.raises(RuntimeError):
|
||||
compile_net(net)
|
||||
|
||||
def test_slice_parameter():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 4, 1), (1, 4, 2))
|
||||
strategy2 = ((1, 4, 2),)
|
||||
net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_slice_tensor():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 4, 1), (1, 4, 2))
|
||||
strategy2 = ((1, 4, 2),)
|
||||
net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=False)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_slice_parameter_no_full_split():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 4, 1), (1, 4, 2))
|
||||
strategy2 = ((1, 2, 2),)
|
||||
net = Net(_w1, _w2, (0, 0, 0), (128, 64, 32), strategy1, strategy2, is_parameter=True)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_slice_output():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 8, 1), (1, 8, 1))
|
||||
strategy2 = ((1, 8, 1),)
|
||||
net = Net2(_w2, (0, 0, 0), (64, 64, 1), strategy1, strategy2)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_stridedslice_output_no_full_split():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 8, 1), (1, 8, 1))
|
||||
strategy2 = ((1, 4, 1),)
|
||||
net = Net2(_w2, (0, 0, 0), (64, 64, 1), strategy1, strategy2)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_stridedslice_no_strategy():
|
||||
context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
|
||||
strategy1 = ((1, 8, 1), (1, 8, 1))
|
||||
strategy2 = None
|
||||
net = Net2(_w2, (0, 0, 0), (128, 64, 1), strategy1, strategy2)
|
||||
compile_net(net)
|
||||
|
||||
|
||||
def test_slice_auto_parallel():
|
||||
context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
|
||||
net = Net2(_w2, (0, 0, 0), (32, 64, 1))
|
||||
compile_net(net)
|
Loading…
Reference in new issue