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.

optimizer.cc 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/optimizer.h"
  17. #include "src/common/log_adapter.h"
  18. namespace mindspore {
  19. namespace lite {
  20. Optimizer::~Optimizer() {
  21. for (auto pass : graphPasses) {
  22. if (pass != nullptr) {
  23. delete (pass);
  24. }
  25. }
  26. for (auto pass : nodePasses) {
  27. if (pass != nullptr) {
  28. delete (pass);
  29. }
  30. }
  31. }
  32. void Optimizer::AddPass(GraphPass *graphPass) {
  33. if (graphPass != nullptr) {
  34. this->graphPasses.emplace_back(graphPass);
  35. }
  36. }
  37. void Optimizer::AddPass(NodePass *nodePass) {
  38. if (nodePass != nullptr) {
  39. this->nodePasses.emplace_back(nodePass);
  40. }
  41. }
  42. STATUS Optimizer::Run(schema::MetaGraphT *graphDefT) {
  43. STATUS status;
  44. bool ifNotChanged = true;
  45. // each node should go through all node pass not each node pass go through all node
  46. for (auto &opDef : graphDefT->nodes) {
  47. for (auto pass : this->nodePasses) {
  48. status = pass->Run(new GraphNode(graphDefT, opDef.get()));
  49. if (status != RET_OK && status != RET_NO_CHANGE && status != RET_INFER_INVALID) {
  50. MS_LOG(ERROR) << "Run NodePass failed";
  51. return status;
  52. } else {
  53. if (status == RET_OK) {
  54. ifNotChanged = false;
  55. }
  56. }
  57. }
  58. }
  59. for (auto pass : this->graphPasses) {
  60. status = pass->Run(graphDefT);
  61. if (status != RET_OK && status != RET_NO_CHANGE && status != RET_INFER_INVALID) {
  62. MS_LOG(ERROR) << "Run GraphPass failed";
  63. return status;
  64. } else {
  65. if (status == RET_OK) {
  66. ifNotChanged = false;
  67. }
  68. }
  69. }
  70. return ifNotChanged ? RET_NO_CHANGE : RET_OK;
  71. }
  72. } // namespace lite
  73. } // namespace mindspore