• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 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 <string>
17 #include "transform/symbol/acl_base_symbol.h"
18 #include "transform/symbol/acl_compiler_symbol.h"
19 #include "transform/symbol/acl_mdl_symbol.h"
20 #include "transform/symbol/acl_op_symbol.h"
21 #include "transform/symbol/acl_prof_symbol.h"
22 #include "transform/symbol/acl_rt_allocator_symbol.h"
23 #include "transform/symbol/acl_rt_symbol.h"
24 #include "transform/symbol/acl_symbol.h"
25 #include "transform/symbol/acl_tdt_symbol.h"
26 #include "transform/symbol/symbol_utils.h"
27 
28 namespace mindspore {
29 namespace transform {
30 
31 static bool load_ascend_api = false;
32 
GetLibHandler(const std::string & lib_path)33 void *GetLibHandler(const std::string &lib_path) {
34   auto handler = dlopen(lib_path.c_str(), RTLD_LAZY | RTLD_LOCAL);
35   if (handler == nullptr) {
36     MS_LOG(INFO) << "Dlopen " << lib_path << " failed!" << dlerror();
37   }
38   return handler;
39 }
40 
GetAscendPath()41 std::string GetAscendPath() {
42   Dl_info info;
43   if (dladdr(reinterpret_cast<void *>(aclrtMalloc), &info) == 0) {
44     MS_LOG(INFO) << "Get dladdr failed, skip.";
45     return "";
46   }
47   auto path_tmp = std::string(info.dli_fname);
48   const std::string kLib64 = "lib64";
49   auto pos = path_tmp.find(kLib64);
50   if (pos == std::string::npos) {
51     MS_EXCEPTION(ValueError) << "Get ascend path failed, please check the run package.";
52   }
53   return path_tmp.substr(0, pos);
54 }
55 
LoadAscendApiSymbols()56 void LoadAscendApiSymbols() {
57   if (load_ascend_api) {
58     MS_LOG(INFO) << "Ascend api is already loaded.";
59     return;
60   }
61   std::string ascend_path = GetAscendPath();
62   LoadAclBaseApiSymbol(ascend_path);
63   LoadAclOpCompilerApiSymbol(ascend_path);
64   LoadAclMdlApiSymbol(ascend_path);
65   LoadAclOpApiSymbol(ascend_path);
66   LoadProfApiSymbol(ascend_path);
67   LoadAclAllocatorApiSymbol(ascend_path);
68   LoadAclRtApiSymbol(ascend_path);
69   LoadAclApiSymbol(ascend_path);
70   LoadAcltdtApiSymbol(ascend_path);
71   load_ascend_api = true;
72   MS_LOG(INFO) << "Load ascend api success!";
73 }
74 
75 }  // namespace transform
76 }  // namespace mindspore
77