Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
e2d1bd6
Vulkan: fuse Q@K^T and softmax on the SDPA decode path
msluszniak Sep 18, 2026
8c348f9
Vulkan: gate the fused SDPA decode path on the live context length
msluszniak Sep 18, 2026
5d95155
Vulkan: generate the fused SDPA decode shader from the coop template
msluszniak Oct 8, 2026
b9a353a
Vulkan: fix the zero dispatch and resize of the fused SDPA decode node
msluszniak Oct 8, 2026
f5aeace
Vulkan: add the fused SDPA decode node to the test_sdpa mirror
msluszniak Oct 8, 2026
efe6b04
Merge remote-tracking branch 'up/main' into ms/vulkan-sdpa-fused-deco…
msluszniak Oct 12, 2026
74723bf
Merge remote-tracking branch 'up/main' into ms/vulkan-sdpa-fused-deco…
msluszniak Oct 13, 2026
73125c9
Vulkan SDPA: size the fused decode row storage to the 256-token gate
msluszniak Oct 20, 2026
56d0ed1
Vulkan SDPA: skip separate decode QK only when a fused node exists
msluszniak Oct 20, 2026
ed54242
Merge upstream main into ms/vulkan-sdpa-fused-decode
msluszniak Nov 17, 2026
3fc4450
Vulkan SDPA: select the fused decode path in one place
msluszniak Nov 17, 2026
b663d9a
Vulkan SDPA: bound the fused decode row in the shader
msluszniak Nov 17, 2026
e6261fa
Vulkan SDPA: test decode across the fused QK+softmax cutoff
msluszniak Nov 17, 2026
8286682
Vulkan SDPA: use the fused decode path only up to head_dim 64
msluszniak Dec 3, 2026
750e7c3
Vulkan CI: validate the sdpa correctness cases
msluszniak Dec 3, 2026
dc5e320
Vulkan SDPA test: prefill the cache, and compare fp16 at fp16 tolerances
msluszniak Dec 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ jobs:
PYTHON_EXECUTABLE=python bash backends/vulkan/test/ops/benchmark_v2/build.sh
for op in binary/add gemm/linear/qw/q8csw convolution/qw/q8csw gemm/linear/qw/q4gsw \
quantization/choose_qparams_per_row quantize/q8ta clone/q8ta binary/q8ta \
warp; do
warp attention/sdpa; do
./cmake-out/backends/vulkan/test/ops/benchmark_v2/etvk_ops_bench --validation_only \
--config=backends/vulkan/test/ops/benchmark_v2/operators/${op}/cases/correctness.json
done
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/vulkan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ jobs:
PYTHON_EXECUTABLE=python bash backends/vulkan/test/ops/benchmark_v2/build.sh
for op in binary/add gemm/linear/qw/q8csw convolution/qw/q8csw gemm/linear/qw/q4gsw \
quantization/choose_qparams_per_row quantize/q8ta clone/q8ta binary/q8ta \
warp; do
warp attention/sdpa; do
./cmake-out/backends/vulkan/test/ops/benchmark_v2/etvk_ops_bench --validation_only \
--config=backends/vulkan/test/ops/benchmark_v2/operators/${op}/cases/correctness.json
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ $if MODE == "llm":

layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;

// Shared memory for cooperative max finding and exp sum reduction.
// For fused SDPA, reductions happen in fp32 for numerical stability.
shared SOFTMAX_ACC_T shared_max[NUM_WORKERS_PER_WG];
shared SOFTMAX_ACC_T shared_exp_sum[NUM_WORKERS_PER_WG];

