set -e
source /usr/local/Ascend/ascend-toolkit/set_env.sh
pip install -r requirements.txt
pip install -r requirements_dev.txt
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUTPUT_DIR="${PROJECT_ROOT}/output"
INTEGRATION_REPORT_DIR="${PROJECT_ROOT}/test_reports/integration_tests"
TITAN_VERSION="v0.2.2"
TITAN_DIR="${PROJECT_ROOT}/third_party/torchtitan"
TIMEOUT_SECONDS=${TIMEOUT_SECONDS:-300}
run_upstream_ut() {
echo "Running torchtitan upstream unit tests..."
if ! python3 -c "import torchtitan_npu" 2>/dev/null; then
python3 -m pip install -e .
fi
if [ ! -d "$TITAN_DIR" ]; then
echo "Cloning torchtitan source..."
mkdir -p third_party
git clone --branch $TITAN_VERSION --depth 1 \
https://gitcode.com/GitHub_Trending/to/torchtitan.git $TITAN_DIR
fi
local titan_test_dir="${TITAN_DIR}/tests/unit_tests"
local conftest_file="${titan_test_dir}/conftest.py"
if [[ ! -d "$titan_test_dir" ]]; then
echo "Torchtitan unit test directory not found: $titan_test_dir"
return 1
fi
cat > "$conftest_file" << 'EOF'
import pytest
def pytest_configure(config):
"""Import torchtitan_npu to apply NPU patches before running tests."""
import torchtitan_npu
EOF
local saved_pythonpath="$PYTHONPATH"
export PYTHONPATH="${TITAN_DIR}:${PROJECT_ROOT}:${PYTHONPATH}"
pytest_args="-v --tb=short --import-mode=importlib"
pytest_args="$pytest_args --ignore=tests/unit_tests/test_tokenizer.py"
pytest_args="$pytest_args --ignore=tests/unit_tests/test_activation_checkpoint.py"
pytest_args="$pytest_args --ignore=tests/unit_tests/test_download_hf_assets.py"
local test_target="tests/unit_tests/"
cd "${TITAN_DIR}"
echo "Running torchtitan tests from: $(pwd)"
set +e
python3 -m pytest $pytest_args $test_target
local exit_code=$?
set -e
cd "$PROJECT_ROOT"
rm -f "$conftest_file"
export PYTHONPATH="$saved_pythonpath"
if [[ $exit_code -eq 0 ]]; then
echo "Torchtitan upstream tests passed!"
elif [[ $exit_code -eq 5 ]]; then
echo "No torchtitan tests found to run."
else
echo "Torchtitan upstream tests failed (exit_code=$exit_code)"
exit $exit_code
fi
}
run_upstream_ut
pytest -v --tb=short tests/unit_tests