• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022-2023 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use tensor 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/tensorlist_c_utils.h"
18 
MallocTensorListData(TensorListC * tensor_list,TypeIdC dtype,const vvector * tensor_shape)19 int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, const vvector *tensor_shape) {
20   // This function will create a new tensors_
21   // Your must to set shape(param2: tensor_shape) and data_type_(tensors_data_type_ = param1: dtype) of each tensor in
22   // tensors_. After that, you need to call function:MallocData to malloc data buf of each tensor in tensors_.
23 
24   if (tensor_list->element_num_ == 0) {
25     return NNACL_OK;
26   }
27   if (((size_t)(tensor_list->element_num_)) != tensor_shape->size_) {
28     return NNACL_ERR;
29   }
30   tensor_list->tensors_data_type_ = dtype;
31   void *addr = malloc(tensor_list->element_num_ * sizeof(void *) +
32                       tensor_list->element_num_ * sizeof(TensorC));  // free in infer_manager
33   if (addr == NULL) {
34     free(tensor_list->tensors_);
35     return NNACL_NULL_PTR;
36   }
37   memset(addr, 0, tensor_list->element_num_ * sizeof(void *) + tensor_list->element_num_ * sizeof(TensorC));
38   tensor_list->tensors_ = (TensorC **)addr;
39   TensorC *tensors = (TensorC *)(tensor_list->tensors_ + tensor_list->element_num_);
40   for (size_t i = 0; i < tensor_list->element_num_; ++i) {
41     TensorC *tensor = tensors + i;
42     tensor_list->tensors_[i] = tensor;
43     tensor->format_ = Format_NHWC;
44     tensor->data_type_ = dtype;
45     ShapeSet(tensor->shape_, &(tensor->shape_size_), tensor_shape->shape_[i], (size_t)tensor_shape->shape_size_[i]);
46   }
47   return NNACL_OK;
48 }
49 
TensorListMergeShape(int * element_shape,size_t * element_shape_size,const int * tmp,size_t tmp_size)50 int TensorListMergeShape(int *element_shape, size_t *element_shape_size, const int *tmp, size_t tmp_size) {
51   if (*element_shape_size >= 255 || element_shape[0] == -1) {
52     ShapeSet(element_shape, element_shape_size, tmp, tmp_size);
53     return NNACL_OK;
54   }
55   if (*element_shape_size != tmp_size) {
56     return NNACL_ERR;
57   }
58   for (size_t j = 0; j < tmp_size; ++j) {
59     if (element_shape[j] >= 0 && tmp[j] >= 0 && element_shape[j] != tmp[j]) {
60       return NNACL_ERR;
61     }
62     element_shape[j] = element_shape[j] >= 0 ? element_shape[j] : tmp[j];
63   }
64   return NNACL_OK;
65 }
66 
TensorListIsFullyDefined(const int * shape,size_t shape_size)67 bool TensorListIsFullyDefined(const int *shape, size_t shape_size) {
68   for (size_t i = 0; i < shape_size; ++i) {
69     if (shape[i] < 0) {
70       return false;
71     }
72   }
73   return true;
74 }
75 
InferFlagTensorList(TensorC * tensorc)76 bool InferFlagTensorList(TensorC *tensorc) {
77   TensorListC *input_tensor_list = (TensorListC *)tensorc;
78   if (input_tensor_list->shape_value_ == -1) {
79     return false;
80   }
81   return true;
82 }
83