NVIDIA / NVIDIA/cutlass

Matrix<R,C>::at() strides by row count: wrong values and out-of-bounds access for all non-square shapes (diagonal/hcat/vcat affected)

Open
#3,516 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

CUTLASS C++
Dominant language
C++
Stars
10.5k
Forks
2.1k
Avg merge
3d 11h
Merged PRs (30d)
7

Description

Description

The value-semantics Matrix<R, C> specializations in include/cutlass/matrix.h implement at(i, j) with a row-count stride instead of a column-count stride:

Element at(int i, int j) const {
    return data[i * kRows + j];   // should be i * kColumns
}

Every non-square specialization is affected (square ones coincide). Consequences for non-square shapes:

  • wrong element returned/stored whenever row >= 1 and R != C;
  • out-of-bounds reads and writes when R > C (e.g. Matrix<float,4,1>::at(3,0) touches data[12] of a 4-element array);
  • GCC at -O2 even warns "iteration invokes undefined behavior" on loops over at() for 3x2/4x2/4x3.

Downstream members built on at() inherit the defect:

  • diagonal() returns wrong values for every non-square shape and performs OOB accesses for R > C shapes (e.g. Matrix<float,3,2>::diagonal() writes 3 entries into its 2-element result while reading data[8] of 6); AddressSanitizer flags it: heap-buffer-overflow READ ... Matrix<float,3,2>::diagonal() const matrix.h.
  • the element-wise forms of hcat/vcat read their operands through at(), so concatenations are wrong for any operand with more than one row and non-square shape. A full differential over all 15 shapes shows 28 hcat-lhs + 18 hcat-rhs + 19 vcat-lhs mismatches plus transpose round-trip failures (t.transpose() != original), all confined to non-square operands.

Everything else in the header (products, transpose storage maps, slice_/set_slice_, dot/sum/norm/trace, uniform constructors) sweeps clean against reference implementations; the defects are exactly the at()-derived paths. No in-tree caller exercises these members on non-square matrices today (test/unit/core/matrix.cu is 4x4 only), so this has stayed latent, but the class is public API.

Suggested fix

Regenerate the specializations with at() striding by kColumns; make diagonal() emit min(kRows, kColumns) entries from data[i * kColumns + i] into a matching-size result; regenerate the element-wise hcat/vcat statics (or index operands directly). A unit test covering at/diagonal/hcat/vcat on non-square shapes would pin it.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in include/cutlass/matrix.h, focusing on the value-semantics Matrix<R,C> specializations and their at(), diagonal(), hcat(), and vcat() members. Read test/unit/core/matrix.cu, then add coverage for non-square shapes and run the matrix tests with AddressSanitizer. Done means correct indexing and matching-size diagonal results without out-of-bounds accesses, with concatenation behavior verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design, testing
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.