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.7 kB

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