diff --git a/libxe2/xe2_gdn.cpp b/libxe2/xe2_gdn.cpp index 05428b3f..bcbf8013 100644 --- a/libxe2/xe2_gdn.cpp +++ b/libxe2/xe2_gdn.cpp @@ -1187,6 +1187,7 @@ Anchor anchor_layout(int64_t K, int64_t V) { struct RecIO { DT q, k, v, av, bv, state, o, alog, dtb, sidx, z, nw, oq, osc; + bool exact = false; /* XE2_EXACT: libref's double chains only */ V3 qv, kv, vv, ov, zv; V2 a2, b2, si; V4 stv; @@ -1397,6 +1398,7 @@ int rec_io(const RadArgs* a, const RadTensor* t, int nt, RecIO* r, int64_t* n_se r->has_z = z != nullptr; r->has_nw = nw != nullptr; r->has_q8 = oq && osc; + r->exact = exact_only(); if (r->has_q8) { Rows qr, sr; if (int rc = rows_view(oq, Hv * V, &qr, "gdn o_q"); rc < 0) return rc; @@ -1913,6 +1915,7 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float if (so >= 0) store_rows(so); /* uniform */ } if constexpr (FUSED) { + const bool fast_o = r.o.dt == RAD_BF16 && !r.exact; /* kernel B's epilogue, a sub-group a token, from the outputs in local memory */ sycl::group_barrier(it.get_group()); for (int64_t t = blk; t < q_len; t += nblk) { @@ -1936,7 +1939,11 @@ int launch_recurrent_reg_t(sycl::queue& qu, const RecIO& r, int64_t n_seq, float if (r.has_nw) x *= r.nw.ld(ee * r.nw_s); if (r.has_z) { const float zz = r.z.ld(at3(r.zv, tok, h, ee)); - x *= (r.nact == 1 ? sigm(zz) : silu(zz)); + /* a bf16 o from a float exp, libref's double chain again only + * near a rounding tie (near_bf16_tie): the same bf16 */ + const float sf = sigmoid_fexp(zz); + const float xf = x * (r.nact == 1 ? sf : zz * sf); + x = fast_o && !near_bf16_tie(xf) ? xf : x * (r.nact == 1 ? sigm(zz) : silu(zz)); } r.o.st(at3(r.ov, tok, h, ee), x); if (r.has_q8) {