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.

device_manager.cc 10 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "frontend/parallel/device_manager.h"
  17. #include <algorithm>
  18. #include <string>
  19. #include <unordered_set>
  20. #include <vector>
  21. #include "frontend/parallel/step_parallel.h"
  22. #include "utils/log_adapter.h"
  23. namespace mindspore {
  24. namespace parallel {
  25. DeviceManagerPtr g_device_manager = nullptr;
  26. bool InitDevice(int64_t device_num, int64_t global_rank, const std::string &backend,
  27. const std::vector<int64_t> &stage) {
  28. if (device_num <= 0) {
  29. MS_LOG(ERROR) << "'device_num' must be positive.";
  30. return false;
  31. }
  32. if (global_rank < 0) {
  33. MS_LOG(ERROR) << "'global_rank' must be nonnegative.";
  34. return false;
  35. }
  36. if (device_num > MAX_DEVICE_NUM) {
  37. MS_LOG(ERROR) << "'device_num' must be no more than " << MAX_DEVICE_NUM << ".";
  38. return false;
  39. }
  40. // 'device_num_converted' must be the power of 2
  41. if ((LongToUlong(device_num) & LongToUlong(device_num - 1)) != 0) {
  42. MS_LOG(ERROR) << "'device_num' must be the power of 2.";
  43. return false;
  44. }
  45. if (global_rank >= device_num) {
  46. MS_LOG(ERROR) << "'global_rank' must be less than 'device_num'.";
  47. return false;
  48. }
  49. if ((backend != HCCL_BACKEND) && (backend != NCCL_BACKEND) && (backend != UNDEFINED_BACKEND)) {
  50. MS_LOG(ERROR) << "Invalid backend: " << backend;
  51. return false;
  52. }
  53. if (stage.empty()) {
  54. MS_LOG(ERROR) << "The size of stage must be positive";
  55. return false;
  56. }
  57. RankList devices, stage_map;
  58. for (int64_t i = 0; i < device_num; ++i) {
  59. devices.push_back(i);
  60. }
  61. int64_t summed_value = 0;
  62. for (auto begin = stage.begin(); begin != stage.end(); ++begin) {
  63. if (*begin <= 0) {
  64. MS_LOG(ERROR) << "The value in the pipeline stages should be positive value";
  65. return false;
  66. }
  67. summed_value += *begin;
  68. stage_map.push_back(*begin);
  69. }
  70. if (summed_value != device_num) {
  71. MS_LOG(ERROR) << "The sum of the pipeline stage :" << summed_value << " is not equal to the device_num "
  72. << device_num;
  73. return false;
  74. }
  75. for (auto &ele : stage_map) {
  76. MS_LOG(DEBUG) << "Obtained stage id: " << ele;
  77. }
  78. g_device_manager = std::make_shared<DeviceManager>();
  79. if (g_device_manager->Init(devices, global_rank, stage_map, backend) == SUCCESS) {
  80. MS_LOG(INFO) << "Device initialization succeeds.";
  81. return true;
  82. }
  83. MS_LOG(ERROR) << "Device initialization fails.";
  84. return false;
  85. }
  86. void CheckGlobalDeviceManager() {
  87. if (g_device_manager == nullptr) {
  88. MS_LOG(EXCEPTION) << "Device information has not been set!";
  89. }
  90. }
  91. int64_t GetListMemberByIndex(size_t index, const RankList &devices) {
  92. size_t i = 0;
  93. int64_t result = 0;
  94. if ((devices.empty()) || (index >= devices.size())) {
  95. MS_LOG(EXCEPTION) << "Index is out of the list scope";
  96. }
  97. auto it = devices.begin();
  98. for (; it != devices.end(); ++it) {
  99. if (i == index) {
  100. result = *it;
  101. break;
  102. }
  103. ++i;
  104. }
  105. return result;
  106. }
  107. std::shared_ptr<Device> GetListMemberByIndex(size_t index, const std::vector<std::shared_ptr<Device>> &device_list) {
  108. size_t i = 0;
  109. std::shared_ptr<Device> result;
  110. if ((device_list.empty()) || (index >= device_list.size())) {
  111. MS_LOG(EXCEPTION) << "Index is out of the list scope";
  112. }
  113. auto it = device_list.begin();
  114. for (; it != device_list.end(); ++it) {
  115. if (i == index) {
  116. result = *it;
  117. break;
  118. }
  119. ++i;
  120. }
  121. return result;
  122. }
  123. // E.g. devices = [0, 1, 2, 3, 4, 5, 6, 7], stage_map = [4, 4],
  124. // therefore the stage_devices_ = [[0, 1, 2, 3], [4, 5, 6, 7]].
  125. Status DeviceManager::Init(const RankList &devices, int64_t global_device_rank, const RankList &stage_map,
  126. const std::string &backend) {
  127. if ((backend != HCCL_BACKEND) && (backend != NCCL_BACKEND) && (backend != UNDEFINED_BACKEND)) {
  128. MS_LOG(ERROR) << "Invalid backend: " << backend;
  129. return FAILED;
  130. }
  131. if (stage_map.empty() || devices.empty()) {
  132. MS_LOG(ERROR) << "The size of stage_map and devices must be positive";
  133. return FAILED;
  134. }
  135. for (auto &dev : devices) {
  136. std::shared_ptr<Device> one = std::make_shared<Device>(dev);
  137. devices_.push_back(one);
  138. }
  139. size_t global_index = 0;
  140. for (auto &stage : stage_map) {
  141. int64_t num_device = stage;
  142. if (num_device > MAX_DEVICE_NUM) {
  143. MS_LOG(ERROR) << "The number of 'devices' in a stage must not be greater than " << MAX_DEVICE_NUM;
  144. return FAILED;
  145. }
  146. if (num_device <= 0) {
  147. MS_LOG(ERROR) << "The number of 'devices' in a stage must be positive";
  148. return FAILED;
  149. }
  150. RankList curr_dev_list;
  151. for (int64_t i = 0; i < num_device; ++i) {
  152. curr_dev_list.push_back(GetListMemberByIndex(global_index, devices));
  153. global_index++;
  154. }
  155. stage_devices_.push_back(curr_dev_list);
  156. }
  157. std::shared_ptr<Device> dev = std::make_shared<Device>(global_device_rank);
  158. device_ = dev;
  159. global_rank_ = global_device_rank;
  160. stage_num_ = static_cast<const int64_t>(stage_map.size());
  161. stage_id_ = global_device_rank / static_cast<const int64_t>(devices.size() / stage_map.size());
  162. rank_index_in_stage_ = global_rank_ - stage_id_ * (static_cast<const int64_t>(devices.size()) / stage_num_);
  163. stage_device_num_ = static_cast<const int64_t>(devices.size()) / stage_num_;
  164. backend_ = backend;
  165. if (backend == HCCL_BACKEND) {
  166. gm_.set_world_group(HCCL_WORLD_GROUP);
  167. } else if (backend_ == NCCL_BACKEND) {
  168. gm_.set_world_group(NCCL_WORLD_GROUP);
  169. } else {
  170. gm_.set_world_group(UNDEFINED_WORLD_GROUP);
  171. }
  172. MS_LOG(INFO) << "The device num: " << devices.size() << ", rank id: " << global_device_rank
  173. << ", the backend: " << backend << ", the stage num: " << stage_num_ << ", the stage id: " << stage_id_
  174. << ", the rank index in stage is: " << rank_index_in_stage_;
  175. return SUCCESS;
  176. }
  177. RankList DeviceManager::GetDeviceListInThisStage() const { return GetDeviceListByStageId(stage_id_); }
  178. RankList DeviceManager::GetDeviceListByStageId(int64_t stage_id) const {
  179. if (LongToSize(stage_id) >= stage_devices_.size())
  180. MS_LOG(ERROR) << "the 'stage_id': " << stage_id
  181. << ", is out of the scope of 'stage_devices_': " << stage_devices_.size();
  182. RankList res;
  183. int64_t index = 0;
  184. for (auto &stage : stage_devices_) {
  185. if (index == stage_id) {
  186. return stage;
  187. }
  188. index++;
  189. }
  190. return res;
  191. }
  192. Device DeviceManager::CreateNewDeviceByRank(int64_t rank) const { return Device(rank); }
  193. std::vector<Device> DeviceManager::CreateDeviceListByRankList(RankList ranks) {
  194. std::vector<Device> dev_list;
  195. for (auto &rank : ranks) {
  196. Device one = CreateNewDeviceByRank(rank);
  197. dev_list.push_back(one);
  198. }
  199. return dev_list;
  200. }
  201. DeviceManager &DeviceManager::GetInstance() {
  202. static DeviceManager instance = DeviceManager();
  203. return instance;
  204. }
  205. std::string DeviceManager::FindRankListNameByHashName(const std::string &hash_name) {
  206. std::string tmp = "WORLD_GROUP";
  207. if ((hash_name == HCCL_WORLD_GROUP) || (hash_name == NCCL_WORLD_GROUP)) {
  208. return tmp;
  209. }
  210. auto iter = group_to_rank_.find(hash_name);
  211. if (iter == group_to_rank_.end()) {
  212. MS_LOG(WARNING) << "Can not find the rank list name by hash name: " << hash_name;
  213. return tmp;
  214. }
  215. return iter->second;
  216. }
  217. std::string HashName(const std::string &origin_name) { return std::to_string(std::hash<string>{}(origin_name)); }
  218. // Group name is generated using the increasing ranks of the devices.
  219. // E.g. the devices' ranks are '<0, 5, 3, 7, 1>', and the generated group name
  220. // is '0-1-3-5-7'.
  221. std::string DeviceManager::GenerateGroupNameByRanks(RankList ranks) {
  222. std::string rank_list_name;
  223. std::vector<int64_t>::iterator it;
  224. std::sort(ranks.begin(), ranks.end()); // sorted in increasing order
  225. for (it = ranks.begin(); it != ranks.end(); ++it) {
  226. if (it == ranks.begin()) {
  227. rank_list_name = std::to_string(*it);
  228. } else {
  229. rank_list_name += "-" + std::to_string(*it);
  230. }
  231. }
  232. // hash rank-list-name and add ranks' size as prefix
  233. std::string group_hash_name = HashName(rank_list_name);
  234. std::string group_name = std::to_string(ranks.size()) + "-" + group_hash_name;
  235. if (rank_to_group_.find(rank_list_name) == rank_to_group_.end()) {
  236. if (group_to_rank_.find(group_name) == group_to_rank_.end()) {
  237. rank_to_group_[rank_list_name] = group_name;
  238. group_to_rank_[group_name] = rank_list_name;
  239. MS_LOG(INFO) << "The rank list name is " << rank_list_name << "nd group name is " << group_name;
  240. } else {
  241. MS_LOG(EXCEPTION) << "Hash collision, the current rank list: " << rank_list_name
  242. << "the old rank list:" << group_to_rank_.find(group_name)->second
  243. << "the group name: " << group_name;
  244. }
  245. }
  246. return group_name;
  247. }
  248. // Create the group with the given devices and the given name. The GroupManager
  249. // gm_ will create a new group only if there does not exit a group with the same
  250. // name. Otherwise, let the pointer g point to that group.
  251. Group DeviceManager::CreateGroup(const std::string &group_name,
  252. const std::vector<mindspore::parallel::Device> &devices) {
  253. Group g;
  254. (void)gm_.CreateGroup(group_name, devices, &g);
  255. return g;
  256. }
  257. // Create the group with only the given devices' ranks.
  258. Group DeviceManager::CreateGroup(const RankList &dev_ranks) {
  259. std::unordered_set<int64_t> rank_set(dev_ranks.begin(), dev_ranks.end());
  260. if (dev_ranks.size() != rank_set.size()) {
  261. MS_LOG(EXCEPTION) << "Invalid dev ranks(" << dev_ranks << "), it has the Duplicate elements in list";
  262. }
  263. std::string group_name = GenerateGroupNameByRanks(dev_ranks);
  264. auto dev_list = CreateDeviceListByRankList(dev_ranks);
  265. return CreateGroup(group_name, dev_list);
  266. }
  267. void DeviceManager::Clear() {
  268. devices_.clear();
  269. stage_devices_.clear();
  270. gm_.Clear();
  271. }
  272. } // namespace parallel
  273. } // namespace mindspore