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/transpose.h"
18 #include <vector>
19 #include <memory>
20 #include <algorithm>
21 #include "ops/op_utils.h"
22 #include "utils/check_convert_utils.h"
23 #include "abstract/primitive_infer_map.h"
24
25 namespace mindspore {
26 namespace ops {
27 namespace {
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)28 abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
29 MS_EXCEPTION_IF_NULL(primitive);
30 auto op_name = primitive->name();
31 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
32 auto x_min_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kMinShape];
33 auto x_max_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kMaxShape];
34 ShapeVector p_value;
35 if (input_args.size() == 1) {
36 ValuePtr perm = primitive->GetAttr("perm");
37 auto perm_val = perm->cast<ValueTuplePtr>();
38 MS_EXCEPTION_IF_NULL(perm_val);
39 auto perm_val_data = perm_val->value();
40 (void)std::transform(std::begin(perm_val_data), std::end(perm_val_data), std::back_inserter(p_value),
41 [](const ValuePtr &e) -> int64_t { return GetValue<int64_t>(e); });
42 } else {
43 auto perm_value = input_args[1]->BuildValue();
44 MS_EXCEPTION_IF_NULL(perm_value);
45 if (perm_value->isa<tensor::Tensor>()) {
46 p_value = CheckAndConvertUtils::CheckTensorIntValue("perm value", perm_value, op_name);
47 } else {
48 p_value = CheckAndConvertUtils::CheckAttrTupleInt("perm value", perm_value, op_name);
49 }
50 }
51 if (x_shape.size() != p_value.size()) {
52 MS_EXCEPTION(ValueError) << "The dimension of x " << x_shape.size() << " and perm " << p_value.size()
53 << " must be equal.";
54 }
55 for (auto i : p_value) {
56 (void)CheckAndConvertUtils::CheckInteger("perm element", i, kGreaterEqual, 0, op_name);
57 (void)CheckAndConvertUtils::CheckInteger("perm element", i, kLessThan, SizeToLong(p_value.size()), op_name);
58 }
59 std::vector<int64_t> tmp(p_value);
60 for (auto it = tmp.begin(); it != tmp.end();) {
61 auto dim = *it;
62 if (!tmp.empty()) {
63 it = tmp.erase(it);
64 }
65 if (std::find(tmp.begin(), tmp.end(), dim) != tmp.end()) {
66 MS_EXCEPTION(ValueError) << "The value of perm is wrong";
67 }
68 }
69 std::vector<int64_t> in_shape(p_value);
70 (void)std::transform(in_shape.begin(), in_shape.end(), in_shape.begin(), [x_shape](size_t i) { return x_shape[i]; });
71 if (!x_min_shape.empty() && !x_max_shape.empty()) {
72 std::vector<int64_t> min_shape;
73 std::vector<int64_t> max_shape;
74 for (auto i : p_value) {
75 min_shape.push_back(x_min_shape[LongToSize(i)]);
76 max_shape.push_back(x_max_shape[LongToSize(i)]);
77 }
78 return std::make_shared<abstract::Shape>(in_shape, min_shape, max_shape);
79 } else {
80 return std::make_shared<abstract::Shape>(in_shape);
81 }
82 }
83
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)84 TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
85 MS_EXCEPTION_IF_NULL(prim);
86 return CheckAndConvertUtils::CheckSubClass("x", input_args[0]->BuildType(), {kTensorType}, prim->name());
87 }
88 } // namespace
89
TransposeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)90 AbstractBasePtr TransposeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
91 const std::vector<AbstractBasePtr> &input_args) {
92 MS_EXCEPTION_IF_NULL(primitive);
93 (void)CheckAndConvertUtils::CheckInteger("Transpose infer", SizeToLong(input_args.size()), kGreaterEqual, 1,
94 primitive->name());
95 auto type = InferType(primitive, input_args);
96 auto shape = InferShape(primitive, input_args);
97 return abstract::MakeAbstract(shape, type);
98 }
99 REGISTER_PRIMITIVE_EVAL_IMPL(Transpose, prim::kPrimTranspose, TransposeInfer, nullptr, true);
100 } // namespace ops
101 } // namespace mindspore
102