19 Capabilities GenerateCapabilities()
21 VLOG(DRIVER) <<
"ArmnnDriverImpl::GenerateCapabilities()";
23 float defaultPerfValue = .1f;
24 const Capabilities::PerformanceInfo defaultPerfInfo = { defaultPerfValue,
27 std::vector<OperandType> operandsTypes({
31 OperandType::TENSOR_FLOAT32,
32 OperandType::TENSOR_INT32,
33 OperandType::TENSOR_QUANT8_ASYMM,
35 OperandType::TENSOR_QUANT16_SYMM,
36 OperandType::TENSOR_FLOAT16,
37 OperandType::TENSOR_BOOL8,
39 OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL,
40 OperandType::TENSOR_QUANT16_ASYMM,
41 OperandType::TENSOR_QUANT8_SYMM,
42 OperandType::TENSOR_QUANT8_ASYMM_SIGNED,
45 std::vector<Capabilities::OperandPerformance> operandPerformances;
46 operandPerformances.reserve(operandsTypes.size());
48 for (
auto opType : operandsTypes)
50 operandPerformances.push_back(
51 Capabilities::OperandPerformance{ opType, defaultPerfInfo });
54 auto operandPerformanceTable =
55 Capabilities::OperandPerformanceTable::create(std::move(operandPerformances)).value();
57 return { defaultPerfInfo,
59 std::move(operandPerformanceTable),
64 size_t Hash(std::vector<uint8_t>& cacheData)
66 std::size_t hash = cacheData.size();
67 for (
auto& i : cacheData)
69 hash = ((hash << 5) - hash) + i;
81 bool ArmnnDriverImpl::ValidateSharedHandle(
const SharedHandle& sharedHandle)
85 if (*sharedHandle < 0)
90 int dataCacheFileAccessMode = fcntl(*sharedHandle, F_GETFL) & O_ACCMODE;
91 if (dataCacheFileAccessMode != O_RDWR)
99 GeneralResult<SharedPreparedModel> ArmnnDriverImpl::PrepareArmnnModel(
104 const std::vector<SharedHandle>& modelCacheHandle,
105 const std::vector<SharedHandle>& dataCacheHandle,
106 const CacheToken& token,
107 bool float32ToFloat16,
110 VLOG(DRIVER) <<
"ArmnnDriverImpl::PrepareArmnnModel()";
114 return NN_ERROR(ErrorStatus::DEVICE_UNAVAILABLE) <<
"Device unavailable";
117 if (
const auto result = validate(model); !result.ok())
119 return NN_ERROR(ErrorStatus::INVALID_ARGUMENT) <<
"Invalid model passed as input";
125 std::set<unsigned int> unsupportedOperations;
128 unsupportedOperations);
132 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) <<
"ModelToINetworkConverter failed";
137 std::vector<uint8_t> dataCacheData;
138 bool serializeToFile = dataCacheHandle.size() < 1 ? false :
true;
139 auto serializedNetworkFileName =
154 unsigned int numberOfCachedModelFiles = 0;
155 if (modelCacheHandle.size() > 0)
157 unsigned int index = 0;
163 if (numberOfCacheFiles > 0)
165 numberOfCachedModelFiles += numberOfCacheFiles;
169 cachedFd = *modelCacheHandle[index];
170 saveCachedNetwork =
true;
172 index += numberOfCachedModelFiles;
180 {
"SaveCachedNetwork", saveCachedNetwork },
183 {
"CachedFileDescriptor", cachedFd }
194 std::vector<std::string> errMessages;
199 runtime->GetDeviceSpec(),
203 catch (std::exception& e)
205 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << e.what();
211 std::stringstream message;
212 message <<
"Invalid optimized network";
213 for (
const std::string& msg : errMessages)
215 message <<
"\n" << msg;
217 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << message.str();
229 MemorySource::Undefined,
230 MemorySource::Undefined,
232 auto numInputs =
getMainModel(model).inputIndexes.size();
233 auto numOutputs =
getMainModel(model).outputIndexes.size();
238 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) <<
"Network could not be loaded";
241 catch (std::exception& e)
243 std::stringstream message;
244 message <<
"Exception (" << e.what()<<
") caught from LoadNetwork.";
245 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << message.str();
256 size_t hashValue = 0;
257 if (dataCacheHandle.size() == 1 )
259 hashValue = Hash(dataCacheData);
263 if (modelCacheHandle.size() > 0)
265 if (modelCacheHandle.size() == numberOfCachedModelFiles)
267 for (uint32_t i = 0; i < modelCacheHandle.size(); ++i)
269 int modelCacheFileAccessMode = fcntl(*modelCacheHandle[i], F_GETFL) & O_ACCMODE;
270 if (modelCacheFileAccessMode != O_RDONLY)
272 struct stat statBuffer;
273 if (fstat(*modelCacheHandle[i], &statBuffer) == 0)
275 long modelDataSize = statBuffer.st_size;
276 if (modelDataSize > 0)
278 std::vector<uint8_t> modelData(modelDataSize);
279 pread(*modelCacheHandle[i], modelData.data(), modelData.size(), 0);
280 hashValue ^= Hash(modelData);
287 if (dataCacheHandle.size() == 1 && hashValue != 0)
289 std::vector<uint8_t> theHashValue(
sizeof(hashValue));
290 ::memcpy(theHashValue.data(), &hashValue,
sizeof(hashValue));
292 write(*dataCacheHandle[0], theHashValue.data(), theHashValue.size());
293 pwrite(*dataCacheHandle[0], dataCacheData.data(), dataCacheData.size(), theHashValue.size());
296 bool executeWithDummyInputs = (std::find(options.
GetBackends().begin(),
300 auto preparedModel = std::make_shared<const ArmnnPreparedModel>(netId,
314 if (!preparedModel->ExecuteWithDummyInputs(numInputs, numOutputs))
316 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) <<
"Network could not be executed";
319 if (clTunedParameters &&
328 catch (std::exception&
error)
330 VLOG(DRIVER) <<
"ArmnnDriverImpl::prepareModel: Failed to save CL tuned parameters file"
335 return std::move(preparedModel);
338 GeneralResult<SharedPreparedModel> ArmnnDriverImpl::PrepareArmnnModelFromCache(
342 const std::vector<SharedHandle>& modelCacheHandle,
343 const std::vector<SharedHandle>& dataCacheHandle,
344 const CacheToken& token,
345 bool float32ToFloat16)
347 VLOG(DRIVER) <<
"ArmnnDriverImpl::PrepareArmnnModelFromCache()";
351 return NN_ERROR(ErrorStatus::DEVICE_UNAVAILABLE)
352 <<
"ArmnnDriverImpl::prepareModelFromCache(): Device unavailable";
355 if (token.size() != ANEURALNETWORKS_BYTE_SIZE_OF_CACHE_TOKEN)
357 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
358 <<
"ArmnnDriverImpl::prepareModelFromCache(): Token size does not match!";
362 if (dataCacheHandle.size() != 1)
364 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
365 <<
"ArmnnDriverImpl::prepareModelFromCache(): Not valid data cache handle!";
368 if (!ValidateSharedHandle(dataCacheHandle[0]))
370 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
371 <<
"ArmnnDriverImpl::prepareModelFromCache(): Not valid data cache handle!";
374 size_t cachedDataSize = 0;
375 struct stat dataStatBuffer;
376 if (fstat(*dataCacheHandle[0], &dataStatBuffer) == 0)
378 cachedDataSize = dataStatBuffer.st_size;
380 if (cachedDataSize == 0)
382 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
383 <<
"ArmnnDriverImpl::prepareModelFromCache(): Not valid cached data!";
387 unsigned int numberOfCachedModelFiles = 0;
392 if (modelCacheHandle.size() != numberOfCachedModelFiles)
394 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
395 <<
"ArmnnDriverImpl::prepareModelFromCache(): Model cache handle size does not match.";
399 std::vector<uint8_t> hashValue(
sizeof(
size_t));
400 pread(*dataCacheHandle[0], hashValue.data(), hashValue.size(), 0);
403 if (cachedDataSize < hashValue.size())
405 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
406 <<
"ArmnnDriverImpl::prepareModelFromCache(): cachedDataSize is less than hashValue!";
408 std::vector<uint8_t> dataCacheData(cachedDataSize - hashValue.size());
409 pread(*dataCacheHandle[0], dataCacheData.data(), dataCacheData.size(), hashValue.size());
410 auto calculatedHashValue = Hash(dataCacheData);
412 int gpuAccCachedFd = -1;
413 if (modelCacheHandle.size() > 0)
415 unsigned int index = 0;
421 if (numberOfCacheFiles > 0)
423 if (!ValidateSharedHandle(modelCacheHandle[index]))
425 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
426 <<
"ArmnnDriverImpl::prepareModelFromCache(): Invalid model cache handle!";
428 int cachedFd = *modelCacheHandle[index];
429 struct stat statBuffer;
430 if (fstat(cachedFd, &statBuffer) == 0)
432 long modelDataSize = statBuffer.st_size;
433 if (modelDataSize > 0)
435 std::vector<uint8_t> modelData(modelDataSize);
436 pread(cachedFd, modelData.data(), modelData.size(), 0);
437 calculatedHashValue ^= Hash(modelData);
441 gpuAccCachedFd = cachedFd;
445 index += numberOfCacheFiles;
450 std::vector<uint8_t> calculatedHashData(
sizeof(calculatedHashValue));
451 ::memcpy(calculatedHashData.data(), &calculatedHashValue,
sizeof(calculatedHashValue));
452 if (hashValue != calculatedHashData)
454 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
455 <<
"ArmnnDriverImpl::prepareModelFromCache(): ValidateHash() failed!";
464 catch (std::exception&)
466 return NN_ERROR(ErrorStatus::GENERAL_FAILURE)
467 <<
"ArmnnDriverImpl::prepareModelFromCache(): Exception caught from Deserializer!";
479 {
"SaveCachedNetwork",
false },
482 {
"CachedFileDescriptor", gpuAccCachedFd }
493 std::vector<std::string> errMessages;
498 runtime->GetDeviceSpec(),
502 catch (std::exception& e)
504 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << e.what();
510 std::stringstream message;
511 message <<
"Invalid optimized network";
512 for (
const std::string& msg : errMessages)
514 message <<
"\n" << msg;
516 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << message.str();
528 MemorySource::Undefined,
529 MemorySource::Undefined,
535 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) <<
"Network could not be loaded";
538 catch (std::exception& e)
540 std::stringstream message;
541 message <<
"Exception (" << e.what()<<
") caught from LoadNetwork.";
542 return NN_ERROR(ErrorStatus::GENERAL_FAILURE) << message.str();
545 auto preparedModel = std::make_shared<const ArmnnPreparedModel>(netId,
551 return std::move(preparedModel);
556 VLOG(DRIVER) <<
"ArmnnDriverImpl::GetCapabilities()";
557 static const Capabilities theCapabilities = GenerateCapabilities();
558 return theCapabilities;