@@ -1250,6 +1250,97 @@ test_custom_script_ops() {
12501250 assert_git_not_dirty
12511251}
12521252
1253+ test_libtorch_agnostic_targetting () {
1254+ echo " Testing libtorch_agnostic runs correctly on TORCH_TARGET_VERSION"
1255+
1256+ REPO_DIR=$( pwd)
1257+ WHEEL_DIR=" ${REPO_DIR} /test/cpp_extensions/.wheels"
1258+
1259+ # Build wheel with current PyTorch (this has TORCH_TARGET_VERSION 2_9_0)
1260+ echo " Building 2.9 extension wheel with current PyTorch..."
1261+ pushd test/cpp_extensions/libtorch_agnostic_2_9_extension
1262+ time python setup.py bdist_wheel
1263+
1264+ # Save the wheel
1265+ mkdir -p " $WHEEL_DIR "
1266+ cp dist/* .whl " $WHEEL_DIR /"
1267+ WHEEL_FILE=$( find " $WHEEL_DIR " -maxdepth 1 -name " *.whl" -type f | head -1)
1268+ echo " Built wheel: $( basename " $WHEEL_FILE " ) "
1269+ popd
1270+
1271+ # Create venv and install PyTorch 2.9
1272+ python -m venv venv_pytorch_2_9
1273+ # shellcheck disable=SC1091
1274+ . venv_pytorch_2_9/bin/activate
1275+
1276+ # Clear PYTHONPATH to avoid using the development PyTorch
1277+ echo " Clearing PYTHONPATH to use only venv packages..."
1278+ unset PYTHONPATH
1279+
1280+ # Upgrade pip to latest version
1281+ echo " Upgrading pip to latest version..."
1282+ pip install --upgrade pip
1283+ pip --version
1284+
1285+ echo " Installing PyTorch 2.9..."
1286+
1287+ # Install from release channel only
1288+ PYTORCH_VERSION=" 2.9.0"
1289+
1290+ # Extract CUDA version from BUILD_ENVIRONMENT (e.g., "cuda12.1" -> "cu121")
1291+ if [[ " $BUILD_ENVIRONMENT " =~ cuda([0-9]+)\. ([0-9]+) ]]; then
1292+ CUDA_MAJOR=" ${BASH_REMATCH[1]} "
1293+ CUDA_MINOR=" ${BASH_REMATCH[2]} "
1294+ CUDA_VERSION=" cu${CUDA_MAJOR}${CUDA_MINOR} "
1295+ echo " Detected CUDA ${CUDA_MAJOR} .${CUDA_MINOR} from BUILD_ENVIRONMENT, using ${CUDA_VERSION} "
1296+ else
1297+ # Default to CPU build
1298+ CUDA_VERSION=" cpu"
1299+ echo " No CUDA detected in BUILD_ENVIRONMENT, using CPU build"
1300+ fi
1301+
1302+ if pip install torch==" ${PYTORCH_VERSION} " --index-url https://download.pytorch.org/whl/${CUDA_VERSION} /; then
1303+ echo " Installed PyTorch ${PYTORCH_VERSION} from release channel (${CUDA_VERSION} )"
1304+ else
1305+ echo " FAILED to install PyTorch 2.9.0 from release channel"
1306+ echo " URL: https://download.pytorch.org/whl/${CUDA_VERSION} /"
1307+ deactivate
1308+ rm -rf venv_pytorch_2_9
1309+ return 1
1310+ fi
1311+
1312+ INSTALLED_VERSION=$( python -c " import torch; print(torch.__version__)" 2> /dev/null || echo " unknown" )
1313+ echo " Installed version: $INSTALLED_VERSION "
1314+
1315+ # Install test dependencies
1316+ echo " Installing test dependencies..."
1317+ pip install expecttest numpy unittest-xml-reporting
1318+
1319+ # Install the pre-built wheel
1320+ echo " "
1321+ echo " Installing pre-built 2.9 extension wheel (built with PyTorch 2.10)..."
1322+ pip install " $WHEEL_FILE "
1323+ echo " Installed $( basename " $WHEEL_FILE " ) into PyTorch 2.9 environment"
1324+
1325+ # Run tests with PyTorch 2.9 runtime (2.10 tests will be skipped automatically)
1326+ echo " "
1327+ echo " Running tests with PyTorch 2.9 runtime (using wheel built on PyTorch 2.10)..."
1328+ if time python test/cpp_extensions/test_libtorch_agnostic.py -v; then
1329+ echo " "
1330+ echo " Wheel built with current torch and TORCH_TARGET_VERSION 2_9_0 works with PyTorch 2.9 runtime!"
1331+ else
1332+ echo " targeting test failed"
1333+ deactivate
1334+ rm -rf venv_pytorch_2_9 " $WHEEL_DIR "
1335+ return 1
1336+ fi
1337+
1338+ deactivate
1339+ rm -rf venv_pytorch_2_9 " $WHEEL_DIR "
1340+
1341+ assert_git_not_dirty
1342+ }
1343+
12531344test_jit_hooks () {
12541345 echo " Testing jit hooks in cpp"
12551346 HOOK_BUILD=" ${CUSTOM_TEST_ARTIFACT_BUILD_DIR} /jit-hook-build"
@@ -1722,6 +1813,8 @@ elif [[ "${BUILD_ENVIRONMENT}" == *aarch64* && "${TEST_CONFIG}" == 'default' ]];
17221813elif [[ " ${TEST_CONFIG} " == * backward* ]]; then
17231814 test_forward_backward_compatibility
17241815 # Do NOT add tests after bc check tests, see its comment.
1816+ elif [[ " ${TEST_CONFIG} " == * libtorch_agnostic_targetting* ]]; then
1817+ test_libtorch_agnostic_targetting
17251818elif [[ " ${TEST_CONFIG} " == * xla* ]]; then
17261819 install_torchvision
17271820 build_xla
0 commit comments