Skip to content

Fix transpose kernels to use actual tensor strides - #1631

Merged
fsx950223 merged 2 commits into
mainfrom
pa_gluon_fix_bf16
Dec 15, 2025
Merged

fsx950223 merged 2 commits into
mainfrom
pa_gluon_fix_bf16

Conversation

@yanguahe

@yanguahe yanguahe commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

Motivation

The current implementation of transpose_query_gluon and transpose_output_gluon kernels assumes that input and output tensors are always contiguous in memory. Stride calculations are hardcoded based on tensor dimensions (e.g., num_kv_heads * query_group_size * head_size), which fails when tensors are non-contiguous (e.g., after slicing, transposing, or view operations). This PR fixes the stride calculation to support non-contiguous tensors by using actual tensor strides via tensor.stride().

Technical Details

  1. Modified stride calculations in transpose_query_gluon:

    • Replaced hardcoded stride calculations with actual tensor strides using query.stride(0), query.stride(1), etc.
    • Input strides are now derived from actual tensor layout: stride_input_batch = query_sequence_length * query_stride_0
    • Output strides use query_gluon.stride(0) and query_gluon.stride(1) directly
    • Applied the same fix to query_scale tensor strides
  2. Modified stride calculations in transpose_output_gluon:

    • Input strides now use output_gluon.stride(0) and output_gluon.stride(1)
    • Output strides use output.stride(0) and output.stride(1) directly
  3. Updated AOT (Ahead-of-Time) compilation versions:

    • Applied the same stride calculation fixes to transpose_query_gluon_aot and transpose_output_gluon_aot in transpose_query_output_gluon_aot.py

Test Plan

  • Extended test configurations in test_transpose_query_output_gluon.py to cover more parameter combinations:
    • Added tests with include_scale=True for float16, bfloat16, and float8_e4m3fnuz dtypes
    • Added tests with different seq_len values (1, 2, 3, 4)
    • Added tests with different batch_size values (1, 64, 128, 16384)
    • Added tests with different num_kv_heads values (1, 4)

Test Result

All test configurations pass, verifying that:

  • Transpose operations produce correct results for both contiguous and non-contiguous tensors
  • Scale tensor transposition works correctly when include_scale=True
  • Both JIT and AOT kernel versions produce consistent results
  • Various dtype combinations (float16, bfloat16, float8_e4m3fnuz) work correctly

Submission Checklist

  • Code changes follow the existing coding style
  • Updated stride calculations to use actual tensor strides
  • Applied changes consistently across JIT and AOT implementations
  • Added comprehensive test cases covering edge cases
  • All existing and new tests pass
  • No breaking changes to the public API

…ose_query/output_gluon

- Replace hardcoded stride calculations with tensor.stride() calls
- Update both JIT and AOT versions of transpose_query_gluon and transpose_output_gluon
- Add more test configurations for various batch sizes and seq lengths
@yanguahe
yanguahe requested a review from a team December 12, 2025 10:12
@yanguahe yanguahe changed the title Use actual tensor strides to support non-contiguous tensors in transp… Fix transpose kernels to use actual tensor strides Dec 12, 2025
@yanguahe
yanguahe requested a review from valarLip December 14, 2025 02:36
@yanguahe yanguahe assigned yanguahe and unassigned yanguahe Dec 14, 2025
@yanguahe
yanguahe requested review from coderfeli and removed request for coderfeli December 14, 2025 02:48
@fsx950223
fsx950223 merged commit 99c1514 into main Dec 15, 2025
27 of 30 checks passed
@fsx950223
fsx950223 deleted the pa_gluon_fix_bf16 branch December 15, 2025 03:09
zhuyuhua-v pushed a commit that referenced this pull request Dec 17, 2025
* Use actual tensor strides to support non-contiguous tensors in transpose_query/output_gluon

- Replace hardcoded stride calculations with tensor.stride() calls
- Update both JIT and AOT versions of transpose_query_gluon and transpose_output_gluon
- Add more test configurations for various batch sizes and seq lengths

* move debug code
ZhangLirong-amd pushed a commit that referenced this pull request Dec 29, 2025
* Use actual tensor strides to support non-contiguous tensors in transpose_query/output_gluon

- Replace hardcoded stride calculations with tensor.stride() calls
- Update both JIT and AOT versions of transpose_query_gluon and transpose_output_gluon
- Add more test configurations for various batch sizes and seq lengths

* move debug code
valarLip pushed a commit that referenced this pull request Mar 18, 2026
* Use actual tensor strides to support non-contiguous tensors in transpose_query/output_gluon

- Replace hardcoded stride calculations with tensor.stride() calls
- Update both JIT and AOT versions of transpose_query_gluon and transpose_output_gluon
- Add more test configurations for various batch sizes and seq lengths

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants