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.

map.cc 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "parallel/tensor_layout/map.h"
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <utility>
  20. #include "common/utils.h"
  21. #include "parallel/status.h"
  22. #include "parallel/tensor_layout/shape_util.h"
  23. #include "utils/convert_utils.h"
  24. #include "utils/log_adapter.h"
  25. namespace mindspore {
  26. namespace parallel {
  27. Status Map::Init(const std::vector<int32_t> &array) {
  28. Status status = Array::Init(array);
  29. if (status != Status::SUCCESS) {
  30. return Status::FAILED;
  31. }
  32. if (!IsValidMap()) {
  33. MS_LOG(ERROR) << "invalid map " << this->ToString();
  34. return Status::FAILED;
  35. }
  36. return Status::SUCCESS;
  37. }
  38. bool Map::IsValidMap() {
  39. if (std::any_of(array_.begin(), array_.end(), [](int32_t value) { return ((value < 0) && (value != MAP_NONE)); })) {
  40. return false;
  41. }
  42. // check that all none -1 value in array_ is different
  43. std::vector<int32_t> sorted_array = array_;
  44. std::sort(sorted_array.begin(), sorted_array.end());
  45. int32_t value = MAP_NONE;
  46. for (auto &element : sorted_array) {
  47. if (element == MAP_NONE) {
  48. continue;
  49. }
  50. if (element == value) {
  51. return false;
  52. }
  53. value = element;
  54. }
  55. return true;
  56. }
  57. int32_t Map::GetMaxItem() const {
  58. if (!array_.empty()) {
  59. return *std::max_element(array_.begin(), array_.end());
  60. } else {
  61. return MAP_NONE;
  62. }
  63. }
  64. int32_t Map::GetIndexByValue(int32_t value) const {
  65. auto iter = find(array_.begin(), array_.end(), value);
  66. if (iter != array_.end()) {
  67. return static_cast<int32_t>(std::distance(array_.begin(), iter));
  68. } else {
  69. return MAP_NONE;
  70. }
  71. }
  72. /*
  73. * expand.size() should be equal to array_.size()
  74. */
  75. std::shared_ptr<Map> Map::ExpandMapByNone(const Arrangement &expand_num_list) const {
  76. if (expand_num_list.GetDimSize() != GetDimSize()) {
  77. return nullptr;
  78. }
  79. std::vector<int32_t> new_shape;
  80. for (uint32_t i = 0; i != GetDimSize(); i++) {
  81. if (GetDimByIdx(i) == MAP_NONE) {
  82. for (int32_t j = 0; j < expand_num_list.GetDimByIdx(i); j++) {
  83. new_shape.push_back(MAP_NONE);
  84. }
  85. } else {
  86. new_shape.push_back(GetDimByIdx(i));
  87. int32_t j = 1;
  88. while (j < expand_num_list.GetDimByIdx(i)) {
  89. new_shape.push_back(MAP_NONE);
  90. j++;
  91. }
  92. }
  93. }
  94. auto map_new = std::make_shared<Map>();
  95. (void)map_new->Init(new_shape);
  96. return map_new;
  97. }
  98. /*
  99. * expand.size() should be equal to array_.size()
  100. */
  101. std::shared_ptr<Map> Map::ExpandMapByDecreaseNumber(const Arrangement &expand_num_list) const {
  102. if (GetMaxItem() >= static_cast<int32_t>(expand_num_list.GetDimSize())) {
  103. return nullptr;
  104. }
  105. std::vector<int32_t> new_shape;
  106. for (uint32_t i = 0; i < GetDimSize(); i++) {
  107. if (GetDimByIdx(i) == MAP_NONE) {
  108. new_shape.push_back(MAP_NONE);
  109. } else {
  110. int32_t start_map =
  111. expand_num_list.ComputeReverseAccumulateSumInReverseOrder()[static_cast<uint32_t>(GetDimByIdx(i))];
  112. for (int32_t k = expand_num_list.GetDimByReverseIdx(static_cast<uint32_t>(GetDimByIdx(i))) - 1; k >= 0; k--) {
  113. new_shape.push_back(k + start_map);
  114. }
  115. }
  116. }
  117. auto map_new = std::make_shared<Map>();
  118. (void)map_new->Init(new_shape);
  119. return map_new;
  120. }
  121. std::shared_ptr<std::vector<Arrangement>> Map::ReMapVector(const std::vector<Arrangement> &input_vector) const {
  122. if (GetMaxItem() >= static_cast<int32_t>(input_vector.size())) {
  123. return nullptr;
  124. }
  125. std::vector<Arrangement> out;
  126. Arrangement empty_arrangement;
  127. for (uint32_t i = 0; i < GetDimSize(); i++) {
  128. if (GetDimByIdx(i) == MAP_NONE) {
  129. out.push_back(empty_arrangement);
  130. } else {
  131. out.push_back(input_vector[IntToUint(SizeToInt(input_vector.size()) - 1 - GetDimByIdx(i))]);
  132. }
  133. }
  134. return std::make_shared<std::vector<Arrangement>>(out);
  135. }
  136. bool Map::CheckNoneByIdxList(std::vector<size_t> idx_list) const {
  137. for (auto &value : idx_list) {
  138. if (GetDimByIdx(SizeToUint(value)) != MAP_NONE) {
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. Map Map::SqueezeMapByIdxList(std::vector<size_t> idx_list) const {
  145. std::vector<int32_t> out_shape;
  146. for (size_t i = 0; i < GetDimSize(); i++) {
  147. auto it = std::find(idx_list.begin(), idx_list.end(), i);
  148. if (it == idx_list.end()) {
  149. out_shape.push_back(GetDimByIdx(SizeToUint(i)));
  150. }
  151. }
  152. if (out_shape.empty()) {
  153. MS_LOG(ERROR) << "out_shape size is 0, this may not happen under current situation";
  154. out_shape.push_back(MAP_NONE);
  155. }
  156. Map out;
  157. (void)out.Init(out_shape);
  158. return out;
  159. }
  160. } // namespace parallel
  161. } // namespace mindspore