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.

getitem_tuple.cc 2.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Copyright 2019 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 "pre_activate/pass/getitem_tuple.h"
  17. #include <memory>
  18. #include "operator/ops.h"
  19. #include "utils/utils.h"
  20. #include "pre_activate/common/helper.h"
  21. namespace mindspore {
  22. namespace opt {
  23. namespace {
  24. bool IsC(const BaseRef &n) {
  25. MS_EXCEPTION_IF_NULL(n);
  26. if (utils::isa<AnfNodePtr>(n)) {
  27. AnfNodePtr in = utils::cast<AnfNodePtr>(n);
  28. MS_EXCEPTION_IF_NULL(in);
  29. return in->isa<ValueNode>();
  30. } else {
  31. return false;
  32. }
  33. }
  34. } // namespace
  35. const BaseRef GetitemTuple::DefinePattern() const {
  36. VarPtr Xs = std::make_shared<SeqVar>();
  37. VarPtr C = std::make_shared<CondVar>(IsC);
  38. return VectorRef({prim::kPrimTupleGetItem, VectorRef({prim::kPrimMakeTuple, Xs}), C});
  39. }
  40. const AnfNodePtr GetitemTuple::Process(const FuncGraphPtr &, const AnfNodePtr &node, const EquivPtr &) const {
  41. MS_EXCEPTION_IF_NULL(node);
  42. CNodePtr tuple_getitem = node->cast<CNodePtr>();
  43. MS_EXCEPTION_IF_NULL(tuple_getitem);
  44. if (tuple_getitem->inputs().size() < kTupleGetitemInputNum) {
  45. MS_LOG(EXCEPTION) << "tuple getitem's input num is wrong";
  46. }
  47. AnfNodePtr make_tuple_anf = tuple_getitem->input(kRealInputNodeIndexInTupleGetItem);
  48. MS_EXCEPTION_IF_NULL(make_tuple_anf);
  49. AnfNodePtr index_node = tuple_getitem->input(kInputNodeOutputIndexInTupleGetItem);
  50. MS_EXCEPTION_IF_NULL(index_node);
  51. if (IsValueNode<Int32Imm>(index_node)) {
  52. ValueNodePtr value_node = index_node->cast<ValueNodePtr>();
  53. MS_EXCEPTION_IF_NULL(value_node);
  54. int index = GetValue<int>(value_node->value());
  55. CNodePtr make_tuple = make_tuple_anf->cast<CNodePtr>();
  56. MS_EXCEPTION_IF_NULL(make_tuple);
  57. if (make_tuple->inputs().size() > IntToSize(index + 1)) {
  58. auto ret = make_tuple->input(IntToSize(index + 1));
  59. MS_EXCEPTION_IF_NULL(ret);
  60. return ret;
  61. }
  62. }
  63. return nullptr;
  64. }
  65. } // namespace opt
  66. } // namespace mindspore