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/leaky_relu.h"
18
19 namespace mindspore {
20 namespace ops {
21 namespace {
InferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)22 abstract::ShapePtr InferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
23 MS_EXCEPTION_IF_NULL(primitive);
24 auto x = input_args[0]->BuildShape();
25 auto shape_element = x->cast<abstract::ShapePtr>();
26 MS_EXCEPTION_IF_NULL(shape_element);
27 return shape_element;
28 }
29
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)30 TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
31 MS_EXCEPTION_IF_NULL(prim);
32 (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim->name());
33 for (const auto &item : input_args) {
34 MS_EXCEPTION_IF_NULL(item);
35 }
36 std::map<std::string, TypePtr> types;
37 (void)types.emplace("x", input_args[0]->BuildType());
38 return CheckAndConvertUtils::CheckTensorTypeSame(types, common_valid_types, prim->name());
39 }
40 } // namespace
Init(const float negative_slope)41 void LeakyRelu::Init(const float negative_slope) { this->set_negative_slope(negative_slope); }
42
set_negative_slope(const float negative_slope)43 void LeakyRelu::set_negative_slope(const float negative_slope) {
44 (void)this->AddAttr(kNegativeSlope, MakeValue(negative_slope));
45 }
get_negative_slope() const46 float LeakyRelu::get_negative_slope() const { return GetValue<float>(GetAttr(kNegativeSlope)); }
47
LeakyReluInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)48 AbstractBasePtr LeakyReluInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
49 const std::vector<AbstractBasePtr> &input_args) {
50 return std::make_shared<abstract::AbstractTensor>(InferType(primitive, input_args),
51 InferShape(primitive, input_args));
52 }
53 REGISTER_PRIMITIVE_C(kNameLeakyRelu, LeakyRelu);
54 } // namespace ops
55 } // namespace mindspore
56