diff --git a/mindspore/lite/micro/example/mnist_x86/CMakeLists.txt b/mindspore/lite/micro/example/mnist_x86/CMakeLists.txt index 72cb7f92b8..f61fb44189 100644 --- a/mindspore/lite/micro/example/mnist_x86/CMakeLists.txt +++ b/mindspore/lite/micro/example/mnist_x86/CMakeLists.txt @@ -22,12 +22,10 @@ endif() if(PLATFORM_ARM64) add_compile_definitions(ENABLE_ARM64) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.2-a+dotprod") endif() if(PLATFORM_ARM32) add_compile_definitions(ENABLE_ARM32) - add_definitions(-mfloat-abi=softfp -mfpu=neon) endif() set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}") diff --git a/mindspore/lite/micro/example/mnist_x86/benchmark/benchmark.cc b/mindspore/lite/micro/example/mnist_x86/benchmark/benchmark.cc index 7c9cb3f6ec..7e7473baf1 100644 --- a/mindspore/lite/micro/example/mnist_x86/benchmark/benchmark.cc +++ b/mindspore/lite/micro/example/mnist_x86/benchmark/benchmark.cc @@ -111,7 +111,7 @@ int main(int argc, const char **argv) { } lite::Context *context = nullptr; - if (argc >= 5) { + if (argc >= 6) { // config benchmark context context = new (std::nothrow) lite::Context(); if (context == nullptr) { diff --git a/mindspore/lite/micro/example/mnist_x86/benchmark/load_input.c b/mindspore/lite/micro/example/mnist_x86/benchmark/load_input.c old mode 100755 new mode 100644 diff --git a/mindspore/lite/micro/example/mnist_x86/benchmark/load_input.h b/mindspore/lite/micro/example/mnist_x86/benchmark/load_input.h old mode 100755 new mode 100644 diff --git a/mindspore/lite/micro/example/mnist_x86/mnist_input.bin b/mindspore/lite/micro/example/mnist_x86/mnist_input.bin index 32154910c7..3f84f468b7 100644 Binary files a/mindspore/lite/micro/example/mnist_x86/mnist_input.bin and b/mindspore/lite/micro/example/mnist_x86/mnist_input.bin differ diff --git a/mindspore/lite/micro/example/mnist_x86/src/CMakeLists.txt b/mindspore/lite/micro/example/mnist_x86/src/CMakeLists.txt index 9b18bb607b..068b562762 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/CMakeLists.txt +++ b/mindspore/lite/micro/example/mnist_x86/src/CMakeLists.txt @@ -8,8 +8,9 @@ endif() get_filename_component(PKG_PATH ${PKG_PATH} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_BINARY_DIR}) -set(OP_LIB ${PKG_PATH}/tools/codegen/operator_library/lib/libops.a) -set(OP_HEADER_PATH ${PKG_PATH}/tools/codegen/operator_library/include) +set(OP_LIB ${PKG_PATH}/inference/lib/libmindspore-lite.a) +set(WRAPPER_LIB ${PKG_PATH}/tools/codegen/lib/libwrapper.a) +set(OP_HEADER_PATH ${PKG_PATH}/tools/codegen/include) set(HEADER_PATH ${PKG_PATH}/inference) message("operator lib path: ${OP_LIB}") @@ -20,6 +21,13 @@ add_compile_definitions(NOT_USE_STL) include_directories(${OP_HEADER_PATH}) include_directories(${HEADER_PATH}) +if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64) + include_directories(${PKG_PATH}/tools/codegen/third_party/include) + include_directories(${PKG_PATH}/tools/codegen/third_party/include/CMSIS/Core/Include) + include_directories(${PKG_PATH}/tools/codegen/third_party/include/CMSIS/DSP/Include) + include_directories(${PKG_PATH}/tools/codegen/third_party/include/CMSIS/NN/Include) +endif() + include(net.cmake) option(PLATFORM_ARM64 "build android arm64" OFF) @@ -32,12 +40,10 @@ endif() if(PLATFORM_ARM64) add_compile_definitions(ENABLE_ARM64) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=armv8.2-a+dotprod") endif() if(PLATFORM_ARM32) add_compile_definitions(ENABLE_ARM32) - add_definitions(-mfloat-abi=softfp -mfpu=neon) endif() set(CMAKE_C_FLAGS "${CMAKE_ENABLE_C99} ${CMAKE_C_FLAGS}") @@ -64,11 +70,17 @@ function(create_library) COMMAND rm -rf tmp COMMAND mkdir tmp COMMAND cd tmp && ar -x ${OP_LIB} + COMMAND cd tmp && ar -x ${WRAPPER_LIB} COMMAND echo "raw static library ${library_name} size:" COMMAND ls -lh ${library_name} COMMAND mv ${library_name} ./tmp && cd tmp && ar -x ${library_name} COMMENT "unzip raw static library ${library_name}" ) + if(NOT PLATFORM_ARM32 AND NOT PLATFORM_ARM64) + set(CMSIS_LIB ${PKG_PATH}/tools/codegen/third_party/lib/libcmsis_nn.a) + add_custom_command(TARGET net POST_BUILD COMMAND cd tmp && ar -x ${CMSIS_LIB}) + endif() + foreach(object_file ${OP_SRC}) add_custom_command(TARGET net POST_BUILD COMMAND mv ./tmp/${object_file} .) endforeach() diff --git a/mindspore/lite/micro/example/mnist_x86/src/model.h b/mindspore/lite/micro/example/mnist_x86/src/model.h index 838cbbbc90..0dc11c0b0e 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/model.h +++ b/mindspore/lite/micro/example/mnist_x86/src/model.h @@ -1,6 +1,6 @@ /** - * Copyright 2020 Huawei Technologies Co., Ltd + * Copyright 2021 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,12 +46,11 @@ class MModel : public Model { Model *Model::Import(const char *model_buf, size_t size) { MS_NULLPTR_IF_NULL(model_buf); - MModel *model = new (std::nothrow) MModel(); - MS_NULLPTR_IF_NULL(model); if (size == 0) { - delete model; return nullptr; } + MModel *model = new (std::nothrow) MModel(); + MS_NULLPTR_IF_NULL(model); model->buf = reinterpret_cast(malloc(size)); if (model->buf == nullptr) { delete model; diff --git a/mindspore/lite/micro/example/mnist_x86/src/net.bin b/mindspore/lite/micro/example/mnist_x86/src/net.bin index 2bde284518..7fe3011570 100644 Binary files a/mindspore/lite/micro/example/mnist_x86/src/net.bin and b/mindspore/lite/micro/example/mnist_x86/src/net.bin differ diff --git a/mindspore/lite/micro/example/mnist_x86/src/net.c b/mindspore/lite/micro/example/mnist_x86/src/net.c index b128efafff..422a4d8845 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/net.c +++ b/mindspore/lite/micro/example/mnist_x86/src/net.c @@ -37,12 +37,12 @@ int CopyOutputsData(void **outputs, int num) { if (num != 1) { return RET_ERROR; } - memcpy(outputs[0], g_Buffer+56, 40); + memcpy(outputs[0], g_Buffer + 32, 40); return RET_OK; } int GetBufferSize() { - return 40032; + return 39248; } int SetBuffer( void *buffer) { if (buffer == NULL) { @@ -62,108 +62,107 @@ void FreeResource() { } void Inference() { { -DoQuantizeFp32ToInt8((float *)(g_Input0), (int8_t *)(g_Buffer+0), 0.007874015718698501587, 0, 784, false); - } - { -memset((int16_t *)(g_Buffer+10928), 0, 2048); -memset((int16_t *)(g_Buffer+12976), 0, 256); -memset((int *)(g_Buffer+13232), 0, 6144); -memset((int8_t *)(g_Buffer+19376), 0, 8112); -memset((int16_t *)(g_Buffer+27488), 0, 12544); -QuantArg conv_param__quant_arg_in[1] = {{0.007874015718698501587, 0}}; -QuantArg conv_param__quant_arg_w[12] = {{0.003238174133002758026, -6}, {0.003890725085511803627, -8}, {0.003394871251657605171, -7}, {0.001685356837697327137, -127}, {0.004322394262999296188, 1}, {0.002274985425174236298, -56}, {0.003617759561166167259, 17}, {0.004447745624929666519, 23}, {0.004683905746787786484, 26}, {0.004021023400127887726, 24}, {0.005650237202644348145, 11}, {0.001966834301128983498, -84}}; -QuantArg conv_param__quant_arg_out[1] = {{0.01778890006244182587, 0}}; -double conv_param__real_multiplier[12] = {0.001433333970799530351, 0.001722176774828924938, 0.00150269379968211614, 0.0007460003866156953226, 0.001913249346122961134, 0.001006991503636309139, 0.001601352314486244018, 0.001968734305210294733, 0.002073267527210802957, 0.00177985160945266568, 0.002501001060249878095, 0.0008705926067589928779}; +memset((int16_t *)(g_Buffer + 10144), 0, 2048); +memset((int16_t *)(g_Buffer + 12192), 0, 256); +memset((int *)(g_Buffer + 12448), 0, 6144); +memset((int8_t *)(g_Buffer + 18592), 0, 8112); +memset((int16_t *)(g_Buffer + 26704), 0, 12544); +QuantArg conv_param__quant_arg_in[1] = {{0.003921568859368562698, -128}}; +QuantArg conv_param__quant_arg_w[12] = {{0.005689438898116350174, 0}, {0.006241692230105400085, 0}, {0.007301395758986473083, 0}, {0.005148916970938444138, 0}, {0.005132303573191165924, 0}, {0.004976313561201095581, 0}, {0.00564815988764166832, 0}, {0.002269793068990111351, 0}, {0.0030086529441177845, 0}, {0.005234404932707548141, 0}, {0.007580270525068044662, 0}, {0.004589735530316829681, 0}}; +QuantArg conv_param__quant_arg_out[1] = {{0.01811622083187103271, 17}}; +double conv_param__real_multiplier[12] = {0.001231577267748737653, 0.001351122051282624588, 0.00158051323770531417, 0.001114571969708069233, 0.001110975704014940469, 0.001077209041359399825, 0.001222641776980984765, 0.0004913359221160916793, 0.0006512749113606706042, 0.001133077320583530554, 0.001640880438584302065, 0.0009935275121536731122}; int conv_param__left_shift[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -int conv_param__right_shift[12] = {-9, -9, -9, -10, -9, -9, -9, -8, -8, -9, -8, -10}; -int conv_param__quant_multiplier[12] = {1575967367, 1893553389, 1652229306, 1640472199, 2103639903, 1107198867, 1760705490, 1082323130, 1139790877, 1956967540, 1374939873, 1914453388}; -int conv_param__out_act_min[1] = {0}; +int conv_param__right_shift[12] = {-9, -9, -9, -9, -9, -9, -9, -10, -10, -9, -9, -9}; +int conv_param__quant_multiplier[12] = {1354133526, 1485574406, 1737792683, 1225484841, 1221530705, 1184403867, 1344308850, 1080459119, 1432168676, 1245831689, 1804167122, 1092395052}; +int conv_param__out_act_min[1] = {-128}; int conv_param__out_act_max[1] = {127}; -ConvQuantArg conv_param__conv_quant_arg = {(RoundingMode)(1), 2, conv_param__quant_arg_in, conv_param__quant_arg_w, conv_param__quant_arg_out, conv_param__real_multiplier, conv_param__left_shift, conv_param__right_shift, conv_param__quant_multiplier, conv_param__out_act_min, conv_param__out_act_max, 1, 12, 1, 2}; +ConvQuantArg conv_param__conv_quant_arg = {(RoundingMode)(2), 2, conv_param__quant_arg_in, conv_param__quant_arg_w, conv_param__quant_arg_out, conv_param__real_multiplier, conv_param__left_shift, conv_param__right_shift, conv_param__quant_multiplier, conv_param__out_act_min, conv_param__out_act_max, 1, 12, 1, 2}; int thread_num = MSMIN(g_thread_num, 26); -ConvParameter conv_param_ = {{ "", 35, g_thread_num}, conv_param__conv_quant_arg, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 28, 28, 1, 1, 26, 26, 12, thread_num, 0, 0, (PadMode)(2), (ActType)(1), 0, 0, 0}; -PackInputToC8Int8((int8_t *)(g_Buffer+0), (int16_t *)(g_Buffer+27488), &conv_param_); -Conv3x3Int8((int16_t *)(g_Buffer+27488), g_Weight10, g_Weight11, (int8_t *)(g_Buffer+784), (int16_t *)(g_Buffer+10928), (int16_t *)(g_Buffer+12976), (int *)(g_Buffer+13232), (int8_t *)(g_Buffer+19376), 0, &conv_param_); -PackNC4HW4ToNHWCInt8((int8_t *)(g_Buffer+19376), (int8_t *)(g_Buffer+784), 1, 676, 12); +ConvParameter conv_param_ = {{ "", true, 35, g_thread_num, 0}, conv_param__conv_quant_arg, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 28, 28, 1, 1, 26, 26, 12, thread_num, 0, 0, (PadMode)(2), (ActType)(0), 0, 0, 0}; +PackInputToC8Int8((int8_t *)(g_Input0), (int16_t *)(g_Buffer + 26704), &conv_param_); +Conv3x3Int8((int16_t *)(g_Buffer + 26704), g_Weight10, g_Weight11, (int8_t *)(g_Buffer + 0), (int16_t *)(g_Buffer + 10144), (int16_t *)(g_Buffer + 12192), (int *)(g_Buffer + 12448), (int8_t *)(g_Buffer + 18592), 0, &conv_param_); +PackNC4HW4ToNHWCInt8((int8_t *)(g_Buffer + 18592), (int8_t *)(g_Buffer + 0), 1, 676, 12); } { -static QuantArg pooling_parameter_quant_in = {0.01778890006244182587, 0}; -static QuantArg pooling_parameter_quant_out = {0.01778890006244182587, 0}; +static QuantArg pooling_parameter_quant_in = {0.01811622083187103271, 17}; +static QuantArg pooling_parameter_quant_out = {0.01811622083187103271, 17}; static QuantArg *pooling_parameter_quant[2] = { &pooling_parameter_quant_in, &pooling_parameter_quant_out}; -const PoolingParameter pooling_parameter = {{ "", 92, g_thread_num}, (PoolMode)(1), (RoundMode)(2), (PadMode)(2), (ActType)(0), 0, false, 2, 2, 2, 2, 26, 26, 1, 12, 13, 13, 1, 12, 0, 0, 0, 0, 0, pooling_parameter_quant, false}; -MaxPoolingInt8((int8_t *)(g_Buffer+784), (int8_t *)(g_Buffer+8896), (PoolingParameter *)&pooling_parameter, 0); +const PoolingParameter pooling_parameter = {{ "", true, 92, g_thread_num, 0}, (PoolMode)(1), (RoundMode)(2), (PadMode)(2), (ActType)(0), 0, false, 2, 2, 2, 2, 26, 26, 1, 12, 13, 13, 1, 12, 0, 0, 0, 0, 0, pooling_parameter_quant, false}; +MaxPoolingInt8((int8_t *)(g_Buffer + 0), (int8_t *)(g_Buffer + 8112), (PoolingParameter *)&pooling_parameter, 0); } { -memset((int16_t *)(g_Buffer+10928), 0, 4096); -memset((int16_t *)(g_Buffer+15024), 0, 256); -memset((int *)(g_Buffer+15280), 0, 6144); -memset((int8_t *)(g_Buffer+21424), 0, 1452); -memset((int16_t *)(g_Buffer+22876), 0, 5408); -QuantArg conv_param__quant_arg_in[1] = {{0.01778890006244182587, 0}}; -QuantArg conv_param__quant_arg_w[12] = {{0.005374609492719173431, 33}, {0.005837683100253343582, 22}, {0.004709810949862003326, -15}, {0.003726204857230186462, 27}, {0.00318551529198884964, -8}, {0.003453079145401716232, 50}, {0.004045850131660699844, -9}, {0.003903790842741727829, 30}, {0.004003710579127073288, -10}, {0.00560879148542881012, 27}, {0.005486610345542430878, -23}, {0.003554018214344978333, 4}}; -QuantArg conv_param__quant_arg_out[1] = {{0.07183934003114700317, 0}}; -double conv_param__real_multiplier[12] = {0.001330863973520378732, 0.001445530533608141606, 0.001166246148374064893, 0.0009226850783705293785, 0.0007887991893445710223, 0.0008550534992628172192, 0.001001835847923064193, 0.0009666590447744700769, 0.0009914011740411567478, 0.001388852288199173826, 0.00135859773990280961, 0.0008800481219728497088}; +memset((int16_t *)(g_Buffer + 10144), 0, 4096); +memset((int16_t *)(g_Buffer + 14240), 0, 256); +memset((int *)(g_Buffer + 14496), 0, 6144); +memset((int8_t *)(g_Buffer + 20640), 0, 1452); +memset((int16_t *)(g_Buffer + 22092), 0, 5408); +QuantArg conv_param__quant_arg_in[1] = {{0.01811622083187103271, 17}}; +QuantArg conv_param__quant_arg_w[12] = {{0.006381968967616558075, 0}, {0.005092236679047346115, 0}, {0.004954888485372066498, 0}, {0.007594361435621976852, 0}, {0.006317862775176763535, 0}, {0.004739056341350078583, 0}, {0.004733041394501924515, 0}, {0.005125139374285936356, 0}, {0.005773660261183977127, 0}, {0.007067613303661346436, 0}, {0.00728381425142288208, 0}, {0.004714466165751218796, 0}}; +QuantArg conv_param__quant_arg_out[1] = {{0.118615470826625824, 31}}; +double conv_param__real_multiplier[12] = {0.0009747224012760375951, 0.0007777407468524931162, 0.0007567634496453238277, 0.001159891919861241348, 0.0009649314419479496259, 0.0007237992569070154231, 0.0007228806183814449719, 0.0007827659621256170689, 0.0008818150205007141765, 0.001079441365823280083, 0.001112461807995879974, 0.0007200436103814696152}; int conv_param__left_shift[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -int conv_param__right_shift[12] = {-9, -9, -9, -10, -10, -10, -9, -10, -9, -9, -9, -10}; -int conv_param__quant_multiplier[12] = {1463300414, 1589377630, 1282301201, 2029005945, 1734587761, 1880282530, 1101530164, 2125705720, 1090057119, 1527059240, 1493794012, 1935246286}; -int conv_param__out_act_min[1] = {0}; +int conv_param__right_shift[12] = {-10, -10, -10, -9, -10, -10, -10, -10, -10, -9, -9, -10}; +int conv_param__quant_multiplier[12] = {2143437228, 1710269989, 1664140425, 1275314653, 2121906681, 1591651398, 1589631291, 1721320554, 1939131737, 1186858333, 1223164693, 1583392644}; +int conv_param__out_act_min[1] = {-128}; int conv_param__out_act_max[1] = {127}; ConvQuantArg conv_param__conv_quant_arg = {(RoundingMode)(1), 2, conv_param__quant_arg_in, conv_param__quant_arg_w, conv_param__quant_arg_out, conv_param__real_multiplier, conv_param__left_shift, conv_param__right_shift, conv_param__quant_multiplier, conv_param__out_act_min, conv_param__out_act_max, 1, 12, 1, 2}; int thread_num = MSMIN(g_thread_num, 11); -ConvParameter conv_param_ = {{ "", 35, g_thread_num}, conv_param__conv_quant_arg, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 13, 13, 12, 1, 11, 11, 12, thread_num, 0, 0, (PadMode)(2), (ActType)(1), 0, 0, 0}; -PackInputToC8Int8((int8_t *)(g_Buffer+8896), (int16_t *)(g_Buffer+22876), &conv_param_); -Conv3x3Int8((int16_t *)(g_Buffer+22876), g_Weight12, g_Weight13, (int8_t *)(g_Buffer+0), (int16_t *)(g_Buffer+10928), (int16_t *)(g_Buffer+15024), (int *)(g_Buffer+15280), (int8_t *)(g_Buffer+21424), 0, &conv_param_); -PackNC4HW4ToNHWCInt8((int8_t *)(g_Buffer+21424), (int8_t *)(g_Buffer+0), 1, 121, 12); +ConvParameter conv_param_ = {{ "", true, 35, g_thread_num, 0}, conv_param__conv_quant_arg, 3, 3, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 13, 13, 12, 1, 11, 11, 12, thread_num, 0, 0, (PadMode)(2), (ActType)(0), 0, 0, 0}; +PackInputToC8Int8((int8_t *)(g_Buffer + 8112), (int16_t *)(g_Buffer + 22092), &conv_param_); +Conv3x3Int8((int16_t *)(g_Buffer + 22092), g_Weight12, g_Weight13, (int8_t *)(g_Buffer + 0), (int16_t *)(g_Buffer + 10144), (int16_t *)(g_Buffer + 14240), (int *)(g_Buffer + 14496), (int8_t *)(g_Buffer + 20640), 0, &conv_param_); +PackNC4HW4ToNHWCInt8((int8_t *)(g_Buffer + 20640), (int8_t *)(g_Buffer + 0), 1, 121, 12); } { -static QuantArg pooling_parameter_quant_in = {0.07136065512895584106, 0}; -static QuantArg pooling_parameter_quant_out = {0.07136065512895584106, 0}; +static QuantArg pooling_parameter_quant_in = {0.118615470826625824, 31}; +static QuantArg pooling_parameter_quant_out = {0.118615470826625824, 31}; static QuantArg *pooling_parameter_quant[2] = { &pooling_parameter_quant_in, &pooling_parameter_quant_out}; -const PoolingParameter pooling_parameter = {{ "", 92, g_thread_num}, (PoolMode)(1), (RoundMode)(2), (PadMode)(2), (ActType)(0), 0, false, 2, 2, 2, 2, 11, 11, 1, 12, 5, 5, 1, 12, 0, 0, 0, 0, 0, pooling_parameter_quant, false}; -MaxPoolingInt8((int8_t *)(g_Buffer+0), (int8_t *)(g_Buffer+1456), (PoolingParameter *)&pooling_parameter, 0); +const PoolingParameter pooling_parameter = {{ "", true, 92, g_thread_num, 0}, (PoolMode)(1), (RoundMode)(2), (PadMode)(2), (ActType)(0), 0, false, 2, 2, 2, 2, 11, 11, 1, 12, 5, 5, 1, 12, 0, 0, 0, 0, 0, pooling_parameter_quant, false}; +MaxPoolingInt8((int8_t *)(g_Buffer + 0), (int8_t *)(g_Buffer + 1456), (PoolingParameter *)&pooling_parameter, 0); } { -const ReshapeQuantArg reshape_quant_arg = {{0.07136065512895584106, 0}, {0.07136065512895584106, 0}, -128, 127}; -Int8Reshape((int8_t *)(g_Buffer+1456), (int8_t *)(g_Buffer+0), 300, reshape_quant_arg); +const ReshapeQuantArg reshape_quant_arg = {{0.118615470826625824, 31}, {0.118615470826625824, 31}, -128, 127}; +Int8Reshape((int8_t *)(g_Buffer + 1456), (int8_t *)(g_Buffer + 0), 300, reshape_quant_arg); } { -int32_t tmp_weight_zp = 1; -RowMajor2Row16x4MajorInt8((int8_t *)(g_Buffer+0)+0, (int8_t *)(g_Buffer+10928), 1, 300); -CalcInputSums((int8_t *)(g_Buffer+0)+0, 1, 300, tmp_weight_zp, (int *)(g_Buffer+12144), RowMajor); -float filter_scale[20] = {0.003479549195617437363, 0.004490676335990428925, 0.004529818892478942871, 0.002983231563121080399, 0.003455155529081821442, 0.003223794745281338692, 0.003272445406764745712, 0.003801185870543122292, 0.003679843153804540634, 0.003040234791114926338, 0.003704284550622105598, 0.003355232765898108482, 0.002904496388509869576, 0.003024494973942637444, 0.002794801956042647362, 0.004355110693722963333, 0.003499472280964255333, 0.004184196703135967255, 0.003057289868593215942, 0.003264668164774775505}; -int filter_zp[20] = {1, 12, 3, 2, -10, -5, -11, 5, 12, 22, 16, 1, -5, 15, 13, 5, -10, -5, -6, 0}; -int left_shift[20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -int right_shift[20] = {-10, -9, -9, -10, -10, -10, -10, -9, -9, -10, -9, -10, -10, -10, -10, -9, -10, -9, -10, -10}; -int multiplier[20] = {2108215049, 1360422072, 1372280070, 1807502393, 2093435146, 1953256619, 1982733521, 1151545365, 1114785262, 1842040025, 1122189669, 2032893316, 1759797843, 1832503464, 1693335354, 1319353429, 2120286176, 1267576078, 1852373503, 1978021333}; -const MatmulQuantParameter matmul_quant_parameter = {{0.07136065512895584106, 0}, {0, 0}, {0.258998185396194458, 0}, -128, 127, filter_scale, filter_zp, left_shift, right_shift, multiplier}; -int32_t *cur_left = matmul_quant_parameter.left_shift_ + 0; -int32_t *cur_right = matmul_quant_parameter.right_shift_ + 0; -int32_t *cur_mul = matmul_quant_parameter.quant_multiplier_ + 0; -int32_t *cur_zp = matmul_quant_parameter.filter_zp_ + 0; -MatmulInt8Opt((int8_t *)(g_Buffer+10928), g_Weight15+0 + 0, (int8_t *)(g_Buffer+304)+0+0, 1, 20, 304, (int *)(g_Buffer+12144), g_Weight16+0, -128, 127, 0, cur_mul, cur_left, cur_right, 20, true, cur_zp); +int32_t tmp_weight_zp = 0; +RowMajor2Row16x4MajorInt8((int8_t *)(g_Buffer + 0)+0, (int8_t *)(g_Buffer + 10144), 1, 300); +CalcInputSums((int8_t *)(g_Buffer + 0)+0, 1, 300, tmp_weight_zp, (int *)(g_Buffer + 11360), RowMajor); +float filter_scale[1] = {0.007667620200663805008}; +int filter_zp[1] = {0}; +int left_shift[1] = {0}; +int right_shift[1] = {-8}; +int multiplier[1] = {1379728867}; +const MatmulQuantParameter matmul_quant_parameter = {{0.118615470826625824, 31}, {0, 0}, {0.3623915016651153564, 11}, -128, 127, filter_scale, filter_zp, left_shift, right_shift, multiplier}; +int32_t *cur_left = matmul_quant_parameter.left_shift_; +int32_t *cur_right = matmul_quant_parameter.right_shift_; +int32_t *cur_mul = matmul_quant_parameter.quant_multiplier_ ; +int32_t *cur_zp = matmul_quant_parameter.filter_zp_ ; +MatmulInt8Opt((int8_t *)(g_Buffer + 10144), g_Weight15+0 + 0, (int8_t *)(g_Buffer + 304)+0+0, 1, 20, 304, (int *)(g_Buffer + 11360), g_Weight16+0, -128, 127, 11, cur_mul, cur_left, cur_right, 20, false, cur_zp); } { -int32_t tmp_weight_zp = 1; -RowMajor2Row16x4MajorInt8((int8_t *)(g_Buffer+304)+0, (int8_t *)(g_Buffer+10928), 1, 20); -CalcInputSums((int8_t *)(g_Buffer+304)+0, 1, 20, tmp_weight_zp, (int *)(g_Buffer+11056), RowMajor); -float filter_scale[10] = {0.004678330849856138229, 0.005127115640789270401, 0.00471437256783246994, 0.004531511571258306503, 0.005476122256368398666, 0.004348111804574728012, 0.004803542047739028931, 0.006081215571612119675, 0.004532597027719020844, 0.004762654658406972885}; -int filter_zp[10] = {7, -2, 9, 2, -6, 21, 16, 10, -19, 8}; -int left_shift[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -int right_shift[10] = {-8, -8, -8, -8, -8, -8, -8, -8, -8, -8}; -int multiplier[10] = {1242805482, 1362025788, 1252380041, 1203802750, 1454739904, 1155082292, 1276068015, 1615483838, 1204091115, 1265206260}; -const MatmulQuantParameter matmul_quant_parameter = {{0.258998185396194458, 0}, {0, 0}, {0.5359870791435241699, 0}, -128, 127, filter_scale, filter_zp, left_shift, right_shift, multiplier}; -int32_t *cur_left = matmul_quant_parameter.left_shift_ + 0; -int32_t *cur_right = matmul_quant_parameter.right_shift_ + 0; -int32_t *cur_mul = matmul_quant_parameter.quant_multiplier_ + 0; -int32_t *cur_zp = matmul_quant_parameter.filter_zp_ + 0; -MatmulInt8Opt((int8_t *)(g_Buffer+10928), g_Weight18+0 + 0, (int8_t *)(g_Buffer+0)+0+0, 1, 10, 32, (int *)(g_Buffer+11056), g_Weight19+0, -128, 127, 0, cur_mul, cur_left, cur_right, 10, true, cur_zp); +int32_t tmp_weight_zp = 0; +RowMajor2Row16x4MajorInt8((int8_t *)(g_Buffer + 304)+0, (int8_t *)(g_Buffer + 10144), 1, 20); +CalcInputSums((int8_t *)(g_Buffer + 304)+0, 1, 20, tmp_weight_zp, (int *)(g_Buffer + 10272), RowMajor); +float filter_scale[1] = {0.006908571347594261169}; +int filter_zp[1] = {0}; +int left_shift[1] = {0}; +int right_shift[1] = {-8}; +int multiplier[1] = {1282256865}; +const MatmulQuantParameter matmul_quant_parameter = {{0.3623915016651153564, 11}, {0, 0}, {1.073398709297180176, -20}, -128, 127, filter_scale, filter_zp, left_shift, right_shift, multiplier}; +int32_t *cur_left = matmul_quant_parameter.left_shift_; +int32_t *cur_right = matmul_quant_parameter.right_shift_; +int32_t *cur_mul = matmul_quant_parameter.quant_multiplier_ ; +int32_t *cur_zp = matmul_quant_parameter.filter_zp_ ; +MatmulInt8Opt((int8_t *)(g_Buffer + 10144), g_Weight18+0 + 0, (int8_t *)(g_Buffer + 0)+0+0, 1, 10, 32, (int *)(g_Buffer + 10272), g_Weight19+0, -128, 127, -20, cur_mul, cur_left, cur_right, 10, false, cur_zp); } { -DoDequantizeInt8ToFp32((int8_t *)(g_Buffer+0), (float *)(g_Buffer+16), 0.5359870791435241699, 0, 10); +const SoftmaxQuantArg quant_args = {{1.073398709297180176, 20}, {0.00390625, -128}, -128, 127, 1152553088, 27, 27}; +const SoftmaxParameter softmax_parameter = {{ "", true, 138, g_thread_num, 0}, 1, {1, 10}, 10, 2}; +memset((int *)(g_Buffer + 10144), 0, 40); +memset((int *)(g_Buffer + 10184), 0, 40); +SoftmaxInt8((int8_t *)(g_Buffer + 0), (int8_t *)(g_Buffer + 16), 1, (int *)(g_Buffer + 10144), (int *)(g_Buffer + 10184), quant_args, (SoftmaxParameter *)&softmax_parameter); } { -const SoftmaxParameter softmax_parameter = {{ "", 138, g_thread_num}, 1, {1, 10}, 10, 2}; -memset((float *)(g_Buffer+10928), 0, 4); -Softmax((float *)(g_Buffer+16), (float *)(g_Buffer+56), (float *)(g_Buffer+10928), &softmax_parameter); +DoDequantizeInt8ToFp32((int8_t *)(g_Buffer + 16), (float *)(g_Buffer + 32), 0.00390625, -128, 10); } } diff --git a/mindspore/lite/micro/example/mnist_x86/src/net.cmake b/mindspore/lite/micro/example/mnist_x86/src/net.cmake index ca68e45c83..db04299e56 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/net.cmake +++ b/mindspore/lite/micro/example/mnist_x86/src/net.cmake @@ -4,7 +4,6 @@ set(OP_SRC common_func_int8.c.o conv3x3_int8.c.o conv_int8.c.o - exp_fp32.c.o fixed_point.c.o matmul_int8.c.o matmul_int8_wrapper.c.o @@ -12,7 +11,7 @@ set(OP_SRC pooling_int8.c.o quant_dtype_cast_int8.c.o reshape_int8.c.o - softmax_fp32.c.o + softmax_int8.c.o weight.c.o net.c.o session.cc.o diff --git a/mindspore/lite/micro/example/mnist_x86/src/session.cc b/mindspore/lite/micro/example/mnist_x86/src/session.cc index 68c48116af..9774db8c65 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/session.cc +++ b/mindspore/lite/micro/example/mnist_x86/src/session.cc @@ -30,14 +30,14 @@ int LiteSession::CompileGraph(lite::Model *model) { in_shape_0[1] = 28; in_shape_0[2] = 28; in_shape_0[3] = 1; - inputs_[0] = new (std::nothrow) MTensor(String("graph_input-0"), kNumberTypeFloat32, in_shape_0); + inputs_[0] = new (std::nothrow) MTensor(String("graph_input-0"), kNumberTypeInt8, in_shape_0); MS_ERROR_IF_NULL(inputs_[0]); outputs_.resize(1); Vector out_shape_0; out_shape_0.resize(2); out_shape_0[0] = 1; out_shape_0[1] = 10; - outputs_[0] = new (std::nothrow) MTensor(String("Softmax-7"), kNumberTypeFloat32, out_shape_0); + outputs_[0] = new (std::nothrow) MTensor(String("int8toft32_Softmax-7_post0/output-0"), kNumberTypeFloat32, out_shape_0); MS_ERROR_IF_NULL(outputs_[0]); int ret = Init(model->buf, static_cast(model)->buf_size()); return ret; @@ -126,7 +126,6 @@ mindspore::tensor::MSTensor *LiteSession::GetOutputByTensorName(const String &te } return nullptr; } - } // namespace lite session::LiteSession *session::LiteSession::CreateSession(const lite::Context *context) { auto *session = new (std::nothrow) lite::LiteSession(); diff --git a/mindspore/lite/micro/example/mnist_x86/src/session.h b/mindspore/lite/micro/example/mnist_x86/src/session.h index 0c4f085091..6a4e628140 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/session.h +++ b/mindspore/lite/micro/example/mnist_x86/src/session.h @@ -78,6 +78,7 @@ class LiteSession : public session::LiteSession { Vector outputs_; void *runtime_buffer_; }; + } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/micro/example/mnist_x86/src/tensor.h b/mindspore/lite/micro/example/mnist_x86/src/tensor.h index 6c49a322f2..dbd9302c5d 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/tensor.h +++ b/mindspore/lite/micro/example/mnist_x86/src/tensor.h @@ -59,7 +59,6 @@ class MTensor : public mindspore::tensor::MSTensor { void *data_ = nullptr; Vector quant_params_; }; - } // namespace lite } // namespace mindspore diff --git a/mindspore/lite/micro/example/mnist_x86/src/weight.c b/mindspore/lite/micro/example/mnist_x86/src/weight.c index 8d97badd6c..c6f319e66b 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/weight.c +++ b/mindspore/lite/micro/example/mnist_x86/src/weight.c @@ -29,6 +29,10 @@ int32_t *g_Weight16 = NULL; int32_t *g_Weight17 = NULL; int8_t *g_Weight18 = NULL; int32_t *g_Weight19 = NULL; +int8_t g_Weight6[6000]; +int32_t g_Weight7[20]; +int8_t g_Weight8[200]; +int32_t g_Weight9[10]; int Init(void *weight_buffer, int weight_size) { if (weight_buffer == NULL) { @@ -39,19 +43,19 @@ int Init(void *weight_buffer, int weight_size) { size_t size; size_t offset; }; - int8_t *g_Weight6 = (weight_buffer + 9312); - int32_t *g_Weight7 = (weight_buffer + 15312); - int8_t *g_Weight8 = (weight_buffer + 15392); - int32_t *g_Weight9 = (weight_buffer + 15592); struct ModelParameter model_params[] = { {g_Weight10, 3072, 0}, {g_Weight11, 48, 3072}, {g_Weight12, 6144, 3120}, {g_Weight13, 48, 9264}, + {g_Weight6, 6000, 9312}, + {g_Weight7, 80, 15312}, + {g_Weight8, 200, 15392}, + {g_Weight9, 40, 15592}, }; - for(int i = 0; i < 4; ++i) { + for(int i = 0; i < 8; ++i) { if (model_params[i].offset + model_params[i].size > weight_size) { return RET_ERROR; } @@ -74,8 +78,8 @@ if (g_Weight15 == NULL) { return RET_ERROR; } memset(g_Weight15, 0, 6080); -int init_filter_zp[20] = {1, 12, 3, 2, -10, -5, -11, 5, 12, 22, 16, 1, -5, 15, 13, 5, -10, -5, -6, 0}; -InitInt8MatrixB(g_Weight6, g_Weight16, g_Weight15, 1, 300, 20, 20, 304, 0, init_filter_zp, g_Weight14, true, true); +int init_filter_zp[1] = {0}; +InitInt8MatrixB(g_Weight6, g_Weight16, g_Weight15, 1, 300, 20, 20, 304, 31, init_filter_zp, g_Weight14, true, false); } { g_Weight17 = malloc(48); @@ -94,8 +98,8 @@ if (g_Weight18 == NULL) { return RET_ERROR; } memset(g_Weight18, 0, 384); -int init_filter_zp[10] = {7, -2, 9, 2, -6, 21, 16, 10, -19, 8}; -InitInt8MatrixB(g_Weight8, g_Weight19, g_Weight18, 1, 20, 10, 12, 32, 0, init_filter_zp, g_Weight17, true, true); +int init_filter_zp[1] = {0}; +InitInt8MatrixB(g_Weight8, g_Weight19, g_Weight18, 1, 20, 10, 12, 32, 11, init_filter_zp, g_Weight17, true, false); } return RET_OK; } diff --git a/mindspore/lite/micro/example/mnist_x86/src/weight.h b/mindspore/lite/micro/example/mnist_x86/src/weight.h index 7be657370a..e56fb5223d 100644 --- a/mindspore/lite/micro/example/mnist_x86/src/weight.h +++ b/mindspore/lite/micro/example/mnist_x86/src/weight.h @@ -17,7 +17,6 @@ #include "nnacl/common_func.h" #include "nnacl/errorcode.h" -#include "nnacl/fp32/softmax_fp32.h" #include "nnacl/int8/common_func_int8.h" #include "nnacl/int8/conv3x3_int8.h" #include "nnacl/int8/conv_int8.h" @@ -25,6 +24,7 @@ #include "nnacl/int8/pooling_int8.h" #include "nnacl/int8/quant_dtype_cast_int8.h" #include "nnacl/int8/reshape_int8.h" +#include "nnacl/int8/softmax_int8.h" #include "wrapper/int8/matmul_int8_wrapper.h" #include #include @@ -45,3 +45,7 @@ extern int32_t *g_Weight16; extern int32_t *g_Weight17; extern int8_t *g_Weight18; extern int32_t *g_Weight19; +extern int8_t g_Weight6[]; +extern int32_t g_Weight7[]; +extern int8_t g_Weight8[]; +extern int32_t g_Weight9[]; diff --git a/mindspore/lite/micro/example/mobilenetv2/README.md b/mindspore/lite/micro/example/mobilenetv2/README.md index 353dcdf30d..50bc6edc0f 100755 --- a/mindspore/lite/micro/example/mobilenetv2/README.md +++ b/mindspore/lite/micro/example/mobilenetv2/README.md @@ -55,12 +55,26 @@ codegen编译[MobileNetv2模型](https://download.mindspore.cn/model_zoo/officia 在编译此工程之前需要预先获取安卓平台对应的[Release包](https://www.mindspore.cn/tutorial/lite/zh-CN/master/use/downloads.html)。 -算子静态库的目录如下: - -```bash -├── operator_library # 对应平台算子库目录 - ├── include # 对应平台算子库头文件目录 - └── lib # 对应平台算子库静态库目录 +安卓平台对应的Release包的目录如下: +```text +mindspore-lite-{version}-inference-android-{arch} +├── inference +│ ├── include # 推理框架头文件 +│ ├── lib # 推理框架库 +│ │ ├── libmindspore-lite.a # MindSpore Lite推理框架的静态库 +│ │ └── libmindspore-lite.so # MindSpore Lite推理框架的动态库 +│ ├── minddata # 图像处理库 +│ │ ├── include +│ │ └── lib +│ │ └── libminddata-lite.so # 图像处理动态库文件 +│ └── third_party # NPU库 +│ └── hiai_ddk +└── tools + ├── benchmark # 基准测试工具 + │ └── benchmark + └── codegen # 代码生成工具 + ├── include # 算子头文件 + └── lib # 算子静态库 ``` 生成代码工程目录如下: @@ -68,7 +82,6 @@ codegen编译[MobileNetv2模型](https://download.mindspore.cn/model_zoo/officia ```bash ├── mobilenetv2 # 生成代码的根目录 ├── benchmark # 生成代码的benchmark目录 - ├── include # 模型推理代码对外暴露头文件目录 └── src # 模型推理代码目录 ``` @@ -91,7 +104,7 @@ cmake -DCMAKE_BUILD_TYPE=Release \ -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" \ -DANDROID_NATIVE_API_LEVEL="19" \ -DPLATFORM_ARM64=ON \ --DPKG_PATH={path to}/mindspore-lite-{version}-inference-android .. +-DPKG_PATH={path to}/mindspore-lite-{version}-inference-android-{arch} .. make ``` @@ -104,7 +117,7 @@ cmake -DCMAKE_BUILD_TYPE=Release \ -DANDROID_TOOLCHAIN_NAME="clang" \ -DANDROID_NATIVE_API_LEVEL="19" \ -DMICRO_BUILD_ARM32=ON \ --DPKG_PATH={path to}/mindspore-lite-{version}-inference-android .. +-DPKG_PATH={path to}/mindspore-lite-{version}-inference-android-{arch} .. make ```