Skip to content

fix: return β*C when K is zero in blas/base/*gemm - #16477

Open
MeKaustubh07 wants to merge 2 commits into
stdlib-js:developfrom
MeKaustubh07:fix-dgemm-2
Open

MeKaustubh07 wants to merge 2 commits into
stdlib-js:developfrom
MeKaustubh07:fix-dgemm-2

Conversation

@MeKaustubh07

Copy link
Copy Markdown
Member

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 ---

Resolves #{{TODO: add issue number}}.

Description

Resolves #{{issue number}}.

Description

What is the purpose of this pull request?

This pull request:

  • fixes blas/base/dgemm, blas/base/sgemm, and blas/base/ggemm so that, when K is 0, the functions always return β*C, regardless of the value of α and of the memory layout of A and B.

The functions compute C = α*op(A)*op(B) + β*C, where each element of op(A)*op(B) is a dot product of length K. When K is 0, op(A) has no columns and op(B) has no rows, so the product contributes nothing and the result is β*C. Reference BLAS (dgemm.f) behaves this way: it scales C by β and then runs a loop over K, which does not execute when K is 0, so α is never used.

In lib/base.js, C is first scaled by β, after which the function returns early only when α is 0:

// Check whether we can early return...
if ( alpha === 0.0 ) {
	return C;
}

When K is 0 and α is nonzero, execution continues into one of two kernels, chosen based on the memory layouts of A and B:

  • the loop-tiling kernel (blocked) iterates for ( k = K; k > 0; ), which does not run when K is 0, so C remains β*C (correct).
  • the dot-product kernel (naive) computes C[ ic ] += alpha * ddot( K, ... ). For K = 0, ddot returns 0.0, and when α is NaN or ±Infinity, alpha * 0.0 is NaN, so every element of C becomes NaN (incorrect).

As a result, the same call returns different results depending only on memory layout:

var Float64Array = require( '@stdlib/array/float64' );
var dgemm = require( '@stdlib/blas/base/dgemm' );

var A = new Float64Array( 0 );
var B = new Float64Array( 0 );

var C1 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
dgemm( 'row-major', 'no-transpose', 'no-transpose', 2, 2, 0, Infinity, A, 1, B, 2, 0.5, C1, 2 );
// C1 => <Float64Array>[ 0.5, 1.0, 1.5, 2.0 ]

var C2 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] );
dgemm( 'row-major', 'no-transpose', 'transpose', 2, 2, 0, Infinity, A, 1, B, 1, 0.5, C2, 2 );
// C2 => <Float64Array>[ NaN, NaN, NaN, NaN ]

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 scaling C by β:

// Check whether we can early return...
if ( alpha === 0.0 || K === 0 ) {
	return C;
}

The existing quick return for β = 1 (( alpha === 0.0 || K === 0 ) && beta === 1.0) is unchanged. The same change is applied to dgemm, sgemm, and ggemm (in both lib/base.js and lib/accessors.js), so it applies to both the main and ndarray APIs.

Tests

The existing tests only cover K = 0 together with β = 1. Added, for each package (and for ggemm with accessor arrays), tests that when K is 0 and α is NaN, ±Infinity, or finite, the functions return β*C (for β = 0.5) and a zero-filled matrix (for β = 0) for row-major no-transpose/no-transpose (loop-tiling kernel) and row-major no-transpose/transpose and column-major transpose/no-transpose (dot-product kernel). These tests fail before this change and pass after it.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

  • #{{issue number}}

Reference Packages

Did the content of this pull request follow one or more existing packages? Answering is optional.

This pull request followed these packages:

  • {{TODO: add package path (e.g., blas/ext/base/gxmy), or none, breaking new ground}}

What carried over from the reference(s)?

  • {{TODO: add what carried over (e.g., package structure, naming, test shape, benchmark layout, and/or documentation)}}

What differed, or what did you have to work out yourself?

  • {{TODO: add what you worked out without a reference}}

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code." or "I consulted ChatGPT to understand the codebase, but the proposed changes were fully authored manually by myself.".

{{TODO: add disclosure if applicable}}


@stdlib-js/reviewers

---
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
---
@MeKaustubh07
MeKaustubh07 requested a review from a team November 3, 2026 16:16
@stdlib-bot stdlib-bot added BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). Needs Review A pull request which needs code review. labels Nov 3, 2026
@MeKaustubh07 MeKaustubh07 added the Tests Pull requests specifically adding tests. label Nov 3, 2026
@stdlib-bot

stdlib-bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
blas/base/dgemm $\\color{green}843/843$
$\\color{green}+0.00\\%$
$\\color{green}120/120$
$\\color{green}+0.00\\%$
$\\color{green}8/8$
$\\color{green}+0.00\\%$
$\\color{green}843/843$
$\\color{green}+0.00\\%$
blas/base/ggemm $\\color{red}1335/1344$
$\\color{green}+0.00\\%$
$\\color{red}174/177$
$\\color{green}+0.00\\%$
$\\color{green}14/14$
$\\color{green}+0.00\\%$
$\\color{red}1335/1344$
$\\color{green}+0.00\\%$
blas/base/sgemm $\\color{green}844/844$
$\\color{green}+0.00\\%$
$\\color{green}120/120$
$\\color{green}+0.00\\%$
$\\color{green}8/8$
$\\color{green}+0.00\\%$
$\\color{green}844/844$
$\\color{green}+0.00\\%$

The above coverage report was generated for the changes in this PR.

@kgryte kgryte removed the Needs Review A pull request which needs code review. label Nov 3, 2026
var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

var c;
var i;

alphas = [ NaN, Infinity, -Infinity, 2.0 ];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use pinf and ninf.

@kgryte kgryte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
---
@MeKaustubh07
MeKaustubh07 requested a review from kgryte November 4, 2026 05:20
@stdlib-bot stdlib-bot added the Needs Review A pull request which needs code review. label Nov 4, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BLAS Issue or pull request related to Basic Linear Algebra Subprograms (BLAS). Needs Review A pull request which needs code review. Tests Pull requests specifically adding tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants