• 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/tile_infer.h"
18 #include <limits.h>
19 #include "nnacl/infer/infer_register.h"
20 #include "nnacl/tile_parameter.h"
21 #include "nnacl/tensor_c_utils.h"
22 
TileParamCaffe2Tflite(TileParameter * param,size_t out_shape_size)23 void TileParamCaffe2Tflite(TileParameter *param, size_t out_shape_size) {
24   if (param->dims_size_ != 0) {
25     int multiples_size_tmp[5] = {0};
26     NNACL_CHECK_TRUE_RET_VOID(out_shape_size <= 5);
27     for (size_t i = 0; i < out_shape_size; i++) {
28       multiples_size_tmp[i] = 1;
29     }
30     for (size_t i = 0; i < param->dims_size_; i++) {
31       if (i >= MAX_SHAPE_SIZE) {
32         return;
33       }
34       multiples_size_tmp[param->dims_[i]] = param->multiples_[i];
35     }
36     for (size_t i = 0; i < 5; i++) {
37       param->multiples_[i] = multiples_size_tmp[i];
38     }
39   }
40 }
41 
TileInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)42 int TileInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
43                    OpParameter *parameter) {
44   int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1);
45   if (check_ret != NNACL_OK) {
46     return check_ret;
47   }
48 
49   const TensorC *input = inputs[0];
50   TensorC *output = outputs[0];
51 
52   SetDataTypeFormat(output, input);
53   if (!InferFlag(inputs, inputs_size)) {
54     return NNACL_INFER_INVALID;
55   }
56 
57   int out_shape[MAX_SHAPE_SIZE] = {0};
58   size_t out_shape_size = 0;
59   TileParameter *param = (TileParameter *)parameter;
60 
61   size_t multiples_size = 0;
62   int input1_shape_size = inputs[1]->shape_size_;
63   if (input1_shape_size > (int)(input->shape_size_) || input->shape_size_ > MAX_SHAPE_SIZE) {
64     NNACL_LOG_ERROR("input1_shape_size > (int)(input->shape_size_) || input->shape_size_ > MAX_SHAPE_SIZE");
65     return NNACL_INPUT_TENSOR_ERROR;
66   }
67   NNACL_CHECK_TRUE_RET(input1_shape_size <= MAX_SHAPE_SIZE, NNACL_ERR);
68   int data_num = GetElementNum(inputs[1]);
69   multiples_size = (size_t)(data_num);
70   if (inputs[1]->data_type_ != kNumberTypeInt && inputs[1]->data_type_ != kNumberTypeInt32) {
71     NNACL_LOG_ERROR("inputs[1]->data_type_ != kNumberTypeInt && inputs[1]->data_type_ != kNumberTypeInt32");
72     return NNACL_INPUT_TENSOR_ERROR;
73   }
74   int *input1_data = inputs[1]->data_;
75   if (input1_data == NULL) {
76     return NNACL_INFER_INVALID;
77   }
78   NNACL_CHECK_TRUE_RET(data_num <= MAX_SHAPE_SIZE, NNACL_ERR);
79   for (int i = 0; i < data_num; i++) {
80     param->multiples_[i] = input1_data[i];
81   }
82 
83   int *dims = param->dims_;
84   size_t dims_size = param->dims_size_;
85   if (dims_size == 0) {
86     int dim_num = GetElementNum(inputs[1]);
87     NNACL_CHECK_TRUE_RET(dim_num <= MAX_SHAPE_SIZE, NNACL_ERR);
88     for (int dim = 0; dim < dim_num; ++dim) {
89       ShapePush(dims, &dims_size, dim);
90     }
91     param->dims_size_ = dims_size;
92   }
93   NNACL_CHECK_TRUE_RET(multiples_size == dims_size, NNACL_ERR);
94   for (size_t i = 0; i < input->shape_size_; ++i) {
95     ShapePush(out_shape, &out_shape_size, input->shape_[i]);
96   }
97   for (size_t i = 0; i < dims_size; ++i) {
98     if (dims[i] >= MAX_SHAPE_SIZE || input->shape_[dims[i]] == 0) {
99       NNACL_LOG_ERROR("dims[%zu] >= MAX_SHAPE_SIZE || input->shape_[dims[%zu]] == 0", i, i);
100       return NNACL_ERR;
101     }
102     if (input->shape_[dims[i]] != 0 && param->multiples_[i] > INT_MAX / input->shape_[dims[i]]) {
103       NNACL_LOG_ERROR("input->shape_[dims[%zu]] != 0 && param->multiples_[%zu] > INT_MAX / input->shape_[dims[%zu]]", i,
104                       i, i);
105       return NNACL_ERR;
106     }
107     NNACL_CHECK_FALSE(INT_MUL_OVERFLOW(input->shape_[dims[i]], (param->multiples_[i])), NNACL_ERR);
108     out_shape[dims[i]] = input->shape_[dims[i]] * (param->multiples_[i]);
109   }
110   // change caffe param format to tflite
111   TileParamCaffe2Tflite(param, out_shape_size);
112   SetShapeArray(output, out_shape, out_shape_size);
113   return NNACL_OK;
114 }
115 
116 REG_INFER(Tile, PrimType_TileFusion, TileInferShape)
117