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

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. namespace mindspore {
  20. namespace abstract {
  21. namespace {
  22. std::string ShapeVectorToStr(const std::vector<int64_t> &shp) {
  23. std::ostringstream buffer;
  24. bool f_begin = true;
  25. buffer << "(";
  26. for (auto &x : shp) {
  27. if (!f_begin) {
  28. buffer << ", ";
  29. } else {
  30. f_begin = false;
  31. }
  32. buffer << x;
  33. }
  34. buffer << ")";
  35. return buffer.str();
  36. }
  37. } // namespace
  38. // used for print BaseShape content
  39. std::ostream &operator<<(std::ostream &os, const BaseShape &bs) {
  40. os << bs.ToString();
  41. return os;
  42. }
  43. std::ostream &operator<<(std::ostream &os, const std::shared_ptr<BaseShape> bs) {
  44. MS_EXCEPTION_IF_NULL(bs);
  45. os << bs->ToString();
  46. return os;
  47. }
  48. bool BaseShape::operator==(const BaseShape &other) const { return tid() == other.tid(); }
  49. bool BaseShape::operator!=(const BaseShape &other) const { return !(*this == other); }
  50. std::string Shape::ToString() const {
  51. std::ostringstream buffer;
  52. bool has_dyn_shape = IsDynamic();
  53. if (has_dyn_shape) {
  54. buffer << "{shape:";
  55. }
  56. buffer << ShapeVectorToStr(shape_);
  57. if (has_dyn_shape) {
  58. buffer << "|min shape:";
  59. buffer << ShapeVectorToStr(min_shape_);
  60. buffer << "|max shape:";
  61. buffer << ShapeVectorToStr(max_shape_);
  62. buffer << "}";
  63. }
  64. return buffer.str();
  65. }
  66. std::string Shape::DumpText() const {
  67. std::ostringstream buffer;
  68. buffer << "[";
  69. for (size_t i = 0; i < shape_.size(); i++) {
  70. buffer << (i > 0 ? ", " : "") << shape_[i];
  71. if (shape_[i] == SHP_ANY && min_shape_.size() == shape_.size() && max_shape_.size() == shape_.size()) {
  72. buffer << "_" << min_shape_[i] << "^" << max_shape_[i];
  73. }
  74. }
  75. buffer << "]";
  76. return buffer.str();
  77. }
  78. bool Shape::operator==(const BaseShape &other) const {
  79. if (tid() != other.tid()) {
  80. return false;
  81. }
  82. Shape other_shape = static_cast<const Shape &>(other);
  83. if (shape_ != other_shape.shape_) {
  84. return false;
  85. }
  86. if (!IsDynamic() || !other_shape.IsDynamic()) {
  87. return true;
  88. }
  89. return (min_shape_ == other_shape.min_shape_) && (max_shape_ == other_shape.max_shape_);
  90. }
  91. const int64_t Shape::SHP_ANY;
  92. void Shape::Broaden() {
  93. for (size_t i = 0; i < shape_.size(); i++) {
  94. shape_[i] = SHP_ANY;
  95. }
  96. }
  97. std::string SequenceShape::ToString() const {
  98. std::ostringstream buffer;
  99. bool f_begin = true;
  100. for (const auto &p_shp : p_shapes_) {
  101. if (!f_begin) {
  102. buffer << ", ";
  103. } else {
  104. f_begin = false;
  105. }
  106. MS_EXCEPTION_IF_NULL(p_shp);
  107. buffer << p_shp->ToString();
  108. }
  109. return buffer.str();
  110. }
  111. BaseShapePtrList SequenceShape::ElementsClone() const {
  112. BaseShapePtrList ele_list;
  113. for (auto p_shp : p_shapes_) {
  114. MS_EXCEPTION_IF_NULL(p_shp);
  115. ele_list.push_back(p_shp->Clone());
  116. }
  117. return ele_list;
  118. }
  119. template bool SequenceShape::SequenceEqual<TupleShape>(const BaseShape &) const;
  120. template bool SequenceShape::SequenceEqual<ListShape>(const BaseShape &) const;
  121. } // namespace abstract
  122. } // namespace mindspore