Add ssd_multibox example to tinn (Part 1)
[tidl/tidl-api.git] / tinn_api / src / executor.cpp
1 #include <assert.h>
2 #include "executor.h"
3 #include "executor_impl.h"
4 #include "parameters.h"
5 #include "util.h"
6 #include "trace.h"
9 using namespace tinn;
11 using std::unique_ptr;
13 Executor::Executor(DeviceType core_type, const DeviceIds& ids,
14                    const Configuration& configuration)
15 {
16     pimpl_m = unique_ptr<ExecutorImpl>
17               { new ExecutorImpl(core_type, ids) };
18     pimpl_m->Initialize(configuration);
19 }
23 // Pointer to implementation idiom: https://herbsutter.com/gotw/_100/:
24 // Both unique_ptr and shared_ptr can be instantiated with an incomplete type
25 // unique_ptr's destructor requires a complete type in order to invoke delete
26 // By writing it yourself in the implementation file, you force it to be
27 // defined in a place where impl is already defined, and this successfully
28 // prevents the compiler from trying to automatically generate the destructor
29 // on demand in the caller’s code where impl is not defined.
30 Executor::~Executor() = default;
32 uint32_t Executor::GetNumDevices(DeviceType device_type)
33 {
34     return Device::GetNumDevices(device_type);
35 }
37 #define STRING(S)  XSTRING(S)
38 #define XSTRING(S) #S
39 std::string Executor::GetAPIVersion()
40 {
41     static std::string version = STRING(_BUILD_VER);
42     version += ".";
43     version += STRING(_BUILD_SHA);
44     return version;
45 }
48 ExecutorImpl::ExecutorImpl(DeviceType core_type, const DeviceIds& ids):
49     configuration_m(),
50     shared_networkparam_heap_m(nullptr, &__free_ddr),
51     device_ids_m(ids),
52     core_type_m(core_type)
53 {
54     std::string name;
55     if (core_type_m == DeviceType::DSP)
56         name  = "";
57     else if (core_type_m == DeviceType::DLA)
58         name = STRING(SETUP_KERNEL) ";" STRING(INIT_KERNEL) ";" STRING(PROCESS_KERNEL) ";" STRING(CLEANUP_KERNEL);
60     device_m = Device::Create(core_type_m, ids, name);
61 }
64 const ExecutionObjects& Executor::GetExecutionObjects() const
65 {
66     return pimpl_m->execution_objects_m;
67 }
69 bool ExecutorImpl::Initialize(const Configuration& configuration)
70 {
71     configuration_m = configuration;
73     // Allocate, initialize TIDL_CreateParams object
74     up_malloc_ddr<TIDL_CreateParams> shared_createparam(
75                                             malloc_ddr<TIDL_CreateParams>(),
76                                             &__free_ddr);
77     InitializeNetworkCreateParam(shared_createparam.get(), configuration);
79     // Read network from file into network struct in TIDL_CreateParams
80     sTIDL_Network_t *net = &(shared_createparam.get())->net;
82     bool status = ReadBinary(configuration_m.netBinFile,
83                              reinterpret_cast<char *>(net),
84                              sizeof(sTIDL_Network_t));
85     assert(status != false);
87     //TODO: Why is this set here?
88     net->interElementSize = 4;
90     // Force to run full network if runFullNet is set
91     if (configuration.runFullNet)
92     {
93         for (int i = 0; i < net->numLayers; i++)
94             if (net->TIDLLayers[i].layerType != TIDL_DataLayer)
95                 net->TIDLLayers[i].layersGroupId = configuration.layersGroupId;
96     }
98     // Call a setup kernel to allocate and fill network parameters
99     InitializeNetworkParams(shared_createparam.get());
101     const ArgInfo create_arg(shared_createparam.get(),
102                              sizeof(TIDL_CreateParams));
103     const ArgInfo param_heap_arg(shared_networkparam_heap_m.get(),
104                                  configuration_m.PARAM_HEAP_SIZE);
105     for (auto ids : device_ids_m)
106     {
107         uint8_t index = static_cast<uint8_t>(ids);
108         execution_objects_m.push_back(
109              unique_ptr<ExecutionObject>
110              {new ExecutionObject(device_m.get(), index,
111                                   create_arg, param_heap_arg,
112                                   configuration_m.EXTMEM_HEAP_SIZE)} );
113     }
115     for (auto &eo : execution_objects_m)
116         eo->RunAsync(ExecutionObject::CallType::INIT);
118     for (auto &eo : execution_objects_m)
119         eo->Wait(ExecutionObject::CallType::INIT);
121     return true;
125 bool ExecutorImpl::InitializeNetworkParams(TIDL_CreateParams *cp)
127     // Determine size of network parameters buffer, allocate it
128     size_t networkparam_size =
129                         GetBinaryFileSize(configuration_m.paramsBinFile);
131     up_malloc_ddr<char> networkparam(malloc_ddr<char>(networkparam_size),
132                                 &__free_ddr);
134     // Read network parameters from bin file into buffer
135     bool status = ReadBinary(configuration_m.paramsBinFile,
136                              networkparam.get(),
137                              networkparam_size);
138     assert(status != false);
140     // Allocate a buffer for passing parameters to the kernel
141     up_malloc_ddr<OCL_TIDL_SetupParams> setupParams(
142                                             malloc_ddr<OCL_TIDL_SetupParams>(),
143                                             &__free_ddr);
145     setupParams->enableTrace = OCL_TIDL_TRACE_OFF;
146     setupParams->networkParamHeapSize = configuration_m.PARAM_HEAP_SIZE;
147     setupParams->noZeroCoeffsPercentage = configuration_m.noZeroCoeffsPercentage;
148     setupParams->sizeofTIDL_CreateParams = sizeof(TIDL_CreateParams);
149     setupParams->offsetofNet = offsetof(TIDL_CreateParams, net);
151     // Allocate buffer for a network parameter heap. Used by the setup
152     // kernel to allocate and initialize network parameters for the layers
153     shared_networkparam_heap_m.reset(malloc_ddr<char>(setupParams->networkParamHeapSize));
155     KernelArgs args = { ArgInfo(cp, sizeof(TIDL_CreateParams)),
156                         ArgInfo(networkparam.get(), networkparam_size),
157                         ArgInfo(shared_networkparam_heap_m.get(),
158                                 setupParams->networkParamHeapSize),
159                         ArgInfo(setupParams.get(),
160                                 sizeof(OCL_TIDL_SetupParams)) };
162     // Execute kernel on first available device in the Executor
163     uint8_t id = static_cast<uint8_t>(*(device_ids_m.cbegin()));
164     unique_ptr<Kernel> K {new Kernel(device_m.get(), STRING(SETUP_KERNEL),
165                                      args, id)};
166     K->RunAsync();
167     K->Wait();
169     if (setupParams->errorCode != OCL_TIDL_SUCCESS)
170         throw Exception(setupParams->errorCode,
171                         __FILE__, __FUNCTION__, __LINE__);
173     return status;
177 void ExecutorImpl::Cleanup()
179     for (auto &eo : execution_objects_m)
180         eo->RunAsync(ExecutionObject::CallType::CLEANUP);
182     for (auto &eo : execution_objects_m)
183         eo->Wait(ExecutionObject::CallType::CLEANUP);
187 void ExecutorImpl::InitializeNetworkCreateParam(TIDL_CreateParams *CP,
188                                           const Configuration& configuration)
190     CP->currCoreId           = configuration.layersGroupId;
191     CP->currLayersGroupId    = configuration.layersGroupId;
192     CP->l1MemSize            = tinn::internal::DMEM0_SIZE;
193     CP->l2MemSize            = tinn::internal::DMEM1_SIZE;
194     CP->l3MemSize            = tinn::internal::OCMC_SIZE;
196     CP->quantHistoryParam1   = tinn::internal::QUANT_HISTORY_PARAM1;
197     CP->quantHistoryParam2   = tinn::internal::QUANT_HISTORY_PARAM2;
198     CP->quantMargin          = tinn::internal::QUANT_MARGIN;
199     CP->optimiseExtMem       = TIDL_optimiseExtMemL1;
202 Exception::Exception(const std::string& error, const std::string& file,
203                      const std::string& func, uint32_t line_no)
206     message_m = "TIDL Error: [";
207     message_m += file;
208     message_m += ", ";
209     message_m += func;
210     message_m += ", ";
211     message_m += std::to_string(line_no);
212     message_m += "]: ";
213     message_m += error;
216 Exception::Exception(int32_t errorCode, const std::string& file,
217                      const std::string& func, uint32_t line_no)
219     message_m = "TIDL Error: [";
220     message_m += file;
221     message_m += ", ";
222     message_m += func;
223     message_m += ", ";
224     message_m += std::to_string(line_no);
225     message_m += "]: ";
227     if (errorCode == OCL_TIDL_ERROR)
228         message_m += "";
229     else if (errorCode == OCL_TIDL_ALLOC_FAIL)
230         message_m += "Allocation failed on device";
231     else if (errorCode == OCL_TIDL_MEMREC_ALLOC_FAIL)
232         message_m += "Memrec allocation failed on device";
233     else if (errorCode == OCL_TIDL_PROCESS_FAIL)
234         message_m += "Process call failed on device";
235     else if (errorCode == OCL_TIDL_CREATE_PARAMS_MISMATCH)
236         message_m += "TIDL_CreateParams definition inconsistent across host"
237                      "and device.";
238     else
239         message_m += std::to_string(errorCode);
243 const char* Exception::what() const noexcept
245     return message_m.c_str();