• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_ASCEND_KERNEL_COMPILE_H_
17 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_ASCEND_KERNEL_COMPILE_H_
18 #include <string>
19 #include <map>
20 #include <tuple>
21 #include <set>
22 #include <memory>
23 #include <vector>
24 #include <utility>
25 #include "ir/anf.h"
26 #include "backend/kernel_compiler/kernel.h"
27 #include "backend/kernel_compiler/kernel_fusion.h"
28 #include "backend/kernel_compiler/tbe/tbe_kernel_build.h"
29 #include "backend/kernel_compiler/tbe/tbe_kernel_parallel_build.h"
30 #include "backend/session/kernel_graph.h"
31 
32 namespace mindspore {
33 namespace kernel {
34 namespace ascend {
35 using KernelModMap = std::map<int64_t, KernelModPtr>;
36 struct TargetJobStatus {
37   int target_job_id;
38   std::string job_status;
39   std::string except_msg;
40   std::string json_name;
41 };
42 
43 class AscendKernelCompileManager {
44  public:
GetInstance()45   static AscendKernelCompileManager &GetInstance() {
46     static AscendKernelCompileManager instance;
47     if (!instance.tbe_init_flag_) {
48       instance.TbeInitialize();
49     }
50     return instance;
51   }
52   void TbeInitialize();
53   void TbeFinalize();
54   // kernel select
55   std::string AscendOpSelectFormat(const AnfNodePtr &node);
56   bool AscendOpCheckSupported(const AnfNodePtr &node);
57   // pre build
58   void AscendPreBuild(const std::shared_ptr<session::KernelGraph> &kernel_graph);
59   // single op compile
60   bool AscendSingleOpCompile(const std::vector<AnfNodePtr> &anf_nodes);
61   // fusion op compile
62   KernelModMap AscendFusionOpCompile(const std::vector<FusionScopeInfo> &fusion_scopes);
63   // clear prev job's cache
64   void ResetOldTask();
65 
66  private:
67   AscendKernelCompileManager() = default;
68   ~AscendKernelCompileManager();
69   void GetAllAscendNodes(const std::shared_ptr<session::KernelGraph> &kernel_graph, std::vector<AnfNodePtr> *tbe_nodes);
70   void QueryFinishJob(const std::string &type);
71   void ParseTargetJobStatus(const std::string &type, const std::string &job_result, std::vector<int> *success_job);
72   void QueryPreBuildFinishJob();
73   void QueryFusionFinishJob(KernelModMap *kernel_mode_ret);
74   void PrintProcessLog(const nlohmann::json &json, int adjust_log_level);
75   void JsonAssemble(const std::string &job_type, const nlohmann::json &src_json, nlohmann::json *dst_json);
76   void PrintInitResult(const nlohmann::json &json);
77   void PrintCompileResult(const nlohmann::json &json);
78   std::string OpSelectAndCheckResultProcess(const nlohmann::json &json, const AnfNodePtr &node);
79   void QueryResultProcess(const nlohmann::json &json, TargetJobStatus *task_info);
80   nlohmann::json TurnStrToJson(const std::string &str) const;
81 
82   static bool tbe_init_flag_;
83   static bool is_tune_flag_;
84   std::set<std::string> single_processed_kernels_;
85   std::set<std::string> fusion_processed_kernels_;
86   std::string op_debug_level_;  // if op_debug_level is "1", skip tbe compile cache and rebuild again.
87   std::shared_ptr<ParallelBuildManager> build_manager_ = nullptr;
88   std::map<int, nlohmann::json> job_list_;
89   std::map<int, AnfNodePtr> job_id_to_node_;
90   std::map<int, std::string> fusion_op_names_;
91 };
92 }  // namespace ascend
93 }  // namespace kernel
94 }  // namespace mindspore
95 
96 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_TBE_ASCEND_KERNEL_COMPILE_H_
97