• 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 #include <vector>
21 
22 #include "ops/sparse_softmax_cross_entropy_with_logits.h"
23 #include "ops/op_utils.h"
24 #include "utils/check_convert_utils.h"
25 
26 namespace mindspore {
27 namespace ops {
Init(const bool is_grad)28 void SparseSoftmaxCrossEntropyWithLogits::Init(const bool is_grad) { this->set_is_grad(is_grad); }
29 
set_is_grad(const bool is_grad)30 void SparseSoftmaxCrossEntropyWithLogits::set_is_grad(const bool is_grad) {
31   (void)this->AddAttr(kIsGrad, MakeValue(is_grad));
32 }
33 
get_is_grad() const34 bool SparseSoftmaxCrossEntropyWithLogits::get_is_grad() const { return GetValue<bool>(GetAttr(kIsGrad)); }
35 
SparseSoftmaxCrossEntropyWithLogitsInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)36 AbstractBasePtr SparseSoftmaxCrossEntropyWithLogitsInfer(const abstract::AnalysisEnginePtr &,
37                                                          const PrimitivePtr &primitive,
38                                                          const std::vector<AbstractBasePtr> &input_args) {
39   MS_EXCEPTION_IF_NULL(primitive);
40   auto prim_name = primitive->name();
41   const int64_t input_num = 2;
42   (void)CheckAndConvertUtils::CheckInteger("input numbers", SizeToLong(input_args.size()), kEqual, input_num,
43                                            prim_name);
44   for (const auto &item : input_args) {
45     MS_EXCEPTION_IF_NULL(item);
46   }
47   // infer shape
48   auto input_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
49   std::vector<int64_t> output_shape;
50   if (GetValue<bool>(primitive->GetAttr(kIsGrad)) != 0) {
51     output_shape = input_shape;
52   } else {
53     output_shape.push_back(1);
54   }
55   // infer type
56   auto output_type = input_args[0]->BuildType()->cast<TensorTypePtr>()->element();
57   return std::make_shared<abstract::AbstractTensor>(output_type, output_shape);
58 }
59 REGISTER_PRIMITIVE_C(kNameSparseSoftmaxCrossEntropyWithLogits, SparseSoftmaxCrossEntropyWithLogits);
60 }  // namespace ops
61 }  // namespace mindspore
62