1 /**
2 * Copyright 2019-2020 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/device/ascend/ge_runtime/task/label_goto_task.h"
18 #include "runtime/mem.h"
19 #include "runtime/device/ascend/ge_runtime/task/task_factory.h"
20
21 namespace mindspore::ge::model_runner {
LabelGotoTask(const ModelContext & model_context,const std::shared_ptr<LabelGotoTaskInfo> & task_info)22 LabelGotoTask::LabelGotoTask(const ModelContext &model_context, const std::shared_ptr<LabelGotoTaskInfo> &task_info)
23 : TaskRepeater<LabelGotoTaskInfo>(model_context, task_info),
24 task_info_(task_info),
25 stream_(nullptr),
26 index_value_(nullptr) {
27 MS_EXCEPTION_IF_NULL(task_info_);
28 auto stream_list = model_context.stream_list();
29 auto label_list = model_context.label_list();
30 rt_model_handle_ = model_context.rt_model_handle();
31 uint32_t stream_id = task_info_->stream_id();
32 label_id_ = task_info_->label_id();
33 MS_LOG(INFO) << "Stream list size: " << stream_list.size() << ", stream id: " << stream_id;
34 MS_LOG(INFO) << "Label list size: " << label_list.size() << ", label id: " << label_id_;
35 if (stream_id >= stream_list.size() || label_id_ >= label_list.size()) {
36 MS_LOG(EXCEPTION) << "Stream/Label id invalid.";
37 }
38 stream_ = stream_list[stream_id];
39 label_manager_ = LabelManager::GetInstance();
40 MS_EXCEPTION_IF_NULL(label_manager_);
41 label_info_ = label_manager_->GetLabelInfo(rt_model_handle_, {label_id_}, label_list);
42 MS_EXCEPTION_IF_NULL(label_info_);
43 }
44
~LabelGotoTask()45 LabelGotoTask::~LabelGotoTask() {
46 if (index_value_ != nullptr) {
47 rtError_t rt_ret = rtFree(index_value_);
48 if (rt_ret != RT_ERROR_NONE) {
49 MS_LOG(ERROR) << "Call rtFree index_value_ failed, ret: " << rt_ret;
50 }
51 index_value_ = nullptr;
52 }
53 }
54
Distribute()55 void LabelGotoTask::Distribute() {
56 MS_LOG(INFO) << "LabelGotoTask Distribute start.";
57 MS_EXCEPTION_IF_NULL(stream_);
58 MS_EXCEPTION_IF_NULL(label_info_);
59
60 if (index_value_ == nullptr) {
61 rtError_t rt_ret = rtMalloc(&index_value_, sizeof(uint64_t), RT_MEMORY_HBM);
62 if (rt_ret != RT_ERROR_NONE) {
63 MS_LOG(EXCEPTION) << "Call rt api rtMalloc failed, ret: " << rt_ret;
64 }
65
66 uint64_t index = 0;
67 rt_ret = rtMemcpy(index_value_, sizeof(uint64_t), &index, sizeof(index), RT_MEMCPY_HOST_TO_DEVICE);
68 if (rt_ret != RT_ERROR_NONE) {
69 MS_LOG(EXCEPTION) << "Call rt api rtMemcpy failed, ret: " << rt_ret;
70 }
71 }
72
73 void *label_info = label_info_->GetLabelInfo();
74 rtError_t rt_ret = rtLabelSwitchByIndex(index_value_, 1, label_info, stream_);
75 if (rt_ret != RT_ERROR_NONE) {
76 MS_LOG(EXCEPTION) << "Call rt api rtLabelSwitchByIndex failed, ret: " << rt_ret;
77 }
78
79 MS_LOG(INFO) << "DistributeTask end.";
80 }
81
82 REGISTER_TASK(TaskInfoType::LABEL_GOTO, LabelGotoTask, LabelGotoTaskInfo);
83 } // namespace mindspore::ge::model_runner
84