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

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