1 /**
2 * Copyright 2022 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/map_cache_idx.h"
18
19 #include <map>
20 #include <set>
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/container.h"
30 #include "ir/dtype/number.h"
31 #include "ir/primitive.h"
32 #include "mindapi/src/helper.h"
33 #include "mindspore/core/ops/array_ops.h"
34 #include "ops/op_name.h"
35 #include "ops/primitive_c.h"
36 #include "utils/check_convert_utils.h"
37 #include "utils/log_adapter.h"
38 #include "utils/shape_utils.h"
39
40 namespace mindspore {
41 namespace ops {
42 namespace {
MapCacheIdxInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)43 abstract::TupleShapePtr MapCacheIdxInferShape(const PrimitivePtr &primitive,
44 const std::vector<AbstractBasePtr> &input_args) {
45 MS_EXCEPTION_IF_NULL(primitive);
46 auto prim_name = primitive->name();
47 constexpr int64_t kInputNum = 5;
48 CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, kInputNum, prim_name);
49 auto hashmap_shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->GetShape());
50 auto hashmap_shape = hashmap_shape_map[kShape];
51 auto indices_shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->GetShape());
52 auto indices_shape = indices_shape_map[kShape];
53
54 auto cache_idx_output = std::make_shared<abstract::Shape>(indices_shape);
55 auto other_output = std::make_shared<abstract::Shape>(std::vector<int64_t>{abstract::Shape::kShapeRankAny});
56 if (IsDynamicRank(indices_shape)) {
57 return std::make_shared<abstract::TupleShape>(
58 std::vector<abstract::BaseShapePtr>{cache_idx_output, other_output, other_output, other_output});
59 }
60
61 const size_t hashmap_shape_size = 2;
62 if (hashmap_shape.size() != hashmap_shape_size) {
63 MS_EXCEPTION(ValueError) << "For '" << prim_name << "',"
64 << " the dimension of hashmap must be equal to 2, but got: " << hashmap_shape.size()
65 << ".";
66 }
67 return std::make_shared<abstract::TupleShape>(
68 std::vector<abstract::BaseShapePtr>{cache_idx_output, other_output, other_output, other_output});
69 }
70
MapCacheIdxInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)71 TuplePtr MapCacheIdxInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
72 MS_EXCEPTION_IF_NULL(prim);
73 auto prim_name = prim->name();
74 constexpr int64_t kInputNum = 5;
75 CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, kInputNum, prim_name);
76 auto hashmap_type = input_args[0]->GetType();
77 auto indices_type = input_args[1]->GetType();
78
79 const std::set<TypePtr> valid_types = {kInt64, kInt32, kInt16, kInt8};
80 std::map<std::string, TypePtr> input_types;
81 (void)input_types.emplace("hashmap", hashmap_type);
82 (void)input_types.emplace("indices", indices_type);
83 (void)CheckAndConvertUtils::CheckTensorTypeSame(input_types, valid_types, prim_name);
84 return std::make_shared<Tuple>(std::vector<TypePtr>{hashmap_type, hashmap_type, hashmap_type, hashmap_type});
85 }
86 } // namespace
87
MapCacheIdxInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)88 AbstractBasePtr MapCacheIdxInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
89 const std::vector<AbstractBasePtr> &input_args) {
90 auto infer_type = MapCacheIdxInferType(primitive, input_args);
91 auto infer_shape = MapCacheIdxInferShape(primitive, input_args);
92 return abstract::MakeAbstract(infer_shape, infer_type);
93 }
94
95 MIND_API_OPERATOR_IMPL(MapCacheIdx, BaseOperator);
96
97 // AG means auto generated
98 class MIND_API AGMapCacheIdxInfer : public abstract::OpInferBase {
99 public:
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const100 BaseShapePtr InferShape(const PrimitivePtr &primitive,
101 const std::vector<AbstractBasePtr> &input_args) const override {
102 return MapCacheIdxInferShape(primitive, input_args);
103 }
104
InferType(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const105 TypePtr InferType(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) const override {
106 return MapCacheIdxInferType(primitive, input_args);
107 }
InferShapeAndType(const abstract::AnalysisEnginePtr & engine,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args) const108 AbstractBasePtr InferShapeAndType(const abstract::AnalysisEnginePtr &engine, const PrimitivePtr &primitive,
109 const std::vector<AbstractBasePtr> &input_args) const override {
110 return MapCacheIdxInfer(engine, primitive, input_args);
111 }
112 };
113
114 REGISTER_PRIMITIVE_OP_INFER_IMPL(MapCacheIdx, prim::kPrimMapCacheIdx, AGMapCacheIdxInfer, false);
115 } // namespace ops
116 } // namespace mindspore
117