Skip to content

[CK] [JIT] Add basic HIP/CK JIT kernel support in Windows - #4306

Closed
menglcai-amd wants to merge 3 commits into
ROCm:mainfrom
menglcai-amd:menglcai/update_windows_support
Closed

menglcai-amd wants to merge 3 commits into
ROCm:mainfrom
menglcai-amd:menglcai/update_windows_support

Conversation

@menglcai-amd

@menglcai-amd menglcai-amd commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Enable aiter to build and run CK/HIP JIT kernels on Windows using the TheRock ROCm SDK package. Previously Windows was forced into Triton-only mode via a hardcoded AITER_TRITON_ONLY=True; this change removes that restriction and adds the necessary platform-specific build glue.

Technical Details

  1. Build system (cpp_extension.py):

    • Use .pyd/.exe/.dll extensions on Windows instead of .so
    • get_hip_version(): fall back to reading .hipVersion from the SDK when rocm_sdk_core is absent in pip build-isolated environments
    • Exclude -fPIC from COMMON_HIP_FLAGS and cflags on Windows (rejected by the MSVC target clang with a hard error)
    • get_cxx_compiler(): use hipcc.exe on Windows; update _accepted_compilers_for_platform() to accept hipcc.exe/clang++.exe
    • get_compiler_abi_compatibility_and_version(): skip the GCC ABI version check on Windows (irrelevant for clang; also left version undefined)
    • _prepare_ldflags(): link Python import lib (-L libs/) explicitly on Windows (Linux resolves it at runtime); guard ELF-only linker flags; normalise lib paths with os.sep (no-op on Linux)
    • _get_rocm_arch_flags(): use _detect_native() on Windows instead of torch._C._cuda_getArchFlags(), which returns all 24 supported archs and would generate 24 duplicate --offload-arch flags after replacement
    • Fix Python include path scheme (posix_prefix gives wrong path on Windows)
    • Add _ninja_escape(): escape Windows drive-letter colons (C: → C$:) in ninja build rule paths; apply to all source/object/link paths
    • _write_ninja_file(): derive nvcc path via executable_path("hipcc") instead of hardcoding <ROCM_HOME>/bin/hipcc
  2. GPU detection (chip_info.py):

    • Add _resolve_gpu_info_tool(): abstracts rocminfo (Linux) vs hipInfo.exe (Windows) tool selection and output regex
    • Update _detect_native() and get_cu_num_custom_op() to use it
  3. MHA kernels (mha.py):

    • Append _ck_targets_flag() to all blob_gen_cmd entries so non-gfx9 targets pass the correct --targets flag to CK's generate.py
    • Fix "-DONLY_FAV3=0" flag (was incorrectly wrapped in extra quotes, causing clang to treat it as a filename)
    • Add module-level _MHA_LOGITS_BLOB_FILTER: on Windows, logits (softcap) blob variants fail to compile because the CK pipeline static_assert requires CK_TILE_FMHA_FWD_FAST_EXP2=1 which defaults to 0; exclude these variants until the root cause is resolved upstream
  4. Packaging (setup.py / pyproject.toml):

    • Exclude flydsl from build and install requirements on Windows
  5. Other:

    • Add *.pyd to .gitignore
    • Guard torch.distributed Backend/ProcessGroup import with try/except in parallel_state.py
    • rm_module / build_module in core.py: use .pyd on Windows, .so on Linux

Test Plan

Run all tests under op_tests/ on Windows 11 / gfx1151 with TheRock ROCm 7.15.0a20260718:

# activate venv first
python op_tests/<test_name>.py
......

Test Result

Test Device : AMD RYZEN AI MAX+ 395 w
Python 3.12.10 | PyTorch 2.12.0+rocm7.15.0a20260718 | TheRock ROCm 7.15.0a20260718

14 PASS / 17 SKIP / 1 PASS* / 51 FAIL / 20 TIMEOUT (103 tests total, Windows)

✅ PASS (14) — correctness verified

Test Pass Evidence
test_aiter_sigmoid checkAllclose atol=0.01 rtol=0.01 passed
test_causal_conv1d_prefill_split_qkv completed with no error
test_causal_conv1d_update completed with no error
test_gemm_a8w8_bpreshuffle_pad_k completed with no error
test_groupnorm is_equal=True
test_jit_arch_guard ALL_PASS
test_layernorm2d checkAllclose atol=0.03 rtol=0.01 passed
test_layernorm2dFusedAddQuant checkAllclose atol=0.01 rtol=0.01 passed
test_mha Output max diff: 0.00390625 (within tolerance)
test_mla_decode_gate all test cases skipped by pytest (rc=0)
test_moe_local_expert_ids completed with no error
test_moeTopkSoftmax checkAllclose atol=0.01 rtol=0.01 passed
test_pretune 84 passed, 0 failed
test_rmsnorm2dFusedAddQuant checkAllclose passed
test_sample completed with no error
test_topk_row_prefill results saved to prefill_topk.csv

