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/range_infer.h"
18 #include <math.h>
19 #include "nnacl/infer/infer_register.h"
20 #include "nnacl/range_parameter.h"
21 #include "nnacl/tensor_c_utils.h"
22
RangeInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)23 int RangeInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
24 OpParameter *parameter) {
25 int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 1, C3NUM, 1);
26 if (check_ret != NNACL_OK) {
27 return check_ret;
28 }
29
30 const TensorC *input = inputs[0];
31 TensorC *output = outputs[0];
32 output->data_type_ = inputs_size == C3NUM ? input->data_type_ : kNumberTypeInt32;
33 output->format_ = input->format_;
34 if (!InferFlag(inputs, inputs_size)) {
35 return NNACL_INFER_INVALID;
36 }
37 if (GetElementNum(inputs[FIRST_INPUT]) < 1) {
38 return NNACL_ERR;
39 }
40 int shape_size = 0;
41 if (inputs_size == C3NUM) {
42 NNACL_CHECK_FALSE(inputs[FIRST_INPUT]->data_ == NULL, NNACL_INFER_INVALID);
43 NNACL_CHECK_FALSE(inputs[SECOND_INPUT]->data_ == NULL, NNACL_INFER_INVALID);
44 NNACL_CHECK_FALSE(inputs[THIRD_INPUT]->data_ == NULL, NNACL_INFER_INVALID);
45 if ((inputs[FIRST_INPUT]->data_type_ != inputs[SECOND_INPUT]->data_type_) ||
46 (inputs[FIRST_INPUT]->data_type_ != inputs[THIRD_INPUT]->data_type_)) {
47 return NNACL_INFER_INVALID;
48 }
49 if (GetElementNum(inputs[SECOND_INPUT]) < 1 || GetElementNum(inputs[THIRD_INPUT]) < 1) {
50 return NNACL_ERR;
51 }
52 switch (inputs[0]->data_type_) {
53 case kNumberTypeInt:
54 case kNumberTypeInt32: {
55 int start = *(int *)(inputs[0]->data_);
56 int limit = *(int *)(inputs[1]->data_);
57 int delta = *(int *)(inputs[2]->data_);
58 if (delta == 0) {
59 return NNACL_ERR;
60 }
61 shape_size = imax((int)(ceil((float)(limit - start) / delta)), 0);
62 } break;
63 case kNumberTypeFloat32:
64 case kNumberTypeFloat: {
65 float start = *(float *)(inputs[0]->data_);
66 float limit = *(float *)(inputs[1]->data_);
67 float delta = *(float *)(inputs[2]->data_);
68 if (fabsf(delta) < EPSILON_VALUE) {
69 return NNACL_ERR;
70 }
71 shape_size = imax((int)(ceil((float)(limit - start) / delta)), 0);
72 } break;
73 default: {
74 return NNACL_ERR;
75 }
76 }
77 } else {
78 RangeParameter *param = (RangeParameter *)parameter;
79 NNACL_CHECK_NULL_RETURN_ERR(param);
80 if (param->delta_ == 0) {
81 return NNACL_PARAM_INVALID;
82 }
83 shape_size = ceil((float)(param->limit_ - param->start_) / param->delta_);
84 }
85
86 output->shape_size_ = 1;
87 output->shape_[0] = shape_size;
88 return NNACL_OK;
89 }
90
91 REG_INFER(Range, PrimType_Range, RangeInferShape)
92