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 "coder/generator/component/const_blocks/mtensor.h" 18 19 namespace mindspore::lite::micro { 20 const char tensor_header[] = R"RAW( 21 /** 22 * Copyright 2021 Huawei Technologies Co., Ltd 23 * 24 * Licensed under the Apache License, Version 2.0 (the "License"); 25 * you may not use this file except in compliance with the License. 26 * You may obtain a copy of the License at 27 * 28 * http://www.apache.org/licenses/LICENSE-2.0 29 * 30 * Unless required by applicable law or agreed to in writing, software 31 * distributed under the License is distributed on an "AS IS" BASIS, 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 33 * See the License for the specific language governing permissions and 34 * limitations under the License. 35 */ 36 37 #ifndef MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ 38 #define MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ 39 40 #include "include/ms_tensor.h" 41 #include "include/api/format.h" 42 43 namespace mindspore { 44 namespace lite { 45 struct LiteQuantParam { 46 double scale; 47 int32_t zeroPoint; 48 float var_corr{1}; 49 float mean_corr{0}; 50 bool inited; 51 Vector<float> clusters{}; 52 int bitNum; 53 int roundType; 54 int multiplier; 55 int dstDtype; 56 }; 57 58 class MTensor : public mindspore::tensor::MSTensor { 59 public: 60 MTensor() = default; 61 MTensor(String name, TypeId type, Vector<int32_t> shape) : tensor_name_(name), data_type_(type), shape_(shape) {} 62 ~MTensor() override; 63 64 void set_allocator(AllocatorPtr allocator) override {} 65 AllocatorPtr allocator() const override { return nullptr; } 66 TypeId data_type() const override { return data_type_; } 67 void set_data_type(TypeId data_type) override { data_type_ = data_type; } 68 void set_format(mindspore::Format format) override {} 69 mindspore::Format format() const override { return mindspore::NHWC; } 70 Vector<int> shape() const override { return shape_; } 71 void set_shape(const Vector<int> &shape) override { shape_ = shape; } 72 int ElementsNum() const override; 73 size_t Size() const override; 74 String tensor_name() const override { return tensor_name_; } 75 void set_tensor_name(const String &name) override { tensor_name_ = name; } 76 void *MutableData() override; 77 void *data() override { return data_; } 78 void set_data(void *data) override { data_ = data; } 79 Vector<LiteQuantParam> quant_params() const override { return this->quant_params_; } 80 void set_quant_params(const Vector<LiteQuantParam> quant_params) override { this->quant_params_ = quant_params; } 81 bool IsConst() const override {return this->data_ != nullptr;} 82 83 private: 84 String tensor_name_; 85 TypeId data_type_; 86 Vector<int> shape_; 87 void *data_ = nullptr; 88 Vector<LiteQuantParam> quant_params_; 89 }; 90 } // namespace lite 91 } // namespace mindspore 92 93 #endif // MINDSPORE_LITE_MICRO_LIBRARY_SOURCE_TENSOR_H_ 94 95 )RAW"; 96 97 const char tensor_source[] = R"RAW( 98 /** 99 * Copyright 2021 Huawei Technologies Co., Ltd 100 * 101 * Licensed under the Apache License, Version 2.0 (the "License"); 102 * you may not use this file except in compliance with the License. 103 * You may obtain a copy of the License at 104 * 105 * http://www.apache.org/licenses/LICENSE-2.0 106 * 107 * Unless required by applicable law or agreed to in writing, software 108 * distributed under the License is distributed on an "AS IS" BASIS, 109 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 110 * See the License for the specific language governing permissions and 111 * limitations under the License. 112 */ 113 114 #include "tensor.h" 115 116 namespace mindspore { 117 namespace lite { 118 size_t DataTypeSize(const TypeId type) { 119 switch (type) { 120 case kNumberTypeFloat64: 121 return sizeof(double); 122 case kNumberTypeFloat: 123 case kNumberTypeFloat32: 124 return sizeof(float); 125 case kNumberTypeInt8: 126 return sizeof(int8_t); 127 case kNumberTypeUInt8: 128 return sizeof(uint8_t); 129 case kNumberTypeFloat16: 130 case kNumberTypeInt16: 131 return sizeof(int16_t); 132 case kNumberTypeInt32: 133 return sizeof(int32_t); 134 case kNumberTypeInt64: 135 return sizeof(int64_t); 136 case kNumberTypeUInt16: 137 return sizeof(uint16_t); 138 case kNumberTypeUInt32: 139 return sizeof(uint32_t); 140 case kNumberTypeUInt64: 141 return sizeof(uint64_t); 142 case kNumberTypeBool: 143 return sizeof(bool); 144 case kObjectTypeString: 145 return sizeof(char); 146 case kObjectTypeTensorType: 147 default: 148 return 0; 149 } 150 } 151 152 MTensor::~MTensor() { 153 if (data_ != nullptr) { 154 free(data_); 155 data_ = nullptr; 156 } 157 } 158 159 int MTensor::ElementsNum() const { 160 int elements = 1; 161 for (int i : shape_) { 162 elements *= i; 163 } 164 return elements; 165 } 166 167 size_t MTensor::Size() const { 168 size_t element_size = DataTypeSize(data_type_); 169 return element_size * ElementsNum(); 170 } 171 172 void *MTensor::MutableData() { 173 if (data_ == nullptr) { 174 data_ = malloc(this->Size()); 175 } 176 return data_; 177 } 178 } // namespace lite 179 } // namespace mindspore 180 181 )RAW"; 182 } // namespace mindspore::lite::micro 183