Repository navigation
[ROCm][Windows] Apply per-config HIP optimization flags via CMAKE_HIP_FLAGS - #183856
adityas-amd wants to merge 2 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/183856
Note: Links to docs will display an error until the docs builds have been completed. ❗ 1 Active SEVsThere are 1 currently active SEVs. If your PR is affected, please view them below: ⏳ No Failures, 10 PendingAs of commit 306a71d with merge base cd1cd1d ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
|
|
Just for clarification for the pytorch team, the diff at this time, which will be shown below, did not fix the regression. diff --git a/cmake/Dependencies.cmake b/cmake/Dependencies.cmake
index 4240dcf059faa..f32504ce7b8d5 100644
--- a/cmake/Dependencies.cmake
+++ b/cmake/Dependencies.cmake
@@ -1063,6 +1063,14 @@ if(USE_ROCM)
string(APPEND CMAKE_HIP_FLAGS " -fclang-abi-compat=17")
# Use host linker instead of device linker (no relocatable device code by default)
string(APPEND CMAKE_HIP_FLAGS " -fno-gpu-rdc")
+ # Explicitly pass --offload-arch for every PYTORCH_ROCM_ARCH. CMake's native
+ # HIP support normally injects these from each target's HIP_ARCHITECTURES
+ # property, but the custom CMAKE_HIP_COMPILE_OBJECT rule for Windows in
+ # cmake/public/LoadHIP.cmake omits the architecture-flag placeholder,
+ # dropping per-target arch entirely and producing generic GFX bytecode.
+ foreach(pytorch_rocm_arch ${PYTORCH_ROCM_ARCH})
+ string(APPEND CMAKE_HIP_FLAGS " --offload-arch=${pytorch_rocm_arch}")
+ endforeach()
set(Caffe2_HIP_INCLUDE
$<INSTALL_INTERFACE:include> ${Caffe2_HIP_INCLUDE}) |
PR pytorch#180485 migrated the Windows HIP build from hip_add_library/hipcc to CMake's native HIP language with clang++ direct. The custom CMAKE_HIP_COMPILE_OBJECT rule installed in cmake/public/LoadHIP.cmake expands <FLAGS>, but the per-config CMAKE_HIP_FLAGS_RELEASE values do not reliably reach <FLAGS> for the HIP language, so HIP code silently fell back to clang++'s -O0 default. Every kernel in torch_hip.dll ended up unoptimized: 100% scratch spillage in the ISA, and the AMD runtime emits scratch ranges on every dispatch (palvirtual.hpp: scratch = ptr:[...] size:[90177536]). Result: ~99% conv2d FP16 perf regression on gfx1150 (2715 -> 16 GFLOP/s, bisected and validated at ROCm/TheRock#5157). Fix: inject -O3 directly into the compile rule template so it is guaranteed regardless of how <FLAGS> expands. Validation on gfx1150 / Radeon 890M (validate_torch_vroom.py): 5/5 nightly (pre-pytorch#180485): 2715 GFLOP/s 5/9 nightly (regressed): 16 GFLOP/s 5/9 + this patch: 2515 GFLOP/s The compile-rule location was identified and validated by @astrelsky on ROCm/TheRock#5157. Authored with assistance from Claude.
2e68d10 to
5ab93a4
Compare
|
@astrelsky @jeffdaily I have updated the PR description to reflect as per the root cause identified by me and @astrelsky. |
|
I re-requested my own review. I need to think on this more. This papers over the problem instead of understanding why it happened. |
It definitely should be done automatically depending on how the cmake build type variable is set. cmake is supposed to do this automatically, so it should be in However, I'm not convinced this really matters at all at the moment because this is device code which currently cannot be debugged or profiled on Windows, at all. |
…_FLAGS PR pytorch#180485 migrated the Windows HIP build to native enable_language(HIP). PR pytorch#183365 stripped MSVC-style defaults (/DWIN32 /D_WINDOWS /O2 /Ob2 /DNDEBUG) out of CMAKE_HIP_FLAGS{,_<CONFIG>} so clang++ would stop warn-ignoring them, and tried to reinstate GNU equivalents by appending to CMAKE_HIP_FLAGS_RELEASE, CMAKE_HIP_FLAGS_DEBUG, etc. The reinstatement step does not work on this code path. The generated HIP compile rule expands <FLAGS> from CMAKE_HIP_FLAGS, but the per-config CMAKE_HIP_FLAGS_<CONFIG> variables do not reach <FLAGS> (verified by inspecting build.ninja: CMAKE_HIP_FLAGS contents like --offload-compress and --offload-arch appear, but the -O3 -DNDEBUG appended to CMAKE_HIP_FLAGS_RELEASE does not). That is the actual cause of the "silent -O0 fallback" reported in TheRock#5157 / pytorch#183853: with the MSVC /O2 stripped and the GNU -O3 unreachable, HIP code was effectively compiled at -O0. The previous version of this PR hardcoded -O3 in the CMAKE_HIP_COMPILE_OBJECT rule template. That restores Release perf but the -O3 sits outside the per-config plumbing, so it also leaks into Debug, RelWithDebInfo, and MinSizeRel builds. Inject the per-config optimization flags into CMAKE_HIP_FLAGS instead, gated on CMAKE_BUILD_TYPE. CMAKE_HIP_FLAGS does reach <FLAGS> in the generated rule, so this lands -O3/-O2/-O0/-Os (matching the build type) on the compile line for single-config generators (Ninja, Makefiles) without contaminating other configs. Drop the now-dead string(APPEND CMAKE_HIP_FLAGS_<CONFIG>) lines and the -O3 hack in the compile rule. Validation on gfx1151 / Radeon AI MAX 395 with CMake 4.3.2: Release shows -O3 -DNDEBUG in build.ninja FLAGS and Sleep.hip compiles cleanly with optimized ISA; Debug shows -O0 -g. Co-investigated with @astrelsky and @adityas-amd on TheRock#5157. Authored with assistance from Claude.
astrelsky
left a comment
There was a problem hiding this comment.
I guess that works and future proofs it for when there is debugging support on Windows.
I only bothered saying this because of my previous comment.
adityas-amd
left a comment
There was a problem hiding this comment.
Looks good to me..
|
@pytorchbot merge -f "no further CI needed, builds passing, this was ROCm WIN32-only change and no upstream CI covers it" |
Merge startedYour change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
|
@claude help review this change, and explain why this approach is wrong of some CMake generators (but OK for CI) and where the actual bug probably is |
|
Claude finished @malfet's task in 1m 37s —— View job Review of PR #183856 — Per-config HIP optimization flags
Why This Approach Is Wrong for Multi-Config GeneratorsThe fix (lines 145–157) does this: if(NOT CMAKE_CONFIGURATION_TYPES)
string(TOUPPER "${CMAKE_BUILD_TYPE}" _hip_cfg)
if(_hip_cfg STREQUAL "DEBUG")
string(APPEND CMAKE_HIP_FLAGS " -O0 -g")
elseif(...)
...
endif()
endif()This is correct for single-config generators (Ninja, Unix Makefiles) where That said, for the current CI (which uses single-config Ninja), this is fine. The guard is also technically correct — baking a single config's flags into Where The Actual Bug IsThe root cause is in the custom compile rule at line 166–167: set(CMAKE_HIP_COMPILE_OBJECT
"<CMAKE_HIP_COMPILER> <DEFINES> <INCLUDES> <FLAGS> -o <OBJECT> -x hip -c <SOURCE>")CMake's rule expansion for
It does not include The normal CMake pipeline for compile rules works like this:
But when you override The proper fix would be to explicitly include In practice, the correct fix for the custom rule would be something like: set(CMAKE_HIP_COMPILE_OBJECT
"<CMAKE_HIP_COMPILER> <DEFINES> <INCLUDES> <FLAGS> ${CMAKE_HIP_FLAGS_${_hip_cfg_upper}} -o <OBJECT> -x hip -c <SOURCE>")...but that's still single-config (the variable is evaluated at configure time). The real solution is to not override Summary
The approach is a reasonable hotfix for the immediate regression (justified, since ROCm Windows CI only uses Ninja). A proper fix would either (a) make the custom compile rule incorporate per-config flags properly, or (b) eliminate the need for a custom compile rule entirely by teaching CMake to emit GNU-style rules for HIP on Windows through the correct platform variables. |
It may be a good idea to track this in an issue, so that a proper fix eventually lands somewhere, even if it's within the cmake internals. It looks like the Claude bot observes the mention when using a quote reply. Claude might want to fix that. |
|
Opened #184397 to track the proper fix per @astrelsky's suggestion. The merged change is a single-config hotfix; the issue captures the root cause (custom |
…_FLAGS (pytorch#183856) ## Issue Fixes ROCm/TheRock#5157. Fixes pytorch#183853. Windows ROCm HIP code was effectively compiled at `-O0` after pytorch#180485 + pytorch#183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157). ## Diagnosis After pytorch#183365 stripped the MSVC-style `/O2 /Ob2 /DNDEBUG` out of `CMAKE_HIP_FLAGS_RELEASE` and re-appended GNU `-O3 -DNDEBUG`, the actual build command still came up unoptimized. Inspecting the generated `build.ninja` shows why: - `CMAKE_HIP_FLAGS` contents (e.g. `--offload-compress -std=c++20 --offload-arch=...`) **do** reach the `$FLAGS` ninja variable used by every HIP compile. - `CMAKE_HIP_FLAGS_<CONFIG>` contents (e.g. `-O3 -DNDEBUG`) **do not**. That's the actual cause of the `-O0` fallback — `CMAKE_HIP_FLAGS_RELEASE` is dead code on the Windows HIP compile path. ## Fix - Drop the dead `string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)` lines. - Inject the appropriate `-O{3,2,0,s}` plus `-DNDEBUG`/`-g` directly into `CMAKE_HIP_FLAGS`, gated on `CMAKE_BUILD_TYPE`. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses. - Drop the `-O3` from the compile rule template so non-Release configs aren't tainted. Pull Request resolved: pytorch#183856 Approved by: https://github.com/jeffdaily, https://github.com/astrelsky Co-authored-by: Jeff Daily <[email protected]>
…_FLAGS (pytorch#183856) ## Issue Fixes ROCm/TheRock#5157. Fixes pytorch#183853. Windows ROCm HIP code was effectively compiled at `-O0` after pytorch#180485 + pytorch#183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157). ## Diagnosis After pytorch#183365 stripped the MSVC-style `/O2 /Ob2 /DNDEBUG` out of `CMAKE_HIP_FLAGS_RELEASE` and re-appended GNU `-O3 -DNDEBUG`, the actual build command still came up unoptimized. Inspecting the generated `build.ninja` shows why: - `CMAKE_HIP_FLAGS` contents (e.g. `--offload-compress -std=c++20 --offload-arch=...`) **do** reach the `$FLAGS` ninja variable used by every HIP compile. - `CMAKE_HIP_FLAGS_<CONFIG>` contents (e.g. `-O3 -DNDEBUG`) **do not**. That's the actual cause of the `-O0` fallback — `CMAKE_HIP_FLAGS_RELEASE` is dead code on the Windows HIP compile path. ## Fix - Drop the dead `string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)` lines. - Inject the appropriate `-O{3,2,0,s}` plus `-DNDEBUG`/`-g` directly into `CMAKE_HIP_FLAGS`, gated on `CMAKE_BUILD_TYPE`. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses. - Drop the `-O3` from the compile rule template so non-Release configs aren't tainted. Pull Request resolved: pytorch#183856 Approved by: https://github.com/jeffdaily, https://github.com/astrelsky Co-authored-by: Jeff Daily <[email protected]>
## 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_FLAGS` based on `CMAKE_BUILD_TYPE`, because per-config flags were believed not to reach the HIP compile line under LoadHIP's custom `CMAKE_HIP_COMPILE_OBJECT`. As a result, Ninja Multi-Config compiles HIP with no `-O` flag at all, in every configuration. Cause: that belief was wrong. The rule's `<FLAGS>` includes `CMAKE_HIP_FLAGS_<CONFIG>` as for any language; the tree just before #183856 already had `-O3 -DNDEBUG` on 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_TYPE` injection and, after the existing strip, set each standard config's GNU default (`-O0 -g`, `-O3 -DNDEBUG`, `-Os -DNDEBUG`, `-O2 -g -DNDEBUG`) in `CMAKE_HIP_FLAGS_<CONFIG>` unless it already has an `-O` level. This keeps user overrides and is idempotent for the second `include(LoadHIP)` in `aten/CMakeLists.txt`. Test plan, done with Claude on Windows 11, TheRock rocm-sdk 7.14, gfx1151, SDK clang-cl, cmake 4.4.4: ``` python tools/amd_build/build_amd.py python -m pip install -e . -v --no-build-isolation ``` All 262 HIP compile edges in build/build.ninja get `-O3 -DNDEBUG` in 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). Pull Request resolved: #201482 Approved by: https://github.com/jeffdaily Co-authored-by: Jeff Daily <[email protected]>
Issue
Fixes ROCm/TheRock#5157.
Fixes #183853.
Windows ROCm HIP code was effectively compiled at
-O0after #180485 + #183365 landed, causing a ~99% conv2d FP16 perf regression on gfx1150 (bisected and validated by @astrelsky and @adityas-amd at ROCm/TheRock#5157).Diagnosis
After #183365 stripped the MSVC-style
/O2 /Ob2 /DNDEBUGout ofCMAKE_HIP_FLAGS_RELEASEand re-appended GNU-O3 -DNDEBUG, the actual build command still came up unoptimized. Inspecting the generatedbuild.ninjashows why:CMAKE_HIP_FLAGScontents (e.g.--offload-compress -std=c++20 --offload-arch=...) do reach the$FLAGSninja variable used by every HIP compile.CMAKE_HIP_FLAGS_<CONFIG>contents (e.g.-O3 -DNDEBUG) do not.That's the actual cause of the
-O0fallback —CMAKE_HIP_FLAGS_RELEASEis dead code on the Windows HIP compile path.Fix
string(APPEND CMAKE_HIP_FLAGS_<CONFIG> ...)lines.-O{3,2,0,s}plus-DNDEBUG/-gdirectly intoCMAKE_HIP_FLAGS, gated onCMAKE_BUILD_TYPE. This works for single-config generators (Ninja, Makefiles), which is what the Windows ROCm build uses.-O3from the compile rule template so non-Release configs aren't tainted.cc @jeffdaily @sunway513 @jithunnair-amd @pruthvistony @ROCmSupport @jataylo @hongxiayang @naromero77amd @pragupta @jerrymannil @xinyazhang @adityas-amd @astrelsky