• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_PROFILING_UTILS_H_
17 #define MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_PROFILING_UTILS_H_
18 
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 #include <set>
24 #include <unordered_map>
25 
26 #include "backend/session/kernel_graph.h"
27 
28 namespace mindspore {
29 namespace profiler {
30 namespace gpu {
31 struct ProfilingTraceInfo {
32   // support get all the op name from environment variable
33   // fp start op is the first op in all subgraph except data related op
34   std::string trace_fp_start;
35   // bp end op is the input node op of the last communication op (if exist)
36   std::string trace_bp_end;
37   // iteration end op is the last executed op
38   std::string trace_iter_end;
39 
40   // profiling specific op, such as AllReduce;
41   std::vector<std::string> trace_custom_node;
42 
IsValidProfilingTraceInfo43   bool IsValid() const { return !(trace_fp_start.empty() || trace_bp_end.empty() || trace_iter_end.empty()); }
44 };
45 
46 class ProfilingUtils {
47  public:
48   ProfilingUtils() = default;
49   ~ProfilingUtils() = default;
50 
51   // Get profiling trace point from envs.
52   // export PROFILING_FP_START='full name of the first cnode to execute'
53   // export PROFILING_BP_END='full name of the last backpropagation cnode to execute'
54   // export PROFILING_ITER_END='full name of last cnode in graph to execute'
55   static ProfilingTraceInfo GetProfilingTraceFromEnv(NotNull<const session::KernelGraph *> graph_ptr);
56   static void OutputStepTraceOpNameStatus();
57   static bool IsFirstStep(const uint32_t graph_id);
58 
59   static bool have_communication_op;
60   static ProfilingTraceInfo profiling_trace;
61 
62  private:
63   static void SetTraceFpStart(const std::vector<CNodePtr> &cnode_exec_order);
64   static void SetTraceBpEnd(const std::vector<CNodePtr> &cnode_exec_order);
65   static void SetTraceIterEnd(const std::vector<CNodePtr> &cnode_exec_order);
66   static std::string GetGraphSecondLastKernelName(const std::vector<CNodePtr> &cnode_exec_order);
67   static void GetTraceHccl(const std::vector<CNodePtr> &cnode_exec_order);
68   static std::unordered_map<uint32_t, bool> is_first_step_map_;
69 };
70 }  // namespace gpu
71 }  // namespace profiler
72 }  // namespace mindspore
73 #endif  // MINDSPORE_CCSRC_PROFILER_DEVICE_GPU_GPU_PROFILING_UTILS_H_
74