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 #ifndef MINDSPORE_NNACL_COMMON_H_ 18 #define MINDSPORE_NNACL_COMMON_H_ 19 20 #include <stddef.h> 21 #include "nnacl/errorcode.h" 22 #include "nnacl/op_base.h" 23 #include "nnacl/tensor_c.h" 24 25 #ifdef __cplusplus 26 extern "C" { 27 #endif 28 29 #define kNCHW_N 0 30 #define kNCHW_C 1 31 #define kNCHW_H 2 32 #define kNCHW_W 3 33 34 typedef enum FormatC { 35 Format_NCHW = 0, 36 Format_NHWC = 1, 37 Format_NHWC4 = 2, 38 Format_HWKC = 3, 39 Format_HWCK = 4, 40 Format_KCHW = 5, 41 Format_CKHW = 6, 42 Format_KHWC = 7, 43 Format_CHWK = 8, 44 Format_HW = 9, 45 Format_HW4 = 10, 46 Format_NC = 11, 47 Format_NC4 = 12, 48 Format_NC4HW4 = 13, 49 Format_NUM_OF_FORMAT = 14, 50 Format_NCDHW = 15, 51 Format_NWC = 16, 52 Format_NCW = 17, 53 Format_MIN = Format_NCHW, 54 Format_MAX = Format_NCW 55 } FormatC; 56 57 typedef enum TypeIdC { 58 kTypeUnknown = 0, 59 kMetaTypeBegin = kTypeUnknown, 60 kMetaTypeType, // Type 61 kMetaTypeAnything, 62 kMetaTypeObject, 63 kMetaTypeTypeType, // TypeType 64 kMetaTypeProblem, 65 kMetaTypeExternal, 66 kMetaTypeNone, 67 kMetaTypeNull, 68 kMetaTypeEllipsis, 69 kMetaTypeEnd, 70 // 71 // Object types 72 // 73 kObjectTypeBegin = kMetaTypeEnd, 74 kObjectTypeNumber, 75 kObjectTypeString, 76 kObjectTypeList, 77 kObjectTypeTuple, 78 kObjectTypeSlice, 79 kObjectTypeKeyword, 80 kObjectTypeTensorType, 81 kObjectTypeRowTensorType, 82 kObjectTypeSparseTensorType, 83 kObjectTypeUndeterminedType, 84 kObjectTypeClass, 85 kObjectTypeDictionary, 86 kObjectTypeFunction, 87 kObjectTypeJTagged, 88 kObjectTypeSymbolicKeyType, 89 kObjectTypeEnvType, 90 kObjectTypeRefKey, 91 kObjectTypeRef, 92 kObjectTypeEnd, 93 // 94 // Number Types 95 // 96 kNumberTypeBegin = kObjectTypeEnd, 97 kNumberTypeBool, 98 kNumberTypeInt, 99 kNumberTypeInt8, 100 kNumberTypeInt16, 101 kNumberTypeInt32, 102 kNumberTypeInt64, 103 kNumberTypeUInt, 104 kNumberTypeUInt8, 105 kNumberTypeUInt16, 106 kNumberTypeUInt32, 107 kNumberTypeUInt64, 108 kNumberTypeFloat, 109 kNumberTypeFloat16, 110 kNumberTypeFloat32, 111 kNumberTypeFloat64, 112 kNumberTypeComplex64, 113 kNumberTypeEnd 114 } TypeIdC; 115 116 enum NNACLLshProjectionType { 117 LshProjectionType_UNKNOWN = 0, 118 LshProjectionType_SPARSE = 1, 119 LshProjectionType_DENSE = 2, 120 LshProjectionType_MIN = LshProjectionType_UNKNOWN, 121 LshProjectionType_MAX = LshProjectionType_DENSE 122 }; 123 124 enum NNACLQuantType { 125 QuantType_QUANT_NONE = 0, 126 QuantType_AwareTraining = 1, 127 QuantType_WeightQuant = 2, 128 QuantType_PostTraining = 3, 129 QuantType_QUANT_WEIGHT = 4, 130 QuantType_QUANT_ALL = 5, 131 QuantType_MIN = QuantType_QUANT_NONE, 132 QuantType_MAX = QuantType_QUANT_ALL 133 }; 134 135 typedef struct vvector { 136 int **shape_; // value of shapes 137 int *shape_size_; // size of shape 138 size_t size_; // number of shapes 139 } vvector; 140 141 #ifndef CONTROLFLOW_TENSORLIST_CLIP 142 typedef struct TensorListC { 143 bool is_ready_; 144 int data_type_; 145 int format_; 146 int shape_value_; 147 int tensors_data_type_; // element_data_type_, keep same as c++ 148 int max_elements_num_; 149 int element_shape_[8]; 150 size_t element_num_; 151 size_t element_shape_size_; 152 TensorC *tensors_; 153 } TensorListC; 154 #endif 155 156 typedef struct VectorC { 157 int *data_; 158 size_t size_; 159 size_t max_size_; 160 size_t per_malloc_size_; 161 } VectorC; 162 163 #ifndef CONTROLFLOW_TENSORLIST_CLIP 164 int MallocTensorListData(TensorListC *tensor_list, TypeIdC dtype, const vvector *tensor_shape); 165 int TensorListMergeShape(int *element_shape, size_t *element_shape_size, const int *tmp, size_t tmp_size); 166 bool TensorListIsFullyDefined(const int *shape, size_t shape_size); 167 #endif 168 169 int GetBatch(const TensorC *tensor); 170 int GetHeight(const TensorC *tensor); 171 int GetWidth(const TensorC *tensor); 172 int GetChannel(const TensorC *tensor); 173 int GetElementNum(const TensorC *tensor); 174 int GetDimensionSize(const TensorC *tensor, const size_t index); 175 176 int CheckAugmentNull(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 177 const OpParameter *parameter); 178 int CheckAugmentNullSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 179 const OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj); 180 int CheckAugmentNullSizeInputTwo(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, 181 size_t outputs_size, const OpParameter *parameter, size_t inputs_size_obj_0, 182 size_t inputs_size_obj_1, size_t outputs_size_obj); 183 int CheckAugmentNullInputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 184 const OpParameter *parameter, size_t inputs_size_obj); 185 int CheckAugmentNullOutputSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 186 const OpParameter *parameter, size_t outputs_size_obj); 187 int CheckAugmentWithMinSize(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 188 const OpParameter *parameter, size_t inputs_size_obj, size_t outputs_size_obj); 189 void SetDataTypeFormat(TensorC *dst, const TensorC *src); 190 191 void SetShapeTensor(TensorC *dst, const TensorC *src); 192 void SetShapeArray(TensorC *dst, const int *src, size_t src_size); 193 void ShapeSet(int *dst_shape, size_t *dst_shape_size, const int *src_shape, size_t src_shape_size); 194 void ShapePush(int *shape, size_t *shape_size, int value); 195 int ShapeInsert(int *shape, size_t *shape_size, int index, int value); 196 int ShapeErase(int *shape, size_t *shape_size, int index); 197 bool ShapeEqual(const int *shape0, size_t shape0_size, const int *shape1, size_t shape1_size); 198 199 void iswap(int *a, int *b); 200 201 int imin(int a, int b); 202 int imax(int a, int b); 203 204 int CommonInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 205 OpParameter *parameter); 206 int FftInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, 207 const OpParameter *parameter); 208 209 bool InferFlag(const TensorC *const *inputs, size_t inputs_size); 210 211 #ifdef __cplusplus 212 } 213 #endif 214 215 #endif // MINDSPORE_NNACL_COMMON__H_ 216