[GPU] Fix for ScaledAttn functional layer tests on PTL #38600
Can we remove the default value for k_head_size, here and in get_seq_len_partition_size? It hides the callers which were not updated, and as far as I see, PA is already affected. get_jit_constants_base is shared with PagedAttentionSDPAOptGeneratorMultiToken, so for PA the JIT now gets desc->k_head_size, while paged_attention_opt.cpp still calls these helpers without it (get_partitioning_size, the multi-token dispatch func, sdpa_opt_seq_len_partition_size). As a result, for PA prefill with different K/V head sizes (e.g. 192/128), the kernel is compiled with SG_SCALE_FACTOR=1, but dispatched with LWS and partition size calculated for scale factor 2. With a required parameter, this would be a compilation error. Could you please double check?
As far as I see, the root cause is in SDPABase::static_canonicalize_shapes: the attention mask input is extended to rank 4 even if it is a placeholder. But the placeholder is just an empty input, so it must remain scalar. Let's skip it in the canonicalization instead: then the original rank check here keeps working, and we don't need this heuristic. Please note: with the current approach, a real static mask with [1,1,1,1] shape would be silently ignored. What do you think?
Why was this line removed?
That statement was unreachable dead code, because dynamic_cast throws only std::bad_cast, which is caught by the catch block. I can revert it back if you have real concern about that.
You are right, modified.
Modified
Please remove default parameter
Minor: this note carries over the caveat about the old single-element heuristic, but it doesn't apply to the rank check below anymore: a static mask with shape [1,1,1,1] has rank 4, so rank().get_length() > 1 is true and it's still detected as a real mask. The actually indistinguishable case is a genuinely scalar (rank-0) real mask, not a [1,1,1,1] one.
Even after the refactoring, k_head_size is derived independently here and in get_jit_constants_base, and still passed around as a magic 0 elsewhere meaning "not applicable for this stage". Let's add a single SDPABase::get_effective_head_sizes(params) (paged-attention just returns desc->k_head_size/v_head_size; the SDPA branch reuses this exact get_head_size + int4-override logic, already identical in both places today) and call it everywhere instead of re-deriving the pair per call site. What do you think?
Removed
Removed
Modified
Should we remove k_head_size == 0 after the refactoring? It looks like 0 is not a special value anymore
Let's assert that result is not nullptr
Only minor comments left
PR #38853
params.typed_desc uses static cast - it does not return nullptr
v-Golubev
nshchego
Details:
- sdpa_opt.cl/sdpa_ref.cl: scale input was read with the wrong buffer type (INPUT3_TYPE instead of the always-correct INPUT4_TYPE), since a placeholder attn_mask input always occupies slot 3 when scale is used.
sdpa/sdpa_utils.hpp: a placeholder (dummy) attention-mask was mis-detected as a real mask because shape canonicalization pads it to rank 4 before the rank()>1 check runs; added an element-count check.
sdpa/sdpa_opt.cpp: the actual segfault — execute()'s stage-selection condition (is_prefill) didn't match the constructor's stage-creation condition (is_prefill || unaligned_head_size), so for static shapes with decode length 1 + unaligned head size, it looked up a kernel stage that was never created, crashing in dynamic_cast.
- In the Q-loading remainder path, int valid_workers = K_HEAD_SIZE - k_sgidSUBGROUP_SIZE could go negative when extra subgroups (sized for a larger V_HEAD_SIZE) exceed what's needed for K_HEAD_SIZE. Comparing the unsigned sglid against that negative int made the guard true for all lanes, corrupting slm_query.
- In load_attn_mask()'s boundary-padding branch, the float literal NAN was assigned into an INPUT3_TYPE intermediate (ushort for bf16 mask tensors). The implicit float→ushort conversion doesn't preserve NaN's bit pattern, so after being decoded back as bf16 it became a large finite garbage value instead of NaN. This broke the masking of out-of-range key positions (e.g., the trailing partition when SOURCE_SEQ_LEN isn't a multiple of the partition size), poisoning the softmax max and collapsing entire output rows to all-zero — exactly matching the Actual: 0, Expected: <value> failures, only for bf16 + prefill + mismatched K/V head size + real runtime mask.
Tickets:
- 196455
AI Assistance:
- AI assistance used: yes*