• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <map>
19 #include <string>
20 
21 #include "ops/grad/binary_cross_entropy_grad.h"
22 
23 namespace mindspore {
24 namespace ops {
25 namespace {
BinaryCrossEntroyGradInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)26 abstract::ShapePtr BinaryCrossEntroyGradInferShape(const PrimitivePtr &primitive,
27                                                    const std::vector<AbstractBasePtr> &input_args) {
28   MS_EXCEPTION_IF_NULL(primitive);
29   auto prim_name = primitive->name();
30   auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape())[kShape];
31   auto y_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex1]->BuildShape())[kShape];
32   auto weight_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex2]->BuildShape())[kShape];
33   CheckAndConvertUtils::Check("x shape", x_shape, kEqual, "y shape", y_shape, prim_name);
34   if (weight_shape.size() < 1) {
35     CheckAndConvertUtils::Check("y shape", y_shape, kEqual, "weight shape", weight_shape, prim_name);
36   }
37   return std::make_shared<abstract::Shape>(x_shape);
38 }
39 
BinaryCrossEntroyGradInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)40 TypePtr BinaryCrossEntroyGradInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
41   const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
42   std::map<std::string, TypePtr> types;
43   (void)types.emplace("x_shape", input_args[kInputIndex0]->BuildType());
44   (void)types.emplace("y_shape", input_args[kInputIndex1]->BuildType());
45   auto infer_type = CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
46   if (input_args[kInputIndex3]->BuildType() != nullptr) {
47     (void)types.emplace("x_shape", input_args[kInputIndex0]->BuildType());
48     (void)types.emplace("weight_shape", input_args[kInputIndex2]->BuildType());
49     infer_type = CheckAndConvertUtils::CheckTensorTypeSame(types, valid_types, prim->name());
50   }
51   return infer_type;
52 }
53 }  // namespace
Init(const Reduction & reduction)54 void BinaryCrossEntropyGrad::Init(const Reduction &reduction) { set_reduction(reduction); }
55 
set_reduction(const Reduction & reduction)56 void BinaryCrossEntropyGrad::set_reduction(const Reduction &reduction) {
57   int64_t swi = reduction;
58   (void)this->AddAttr(kReduction, MakeValue(swi));
59 }
get_reduction() const60 Reduction BinaryCrossEntropyGrad::get_reduction() const {
61   auto value_ptr = GetAttr(kReduction);
62   return Reduction(GetValue<int64_t>(value_ptr));
63 }
64 
BinaryCrossEntropyGradInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)65 AbstractBasePtr BinaryCrossEntropyGradInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
66                                             const std::vector<AbstractBasePtr> &input_args) {
67   MS_EXCEPTION_IF_NULL(primitive);
68   for (const auto &item : input_args) {
69     MS_EXCEPTION_IF_NULL(item);
70   }
71   const int64_t input_num = 4;
72   (void)CheckAndConvertUtils::CheckInteger("BinaryCrossEntropyGrad infer", SizeToLong(input_args.size()), kGreaterEqual,
73                                            input_num, primitive->name());
74   return std::make_shared<abstract::AbstractTensor>(BinaryCrossEntroyGradInferType(primitive, input_args),
75                                                     BinaryCrossEntroyGradInferShape(primitive, input_args));
76 }
77 REGISTER_PRIMITIVE_C(kNameBinaryCrossEntropyGrad, BinaryCrossEntropyGrad);
78 }  // namespace ops
79 }  // namespace mindspore
80