Archived
fix: preserve BF16 W4 CPU narrowing semantics
This commit is contained in:
1 parent
da8c95d4d9
commit
d947d7139e
4 files changed
+113
-7
No files matched your search
@@ -4063,6 +4063,7 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
static const Grid kGrids[] = {
|
||||
{ "w2a8", RAD_U2, 0, 1 }, { "w4a8_asym", RAD_U4, 0, 1 },
|
||||
{ "w4a16_asym", RAD_U4, 2, 1 }, { "w4a16_asym", RAD_U4, 2, 2 },
|
||||
{ "w4a16_asym", RAD_U4, 3, 1 }, /* BF16 input, oracle is the former F16 cast */
|
||||
{ "w5a16", RAD_U5, 2, 1 }, { "w5a16", RAD_U5, 2, 3 },
|
||||
{ "w4a8i8_asym", RAD_U4, 1, 1 }, { "w4a8i8_asym", RAD_U4, 1, 2 },
|
||||
{ "w5a8i8", RAD_U5, 1, 1 }, { "w5a8i8", RAD_U5, 1, 3 },
|
||||
@@ -4085,6 +4086,11 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
const int64_t k = (r * G + c) % 4;
|
||||
return (float)(k == 0 ? 0 : k == 1 ? top : hint(251, r * G + c, 0, top));
|
||||
});
|
||||
if (gd.act == 3) {
|
||||
w.fill(0, [](int64_t, int64_t) { return 1.0f; });
|
||||
w.fill(1, [](int64_t, int64_t) { return 1.0f; });
|
||||
w.fill(2, [](int64_t, int64_t) { return 0.0f; });
|
||||
}
|
||||
const int64_t pb1 = P > 1 ? (P == 3 ? 32 : 16) : 0, pb2 = P == 3 ? 64 : 0;
|
||||
std::vector<RadParam> p = rp({ { "M", M }, { "N", N }, { "K", K }, { "group", GS } },
|
||||
{ { "dtype", gd.dtype } });
|
||||
@@ -4100,6 +4106,7 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
const int64_t AC = P * K;
|
||||
/* The activation as the grid means it, for the oracle, and its operands. */
|
||||
std::vector<float> af((size_t)(M * AC));
|
||||
std::vector<float> bf_input((size_t)(M * AC));
|
||||
std::vector<float> codes((size_t)(M * AC)), sa;
|
||||
if (gd.act == 0) {
|
||||
for (int64_t t = 0; t < M * AC; ++t)
|
||||
@@ -4116,9 +4123,15 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
for (int64_t t = 0; t < M * AC; ++t)
|
||||
af[(size_t)t] =
|
||||
codes[(size_t)t] * sa[(size_t)((t / AC) * P * G + (t % AC) / GS)];
|
||||
} else {
|
||||
} else if (gd.act == 2) {
|
||||
for (int64_t t = 0; t < M * AC; ++t)
|
||||
af[(size_t)t] = rad_f16_to_f32(rad_f32_to_f16(hfill(254)(t)));
|
||||
} else {
|
||||
const float edge = std::ldexp(1.0f + 1.0f / 128.0f, -20);
|
||||
const float bf = rad_bf16_to_f32(rad_f32_to_bf16(edge));
|
||||
const float f16 = rad_f16_to_f32(rad_f32_to_f16(bf));
|
||||
std::fill(bf_input.begin(), bf_input.end(), bf);
|
||||
std::fill(af.begin(), af.end(), f16);
|
||||
}
|
||||
const std::string nm = std::string(gd.dtype) +
|
||||
(P > 1 ? ", " + std::to_string(P) + " parts" : "") + ", M=" +
|
||||
@@ -4156,7 +4169,9 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
add_joined(h, jdt, { N, K }, j);
|
||||
h.dense(RAD_BF16, { M, N }, zero, true).none().none();
|
||||
} else {
|
||||
h.dense(RAD_F16, { M, AC }, [af](int64_t t) { return af[(size_t)t]; }).none();
|
||||
const uint32_t adt = gd.act == 3 ? RAD_BF16 : RAD_F16;
|
||||
const auto& input = gd.act == 3 ? bf_input : af;
|
||||
h.dense(adt, { M, AC }, [input](int64_t t) { return input[(size_t)t]; }).none();
|
||||
h.raw(gd.codes, { N, K }, w.data[0].data());
|
||||
add_joined(h, jdt, { N, K }, j);
|
||||
h.dense(RAD_BF16, { M, N }, zero, true).none().none();
|
||||
@@ -4171,7 +4186,7 @@ void planes_cases(const Check& ck, std::vector<Hand>& hs, Broken& broken) {
|
||||
h.oracle = oracle_gemm(af, AC, core_decode(w), M, N, K, [e1, e2, K](int64_t n) {
|
||||
return (n < e1 ? 0 : n < e2 ? 1 : 2) * K;
|
||||
});
|
||||
h.oracle_tol = 6e-3;
|
||||
h.oracle_tol = gd.act == 3 ? 1e-7 : 6e-3;
|
||||
hs.push_back(std::move(h));
|
||||
}
|
||||
|
||||
|
||||
+23
-1
@@ -838,7 +838,29 @@ static int gemm_zp(const avxp::ZpGrid& zg, const RadTensor* A, const RadTensor*
|
||||
const int64_t acols = parts * K;
|
||||
if (numel(A) < M * acols) return RAD_E_SHAPE;
|
||||
if (A->rank >= 2 && A->shape[A->rank - 1] != acols) return RAD_E_SHAPE;
|
||||
const int64_t ars = row_stride(A, acols), acs = laststride(A);
|
||||
int64_t ars = row_stride(A, acols), acs = laststride(A);
|
||||
|
||||
/* The W4 decode graph supplies BF16 directly so HIP can narrow on load. Keep the previous
|
||||
* explicit BF16->F16 cast semantics for CPU execution before entering the F16 activation grid. */
|
||||
std::vector<uint16_t> narrowed;
|
||||
RadTensor a_f16{};
|
||||
if (zg.act == ZP_F16 && A->dtype == RAD_BF16) {
|
||||
narrowed.resize((size_t)(M * acols));
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t k = 0; k < acols; ++k)
|
||||
narrowed[(size_t)(m * acols + k)] = rad_f32_to_f16(ldt(A, m * ars + k * acs));
|
||||
a_f16 = *A;
|
||||
a_f16.data = narrowed.data();
|
||||
a_f16.dtype = RAD_F16;
|
||||
a_f16.rank = 2;
|
||||
a_f16.shape[0] = M;
|
||||
a_f16.shape[1] = acols;
|
||||
a_f16.stride[0] = acols;
|
||||
a_f16.stride[1] = 1;
|
||||
A = &a_f16;
|
||||
ars = acols;
|
||||
acs = 1;
|
||||
}
|
||||
|
||||
const bool i8 = zg.act != ZP_F16;
|
||||
if (i8 && A->dtype != RAD_I8) return RAD_E_DTYPE;
|
||||
|
||||
+25
-3
@@ -286,8 +286,8 @@ static int gemm_nt_impl(const RadArgs* a, bool bias_operand, const char* nkey, c
|
||||
* acc += (sub * a_scale[m]) * scale
|
||||
* ZP_I8_GROUP sub = sum(a * (code - zero)) exact
|
||||
* acc += (sub * a_scale[m][g]) * scale
|
||||
* ZP_F16 sub = sum over ascending k of a * (code - zero), each product exact in f32
|
||||
* acc += sub * scale
|
||||
* ZP_F16 BF16 inputs are first narrowed to F16 (as by the graph's former cast); sub is
|
||||
* the sum over ascending k of a * (code - zero), then acc += sub * scale
|
||||
*
|
||||
* `a` is already in column order (the caller undoes the step order). `a_sum` is quant_act_i8's,
|
||||
* group-major [K/group][Mpad], Mpad = ceil(M/16)*16; the grid's zero is removed through it, as
|
||||
@@ -319,7 +319,29 @@ static int gemm_zp(const ZpGrid& zg, const RadTensor* A, const RadTensor* as_,
|
||||
const int64_t acols = parts * K;
|
||||
if (numel(A) < M * acols) return RAD_E_SHAPE;
|
||||
if (A->rank >= 2 && A->shape[A->rank - 1] != acols) return RAD_E_SHAPE;
|
||||
const int64_t ars = row_stride(A, acols), acs = laststride(A);
|
||||
int64_t ars = row_stride(A, acols), acs = laststride(A);
|
||||
|
||||
/* The W4 decode graph supplies BF16 directly so HIP can narrow on load. Preserve the former
|
||||
* explicit BF16->F16 cast semantics on host backends before evaluating the F16 activation grid. */
|
||||
std::vector<uint16_t> narrowed;
|
||||
RadTensor a_f16{};
|
||||
if (zg.act == ZP_F16 && A->dtype == RAD_BF16) {
|
||||
narrowed.resize((size_t)(M * acols));
|
||||
for (int64_t m = 0; m < M; ++m)
|
||||
for (int64_t k = 0; k < acols; ++k)
|
||||
narrowed[(size_t)(m * acols + k)] = f32_to_f16(ldt(A, m * ars + k * acs));
|
||||
a_f16 = *A;
|
||||
a_f16.data = narrowed.data();
|
||||
a_f16.dtype = RAD_F16;
|
||||
a_f16.rank = 2;
|
||||
a_f16.shape[0] = M;
|
||||
a_f16.shape[1] = acols;
|
||||
a_f16.stride[0] = acols;
|
||||
a_f16.stride[1] = 1;
|
||||
A = &a_f16;
|
||||
ars = acols;
|
||||
acs = 1;
|
||||
}
|
||||
|
||||
const bool i8 = zg.act != ZP_F16;
|
||||
if (i8 && A->dtype != RAD_I8) return RAD_E_DTYPE;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "rad_sample.h"
|
||||
#include "rad_philox.h"
|
||||
#include "rad_runtime.h" /* RAD_VGEOM_* */
|
||||
#include "rad_planes.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -1321,6 +1322,52 @@ TEST(bf16_add_matches_explicit_fp32_combine) {
|
||||
CHECK_EQ(std::memcmp(chain.mem.data(), folded.mem.data(), M * N * 2), 0);
|
||||
}
|
||||
|
||||
TEST(w4a16_bf16_activation_matches_the_former_explicit_f16_cast) {
|
||||
constexpr int64_t M = 1, N = 16, K = 128;
|
||||
T bf16(RAD_BF16, {M, K}), f16(RAD_F16, {M, K});
|
||||
T codes(RAD_U4, {N, K}), scales(RAD_F16, {N, 1}), zeros(RAD_U8, {N, 1});
|
||||
T out_bf16(RAD_BF16, {M, N}), out_f16(RAD_BF16, {M, N});
|
||||
const float edge = std::ldexp(1.0f + 1.0f / 128.0f, -20);
|
||||
const uint16_t b = f32_to_bf16(edge);
|
||||
const uint16_t f = f32_to_f16(bf16_to_f32(b));
|
||||
for (int64_t k = 0; k < K; ++k) {
|
||||
std::memcpy((uint8_t*)bf16.t.data + k * 2, &b, 2);
|
||||
std::memcpy((uint8_t*)f16.t.data + k * 2, &f, 2);
|
||||
}
|
||||
std::fill(codes.mem.begin(), codes.mem.end(), 0x11); // every u4 code is one
|
||||
scales.f(std::vector<float>((size_t)N, 1.0f));
|
||||
zeros.n(std::vector<int64_t>((size_t)N, 0));
|
||||
|
||||
RadEncoding enc = rad_enc_affine(RAD_U4, RAD_F16, 1, K);
|
||||
rad_enc_add_plane(&enc, "zero", RAD_U8, 1, K);
|
||||
const int selected[] = { 1, 2 };
|
||||
RadTensor planes[] = { scales.t, zeros.t };
|
||||
RadLayout layout{};
|
||||
REQUIRE_EQ(rad_planes_layout(&enc, selected, planes, 2, &layout), RAD_OK);
|
||||
std::vector<uint8_t> joined((size_t)layout.bytes);
|
||||
REQUIRE_EQ(rad_planes_relayout(&enc, selected, planes, 2, joined.data(), layout.bytes), RAD_OK);
|
||||
RadTensor scale_zero{};
|
||||
scale_zero.data = joined.data(); scale_zero.dtype = layout.dtype; scale_zero.rank = 1;
|
||||
scale_zero.shape[0] = layout.bytes; scale_zero.stride[0] = 1;
|
||||
|
||||
const RadKernelInfo* gemm = kern("gemm_nt_q");
|
||||
REQUIRE(gemm && gemm->launch);
|
||||
RadParam params[] = { { "M", RAD_P_INT, M }, { "N", RAD_P_INT, N },
|
||||
{ "K", RAD_P_INT, K }, { "group", RAD_P_INT, K },
|
||||
{ "dtype", RAD_P_STR, 0, 0, "w4a16_asym" } };
|
||||
auto launch = [&](T& activation, T& output) {
|
||||
RadTensor ts[7]{};
|
||||
ts[0] = activation.t; ts[2] = codes.t; ts[3] = scale_zero; ts[4] = output.t;
|
||||
RadArgs args{}; args.t = ts; args.n_t = 7; args.p = params; args.n_p = 5;
|
||||
args.world_size = 1;
|
||||
return gemm->launch(&args, nullptr);
|
||||
};
|
||||
REQUIRE_EQ(launch(f16, out_f16), RAD_OK);
|
||||
REQUIRE_EQ(launch(bf16, out_bf16), RAD_OK);
|
||||
CHECK_EQ(std::memcmp(out_f16.t.data, out_bf16.t.data, (size_t)M * N * 2), 0);
|
||||
CHECK_EQ(*(const uint16_t*)out_bf16.t.data, (uint16_t)0x3900);
|
||||
}
|
||||
|
||||
/* add's `div` divides the sum after its rounding to y's dtype: 1 + 0.01953125 rounds to bf16
|
||||
* 1.015625 (a tie, to even), and 1.015625 / 3 to 0.337890625; the unrounded sum's third would be
|
||||
* 0.33984375. In f32 the quotient is f32(4 / 3). */
|
||||
|
||||
Reference in new issue
Block a user