1 /**
2 * Copyright 2020-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
17 #include "backend/common/session/kernel_build_client.h"
18 #include <memory>
19
20 namespace mindspore {
21 namespace kernel {
ReplaceStr(std::string * dest,const std::string & replace,char new_char)22 void ReplaceStr(std::string *dest, const std::string &replace, char new_char) {
23 std::string::size_type start = 0;
24 while ((start = (*dest).find(replace, start)) != std::string::npos) {
25 (*dest).replace(start, replace.size(), 1, new_char);
26 start++; // Replaced 1 character.
27 }
28 }
29
GetPyExe()30 std::string GetPyExe() {
31 // get real python executable path
32 auto ms_context = MsContext::GetInstance();
33 if (ms_context == nullptr) {
34 return kEnv;
35 }
36 auto env = ms_context->get_param<std::string>(MS_CTX_PYTHON_EXE_PATH);
37 if (env.empty()) {
38 return kEnv;
39 }
40 return env;
41 }
42
CompilerStart(int process_num,int wait_time,const std::string & platform)43 bool KernelBuildClient::CompilerStart(int process_num, int wait_time, const std::string &platform) {
44 // Start compiling..
45 auto res = SendRequest(kCompilerStart);
46 if (res != kAck) {
47 MS_LOG(ERROR) << "AKG/START failed, res: " << res;
48 return false;
49 }
50 std::string process_num_str = std::to_string(process_num);
51 res = SendRequest(process_num_str);
52 if (res != kAck) {
53 MS_LOG(ERROR) << "AKG/START(process_num) responds failed, res: " << res;
54 return false;
55 }
56 std::string wait_time_str = std::to_string(wait_time);
57 res = SendRequest(wait_time_str);
58 if (res != kAck) {
59 MS_LOG(ERROR) << "AKG/START(wait_time) responds failed, res: " << res;
60 return false;
61 }
62 res = SendRequest(platform);
63 if (res != kAck) {
64 MS_LOG(ERROR) << "AKG/START(platform) responds failed, res: " << res;
65 return false;
66 }
67 return true;
68 }
69
CompilerSendAttr(const std::string & attr)70 bool KernelBuildClient::CompilerSendAttr(const std::string &attr) {
71 auto res = SendRequest(kCompilerAttr);
72 if (res != kAck) {
73 MS_LOG(ERROR) << "COMPILER/ATTR failed, res: " << res;
74 return false;
75 }
76 res = SendRequest(attr);
77 if (res != kAck) {
78 MS_LOG(ERROR) << "COMPILER/ATTR.. responds failed, res: " << res << ", when sending [" << attr << "]";
79 return false;
80 }
81 return true;
82 }
83
CompilerSendData(const std::vector<std::string> & jsons)84 bool KernelBuildClient::CompilerSendData(const std::vector<std::string> &jsons) {
85 auto res = SendRequest(kCompilerData);
86 if (res != kAck) {
87 MS_LOG(ERROR) << "COMPILER/DATA failed, res: " << res;
88 return false;
89 }
90 for (auto &json : jsons) {
91 res = SendRequest(json);
92 if (res != kAck) {
93 MS_LOG(ERROR) << "COMPILER/DATA.. responds failed, res: " << res << ", when sending [" << json << "]";
94 return false;
95 }
96 }
97 return true;
98 }
99
100 // Fetch the result of AKG compiling.
CompilerWait()101 bool KernelBuildClient::CompilerWait() {
102 auto res = SendRequest(kCompilerWait);
103 if (res != kTrue) {
104 MS_LOG(ERROR) << "COMPILER/WAIT failed, res: " << res;
105 return false;
106 }
107 return true;
108 }
109
Instance()110 AkgKernelBuildClient &AkgKernelBuildClient::Instance() {
111 static AkgKernelBuildClient instance{};
112 return instance;
113 }
114
Instance()115 AkgV2KernelBuildClient &AkgV2KernelBuildClient::Instance() {
116 static AkgV2KernelBuildClient instance{};
117 return instance;
118 }
119 } // namespace kernel
120 } // namespace mindspore
121