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

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