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/softmax_cross_entropy_with_logits.h"
18 #include <string>
19 #include <algorithm>
20 #include <memory>
21 #include <set>
22 #include <vector>
23 #include "ops/op_utils.h"
24 #include "utils/check_convert_utils.h"
25 #include "abstract/primitive_infer_map.h"
26
27 namespace mindspore {
28 namespace ops {
SoftmaxCrossEntropyWithLogitsInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)29 AbstractBasePtr SoftmaxCrossEntropyWithLogitsInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
30 const std::vector<AbstractBasePtr> &input_args) {
31 MS_EXCEPTION_IF_NULL(primitive);
32 auto prim_name = primitive->name();
33 const int64_t input_num = 2;
34 CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, prim_name);
35 // Infer shape
36 auto logits_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
37 auto labels_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[1]->BuildShape())[kShape];
38 CheckAndConvertUtils::Check("logits shape", logits_shape, kEqual, "labels shape", labels_shape, prim_name, TypeError);
39 std::vector<int64_t> loss_shape = {logits_shape[0]};
40 auto dlogits_shape = logits_shape;
41
42 // Infer type
43 const std::set<TypePtr> valid_types = {kFloat16, kFloat32};
44 std::map<std::string, TypePtr> args;
45 args.emplace("logits_type", input_args[0]->BuildType());
46 args.emplace("labels_type", input_args[1]->BuildType());
47 auto logits_type = CheckAndConvertUtils::CheckTensorTypeSame(args, valid_types, prim_name);
48
49 auto output0 = std::make_shared<abstract::AbstractTensor>(logits_type, loss_shape);
50 auto output1 = std::make_shared<abstract::AbstractTensor>(logits_type, dlogits_shape);
51 AbstractBasePtrList output = {output0, output1};
52 return std::make_shared<abstract::AbstractTuple>(output);
53 }
54 REGISTER_PRIMITIVE_C(kNameSoftmaxCrossEntropyWithLogits, SoftmaxCrossEntropyWithLogits);
55 } // namespace ops
56 } // namespace mindspore
57