Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ endif()

# add benchncnn to a virtual project group
set_property(TARGET benchncnn PROPERTY FOLDER "benchmark")

# generate model data
include(${CMAKE_CURRENT_SOURCE_DIR}/../cmake/ncnn_generate_model_data_header.cmake)

file(GLOB PARAM_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.param")

foreach(PARAM_FILE ${PARAM_FILES})
ncnn_convert_param_file(${PARAM_FILE})
endforeach()

configure_file(model_param_registry.h.in ${CMAKE_CURRENT_BINARY_DIR}/model_param_registry.h)
configure_file(model_param_spv_data.h.in ${CMAKE_CURRENT_BINARY_DIR}/model_param_spv_data.h)
6 changes: 3 additions & 3 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Usage
```shell
# copy all param files to the current directory
./benchncnn [loop count] [num threads] [powersave] [gpu device] [cooling down] [(key=value)...]
param=model.param
param=model # no need to add the .param suffix
shape=[227,227,3],..
```
run benchncnn on android device
Expand All @@ -37,7 +37,7 @@ adb shell
# executed in android adb shell
cd /data/local/tmp/
./benchncnn [loop count] [num threads] [powersave] [gpu device] [cooling down] [(key=value)...]
param=model.param
param=model # no need to add the .param suffix
shape=[227,227,3],..
```

Expand All @@ -50,7 +50,7 @@ Parameter
|powersave|0=all cores, 1=little cores only, 2=big cores only|0|
|gpu device|-1=cpu-only, 0=gpu0, 1=gpu1 ...|-1|
|cooling down|0=disable, 1=enable|1|
|param|ncnn model.param filepath|-|
|param|ncnn model.param filename without .param suffix|-|
|shape|model input shapes with, whc format|-|

Tips: Disable android UI server and set CPU and GPU to max frequency
Expand Down
49 changes: 42 additions & 7 deletions benchmark/benchncnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "net.h"
#include "gpu.h"

#include "../benchmark/model_param_spv_data.h"

#ifndef NCNN_SIMPLESTL
#include <vector>
#endif
Expand All @@ -40,12 +42,37 @@ static bool g_enable_cooling_down = true;
static ncnn::UnlockedPoolAllocator g_blob_pool_allocator;
static ncnn::PoolAllocator g_workspace_pool_allocator;

struct model_param_registry_entry
{
const char* model_name;
const char* model_param;
};

static const model_param_registry_entry model_param_registry[] = {
#include "../benchmark/model_param_registry.h"
};

#if NCNN_VULKAN
static ncnn::VulkanDevice* g_vkdev = 0;
static ncnn::VkAllocator* g_blob_vkallocator = 0;
static ncnn::VkAllocator* g_staging_vkallocator = 0;
#endif // NCNN_VULKAN

const char* find_model_param(const char* model_name)
{
int param_num = sizeof(model_param_registry) / sizeof(model_param_registry_entry);
const char* model_param = NULL;
for (int i = 0; i < param_num; i++)
{
if (!strcmp(model_name, model_param_registry[i].model_name))
{
model_param = model_param_registry[i].model_param;
break;
}
}
return model_param;
}

void benchmark(const char* comment, const std::vector<ncnn::Mat>& _in, const ncnn::Option& opt, bool fixed_path = true)
{
// Skip if int8 model name and using GPU
Expand Down Expand Up @@ -84,17 +111,25 @@ void benchmark(const char* comment, const std::vector<ncnn::Mat>& _in, const ncn
#define MODEL_DIR ""
#endif

const char* model_param = NULL;

if (fixed_path)
{
char parampath[256];
sprintf(parampath, MODEL_DIR "%s.param", comment);
net.load_param(parampath);
model_param = find_model_param(comment);
}
else
{
net.load_param(comment);
// use load_param_mem
model_param = find_model_param(comment);
if (model_param == NULL)
{
fprintf(stderr, "can't find %s.param in model dir\n", comment);
Copy link

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message references 'model dir' which is no longer accurate since models are now embedded in memory. Consider updating to 'can't find %s model in embedded data' or similar.

Suggested change
fprintf(stderr, "can't find %s.param in model dir\n", comment);
fprintf(stderr, "can't find %s.param in embedded data\n", comment);

Copilot uses AI. Check for mistakes.
return;
}
}
Comment on lines 116 to 129
Copy link

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic in both branches of the if-else statement is identical. Consider removing the conditional check since both fixed_path true and false cases now perform the same operations.

Copilot uses AI. Check for mistakes.

net.load_param_mem(model_param);

DataReaderFromEmpty dr;
net.load_model(dr);

Expand Down Expand Up @@ -452,13 +487,13 @@ int main(int argc, char** argv)

benchmark("mobilenetv2_yolov3", ncnn::Mat(352, 352, 3), opt);

benchmark("yolov4-tiny", ncnn::Mat(416, 416, 3), opt);
benchmark("yolov4_tiny", ncnn::Mat(416, 416, 3), opt);

benchmark("nanodet_m", ncnn::Mat(320, 320, 3), opt);

benchmark("yolo-fastest-1.1", ncnn::Mat(320, 320, 3), opt);
benchmark("yolo_fastest_1_1", ncnn::Mat(320, 320, 3), opt);

benchmark("yolo-fastestv2", ncnn::Mat(352, 352, 3), opt);
benchmark("yolo_fastestv2", ncnn::Mat(352, 352, 3), opt);

benchmark("vision_transformer", ncnn::Mat(384, 384, 3), opt);

Expand Down
5 changes: 5 additions & 0 deletions benchmark/model_param_registry.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Model Param Registry header
//
// This file is auto-generated by cmake, don't edit it.

@model_param_registry@
5 changes: 5 additions & 0 deletions benchmark/model_param_spv_data.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Model Param Spv Data header
//
// This file is auto-generated by cmake, don't edit it.

@model_param_spv_data@
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions cmake/ncnn_generate_model_data_header.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

macro(ncnn_convert_param_file PARAM_FILE)
# Use a macro to convert the contents of a single parameter file into memory content
file(READ ${PARAM_FILE} param_file_data)

# remove whitespace
string(REGEX REPLACE "\n +" "\n" param_file_data ${param_file_data})

# remove empty line
string(REGEX REPLACE "\n\n" "\n" param_file_data ${param_file_data})

# Get the file name with extension
get_filename_component(PARAM_FILE_NAME ${PARAM_FILE} NAME)
# Manually remove ".param" since NAME_WE treats ".1.param" as a multi-extension
string(REPLACE ".param" "" PARAM_FILE_NAME_WE "${PARAM_FILE_NAME}")
# Check if the result is empty
if (NOT PARAM_FILE_NAME_WE)
message(FATAL_ERROR "Failed to extract valid filename from '${PARAM_FILE}'")
endif()
# Check if the extracted filename is a valid C identifier
string(REGEX MATCH "^[A-Za-z_][A-Za-z0-9_]*$" is_valid "${PARAM_FILE_NAME_WE}")
if (NOT is_valid)
message(FATAL_ERROR "Extracted filename '${PARAM_FILE_NAME_WE}' is not a valid C identifier")
endif()

# text to hex
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/param_hex_data/${PARAM_FILE_NAME_WE}.text2hex.txt "${param_file_data}")
file(READ ${CMAKE_CURRENT_BINARY_DIR}/param_hex_data/${PARAM_FILE_NAME_WE}.text2hex.txt param_file_data_hex HEX)
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," param_file_data_hex ${param_file_data_hex})
string(FIND "${param_file_data_hex}" "," tail_comma REVERSE)
string(SUBSTRING "${param_file_data_hex}" 0 ${tail_comma} param_file_data_hex)

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/model_param_header/${PARAM_FILE_NAME_WE}.comp.hex.h "static const char ${PARAM_FILE_NAME_WE}_param_data[] = {${param_file_data_hex},0x00};\n")
string(APPEND model_param_spv_data "#include \"model_param_header/${PARAM_FILE_NAME_WE}.comp.hex.h\"\n")
string(APPEND model_param_registry "{\"${PARAM_FILE_NAME_WE}\", ${PARAM_FILE_NAME_WE}_param_data},\n")
endmacro()
Loading