From e17f61827b848861d5a0f049c589fc8c7ae6425c Mon Sep 17 00:00:00 2001 From: daquexian Date: Sun, 11 Aug 2019 21:07:07 +0800 Subject: [PATCH] Check Android SDK in runtime instead of compile time --- dnnlibrary/DaqReader.cpp | 10 ---- dnnlibrary/ModelBuilderImpl.cpp | 95 ++++++++++++++++++------------- generate_code.py | 7 +-- include/dnnlibrary/ModelBuilder.h | 40 +------------ 4 files changed, 61 insertions(+), 91 deletions(-) diff --git a/dnnlibrary/DaqReader.cpp b/dnnlibrary/DaqReader.cpp index 3321d6c..51d62cd 100644 --- a/dnnlibrary/DaqReader.cpp +++ b/dnnlibrary/DaqReader.cpp @@ -277,29 +277,19 @@ void AddLayers(const DNN::Model &model, ModelBuilder &builder) { break; } case DNN::LayerType::BatchToSpace: { -#if __ANDROID_API__ >= __ANDROID_API_P__ ADD_LAYER(batch_to_space, BatchToSpaceND, input, block_sizes, output); break; -#endif } case DNN::LayerType::SpaceToBatch: { -#if __ANDROID_API__ >= __ANDROID_API_P__ ADD_LAYER(space_to_batch, SpaceToBatchND, input, block_sizes, pads, output); break; -#endif } case DNN::LayerType::StridedSlice: { -#if __ANDROID_API__ >= __ANDROID_API_P__ ADD_LAYER(strided_slice, StridedSlice, input, starts, ends, strides, begin_mask, end_mask, shrink_axis_mask, output); -#else - throw std::invalid_argument("Unsupported layer " + - layer_type_to_str(layer->type()) + - " in API 28"); -#endif break; } case DNN::LayerType::LRN: { diff --git a/dnnlibrary/ModelBuilderImpl.cpp b/dnnlibrary/ModelBuilderImpl.cpp index df9873e..2042e2b 100644 --- a/dnnlibrary/ModelBuilderImpl.cpp +++ b/dnnlibrary/ModelBuilderImpl.cpp @@ -12,7 +12,6 @@ namespace dnn { using namespace android::nn::wrapper; // ModelBuilder auto generated methods start -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddConv( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t padding_left, @@ -20,6 +19,9 @@ ModelBuilder::Index ModelBuilder::AddConv( int32_t stride_x, int32_t stride_y, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Conv requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -64,14 +66,15 @@ ModelBuilder::Index ModelBuilder::AddConv( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddAvePool( const std::string &input, int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, int32_t stride_y, int32_t kernel_width, int32_t kernel_height, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("AvePool requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -90,14 +93,15 @@ ModelBuilder::Index ModelBuilder::AddAvePool( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddMaxPool( const std::string &input, int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, int32_t stride_y, int32_t kernel_width, int32_t kernel_height, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("MaxPool requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -116,10 +120,11 @@ ModelBuilder::Index ModelBuilder::AddMaxPool( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddReLU(const std::string &input, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("ReLU requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -133,11 +138,12 @@ ModelBuilder::Index ModelBuilder::AddReLU(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddSoftmax(const std::string &input, float beta, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Softmax requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -152,13 +158,14 @@ ModelBuilder::Index ModelBuilder::AddSoftmax(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddFC( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("FC requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -201,12 +208,13 @@ ModelBuilder::Index ModelBuilder::AddFC( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddAdd( const std::string &input1, const std::string &input2, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Add requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input1); const auto input1_idx = operand_indexes_.at(input1); @@ -224,11 +232,12 @@ ModelBuilder::Index ModelBuilder::AddAdd( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddConcat( const std::vector &inputs, int32_t axis, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Concat requires API 27"); + } IndexSeq input_indexes; for (const auto &x : inputs) { imm_blob_inputs_.insert(x); @@ -244,8 +253,6 @@ ModelBuilder::Index ModelBuilder::AddConcat( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddDepthwiseConv( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t padding_left, @@ -253,6 +260,9 @@ ModelBuilder::Index ModelBuilder::AddDepthwiseConv( int32_t stride_x, int32_t stride_y, int32_t depth_multiplier, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("DepthwiseConv requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -299,11 +309,12 @@ ModelBuilder::Index ModelBuilder::AddDepthwiseConv( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 28 ModelBuilder::Index ModelBuilder::AddBatchToSpaceND( const std::string &input, const std::vector &block_sizes, const std::string &output) { + if (nnapi_->android_sdk_version < 28) { + throw std::runtime_error("BatchToSpaceND requires API 28"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -321,11 +332,12 @@ ModelBuilder::Index ModelBuilder::AddBatchToSpaceND( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 28 ModelBuilder::Index ModelBuilder::AddSpaceToBatchND( const std::string &input, const std::vector &block_sizes, const std::vector &pads, const std::string &output) { + if (nnapi_->android_sdk_version < 28) { + throw std::runtime_error("SpaceToBatchND requires API 28"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -347,13 +359,14 @@ ModelBuilder::Index ModelBuilder::AddSpaceToBatchND( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 28 ModelBuilder::Index ModelBuilder::AddStridedSlice( const std::string &input, const std::vector &starts, const std::vector &ends, const std::vector &strides, int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, const std::string &output) { + if (nnapi_->android_sdk_version < 28) { + throw std::runtime_error("StridedSlice requires API 28"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -381,12 +394,13 @@ ModelBuilder::Index ModelBuilder::AddStridedSlice( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddMul( const std::string &input1, const std::string &input2, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Mul requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input1); const auto input1_idx = operand_indexes_.at(input1); @@ -404,11 +418,12 @@ ModelBuilder::Index ModelBuilder::AddMul( imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddAdd(const std::string &input, float scalar, int32_t fuse_code, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Add requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -426,11 +441,12 @@ ModelBuilder::Index ModelBuilder::AddAdd(const std::string &input, float scalar, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddMul(const std::string &input, float scalar, int32_t fuse_code, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Mul requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -448,10 +464,11 @@ ModelBuilder::Index ModelBuilder::AddMul(const std::string &input, float scalar, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddDequantize(const std::string &input, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Dequantize requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -465,12 +482,13 @@ ModelBuilder::Index ModelBuilder::AddDequantize(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddLRN(const std::string &input, int32_t radius, float bias, float alpha, float beta, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("LRN requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -486,10 +504,11 @@ ModelBuilder::Index ModelBuilder::AddLRN(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddTanh(const std::string &input, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Tanh requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -503,10 +522,11 @@ ModelBuilder::Index ModelBuilder::AddTanh(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index ModelBuilder::AddFloor(const std::string &input, const std::string &output) { + if (nnapi_->android_sdk_version < 27) { + throw std::runtime_error("Floor requires API 27"); + } IndexSeq input_indexes; imm_blob_inputs_.insert(input); const auto input_idx = operand_indexes_.at(input); @@ -520,7 +540,6 @@ ModelBuilder::Index ModelBuilder::AddFloor(const std::string &input, imm_blob_outputs_.insert(output); return output_idx; } -#endif // __ANDROID_API__ >= 27 // ModelBuilder auto generated methods end // Methods for backward compatibility diff --git a/generate_code.py b/generate_code.py index 6239436..8f49922 100644 --- a/generate_code.py +++ b/generate_code.py @@ -278,13 +278,15 @@ def generate_model_builder(): for i, op in enumerate(cfg): if len(op['input']) == 0: continue - cogoutl('#if __ANDROID_API__ >= {}'.format(op['api'])) ipt_opt = op['input'] + op['output'] params = list(map(get_param, ipt_opt)) if op['support_quant_asymm']: params.append(('const dnn::optional &', 'output_quant_info')) params_str = ', '.join(map(lambda param: "{} {}".format(*param), params)) cogoutl("ModelBuilder::Index ModelBuilder::Add{}({}) {{".format(op['name'], params_str)) + cogoutl(f'if (nnapi_->android_sdk_version < {op["api"]}) {{' + f'throw std::runtime_error("{op["name"]} requires API {op["api"]}");' + f'}}') tensor_input = list(filter(lambda x: x['nnapi_type'] == 'tensor', op['input'])) scalar_input = list(filter(lambda x: x['nnapi_type'] == 'scalar', op['input'])) @@ -317,19 +319,16 @@ def generate_model_builder(): } ''' ) - cogoutl('#endif // __ANDROID_API__ >= {}'.format(op['api'])) update_code('dnnlibrary/ModelBuilderImpl.cpp', 'ModelBuilder auto generated methods') for i, op in enumerate(cfg): if len(op['input']) == 0: continue - cogoutl('#if __ANDROID_API__ >= {}'.format(op['api'])) ipt_opt = op['input'] + op['output'] params = list(map(get_param, ipt_opt)) if op['support_quant_asymm']: params.append(('const dnn::optional &', 'output_quant_info')) params_str = ', '.join(map(lambda param: "{} {}".format(*param), params)) cogoutl("ModelBuilder::Index Add{}({});".format(op['name'], params_str)) - cogoutl('#endif // __ANDROID_API__ >= {}'.format(op['api'])) update_code('include/dnnlibrary/ModelBuilder.h', 'ModelBuilder auto generated methods') diff --git a/include/dnnlibrary/ModelBuilder.h b/include/dnnlibrary/ModelBuilder.h index 9facca2..35c592a 100644 --- a/include/dnnlibrary/ModelBuilder.h +++ b/include/dnnlibrary/ModelBuilder.h @@ -112,7 +112,6 @@ class ModelBuilder { Index AddInput(std::string name, const android::nn::wrapper::OperandType &operand_type); // ModelBuilder auto generated methods start -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddConv( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t padding_left, @@ -120,49 +119,33 @@ class ModelBuilder { int32_t stride_x, int32_t stride_y, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddAvePool( const std::string &input, int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, int32_t stride_y, int32_t kernel_width, int32_t kernel_height, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddMaxPool( const std::string &input, int32_t padding_left, int32_t padding_right, int32_t padding_top, int32_t padding_bottom, int32_t stride_x, int32_t stride_y, int32_t kernel_width, int32_t kernel_height, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddReLU(const std::string &input, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddSoftmax(const std::string &input, float beta, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddFC( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddAdd( const std::string &input1, const std::string &input2, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddConcat(const std::vector &inputs, int32_t axis, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddDepthwiseConv( const std::string &input, const std::string &weight, const dnn::optional &bias, int32_t padding_left, @@ -170,18 +153,12 @@ class ModelBuilder { int32_t stride_x, int32_t stride_y, int32_t depth_multiplier, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 28 ModelBuilder::Index AddBatchToSpaceND( const std::string &input, const std::vector &block_sizes, const std::string &output); -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 28 ModelBuilder::Index AddSpaceToBatchND( const std::string &input, const std::vector &block_sizes, const std::vector &pads, const std::string &output); -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 28 ModelBuilder::Index AddStridedSlice(const std::string &input, const std::vector &starts, const std::vector &ends, @@ -189,39 +166,24 @@ class ModelBuilder { int32_t begin_mask, int32_t end_mask, int32_t shrink_axis_mask, const std::string &output); -#endif // __ANDROID_API__ >= 28 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddMul( const std::string &input1, const std::string &input2, int32_t fuse_code, const std::string &output, const dnn::optional &output_quant_info); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddAdd(const std::string &input, float scalar, int32_t fuse_code, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddMul(const std::string &input, float scalar, int32_t fuse_code, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddDequantize(const std::string &input, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddLRN(const std::string &input, int32_t radius, float bias, float alpha, float beta, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddTanh(const std::string &input, const std::string &output); -#endif // __ANDROID_API__ >= 27 -#if __ANDROID_API__ >= 27 ModelBuilder::Index AddFloor(const std::string &input, const std::string &output); -#endif // __ANDROID_API__ >= 27 - // ModelBuilder auto generated methods end + // ModelBuilder auto generated methods end Index AddDepthWiseConv( const std::string &input_name, int32_t strideX, int32_t strideY, int32_t paddingLeft, int32_t paddingRight, int32_t paddingBottom,