• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 "nnacl/infer/argmin_max_infer.h"
18 #include "nnacl/infer/infer_register.h"
19 
ArgMinMaxInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)20 int ArgMinMaxInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
21                         OpParameter *parameter) {
22   int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
23   if (check_ret != NNACL_OK) {
24     return check_ret;
25   }
26   if (inputs_size != 1 || outputs_size > 2) {
27     return NNACL_ERR;
28   }
29 
30   ArgMinMaxParameter *param = (ArgMinMaxParameter *)parameter;
31   const TensorC *input = inputs[0];
32   TensorC *output_1 = NULL;
33   TensorC *output_2 = NULL;
34   if (outputs_size == 2) {
35     output_1 = outputs[0];
36     output_2 = outputs[1];
37   } else if (param->out_value_) {
38     output_2 = outputs[0];
39   } else {
40     output_1 = outputs[0];
41   }
42 
43   if (output_1 != NULL) {
44     output_1->format_ = input->format_;
45     output_1->data_type_ = kNumberTypeInt32;
46   }
47   if (output_2 != NULL) {
48     SetDataTypeFormat(output_2, input);
49   }
50   if (!InferFlag(inputs, inputs_size)) {
51     return NNACL_INFER_INVALID;
52   }
53   if (input->shape_size_ > MAX_SHAPE_SIZE) {
54     return NNACL_INPUT_TENSOR_ERROR;
55   }
56   int output_shape[MAX_SHAPE_SIZE] = {0};
57   size_t output_shape_size = 0;
58   ShapeSet(output_shape, &output_shape_size, input->shape_, input->shape_size_);
59   int input_shape_size = (int)input->shape_size_;
60   int axis = param->axis_ < 0 ? param->axis_ + input_shape_size : param->axis_;
61   if (axis >= input_shape_size || axis < 0) {
62     return NNACL_PARAM_INVALID;
63   }
64   if (param->topk_ == 1 && !param->keep_dims_) {
65     int erase_ret = ShapeErase(output_shape, &output_shape_size, axis);
66     if (erase_ret != NNACL_OK) {
67       return NNACL_ERR;
68     }
69   } else {
70     output_shape[axis] = param->topk_;
71   }
72 
73   if (output_1 != NULL) {
74     SetShapeArray(output_1, output_shape, output_shape_size);
75   }
76   if (output_2 != NULL) {
77     SetShapeArray(output_2, output_shape, output_shape_size);
78   }
79   return NNACL_OK;
80 }
81 
82 REG_INFER(ArgMin, PrimType_ArgMinFusion, ArgMinMaxInferShape)
83 REG_INFER(ArgMax, PrimType_ArgMaxFusion, ArgMinMaxInferShape)
84