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.

kernel_build_client.cc 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Copyright 2020 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 "backend/session/kernel_build_client.h"
  17. #include <memory>
  18. namespace mindspore {
  19. namespace kernel {
  20. inline static bool init_flag = false;
  21. void ReplaceStr(std::string *dest, const std::string &replace, char new_char) {
  22. std::string::size_type start = 0;
  23. while ((start = (*dest).find(replace, start)) != std::string::npos) {
  24. (*dest).replace(start, replace.size(), 1, new_char);
  25. start++; // Replaced 1 character.
  26. }
  27. }
  28. bool AscendKernelBuildClient::TbePre() {
  29. auto res = SendRequest(kTbePre);
  30. if (res.find(kSuccess) == res.npos) {
  31. MS_LOG(EXCEPTION) << "PRE failed, res: " << res;
  32. }
  33. MS_LOG(INFO) << "Pre " << res;
  34. return true;
  35. }
  36. int AscendKernelBuildClient::TbeStart(const std::string &json) {
  37. if (!init_flag) {
  38. if (!TbePre()) {
  39. MS_LOG(EXCEPTION) << "START failed";
  40. }
  41. init_flag = true;
  42. }
  43. // Start compiling..
  44. auto res = SendRequest(kTbeStart);
  45. if (res != kAck) {
  46. MS_LOG(ERROR) << "START failed, res: " << res;
  47. return -1;
  48. }
  49. // Send the json data.
  50. res = SendRequest(json);
  51. if (res == kFailed) {
  52. MS_LOG(ERROR) << "TBE/START responds failed, res: " << res;
  53. return -1;
  54. }
  55. // Return task id.
  56. return std::stoi(res);
  57. }
  58. bool AscendKernelBuildClient::TbeWait(int *task_id, std::string *task_result, std::string *pre_build_result) {
  59. // Start waiting..
  60. auto res = SendRequest(kTbeWait);
  61. if (res != kAck) {
  62. MS_LOG(ERROR) << "TBE/WAIT failed, res: " << res;
  63. return false;
  64. }
  65. // Request task id.
  66. *task_id = std::stoi(SendRequest(kContinue));
  67. // Request task result.
  68. *task_result = SendRequest(kContinue);
  69. // Request prebuild result.
  70. *pre_build_result = SendRequest(kContinue);
  71. return true;
  72. }
  73. void AscendKernelBuildClient::TbeReset() {
  74. // Start compiling..
  75. init_flag = false;
  76. auto res = SendRequest(kTbeReset);
  77. if (res != kAck) {
  78. MS_LOG(EXCEPTION) << "TBE/RESET response is: " << res;
  79. }
  80. }
  81. bool AscendKernelBuildClient::AkgStart(int process_num, int wait_time) {
  82. // Start compiling..
  83. auto res = SendRequest(kAkgStart);
  84. if (res != kAck) {
  85. MS_LOG(ERROR) << "AKG/START failed, res: " << res;
  86. return false;
  87. }
  88. std::string process_num_str = std::to_string(process_num);
  89. res = SendRequest(process_num_str);
  90. if (res != kAck) {
  91. MS_LOG(ERROR) << "AKG/START(process_num) responds failed, res: " << res;
  92. return false;
  93. }
  94. std::string wait_time_str = std::to_string(wait_time);
  95. res = SendRequest(wait_time_str);
  96. if (res != kAck) {
  97. MS_LOG(ERROR) << "AKG/START(wait_time) responds failed, res: " << res;
  98. return false;
  99. }
  100. return true;
  101. }
  102. bool AscendKernelBuildClient::AkgSendData(const std::vector<std::string> &jsons) {
  103. auto res = SendRequest(kAkgData);
  104. if (res != kAck) {
  105. MS_LOG(ERROR) << "AKG/DATA failed, res: " << res;
  106. return false;
  107. }
  108. for (auto &json : jsons) {
  109. res = SendRequest(json);
  110. if (res != kAck) {
  111. MS_LOG(ERROR) << "AKG/DATA.. responds failed, res: " << res << ", when sending [" << json << "]";
  112. return false;
  113. }
  114. }
  115. return true;
  116. }
  117. // Fetch the result of AKG compiling.
  118. bool AscendKernelBuildClient::AkgWait() {
  119. auto res = SendRequest(kAkgWait);
  120. if (res != kTrue) {
  121. MS_LOG(ERROR) << "AKG/WAIT failed, res: " << res;
  122. return false;
  123. }
  124. return true;
  125. }
  126. std::string AscendKernelBuildClient::SelectFormat(const std::string &json) {
  127. // Start compiling..
  128. auto res = SendRequest(kFormat);
  129. if (res != kAck) {
  130. MS_LOG(ERROR) << "FORMAT failed, res: " << res;
  131. return "";
  132. }
  133. // Send the json data.
  134. res = SendRequest(json);
  135. if (res == kErr) {
  136. MS_LOG(ERROR) << "FORMAT responds failed, res: " << res;
  137. return "";
  138. }
  139. return res;
  140. }
  141. bool AscendKernelBuildClient::CheckSupported(const std::string &json) {
  142. // Checking support..
  143. auto res = SendRequest(kSupport);
  144. if (res != kAck) {
  145. MS_LOG(ERROR) << "SUPPORT failed, res: " << res;
  146. return false;
  147. }
  148. // Send the json data.
  149. res = SendRequest(json);
  150. if (res != kTrue) {
  151. MS_LOG(INFO) << "SUPPORT responds failed, res: " << res;
  152. return false;
  153. }
  154. return true;
  155. }
  156. int GpuKernelBuildClient::AkgGetPid() {
  157. auto res = SendRequest(kAkgPid);
  158. if (res == kErr) {
  159. MS_LOG(ERROR) << "AKG/PID failed, res: " << res;
  160. return -1;
  161. }
  162. return std::stoi(res);
  163. }
  164. bool GpuKernelBuildClient::AkgCompileSingle(const std::string json) {
  165. auto res = SendRequest(kAkgCompileOp);
  166. if (res != kAck) {
  167. MS_LOG(ERROR) << "AKG/COMPILE failed, res: " << res;
  168. return false;
  169. }
  170. // Send single json data.
  171. res = SendRequest(json);
  172. if (res != kAck) {
  173. MS_LOG(ERROR) << "AKG/COMPILE responds failed, res: " << res;
  174. return false;
  175. }
  176. return true;
  177. }
  178. } // namespace kernel
  179. } // namespace mindspore