Partitioned execution of TI DL network
[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                                   configuration_m.enableInternalInput)} );
114     }
116     for (auto &eo : execution_objects_m)
117         eo->RunAsync(ExecutionObject::CallType::INIT);
119     for (auto &eo : execution_objects_m)
120         eo->Wait(ExecutionObject::CallType::INIT);
122     return true;
126 bool ExecutorImpl::InitializeNetworkParams(TIDL_CreateParams *cp)
128     // Determine size of network parameters buffer, allocate it
129     size_t networkparam_size =
130                         GetBinaryFileSize(configuration_m.paramsBinFile);
132     up_malloc_ddr<char> networkparam(malloc_ddr<char>(networkparam_size),
133                                 &__free_ddr);
135     // Read network parameters from bin file into buffer
136     bool status = ReadBinary(configuration_m.paramsBinFile,
137                              networkparam.get(),
138                              networkparam_size);
139     assert(status != false);
141     // Allocate a buffer for passing parameters to the kernel
142     up_malloc_ddr<OCL_TIDL_SetupParams> setupParams(
143                                             malloc_ddr<OCL_TIDL_SetupParams>(),
144                                             &__free_ddr);
146     setupParams->enableTrace = OCL_TIDL_TRACE_OFF;
147     setupParams->networkParamHeapSize = configuration_m.PARAM_HEAP_SIZE;
148     setupParams->noZeroCoeffsPercentage = configuration_m.noZeroCoeffsPercentage;
149     setupParams->sizeofTIDL_CreateParams = sizeof(TIDL_CreateParams);
150     setupParams->offsetofNet = offsetof(TIDL_CreateParams, net);
152     // Allocate buffer for a network parameter heap. Used by the setup
153     // kernel to allocate and initialize network parameters for the layers
154     shared_networkparam_heap_m.reset(malloc_ddr<char>(setupParams->networkParamHeapSize));
156     KernelArgs args = { ArgInfo(cp, sizeof(TIDL_CreateParams)),
157                         ArgInfo(networkparam.get(), networkparam_size),
158                         ArgInfo(shared_networkparam_heap_m.get(),
159                                 setupParams->networkParamHeapSize),
160                         ArgInfo(setupParams.get(),
161                                 sizeof(OCL_TIDL_SetupParams)) };
163     // Execute kernel on first available device in the Executor
164     uint8_t id = static_cast<uint8_t>(*(device_ids_m.cbegin()));
165     unique_ptr<Kernel> K {new Kernel(device_m.get(), STRING(SETUP_KERNEL),
166                                      args, id)};
167     K->RunAsync();
168     K->Wait();
170     if (setupParams->errorCode != OCL_TIDL_SUCCESS)
171         throw Exception(setupParams->errorCode,
172                         __FILE__, __FUNCTION__, __LINE__);
174     return status;
178 void ExecutorImpl::Cleanup()
180     for (auto &eo : execution_objects_m)
181         eo->RunAsync(ExecutionObject::CallType::CLEANUP);
183     for (auto &eo : execution_objects_m)
184         eo->Wait(ExecutionObject::CallType::CLEANUP);
188 void ExecutorImpl::InitializeNetworkCreateParam(TIDL_CreateParams *CP,
189                                           const Configuration& configuration)
191     CP->currCoreId           = configuration.layersGroupId;
192     CP->currLayersGroupId    = configuration.layersGroupId;
193     CP->l1MemSize            = tinn::internal::DMEM0_SIZE;
194     CP->l2MemSize            = tinn::internal::DMEM1_SIZE;
195     CP->l3MemSize            = tinn::internal::OCMC_SIZE;
197     CP->quantHistoryParam1   = tinn::internal::QUANT_HISTORY_PARAM1;
198     CP->quantHistoryParam2   = tinn::internal::QUANT_HISTORY_PARAM2;
199     CP->quantMargin          = tinn::internal::QUANT_MARGIN;
200     CP->optimiseExtMem       = TIDL_optimiseExtMemL1;
203 Exception::Exception(const std::string& error, const std::string& file,
204                      const std::string& func, uint32_t line_no)
207     message_m = "TIDL Error: [";
208     message_m += file;
209     message_m += ", ";
210     message_m += func;
211     message_m += ", ";
212     message_m += std::to_string(line_no);
213     message_m += "]: ";
214     message_m += error;
217 Exception::Exception(int32_t errorCode, const std::string& file,
218                      const std::string& func, uint32_t line_no)
220     message_m = "TIDL Error: [";
221     message_m += file;
222     message_m += ", ";
223     message_m += func;
224     message_m += ", ";
225     message_m += std::to_string(line_no);
226     message_m += "]: ";
228     if (errorCode == OCL_TIDL_ERROR)
229         message_m += "";
230     else if (errorCode == OCL_TIDL_ALLOC_FAIL)
231         message_m += "Allocation failed on device";
232     else if (errorCode == OCL_TIDL_MEMREC_ALLOC_FAIL)
233         message_m += "Memrec allocation failed on device";
234     else if (errorCode == OCL_TIDL_PROCESS_FAIL)
235         message_m += "Process call failed on device";
236     else if (errorCode == OCL_TIDL_CREATE_PARAMS_MISMATCH)
237         message_m += "TIDL_CreateParams definition inconsistent across host"
238                      "and device.";
239     else
240         message_m += std::to_string(errorCode);
244 const char* Exception::what() const noexcept
246     return message_m.c_str();