SOFTMAX_IN_VEC4_T load_attn_weights_c4(
const int c4,
const int s,
Expand Down Expand Up @@ -76,6 +71,13 @@ void store_attn_weights_softmax_c4(
#endif
}

#ifndef HAS_INPUT_POS
// ATen's safe softmax returns zero for rows masked entirely with -inf.
#define SOFTMAX_ZERO_FULLY_MASKED_ROWS
#endif

#include "sdpa_attn_weights_softmax_row.glslh"

/*
* 3-pass numerically stable softmax over the context_len dimension of
* attention weights.
Expand Down Expand Up @@ -119,7 +121,6 @@ void main() {
#else
const int context_len = k_sizes.y;
#endif
const int context_texel_len = div_up_4(context_len);

// LLM: attn_weights S dim is padded to S_aligned; fused: not padded.
#ifdef HAS_INPUT_POS
Expand All @@ -134,142 +135,5 @@ void main() {
return;
}

const int context_len_aligned_down = context_len - mod_4(context_len);
const int C4_limit = div_4(context_len_aligned_down);

// =========================================================================
// Pass 1: Find the maximum value across the row for numerical stability.
// Without this, exp(x) can overflow float32 when x > ~88.7.
// =========================================================================

SOFTMAX_ACC_T local_max = SOFTMAX_ACC_T(-1.0 / 0.0); // -infinity

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

for (int comp = 0; comp < 4; comp++) {
local_max = max(local_max, SOFTMAX_ACC_T(in_texel[comp]));
}
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
local_max = max(local_max, SOFTMAX_ACC_T(in_texel[comp]));
}
}
}
}

shared_max[worker_id] = local_max;

memoryBarrierShared();
barrier();

// Tree reduction to find the global max
for (int i = NUM_WORKERS_PER_WG / 2; i > 0; i >>= 1) {
if (worker_id < i) {
shared_max[worker_id] = max(
shared_max[worker_id], shared_max[worker_id + i]);
}
memoryBarrierShared();
barrier();
}

const SOFTMAX_ACC_T global_max = shared_max[0];

#ifndef HAS_INPUT_POS
// ATen's safe softmax returns zero for rows masked entirely with -inf.
if (global_max == SOFTMAX_ACC_T(-1.0 / 0.0)) {
for (int c4 = worker_id; c4 < context_texel_len; c4 += NUM_WORKERS_PER_WG) {
store_attn_weights_softmax_c4(
VEC4_T(0), c4, s, q_h, context_texel_len, attn_S, Q_H);
}
return;
}
#endif

// =========================================================================
// Pass 2: Compute sum(exp(x - max)) using the global max for stability
// =========================================================================

SOFTMAX_ACC_T local_exp_sum = SOFTMAX_ACC_T(0);

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

for (int comp = 0; comp < 4; comp++) {
local_exp_sum += exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max);
}
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
local_exp_sum += exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max);
}
}
}
}

shared_exp_sum[worker_id] = local_exp_sum;

memoryBarrierShared();
barrier();

// Tree reduction to compute the overall exp sum
for (int i = NUM_WORKERS_PER_WG / 2; i > 0; i >>= 1) {
if (worker_id < i) {
shared_exp_sum[worker_id] = shared_exp_sum[worker_id] +
shared_exp_sum[worker_id + i];
}
memoryBarrierShared();
barrier();
}

local_exp_sum = shared_exp_sum[0];

// =========================================================================
// Pass 3: Normalize each element: out = exp(x - max) / sum(exp(x - max))
// =========================================================================

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

VEC4_T out_texel;
[[unroll]] for (int comp = 0; comp < 4; comp++) {
out_texel[comp] = T(
exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max) / local_exp_sum);
}
store_attn_weights_softmax_c4(
out_texel, c4, s, q_h, context_texel_len, attn_S, Q_H);
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

VEC4_T out_texel = VEC4_T(0);
[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
out_texel[comp] = T(
exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max) / local_exp_sum);
}
}
store_attn_weights_softmax_c4(
out_texel, c4, s, q_h, context_texel_len, attn_S, Q_H);
}
}
softmax_attn_weights_row(worker_id, s, q_h, context_len, attn_S, Q_H);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

/*
* Cooperative softmax over one row of attention weights, shared by the
* standalone softmax shader and the fused QK + softmax variant of the coop
* attention weights shader.
*
* Required macros:
* NUM_WORKERS_PER_WG - number of invocations in the work group
* SOFTMAX_IN_VEC4_T - texel type returned by load_attn_weights_c4
* SOFTMAX_ACC_T - scalar type the max and exp sum are reduced in
* VEC4_T, T - texel and scalar type of the softmax output
*
* Required functions, defined before including this file:
* SOFTMAX_IN_VEC4_T load_attn_weights_c4(c4, s, q_h, C4, S, Q_H)
* void store_attn_weights_softmax_c4(VEC4_T, c4, s, q_h, C4, S, Q_H)
*
* Optional macros:
* SOFTMAX_ZERO_FULLY_MASKED_ROWS - write zeros for a row whose elements are
* all -inf, as ATen's safe softmax does,
* instead of NaN
*/

#ifndef SDPA_ATTN_WEIGHTS_SOFTMAX_ROW_GLSLH
#define SDPA_ATTN_WEIGHTS_SOFTMAX_ROW_GLSLH

#extension GL_EXT_control_flow_attributes : require

// Shared memory for cooperative max finding and exp sum reduction.
// For fused SDPA, reductions happen in fp32 for numerical stability.
shared SOFTMAX_ACC_T shared_max[NUM_WORKERS_PER_WG];
shared SOFTMAX_ACC_T shared_exp_sum[NUM_WORKERS_PER_WG];

/*
* 3-pass numerically stable softmax over the first context_len elements of
* row (s, q_h). attn_S is the S extent of the attention weight tensors. Every
* invocation of the work group must call this, since it contains barriers.
*/
void softmax_attn_weights_row(
const int worker_id,
const int s,
const int q_h,
const int context_len,
const int attn_S,
const int Q_H) {
const int context_texel_len = div_up_4(context_len);

const int context_len_aligned_down = context_len - mod_4(context_len);
const int C4_limit = div_4(context_len_aligned_down);

// =========================================================================
// Pass 1: Find the maximum value across the row for numerical stability.
// Without this, exp(x) can overflow float32 when x > ~88.7.
// =========================================================================

SOFTMAX_ACC_T local_max = SOFTMAX_ACC_T(-1.0 / 0.0); // -infinity

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

for (int comp = 0; comp < 4; comp++) {
local_max = max(local_max, SOFTMAX_ACC_T(in_texel[comp]));
}
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
local_max = max(local_max, SOFTMAX_ACC_T(in_texel[comp]));
}
}
}
}