⚠️ PASS rc=0 but all_close=False (1)

Test Detail
test_split_gdr_update all_close=False, output_diff=0.301, state_diff=0.723; test does not assert on correctness so rc=0

⏭️ SKIP (17) — gfx1151 not in supported arch list; test exited rc=0

Test Skip Reason
test_f4gemm F4GEMM unsupported on gfx1151
test_flydsl_grouped_gemm_gfx1250 requires gfx1250
test_flydsl_pa_mqa_logits_fp4_prefill kernel only supports gfx950
test_fmha_fwd_with_sink_asm unsupported on gfx1151
test_fmha_fwd_with_sink_varlen_asm unsupported on gfx1151
test_fused_qk_norm_rope_1way_perhead FP8 tests skipping on gfx1151
test_gemm_a8w8_blockscale_cktile_aq_rowmajor AQRowMajor only supported on gfx950
test_mha_flydsl_varlen requires gfx1250
test_mla_decode_pagesize64 unsupported on gfx1151
test_mla_v4_kargpreld unsupported on gfx1151
test_mla_v4_nm shipped only for gfx950
test_moe_sorting moe_sorting unsupported on gfx1151
test_mxfp8fp4gemm mxfp8fp4 gemm unsupported on gfx1151
test_opus_a16w16_gemm requires gfx950/gfx942/gfx1250
test_pa_decode_bf16_asm requires gfx1250
test_pa_sparse_prefill_opus requires gfx950
test_split_gdr_update (rc=0 but failed numerically — see below) —

❌ FAIL (51)

Test Failure Reason
test_activation JIT build failed — hip_bfloat16 → __half conversion error in binary_operator.cuh
test_aiter_add JIT build failed — same as above
test_aiter_addInp JIT build failed — same as above
test_batched_gemm_a8w8 JIT build failed
test_batched_gemm_bf16 JIT build failed
test_concat_cache_mla kv cache data type is not supported in cache_kernels.cu
test_deepgemm JIT build failed
test_flydsl_compress_attn ModuleNotFoundError: No module named 'flydsl'
test_flydsl_pa_mqa_logits_fp4 ImportError: cannot import from flydsl
test_flydsl_qk_norm_rope_quant ImportError: cannot import from flydsl
test_fmha_fwd_mxfp8_asm FP8 dtype handling error
test_fused_kv_norm_rope_group_quant JIT build failed
test_fused_qknorm_idxrqknorm JIT build failed
test_fused_qk_norm JIT build failed
test_fused_qk_norm_mrope_cache_quant JIT build failed
test_fused_qk_norm_rope_2way_perhead JIT build failed
test_fused_qk_norm_rope_cache_quant JIT build failed
test_fused_qk_norm_rope_group_quant FP8 dtype handling error
test_fused_qk_rmsnorm_group_quant JIT build failed
test_gated_rmsnorm_fp8_quant FP8 dtype handling error
test_gemm_a16w16 FileNotFoundError: module_gemm_common.so — ctypes standalone module uses .so; Windows needs .pyd
test_gemm_a4w4 JIT build failed
test_gemm_a8w8 OSError: Invalid argument — multi-path config file path issue on Windows
test_gemm_a8w8_blockscale ValueError: Unsupported FP8 dtype
test_gemm_codegen FileNotFoundError: module_gemm_common.so — same as test_gemm_a16w16
test_indexer_k_quant_and_cache torch.finfo() type error
test_kvcache FileNotFoundError: module_cache.so — ctypes standalone module .so path issue
test_kvcache_blockscale Unsupported kv cache dtype
test_metadata AssertionError: parallel MLA metadata diverged
test_mha_varlen ModuleNotFoundError: No module named 'flydsl'
test_mhc timed out (300s) even after tabulate installed
test_mla ModuleNotFoundError: No module named 'flydsl'
test_mla_prefill_ps ModuleNotFoundError: No module named 'flydsl'
test_mla_reduce FileNotFoundError: module_X.so — ctypes standalone module .so path issue
test_mla_stage2_merge AttributeError: host_time_sum
test_mla_v40_persistent JIT build failed
test_moe_2stage OSError: Invalid argument — multi-path config file path issue on Windows
test_moe_topk_gating JIT build failed
test_pa AssertionError: GPU arch [''] invalid — gfx1151 not in paged attention supported arch list
test_pa_block_id_truncation FileNotFoundError: module_attention_asm.so — ctypes standalone module .so path issue
test_pa_mqa_logits_offset JIT build failed
test_pa_mtp JIT build failed
test_pa_ragged AssertionError: GPU arch [''] invalid — same as test_pa
test_pa_ragged_experimental AssertionError: GPU arch [''] invalid — same as test_pa
test_pa_v1 AssertionError: GPU arch [''] invalid — same as test_pa
test_quant JIT build failed
test_quant_mxfp4 JIT build failed
test_rmsnorm2d FileNotFoundError: module_X.so — ctypes standalone module .so path issue
test_sampling ModuleNotFoundError: No module named 'scipy'
test_smoothquant JIT build failed
test_topk_plain JIT build failed

