1 /******************************************************************************
2 * Copyright (c) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 #include <assert.h>
30 #include "executor.h"
31 #include "executor_impl.h"
32 #include "parameters.h"
33 #include "util.h"
34 #include "trace.h"
37 using namespace tidl;
39 using std::unique_ptr;
41 Executor::Executor(DeviceType core_type, const DeviceIds& ids,
42 const Configuration& configuration, int layers_group_id)
43 {
44 TRACE::enabled = configuration.enableApiTrace;
46 TRACE::print("-> Executor::Executor()\n");
48 pimpl_m = unique_ptr<ExecutorImpl>
49 { new ExecutorImpl(core_type, ids, layers_group_id) };
50 pimpl_m->Initialize(configuration);
52 TRACE::print("<- Executor::Executor()\n");
53 }
57 // Pointer to implementation idiom: https://herbsutter.com/gotw/_100/:
58 // Both unique_ptr and shared_ptr can be instantiated with an incomplete type
59 // unique_ptr's destructor requires a complete type in order to invoke delete
60 // By writing it yourself in the implementation file, you force it to be
61 // defined in a place where impl is already defined, and this successfully
62 // prevents the compiler from trying to automatically generate the destructor
63 // on demand in the caller’s code where impl is not defined.
64 Executor::~Executor() = default;
66 uint32_t Executor::GetNumDevices(DeviceType device_type)
67 {
68 return Device::GetNumDevices(device_type);
69 }
71 #define STRING(S) XSTRING(S)
72 #define XSTRING(S) #S
73 std::string Executor::GetAPIVersion()
74 {
75 static std::string version = STRING(_BUILD_VER);
76 version += ".";
77 version += STRING(_BUILD_SHA);
78 return version;
79 }
82 ExecutorImpl::ExecutorImpl(DeviceType core_type, const DeviceIds& ids,
83 int layers_group_id):
84 configuration_m(),
85 shared_networkparam_heap_m(nullptr, &__free_ddr),
86 device_ids_m(ids),
87 core_type_m(core_type),
88 layers_group_id_m(layers_group_id)
89 {
90 std::string name = STRING(SETUP_KERNEL) ";" STRING(INIT_KERNEL) ";"
91 STRING(PROCESS_KERNEL) ";" STRING(CLEANUP_KERNEL);
92 device_m = Device::Create(core_type_m, ids, name);
93 }
95 ExecutionObject* Executor::operator[](uint32_t index) const
96 {
97 assert(index < pimpl_m->execution_objects_m.size());
98 return pimpl_m->execution_objects_m[index].get();
99 }
101 uint32_t Executor::GetNumExecutionObjects() const
102 {
103 return pimpl_m->execution_objects_m.size();
104 }
106 bool ExecutorImpl::Initialize(const Configuration& configuration)
107 {
108 configuration_m = configuration;
110 // Allocate, initialize TIDL_CreateParams object
111 up_malloc_ddr<TIDL_CreateParams> shared_createparam(
112 malloc_ddr<TIDL_CreateParams>(),
113 &__free_ddr);
114 InitializeNetworkCreateParam(shared_createparam.get(), configuration);
116 // Read network from file into network struct in TIDL_CreateParams
117 sTIDL_Network_t *net = &(shared_createparam.get())->net;
119 bool status = ReadNetworkBinary(configuration_m.netBinFile,
120 reinterpret_cast<char *>(net));
121 assert(status != false);
123 // Force to run full network if runFullNet is set
124 if (configuration.runFullNet)
125 {
126 for (int i = 0; i < net->numLayers; i++)
127 if (net->TIDLLayers[i].layerType != TIDL_DataLayer)
128 net->TIDLLayers[i].layersGroupId = layers_group_id_m;
129 }
131 // If the user has specified an override mapping, apply it
132 else if (!configuration.layerIndex2LayerGroupId.empty())
133 {
134 for (const auto &item : configuration.layerIndex2LayerGroupId)
135 if (item.first < net->numLayers)
136 net->TIDLLayers[item.first].layersGroupId = item.second;
137 }
139 // Call a setup kernel to allocate and fill network parameters
140 InitializeNetworkParams(shared_createparam.get());
142 const ArgInfo create_arg(shared_createparam.get(),
143 sizeof(TIDL_CreateParams));
144 const ArgInfo param_heap_arg(shared_networkparam_heap_m.get(),
145 configuration_m.PARAM_HEAP_SIZE);
146 for (auto ids : device_ids_m)
147 {
148 uint8_t index = static_cast<uint8_t>(ids);
149 execution_objects_m.push_back(
150 unique_ptr<ExecutionObject>
151 {new ExecutionObject(device_m.get(), core_type_m, index,
152 create_arg, param_heap_arg,
153 configuration_m,
154 layers_group_id_m)} );
155 }
157 for (auto &eo : execution_objects_m)
158 eo->RunAsync(ExecutionObject::CallType::INIT);
160 for (auto &eo : execution_objects_m)
161 eo->Wait(ExecutionObject::CallType::INIT);
163 return true;
164 }
167 bool ExecutorImpl::InitializeNetworkParams(TIDL_CreateParams *cp)
168 {
169 // Determine size of network parameters buffer, allocate it
170 size_t networkparam_size =
171 GetBinaryFileSize(configuration_m.paramsBinFile);
173 up_malloc_ddr<char> networkparam(malloc_ddr<char>(networkparam_size),
174 &__free_ddr);
176 // Read network parameters from bin file into buffer
177 bool status = ReadBinary(configuration_m.paramsBinFile,
178 networkparam.get(),
179 networkparam_size);
180 assert(status != false);
182 // Allocate a buffer for passing parameters to the kernel
183 up_malloc_ddr<OCL_TIDL_SetupParams> setupParams(
184 malloc_ddr<OCL_TIDL_SetupParams>(),
185 &__free_ddr);
187 // Set up execution trace specified in the configuration
188 EnableExecutionTrace(configuration_m, &setupParams->enableTrace);
190 setupParams->networkParamHeapSize = configuration_m.PARAM_HEAP_SIZE;
191 setupParams->noZeroCoeffsPercentage = configuration_m.noZeroCoeffsPercentage;
192 setupParams->sizeofTIDL_CreateParams = sizeof(TIDL_CreateParams);
193 setupParams->offsetofNet = offsetof(TIDL_CreateParams, net);
195 // Allocate buffer for a network parameter heap. Used by the setup
196 // kernel to allocate and initialize network parameters for the layers
197 shared_networkparam_heap_m.reset(malloc_ddr<char>(setupParams->networkParamHeapSize));
199 KernelArgs args = { DeviceArgInfo(cp, sizeof(TIDL_CreateParams),
200 DeviceArgInfo::Kind::BUFFER),
201 DeviceArgInfo(networkparam.get(), networkparam_size,
202 DeviceArgInfo::Kind::BUFFER),
203 DeviceArgInfo(shared_networkparam_heap_m.get(),
204 setupParams->networkParamHeapSize,
205 DeviceArgInfo::Kind::BUFFER),
206 DeviceArgInfo(setupParams.get(),
207 sizeof(OCL_TIDL_SetupParams),
208 DeviceArgInfo::Kind::BUFFER) };
210 // Execute kernel on first available device in the Executor
211 uint8_t id = static_cast<uint8_t>(*(device_ids_m.cbegin()));
212 unique_ptr<Kernel> K {new Kernel(device_m.get(), STRING(SETUP_KERNEL),
213 args, id)};
214 K->RunAsync();
215 K->Wait();
217 if (setupParams->errorCode != OCL_TIDL_SUCCESS)
218 throw Exception(setupParams->errorCode,
219 __FILE__, __FUNCTION__, __LINE__);
221 return status;
222 }
225 void ExecutorImpl::Cleanup()
226 {
227 for (auto &eo : execution_objects_m)
228 eo->RunAsync(ExecutionObject::CallType::CLEANUP);
230 for (auto &eo : execution_objects_m)
231 eo->Wait(ExecutionObject::CallType::CLEANUP);
232 }
235 void ExecutorImpl::InitializeNetworkCreateParam(TIDL_CreateParams *CP,
236 const Configuration& c)
237 {
238 CP->currCoreId = layers_group_id_m;
239 CP->currLayersGroupId = layers_group_id_m;
240 CP->l1MemSize = tidl::internal::DMEM0_SIZE;
241 CP->l2MemSize = tidl::internal::DMEM1_SIZE;
242 CP->l3MemSize = tidl::internal::OCMC_SIZE;
244 CP->quantHistoryParam1 = c.quantHistoryParam1;
245 CP->quantHistoryParam2 = c.quantHistoryParam2;
246 CP->quantMargin = c.quantMargin;
248 // If trace is enabled, setup the device TIDL library to allocate separate
249 // output buffers for each layer. This makes it possible for the host
250 // to access the output of each layer after a frame is processed.
251 if (configuration_m.enableOutputTrace)
252 CP->optimiseExtMem = TIDL_optimiseExtMemL0;
253 else
254 CP->optimiseExtMem = TIDL_optimiseExtMemL1;
255 }
257 Exception::Exception(const std::string& error, const std::string& file,
258 const std::string& func, uint32_t line_no)
259 {
261 message_m = "TIDL Error: [";
262 message_m += file;
263 message_m += ", ";
264 message_m += func;
265 message_m += ", ";
266 message_m += std::to_string(line_no);
267 message_m += "]: ";
268 message_m += error;
269 }
271 // Refer ti-opencl/builtins/include/custom.h for error codes
272 Exception::Exception(int32_t errorCode, const std::string& file,
273 const std::string& func, uint32_t line_no)
274 {
275 message_m = "TIDL Error: [";
276 message_m += file;
277 message_m += ", ";
278 message_m += func;
279 message_m += ", ";
280 message_m += std::to_string(line_no);
281 message_m += "]: ";
283 switch (errorCode)
284 {
285 case OCL_TIDL_ERROR:
286 message_m += "";
287 break;
288 case OCL_TIDL_ALLOC_FAIL:
289 case OCL_TIDL_MEMREC_ALLOC_FAIL:
290 message_m += "Memory allocation failed on device";
291 break;
292 case OCL_TIDL_PROCESS_FAIL:
293 message_m += "Process call failed on device";
294 break;
295 case OCL_TIDL_CREATE_PARAMS_MISMATCH:
296 message_m += "TIDL API headers inconsistent with OpenCL";
297 break;
298 case OCL_TIDL_INIT_FAIL:
299 message_m += "Initialization failed on device";
300 break;
301 default:
302 message_m += std::to_string(errorCode);
303 break;
304 }
305 }
307 const char* Exception::what() const noexcept
308 {
309 return message_m.c_str();
310 }