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/shape.h"
18 #include <string>
19 #include <algorithm>
20 #include <memory>
21 #include <set>
22 #include <vector>
23 #include "ops/op_utils.h"
24 #include "utils/check_convert_utils.h"
25 #include "abstract/primitive_infer_map.h"
26
27 namespace mindspore {
28 namespace ops {
ShapeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)29 AbstractBasePtr ShapeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
30 const std::vector<AbstractBasePtr> &input_args) {
31 // infer shape
32 MS_EXCEPTION_IF_NULL(primitive);
33 auto op_name = primitive->name();
34 (void)CheckAndConvertUtils::CheckInteger("shape infer", int64_t(input_args.size()), kEqual, 1, op_name);
35 MS_EXCEPTION_IF_NULL(input_args[0]);
36 auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape());
37 auto in_shape = shape_map[kShape];
38 // infer type
39 std::set<TypePtr> valid_params_types = {kTensorType};
40 (void)CheckAndConvertUtils::CheckSubClass("shape type", input_args[0]->BuildType(), valid_params_types, op_name);
41 AbstractBasePtrList abs_list;
42 (void)std::transform(in_shape.begin(), in_shape.end(), std::back_inserter(abs_list),
43 [](int64_t item) -> std::shared_ptr<abstract::AbstractScalar> {
44 return std::make_shared<abstract::AbstractScalar>(item);
45 });
46 auto abs = std::make_shared<abstract::AbstractTuple>(abs_list);
47 return abs;
48 }
49
ShapeInferValue(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)50 ValuePtr ShapeInferValue(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
51 MS_EXCEPTION_IF_NULL(primitive);
52 auto op_name = primitive->name();
53 (void)CheckAndConvertUtils::CheckInteger("shape infer", int64_t(input_args.size()), kEqual, 1, op_name);
54 MS_EXCEPTION_IF_NULL(input_args[0]);
55 std::set<TypePtr> valid_params_types = {kTensorType};
56 (void)CheckAndConvertUtils::CheckSubClass("shape type", input_args[0]->BuildType(), valid_params_types, op_name);
57 auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape());
58 auto inshape = shape_map[kShape];
59 auto value = MakeValue(inshape);
60 return value;
61 }
62 REGISTER_PRIMITIVE_EVAL_IMPL(Shape, prim::kPrimShape, ShapeInfer, ShapeInferValue, false);
63 } // namespace ops
64 } // namespace mindspore
65