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

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