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.

graphdef_transform.cc 7.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright 2020-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. #include "tools/converter/graphdef_transform.h"
  17. #include <string>
  18. #include <algorithm>
  19. #include "schema/model_generated.h"
  20. #include "src/common/log_adapter.h"
  21. #include "tools/converter/converter_flags.h"
  22. #include "tools/converter/legacy_optimizer/graph/dtype_trans_pass.h"
  23. #include "tools/converter/legacy_optimizer/fusion/quant_cast_fusion_pass.h"
  24. #include "tools/converter/legacy_optimizer/graph/infershape_pass.h"
  25. #include "tools/converter/legacy_optimizer/graph/isolated_node_remove_pass.h"
  26. #include "tools/converter/legacy_optimizer/graph/dropout_node_remove_pass.h"
  27. #include "tools/converter/legacy_optimizer/graph/topological_sort_pass.h"
  28. #include "tools/converter/legacy_optimizer/graph/tensor_quant_pass.h"
  29. #include "tools/converter/legacy_optimizer/graph/tensor_name_pass.h"
  30. #include "tools/converter/legacy_optimizer/graph/infer_quant_param_pass.h"
  31. #include "tools/converter/legacy_optimizer/graph/set_unused_quant_param_to_default_pass.h"
  32. #include "tools/converter/legacy_optimizer/graph/convert_fp32_to_fp16_pass.h"
  33. #include "tools/converter/legacy_optimizer/graph/subgraph_node_pass.h"
  34. #include "tools/converter/legacy_optimizer/graph/subgraph_tensor_pass.h"
  35. using std::string;
  36. namespace mindspore::lite {
  37. GraphDefTransform::GraphDefTransform() = default;
  38. GraphDefTransform::~GraphDefTransform() { this->graph_defT_ = nullptr; }
  39. void GraphDefTransform::SetGraphDef(schema::MetaGraphT *dst_def) { graph_defT_ = dst_def; }
  40. namespace {
  41. std::vector<schema::CNodeT *> GetGraphNodes(const schema::MetaGraphT &graph_defT) {
  42. std::vector<schema::CNodeT *> old_nodes{};
  43. old_nodes.resize(graph_defT.nodes.size());
  44. std::transform(graph_defT.nodes.begin(), graph_defT.nodes.end(), old_nodes.begin(),
  45. [](const std::unique_ptr<schema::CNodeT> &node) { return node.get(); });
  46. return old_nodes;
  47. }
  48. int QuantTransform(const converter::Flags &ctx, schema::MetaGraphT *graph_defT) {
  49. MS_ASSERT(graph_defT != nullptr);
  50. // quantization
  51. if (ctx.commonQuantParam.quant_type != schema::QuantType_QUANT_ALL) {
  52. // quantization
  53. if (ctx.fmk != converter::kFmkTypeTf) {
  54. // init old node indices
  55. auto old_nodes = GetGraphNodes(*graph_defT);
  56. Optimizer tensor_quant_optimizer;
  57. tensor_quant_optimizer.AddPass(new (std::nothrow) TopologicalSortPass());
  58. tensor_quant_optimizer.AddPass(new (std::nothrow) InferQuantParamPass());
  59. tensor_quant_optimizer.AddPass(new (std::nothrow) InferShapePass(ctx.fmk));
  60. tensor_quant_optimizer.AddPass(new (std::nothrow) TensorQuantPass());
  61. tensor_quant_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
  62. auto status = tensor_quant_optimizer.Run(graph_defT);
  63. if (status != RET_OK) {
  64. MS_LOG(ERROR) << "DoQuantize failed!";
  65. return status;
  66. }
  67. }
  68. // quantization
  69. if (ctx.fmk != converter::kFmkTypeTf) {
  70. // init old node indices
  71. Optimizer quant_node_optimizer;
  72. quant_node_optimizer.AddPass(new (std::nothrow) TopologicalSortPass());
  73. auto old_nodes = GetGraphNodes(*graph_defT);
  74. quant_node_optimizer.AddPass(new (std::nothrow) InferShapePass(ctx.fmk));
  75. quant_node_optimizer.AddPass(new (std::nothrow) DTypeTransPass(ctx.inputDataType, ctx.outputDataType));
  76. quant_node_optimizer.AddPass(new (std::nothrow) QuantCastFusionPass());
  77. quant_node_optimizer.AddPass(new (std::nothrow) IsolatedNodeRemovePass());
  78. quant_node_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
  79. auto status = quant_node_optimizer.Run(graph_defT);
  80. if (status != RET_OK && status != RET_NO_CHANGE) {
  81. MS_LOG(ERROR) << "Run quant_node_optimizer graphPasses Failed";
  82. return status;
  83. }
  84. }
  85. }
  86. return RET_OK;
  87. }
  88. } // namespace
  89. int GraphDefTransform::Transform(const converter::Flags &ctx) {
  90. STATUS status;
  91. {
  92. auto old_nodes = GetGraphNodes(*graph_defT_);
  93. Optimizer unused_op_remove_optimizer;
  94. if (!ctx.trainModel) {
  95. unused_op_remove_optimizer.AddPass(new DropoutNodeRemovePass());
  96. }
  97. unused_op_remove_optimizer.AddPass(new IsolatedNodeRemovePass());
  98. unused_op_remove_optimizer.AddPass(new SubgraphNodePass(old_nodes));
  99. status = unused_op_remove_optimizer.Run(graph_defT_);
  100. if (status != RET_OK && status != RET_NO_CHANGE) {
  101. MS_LOG(ERROR) << "Run unused_op_remove_optimizer graphPasses Failed";
  102. return status;
  103. }
  104. }
  105. // format transpose global optimize
  106. {
  107. // init old node indices
  108. auto old_nodes = GetGraphNodes(*graph_defT_);
  109. Optimizer format_trans_optimizer;
  110. if (!ctx.trainModel && ctx.fmk != converter::kFmkTypeOnnx) {
  111. format_trans_optimizer.AddPass(new (std::nothrow) IsolatedNodeRemovePass());
  112. format_trans_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
  113. }
  114. status = format_trans_optimizer.Run(graph_defT_);
  115. if (status != RET_OK && status != RET_NO_CHANGE && status != RET_INFER_INVALID) {
  116. MS_LOG(ERROR) << "Run format_trans_optimizer graphPasses Failed";
  117. return status;
  118. }
  119. }
  120. auto ret = QuantTransform(ctx, graph_defT_);
  121. if (ret != RET_OK && status != RET_NO_CHANGE) {
  122. return status;
  123. }
  124. {
  125. Optimizer nested_loop_optimizer;
  126. auto old_nodes = GetGraphNodes(*graph_defT_);
  127. nested_loop_optimizer.AddPass(new (std::nothrow) IsolatedNodeRemovePass());
  128. nested_loop_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
  129. nested_loop_optimizer.AddPass(new (std::nothrow) SubgraphTensorPass());
  130. nested_loop_optimizer.AddPass(new (std::nothrow) SubgraphNodePass(old_nodes));
  131. nested_loop_optimizer.AddPass(new (std::nothrow) TopologicalSortPass());
  132. status = nested_loop_optimizer.Run(graph_defT_);
  133. if (status != RET_OK && status != RET_NO_CHANGE) {
  134. MS_LOG(ERROR) << "Run nested_loop_optimizer graphPasses Failed";
  135. return status;
  136. }
  137. }
  138. {
  139. Optimizer forming_model_optimizer;
  140. forming_model_optimizer.AddPass(new (std::nothrow) InferShapePass(ctx.fmk));
  141. forming_model_optimizer.AddPass(new (std::nothrow) SetUnusedQuantParamToDefaultPass());
  142. forming_model_optimizer.AddPass(new (std::nothrow) TensorNamePass());
  143. forming_model_optimizer.AddPass(new (std::nothrow) ConvertFP32ToFP16Pass(ctx.saveFP16));
  144. status = forming_model_optimizer.Run(graph_defT_);
  145. if (status != RET_OK) {
  146. MS_LOG(ERROR) << "Run InferShapeOptimizer graphPasses Failed.";
  147. return status;
  148. }
  149. }
  150. return RET_OK;
  151. }
  152. } // namespace mindspore::lite