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 "ops/custom_normalize.h"
18 #include "utils/check_convert_utils.h"
19 #include "abstract/primitive_infer_map.h"
20
21 namespace mindspore {
22 namespace ops {
23 namespace {
CustomNormalizeInferShape(const std::vector<AbstractBasePtr> & input_args)24 abstract::ShapePtr CustomNormalizeInferShape(const std::vector<AbstractBasePtr> &input_args) {
25 auto base_value = input_args[0]->BuildValue();
26 MS_EXCEPTION_IF_NULL(base_value);
27 auto tensor_value = base_value->cast<tensor::TensorPtr>();
28 MS_EXCEPTION_IF_NULL(tensor_value);
29 MS_EXCEPTION_IF_NULL(tensor_value->data_c());
30 std::vector<int64_t> infer_shape;
31 auto string_num = reinterpret_cast<int64_t *>(tensor_value->data_c());
32 if (*string_num == 0) {
33 infer_shape.push_back(1);
34 } else {
35 infer_shape.push_back(*string_num);
36 }
37 return std::make_shared<abstract::Shape>(infer_shape);
38 }
39
CustomNormalizeInferType(const std::vector<AbstractBasePtr> & input_args)40 TypePtr CustomNormalizeInferType(const std::vector<AbstractBasePtr> &input_args) {
41 auto infer_type = input_args[0]->BuildType();
42 auto tensor_type = infer_type->cast<TensorTypePtr>();
43 MS_EXCEPTION_IF_NULL(tensor_type);
44 auto data_type = tensor_type->element();
45 MS_EXCEPTION_IF_NULL(data_type);
46 return data_type;
47 }
48 } // namespace
49
CustomNormalizeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)50 AbstractBasePtr CustomNormalizeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
51 const std::vector<AbstractBasePtr> &input_args) {
52 MS_EXCEPTION_IF_NULL(primitive);
53 const int64_t input_num = 1;
54 CheckAndConvertUtils::CheckInputArgs(input_args, kGreaterEqual, input_num, primitive->name());
55 return std::make_shared<abstract::AbstractTensor>(CustomNormalizeInferType(input_args),
56 CustomNormalizeInferShape(input_args));
57 }
58 REGISTER_PRIMITIVE_C(kNameCustomNormalize, CustomNormalize);
59 } // namespace ops
60 } // namespace mindspore
61