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.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 {
set_axis(const std::vector<int64_t> & axis)29 void Softmax::set_axis(const std::vector<int64_t> &axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
30
get_axis() const31 std::vector<int64_t> Softmax::get_axis() const {
32 auto value_ptr = GetAttr(kAxis);
33 return GetValue<std::vector<int64_t>>(value_ptr);
34 }
35
Init(const int64_t axis)36 void Softmax::Init(const int64_t axis) {
37 auto op_name = this->name();
38 std::vector<int64_t> axis_vec = {axis};
39 (void)CheckAndConvertUtils::CheckInteger("axis_len", SizeToLong(axis_vec.size()), kEqual, 1, op_name);
40 auto rank = SizeToLong(axis_vec.size());
41 for (auto &item : axis_vec) {
42 CheckAndConvertUtils::CheckInRange<int64_t>("axis", item, kIncludeLeft, {-rank, rank}, op_name);
43 }
44 this->set_axis(axis_vec);
45 }
46
47 namespace {
SoftMaxInferShape(const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)48 abstract::ShapePtr SoftMaxInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
49 MS_EXCEPTION_IF_NULL(primitive);
50 auto op_name = primitive->name();
51 auto axis = GetValue<std::vector<int64_t>>(primitive->GetAttr(kAxis));
52 (void)CheckAndConvertUtils::CheckValue<size_t>("length of axis", axis.size(), kGreaterEqual, 1, op_name);
53 auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape());
54 if (shape_map.empty()) {
55 // Scalar input, has no shape
56 return std::make_shared<abstract::Shape>(std::vector<int64_t>());
57 }
58 auto in_shape = shape_map[kShape];
59 auto min_shape = shape_map[kMinShape];
60 auto max_shape = shape_map[kMaxShape];
61 auto rank = SizeToLong(in_shape.size());
62 for (auto &item : axis) {
63 CheckAndConvertUtils::CheckInRange<int64_t>("axis", item, kIncludeLeft, {-rank, rank}, op_name);
64 }
65 if (min_shape.size() != 0 && max_shape.size() != 0) {
66 return std::make_shared<abstract::Shape>(in_shape, min_shape, max_shape);
67 }
68 return std::make_shared<abstract::Shape>(in_shape);
69 }
70
SoftMaxInferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)71 TypePtr SoftMaxInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
72 if (std::any_of(input_args.begin(), input_args.end(), [](const AbstractBasePtr &a) { return a == nullptr; })) {
73 MS_LOG(EXCEPTION) << "nullptr";
74 }
75 const std::set<TypePtr> valid_types = {kFloat16, kFloat32, kFloat64};
76 return CheckAndConvertUtils::CheckTensorTypeValid("x", input_args[0]->BuildType(), valid_types, prim->name());
77 }
78 } // namespace
79
SoftmaxInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)80 AbstractBasePtr SoftmaxInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
81 const std::vector<AbstractBasePtr> &input_args) {
82 return abstract::MakeAbstract(SoftMaxInferShape(primitive, input_args), SoftMaxInferType(primitive, input_args));
83 }
84 REGISTER_PRIMITIVE_EVAL_IMPL(Softmax, prim::kPrimSoftmax, SoftmaxInfer, nullptr, true);
85 } // namespace ops
86 } // namespace mindspore
87