1 /**
2 * Copyright 2023 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 "extendrt/delegate/ascend_ge/ge_context_manager.h"
18 #include "src/common/log_adapter.h"
19 #include "transform/symbol/acl_rt_symbol.h"
20 #include "transform/symbol/symbol_utils.h"
21
22 namespace mindspore {
GeContextManager()23 GeContextManager::GeContextManager() {}
24
~GeContextManager()25 GeContextManager::~GeContextManager() { DestroyContext(); }
26
InitContext(uint32_t device_id)27 bool GeContextManager::InitContext(uint32_t device_id) {
28 device_id_ = device_id;
29 auto ret = CALL_ASCEND_API(aclrtSetDevice, device_id_);
30 if (ret != ACL_RT_SUCCESS) {
31 MS_LOG(ERROR) << "Failed to call aclrtSetDevice , device id " << device_id_ << ", ret: " << static_cast<int>(ret);
32 return false;
33 }
34 // Context will be created by aclrtSetDevice
35 ret = CALL_ASCEND_API(aclrtGetCurrentContext, &context_);
36 if (ret != ACL_RT_SUCCESS || context_ == nullptr) {
37 MS_LOG(ERROR) << "Call aclrtGetCurrentContext failed, ret[" << ret << "]";
38 return false;
39 }
40 MS_LOG(INFO) << "Open device " << device_id_ << " success";
41 MS_LOG(INFO) << "Create context success";
42 if (!CreateDefaultStream()) {
43 MS_LOG(ERROR) << "Failed to create default stream";
44 return false;
45 }
46 return true;
47 }
48
SetContext()49 bool GeContextManager::SetContext() {
50 auto rt_ret = CALL_ASCEND_API(aclrtSetCurrentContext, context_);
51 if (rt_ret != ACL_RT_SUCCESS) {
52 MS_LOG(ERROR) << "Failed to call aclrtSetCurrentContext";
53 return false;
54 }
55 return true;
56 }
57
DestroyContext()58 void GeContextManager::DestroyContext() {
59 if (context_) {
60 (void)SetContext();
61 DestroyDefaultStream();
62 context_ = nullptr;
63 }
64 MS_LOG(INFO) << "End to destroy context";
65 }
66
GetDefaultStream()67 aclrtStream GeContextManager::GetDefaultStream() {
68 if (default_stream_ != nullptr) {
69 return default_stream_;
70 }
71 if (!CreateDefaultStream()) {
72 return nullptr;
73 }
74 return default_stream_;
75 }
76
CreateDefaultStream()77 bool GeContextManager::CreateDefaultStream() {
78 if (default_stream_ != nullptr) {
79 return true;
80 }
81
82 auto priority = 0;
83 auto ret = CALL_ASCEND_API(aclrtCreateStreamWithConfig, &default_stream_, priority,
84 (ACL_STREAM_FAST_LAUNCH | ACL_STREAM_FAST_SYNC));
85 if (ret != ACL_ERROR_NONE) {
86 MS_LOG(ERROR) << "Create stream failed, ret:" << ret;
87 return false;
88 }
89 ret = CALL_ASCEND_API(aclrtSetStreamFailureMode, default_stream_, ACL_STOP_ON_FAILURE);
90 if (ret != ACL_ERROR_NONE) {
91 MS_LOG(ERROR) << "aclrtSetStreamFailureMode failed, ret:" << ret;
92 return false;
93 }
94 return true;
95 }
96
SyncStream(aclrtStream stream) const97 bool GeContextManager::SyncStream(aclrtStream stream) const {
98 MS_EXCEPTION_IF_NULL(stream);
99 auto RET = CALL_ASCEND_API(aclrtSynchronizeStream, stream);
100 if (RET != ACL_ERROR_NONE && RET != ACL_ERROR_RT_AICORE_OVER_FLOW) { // o for switch stream
101 MS_LOG(ERROR) << "Call runtime aclrtSynchronizeStream error.";
102 return false;
103 }
104 if (RET == ACL_ERROR_RT_AICORE_OVER_FLOW) {
105 MS_LOG(WARNING) << "Call runtime aclrtSynchronizeStream, the stream get overflow.";
106 }
107 return true;
108 }
109
DestroyDefaultStream()110 void GeContextManager::DestroyDefaultStream() {
111 if (default_stream_ == nullptr) {
112 return;
113 }
114 const auto ret = CALL_ASCEND_API(aclrtDestroyStream, default_stream_);
115 if (ret != ACL_ERROR_NONE) {
116 MS_LOG(ERROR) << "Call aclrtDestroyStream, ret[" << ret << "]";
117 return;
118 }
119 default_stream_ = nullptr;
120 }
121 } // namespace mindspore
122