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.

dshape.cc 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "pipeline/static_analysis/dshape.h"
  19. #include <exception>
  20. #include <iostream>
  21. #include "utils/log_adapter.h"
  22. namespace mindspore {
  23. namespace abstract {
  24. // used for print BaseShape content
  25. std::ostream &operator<<(std::ostream &os, const BaseShape &bs) {
  26. os << bs.ToString();
  27. return os;
  28. }
  29. std::ostream &operator<<(std::ostream &os, const std::shared_ptr<BaseShape> bs) {
  30. MS_EXCEPTION_IF_NULL(bs);
  31. os << bs->ToString();
  32. return os;
  33. }
  34. bool BaseShape::operator==(const BaseShape &other) const {
  35. if (tid() != other.tid()) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. bool BaseShape::operator!=(const BaseShape &other) const { return !(*this == other); }
  41. std::string Shape::ToString() const {
  42. std::ostringstream buffer;
  43. bool f_begin = true;
  44. buffer << "(";
  45. for (auto &x : shape_) {
  46. if (!f_begin) {
  47. buffer << ", ";
  48. } else {
  49. f_begin = false;
  50. }
  51. buffer << x;
  52. }
  53. buffer << ")";
  54. return buffer.str();
  55. }
  56. std::string Shape::DumpText() const {
  57. std::ostringstream buffer;
  58. buffer << "[";
  59. for (size_t i = 0; i < shape_.size(); i++) {
  60. buffer << (i > 0 ? ", " : "") << shape_[i];
  61. }
  62. buffer << "]";
  63. return buffer.str();
  64. }
  65. bool Shape::operator==(const BaseShape &other) const {
  66. if (tid() != other.tid()) {
  67. return false;
  68. }
  69. return shape_ == static_cast<const Shape &>(other).shape_;
  70. }
  71. const int Shape::SHP_ANY;
  72. void Shape::Broaden() {
  73. for (size_t i = 0; i < shape_.size(); i++) {
  74. shape_[i] = SHP_ANY;
  75. }
  76. }
  77. std::string SequeueShape::ToString() const {
  78. std::ostringstream buffer;
  79. bool f_begin = true;
  80. for (auto p_shp : p_shapes_) {
  81. if (!f_begin) {
  82. buffer << ", ";
  83. } else {
  84. f_begin = false;
  85. }
  86. MS_EXCEPTION_IF_NULL(p_shp);
  87. buffer << p_shp->ToString();
  88. }
  89. return buffer.str();
  90. }
  91. BaseShapePtrList SequeueShape::ElementsClone() const {
  92. BaseShapePtrList ele_list;
  93. for (auto p_shp : p_shapes_) {
  94. MS_EXCEPTION_IF_NULL(p_shp);
  95. ele_list.push_back(p_shp->Clone());
  96. }
  97. return ele_list;
  98. }
  99. template <typename T>
  100. bool SequeueShape::SequeueEqual(const BaseShape &other) const {
  101. if (tid() != other.tid()) {
  102. return false;
  103. }
  104. auto other_shapes = static_cast<const T &>(other).p_shapes_;
  105. if (other_shapes.size() != p_shapes_.size()) {
  106. return false;
  107. }
  108. for (unsigned int i = 0; i < p_shapes_.size(); ++i) {
  109. if (!(*p_shapes_[i] == *other_shapes[i])) {
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. template bool SequeueShape::SequeueEqual<TupleShape>(const BaseShape &) const;
  116. template bool SequeueShape::SequeueEqual<ListShape>(const BaseShape &) const;
  117. const std::shared_ptr<NoShape> kNoShape = std::make_shared<NoShape>();
  118. } // namespace abstract
  119. } // namespace mindspore