You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

weight_quantizer.cc 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright 2020 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. #include "tools/converter/quantizer/weight_quantizer.h"
  17. #include <list>
  18. #include <string>
  19. #include <vector>
  20. #include "src/common/common.h"
  21. #include "ir/dtype/type_id.h"
  22. using std::string;
  23. using std::vector;
  24. namespace mindspore {
  25. namespace lite {
  26. namespace quant {
  27. bool WeightQuantizer::IsPosNum(const std::string &str) {
  28. for (size_t i = 0; i < str.size(); i++) {
  29. if (str.at(i) < '0' || str.at(i) > '9') {
  30. return false;
  31. }
  32. if (str.at(i) == '0' && i == 0 && str.size() != 1) {
  33. return false;
  34. }
  35. }
  36. return true;
  37. }
  38. STATUS WeightQuantizer::WeightQuantInputCheck(const converter::Flags *config) {
  39. MS_ASSERT(config != nullptr);
  40. if (!WeightQuantizer::IsPosNum(config->quantWeightChannel)) {
  41. MS_LOG(ERROR) << "convWeightQuantChannelThreshold must be valid pos num.";
  42. return RET_ERROR;
  43. }
  44. if (!WeightQuantizer::IsPosNum(config->quantWeightSize)) {
  45. MS_LOG(ERROR) << "quantSize must be valid pos num.";
  46. return RET_ERROR;
  47. }
  48. if (!WeightQuantizer::IsPosNum(config->bitNum) || (config->bitNum != "8" && config->bitNum != "16")) {
  49. MS_LOG(ERROR) << "bitNum must be valid pos num, current only support 8 or 16 bit weight quant.";
  50. return RET_ERROR;
  51. }
  52. return RET_OK;
  53. }
  54. WeightQuantizer::WeightQuantizer(FuncGraphPtr graph, const string &weightSize,
  55. const std::string &convWeightChannelThreshold, const std::string &bitNum)
  56. : Quantizer(graph) {
  57. auto quantSize = static_cast<size_t>(std::stoull(weightSize));
  58. this->bitNum = static_cast<size_t>(std::stoull(bitNum));
  59. auto convQuantWeightChannelThreshold = static_cast<size_t>(std::stoull(convWeightChannelThreshold));
  60. mStrategy.reset(new QuantStrategy(quantSize, convQuantWeightChannelThreshold));
  61. quant_max = (1 << (unsigned int)(this->bitNum - 1)) - 1;
  62. quant_min = -(1 << (unsigned int)(this->bitNum - 1));
  63. if (this->bitNum == 8) {
  64. type_id = kNumberTypeInt8;
  65. } else if (this->bitNum == 16) {
  66. type_id = kNumberTypeInt16;
  67. }
  68. }
  69. STATUS WeightQuantizer::DoConvQuantize(const std::list<CNodePtr> &nodes) {
  70. for (auto &cnode : nodes) {
  71. if (!mStrategy->CanConvOpQuantized(cnode)) {
  72. continue;
  73. }
  74. auto primitive_c = GetValueNode<std::shared_ptr<PrimitiveC>>(cnode->input(0));
  75. if (primitive_c == nullptr) {
  76. MS_LOG(ERROR) << "primitive_c is nullptr";
  77. return RET_ERROR;
  78. }
  79. auto input_node = cnode->input(2);
  80. if (!input_node->isa<Parameter>()) {
  81. return RET_ERROR;
  82. }
  83. auto param_node = input_node->cast<ParameterPtr>();
  84. if (!param_node->has_default()) {
  85. return RET_ERROR;
  86. }
  87. ParamValueLitePtr param_value = std::static_pointer_cast<ParamValueLite>(param_node->default_param());
  88. if (param_value == nullptr) {
  89. return RET_ERROR;
  90. }
  91. if (param_value->tensor_type() != mindspore::kNumberTypeFloat32) {
  92. MS_LOG(ERROR) << "model weight data type invalid which is " << param_value->tensor_type();
  93. return RET_ERROR;
  94. }
  95. auto status = RET_ERROR;
  96. if (type_id == kNumberTypeInt8) {
  97. status = QuantFilter<int8_t>(param_value, primitive_c, QuantType_WeightQuant, quant_max, quant_min, bitNum, true);
  98. } else if (type_id == kNumberTypeInt16) {
  99. status =
  100. QuantFilter<int16_t>(param_value, primitive_c, QuantType_WeightQuant, quant_max, quant_min, bitNum, true);
  101. }
  102. if (status != RET_OK) {
  103. MS_LOG(ERROR) << "QuantFilter failed : " << status;
  104. return status;
  105. }
  106. // set dtype
  107. param_value->set_tensor_type(type_id);
  108. auto abstractBase = param_node->abstract();
  109. if (abstractBase == nullptr) {
  110. MS_LOG(ERROR) << "Abstract of parameter is nullptr, " << param_node->name();
  111. return RET_ERROR;
  112. }
  113. if (!utils::isa<abstract::AbstractTensorPtr>(abstractBase)) {
  114. MS_LOG(ERROR) << "Abstract of parameter should be anstract tensor, " << param_node->name();
  115. return RET_ERROR;
  116. }
  117. auto abstractTensor = utils::cast<abstract::AbstractTensorPtr>(abstractBase);
  118. abstractTensor->element()->set_type(TypeIdToType(type_id));
  119. primitive_c->SetQuantType(schema::QuantType_WeightQuant);
  120. }
  121. return RET_OK;
  122. }
  123. STATUS WeightQuantizer::DoMulQuantize(const std::list<CNodePtr> &nodes) {
  124. for (auto &node : nodes) {
  125. if (!mStrategy->CanMulOpQuantized(node)) {
  126. continue;
  127. }
  128. ParamValueLitePtr param_value = nullptr;
  129. ParameterPtr param_node = nullptr;
  130. for (size_t i = 1; i < node->size(); i++) {
  131. auto inputNode = node->input(i);
  132. if (inputNode->isa<Parameter>()) {
  133. param_node = inputNode->cast<ParameterPtr>();
  134. if ((param_node != nullptr) && param_node->has_default()) {
  135. param_value = std::static_pointer_cast<ParamValueLite>(param_node->default_param());
  136. if ((param_value == nullptr) || (param_value->tensor_size() == 0) ||
  137. (param_value->tensor_addr() == nullptr) ||
  138. (param_value->tensor_type() != mindspore::kNumberTypeFloat32)) {
  139. param_value = nullptr;
  140. continue;
  141. } else {
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. if (param_value == nullptr) {
  148. MS_LOG(ERROR) << "No valid input param node !";
  149. return RET_ERROR;
  150. }
  151. auto primitive_c = GetValueNode<std::shared_ptr<PrimitiveC>>(node->input(0));
  152. if (primitive_c == nullptr) {
  153. MS_LOG(ERROR) << "primitive_c is nullptr";
  154. return RET_ERROR;
  155. }
  156. auto status = RET_ERROR;
  157. if (type_id == kNumberTypeInt8) {
  158. status = QuantFilter<int8_t>(param_value, primitive_c, QuantType_WeightQuant, quant_max, quant_min, bitNum, true);
  159. } else if (type_id == kNumberTypeInt16) {
  160. status =
  161. QuantFilter<int16_t>(param_value, primitive_c, QuantType_WeightQuant, quant_max, quant_min, bitNum, true);
  162. }
  163. if (status != RET_OK) {
  164. MS_LOG(ERROR) << "QuantFilter failed : " << status;
  165. return status;
  166. }
  167. param_value->set_tensor_type(type_id);
  168. // set dtype
  169. auto abstractBase = param_node->abstract();
  170. if (abstractBase == nullptr) {
  171. MS_LOG(ERROR) << "Abstract of parameter is nullptr, " << param_node->name();
  172. return RET_ERROR;
  173. }
  174. if (!utils::isa<abstract::AbstractTensorPtr>(abstractBase)) {
  175. MS_LOG(ERROR) << "Abstract of parameter should be anstract tensor, " << param_node->name();
  176. return RET_ERROR;
  177. }
  178. auto abstractTensor = utils::cast<abstract::AbstractTensorPtr>(abstractBase);
  179. abstractTensor->element()->set_type(TypeIdToType(type_id));
  180. primitive_c->SetQuantType(schema::QuantType_WeightQuant);
  181. }
  182. return RET_OK;
  183. }
  184. STATUS WeightQuantizer::DoQuantize(FuncGraphPtr funcGraph) {
  185. auto ret = RET_OK;
  186. auto cnodes = funcGraph->GetOrderedCnodes();
  187. ret = DoConvQuantize(cnodes);
  188. if (ret != RET_OK) {
  189. MS_LOG(ERROR) << "DoConvQuantize failed :" << ret;
  190. return ret;
  191. }
  192. ret = DoMulQuantize(cnodes);
  193. if (ret != RET_OK) {
  194. MS_LOG(ERROR) << "DoMulQuantize failed :" << ret;
  195. return ret;
  196. }
  197. return ret;
  198. }
  199. } // namespace quant
  200. } // namespace lite
  201. } // namespace mindspore