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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "backend/optimizer/pass/getitem_tuple.h"
  17. #include <memory>
  18. #include "base/core_ops.h"
  19. #include "utils/utils.h"
  20. #include "backend/optimizer/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. CheckCNodeInputSize(tuple_getitem, kTupleGetItemInputTensorNum);
  45. AnfNodePtr make_tuple_anf = tuple_getitem->input(kRealInputNodeIndexInTupleGetItem);
  46. MS_EXCEPTION_IF_NULL(make_tuple_anf);
  47. AnfNodePtr index_node = tuple_getitem->input(kInputNodeOutputIndexInTupleGetItem);
  48. MS_EXCEPTION_IF_NULL(index_node);
  49. if (IsValueNode<Int64Imm>(index_node)) {
  50. ValueNodePtr value_node = index_node->cast<ValueNodePtr>();
  51. MS_EXCEPTION_IF_NULL(value_node);
  52. auto index = GetValue<int64_t>(value_node->value());
  53. CNodePtr make_tuple = make_tuple_anf->cast<CNodePtr>();
  54. MS_EXCEPTION_IF_NULL(make_tuple);
  55. if (make_tuple->inputs().size() > LongToSize(index + 1)) {
  56. auto ret = make_tuple->input(LongToSize(index + 1));
  57. MS_EXCEPTION_IF_NULL(ret);
  58. return ret;
  59. }
  60. }
  61. return nullptr;
  62. }
  63. } // namespace opt
  64. } // namespace mindspore