Skip to content

refactor: MatMul, MatMul32 and the linalg products run on go-blas/blas - #122

Merged
tannevaled merged 4 commits into
mainfrom
refactor/use-go-blas
Nov 15, 2026
Merged

tannevaled merged 4 commits into
mainfrom
refactor/use-go-blas

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

MatMul, MatMul32 and the linalg products call
go-blas/blas v0.2.1 (DgemmStrided /
SgemmStrided) instead of ndarray's own copy of the same GEMM, so there is one
implementation again (go-blas/blas#1 moved it byte for byte).

  • Removed from internal/kernels: gemm.go, gemm_<arch>.go,
    gemm32_*, gemmua_other.go, gemm_generic.go, the GEMM tests (kernels,
    fences, packRows8, TestGemmOneWorkerSameBits), the GEMM half of every
    generator in asmgen/<arch> and of every sum_<arch>.s.
  • Added: internal/blasmm (an operand's row and column strides go to
    go-blas as they are: a transposed or a[:, ::2] view is not copied; extent-1
    axes are normalised; a broadcast or reversed view is copied;
    HasSgemmKernel() picks native float32 or the widening path),
    matvec.go and dot_<arch>.go (the mat-vec and dot code that shared files
    with the GEMM, moved unchanged), TestMatMulOneWorkerSameBits.
  • Kept: dot, mat-vec, blocks8, the float32 elementwise and sqrt kernels,
    max/min, the thread pool, hwcap (CPU detection of those kernels).
  • Docs: README, SECURITY.md (kernel audit now go-blas's, dated entry),
    docs/plan-ndarray.md.

Proofs

  • sum_.s: the old file is exactly the regenerated one plus go-blas
    v0.1.0's; every block of both is byte-identical to the old file's, in order,
    headers equal, on all six architectures (hasFMA is in both files on amd64,
    by design: the dot kernel needs it).
  • Bits: the FNV-1a hash of MatMul's result bits, 63 cases (float64 and
    float32; contiguous, transposed A and B, decimated A and B rows, sub-block,
    reversed columns; sizes 1x1x1 to 300x301x302), is identical between the base
    commit and this branch, on arm64 (Apple) and on amd64 (EPYC 7773X, FMA).
  • Full suite passes, linalg and TestAccuracyBoundMatMul included; vet on
    amd64, arm64, 386, arm, loong64, ppc64le, riscv64, s390x; 0 uncovered blocks
    on arm64.

Performance

cfarm420 (EPYC 7773X), one core (GOMAXPROCS=1, taskset -c 9), base binary
against branch binary interleaved, 5 rounds, load 2.1 to 2.9, medians. Allocation
counts equal everywhere; bytes equal.

MatMul base branch ratio
n=64 17.4 us 18.4 us 1.06x
n=128 120.5 us 123.2 us 1.02x
n=256 964 us 986 us 1.02x
n=512 8.98 ms 9.02 ms 1.00x
n=1024 79.1 ms 80.0 ms 1.01x
float32 n=64 8.46 us 8.86 us 1.05x
float32 n=256 439 us 430 us 0.98x
float32 n=1024 39.7 ms 38.7 ms 0.97x
transposed A n=1024 55.4 ms 52.4 ms 0.95x
a[:, ::2] n=64 / 256 / 1024 24.4 us / 914 us / 52.9 ms 24.7 us / 909 us / 52.0 ms 1.01x / 0.99x / 0.98x

The a[:, ::2] case was 1.5x slower through go-blas v0.2.0 at n=1024 as a copy
and 1.15-1.45x slower at small n through its element-wise scaling of C; v0.2.1
(go-blas/blas#3) removed both.

Open defect: n=64 float64 is still 1.06x (1 us) and float32 1.05x slower,
beyond the noise of those runs: it is the entry's own cost (argument checks,
operand normalisation) beside an 17 us product. Not fixed here; it needs a
profile of DgemmStrided at small sizes.

Refs #118, go-blas/blas#1, go-blas/blas#3

🤖 Generated with Claude Code

tannevaled and others added 4 commits November 15, 2026 15:14
The packed GEMM (gemm.go, gemm_<arch>.go, gemm32_*, gemmua_other, the GEMM
micro-kernels of sum_<arch>.s and their generators) moved to
github.com/go-blas/blas in go-blas/blas#1, byte for byte. ndarray now calls
DgemmStrided / SgemmStrided through internal/blasmm, which reads an operand
through its strides (a transposed or a[:, ::2] view is not copied; a broadcast
or reversed one is) and picks the float32 path with HasSgemmKernel.

Kept here: the dot kernels (moved from gemm_<arch>.go to dot_<arch>.go,
unchanged), mat-vec and Dot1D (matvec.go), blocks8, the float32 elementwise
and sqrt kernels, max/min, the thread pool and hwcap. sum_<arch>.s is
regenerated without the GEMM blocks: every other block is byte-identical.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
v0.2.1 scales C a contiguous run at a time in DgemmStrided/SgemmStrided
(go-blas/blas#3): MatMul through v0.2.0 was 1.15 to 1.45x slower than the GEMM
it replaces at n = 64..256 because of that loop.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…s v0.2.1

TestMatMulLayouts multiplies 16 shapes x 11 x 11 layouts (contiguous,
transposed, reversed, broadcast, decimated with an offset, extent-1 axes with
garbage strides) in float32 and float64 against a naive loop within a bound,
and requires the same bits from the heap and from a NaN-filled Workspace and at
GOMAXPROCS 1 and N. TestMatMulConcurrentViews runs 16 goroutines on the same
views. -short drops the three largest shapes (0.6 s against 12 s).

SECURITY.md's 2026-11-15 entry said go-blas/blas v0.2.0; go.mod pins v0.2.1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… its timeout

The two largest shapes (1.3M and 2.1M multiplications per layout pair) took
the test job past 10 minutes under -race. They are replaced by {100, 40, 70},
which is past the parallel threshold (m*n >= 6000), and the naive reference
runs only in the heap run, the workspace and one-worker runs being compared
with it by hash: 25 s under -race on a loaded machine, 0.6 s with -short.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 208bdac into main Nov 15, 2026
13 checks passed
@tannevaled
tannevaled deleted the refactor/use-go-blas branch November 15, 2026 16:27
tannevaled added a commit to go-ndarray/docs that referenced this pull request Nov 15, 2026
Say the GEMM code moved to go-blas/blas byte for byte (no bit-identity claim),
label the perf table as measured at v0.41.0, and mirror SECURITY.md verbatim
("unreleased" in both audit entries, as the source has it).

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit to go-ndarray/go-ndarray.github.io that referenced this pull request Nov 15, 2026
The blurb listed the float32 RVV kernels (6x16, 8x16); the float64 one is 6x8,
as ndarray v0.42.0 README says. The other entries match that README.

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit to go-ndarray/docs that referenced this pull request Nov 15, 2026
* docs: ndarray v0.42.0

Every mirrored page, hugo.yaml (version and baseURL /docs/0.42/) and the
README now point at v0.42.0. roadmap.md (docs/plan-ndarray.md) and security.md
(SECURITY.md) are re-synced from v0.42.0, since their sources changed: the
GEMM is github.com/go-blas/blas v0.2.1, through internal/blasmm, and the
audit log gains the 2026-11-15 entry. The home page says the same. The other
mirrored sources (dtypes, linalg, performance, random, accuracy) are unchanged
between v0.41.3 and v0.42.0 and are only relabelled. performance-history.md
is history and is left alone.

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: fix claims in the v0.42.0 bump

Say the GEMM code moved to go-blas/blas byte for byte (no bit-identity claim),
label the perf table as measured at v0.41.0, and mirror SECURITY.md verbatim
("unreleased" in both audit entries, as the source has it).

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit to go-ndarray/go-ndarray.github.io that referenced this pull request Nov 15, 2026
* landing: ndarray v0.42.0

The card says "library · v0.42.0". The ndarray blurb, the "Linear algebra"
family and the hero text say MatMul and Dot run on go-blas/blas v0.2.1, with
the same kernels as before: ndarray stopped carrying its own GEMM in v0.42.0.
The SIMD card is about ndarray's own kernels (sum, elementwise, dot), still
go-asmgen, and stays as it was. The playground builds against ndarray v0.42.0
(go-blas/blas v0.2.1 comes with it); its tests pass.

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* landing: correct the float64 RVV micro-kernel size

The blurb listed the float32 RVV kernels (6x16, 8x16); the float64 one is 6x8,
as ndarray v0.42.0 README says. The other entries match that README.

Refs go-ndarray/ndarray#122

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit that referenced this pull request Nov 15, 2026
…udit entries

docs/linalg.md and docs/design-linalg.md named the ndarray package's own
GEMM; since #122 it is go-blas/blas. SECURITY.md: the go-blas entry is
v0.42.0 (#122, commit 208bdac, tag v0.42.0 contains it); the Choice gather
entry is v0.40.2 (#115, 8ac94fb, first tag containing it, Refs #103).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
tannevaled added a commit that referenced this pull request Nov 15, 2026
… v0.23.0; Round is half to even, add RoundHalfAway (#123)

* perf: float64 Abs, Neg, Square, Floor, Ceil on go-simd/floats v0.23.0 kernels

Bump go-simd/floats to v0.23.0 and route the contiguous float64 case of
Abs, Neg, Square, Floor and Ceil through its SIMD kernels, still split
across cores above kernels.ParThreshold. Results are bit for bit those of
math.Abs, -x, x*x, math.Floor and math.Ceil (a signalling NaN may come
back quieted). Round is unchanged: it rounds half away from zero, which
floats.RoundEven does not.

On 10^5 float64, one core of cfarm420: Abs 244-258 -> 70-75 us, Neg
243-249 -> 72-75, Square 243-247 -> 72-80, Floor 432-457 -> 73-81, Ceil
437-464 -> 72-81 (result allocation included).

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: GEMM now in go-blas/blas; release tags for the two unreleased audit entries

docs/linalg.md and docs/design-linalg.md named the ndarray package's own
GEMM; since #122 it is go-blas/blas. SECURITY.md: the go-blas entry is
v0.42.0 (#122, commit 208bdac, tag v0.42.0 contains it); the Choice gather
entry is v0.40.2 (#115, 8ac94fb, first tag containing it, Refs #103).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* feat!: Round is half to even (np.round) on the RoundEven kernel; add RoundHalfAway

Round now rounds exact halves to the even neighbour on every dtype
(float64 on the go-simd/floats RoundEven kernel; float32 and each part of
a complex number through math.RoundToEven), as np.round with no decimals.
The old half-away-from-zero behaviour (math.Round) is RoundHalfAway, on
the scalar path, on the same dtypes. Integers and bools are unchanged by
both. There is no decimals variant.

Tests: halves and the edge table for both against math.RoundToEven and
math.Round; Abs and Neg now compared with strict NaN bits, the others
with is-NaN only. On 10^5 float64, cfarm420 one core: Round 325-340 ->
71-80 us; RoundHalfAway (control) 324-345 us.

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* docs: exempt Array.RoundHalfAway from the perf table (no NumPy counterpart)

TestPerf requires every public function to have a row or an exemption.
RoundHalfAway rounds halves away from zero; np.round is half to even,
which is Array.Round, so there is nothing to time it against.

Refs #118

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant