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.

partial_eliminate.h 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_PARTIAL_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_PARTIAL_ELIMINATE_H_
  18. #include <vector>
  19. #include <algorithm>
  20. #include <memory>
  21. #include "frontend/optimizer/irpass.h"
  22. #include "frontend/optimizer/optimizer.h"
  23. #include "frontend/optimizer/anf_visitor.h"
  24. #include "frontend/operator/ops.h"
  25. namespace mindspore {
  26. namespace opt {
  27. namespace irpass {
  28. // {{prim::kPrimPartial, X, Xs}, Ys} -> {X, Xs, Ys}
  29. class PartialEliminater : public AnfVisitor {
  30. public:
  31. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  32. if (!node->isa<CNode>() || node->func_graph() == nullptr) {
  33. return nullptr;
  34. }
  35. Xs_.clear();
  36. auto &inputs = node->cast<CNodePtr>()->inputs();
  37. Visit(inputs[0]);
  38. if (Xs_.size() == 0) {
  39. return nullptr;
  40. }
  41. // {X, Xs, Ys}
  42. std::vector<AnfNodePtr> args{};
  43. (void)std::copy(Xs_.begin(), Xs_.end(), std::back_inserter(args));
  44. (void)std::copy(inputs.begin() + 1, inputs.end(), std::back_inserter(args));
  45. TraceManager::DebugTrace(std::make_shared<TracePartialTransform>(node->debug_info()));
  46. auto new_node = node->func_graph()->NewCNode(args);
  47. TraceManager::EndTrace();
  48. return new_node;
  49. }
  50. void Visit(const AnfNodePtr &node) override {
  51. if (!IsPrimitiveCNode(node, prim::kPrimPartial)) {
  52. return;
  53. }
  54. auto &inputs = node->cast<CNodePtr>()->inputs();
  55. // {prim::kPrimPartial, X, Xs}
  56. if (inputs.size() < 2) {
  57. return;
  58. }
  59. // fill Xs
  60. (void)std::copy(inputs.begin() + 1, inputs.end(), std::back_inserter(Xs_));
  61. }
  62. private:
  63. std::vector<AnfNodePtr> Xs_{};
  64. };
  65. } // namespace irpass
  66. } // namespace opt
  67. } // namespace mindspore
  68. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_PARTIAL_ELIMINATE_H_