shared_max[worker_id] = local_max;

memoryBarrierShared();
barrier();

// Tree reduction to find the global max
for (int i = NUM_WORKERS_PER_WG / 2; i > 0; i >>= 1) {
if (worker_id < i) {
shared_max[worker_id] = max(
shared_max[worker_id], shared_max[worker_id + i]);
}
memoryBarrierShared();
barrier();
}

const SOFTMAX_ACC_T global_max = shared_max[0];

#ifdef SOFTMAX_ZERO_FULLY_MASKED_ROWS
if (global_max == SOFTMAX_ACC_T(-1.0 / 0.0)) {
for (int c4 = worker_id; c4 < context_texel_len; c4 += NUM_WORKERS_PER_WG) {
store_attn_weights_softmax_c4(
VEC4_T(0), c4, s, q_h, context_texel_len, attn_S, Q_H);
}
return;
}
#endif

// =========================================================================
// Pass 2: Compute sum(exp(x - max)) using the global max for stability
// =========================================================================

SOFTMAX_ACC_T local_exp_sum = SOFTMAX_ACC_T(0);

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

for (int comp = 0; comp < 4; comp++) {
local_exp_sum += exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max);
}
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
local_exp_sum += exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max);
}
}
}
}

shared_exp_sum[worker_id] = local_exp_sum;

memoryBarrierShared();
barrier();

// Tree reduction to compute the overall exp sum
for (int i = NUM_WORKERS_PER_WG / 2; i > 0; i >>= 1) {
if (worker_id < i) {
shared_exp_sum[worker_id] = shared_exp_sum[worker_id] +
shared_exp_sum[worker_id + i];
}
memoryBarrierShared();
barrier();
}

local_exp_sum = shared_exp_sum[0];

// =========================================================================
// Pass 3: Normalize each element: out = exp(x - max) / sum(exp(x - max))
// =========================================================================

for (int c4 = worker_id; c4 < C4_limit; c4 += NUM_WORKERS_PER_WG) {
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

VEC4_T out_texel;
[[unroll]] for (int comp = 0; comp < 4; comp++) {
out_texel[comp] = T(
exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max) / local_exp_sum);
}
store_attn_weights_softmax_c4(
out_texel, c4, s, q_h, context_texel_len, attn_S, Q_H);
}
if (worker_id == 0) {
for (int c4 = C4_limit; c4 < context_texel_len; ++c4) {
const int c_base = mul_4(c4);
SOFTMAX_IN_VEC4_T in_texel = load_attn_weights_c4(
c4, s, q_h, context_texel_len, attn_S, Q_H);

VEC4_T out_texel = VEC4_T(0);
[[unroll]] for (int comp = 0; comp < 4; comp++) {
if (c_base + comp < context_len) {
out_texel[comp] = T(
exp(SOFTMAX_ACC_T(in_texel[comp]) - global_max) / local_exp_sum);
}
}
store_attn_weights_softmax_c4(
out_texel, c4, s, q_h, context_texel_len, attn_S, Q_H);
}
}
}

#endif // SDPA_ATTN_WEIGHTS_SOFTMAX_ROW_GLSLH
Loading