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/control/tensorlist_fromtensor_infer.h"
18 #include "nnacl/infer/infer_register.h"
19 #include "nnacl/tensorlist_c_utils.h"
20 #include "nnacl/tensor_c_utils.h"
21
TensorListFromTensorInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)22 int TensorListFromTensorInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs,
23 size_t outputs_size, OpParameter *parameter) {
24 int check_ret = CheckAugmentWithMinSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1);
25 if (check_ret != NNACL_OK) {
26 return check_ret;
27 }
28
29 TensorListC *output = (TensorListC *)(outputs[0]);
30 const TensorC *input0 = inputs[0];
31 output->data_type_ = kObjectTypeTensorType;
32 output->format_ = Format_NHWC;
33 output->tensors_data_type_ = input0->data_type_;
34
35 if (!InferFlag(inputs, inputs_size)) {
36 return NNACL_INFER_INVALID;
37 }
38
39 if (input0->shape_size_ < 1) {
40 return NNACL_ERR;
41 }
42 int dim0 = input0->shape_[0];
43 if (dim0 < 0) {
44 return NNACL_ERR;
45 }
46 const TensorC *input1 = inputs[1];
47 if (input1->data_ == NULL) {
48 return NNACL_NULL_PTR;
49 }
50 int *ele_shape_ptr = (int *)(input1->data_);
51 NNACL_CHECK_NULL_RETURN_ERR(ele_shape_ptr);
52 vvector tensor_shape;
53 tensor_shape.size_ = (size_t)(dim0);
54 tensor_shape.shape_ = (int **)malloc(tensor_shape.size_ * sizeof(int *));
55 if (tensor_shape.shape_ == NULL) {
56 return NNACL_NULL_PTR;
57 }
58 tensor_shape.shape_size_ = (int *)malloc(tensor_shape.size_ * sizeof(int));
59 if (tensor_shape.shape_size_ == NULL) {
60 free(tensor_shape.shape_);
61 return NNACL_NULL_PTR;
62 }
63 for (int i = 0; i < dim0; i++) {
64 tensor_shape.shape_[i] = (int *)(input0->shape_ + 1);
65 tensor_shape.shape_size_[i] = (int)(input0->shape_size_) - 1;
66 }
67
68 ShapeSet(output->element_shape_, &(output->element_shape_size_), ele_shape_ptr, (size_t)GetElementNum(input1));
69 output->element_num_ = (size_t)(dim0);
70 int ret = MallocTensorListData(output, input0->data_type_, &tensor_shape);
71 if (ret != NNACL_OK) {
72 free(tensor_shape.shape_);
73 free(tensor_shape.shape_size_);
74 return NNACL_ERR;
75 }
76 free(tensor_shape.shape_);
77 free(tensor_shape.shape_size_);
78 return NNACL_OK;
79 }
80
81 REG_INFER(TensorListFromTensor, PrimType_TensorListFromTensor, TensorListFromTensorInferShape)
82