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.

quant_cast.cc 5.6 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "mindspore/lite/tools/converter/quantizer/quant_cast.h"
  17. #include <memory>
  18. #include <vector>
  19. #include "src/ops/primitive_c.h"
  20. namespace mindspore::lite::quant {
  21. ValueNodePtr NewQuantCastValueNode(int src_type, int dst_type, const std::vector<schema::QuantParamT> &quant_params) {
  22. std::unique_ptr<schema::PrimitiveT> primitive = std::make_unique<schema::PrimitiveT>();
  23. schema::QuantDTypeCastT quant_dtype_cast;
  24. quant_dtype_cast.srcT = src_type; // kNumberTypeInt8;
  25. quant_dtype_cast.dstT = dst_type; // kNumberTypeFloat32;
  26. primitive->value.Set(quant_dtype_cast);
  27. auto primTValue = std::make_shared<PrimitiveC>(primitive.release());
  28. primTValue->SetQuantType(schema::QuantType_PostTraining);
  29. for (auto &quant_param : quant_params) {
  30. std::vector<schema::QuantParamT> quant_params_in = {quant_param};
  31. primTValue->AddInputQuantParam(quant_params_in);
  32. primTValue->AddOutputQuantParam(quant_params_in);
  33. }
  34. return NewValueNode(primTValue);
  35. }
  36. STATUS QuantCast::Run(FuncGraphPtr graph) {
  37. MS_ASSERT(graph != nullptr);
  38. auto cnodes = graph->GetOrderedCnodes();
  39. bool first = true;
  40. for (auto &cnode : cnodes) {
  41. auto primitive_c = GetValueNode<std::shared_ptr<PrimitiveC>>(cnode->input(0));
  42. auto curnode_quant_type = schema::QuantType_QUANT_NONE;
  43. if (primitive_c == nullptr) {
  44. MS_LOG(WARNING) << "primitive_c is nullptr: " << cnode->fullname_with_scope();
  45. } else {
  46. curnode_quant_type = primitive_c->GetQuantType();
  47. }
  48. if (first) {
  49. if (curnode_quant_type == schema::QuantType_PostTraining && inputDataDType == kNumberTypeFloat32) {
  50. auto value_node =
  51. NewQuantCastValueNode(kNumberTypeFloat32, kNumberTypeInt8, primitive_c->GetInputQuantParams().front());
  52. std::vector<AnfNodePtr> op_inputs = {value_node, cnode->input(1)};
  53. auto quant_cast_cnode = graph->NewCNode(op_inputs);
  54. quant_cast_cnode->set_fullname_with_scope(cnode->fullname_with_scope() + "_quant_cast");
  55. cnode->set_input(1, quant_cast_cnode);
  56. MS_LOG(DEBUG) << "Add quant cast at front. "
  57. << "cur_node: " << cnode->fullname_with_scope() << " quant_type: " << curnode_quant_type;
  58. }
  59. first = false;
  60. continue;
  61. }
  62. for (size_t i = 1; i < cnode->inputs().size(); i++) {
  63. auto input_node = cnode->input(i);
  64. if (!input_node->isa<CNode>()) {
  65. continue;
  66. }
  67. auto input_cnode = std::dynamic_pointer_cast<CNode>(input_node);
  68. auto input_cnode_primitive_c = GetValueNode<std::shared_ptr<PrimitiveC>>(input_cnode->input(0));
  69. if (input_cnode_primitive_c == nullptr) {
  70. MS_LOG(DEBUG) << "input: " << i << " " << input_cnode->fullname_with_scope() << ": "
  71. << " PrimitiveC is null";
  72. continue;
  73. }
  74. auto input_cnode_quant_type = input_cnode_primitive_c->GetQuantType();
  75. if (curnode_quant_type != input_cnode_quant_type) {
  76. ValueNodePtr value_node = nullptr;
  77. if (curnode_quant_type == schema::QuantType_PostTraining &&
  78. input_cnode_quant_type == schema::QuantType_QUANT_NONE) {
  79. value_node =
  80. NewQuantCastValueNode(kNumberTypeFloat32, kNumberTypeInt8, primitive_c->GetInputQuantParams()[i - 1]);
  81. } else if (curnode_quant_type == schema::QuantType_QUANT_NONE &&
  82. input_cnode_quant_type == schema::QuantType_PostTraining) {
  83. value_node = NewQuantCastValueNode(kNumberTypeInt8, kNumberTypeFloat32,
  84. input_cnode_primitive_c->GetOutputQuantParams().front());
  85. }
  86. if (value_node == nullptr) {
  87. MS_LOG(WARNING) << "value_node is null! "
  88. << "cur_node: " << cnode->fullname_with_scope() << " quant_type: "
  89. << " input_" << i << ": " << input_cnode->fullname_with_scope()
  90. << " quant_type:" << input_cnode_quant_type;
  91. continue;
  92. }
  93. std::vector<AnfNodePtr> op_inputs = {value_node, input_cnode};
  94. auto quant_cast_cnode = graph->NewCNode(op_inputs);
  95. quant_cast_cnode->set_fullname_with_scope(cnode->fullname_with_scope() + "_quant_cast_" + std::to_string(i));
  96. cnode->set_input(i, quant_cast_cnode);
  97. MS_LOG(DEBUG) << "Add quant cast. "
  98. << "cur_node: " << cnode->fullname_with_scope() << " quant_type: " << curnode_quant_type
  99. << " input_" << i << ": " << input_cnode->fullname_with_scope()
  100. << " quant_type:" << input_cnode_quant_type;
  101. } else {
  102. MS_LOG(DEBUG) << "No need to add quant cast. "
  103. << "cur_node: " << cnode->fullname_with_scope() << " quant_type: " << curnode_quant_type
  104. << " input_" << i << ": " << input_cnode->fullname_with_scope()
  105. << " quant_type:" << input_cnode_quant_type;
  106. }
  107. }
  108. }
  109. return RET_OK;
  110. }
  111. } // namespace mindspore::lite::quant