Repository navigation
[ROCm][Windows] Restore per-config HIP optimization flags - #201482
pablo-garay wants to merge 1 commit into
Conversation
Fixes pytorch#184397. pytorch#183856 moved the Windows HIP optimization flags out of CMAKE_HIP_FLAGS_<CONFIG> and into CMAKE_HIP_FLAGS, keyed on CMAKE_BUILD_TYPE, on the premise that per-config flags never reached the HIP compile line under LoadHIP's custom CMAKE_HIP_COMPILE_OBJECT rule. That premise does not hold: the rule's <FLAGS> is filled from CMAKE_HIP_FLAGS plus CMAKE_HIP_FLAGS_<CONFIG> like any other language, and the tree just before pytorch#183856 already put -O3 -DNDEBUG on every HIP compile in build.ninja (CMake 3.31.6 through 4.4.4). The side effect of the workaround is that multi-config generators (Ninja Multi-Config) compile HIP with no -O flag at all, so at clang's -O0. The -O0 regression in pytorch#183853 came from somewhere else. With C/CXX on clang-cl, CMake treats HIP as an MSVC-frontend language: before CMake 4.3 it seeds no per-config HIP flags, and from 4.3 on it seeds MSVC ones (/O2 /Ob2 /DNDEBUG) that clang++ rejects. pytorch#180485 relied on those defaults, so HIP built at -O0; pytorch#183365 stripped the MSVC flags and appended GNU ones, which had already fixed it. The regression was bisected and measured on builds that predate pytorch#183365. This drops the CMAKE_BUILD_TYPE injection and, after the existing strip, gives each standard config the GNU default (-O0 -g, -O3 -DNDEBUG, -Os -DNDEBUG, -O2 -g -DNDEBUG) in CMAKE_HIP_FLAGS_<CONFIG> unless it already sets an -O level. That keeps user overrides and is idempotent when LoadHIP.cmake is included a second time (aten/CMakeLists.txt). Visual Studio generators do not support HIP at all, so Ninja, Ninja Multi-Config and Makefiles are the only generators this affects. Test Plan Windows 11, TheRock rocm-sdk 7.14.0a20260519, gfx1151, SDK clang-cl as CC/CXX, cmake 4.4.4: ``` python tools/amd_build/build_amd.py python -m pip install -e . -v --no-build-isolation ``` build/build.ninja: all 262 HIP compile edges have -O3 -DNDEBUG after the CMAKE_HIP_FLAGS part of FLAGS and none have it injected at the front. A standalone CMake project that uses LoadHIP's setup was also checked on CMake 3.31.6, 4.2.3, 4.3.2 and 4.4.4: Ninja Debug, Release, RelWithDebInfo and MinSizeRel each get their config's flags, Ninja Multi-Config gets per-config flags in every impl-<Config>.ninja (main's code gives none), and -DCMAKE_HIP_FLAGS_RELEASE="-O1 ..." is kept. conv2d fp16 (1x3x1024x1024, k7, 64 out channels) and the native normal_/add/sum/softmax kernels run at parity with the TheRock torch 2.12 wheel on gfx1151 (conv2d 2458-2907 vs 2437-2517 GFLOP/s). Authored with Claude.
|
@pytorchbot merge |
Merge failedReason: This PR needs a If not, please add the To add a label, you can comment to pytorchbot, for example For more information, see Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Merge failedReason: 2 jobs have failed, first few of them are: Auto Request Review, trunk / macos-py3-arm64 / test (mps, 1, 1, macos-m2-15) Details for Dev Infra teamRaised by workflow job |
|
@pytorchbot merge -i |
Linked issue or supporting maintainer
Fixes #184397
Supported by @jeffdaily
Related: #183856 (the workaround this replaces), #183853 and ROCm/TheRock#5157 (the original -O0 regression).
BC-breaking?
No.
Summary (human written only)
Replaces the CMAKE_BUILD_TYPE-gated HIP flag injection on Windows with per-config defaults, so multi-config generators get optimized HIP code too.
Agent details (if applicable)
Problem: #183856 injects the HIP optimization flags into
CMAKE_HIP_FLAGSbased onCMAKE_BUILD_TYPE, because per-config flags were believed not to reach the HIP compile line under LoadHIP's customCMAKE_HIP_COMPILE_OBJECT. As a result, Ninja Multi-Config compiles HIP with no-Oflag at all, in every configuration.Cause: that belief was wrong. The rule's
<FLAGS>includesCMAKE_HIP_FLAGS_<CONFIG>as for any language; the tree just before #183856 already had-O3 -DNDEBUGon every HIP compile in build.ninja (CMake 3.31.6 through 4.4.4). The -O0 regression came from CMake treating HIP as an MSVC-frontend language when C/CXX use clang-cl: no per-config HIP defaults before CMake 4.3, MSVC ones that clang++ rejects from 4.3 on. #183365 had already fixed that; the regression was measured only on builds predating it.Change: drop the
CMAKE_BUILD_TYPEinjection and, after the existing strip, set each standard config's GNU default (-O0 -g,-O3 -DNDEBUG,-Os -DNDEBUG,-O2 -g -DNDEBUG) inCMAKE_HIP_FLAGS_<CONFIG>unless it already has an-Olevel. This keeps user overrides and is idempotent for the secondinclude(LoadHIP)inaten/CMakeLists.txt.Test plan, done with Claude on Windows 11, TheRock rocm-sdk 7.14, gfx1151, SDK clang-cl, cmake 4.4.4:
All 262 HIP compile edges in build/build.ninja get
-O3 -DNDEBUGin the per-config position. A standalone project with LoadHIP's setup on CMake 3.31.6, 4.2.3, 4.3.2 and 4.4.4: Ninja Debug/Release/RelWithDebInfo/MinSizeRel each get their config's flags, Ninja Multi-Config gets per-config flags (main gives none), and-DCMAKE_HIP_FLAGS_RELEASE=-O1 ...is kept. conv2d fp16 (1x3x1024x1024, k7, 64 out channels) runs at parity with the TheRock 2.12 wheel on gfx1151 (2458-2907 vs 2437-2517 GFLOP/s).cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang