• 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 
17 #ifndef MINDSPORE_PI_JIT_GRAD_EXECUTOR_H_
18 #define MINDSPORE_PI_JIT_GRAD_EXECUTOR_H_
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include "pipeline/jit/pi/auto_grad/async_task_manager.h"
25 #include "pipeline/jit/pi/auto_grad/bprop_func_graph_manager.h"
26 #include "pybind11/stl.h"
27 
28 namespace mindspore {
29 namespace pijit {
30 namespace grad {
31 namespace py = pybind11;
32 
33 class FunctionNode;
34 using FunctionNodePtr = std::shared_ptr<FunctionNode>;
35 
36 class GradExecutor;
37 using GradExecutorPtr = std::shared_ptr<GradExecutor>;
38 
39 class GradExecutor {
40  public:
GradExecutor()41   GradExecutor()
42       : func_graph_manager_(std::make_shared<BpropFuncGraphManager>()),
43         async_task_manager_(std::make_shared<AsyncTaskManager>()) {}
44   virtual ~GradExecutor() = default;
45 
GetInstance()46   static GradExecutorPtr GetInstance() { return grad_executor_; }
47 
48   FuncGraphPtr PrimBpropGraphPass(const FuncGraphPtr &prim_grad_graph);
49   FuncGraphPtr GetAccumulateGraph(const ValuePtr &tensor);
50   FuncGraphPtr GetBpropGraph(const AnfNodePtr &func, const ValuePtrList &inputs, const ValuePtr &out,
51                              const ValuePtr &dout);
52   FuncGraphPtr GetFuncGraphBpropGraph(const std::string &phase, const py::tuple &args);
53   ValuePtr RunGraph(const FuncGraphPtr &func_graph, const ValuePtrList &inputs, const ValuePtr &out,
54                     const ValuePtr &dout);
55   ValuePtr RunGraph(const FuncGraphPtr &func_graph, const ValuePtrList &inputs);
56   ValuePtr RunGraph(const FuncGraphPtr &func_graph, const VectorRef &inputs);
Clear()57   void Clear() const { func_graph_manager_->Clear(); }
DispatchRecordTask(const AsyncTaskPtr & task)58   void DispatchRecordTask(const AsyncTaskPtr &task) { async_task_manager_->DispatchRecordTask(task); }
DispatchGenerateTask(const AsyncTaskPtr & task)59   void DispatchGenerateTask(const AsyncTaskPtr &task) { async_task_manager_->DispatchGenerateTask(task); }
DispatchRunTask(const AsyncTaskPtr & task)60   void DispatchRunTask(const AsyncTaskPtr &task) { async_task_manager_->DispatchRunTask(task); }
GetAsyncTaskManager()61   const AsyncTaskManagerPtr &GetAsyncTaskManager() const { return async_task_manager_; }
62 
63  private:
64   static GradExecutorPtr grad_executor_;
65   BpropFuncGraphManagerPtr func_graph_manager_;
66   AsyncTaskManagerPtr async_task_manager_;
67 };
68 }  // namespace grad
69 }  // namespace pijit
70 }  // namespace mindspore
71 
72 #endif  // MINDSPORE_PI_JIT_GRAD_EXECUTOR_H_
73