Skip to content
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
a4d7e60
Limit softmax to causally-valid elements in cpu_sdpa
kimishpatel Apr 1, 2026
2e6dc3a
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 6, 2026
3f4a091
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 6, 2026
d4c4d4c
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 7, 2026
a8009f6
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 8, 2026
e82332a
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 9, 2026
4fc0c00
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 13, 2026
bb4e73f
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 13, 2026
695e7ad
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 23, 2026
739cbcc
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 24, 2026
9ca8053
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 27, 2026
fd9866f
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 28, 2026
ab0bef6
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 29, 2026
40d48c7
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 30, 2026
1da0df4
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel Apr 30, 2026
b4a3397
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel May 1, 2026
bbc0c50
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel May 5, 2026
fe6e84d
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel May 5, 2026
b221511
Update on "Limit softmax to causally-valid elements in cpu_sdpa"
kimishpatel May 6, 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
37 changes: 21 additions & 16 deletions extension/llm/custom_ops/op_sdpa_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1349,42 +1349,47 @@ void cpu_sdpa(
kStrideN,
scores);

// Causal mask + scaling + attention mask + softmax per query row
// Scaling + causal-limited softmax per query row
for (int64_t qi = 0; qi < qSize; ++qi) {
accum_t* row = scores + qi * kvSize;

// Apply causal mask
if (is_causal) {
int64_t valid = std::min(start_pos + qi + 1, kvSize);
for (int64_t j = valid; j < kvSize; ++j) {
row[j] = -std::numeric_limits<accum_t>::infinity();
}
}
int64_t num_valid =
is_causal ? std::min(start_pos + qi + 1, kvSize) : kvSize;

accum_t max_val;
const int kvSizeInt = static_cast<int>(kvSize);
const int num_valid_int = static_cast<int>(num_valid);
if (has_attn_mask) {
// Apply scaling factor and attention mask in fusion
// Apply scaling factor and attention mask over valid range
const accum_t* mask_row = mask_data + qi * mStrideM;
for (int64_t j = 0; j < kvSize; ++j) {
for (int64_t j = 0; j < num_valid; ++j) {
row[j] = row[j] * scaling_factor + mask_row[j];
}
max_val = vec::reduce_all<accum_t>(
[](Vec& x, Vec& y) { return vec::maximum(x, y); }, row, kvSize);
[](Vec& x, Vec& y) { return vec::maximum(x, y); },
row,
num_valid);
} else {
// Apply scaling factor and find max in fusion
// Apply scaling factor and find max over valid range
_mul_reduce_max_fusion_kernel(
row, scaling_factor, kvSizeInt, row, max_val);
row, scaling_factor, num_valid_int, row, max_val);
}

if (max_val == -std::numeric_limits<accum_t>::infinity()) {
fill_stub(row, static_cast<accum_t>(0), kvSize);
} else {
accum_t sum_val = max_val;
_exp_reduce_sum_fusion_kernel(row, kvSizeInt, row, sum_val);
_exp_reduce_sum_fusion_kernel(row, num_valid_int, row, sum_val);
accum_t inv_sum = static_cast<accum_t>(1) / sum_val;
vec::map<accum_t>(
[inv_sum](Vec x) { return x * Vec(inv_sum); }, row, row, kvSize);
[inv_sum](Vec x) { return x * Vec(inv_sum); },
row,
row,
num_valid);
// Zero out masked positions for GEMM 2
if (num_valid < kvSize) {
fill_stub(
row + num_valid, static_cast<accum_t>(0), kvSize - num_valid);
}
}
}

Expand Down
Loading