1 /**
2 * Copyright 2021-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 #include "runtime/graph_scheduler/actor/profiler_actor.h"
18 #include <vector>
19 #include <memory>
20 #include <string>
21 #include "mindrt/include/async/async.h"
22 #include "utils/log_adapter.h"
23 #include "utils/file_utils.h"
24 #include "include/backend/debug/profiler/profiling.h"
25
26 namespace mindspore {
27 namespace runtime {
28 /*
29 * Feature group: ascend step start timestamp
30 * Target device group: Ascend.
31 * Description: Add step start timestamp when profiler is started.
32 */
AscendStepStart(const std::vector<KernelGraphPtr> & graphs,std::vector<DeviceContext * > device_contexts)33 void ProfilerActor::AscendStepStart(const std::vector<KernelGraphPtr> &graphs,
34 std::vector<DeviceContext *> device_contexts) {
35 auto profiler = profiler::Profiler::GetInstance(kAscendDevice);
36 if (profiler == nullptr || !profiler->IsInitialized() || graphs.empty()) {
37 return;
38 }
39 if (profiler->GetEnableFlag() && !graphs[0]->IsDatasetGraph()) {
40 profile_started_ = false;
41 for (size_t i = 0; i < graphs.size(); ++i) {
42 MS_EXCEPTION_IF_NULL(graphs[i]);
43 MS_EXCEPTION_IF_NULL(device_contexts[i]);
44 if (device_contexts[i]->GetDeviceType() == device::DeviceType::kAscend && !profile_started_) {
45 device_ctx_ = device_contexts[i];
46 device_ctx_->device_res_manager_->BindDeviceToCurrentThread(false);
47 MS_LOG(INFO) << "Dot step start timestamp.";
48 profiler->StepStart(current_step++, device_contexts[i]->device_res_manager_->GetStream());
49 profile_started_ = true;
50 }
51 }
52 }
53 }
54
55 /*
56 * Feature group: ascend step end timestamp
57 * Target device group: Ascend.
58 * Description: Add step end timestamp when profiler is end.
59 */
AscendStepEnd()60 void ProfilerActor::AscendStepEnd() {
61 auto profiler = profiler::Profiler::GetInstance(kAscendDevice);
62 if (profile_started_ && profiler != nullptr && profiler->GetEnableFlag()) {
63 MS_EXCEPTION_IF_NULL(device_ctx_);
64 device_ctx_->device_res_manager_->BindDeviceToCurrentThread(false);
65 device_ctx_->device_res_manager_->SyncAllStreams();
66 MS_LOG(INFO) << "Dot step end timestamp.";
67 profiler->StepStop();
68 profile_started_ = false;
69 }
70 }
71
72 /*
73 * Feature group: Dump, Online Profilerger.
74 * Target device group: Ascend, GPU.
75 * Runtime category: MindRT.
76 */
ProfilerOnStepBegin(const std::vector<KernelGraphPtr> & graphs,const std::vector<AnfNodePtr> & origin_parameters_order,std::vector<DeviceContext * > device_contexts,OpContext<DeviceTensor> * const op_context,const AID *)77 void ProfilerActor::ProfilerOnStepBegin(const std::vector<KernelGraphPtr> &graphs,
78 const std::vector<AnfNodePtr> &origin_parameters_order,
79 std::vector<DeviceContext *> device_contexts,
80 OpContext<DeviceTensor> *const op_context, const AID *) {
81 MS_LOG(INFO) << "Profiler on step begin.";
82 auto context = MsContext::GetInstance();
83 MS_EXCEPTION_IF_NULL(context);
84 std::string backend = context->backend_policy();
85 device_ctx_ = device_contexts[0];
86 if (backend == "ge") {
87 AscendStepStart(graphs, device_contexts);
88 MS_LOG(INFO) << "Profiler_actor ProfilerOnStepBegin.";
89 return;
90 }
91 }
92
93 /*
94 * Feature group: Dump, Online Profilerger.
95 * Target device group: Ascend, GPU and CPU.
96 * Runtime category: MindRT.
97 */
ProfilerOnStepEnd(OpContext<DeviceTensor> * const op_context,const AID *,int total_running_count_)98 void ProfilerActor::ProfilerOnStepEnd(OpContext<DeviceTensor> *const op_context, const AID *,
99 int total_running_count_) {
100 MS_LOG(INFO) << "Profiler on step begin.";
101 auto context = MsContext::GetInstance();
102 MS_EXCEPTION_IF_NULL(context);
103 std::string backend = context->backend_policy();
104 step_count = total_running_count_;
105 if (backend == "ge") {
106 AscendStepEnd();
107 device_ctx_->device_res_manager_->SyncAllStreams();
108 MS_LOG(INFO) << "Profiler_actor ProfilerOnStepEnd.";
109 return;
110 }
111 }
112 } // namespace runtime
113 } // namespace mindspore
114