-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: prevent division by zero in soft_max vulkan shader #2604
Conversation
This change prevents a division by zero error when p.KY is 0.
The Vulkan fix looks correct, this can happen when using |
Looks good, but what are the other two changes? |
Closing this PR in favor of a more focused fix in #2633. |
Thank you. I'm still interested in which cases |
I believe it happens when using using a NULL mask, which happens when using the plain ggml_vk_op_f32<vk_op_soft_max_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SOFT_MAX, {
ncols,
src1 != nullptr ? nrows_y : (uint32_t)0, // this is KY
scale, max_bias,
m0, m1,
n_head_log2,
nrows_x,
}, dryrun); |
That's what I thought as well, but we do have tests in |
Division by zero is likely just undefined behavior, and in some GPUs it may just return zero. Still, can't rely on that behavior. |
Fix division by zero error in soft_max vulkan shader
This PR fixes #2596 by adding a check for p.KY being zero in the soft_max compute shader.
Changes:
const uint rowy = rowx % p.KY;
toconst uint rowy = (p.KY > 0) ? (rowx % p.KY) : 0;
The original code would cause a division by zero error when p.KY is 0. This fix ensures that rowy is set to 0 in such cases, preventing the crash while maintaining the expected behavior in normal scenarios.
Testing: