1 /**
2 * Copyright 2020-2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ops/fusion/slice_fusion.h"
18 #include <string>
19 #include "ops/op_utils.h"
20
21 namespace mindspore {
22 namespace ops {
Init(const std::vector<int64_t> & axes)23 void SliceFusion::Init(const std::vector<int64_t> &axes) { this->set_axes(axes); }
24
set_axes(const std::vector<int64_t> & axes)25 void SliceFusion::set_axes(const std::vector<int64_t> &axes) { (void)this->AddAttr(kAxes, MakeValue(axes)); }
26
get_axes() const27 std::vector<int64_t> SliceFusion::get_axes() const {
28 auto value_ptr = GetAttr(kAxes);
29 MS_EXCEPTION_IF_NULL(value_ptr);
30 return GetValue<std::vector<int64_t>>(value_ptr);
31 }
32
SliceFusionInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)33 AbstractBasePtr SliceFusionInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
34 const std::vector<AbstractBasePtr> &input_args) {
35 MS_EXCEPTION_IF_NULL(primitive);
36 auto op_name = primitive->name();
37 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
38 auto x_shape_len = x_shape.size();
39 auto begin_v = input_args[kInputIndex1]->BuildValue();
40 auto size_v = input_args[kInputIndex2]->BuildValue();
41 auto x_type = input_args[kInputIndex0]->BuildType();
42 MS_EXCEPTION_IF_NULL(x_type);
43 MS_EXCEPTION_IF_NULL(begin_v);
44 MS_EXCEPTION_IF_NULL(size_v);
45 auto tensor_type = x_type->cast<TensorTypePtr>();
46 MS_EXCEPTION_IF_NULL(tensor_type);
47 auto data_type = tensor_type->element();
48 MS_EXCEPTION_IF_NULL(data_type);
49 if (begin_v == kAnyValue || size_v == kAnyValue) {
50 return std::make_shared<abstract::AbstractTensor>(data_type, std::vector<int64_t>{});
51 }
52 auto begin = GetValue<std::vector<int64_t>>(begin_v);
53 auto size = GetValue<std::vector<int64_t>>(size_v);
54 CheckAndConvertUtils::Check("len of begin", (int64_t)begin.size(), kEqual, "len x's dim", SizeToLong(x_shape_len));
55 CheckAndConvertUtils::Check("len of size", (int64_t)size.size(), kEqual, "len x's dim", SizeToLong(x_shape_len));
56
57 for (size_t i = 0; i < x_shape_len; i++) {
58 (void)CheckAndConvertUtils::CheckInteger("input size[" + std::to_string(i) + "]", size[i], kGreaterThan, 0, "");
59 if (x_shape[i] < (begin[i] + size[i])) {
60 auto y = begin[i] + size[i];
61 MS_EXCEPTION(ValueError) << "For " + op_name + "slice shape can't bigger than origin shape " +
62 std::to_string(x_shape[i]) + "," + std::to_string(y);
63 }
64 }
65 return std::make_shared<abstract::AbstractTensor>(data_type, size);
66 }
67 REGISTER_PRIMITIVE_C(kNameSliceFusion, SliceFusion);
68 } // namespace ops
69 } // namespace mindspore
70