Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1. make model_check support dynamic range quantized model; #1653

Merged
merged 1 commit into from
Apr 28, 2022
Merged
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
11 changes: 9 additions & 2 deletions platforms/android/model_check_android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ OPTION_DUMP_OUTPUT=""
OPTION_CHECK_BATCH=
OPTION_CHECK_OUTPUT=
SET_PRECISION=
OPTION_REFERENCE_FILE=""

function usage() {
echo "usage: ./model_check_android.sh [-32] [-v82] [-c] [-b] [-d] <device-id> [-t] <CPU/GPU> [-m] <tnnproto> [-i] <input_file> [-p] [-o] [-s <AUTO/...>]"
Expand All @@ -32,6 +33,7 @@ function usage() {
echo " -t ARM/OPENCL/HUAWEI_NPU specify the platform to run (default: ARM)"
echo " -m tnnproto"
echo " -i input file (NCHW Float)"
echo " -f the reference output file to compare"
echo " -p Push models to device"
echo " -do specify the dump output path(eg: /data/local/tmp/model_check/output_dump)"
echo " -a check multi batch"
Expand Down Expand Up @@ -148,14 +150,14 @@ function run_android() {

if [ -n "$INPUT_PATH" ]
then
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR:${ANDROID_DIR}/lib ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel -i $ANDROID_DATA_DIR/input.txt $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR:${ANDROID_DIR}/lib ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel -i $ANDROID_DATA_DIR/input.txt $OPTION_REFERENCE_FILE $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
else
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR:${ANDROID_DIR}/lib ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
fi
else
if [ -n "$INPUT_PATH" ]
then
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel -i $ANDROID_DATA_DIR/input.txt $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel -i $ANDROID_DATA_DIR/input.txt $OPTION_REFERENCE_FILE $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
else
$ADB shell "cd $ANDROID_DIR ; LD_LIBRARY_PATH=$ANDROID_DIR ./model_check -d $DEVICE -p $ANDROID_DATA_DIR/test.tnnproto -m $ANDROID_DATA_DIR/test.tnnmodel $OPTION_DUMP_OUTPUT $OPTION_CHECK_BATCH $OPTION_CHECK_OUTPUT $SET_PRECISION >> $ANDROID_DIR/test_log.txt"
fi
Expand Down Expand Up @@ -209,6 +211,11 @@ while [ "$1" != "" ]; do
INPUT_PATH="$1"
shift
;;
-f)
shift
OPTION_REFERENCE_FILE=" -f $1"
shift
;;
-do)
shift
OPTION_DUMP_OUTPUT=" -do $1"
Expand Down
23 changes: 20 additions & 3 deletions source/tnn/optimizer/net_optimizer_dynamic_range_dequant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ namespace optimizer {
default:
break;
}
layer->param->dynamic_range_quantized = false;
}

return TNN_OK;
Expand All @@ -74,6 +73,11 @@ namespace optimizer {
auto layer_name = layer->name;
auto conv_resource = std::dynamic_pointer_cast<ConvLayerResource>(resource->resource_map[layer_name]);
auto filter_handle = conv_resource->filter_handle;
if (filter_handle.GetDataType() != DATA_TYPE_INT8) {
LOGD("dynamic range dequantize layer(%s) weight data type is not int8_t."
"This weight might have been dequantized before.\n", layer_name.c_str());
return TNN_OK;
}
auto scale_handler = conv_resource->scale_handle;

const auto filter_dims = filter_handle.GetBufferDims();
Expand All @@ -97,6 +101,7 @@ namespace optimizer {
conv_resource->filter_handle = weight_buf;
conv_resource->scale_handle = RawBuffer();

layer->param->dynamic_range_quantized = false;
return TNN_OK;
}

Expand All @@ -108,6 +113,11 @@ namespace optimizer {
auto scale_name = buffer_name + DynamicRangeQuantScaleSuffix;
auto buffer = resource->constant_map[buffer_name];
auto scale = resource->constant_map[scale_name];
if (buffer->GetDataType() != DATA_TYPE_INT8) {
LOGD("dynamic range dequantize layer(%s) weight data type is not int8_t."
"This weight might have been dequantized before.\n", layer->name.c_str());
return TNN_OK;
}

const int data_size = buffer->GetDataCount();
auto weight_ptr = buffer->force_to<int8_t *>();
Expand All @@ -129,6 +139,8 @@ namespace optimizer {
resource->constant_map.erase(scale_name);
}
}

layer->param->dynamic_range_quantized = false;
return TNN_OK;
}

Expand All @@ -137,6 +149,11 @@ namespace optimizer {
auto layer_name = layer->name;
auto matmul_resource = std::dynamic_pointer_cast<MatMulLayerResource>(resource->resource_map[layer_name]);
auto scale_handle = matmul_resource->scale_handle;
if (matmul_resource->weight.GetDataType() != DATA_TYPE_INT8) {
LOGD("Dynamic range dequantize layer(%s) weight data type is not int8_t."
"This weight might have been dequantized before.\n", layer_name.c_str());
return TNN_OK;
}

const int data_size = matmul_resource->weight.GetDataCount();
auto weight_ptr = matmul_resource->weight.force_to<int8_t *>();
Expand All @@ -151,8 +168,8 @@ namespace optimizer {
weight_buf.SetDataType(DATA_TYPE_FLOAT);
weight_buf.SetBufferDims(matmul_resource->weight.GetBufferDims());

matmul_resource->weight = weight_buf;

matmul_resource->weight = weight_buf;
layer->param->dynamic_range_quantized = false;
return TNN_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions tools/dynamic_range_quantization/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ int main(int argc, char* argv[]) {
{
std::ifstream proto_stream(tnn_proto);
if (!proto_stream.is_open() || !proto_stream.good()) {
LOGE("Dynamic_range_quantization: open %s failed\n", tnn_proto.c_str());
return -1;
}
auto buffer = std::string((std::istreambuf_iterator<char>(proto_stream)), std::istreambuf_iterator<char>());
Expand All @@ -65,6 +66,7 @@ int main(int argc, char* argv[]) {
{
std::ifstream model_stream(tnn_model);
if (!model_stream.is_open() || !model_stream.good()) {
LOGE("Dynamic_range_quantization: open %s failed\n", tnn_model.c_str());
return -1;
}
auto buffer = std::string((std::istreambuf_iterator<char>(model_stream)), std::istreambuf_iterator<char>());
Expand Down