PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
#!/bin/bash
set -e
PATTERN='[-a-zA-Z0-9_]*='
for i in "$@"; do
case $i in
--test_case=*)
test_case=$(echo $i | sed "s/${PATTERN}//")
;;
--test_part=*)
test_part=$(echo $i | sed "s/${PATTERN}//")
;;
*)
echo "Parameter $i not recognized."
exit 1
;;
esac
done
source ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/change_color.sh
# Change-based test selection helpers. REPO_DIR points the detector at the
# agent checkout instead of the container default (/auto-round).
REPO_DIR="${BUILD_SOURCESDIRECTORY}"
source ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/ut/detect_changed_tests.sh
LOG_DIR="${BUILD_SOURCESDIRECTORY}/log_dir"
mkdir -p "${LOG_DIR}"
source ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/ut/retry_failed_tests.sh
SUMMARY_LOG="${LOG_DIR}/results_summary.log"
# print_summary reads this file unconditionally; a matrix part that selects no
# test never writes it, so make sure it always exists.
touch "${SUMMARY_LOG}"
function setup_environment() {
export TZ='Asia/Shanghai'
export TQDM_MININTERVAL=120
export CUDA_VISIBLE_DEVICES=0
export HF_HUB_DISABLE_PROGRESS_BARS=1
export COVERAGE_RCFILE="${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/ut/coveragerc/cuda.coveragerc"
if [[ -n "${GITHUB_TOKEN:-}" ]]; then
export GIT_CONFIG_COUNT=1
export GIT_CONFIG_KEY_0="url.https://x-access-token:${GITHUB_TOKEN}@github.com/.insteadOf"
export GIT_CONFIG_VALUE_0="https://github.com/"
fi
}
function cleanup_test_workspace() {
local tmp_root="/test/tmp"
if [ -d "" ]; then
find "" -mindepth 1 -maxdepth 1 ! -name tiny_models -exec rm -rf {} +
fi
rm -rf "/test/ar_work_space"
rm -rf "/test/tmp_autoround"
}
trap cleanup_test_workspace EXIT
function print_summary() {
python ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/ut/print_summary.py --summary-log "${SUMMARY_LOG}"
exit $?
}
function check_storage_usage() {
echo "##[group]check storage usage..."
df -h
du -sh "${BUILD_SOURCESDIRECTORY}" || true
du -sh /root/.cache/huggingface || true
du -sh /root/.cache/huggingface/hub/* || true
du -sh /root/.venv || true
echo "##[endgroup]"
}
function setup_basic_test_env() {
echo "##[group]Setting up test environment..."
cd "${BUILD_SOURCESDIRECTORY}" || exit 1
rm -rf /root/.venv
uv venv --python=3.14 /root/.venv
uv pip install torch==2.15.0 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 test/unit/test_cuda/requirements.txt
uv pip install -r test/unit/test_cuda/requirements_diffusion.txt
uv pip install -U transformers chardet timm
uv pip install -U pytest-cov pytest-timeout
uv pip install kernels==0.17.2 # For sm120: https://github.com/huggingface/transformers/blob/v5.19.0/setup.py#L94
uv pip uninstall torch torchvision
uv pip install torch==2.15.0 torchvision torchao --index-url https://download.pytorch.org/whl/cu132
uv pip install .
echo "List dependencies ..."
uv pip list
echo "##[endgroup]"
cd "${BUILD_SOURCESDIRECTORY}/test" || exit 1
}
function run_pytest() {
local test_case=$1
local ut_log_name=$2
echo "##[group]Running ${test_case}..."
# Record the test targets so a retry can rerun exactly these cases.
printf '%s\n' ${test_case} > "${ut_log_name%.log}.list"
pytest -m "not skip_ci" --timeout=600 --cov=auto_round --cov-report= --cov-append -vs \
--junitxml="${ut_log_name%.log}.xml" ${test_case} 2>&1 | tee ${ut_log_name}
echo "##[endgroup]"
}
function run_common_group() {
# Run a group of common test files together in a single pytest invocation.
# $1: group name (used for log file), remaining args: test files
local group_name=$1
shift
local group_tests
group_tests=$(filter_changed_tests "test" "$*")
if [ -n "${group_tests}" ]; then
local ut_log_name="${LOG_DIR}/unittest_cuda_common_${group_name}.log"
run_pytest "${group_tests}" "${ut_log_name}"
fi
}
function run_common_unit_test() {
run_if_retry && return 0
# common test case for cpu/gpu/xpu
# Group cases by the first-level folder under unit/common; a single test
# file placed directly under unit/common (e.g. test_main.py) runs on its own.
for entry in $(find ./unit/common -mindepth 1 -maxdepth 1 | sort); do
if [ -d "${entry}" ]; then
local group_name=$(basename "${entry}")
run_common_group "${group_name}" "$(find "${entry}" -name "test*.py" | sort)"
elif [[ "$(basename "${entry}")" == test*.py ]]; then
local group_name=$(basename "${entry}" .py)
run_common_group "${group_name}" "${entry}"
fi
done
}
function run_unit_test() {
run_if_retry && return 0
# run ci cuda ut scope
find ./unit/test_cuda -type f -name "test_*.py" | grep -Ev "vlms|llmc|sglang|vllm|multiple_card" | sort > all_tests.txt
total_lines=$(wc -l < all_tests.txt)
NUM_CHUNKS=2
q=$(( total_lines / NUM_CHUNKS ))
r=$(( total_lines % NUM_CHUNKS ))
if [ "$test_part" -le "$r" ]; then
chunk_size=$(( q + 1 ))
start_line=$(( (test_part - 1) * chunk_size + 1 ))
else
chunk_size=$q
start_line=$(( r * (q + 1) + (test_part - r - 1) * q + 1 ))
fi
end_line=$(( start_line + chunk_size - 1 ))
selected_files=$(sed -n "${start_line},${end_line}p" all_tests.txt)
selected_files=$(filter_changed_tests "test" "${selected_files}")
if [ -z "${selected_files}" ]; then
echo "No changed CUDA unit test file in part ${test_part}, skip."
return 0
fi
for test_file in ${selected_files}; do
local test_basename=$(basename ${test_file} .py)
local ut_log_name=${LOG_DIR}/unittest_cuda_${test_basename}.log
run_pytest "${test_file}" "${ut_log_name}"
done
}
function run_unit_test_llmc() {
echo "##[group]set up UT env..."
cd "${BUILD_SOURCESDIRECTORY}" || exit 1
rm -rf /root/.venv
uv venv --python=3.14 /root/.venv
uv pip install -U pytest-cov pytest-timeout
BUILD_TYPE="nightly" uv pip install \
-r test/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
uv pip install .
uv pip list
echo "##[endgroup]"
cd "${BUILD_SOURCESDIRECTORY}/test" || exit 1
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
run_pytest "${test_file}" "${ut_log_name}"
done
}
function run_unit_test_sglang() {
echo "##[group]set up UT env..."
cd "${BUILD_SOURCESDIRECTORY}" || exit 1
rm -rf /root/.venv
uv venv --python=3.14 /root/.venv
uv pip install -U pytest-cov pytest-timeout
uv pip install -r test/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
uv pip install .
uv pip list
echo "##[endgroup]"
cd "${BUILD_SOURCESDIRECTORY}/test" || exit 1
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
run_pytest "${test_file}" "${ut_log_name}"
done
}
function run_unit_test_vllm() {
echo "##[group]set up UT env..."
cd "${BUILD_SOURCESDIRECTORY}" || exit 1
rm -rf /root/.venv
uv venv --python=3.14 /root/.venv
uv pip install -U pytest-cov pytest-timeout
uv pip install -r test/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
uv pip install .
uv pip list
echo "##[endgroup]"
cd "${BUILD_SOURCESDIRECTORY}/test" || exit 1
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
run_pytest "${test_file}" "${ut_log_name}"
done
}
function collect_log() {
touch "${SUMMARY_LOG}"
# collect_result.py also stages only the failed logs so a retry can rerun them.
python ${BUILD_SOURCESDIRECTORY}/.azure-pipelines/scripts/ut/collect_result.py \
--test-type "Unit Tests" --log-pattern "unittest_*.log" --log-dir ${LOG_DIR} \
--summary-log ${SUMMARY_LOG} --failed-logs-dir "${LOG_DIR}/failed_logs"
if [ -f .coverage ]; then
cp .coverage "${LOG_DIR}/.coverage.part${test_part}"
# Keep .coverage in the failure artifact so a retry can accumulate onto it.
if [ -d "${LOG_DIR}/failed_logs" ]; then
cp .coverage "${LOG_DIR}/failed_logs/.coverage"
fi
fi
}
function main() {
setup_environment
init_changed_tests
if [ "${test_case}" == "nightly" ]; then
run_unit_test_sglang
run_unit_test_llmc
run_unit_test_vllm
elif [ "${test_case}" == "ci" ]; then
# Mirror the selection below: tests excluded from the ci run (vlms,
# llmc, sglang, vllm, multiple_card) must not enable filtering.
scope_changed_tests "$(cd "${BUILD_SOURCESDIRECTORY}" && find test/unit/common test/unit/test_cuda -type f -name "test_*.py" | grep -Ev "vlms|llmc|sglang|vllm|multiple_card")"
setup_basic_test_env
if [ "${test_part}" == "0" ]; then
run_common_unit_test
else
run_unit_test
fi
else
echo "##[error]Invalid test case specified: ${test_case}. Please use 'nightly' or 'ci'."
exit 1
fi
collect_log
check_storage_usage
print_summary
}
main "$@"