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/unstack.h"
18
19 namespace mindspore {
20 namespace ops {
Init(const int64_t axis)21 void Unstack::Init(const int64_t axis) { this->set_axis(axis); }
set_axis(const int64_t axis)22 void Unstack::set_axis(const int64_t axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
get_axis() const23 int64_t Unstack::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
UnstackInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)24 AbstractBasePtr UnstackInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
25 const std::vector<AbstractBasePtr> &input_args) {
26 MS_EXCEPTION_IF_NULL(primitive);
27 auto prim_name = primitive->name();
28 MS_EXCEPTION_IF_NULL(input_args[0]);
29 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
30 int64_t dim = SizeToLong(x_shape.size());
31 int64_t axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
32 if (axis < 0) {
33 axis = axis + dim;
34 }
35 auto output_num = x_shape[LongToSize(axis)];
36 (void)CheckAndConvertUtils::CheckInteger("output_num", output_num, kGreaterThan, 0, prim_name);
37 auto output_valid_check = x_shape[LongToSize(axis)] - output_num;
38 (void)CheckAndConvertUtils::CheckInteger("The dimension which to unstack divides output_num", output_valid_check,
39 kEqual, 0, prim_name);
40 std::vector<int64_t> infer_shape(x_shape.begin(), x_shape.begin() + axis);
41 (void)infer_shape.insert(infer_shape.end(), x_shape.begin() + axis + 1, x_shape.end());
42 AbstractBasePtrList output;
43 auto tensor_type = input_args[0]->BuildType()->cast<TensorTypePtr>();
44 MS_EXCEPTION_IF_NULL(tensor_type);
45 auto element = tensor_type->element();
46 for (int64_t i = 0; i != output_num; i++) {
47 output.push_back(std::make_shared<abstract::AbstractTensor>(element, infer_shape));
48 }
49 return std::make_shared<abstract::AbstractTuple>(output);
50 }
51 REGISTER_PRIMITIVE_C(kNameUnstack, Unstack);
52 } // namespace ops
53 } // namespace mindspore
54