Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,12 @@ void exec_wmma_f32_mixed(auto &cu, uint32_t M, uint32_t N, uint32_t K, uint32_t
ExtractA ea, ExtractB eb, uint32_t const_acc = ACC_FROM_VGPR,
uint32_t c_modifier = 0, uint32_t wave_size = WMMA_WAVE32) {
require_gfx12_wmma_wave_size(wave_size);
// Keep the CDNA5 BF16 dot separate from C so cancelling products do not
// discard C's low bits. This grouping fixes Triton scaled-dot numerics;
// the physical CDNA5 reduction order has not been qualified.
const bool dot_before_c = std::is_same_v<std::remove_cvref_t<ExtractA>, ExtractBf16> &&
std::is_same_v<std::remove_cvref_t<ExtractB>, ExtractBf16> && M == 16 &&
N == 16 && K == 32 && wave_size == WMMA_WAVE32;
struct Result {
uint32_t reg;
uint32_t lane;
Expand All @@ -1750,11 +1756,14 @@ void exec_wmma_f32_mixed(auto &cu, uint32_t M, uint32_t N, uint32_t K, uint32_t
? std::bit_cast<float>(const_acc)
: std::bit_cast<float>(RegisterAccess(cu).read_vgpr(s2 + out.reg, out.lane));
acc = apply_wmma_c_modifier(acc, c_modifier);
float dot = dot_before_c ? 0.0f : acc;
for (uint32_t k = 0; k < K; ++k) {
auto al = gfx12_wmma_a_input_loc(wave_size, M, K, row, k, a_bits, b_bits);
auto bl = gfx12_wmma_b_input_loc(wave_size, N, K, col, k, a_bits, b_bits);
acc += ea(cu, s0, al) * eb(cu, s1, bl);
const float a = ea(cu, s0, al), b = eb(cu, s1, bl);
dot = dot_before_c ? std::fma(a, b, dot) : dot + a * b;
}
acc = dot_before_c ? dot + acc : dot;
results.push_back({out.reg, out.lane, std::bit_cast<uint32_t>(acc)});
}
}
Expand All @@ -1781,10 +1790,12 @@ void exec_wmma_f32_mixed(auto &cu, uint32_t M, uint32_t N, uint32_t K, uint32_t
for (uint32_t col = 0; col < N; ++col) {
auto out = gfx12_wmma_output_loc_32(wave_size, M, N, row, col);
Cbuf[row * stride + col] =
apply_wmma_c_modifier((const_acc != ACC_FROM_VGPR)
? std::bit_cast<float>(const_acc)
: std::bit_cast<float>(reads.acc->lane(out.reg, out.lane)),
c_modifier);
dot_before_c ? 0.0f
: apply_wmma_c_modifier(
(const_acc != ACC_FROM_VGPR)
? std::bit_cast<float>(const_acc)
: std::bit_cast<float>(reads.acc->lane(out.reg, out.lane)),
c_modifier);
}
for (uint32_t row = 0; row < M; ++row)
for (uint32_t k = 0; k < K; ++k)
Expand All @@ -1798,6 +1809,12 @@ void exec_wmma_f32_mixed(auto &cu, uint32_t M, uint32_t N, uint32_t K, uint32_t
for (uint32_t row = 0; row < M; ++row)
for (uint32_t col = 0; col < N; ++col) {
auto out = gfx12_wmma_output_loc_32(wave_size, M, N, row, col);
if (dot_before_c) {
const float acc = (const_acc != ACC_FROM_VGPR)
? std::bit_cast<float>(const_acc)
: std::bit_cast<float>(reads.acc->lane(out.reg, out.lane));
Cbuf[row * stride + col] += apply_wmma_c_modifier(acc, c_modifier);
}
results.push_back({out.reg, out.lane, std::bit_cast<uint32_t>(Cbuf[row * stride + col])});
}
}
Expand Down Expand Up @@ -2360,17 +2377,19 @@ inline void exec_wmma_f32_16x16x32_bf16(auto &cu, uint32_t dst, uint32_t s0, uin
auto bl = wmma_input_loc(N, K, col, k, in_bits);
B_buf[k * N + col] = B_f32[(bl.vgpr_offset * wf + bl.lane) * 2 + bl.sub_element];
}
// Dense 16x32 * 32x16 -> 16x16 matmul, 16-lane stdx FMA per row.
// Use the same dot-before-C grouping as the generic BF16 path.
for (uint32_t row = 0; row < M; ++row) {
util::native<float> c_row;
c_row.copy_from(&C_buf[row * N], util::stdx::vector_aligned);
util::native<float> dot_row(0.0f);
for (uint32_t k = 0; k < K; ++k) {
util::native<float> a_bcast(A_buf[row * K + k]);
util::native<float> b_row;
b_row.copy_from(&B_buf[k * N], util::stdx::vector_aligned);
c_row = util::stdx::fma(a_bcast, b_row, c_row);
dot_row = util::stdx::fma(a_bcast, b_row, dot_row);
}
c_row.copy_to(&C_buf[row * N], util::stdx::vector_aligned);
dot_row += c_row;
dot_row.copy_to(&C_buf[row * N], util::stdx::vector_aligned);
}
// Scatter directly back to VGPRs (no Result staging vector).
for (uint32_t row = 0; row < M; ++row)
Expand Down Expand Up @@ -3390,19 +3409,23 @@ inline void exec_wmma_bf16f32_16x16x32_bf16(auto &cu, uint32_t dst, uint32_t s0,
(const_acc != ACC_FROM_VGPR)
? std::bit_cast<float>(const_acc)
: std::bit_cast<float>(RegisterAccess(cu).read_vgpr(s2 + cout.reg, cout.lane));
float acc = apply_wmma_c_modifier(initial_acc, c_modifier);
// Match the F32-output instruction's dot-before-C grouping, then
// apply this instruction's final BF16 conversion.
float dot = 0.0f;
for (uint32_t k = 0; k < K; ++k) {
auto al = wmma_input_loc(M, K, row, k, in_bits);
auto bl = wmma_input_loc(N, K, col, k, in_bits);
acc = std::fma(extract_bf16(cu, s0, al), extract_bf16(cu, s1, bl), acc);
dot = std::fma(extract_bf16(cu, s0, al), extract_bf16(cu, s1, bl), dot);
}
float acc = dot + apply_wmma_c_modifier(initial_acc, c_modifier);
if (std::isnan(acc)) {
acc = apply_wmma_c_modifier(initial_acc, c_modifier);
dot = 0.0f;
for (uint32_t k = 0; k < K; ++k) {
auto al = wmma_input_loc(M, K, row, k, in_bits);
auto bl = wmma_input_loc(N, K, col, k, in_bits);
acc = matrix_fma(extract_bf16(cu, s0, al), extract_bf16(cu, s1, bl), acc);
dot = matrix_fma(extract_bf16(cu, s0, al), extract_bf16(cu, s1, bl), dot);
}
acc = matrix_fma(dot, 1.0f, apply_wmma_c_modifier(initial_acc, c_modifier));
}
auto out = wmma_output_loc_16(M, N, row, col);
results.push_back({out.reg, out.lane, out.sub_element, util::f32_to_bf16(acc)});
Expand Down Expand Up @@ -3484,11 +3507,13 @@ inline void exec_wmma_bf16f32_16x16x32_bf16(auto &cu, uint32_t dst, uint32_t s0,
for (uint32_t row = 0; row < M; ++row) {
util::native<float> c_row;
c_row.copy_from(&C_buf[row * N], util::stdx::vector_aligned);
util::native<float> dot_row(0.0f);
for (uint32_t k = 0; k < K; ++k) {
util::native<float> b_row;
b_row.copy_from(&B_buf[k * N], util::stdx::vector_aligned);
c_row = util::native_fma(util::native<float>(A_buf[row * K + k]), b_row, c_row);
dot_row = util::native_fma(util::native<float>(A_buf[row * K + k]), b_row, dot_row);
}
c_row = dot_row + c_row;
const uint64_t nan_lanes = util::simd_mask_to_bits(util::stdx::isnan(c_row));
c_row.copy_to(&C_buf[row * N], util::stdx::vector_aligned);
// A packed host FMA may propagate a different NaN operand than scalar
Expand All @@ -3498,10 +3523,10 @@ inline void exec_wmma_bf16f32_16x16x32_bf16(auto &cu, uint32_t dst, uint32_t s0,
while (pending_nan_lanes != 0) {
const uint32_t col = static_cast<uint32_t>(std::countr_zero(pending_nan_lanes));
pending_nan_lanes &= pending_nan_lanes - 1;
float acc = initial_acc(row, col);
float dot = 0.0f;
for (uint32_t k = 0; k < K; ++k)
acc = matrix_fma(A_buf[row * K + k], B_buf[k * N + col], acc);
C_buf[row * N + col] = acc;
dot = matrix_fma(A_buf[row * K + k], B_buf[k * N + col], dot);
C_buf[row * N + col] = matrix_fma(dot, 1.0f, initial_acc(row, col));
}
}

Expand Down
27 changes: 18 additions & 9 deletions emulation/rocjitsu/tests/instruction_execution_harness_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3908,9 +3908,9 @@ TEST(Gfx1250WmmaTest, Bf16F32K32Bf16PreservesAccumulatorLayout) {
wf->halt();
}

TEST(Gfx1250WmmaTest, Bf16F32K32Bf16ScalarUsesFusedMac) {
amdgpu::GpuMemory gpu_mem("gfx1250_wmma_bf16f32_fused_mem");
amdgpu::L2Cache l2("gfx1250_wmma_bf16f32_fused_l2");
TEST(Gfx1250WmmaTest, Bf16F32K32Bf16MatchesF32DotOverflow) {
amdgpu::GpuMemory gpu_mem("gfx1250_wmma_bf16f32_dot_mem");
amdgpu::L2Cache l2("gfx1250_wmma_bf16f32_dot_l2");

amdgpu::ComputeUnitCore::Config cfg{};
cfg.arch = ROCJITSU_CODE_ARCH_CDNA5;
Expand Down Expand Up @@ -3945,12 +3945,21 @@ TEST(Gfx1250WmmaTest, Bf16F32K32Bf16ScalarUsesFusedMac) {
const auto c = amdgpu::wmma_output_loc_32(16, 16, /*row=*/0, /*col=*/0);
cu->write_vgpr(c_base + c.reg, c.lane, 0xFF7FFFFFu); // -FLT_MAX

ForceScalarGuard force_scalar(/*force_scalar=*/true);
amdgpu::exec_wmma_bf16f32_16x16x32_bf16(*cu, d_base, a_base, b_base, c_base);

const auto out = amdgpu::wmma_output_loc_16(16, 16, /*row=*/0, /*col=*/0);
const uint32_t word = cu->read_vgpr(d_base + out.reg, out.lane);
EXPECT_EQ(static_cast<uint16_t>(word >> (16 * out.sub_element)), 0x7380u);
// Both forms round the dot before adding C. Its overflow remains infinite,
// even though fusing this product with -FLT_MAX would give a finite result.
for (bool scalar : {false, true}) {
SCOPED_TRACE(testing::Message() << "scalar=" << scalar);
ForceScalarGuard force_scalar(scalar);
amdgpu::exec_wmma_bf16f32_16x16x32_bf16(*cu, d_base, a_base, b_base, c_base);
const auto out = amdgpu::wmma_output_loc_16(16, 16, /*row=*/0, /*col=*/0);
const uint32_t word = cu->read_vgpr(d_base + out.reg, out.lane);
EXPECT_EQ(static_cast<uint16_t>(word >> (16 * out.sub_element)), 0x7F80u);

const uint32_t f32_d_base = vb + 50;
amdgpu::exec_wmma_f32_16x16x32_bf16(*cu, f32_d_base, a_base, b_base, c_base);
const auto f32_out = amdgpu::wmma_output_loc_32(16, 16, /*row=*/0, /*col=*/0);
EXPECT_EQ(cu->read_vgpr(f32_d_base + f32_out.reg, f32_out.lane), 0x7F800000u);
}

if (!wf->is_halted())
wf->halt();
Expand Down
74 changes: 73 additions & 1 deletion emulation/rocjitsu/tests/packed_wmma_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// SPDX-License-Identifier: MIT

/// @file packed_wmma_test.cpp
/// @brief Packed WMMA raw hardware fixtures across architectures and wave sizes.
/// @brief WMMA hardware fixtures and numerical regression checks.

#include "fixtures/float_dot/packed_operand_cases.h"
#include "fixtures/float_dot/packed_wmma_cases.h"
#include "rocjitsu/isa/arch/amdgpu/generated/cdna5/builders.h"
#include "rocjitsu/isa/arch/amdgpu/generated/cdna5/opcodes.h"
#include "rocjitsu/isa/arch/amdgpu/generated/rdna3/builders.h"
#include "rocjitsu/isa/arch/amdgpu/generated/rdna3/opcodes.h"
#include "rocjitsu/isa/arch/amdgpu/generated/rdna4/builders.h"
Expand All @@ -19,7 +21,9 @@
#include "util/simd_test_hooks.h"

#include <array>
#include <bit>
#include <cfenv>
#include <cmath>
#include <cstdint>
#include <gtest/gtest.h>
#include <memory>
Expand Down Expand Up @@ -71,6 +75,74 @@ class DotMachine {
uint32_t base;
};

// Regression for the dense BF16 reference used by Triton's test_scaled_dot
// and its BF16-output counterpart with the same F32 accumulator.
// These products cancel exactly. Grouping them separately preserves a small C;
// this test does not establish the physical CDNA5 accumulation order.
TEST(Cdna5Wmma, Bf16CancellationPreservesIncomingC) {
HostState restore;
DotMachine machine(ROCJITSU_CODE_ARCH_CDNA5);
constexpr std::array<float, 32> products = {3456, 4608, 5376, 3072, -3840, 2816, -27648, 3072,
-1920, 1024, -6656, 1792, 20480, 7680, 0, 1024,
-43008, 3072, -7168, 1280, 14336, 2560, 3456, 1536,
-9216, 1536, -5760, 1408, 10240, 896, 7168, 3328};
constexpr float incoming = -0.012669563293457031f;
for (bool scalar : {false, true})
for (bool packed : {false, true})
for (uint32_t dst : {0u, 4u, 8u, 12u, 16u, 20u, 24u})
for (uint16_t src2 : {uint16_t{272}, uint16_t{128}, uint16_t{242}})
for (uint8_t modifier = 0; modifier < 4; ++modifier) {
SCOPED_TRACE(testing::Message()
<< "scalar=" << scalar << " packed=" << packed << " dst=" << dst
<< " src2=" << src2 << " modifier=" << unsigned(modifier));
util::set_force_scalar_for_testing(scalar);
for (uint32_t lane = 0; lane < 32; ++lane) {
for (uint32_t reg = 0; reg < 32; ++reg)
machine.set(reg, lane, 0xa55a1234u ^ (lane << 16) ^ reg);
for (uint32_t reg = 0; reg < 8; ++reg) {
const uint32_t k = (reg / 4) * 16 + (lane / 16) * 8 + (reg % 4) * 2;
machine.set(reg, lane, 0x3f803f80u); // A = 1 in BF16.
machine.set(8 + reg, lane,
(std::bit_cast<uint32_t>(products[k]) >> 16) |
(std::bit_cast<uint32_t>(products[k + 1]) & 0xffff0000u));
machine.set(16 + reg, lane, std::bit_cast<uint32_t>(incoming));
}
}
std::array<uint32_t, 32 * 32> before;
for (uint32_t reg = 0; reg < 32; ++reg)
for (uint32_t lane = 0; lane < 32; ++lane)
before[reg * 32 + lane] = machine.get(reg, lane);
float expected = src2 == 272 ? incoming : src2 == 128 ? 0.0f : 1.0f;
if (modifier & 2)
expected = std::fabs(expected);
if (modifier & 1)
expected = -expected;
// Adding an exactly zero dot produces +0 for either signed zero C.
expected += 0.0f;
uint32_t expected_word = std::bit_cast<uint32_t>(expected);
if (packed) {
const uint32_t half = expected_word >> 16;
expected_word = half | (half << 16);
}
const uint32_t dst_regs = packed ? 4 : 8;
machine.execute(cdna5::build_vop3p(packed ? cdna5::kVWmmaBf16f3216x16x32Bf16Vop3p
: cdna5::kVWmmaF3216x16x32Bf16Vop3p,
{.vdst = uint8_t(dst),
.neg_hi = uint8_t((modifier & 2) << 1),
.src0 = 256,
.src1 = 264,
.src2 = src2,
.opsel_hi = 3,
.neg = uint8_t((modifier & 1) << 2)}));
for (uint32_t reg = 0; reg < 32; ++reg)
for (uint32_t lane = 0; lane < 32; ++lane)
ASSERT_EQ(machine.get(reg, lane), reg >= dst && reg < dst + dst_regs
? expected_word
: before[reg * 32 + lane])
<< "reg=" << reg << " lane=" << lane;
}
}

std::array<uint32_t, 2> packed_encoding(bool gfx12, bool bf16, uint32_t dst, uint32_t high,
uint16_t src2 = 272, uint8_t neg = 0, uint8_t neg_hi = 0) {
if (gfx12)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ TEST(WmmaSimdExact, Bf16F32MixedPackLayout) {
}
}

// Multiplication overflows in isolation, but the hardware-fused operation is
// finite after adding -FLT_MAX. This witnesses the single-rounding contract
// in both the forced-scalar oracle and the vector path.
TEST(WmmaSimdExact, Bf16F32MixedFusedOverflow) {
// The selected dot-before-C model rounds the dot to F32 before adding C.
// An overflowing dot therefore remains infinite even when a product fused
// with C could be finite. This is a simulator grouping choice.
TEST(WmmaSimdExact, Bf16F32MixedDotOverflow) {
SKIP_IF_NO_SIMD();
if (util::native<float>::size() != 16)
GTEST_SKIP() << "the BF16F32 fast path requires 16-lane native SIMD";
Expand All @@ -449,7 +449,7 @@ TEST(WmmaSimdExact, Bf16F32MixedFusedOverflow) {
fx.cu->write_vgpr(fx.vbase + ACC + c.reg, c.lane, 0xFF7FFFFFu); // -FLT_MAX
};
expect_bit_exact(
"wmma_bf16f32_fused_overflow", Mode::MaxFinite, fx, reseed,
"wmma_bf16f32_dot_overflow", Mode::MaxFinite, fx, reseed,
[&] {
amdgpu::exec_wmma_bf16f32_16x16x32_bf16(*fx.cu, fx.vbase + BF16_DST, fx.vbase + S0,
fx.vbase + S1, fx.vbase + ACC,
Expand All @@ -459,7 +459,7 @@ TEST(WmmaSimdExact, Bf16F32MixedFusedOverflow) {
ASSERT_FALSE(testing::Test::HasFatalFailure());
const auto out = amdgpu::wmma_output_loc_16(16, 16, /*row=*/0, /*col=*/0);
const uint32_t word = fx.cu->read_vgpr(fx.vbase + BF16_DST + out.reg, out.lane);
EXPECT_EQ(static_cast<uint16_t>(word >> (16 * out.sub_element)), 0x7380u);
EXPECT_EQ(static_cast<uint16_t>(word >> (16 * out.sub_element)), 0x7F80u);
}

// --- dense fp8/bf8 inputs, all four A/B combos, K=64 and K=128, f32/f16 out ---
Expand Down
Loading