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

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