diff --git a/projects/hipblaslt/tensilelite/include/Tensile/ContractionProblemPredicates.hpp b/projects/hipblaslt/tensilelite/include/Tensile/ContractionProblemPredicates.hpp index bf638f5c6c00..36c1f7190fa3 100755 --- a/projects/hipblaslt/tensilelite/include/Tensile/ContractionProblemPredicates.hpp +++ b/projects/hipblaslt/tensilelite/include/Tensile/ContractionProblemPredicates.hpp @@ -1555,12 +1555,35 @@ namespace TensileLite return "BufferStoreOffsetLimitCheck"; } - // The min operator is used to handle cases where size_N is smaller than the value(usually is MacroTile1) + // Each BufferStore=True kernel writes D through a buffer resource + // descriptor whose 32-bit num_records field bounds every store: an + // offset at or past it is discarded by the hardware without raising a + // fault. allocPostLoopSrd in KernelWriterAssembly.py programs that + // field with the BufferOOB sentinel, so this threshold has to be the + // same number the generator emits. When it is larger, this predicate + // reports a shape as supported that the kernel will only partly write. + // + // That agreement only holds for generated kernels. A hand-written + // kernel under Tensile/CustomKernels sets its own BufferOOB and + // nothing checks it against this value, so adding or changing one + // means confirming by hand that its sentinel is at least this large. + // A kernel with a smaller sentinel drops stores this predicate admits. + static constexpr uint64_t BufferOOBBytes = 0xfffff000ull; + + // Each workgroup re-bases the descriptor along N before storing (see + // computeStoreSrdStart), so the extent that has to fit is one + // MacroTile1 of columns rather than all of D. min() covers the case + // where N is smaller than MacroTile1. + static uint64_t storeExtentBytes(ContractionProblemGemm const& problem, size_t value) + { + return multiplyElementSize( + problem.d().strides()[1] * std::min(value, problem.d().sizes()[1]), + problem.d().elementBytes()); + } + virtual bool operator()(ContractionProblemGemm const& problem) const override { - const uint64_t TWO_POW_32 = 4294967296; - return multiplyElementSize(problem.d().strides()[1] * std::min(value, problem.d().sizes()[1]), problem.d().elementBytes()) - < TWO_POW_32; + return storeExtentBytes(problem, value) < BufferOOBBytes; } virtual std::string toString() const override @@ -1573,8 +1596,12 @@ namespace TensileLite { bool rv = (*this)(problem); std::ostringstream details; - details << "D:" << problem.d().strides()[1] << "*" - << problem.d().elementBytes() << "*" << value << "<2^32"; + // Reports the same quantity operator() compares, including the + // min() against N. Reading strides()[1] * value directly would + // print a larger number than the one that decided the result + // whenever N is below MacroTile1. + details << "D:" << storeExtentBytes(problem, value) << "<0x" << std::hex + << BufferOOBBytes << std::dec; PredicateDebugger::printRow(stream, rv, this->type(), details.str()); return rv; } diff --git a/projects/hipblaslt/tensilelite/tests/Predicates_test.cpp b/projects/hipblaslt/tensilelite/tests/Predicates_test.cpp index e31388ecf4c7..b7e8878e83b2 100644 --- a/projects/hipblaslt/tensilelite/tests/Predicates_test.cpp +++ b/projects/hipblaslt/tensilelite/tests/Predicates_test.cpp @@ -127,3 +127,135 @@ TEST(Predicates, WorkgroupMappingXCCCheck_FallbackTreatsXCCAs1) problem.setParams().setFallbackStatus(true); EXPECT_TRUE((*pred)(problem)) << "With fallback status, effective XCC=1 so 38 % 1 == 0"; } + +// ---------------------------------------------------------------------------- +// BufferStoreOffsetLimitCheck bounds the byte range a kernel's output stores +// can reach, so that solution selection never offers a shape the kernel would +// only partly write. The bound has to be the BufferOOB sentinel that +// KernelWriterAssembly.py programs as the store descriptor's num_records, +// 0xfffff000. A larger bound admits shapes whose stores the hardware discards +// past that offset, without raising a fault. +// +// Regression coverage for ROCM-31016, reported as +// https://github.com/ROCm/hipBLASLt/issues/2299. +// +// The M values below come from hardware rather than from arithmetic. Measured +// on an MI350X (gfx950) at N = 256, K = 48 in bfloat16: at M = 8,388,607, 22 of +// the 64 algorithms the heuristic offered returned success while leaving part +// of D unwritten, and every one of them had MacroTile1 = 256, which for this +// shape makes one workgroup's store extent span the whole tensor. M = 8,388,599 +// is the largest M in the same geometry where all 64 ran clean. +// ---------------------------------------------------------------------------- + +namespace +{ + // Column-major bf16 D of M x N with ldd == M, which is the layout the + // reported defect used. + TensileLite::ContractionProblemGemm bf16ColumnMajorD(size_t m, size_t n, size_t k = 48) + { + using namespace TensileLite; + return ContractionProblemGemm::GEMM_Strides(false, + false, + rocisa::DataType::BFloat16, + rocisa::DataType::BFloat16, + rocisa::DataType::BFloat16, + rocisa::DataType::BFloat16, + m, + n, + k, + /*batchSize=*/1, + /*lda=*/m, + /*aStride=*/-1, + /*ldb=*/k, + /*bStride=*/-1, + /*ldc=*/m, + /*cStride=*/-1, + /*ldd=*/m, + /*dStride=*/-1, + /*beta=*/0.0); + } +} + +TEST(Predicates, BufferStoreOffsetLimitCheck_PastSentinel_Rejected) +{ + using namespace TensileLite; + // The shape that reproduced the defect. Its store extent lands between the + // sentinel the kernel enforces and the 2^32 the predicate used to compare + // against, so only the corrected threshold rejects it. + constexpr size_t m = 8388607; + constexpr size_t n = 256; + constexpr size_t macroTile1 = 256; + static_assert(m * n * 2 > 0xfffff000ull, "must exceed the sentinel the kernel enforces"); + static_assert(m * n * 2 < 4294967296ull, "must stay under the threshold the predicate used"); + + auto pred = std::make_shared(macroTile1); + EXPECT_FALSE((*pred)(bf16ColumnMajorD(m, n))) + << "M=" << m << " N=" << n << " bf16: the store extent is " << (size_t)m * n * 2 + << " bytes, past the 4294963200-byte sentinel, so 22 of 64 offered algorithms left " + "part of D unwritten on gfx950."; +} + +TEST(Predicates, BufferStoreOffsetLimitCheck_BelowSentinel_Accepted) +{ + using namespace TensileLite; + // The largest shape in this geometry that the kernel writes in full, and + // the control the hardware measurement used: all 64 offered algorithms ran + // clean here. Rejecting it would cost solutions for no reason, so this + // holds the accepting side of the boundary one shape away from the test + // above. + constexpr size_t m = 8388599; + constexpr size_t n = 256; + constexpr size_t macroTile1 = 256; + static_assert(m * n * 2 < 0xfffff000ull, "must stay within the sentinel"); + + auto pred = std::make_shared(macroTile1); + EXPECT_TRUE((*pred)(bf16ColumnMajorD(m, n))) + << "M=" << m << " N=" << n << " bf16: extent " << (size_t)m * n * 2 + << " bytes fits within the sentinel and must remain selectable."; +} + +TEST(Predicates, BufferStoreOffsetLimitCheck_ExactlyAtSentinel_RejectedConservatively) +{ + using namespace TensileLite; + // One shape sits exactly on the sentinel: its last store begins at + // extent - 2 and so is still in range, but the comparison is strict and + // rejects it. Losing a single extent value is worth not having to reason + // about how wide the final store of a tile is, which varies with the + // vector width a solution uses. Recorded here so the choice is visible + // rather than looking like an off-by-one. + constexpr size_t n = 256; + constexpr size_t macroTile1 = 256; + constexpr size_t m = 0xfffff000ull / (n * 2); + static_assert(m * n * 2 == 0xfffff000ull, "extent lands exactly on the sentinel"); + + auto pred = std::make_shared(macroTile1); + EXPECT_FALSE((*pred)(bf16ColumnMajorD(m, n))); +} + +TEST(Predicates, BufferStoreOffsetLimitCheck_ReportedShape_Accepted) +{ + using namespace TensileLite; + // The shape in the original report. It was rejected by the hardware on + // ROCm 7.2.x, where the kernel's sentinel was 0x80000000, and runs + // correctly now that the sentinel is 0xfffff000. It must stay selectable: + // rejecting it would turn a fixed case back into a lost solution. + auto pred = std::make_shared(256); + EXPECT_TRUE((*pred)(bf16ColumnMajorD(5326464, 256))); +} + +TEST(Predicates, BufferStoreOffsetLimitCheck_NarrowOutputUsesItsOwnWidth) +{ + using namespace TensileLite; + // A workgroup re-bases along N, so the extent is one MacroTile1 of columns. + // When N is smaller than MacroTile1 the tensor supplies the width, and a + // tall output with only 8 columns stays selectable even though multiplying + // by MacroTile1 would put it past the sentinel. + constexpr size_t n = 8; + constexpr size_t macroTile1 = 256; + constexpr size_t m = 100000000; + static_assert(m * n * 2 < 0xfffff000ull, "the real extent fits"); + static_assert(m * macroTile1 * 2 > 0xfffff000ull, "ignoring min() would not"); + + auto pred = std::make_shared(macroTile1); + EXPECT_TRUE((*pred)(bf16ColumnMajorD(m, n))); +}