⏱️ TIMEOUT (20)

Test Note
test_batch_prefill Timed out after 600s
test_dsv4_rotate_quant Timed out after 600s
test_fused_qk_rmsnorm_per_token_quant Timed out after 600s
test_gated_delta_rule Timed out after 600s
test_jit_dir_with_enum Timed out after 600s
test_mha_fp8 Timed out after 600s
test_mha_varlen_fp8 Timed out after 600s
test_mhc Timed out after 300s (tabulate installed, but test did not complete)
test_mla_persistent Timed out after 600s
test_mla_persistent_round_robin Timed out after 600s
test_mla_sparse Timed out after 600s
test_moe Timed out after 600s
test_moe_blockscale Timed out after 600s
test_moe_dp_share_expert Timed out after 600s
test_moe_ep Timed out after 600s
test_moe_sorting_mxfp4 Timed out after 600s
test_moe_tkw1 Timed out after 600s
test_pa_ps Timed out after 600s
test_rope Timed out after 600s
test_topk_per_row Timed out after 600s

Submission Checklist

@menglcai-amd
menglcai-amd requested a review from a team July 20, 2026 19:57
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 4306 --add-label <label>

@zufayu
zufayu requested a review from valarLip July 21, 2026 01:40
@menglcai-amd
menglcai-amd force-pushed the menglcai/update_windows_support branch from 36b7eff to 8d44d07 Compare July 28, 2026 05:52
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 7, 2026
@zufayu
zufayu self-requested a review October 8, 2026 07:19
@zufayu

zufayu commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

@menglcai — apologies for the long silence on this, and thank you for the work.

Some context you'll want: #2699 ("[HIP] [JIT] Add Windows support") was merged on 8 October as b1cdc19eb. It covers much of the same ground as this PR — cpp_extension.py, chip_info.py, jit/core.py, setup.py, pyproject.toml and aiter/__init__.py were all rewritten there. As a result this PR is now CONFLICTING against main, and the conflicts are not mechanical: the same files were changed for the same reasons by a different approach.

I would rather not resolve those conflicts on your behalf. #2699 made some deliberate choices that are easy to lose in a merge — for instance FileBaton._pid_alive no longer probes with os.kill(pid, 0) on Windows, because CPython maps that onto TerminateProcess and the liveness check would kill the builder it was checking on. A conflict resolved in the wrong direction would reintroduce that quietly.

Looking at your diff against what landed, the parts that appear genuinely unique to this PR are aiter/dist/parallel_state.py and aiter/ops/mha.py; the rest looks superseded. If that matches your reading, the cleanest path is to rebase onto current main and resubmit just that delta — it should be a much smaller PR and I'll make sure it gets reviewed promptly this time.

If you think something else here is still needed, say so and I'll look again. And if you'd rather not carry this forward, that's completely fine — just let me know and I'll close it with thanks.

@menglcai-amd
menglcai-amd force-pushed the menglcai/update_windows_support branch from 8d44d07 to bd83a7f Compare October 9, 2026 18:35
@github-actions github-actions Bot changed the title Add basic HIP/CK JIT kernel support in Windows [JIT] Add basic HIP/CK JIT kernel support in Windows Oct 9, 2026
@github-actions github-actions Bot added the JIT label Oct 9, 2026
@menglcai-amd

Copy link
Copy Markdown
Contributor Author

Hi @zufayu,

Thanks for the review. #2699 is an impressive piece of work, and you're right that it supersedes most of what this PR was carrying — I've dropped everything it made obsolete.

