PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
#!/bin/bash
# This script used for weekly unit test on CUDA environment, and will not be triggered by PR or CI pipeline.
# CI related cuda UT could be found in .azure-pipelines/scripts/cuda_unit_test.
set -xe
CONDA_ENV_NAME="unittest_cuda"
PYTHON_VERSION="3.12"
TORCH_VERSION="2.15.0"
REPO_PATH=$(git rev-parse --show-toplevel)
LOG_DIR=${REPO_PATH}/ut_log_dir
SUMMARY_LOG=${LOG_DIR}/results_summary.log
rm -rf ${LOG_DIR} && mkdir -p ${LOG_DIR}
touch ${SUMMARY_LOG}
[[ -z "$CUDA_VISIBLE_DEVICES" ]] && export CUDA_VISIBLE_DEVICES=0
function create_conda_env() {
echo "-----[VAL INFO] create conda env -----"
[[ -d ${HOME}/anaconda3/bin ]] && export PATH=${HOME}/anaconda3/bin/:$PATH
[[ -d ${HOME}/miniforge3/bin ]] && export PATH=${HOME}/miniforge3/bin/:$PATH
[[ -d ${HOME}/miniconda3/bin ]] && export PATH=${HOME}/miniconda3/bin/:$PATH
# create conda env
source activate base > /dev/null 2>&1
if conda info --envs | grep -q "^$CONDA_ENV_NAME\s"; then conda remove -n ${CONDA_ENV_NAME} --all -y; fi
conda create --quiet -n ${CONDA_ENV_NAME} python=${PYTHON_VERSION} setuptools -y
source activate ${CONDA_ENV_NAME} > /dev/null 2>&1
conda install --quiet -c conda-forge git gxx=13.4 gcc=13.4 gdb sysroot_linux-64 libgcc uv -y
export LD_PRELOAD=${CONDA_PREFIX}/lib/libstdc++.so.6
# install AutoRound
cd ${REPO_PATH}
if [ -d "/proc/driver/nvidia" ]; then
export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=$(python -c "import site; print(site.getsitepackages()[0])")/nvidia/nvjitlink/lib:$LD_LIBRARY_PATH
fi
uv pip install pytest-cov pytest-timeout cmake requests ninja psutil
uv cache prune
}
function print_test_results_table() {
local log_pattern=$1
local test_type=$2
echo ""
{ printf '=%.0s' {1..120}; echo; } >> ${SUMMARY_LOG}
echo "Test Results Summary - ${test_type}" >> ${SUMMARY_LOG}
{ printf '=%.0s' {1..120}; echo; } >> ${SUMMARY_LOG}
printf "%-30s %-10s %-50s\n" "Test Case" "Result" "Log File" >> ${SUMMARY_LOG}
printf "%-30s %-10s %-50s\n" "----------" "------" "--------" >> ${SUMMARY_LOG}
local total_tests=0
local passed_tests=0
local failed_tests=0
for log_file in ${LOG_DIR}/${log_pattern}; do
if [ -f "${log_file}" ]; then
local test_name=$(basename "${log_file}" .log)
# Remove prefix to get clean test case name
test_name=${test_name#unittest_cuda_}
test_name=${test_name#unittest_cuda_vlm_}
local result="UNKNOWN"
local failure_count=$(grep -c '== FAILURES ==' "${log_file}" 2>/dev/null || echo 0)
local error_count=$(grep -c '== ERRORS ==' "${log_file}" 2>/dev/null || echo 0)
local killed_count=$(grep -c 'Killed' "${log_file}" 2>/dev/null || echo 0)
local passed_count=$(grep -c ' passed' "${log_file}" 2>/dev/null || echo 0)
if [ ${failure_count} -gt 0 ] || [ ${error_count} -gt 0 ] || [ ${killed_count} -gt 0 ]; then
result="FAILED"
failed_tests=$((failed_tests + 1))
elif [ ${passed_count} -gt 0 ]; then
result="PASSED"
passed_tests=$((passed_tests + 1))
else
result="NO_TESTS"
fi
total_tests=$((total_tests + 1))
local log_filename=$(basename "${log_file}")
printf "%-30s %-10s %-50s\n" "${test_name}" "${result}" "${log_filename}" >> ${SUMMARY_LOG}
fi
done
{ printf '=%.0s' {1..120}; echo; } >> ${SUMMARY_LOG}
printf "Total: %d, Passed: %d, Failed: %d\n" ${total_tests} ${passed_tests} ${failed_tests} >> ${SUMMARY_LOG}
{ printf '=%.0s' {1..120}; echo; } >> ${SUMMARY_LOG}
echo "" >> ${SUMMARY_LOG}
}
function run_unit_test() {
# install unit test dependencies
create_conda_env
cd ${REPO_PATH}/test
rm -rf .coverage* *.xml *.html
uv pip install torch==${TORCH_VERSION} torchvision torchao --index-url https://download.pytorch.org/whl/cu132
uv pip install llama-cpp-python --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/cu132
uv pip install 'git+https://github.com/ggml-org/llama.cpp.git@master#subdirectory=gguf-py'
uv pip install -r unit/test_cuda/requirements.txt
uv pip install -r unit/test_cuda/requirements_diffusion.txt
uv pip install -U transformers chardet timm
uv pip uninstall torch torchvision
uv pip install torch==${TORCH_VERSION} torchvision torchao --index-url https://download.pytorch.org/whl/cu132
cd ${REPO_PATH} && uv pip install . && cd ${REPO_PATH}/test
pip list > ${LOG_DIR}/ut_pip_list.txt
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
# run unit tests individually with separate logs
for test_file in $(find ./unit/test_cuda ./unit/common -type f -name "test_*.py" | grep -Ev "vlms|llmc|sglang|vllm|multiple_card" | sort); do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_${test_basename}.log
echo "Running ${test_file}..."
pytest --cov=auto_round --cov-report= --cov-append \
-p no:timeout -vs ${test_file} 2>&1 | tee ${ut_log_name}
done
[ -f .coverage ] && cp .coverage ${LOG_DIR}/.coverage.unit
# Print test results table and check for failures
if ! print_test_results_table "unittest_cuda_test_*.log" "CUDA Unit Tests"; then
echo "Some CUDA unit tests failed. Please check the individual log files for details."
fi
}
function run_unit_test_vlm() {
# install unit test dependencies
create_conda_env
cd ${REPO_PATH}/test
rm -rf .coverage* *.xml *.html
uv pip install torch==${TORCH_VERSION} torchvision --index-url https://download.pytorch.org/whl/cu132
uv pip install git+https://github.com/haotian-liu/[email protected] --no-deps
uv pip install -r unit/test_cuda/requirements_vlm.txt \
--extra-index-url https://download.pytorch.org/whl/cu132 \
--index-strategy unsafe-best-match
uv pip install -U chardet
cd ${REPO_PATH} && uv pip install . && cd ${REPO_PATH}/test
pip list > ${LOG_DIR}/vlm_ut_pip_list.txt
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
# run VLM unit tests individually with separate logs
for test_file in $(find ./unit/test_cuda -name "test*vlms.py"); do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_vlm_${test_basename}.log
echo "Running ${test_file}..."
pytest --cov=auto_round --cov-report= --cov-append \
-p no:timeout -vs ${test_file} 2>&1 | tee ${ut_log_name}
done
[ -f .coverage ] && cp .coverage ${LOG_DIR}/.coverage.vlm
# Print test results table and check for failures
if ! print_test_results_table "unittest_cuda_vlm_test*.log" "CUDA VLM Tests"; then
echo "Some CUDA VLM tests failed. Please check the individual log files for details."
fi
}
function run_unit_test_llmc() {
# install unit test dependencies
create_conda_env
cd ${REPO_PATH}/test
rm -rf .coverage* *.xml *.html
BUILD_TYPE="nightly" uv pip install -r integration/test_cuda/requirements_llmc.txt \
--extra-index-url https://download.pytorch.org/whl/cu132 \
--index-strategy unsafe-best-match
uv pip install -U chardet
cd ${REPO_PATH} && uv pip install . && cd ${REPO_PATH}/test
pip list > ${LOG_DIR}/llmc_ut_pip_list.txt
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
# run unit tests individually with separate logs
for test_file in $(find ./integration/test_cuda -name "test_llmc*.py" | sort); do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_llmc_${test_basename}.log
echo "Running ${test_file}..."
pytest --cov=auto_round --cov-report= --cov-append \
-p no:timeout -vs ${test_file} 2>&1 | tee ${ut_log_name}
done
[ -f .coverage ] && cp .coverage ${LOG_DIR}/.coverage.llmc
# Print test results table and check for failures
if ! print_test_results_table "unittest_cuda_llmc_test_*.log" "CUDA LLMC Tests"; then
echo "Some CUDA LLMC tests failed. Please check the individual log files for details."
fi
}
function run_unit_test_sglang() {
# install unit test dependencies
create_conda_env
cd ${REPO_PATH}/test
rm -rf .coverage* *.xml *.html
uv pip install -r integration/test_cuda/requirements_sglang.txt \
--prerelease=allow \
--extra-index-url https://download.pytorch.org/whl/cu130 \
--index-strategy unsafe-best-match
local flashinfer_version=$(uv pip show flashinfer-python 2>/dev/null | grep -i "^Version" | awk '{print $2}')
uv pip install flashinfer-jit-cache==${flashinfer_version} --index-url https://flashinfer.ai/whl/cu130
cd ${REPO_PATH} && uv pip install . && cd ${REPO_PATH}/test
pip list > ${LOG_DIR}/sglang_ut_pip_list.txt
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
# run unit tests individually with separate logs
for test_file in $(find ./integration/test_cuda ./e2e/test_cuda -name "test_sglang*.py" | sort); do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_sglang_${test_basename}.log
echo "Running ${test_file}..."
pytest --cov=auto_round --cov-report= --cov-append \
-p no:timeout -vs ${test_file} 2>&1 | tee ${ut_log_name}
done
[ -f .coverage ] && cp .coverage ${LOG_DIR}/.coverage.sglang
# Print test results table and check for failures
if ! print_test_results_table "unittest_cuda_sglang_test*.log" "CUDA SGLang Unit Tests"; then
echo "Some CUDA SGLang unit tests failed. Please check the individual log files for details."
fi
}
function run_unit_test_vllm() {
# install unit test dependencies
create_conda_env
unset LD_LIBRARY_PATH
cd ${REPO_PATH}/test
rm -rf .coverage* *.xml *.html
uv pip install -r integration/test_cuda/requirements_vllm.txt \
--extra-index-url https://download.pytorch.org/whl/cu130 \
--index-strategy unsafe-best-match
local flashinfer_version=$(uv pip show flashinfer-python 2>/dev/null | grep -i "^Version" | awk '{print $2}')
uv pip install flashinfer-jit-cache==${flashinfer_version} --index-url https://flashinfer.ai/whl/cu130
uv pip install -U chardet
cd ${REPO_PATH} && uv pip install . && cd ${REPO_PATH}/test
pip list > ${LOG_DIR}/vllm_ut_pip_list.txt
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
# run unit tests individually with separate logs
for test_file in $(find ./integration/test_cuda ./e2e/test_cuda -name "test_vllm*.py" | sort); do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_vllm_${test_basename}.log
echo "Running ${test_file}..."
pytest --cov=auto_round --cov-report= --cov-append \
-p no:timeout -vs ${test_file} 2>&1 | tee ${ut_log_name}
done
[ -f .coverage ] && cp .coverage ${LOG_DIR}/.coverage.vllm
# Print test results table and check for failures
if ! print_test_results_table "unittest_cuda_vllm_test*.log" "CUDA VLLM Unit Tests"; then
echo "Some CUDA VLLM unit tests failed. Please check the individual log files for details."
fi
}
function merge_coverage() {
echo "-----[VAL INFO] merging coverage data -----"
# Must run from the repo root: the first entry of "[paths] source" in
# cuda.coveragerc is the relative path "auto_round", which coverage.py
# resolves against the current directory. From ${REPO_PATH}/test it would
# resolve to a non-existent "test/auto_round" and the site-packages paths
# recorded during the runs would never be remapped back to the sources.
cd ${REPO_PATH}
rm -f .coverage
local coverage_files=$(find ${LOG_DIR} -maxdepth 1 -name ".coverage.*" 2>/dev/null)
if [ -z "${coverage_files}" ]; then
echo "No coverage data files found in ${LOG_DIR}, skipping merge."
return
fi
export COVERAGE_RCFILE=${REPO_PATH}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc
coverage combine ${coverage_files}
coverage xml -o ${LOG_DIR}/coverage_merged.xml
coverage html -d ${LOG_DIR}/htmlcov
coverage report | tee ${LOG_DIR}/coverage_summary.log
echo "Merged coverage report saved to ${LOG_DIR}/coverage_merged.xml and ${LOG_DIR}/htmlcov/index.html"
}
function main() {
run_unit_test_vllm
run_unit_test_vlm
run_unit_test_llmc
run_unit_test_sglang
run_unit_test
merge_coverage
cat ${SUMMARY_LOG}
}
main