Repository navigation
fix: return β*C when K is zero in blas/base/*gemm - #16477
Open
MeKaustubh07 wants to merge 2 commits into
Open
MeKaustubh07 wants to merge 2 commits into
MeKaustubh07 wants to merge 2 commits into
Conversation
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: na
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: passed
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
Contributor
Coverage Report
The above coverage report was generated for the changes in this PR. |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
reviewed
Nov 3, 2026
| var c; | ||
| var i; | ||
|
|
||
| alphas = [ NaN, Infinity, -Infinity, 2.0 ]; |
kgryte
requested changes
Nov 3, 2026
kgryte
left a comment
Member
There was a problem hiding this comment.
Other than making sure to use the corresponding constants packages, this PR is looking good. Thanks for finding and fixing, @MeKaustubh07!
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
- task: lint_filenames
status: passed
- task: lint_editorconfig
status: passed
- task: lint_markdown_pkg_readmes
status: na
- task: lint_markdown_docs
status: na
- task: lint_markdown
status: na
- task: lint_package_json
status: na
- task: lint_repl_help
status: na
- task: lint_javascript_src
status: na
- task: lint_javascript_cli
status: na
- task: lint_javascript_examples
status: na
- task: lint_javascript_tests
status: passed
- task: lint_javascript_benchmarks
status: na
- task: lint_python
status: na
- task: lint_r
status: na
- task: lint_c_src
status: na
- task: lint_c_examples
status: na
- task: lint_c_benchmarks
status: na
- task: lint_c_tests_fixtures
status: na
- task: lint_shell
status: na
- task: lint_typescript_declarations
status: passed
- task: lint_typescript_tests
status: na
- task: lint_license_headers
status: passed
---
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes. report:
Resolves #{{TODO: add issue number}}.
Description
Resolves #{{issue number}}.
Description
This pull request:
blas/base/dgemm,blas/base/sgemm, andblas/base/ggemmso that, whenKis0, the functions always returnβ*C, regardless of the value ofαand of the memory layout ofAandB.The functions compute
C = α*op(A)*op(B) + β*C, where each element ofop(A)*op(B)is a dot product of lengthK. WhenKis0,op(A)has no columns andop(B)has no rows, so the product contributes nothing and the result isβ*C. Reference BLAS (dgemm.f) behaves this way: it scalesCbyβand then runs a loop overK, which does not execute whenKis0, soαis never used.In
lib/base.js,Cis first scaled byβ, after which the function returns early only whenαis0:When
Kis0andαis nonzero, execution continues into one of two kernels, chosen based on the memory layouts ofAandB:blocked) iteratesfor ( k = K; k > 0; ), which does not run whenKis0, soCremainsβ*C(correct).naive) computesC[ ic ] += alpha * ddot( K, ... ). ForK = 0,ddotreturns0.0, and whenαisNaNor±Infinity,alpha * 0.0isNaN, so every element ofCbecomesNaN(incorrect).As a result, the same call returns different results depending only on memory layout:
Both calls should return
[ 0.5, 1.0, 1.5, 2.0 ].Change
The early return now also covers
K = 0, which, likeα = 0, leaves nothing to add after scalingCbyβ:The existing quick return for
β = 1(( alpha === 0.0 || K === 0 ) && beta === 1.0) is unchanged. The same change is applied todgemm,sgemm, andggemm(in bothlib/base.jsandlib/accessors.js), so it applies to both the main andndarrayAPIs.Tests
The existing tests only cover
K = 0together withβ = 1. Added, for each package (and forggemmwith accessor arrays), tests that whenKis0andαisNaN,±Infinity, or finite, the functions returnβ*C(forβ = 0.5) and a zero-filled matrix (forβ = 0) for row-majorno-transpose/no-transpose(loop-tiling kernel) and row-majorno-transpose/transposeand column-majortranspose/no-transpose(dot-product kernel). These tests fail before this change and pass after it.Related Issues
This pull request has the following related issues:
Reference Packages
This pull request followed these packages:
blas/ext/base/gxmy), or none, breaking new ground}}Questions
No.
Other
No.
Checklist
AI Assistance
If you answered "yes" above, how did you use AI assistance?
Disclosure
{{TODO: add disclosure if applicable}}
@stdlib-js/reviewers