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

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