On the two files you called out:

  • aiter/dist/parallel_state.py — no longer an issue, so I removed it. The torch build in the current TheRock package exposes both torch.distributed.Backend and ProcessGroup, which is what the guard existed to work around.

  • aiter/ops/mha.py — kept, and rebased onto current main. I also added aiter/jit/utils/flydsl_guard.py with is_flydsl_available(), so the three FlyDSL entry points in mha.py fall back to CK instead of raising ModuleNotFoundError where the package isn't installed. They import it unconditionally today.

One caveat: on Windows this PR also needs a CK submodule fix. Without it the ck_tile buffer addressing path fails to compile:

error: reference to __host__ function 'memcpy' in __device__ function

cast_to_amdgpu_buffer_rsrc_t calls std::memcpy from device code; replacing it with __builtin_memcpy resolves it. I landed on the same patch locally before finding that ROCm/rocm-libraries#13393 already proposes exactly that, covering both amd_buffer_addressing.hpp and amd_buffer_addressing_builtins.hpp. That PR is still open.

With that CK fix applied on top of this one, op_tests/test_mha.py and op_tests/test_mha_varlen.py both complete their full default sweeps (224 configurations each, no assertion failures) on gfx1151 / Windows 25H2.

So while this PR does depend on the CK fix to be useful, it is still a net positive for the Windows platform.

… hosts

mha_fwd failed at dispatch with "invalid argument for fmha_fwd" on every
non-gfx9 GPU. ck-tile's generate.py defaults to --targets gfx9,gfx950, so the
JIT path emitted an empty kernel set for gfx10/11/12 even though the compiler
was correctly given --offload-arch for the runtime arch. mha_recipes already
has _ck_targets_flag() and the prebuild path already uses it; wire it into the
six cmdGenFunc blob_gen_cmd sites the JIT path goes through. For batch_prefill
the same gap produced CDNA-shaped kernels (w32x32x16) that fail to compile for
RDNA at all, rather than an empty set.

flash_attn_func and flash_attn_varlen_func import the FlyDSL kernels
unconditionally, but flydsl ships Linux-only wheels and setup.py already drops
it from the install requirements on Windows, so the import raised
ModuleNotFoundError instead of falling back to CK. Add is_flydsl_available()
and gate the three entry points on it. The probe imports the same module the
callers do, so the version check in aiter/ops/flydsl/__init__.py stays the
single source of truth; it is lazy because that package imports the rest of
aiter. At the varlen backward gate it is applied with `and` at the call site
instead of inside the capability chain, whose `&=` would not short-circuit, so
a host ruled out by arch or shape never imports FlyDSL.

The quotes in flags_extra_cc reached clang literally:
  clang: error: no such file or directory: ''-DONLY_FAV3=0''

Verified on gfx1151 (Windows, TheRock SDK, torch 2.14.0+rocm10.2.0): fwd and
bwd both build and match the reference within tolerance, for test_mha.py and
test_mha_varlen.py.

Signed-off-by: menglcai <[email protected]>
@menglcai-amd
menglcai-amd force-pushed the menglcai/update_windows_support branch from bd83a7f to a93c7b9 Compare October 9, 2026 18:58
@zufayu

zufayu commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Thanks for narrowing the scope. The FlyDSL fallback itself looks reasonable, but I don't think this is merge-ready yet:

  • batch_prefill now receives --targets gfx1151, although the current CK generator only supports gfx9 for this path. This can produce an empty kernel set and fail at runtime with no matching kernel found; the module should be excluded on unsupported targets.
  • The target fix only covers the dynamic cmdGenFunc_* path. PREBUILD still reads the commands from optCompilerConfig.json, where the MHA generators do not have the same target handling. Please keep JIT and PREBUILD behavior consistent.
  • _ck_targets_flag() uses get_gfx(), which only keeps the last entry from a multi-arch GPU_ARCHS value. A build such as gfx942;gfx950;gfx1250 would therefore miss earlier architectures.
  • The Windows validation was done with fix(ck-tile): use __builtin_memcpy for buffer resource casts in device code rocm-libraries#13393 applied locally. Please land that dependency in the AITER CK submodule and rerun the Windows validation from a clean checkout; the Windows smoke check is currently skipped.

Please also update the PR description, since it still describes the files and test results from the old, much larger version of this PR.

@github-actions github-actions Bot changed the title [JIT] Add basic HIP/CK JIT kernel support in Windows [CK] [JIT] Add basic HIP/CK JIT kernel support in Windows Oct 11, 2026
@github-actions github-actions Bot added the CK label Oct 11, 2026
@menglcai-amd

Copy link
Copy Markdown
Contributor Author

These are all fair points.

I have removed the batch_prefill change and bumped the submodule, then re-ran op_tests/test_mha.py and op_tests/test_mha_varlen.py on a clean gfx1151 Windows setup. Results below.

