From a28ab854779f198b9643fc89ead833bc47236b70 Mon Sep 17 00:00:00 2001 From: Hyung-Kyu Choi Date: Thu, 22 Mar 2018 20:13:19 +0900 Subject: [PATCH] Add Utils.h for reference NN runtime - add src/runtime/ref/nn/common/include/Utils.h - include Utils.h from CpuExecutor.h Signed-off-by: Hyung-Kyu Choi --- src/runtime/ref/nn/common/include/CpuExecutor.h | 2 +- src/runtime/ref/nn/common/include/Utils.h | 167 ++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/runtime/ref/nn/common/include/Utils.h diff --git a/src/runtime/ref/nn/common/include/CpuExecutor.h b/src/runtime/ref/nn/common/include/CpuExecutor.h index d63d67e..ad94b17 100644 --- a/src/runtime/ref/nn/common/include/CpuExecutor.h +++ b/src/runtime/ref/nn/common/include/CpuExecutor.h @@ -20,8 +20,8 @@ #if 0 // REF-ANN #include "HalInterfaces.h" #include "OperationsUtils.h" -#include "Utils.h" #endif +#include "Utils.h" #include #include diff --git a/src/runtime/ref/nn/common/include/Utils.h b/src/runtime/ref/nn/common/include/Utils.h new file mode 100644 index 0000000..4889fe2 --- /dev/null +++ b/src/runtime/ref/nn/common/include/Utils.h @@ -0,0 +1,167 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ANDROID_ML_NN_COMMON_UTILS_H +#define ANDROID_ML_NN_COMMON_UTILS_H + +#if 0 // REF-ANN +#include "HalInterfaces.h" +#endif +#include "NeuralNetworks.h" + +#if 0 // REF-ANN +#include +#endif +#include + +namespace android { +namespace nn { + +// The number of data types (OperandCode) defined in NeuralNetworks.h. +const int kNumberOfDataTypes = 6; + +// The number of operation types (OperationCode) defined in NeuralNetworks.h. +const int kNumberOfOperationTypes = 30; + +// The number of execution preferences defined in NeuralNetworks.h. +const int kNumberOfPreferences = 3; + +// The number of data types (OperandCode) defined in NeuralNetworksOEM.h. +const int kNumberOfDataTypesOEM = 2; + +// The number of operation types (OperationCode) defined in NeuralNetworksOEM.h. +const int kNumberOfOperationTypesOEM = 1; + +// The lowest number assigned to any OEM Code in NeuralNetworksOEM.h. +const int kOEMCodeBase = 10000; + +#if 0 // REF-ANN +/* IMPORTANT: if you change the following list, don't + * forget to update the corresponding 'tags' table in + * the initVlogMask() function implemented in Utils.cpp. + */ +enum VLogFlags { + MODEL = 0, + COMPILATION, + EXECUTION, + CPUEXE, + MANAGER, + DRIVER +}; + +#define VLOG_IS_ON(TAG) \ + ((vLogMask & (1 << (TAG))) != 0) + +#define VLOG(TAG) \ + if (LIKELY(!VLOG_IS_ON(TAG))) \ + ; \ + else \ + LOG(INFO) + +extern int vLogMask; +void initVLogMask(); + +// Assert macro, as Android does not generally support assert. +#define nnAssert(v) \ + do { \ + if (!(v)) { \ + LOG(ERROR) << "nnAssert failed at " << __FILE__ << ":" << __LINE__ << " - '" << #v \ + << "'\n"; \ + abort(); \ + } \ + } while (0) + +// Returns the amount of space needed to store a value of the specified +// dimensions and type. +uint32_t sizeOfData(OperandType type, const std::vector& dimensions); + +// Returns the amount of space needed to store a value of the dimensions and +// type of this operand. +inline uint32_t sizeOfData(const Operand& operand) { + return sizeOfData(operand.type, operand.dimensions); +} + +// Returns the name of the operation in ASCII. +const char* getOperationName(OperationType opCode); + +// Memory is unmapped. +// Memory is reference counted by hidl_memory instances, and is deallocated +// once there are no more references. +hidl_memory allocateSharedMemory(int64_t size); + +// Returns the number of padding bytes needed to align data of the +// specified length. It aligns object of length: +// 2, 3 on a 2 byte boundary, +// 4+ on a 4 byte boundary. +// We may want to have different alignments for tensors. +// TODO: This is arbitrary, more a proof of concept. We need +// to determine what this should be. +uint32_t alignBytesNeeded(uint32_t index, size_t length); + +// Does a detailed LOG(INFO) of the model +void logModelToInfo(const Model& model); + +inline void setFromIntList(hidl_vec* vec, uint32_t count, const uint32_t* data) { + vec->resize(count); + for (uint32_t i = 0; i < count; i++) { + (*vec)[i] = data[i]; + } +} + +inline void setFromIntList(std::vector* vec, uint32_t count, const uint32_t* data) { + vec->resize(count); + for (uint32_t i = 0; i < count; i++) { + (*vec)[i] = data[i]; + } +} + +inline std::string toString(uint32_t obj) { + return std::to_string(obj); +} + +template +std::string toString(const std::vector& range) { + std::string os = "["; + for (size_t i = 0; i < range.size(); ++i) { + os += (i == 0 ? "" : ", ") + toString(range[i]); + } + return os += "]"; +} + +inline bool validCode(uint32_t codeCount, uint32_t codeCountOEM, uint32_t code) { + return (code < codeCount) || (code >= kOEMCodeBase && (code - kOEMCodeBase) < codeCountOEM); +} + +int validateOperandType(const ANeuralNetworksOperandType& type, const char* tag, bool allowPartial); +int validateOperandList(uint32_t count, const uint32_t* list, uint32_t operandCount, + const char* tag); +bool validateModel(const Model& model); +bool validateRequest(const Request& request, const Model& model); + +inline size_t getSizeFromInts(int lower, int higher) { + return (uint32_t)(lower) + ((uint64_t)(uint32_t)(higher) << 32); +} + +#ifdef NN_DEBUGGABLE +uint32_t getProp(const char* str, uint32_t defaultValue = 0); +#endif // NN_DEBUGGABLE + +#endif // REF-ANN + +} // namespace nn +} // namespace android + +#endif // ANDROID_ML_NN_COMMON_UTILS_H -- 2.7.4