#!/usr/bin/env bash

RUN_DIR="$(pwd)"
SCRIPT_DIR=$(dirname $(readlink -f "$0"))

# configure tool path
quantizeTool=$QUANTIZETOOL_PATH
if [ -z $quantizeTool ];then
  echo "no set QUANTIZETOOL_PATH , such as export QUANTIZETOOL_PATH="/**/HeronRT_v0.10.2_2025.02.20/tool/quantizeTool/""
  exit
fi



TOOL_PATH=$quantizeTool
HERON_CONVERTER_PATH=${TOOL_PATH}/HeronConverter
HERON_QUANTIZER_PATH=${TOOL_PATH}/HeronQuantizer
HERON_EVALUATOR_PATH=${TOOL_PATH}/HeronImageClassificitionEvaluator
HERON_EXPORTER_PATH=${TOOL_PATH}/HeronFullQuantizeModelExporter
HERON_COMPILER_PATH=${TOOL_PATH}/../HeronCompiler
HERON_profile_PATH=${TOOL_PATH}/../HeronProfiler
HERON_ACC_analyze=${TOOL_PATH}/AccuracyAnalyser
HERON_GoldenGenerator=${TOOL_PATH}/GoldenGenerator
model_path=$MODEL_PATH


# if [ -z $model_path ];then

#   echo "no set model path , need download ..."
#   rm -rf ./output_inference
#   wget -P ./output_inference https://gitee.com/onnx-models/models/raw/master/Yolo/yolov10n.onnx
#   ## You might need to download this file separately. Sometimes the size of the downloaded file may not be correct.
#   if [ $? -ne 0 ]; then
#     exit
#   fi
#   model_path=./output_inference/yolov10n.onnx
# fi
model_path=/data1/guofeng/code/vnne_dev_tools/debug_6920_onnx_json_sym_rgb_deploy_model.onnx

model_name=$(basename "$model_path")

model_file_name=${model_name%%.onnx}
DUMP_PATH=${RUN_DIR}/${model_file_name}

mkdir ${DUMP_PATH}

FLOAT_HQ_PATH=${DUMP_PATH}/$model_file_name-float.hq
QUANT_HQ_PATH=${DUMP_PATH}/$model_file_name-int8.hq
QUANT_HIR_PATH=${DUMP_PATH}/$model_file_name-int8.hir
FULL_QUANT_HQ_PATH=${DUMP_PATH}/$model_file_name-fullInt8.hq

echo "convert model to hq ..."
${HERON_CONVERTER_PATH} -i ${model_path} -o ${FLOAT_HQ_PATH} -c ./config.json
if [ $? -ne 0 ]; then
    exit
fi

echo "quantize model ..."
echo "${HERON_QUANTIZER_PATH} -i ${FLOAT_HQ_PATH} -o ${QUANT_HQ_PATH} -c ./config.json"
${HERON_QUANTIZER_PATH} -i ${FLOAT_HQ_PATH} -o ${QUANT_HQ_PATH} -c ./config.json
if [ $? -ne 0 ]; then
    exit
fi

# echo "evaluate float model accuracy ..."
# echo "${HERON_EVALUATOR_PATH} -m ${FLOAT_HQ_PATH} -c ./config.json"
# ${HERON_EVALUATOR_PATH} -m ${FLOAT_HQ_PATH} -c ./config.json
# if [ $? -ne 0 ]; then
#     exit
# fi

# echo "evaluate int8 model accuracy ..."
# ${HERON_EVALUATOR_PATH} -m ${QUANT_HQ_PATH} -c ./config.json
# if [ $? -ne 0 ]; then
#     exit
# fi

echo "export full quantize model ..."
${HERON_EXPORTER_PATH} -i ${QUANT_HQ_PATH} -o ${FULL_QUANT_HQ_PATH}
if [ $? -ne 0 ]; then
    exit
fi

echo "compile hq to hir ..."
${HERON_COMPILER_PATH} -i ${FULL_QUANT_HQ_PATH} -o ${QUANT_HIR_PATH} --dataFormat RGBA --withNetInfo 
if [ $? -ne 0 ]; then
    exit
fi
  

# echo "compare float and quant model ..."
# ${HERON_ACC_analyze} --float ${FLOAT_HQ_PATH} --fixed ${QUANT_HQ_PATH} --config config.json
# if [ $? -ne 0 ]; then
#     exit
# fi

# echo "generate float golden ..."
# ${HERON_GoldenGenerator} -m ${FLOAT_HQ_PATH} -c config.json -i /data1/guofeng/code/vnne_dev_tools/demo/car_plate_yolox.jpg
# if [ $? -ne 0 ]; then
#     exit
# fi

# echo "generate quant golden ..."
# ${HERON_GoldenGenerator} -m ${QUANT_HQ_PATH} -c config.json -i /data1/guofeng/code/vnne_dev_tools/demo/car_plate_yolox.jpg --dumpType all --saveTxt
# if [ $? -ne 0 ]; then
#     exit
# fi


echo "run profiler quant golden ..."
${HERON_profile_PATH} -m ${QUANT_HIR_PATH} 
if [ $? -ne 0 ]; then
    exit
fi