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/arg_max.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 prim_name = primitive->name();
25 auto axis = GetValue<int64_t>(primitive->GetAttr(kAxis));
26 auto x_shape = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[0]->BuildShape())[kShape];
27 auto x_rank = SizeToLong(x_shape.size());
28 CheckAndConvertUtils::CheckInRange<int64_t>("argmax axis", axis, kIncludeLeft, {-x_rank, x_rank}, prim_name);
29 axis = axis < 0 ? axis + x_rank : axis;
30 std::vector<int64_t> out_shape;
31 for (size_t i = 0; i < x_shape.size(); ++i) {
32 if (SizeToLong(i) != axis) {
33 (void)out_shape.emplace_back(x_shape[i]);
34 }
35 }
36 return std::make_shared<abstract::Shape>(out_shape);
37 }
38
InferType(const PrimitivePtr & prim,const std::vector<AbstractBasePtr> & input_args)39 TypePtr InferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
40 MS_EXCEPTION_IF_NULL(prim);
41 (void)CheckAndConvertUtils::CheckInteger("input number", SizeToLong(input_args.size()), kEqual, 1, prim->name());
42 for (const auto &item : input_args) {
43 MS_EXCEPTION_IF_NULL(item);
44 }
45 return kInt32;
46 }
47 } // namespace
48
Init(const int64_t axis,const TypeId output_type)49 void ArgMax::Init(const int64_t axis, const TypeId output_type) {
50 set_axis(axis);
51 set_output_type(output_type);
52 }
53
set_axis(const int64_t axis)54 void ArgMax::set_axis(const int64_t axis) { (void)this->AddAttr(kAxis, MakeValue(axis)); }
set_output_type(const TypeId output_type)55 void ArgMax::set_output_type(const TypeId output_type) { (void)this->AddAttr(kOutputType, TypeIdToType(output_type)); }
56
get_axis() const57 int64_t ArgMax::get_axis() const { return GetValue<int64_t>(GetAttr(kAxis)); }
get_output_type() const58 TypeId ArgMax::get_output_type() const {
59 auto type_ptr = GetAttr(kOutputType)->cast<TensorTypePtr>()->element();
60 return type_ptr->type_id();
61 }
62
ArgMaxInfer(const abstract::AnalysisEnginePtr &,const PrimitivePtr & primitive,const std::vector<AbstractBasePtr> & input_args)63 AbstractBasePtr ArgMaxInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
64 const std::vector<AbstractBasePtr> &input_args) {
65 return std::make_shared<abstract::AbstractTensor>(InferType(primitive, input_args),
66 InferShape(primitive, input_args)->shape());
67 }
68 REGISTER_PRIMITIVE_C(kNameArgMax, ArgMax);
69 } // namespace ops
70 } // namespace mindspore
71