Re-validated on a cleared JIT cache with the bumped submodule, no local CK patches and no CK_DIR:

Environment: gfx1151 (Radeon 8060S), Windows 11 25H2 (build 26200.9168), TheRock SDK, torch 2.14.0+rocm10.2.0a20261009, CK submodule pinned at 509552a1b5170d3812dcbd77c000fa7d06ee2a44.

configurations assertion failures
op_tests/test_mha.py (full default sweep) 224 / 224 0
op_tests/test_mha_varlen.py (full default sweep) 224 / 224 0

I have not made the remaining changes yet, because I noticed that #4340 already covers most of what you raised — the multi-arch get_gfx_list() / _ck_targets_flag_for_arches(), the batch_prefill exclusion, and the forward PREBUILD policy. From your comment there, #4340 looks close to final review with only the dependency and validation left. Submitting a duplicate of the same approach here would likely just add review overhead.

What this PR still has that is not covered there:

  • An arch-independent fix: flags_extra_cc is written as ["'-DONLY_FAV3=0'"]. Since extra_cflags is passed as an argv list, ninja strips the quotes through sh on Linux but uses CreateProcess directly on Windows, so clang receives ''-DONLY_FAV3=0'' and the backward build fails outright.
    • This flag only appears on the two backward generators.
    • With that fixed, and with receipts 300/400 carrying targets, the full default
      sweeps of test_mha.py and test_mha_varlen.py both pass 224/224. Without
      targets they fail at runtime with invalid argument for fmha_bwd.
  • flydsl_guard.py — though [HIP] [FlyDSL] [JIT] Add Windows RDNA CK FMHA support #4340 has its own _FLYDSL_AVAILABLE, and I am not sure which form fits aiter's conventions better. If it merges first and you consider that approach more appropriate, I am happy to switch to it.

I would like your view on narrowing this PR as follows:

  • Reduce it to the backward-related parts only — receipts 300/400 on the JIT side, the module_mha_bwd / module_mha_varlen_bwd / libmha_bwd modules on the PREBUILD side, plus the ONLY_FAV3 fix. For multi-arch I would inherit _ck_targets_flag_for_arches() from PR [HIP] [FlyDSL] [JIT] Add Windows RDNA CK FMHA support #4340 once it lands.

Does that sound reasonable? I will update the PR changes and description once the direction is settled.

@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Oct 11, 2026
@zufayu

zufayu commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Yes, narrowing this PR to the backward-specific delta sounds right.

Please rebase after #4340 lands and keep only:

  • CK target propagation for the JIT backward generators using receipts 300 and 400;
  • the corresponding PREBUILD handling for module_mha_bwd, module_mha_varlen_bwd, and libmha_bwd;
  • the ONLY_FAV3=0 quoting fix.

The forward target policy, FlyDSL availability handling, and CK submodule update should come from #4340 rather than being duplicated here.

Please extend the #4340 PREBUILD policy tests to cover the backward modules, confirm that the existing gfx9 behavior remains unchanged, and rerun both the clean-cache Windows JIT tests and an actual single-architecture Windows PREBUILD/codegen build on gfx1151. Please also update the PR description to reflect the final reduced scope.

@zufayu

zufayu commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

A quick correction after reviewing this against AITER's longer-term backend direction: please hold off on the backward-only CK expansion for now.

AITER expects to reduce, rather than expand, its CK-specific maintenance surface. Adding new Windows CK JIT and PREBUILD handling for backward would create a permanent target-policy and validation obligation that our current Windows CI does not cover. Before taking that on, we need a concrete Windows consumer or workload that requires CK and cannot be served by the Triton/FlyDSL path.

Please do not spend time reworking this PR yet. We will first determine whether the small ONLY_FAV3=0 quoting fix should be kept as an independent bug fix; otherwise, the remaining CK-specific work should be closed rather than expanded. Thank you for narrowing and validating the issue—the technical findings are still useful even if we choose a different maintenance boundary.

@menglcai-amd

Copy link
Copy Markdown
Contributor Author

No problem, thanks for the careful review and for pushing this along.
I will pause here and wait until the direction is settled.

@zufayu

zufayu commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for pausing this work. We have decided not to expand the Windows CK support surface, so I am closing this PR. If the ONLY_FAV3=0 quoting fix is still independently needed, please send it as a small standalone PR without the CK backward/PREBUILD changes.

@menglcai-amd

Copy link
Copy Markdown
Contributor Author

I have opened #6616 for the -DONLY_FAV3=0 quoting fix, as suggested. Two-line change, no CK backward/PREBUILD work included.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants