1 /**
2 * Copyright 2020 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 <algorithm>
18 #include <memory>
19 #include <set>
20 #include <vector>
21
22 #include "abstract/abstract_value.h"
23 #include "abstract/dshape.h"
24 #include "abstract/ops/op_infer.h"
25 #include "abstract/ops/primitive_infer_map.h"
26 #include "abstract/utils.h"
27 #include "base/base.h"
28 #include "ir/anf.h"
29 #include "ir/dtype/number.h"
30 #include "ir/primitive.h"
31 #include "mindapi/base/shared_ptr.h"
32 #include "mindapi/ir/value.h"
33 #include "mindapi/src/helper.h"
34 #include "mindspore/core/ops/array_ops.h"
35 #include "mindspore/core/ops/math_ops.h"
36 #include "ops/op_name.h"
37 #include "ops/primitive_c.h"
38 #include "ops/reverse_sequence.h"
39 #include "utils/check_convert_utils.h"
40 #include "utils/convert_utils_base.h"
41 #include "utils/log_adapter.h"
42 #include "utils/shape_utils.h"
43
44 namespace mindspore {
45 namespace ops {
46 MIND_API_OPERATOR_IMPL(ReverseSequence, BaseOperator);
Init(const int64_t seq_dim,const int64_t batch_dim)47 void ReverseSequence::Init(const int64_t seq_dim, const int64_t batch_dim) {
48 this->set_seq_dim(seq_dim);
49 this->set_batch_dim(batch_dim);
50 }
set_seq_dim(const int64_t seq_dim)51 void ReverseSequence::set_seq_dim(const int64_t seq_dim) { (void)this->AddAttr(kSeqDim, api::MakeValue(seq_dim)); }
set_batch_dim(const int64_t batch_dim)52 void ReverseSequence::set_batch_dim(const int64_t batch_dim) {
53 (void)this->AddAttr(kBatchDim, api::MakeValue(batch_dim));
54 }
55
get_seq_dim() const56 int64_t ReverseSequence::get_seq_dim() const { return GetValue<int64_t>(GetAttr(kSeqDim)); }
get_batch_dim() const57 int64_t ReverseSequence::get_batch_dim() const {
58 auto value_ptr = this->GetAttr(kBatchDim);
59 return GetValue<int64_t>(value_ptr);
60 }
61
62 namespace {
ReverseSequenceInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)63 abstract::ShapePtr ReverseSequenceInferShape(const PrimitivePtr &primitive,
64 const std::vector<AbstractBasePtr> &input_args) {
65 MS_EXCEPTION_IF_NULL(primitive);
66 auto x_shape_ptr = CheckAndConvertUtils::GetTensorInputShape("ReverseSequence", input_args, 0);
67 MS_EXCEPTION_IF_NULL(x_shape_ptr);
68 auto seq_lengths_shape_ptr = CheckAndConvertUtils::GetTensorInputShape("ReverseSequence", input_args, 1);
69 MS_EXCEPTION_IF_NULL(seq_lengths_shape_ptr);
70 auto x_shape = x_shape_ptr->shape();
71 if (IsDynamicRank(x_shape)) {
72 return std::make_shared<abstract::Shape>(std::vector<int64_t>{-2});
73 }
74 auto seq_lengths_shape = seq_lengths_shape_ptr->shape();
75
76 auto seq_dim_ptr = primitive->GetAttr("seq_dim");
77 MS_EXCEPTION_IF_NULL(seq_dim_ptr);
78 auto seq_dim = GetValue<int64_t>(seq_dim_ptr);
79 auto batch_dim_ptr = primitive->GetAttr("batch_dim");
80 MS_EXCEPTION_IF_NULL(batch_dim_ptr);
81 auto batch_dim = GetValue<int64_t>(batch_dim_ptr);
82
83 if (seq_dim >= SizeToLong(x_shape.size()) || seq_dim < 0) {
84 MS_EXCEPTION(ValueError) << "For 'ReverseSequence', the 'seq_dim' must be in range of [0, " << x_shape.size()
85 << "), but got " << seq_dim << " with type 'int'.";
86 }
87 if (batch_dim >= SizeToLong(x_shape.size()) || batch_dim < 0) {
88 MS_EXCEPTION(ValueError) << "For 'ReverseSequence', the 'batch_dim' must be in range of [0, " << x_shape.size()
89 << "), but got " << batch_dim << " with type 'int'.";
90 }
91 if (batch_dim == seq_dim) {
92 MS_EXCEPTION(ValueError) << "For 'ReverseSequence', the 'batch_dim' should be != seq_dim: " << seq_dim
93 << ", but got " << batch_dim << ".";
94 }
95 if (seq_lengths_shape.size() != 1) {
96 MS_EXCEPTION(ValueError) << "For 'ReverseSequence', the 'seq_lengths' rank should be = expected: 1 , but got "
97 << seq_lengths_shape.size() << ".";
98 }
99 if (seq_lengths_shape[0] != x_shape[batch_dim]) {
100 MS_EXCEPTION(ValueError)
101 << "For 'ReverseSequence', the 'seq_lengths' vector size should be = input size along batch_dim: "
102 << x_shape[batch_dim] << ", but got " << seq_lengths_shape[0] << ".";
103 }
104 return std::make_shared<abstract::Shape>(x_shape);
105 }
106
ReverseSequenceInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)107 TypePtr ReverseSequenceInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
108 if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
109 MS_LOG(EXCEPTION) << "For '" << prim->name()
110 << ", the input args used for infer shape and type is necessary, but missing it.";
111 }
112 const std::set<TypePtr> seq_lengths_valid_types = {kInt32, kInt64};
113 (void)CheckAndConvertUtils::CheckTensorTypeValid("seq_lengths", input_args[1]->GetType(), seq_lengths_valid_types,
114 prim->name());
115
116 const std::set<TypePtr> x_valid_types = {kFloat16, kFloat32, kFloat64, kUInt8, kUInt16, kUInt32, kUInt64,
117 kInt8, kInt16, kInt32, kInt64, kComplex64, kComplex128, kBool};
118 return CheckAndConvertUtils::CheckTensorTypeValid("x", input_args[0]->GetType(), x_valid_types, prim->name());
119 }
120 } // namespace
ReverseSequenceInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)121 AbstractBasePtr ReverseSequenceInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
122 const std::vector<AbstractBasePtr> &input_args) {
123 MS_EXCEPTION_IF_NULL(primitive);
124 const int64_t input_num = 2;
125 CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
126 auto infer_type = ReverseSequenceInferType(primitive, input_args);
127 auto infer_shape = ReverseSequenceInferShape(primitive, input_args);
128 return abstract::MakeAbstract(infer_shape, infer_type);
129 }
130
131 // AG means auto generated
132 class MIND_API AGReverseSequenceInfer : public abstract::OpInferBase {
133 public:
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const134 BaseShapePtr InferShape(const PrimitivePtr &primitive,
135 const std::vector<AbstractBasePtr> &input_args) const override {
136 return ReverseSequenceInferShape(primitive, input_args);
137 }
138
InferType(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const139 TypePtr InferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const override {
140 return ReverseSequenceInferType(primitive, input_args);
141 }
InferShapeAndType(const abstract::AnalysisEnginePtr & engine,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const142 AbstractBasePtr InferShapeAndType(const abstract::AnalysisEnginePtr &engine, const PrimitivePtr &primitive,
143 const std::vector<AbstractBasePtr> &input_args) const override {
144 return ReverseSequenceInfer(engine, primitive, input_args);
145 }
146 };
147
148 REGISTER_PRIMITIVE_OP_INFER_IMPL(ReverseSequence, prim::kPrimReverseSequence, AGReverseSequenceInfer, false);
149 } // namespace ops
150 } // namespace mindspore
151