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.

convert.h 2.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_CONVERT_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_CONVERT_H_
  18. #include <vector>
  19. #include "frontend/optimizer/optimizer.h"
  20. #include "frontend/optimizer/irpass.h"
  21. #include "frontend/optimizer/anf_visitor.h"
  22. #include "ir/func_graph.h"
  23. #include "frontend/operator/ops.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace irpass {
  27. // {prim::kPrimPrint, Xs} -> {prim::kPrimPrint, {prim::kPrinMakeTuple, Xs}}
  28. class PrintTupleWrapper : public AnfVisitor {
  29. public:
  30. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  31. if (!IsPrimitiveCNode(node, prim::kPrimPrint)) {
  32. return nullptr;
  33. }
  34. // already be {prim::kPrimPrint, {prim::kPrinMakeTuple, Xs}}
  35. auto cnode = node->cast<CNodePtr>();
  36. if (cnode->size() == 2 && IsPrimitiveCNode(cnode->input(1), prim::kPrimMakeTuple)) {
  37. return nullptr;
  38. }
  39. std::vector<AnfNodePtr> args;
  40. args.push_back(NewValueNode(prim::kPrimMakeTuple));
  41. // {prim::kPrimPrint, Xs}
  42. auto &inputs = cnode->inputs();
  43. (void)args.insert(args.end(), inputs.begin() + 1, inputs.end());
  44. // {prim::kPrinMakeTuple, Xs}
  45. auto fg = node->func_graph();
  46. auto tuple = NewCNode(args, fg);
  47. auto print = GetValueNode<PrimitivePtr>(cnode->input(0));
  48. return NewCNode({NewValueNode(print), tuple}, fg);
  49. }
  50. };
  51. } // namespace irpass
  52. } // namespace opt
  53. } // namespace mindspore
  54. #endif // #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_CONVERT_H_