• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_GRAPH_ADAPTER_H_
18 #define MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_GRAPH_ADAPTER_H_
19 
20 #include <vector>
21 #include <set>
22 #include <unordered_map>
23 #include "include/backend/kernel_graph.h"
24 #include "runtime/hardware/device_context.h"
25 #include "runtime/graph_scheduler/graph_compiler.h"
26 
27 namespace mindspore::pynative {
28 using GraphCompilerInfo = runtime::GraphCompilerInfo;
29 class GraphAdapter {
30  public:
31   void UpdateForwardOutputInBpropGraph(const KernelGraphPtr &graph, const device::DeviceContext *device_context,
32                                        bool no_control_flow);
33   void GenerateBackoffValueNodeOwners(const KernelGraphPtr &graph);
34   static void ReplaceGraphParameterProperties(const KernelGraphPtr &graph,
35                                               const std::vector<tensor::TensorPtr> &input_tensors,
36                                               const device::DeviceContext *device_context);
37   static void GenerateRefCountForBpropValueNode(const KernelGraphPtr &graph);
38   static void ClearForwardOutputValueNodeDeviceAddress(const KernelGraphPtr &graph,
39                                                        const device::DeviceContext *device_context);
40   static void RemoveUnusedValueNodes(const KernelGraphPtr &graph);
41   static void HandleHeterogeneousTensors(const std::vector<std::vector<tensor::TensorPtr>> &tensors,
42                                          const std::vector<device::DeviceContext *> &device_contexts);
43   static bool IsPynativeGeGraphSink(const GraphCompilerInfo &graph_compiler_info);
44   static bool IsPynativeGeGraphSink(const FuncGraphPtr &func_graph);
45   static bool PyNativeEnableTaskSink(const FuncGraphPtr &func_graph);
46   static bool IsAutoParallel();
47   static void UpdateDynamicValueNodeAbstract(const KernelGraphPtr &graph);
48   static void SensTensorToDevice(const KernelGraphPtr &graph, const device::DeviceContext *device_context);
49 
50  private:
51   void HandleBackoffValueNode(const ValueNodePtr &value_node, const AnfNodePtr &front_node,
52                               const DeviceContext *device_context) const;
53   // Each backend has an independent map.
54   // The map will be destroyed when the backend object is destroyed.
55   std::unordered_map<AnfNode *, std::set<CNodePtr>> node_to_backoff_kernels_;
56 };
57 }  // namespace mindspore::pynative
58 #endif  // MINDSPORE_MINDSPORE_CCSRC_RUNTIME_PYNATIVE_GRAPH_ADAPTER_H_
59