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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "abstract/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. if (shape_[i] == SHP_ANY && min_shape_.size() == shape_.size() && max_shape_.size() == shape_.size()) {
  62. buffer << "_" << min_shape_[i] << "^" << max_shape_[i];
  63. }
  64. }
  65. buffer << "]";
  66. return buffer.str();
  67. }
  68. bool Shape::operator==(const BaseShape &other) const {
  69. if (tid() != other.tid()) {
  70. return false;
  71. }
  72. return shape_ == static_cast<const Shape &>(other).shape_;
  73. }
  74. const int Shape::SHP_ANY;
  75. void Shape::Broaden() {
  76. for (size_t i = 0; i < shape_.size(); i++) {
  77. shape_[i] = SHP_ANY;
  78. }
  79. }
  80. std::string SequeueShape::ToString() const {
  81. std::ostringstream buffer;
  82. bool f_begin = true;
  83. for (auto p_shp : p_shapes_) {
  84. if (!f_begin) {
  85. buffer << ", ";
  86. } else {
  87. f_begin = false;
  88. }
  89. MS_EXCEPTION_IF_NULL(p_shp);
  90. buffer << p_shp->ToString();
  91. }
  92. return buffer.str();
  93. }
  94. BaseShapePtrList SequeueShape::ElementsClone() const {
  95. BaseShapePtrList ele_list;
  96. for (auto p_shp : p_shapes_) {
  97. MS_EXCEPTION_IF_NULL(p_shp);
  98. ele_list.push_back(p_shp->Clone());
  99. }
  100. return ele_list;
  101. }
  102. template <typename T>
  103. bool SequeueShape::SequeueEqual(const BaseShape &other) const {
  104. if (tid() != other.tid()) {
  105. return false;
  106. }
  107. auto other_shapes = static_cast<const T &>(other).p_shapes_;
  108. if (other_shapes.size() != p_shapes_.size()) {
  109. return false;
  110. }
  111. for (unsigned int i = 0; i < p_shapes_.size(); ++i) {
  112. if (!(*p_shapes_[i] == *other_shapes[i])) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. template bool SequeueShape::SequeueEqual<TupleShape>(const BaseShape &) const;
  119. template bool SequeueShape::SequeueEqual<ListShape>(const BaseShape &) const;
  120. const std::shared_ptr<NoShape> kNoShape = std::make_shared<NoShape>();
  121. } // namespace abstract
  122. } // namespace mindspore