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 <set>
18
19 #include "ops/l2_normalize.h"
20
21 namespace mindspore {
22 namespace ops {
Init(const std::vector<int64_t> & axis,const float epsilon)23 void L2Normalize::Init(const std::vector<int64_t> &axis, const float epsilon) {
24 this->set_axis(axis);
25 this->set_epsilon(epsilon);
26 }
27
set_axis(const std::vector<int64_t> & axis)28 void L2Normalize::set_axis(const std::vector<int64_t> &axis) { (void)AddAttr(kAxis, MakeValue(axis)); }
29
set_epsilon(const float epsilon)30 void L2Normalize::set_epsilon(const float epsilon) { (void)AddAttr(kEpsilon, MakeValue(epsilon)); }
31
get_axis() const32 std::vector<int64_t> L2Normalize::get_axis() const { return GetValue<std::vector<int64_t>>(GetAttr(kAxis)); }
33
get_epsilon() const34 float L2Normalize::get_epsilon() const {
35 auto value_ptr = GetAttr(kEpsilon);
36 return GetValue<float>(value_ptr);
37 }
38
L2NormalizeInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)39 AbstractBasePtr L2NormalizeInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
40 const std::vector<AbstractBasePtr> &input_args) {
41 MS_EXCEPTION_IF_NULL(primitive);
42 auto prim_name = primitive->name();
43 (void)CheckAndConvertUtils::CheckInteger("input number", int64_t(input_args.size()), kEqual, 1, prim_name);
44 for (const auto &item : input_args) {
45 MS_EXCEPTION_IF_NULL(item);
46 }
47 const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
48 (void)CheckAndConvertUtils::CheckTensorTypeValid("input_x", input_args[0]->BuildType(), valid_types, prim_name);
49 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
50 auto x_rank = SizeToLong(x_shape.size());
51 auto axiss = GetValue<std::vector<int64_t>>(primitive->GetAttr(kAxis));
52 for (auto &axis : axiss) {
53 CheckAndConvertUtils::CheckInRange<int64_t>("axis", axis, kIncludeLeft, {-x_rank, x_rank}, prim_name);
54 }
55 return input_args[0]->Broaden();
56 }
57 REGISTER_PRIMITIVE_C(kNameL2Normalize, L2Normalize);
58 } // namespace ops
59 } // namespace mindspore
60