1 /**
2 * Copyright 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
17 #include "src/common/context_util.h"
18 #include <map>
19 #include <memory>
20 #include <set>
21 #include <string>
22 #include "src/common/log_adapter.h"
23
24 namespace mindspore {
25 namespace lite {
26 namespace {
27 template <class T>
PassBasicProperties(std::shared_ptr<T> device_info,const lite::DeviceContext & device_context)28 void PassBasicProperties(std::shared_ptr<T> device_info, const lite::DeviceContext &device_context) {
29 MS_ASSERT(device_info != nullptr);
30 device_info->SetProvider(device_context.provider_);
31 device_info->SetProviderDevice(device_context.provider_device_);
32 device_info->SetAllocator(device_context.allocator_);
33 }
34
CPUDeviceInfoFromCPUDeviceContext(const lite::DeviceContext & cpu_context)35 std::shared_ptr<mindspore::CPUDeviceInfo> CPUDeviceInfoFromCPUDeviceContext(const lite::DeviceContext &cpu_context) {
36 if (cpu_context.device_type_ != DT_CPU) {
37 MS_LOG(ERROR) << "function input parameter is not cpu context.";
38 return nullptr;
39 }
40 auto cpu_info = std::make_shared<mindspore::CPUDeviceInfo>();
41 MS_CHECK_TRUE_RET(cpu_info != nullptr, nullptr);
42 cpu_info->SetEnableFP16(cpu_context.device_info_.cpu_device_info_.enable_float16_);
43 PassBasicProperties(cpu_info, cpu_context);
44 return cpu_info;
45 }
46
GPUDeviceInfoFromGPUDeviceContext(const lite::DeviceContext & gpu_context)47 std::shared_ptr<mindspore::GPUDeviceInfo> GPUDeviceInfoFromGPUDeviceContext(const lite::DeviceContext &gpu_context) {
48 if (gpu_context.device_type_ != DT_GPU) {
49 MS_LOG(ERROR) << "function input parameter is not gpu context.";
50 return nullptr;
51 }
52 auto gpu_info = std::make_shared<mindspore::GPUDeviceInfo>();
53 MS_CHECK_TRUE_RET(gpu_info != nullptr, nullptr);
54 gpu_info->SetEnableFP16(gpu_context.device_info_.gpu_device_info_.enable_float16_);
55 gpu_info->SetDeviceID(gpu_context.device_info_.gpu_device_info_.gpu_device_id_);
56 PassBasicProperties(gpu_info, gpu_context);
57 return gpu_info;
58 }
59
NPUDeviceInfoFromNPUDeviceContext(const lite::DeviceContext & npu_context)60 std::shared_ptr<mindspore::KirinNPUDeviceInfo> NPUDeviceInfoFromNPUDeviceContext(
61 const lite::DeviceContext &npu_context) {
62 if (npu_context.device_type_ != DT_NPU) {
63 MS_LOG(ERROR) << "function input parameter is not npu context.";
64 return nullptr;
65 }
66 auto npu_info = std::make_shared<mindspore::KirinNPUDeviceInfo>();
67 MS_CHECK_TRUE_RET(npu_info != nullptr, nullptr);
68 npu_info->SetFrequency(npu_context.device_info_.npu_device_info_.frequency_);
69 PassBasicProperties(npu_info, npu_context);
70 return npu_info;
71 }
72
Ascend310DeviceInfoFromAscend310DeviceContext(const lite::DeviceContext & ascend310_context)73 std::shared_ptr<mindspore::Ascend310DeviceInfo> Ascend310DeviceInfoFromAscend310DeviceContext(
74 const lite::DeviceContext &ascend310_context) {
75 if (ascend310_context.device_type_ != DT_ASCEND310) {
76 MS_LOG(ERROR) << "Function input parameter is not ascend310 context.";
77 return nullptr;
78 }
79 auto ascend310_info = std::make_shared<mindspore::Ascend310DeviceInfo>();
80 MS_CHECK_TRUE_RET(ascend310_info != nullptr, nullptr);
81 ascend310_info->SetDeviceID(ascend310_context.device_info_.ascend310_device_info_.device_id_);
82 return ascend310_info;
83 }
84
NNRtDeviceInfoFromNNRtDeviceContext(const lite::DeviceContext & nnrt_context)85 std::shared_ptr<mindspore::NNRTDeviceInfo> NNRtDeviceInfoFromNNRtDeviceContext(
86 const lite::DeviceContext &nnrt_context) {
87 if (nnrt_context.device_type_ != DT_NNRT) {
88 MS_LOG(ERROR) << "Function input parameter is not NNRt context.";
89 return nullptr;
90 }
91 auto nnrt_info = std::make_shared<mindspore::NNRTDeviceInfo>();
92 MS_CHECK_TRUE_RET(nnrt_info != nullptr, nullptr);
93 return nnrt_info;
94 }
95 } // namespace
96
MSContextFromContext(const lite::Context * context)97 mindspore::Context *MSContextFromContext(const lite::Context *context) {
98 if (context == nullptr) {
99 MS_LOG(ERROR) << "context is nullptr";
100 return nullptr;
101 }
102 auto ms_context = new (std::nothrow) mindspore::Context();
103 if (ms_context == nullptr) {
104 MS_LOG(ERROR) << "New Context failed";
105 return nullptr;
106 }
107 ms_context->SetThreadNum(context->thread_num_);
108 ms_context->SetThreadAffinity(context->affinity_core_list_);
109 ms_context->SetEnableParallel(context->enable_parallel_);
110 ms_context->SetDelegate(context->delegate);
111 auto &device_infos = ms_context->MutableDeviceInfo();
112 std::map<DeviceType, std::function<std::shared_ptr<mindspore::DeviceInfoContext>(const lite::DeviceContext &)>>
113 transfer_funcs = {{DT_CPU, CPUDeviceInfoFromCPUDeviceContext},
114 {DT_GPU, GPUDeviceInfoFromGPUDeviceContext},
115 {DT_NPU, NPUDeviceInfoFromNPUDeviceContext},
116 {DT_ASCEND310, Ascend310DeviceInfoFromAscend310DeviceContext},
117 {DT_NNRT, NNRtDeviceInfoFromNNRtDeviceContext}};
118 for (auto &device_context : context->device_list_) {
119 auto device_type = device_context.device_type_;
120 if (transfer_funcs.find(device_type) == transfer_funcs.end()) {
121 MS_LOG(ERROR) << "device type is invalid.";
122 return nullptr;
123 }
124 auto device_info = transfer_funcs[device_type](device_context);
125 if (device_info == nullptr) {
126 MS_LOG(ERROR) << "transfer device context to device info failed.";
127 return nullptr;
128 }
129 if (device_type == DT_CPU) {
130 ms_context->SetThreadAffinity(device_context.device_info_.cpu_device_info_.cpu_bind_mode_);
131 }
132 device_infos.push_back(device_info);
133 }
134 return ms_context;
135 }
136
DeviceTypePriority(const lite::Context * context,int device_type1,int device_type2)137 bool DeviceTypePriority(const lite::Context *context, int device_type1, int device_type2) {
138 /* dt1 > dt2 true
139 * dt1 < dt2 false */
140
141 if (context == nullptr) {
142 return false;
143 }
144 DeviceContextVector device_infos = context->device_list_;
145 for (DeviceContext device_info : device_infos) {
146 if (device_info.device_type_ == device_type1) {
147 return true;
148 }
149 if (device_info.device_type_ == device_type2) {
150 return false;
151 }
152 }
153 return false;
154 }
155
KernelArchToDeviceType(kernel::KERNEL_ARCH kernel_arch)156 DeviceType KernelArchToDeviceType(kernel::KERNEL_ARCH kernel_arch) {
157 switch (kernel_arch) {
158 case kernel::KERNEL_ARCH::kCPU:
159 return DT_CPU;
160 case kernel::KERNEL_ARCH::kGPU:
161 return DT_GPU;
162 case kernel::KERNEL_ARCH::kNPU:
163 return DT_NPU;
164 default:
165 return DT_CPU;
166 }
167 }
168 } // namespace lite
169 } // namespace mindspore
170