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

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