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.

cast_eliminate.cc 2.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 "frontend/optimizer/irpass/cast_eliminate.h"
  17. #include "frontend/optimizer/irpass.h"
  18. #include "frontend/optimizer/anf_visitor.h"
  19. #include "frontend/operator/ops.h"
  20. #include "ir/func_graph.h"
  21. #include "pipeline/jit/parse/data_converter.h"
  22. #include "pipeline/jit/parse/python_adapter.h"
  23. namespace mindspore {
  24. namespace opt {
  25. namespace irpass {
  26. // {prim::kPrimCast, X, T}
  27. AnfNodePtr CastSameTypeEliminater::operator()(const OptimizerPtr &, const AnfNodePtr &node) {
  28. Reset();
  29. AnfVisitor::Match(prim::kPrimCast, {IsNode, IsVNode})(node);
  30. // check pattern match
  31. if (tgt_ == nullptr) {
  32. return nullptr;
  33. }
  34. // src type check
  35. auto src_type = src_->Type();
  36. if (src_type == nullptr || !src_type->isa<TensorType>()) {
  37. return nullptr;
  38. }
  39. src_type = src_type->cast<TensorTypePtr>()->element();
  40. // tgt type check
  41. auto tgt_type = GetValueNode<TypePtr>(tgt_);
  42. if (tgt_type->isa<TensorType>()) {
  43. tgt_type = tgt_type->cast<TensorTypePtr>()->element();
  44. }
  45. if (src_type->type_id() == tgt_type->type_id()) {
  46. return src_;
  47. }
  48. return nullptr;
  49. }
  50. void CastSameTypeEliminater::Visit(const AnfNodePtr &node) {
  51. if (src_ == nullptr) {
  52. src_ = node;
  53. } else {
  54. tgt_ = node;
  55. }
  56. }
  57. // {prim::kPrimCast, {prim::kPrimCast, X, Y}, T}
  58. AnfNodePtr TwoCastEliminater::operator()(const OptimizerPtr &, const AnfNodePtr &node) {
  59. Reset();
  60. AnfVisitor::Match(prim::kPrimCast, {IsCNode, IsNode})(node);
  61. if (x_ != nullptr && t_ != nullptr) {
  62. auto cast_op = parse::python_adapter::GetPyFn("mindspore.ops.operations", "Cast")();
  63. ValuePtr cast = parse::data_converter::PyDataToValue(cast_op);
  64. auto cnode = NewCNode({NewValueNode(cast), x_, t_}, node->func_graph());
  65. cnode->set_abstract(node->abstract());
  66. return cnode;
  67. }
  68. return nullptr;
  69. }
  70. void TwoCastEliminater::Visit(const AnfNodePtr &node) {
  71. if (IsPrimitiveCNode(node, prim::kPrimCast)) {
  72. auto cnode = node->cast<CNodePtr>();
  73. // {prim::kPrimCast, X, Y}
  74. if (cnode->size() != 3) {
  75. return;
  76. }
  77. x_ = cnode->input(1);
  78. } else {
  79. t_ = node;
  80. }
  81. }
  82. } // namespace irpass
  83. } // namespace opt
  84. } // namespace mindspore