4#ifndef EIGEN_MATRIX_PRODUCT_MMA_BFLOAT16_ALTIVEC_H
5#define EIGEN_MATRIX_PRODUCT_MMA_BFLOAT16_ALTIVEC_H
8#define BFLOAT16_UNROLL _Pragma("unroll 8")
10#define BFLOAT16_UNROLL _Pragma("GCC unroll(8)")
18EIGEN_ALWAYS_INLINE Packet8bf loadBfloat16(
const bfloat16* indexA) {
19 Packet8bf lhs1 = ploadu<Packet8bf>(indexA);
21 Packet8bf lhs2 = pset1<Packet8bf>(Eigen::bfloat16(0));
22 return vec_mergeh(lhs1.m_val, lhs2.m_val);
29EIGEN_ALWAYS_INLINE Packet8bf loadRhsBfloat16(
const bfloat16* blockB, Index strideB, Index i) {
30 return loadBfloat16<zero>(blockB + strideB * i);
33template <Index num_acc, Index num_packets,
bool zero,
bool rhsExtraCols,
bool lhsExtraRows, Index num_rhs,
35EIGEN_ALWAYS_INLINE
void KLoop(
const bfloat16* indexA,
const bfloat16* indexB, __vector_quad (&quad_acc)[num_acc],
36 Index strideB, Index k, Index offsetB, Index extra_cols, Index extra_rows) {
37 Packet8bf lhs[num_lhs], rhs[num_rhs];
40 for (Index i = 0; i < (num_rhs - (rhsExtraCols ? 1 : 0)); i++) {
41 rhs[i] = loadRhsBfloat16<zero>(indexB + k * 4, strideB, i);
44 rhs[num_rhs - 1] = loadRhsBfloat16<zero>(indexB + k * extra_cols - offsetB, strideB, num_rhs - 1);
47 indexA += k * (lhsExtraRows ? extra_rows : num_packets);
49 lhs[0] = loadBfloat16<zero>(indexA);
52 for (Index j = 0; j < num_lhs; j += 2) {
53 Packet8bf lhs1 = ploadu<Packet8bf>(indexA + (j + 0) * (zero ? 4 : 8));
55 Packet8bf lhs2 = pset1<Packet8bf>(Eigen::bfloat16(0));
56 lhs[j + 0] = vec_mergeh(lhs1.m_val, lhs2.m_val);
57 lhs[j + 1] = vec_mergel(lhs1.m_val, lhs2.m_val);
60 lhs[j + 1] = ploadu<Packet8bf>(indexA + (j + 1) * 8);
66 for (Index i = 0, x = 0; i < num_rhs; i++) {
68 for (Index j = 0; j < num_lhs; j++, x++) {
69 __builtin_mma_xvbf16ger2pp(&(quad_acc[x]),
reinterpret_cast<Packet16uc
>(rhs[i].m_val),
70 reinterpret_cast<Packet16uc
>(lhs[j].m_val));
75template <Index num_acc>
76EIGEN_ALWAYS_INLINE
void zeroAccumulators(__vector_quad (&quad_acc)[num_acc]) {
78 for (Index k = 0; k < num_acc; k++) __builtin_mma_xxsetaccz(&(quad_acc[k]));
81template <Index num_acc>
82EIGEN_ALWAYS_INLINE
void disassembleAccumulators(__vector_quad (&quad_acc)[num_acc], Packet4f (&acc)[num_acc][4]) {
84 for (Index k = 0; k < num_acc; k++) __builtin_mma_disassemble_acc((
void*)acc[k], &(quad_acc[k]));
87template <Index num_acc,
bool rhsExtraCols,
bool lhsExtraRows, Index num_rhs, Index num_lhs>
88EIGEN_ALWAYS_INLINE
void outputResults(Packet4f (&acc)[num_acc][4], Index rows,
const Packet4f& pAlpha,
float* result,
89 const Index extra_cols, Index extra_rows) {
91 for (Index i = 0, k = 0; i < num_rhs - (rhsExtraCols ? 1 : 0); i++, result += 4 * rows) {
93 for (Index j = 0; j < num_lhs; j++, k++) {
94 storeResults<false, lhsExtraRows>(acc[k], rows, pAlpha, result + j * 4, extra_cols, extra_rows);
98 storeResults<rhsExtraCols, lhsExtraRows>(acc[num_acc - 1], rows, pAlpha, result, extra_cols, extra_rows);
102template <const Index num_acc, const Index num_packets,
bool rhsExtraCols,
bool lhsExtraRows,
bool multiIter = false>
103EIGEN_ALWAYS_INLINE
void colLoopBodyIter(Index depth, Index rows,
const Packet4f& pAlpha,
const bfloat16* indexA,
104 const bfloat16* indexB, Index strideB, Index offsetB,
float* result,
105 const Index extra_cols,
const Index extra_rows) {
106 constexpr Index num_lhs = multiIter ? (num_packets / 4) : 1;
107 constexpr Index num_rhs = (num_acc + num_lhs - 1) / num_lhs;
109 for (Index offset_row = 0; offset_row < num_packets; offset_row += 4, indexA += (multiIter ? 0 : 8),
110 indexB += (multiIter ? (num_rhs * strideB) : 0), result += (multiIter ? (4 * rows * num_rhs) : 4)) {
111 Packet4f acc[num_acc][4];
112 __vector_quad quad_acc[num_acc];
114 zeroAccumulators<num_acc>(quad_acc);
117 for (k = 0; k + 2 <= depth; k += 2) {
118 KLoop<num_acc, num_packets, false, rhsExtraCols, lhsExtraRows, num_rhs, num_lhs>(
119 indexA, indexB, quad_acc, strideB, k, offsetB, extra_cols, extra_rows);
122 KLoop<num_acc, num_packets, true, rhsExtraCols, lhsExtraRows, num_rhs, num_lhs>(
123 indexA - (multiIter ? 0 : offset_row), indexB, quad_acc, strideB, k, offsetB, extra_cols, extra_rows);
126 disassembleAccumulators<num_acc>(quad_acc, acc);
128 outputResults<num_acc, rhsExtraCols, lhsExtraRows, num_rhs, num_lhs>(acc, rows, pAlpha, result, extra_cols,
133#define MAX_BFLOAT16_ACC 8
135template <const Index num_acc, const Index num_packets,
bool rhsExtraCols,
bool lhsExtraRows>
136void colLoopBody(Index& col, Index depth, Index cols, Index rows,
const Packet4f& pAlpha,
const bfloat16* indexA,
137 const bfloat16* indexB, Index strideB, Index offsetB,
float* result) {
138 constexpr Index step = (num_acc * 4);
139 const Index extra_cols = (rhsExtraCols) ? (cols & 3) : 0;
140 const Index extra_rows = (lhsExtraRows) ? (rows & 3) : 0;
141 constexpr bool multiIters = !rhsExtraCols && (num_acc == MAX_BFLOAT16_ACC);
142 constexpr bool normIters = multiIters && ((num_acc % (num_packets / 4)) == 0);
145 colLoopBodyIter<num_acc, num_packets, rhsExtraCols, lhsExtraRows, normIters>(
146 depth, rows, pAlpha, indexA, indexB, strideB, offsetB, result, extra_cols, extra_rows);
148 indexB += strideB * num_acc;
149 result += rows * step;
150 }
while (multiIters && (step <= cols - (col += step)));
153template <const Index num_acc, const Index num_packets,
bool rhsExtraCols,
bool lhsExtraRows>
154EIGEN_ALWAYS_INLINE
void colLoopBodyExtraN(Index col, Index depth, Index cols, Index rows,
const Packet4f& pAlpha,
155 const bfloat16* indexA,
const bfloat16* blockB, Index strideB, Index offsetB,
157 if (MAX_BFLOAT16_ACC > num_acc) {
158 colLoopBody<num_acc + (rhsExtraCols ? 1 : 0), num_packets, rhsExtraCols, lhsExtraRows>(
159 col, depth, cols, rows, pAlpha, indexA, blockB, strideB, offsetB, result);
163template <const Index num_packets,
bool rhsExtraCols,
bool lhsExtraRows>
164void colLoopBodyExtra(Index col, Index depth, Index cols, Index rows,
const Packet4f& pAlpha,
const bfloat16* indexA,
165 const bfloat16* blockB, Index strideB, Index offsetB,
float* result) {
166 switch ((cols - col) >> 2) {
168 colLoopBodyExtraN<7, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
169 strideB, offsetB, result);
172 colLoopBodyExtraN<6, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
173 strideB, offsetB, result);
176 colLoopBodyExtraN<5, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
177 strideB, offsetB, result);
180 colLoopBodyExtraN<4, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
181 strideB, offsetB, result);
184 colLoopBodyExtraN<3, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
185 strideB, offsetB, result);
188 colLoopBodyExtraN<2, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
189 strideB, offsetB, result);
192 colLoopBodyExtraN<1, num_packets, rhsExtraCols, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
193 strideB, offsetB, result);
197 colLoopBody<1, num_packets, true, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB, strideB,
204template <const Index num_packets,
bool lhsExtraRows = false>
205EIGEN_ALWAYS_INLINE
void colLoops(Index depth, Index cols, Index rows,
const Packet4f& pAlpha,
const bfloat16* indexA,
206 const bfloat16* blockB, Index strideB, Index offsetB,
float* result) {
208 if (cols >= (MAX_BFLOAT16_ACC * 4)) {
209 colLoopBody<MAX_BFLOAT16_ACC, num_packets, false, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB,
211 blockB += (strideB >> 2) * col;
212 result += rows * col;
215 colLoopBodyExtra<num_packets, true, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB, strideB, offsetB,
218 colLoopBodyExtra<num_packets, false, lhsExtraRows>(col, depth, cols, rows, pAlpha, indexA, blockB, strideB, 0,
223EIGEN_ALWAYS_INLINE Packet8bf convertF32toBF16(
const float* res) {
225 __vector_pair fp16_vp = *
reinterpret_cast<__vector_pair*
>(
const_cast<float*
>(res));
226 __builtin_vsx_disassemble_pair(
reinterpret_cast<void*
>(fp16), &fp16_vp);
227 fp16[0] = __builtin_vsx_xvcvspbf16(fp16[0]);
228 fp16[1] = __builtin_vsx_xvcvspbf16(fp16[1]);
229 return vec_pack(
reinterpret_cast<Packet4ui
>(fp16[0]),
reinterpret_cast<Packet4ui
>(fp16[1]));
232template <
typename DataMapper, const Index size>
233EIGEN_ALWAYS_INLINE
void convertArrayF32toBF16Col(
float* result, Index col, Index rows,
const DataMapper& res) {
234 const DataMapper res2 = res.getSubMapper(0, col);
236 float* result2 = result + col * rows;
237 for (row = 0; row + 8 <= rows; row += 8, result2 += 8) {
239 PacketBlock<Packet8bf, size> block;
241 for (Index j = 0; j < size; j++) {
242 block.packet[j] = convertF32toBF16(result2 + j * rows);
244 res2.template storePacketBlock<Packet8bf, size>(row, 0, block);
249 for (Index j = 0; j < size; j++) {
250 Packet8bf fp16 = convertF32toBF16(result2 + j * rows);
251 res2.template storePacketPartial<Packet8bf>(row, j, fp16, rows & 7);
256template <const Index size,
bool non_unit_str
ide = false>
257EIGEN_ALWAYS_INLINE
void convertPointerF32toBF16(Index& i,
float* result, Index rows, bfloat16*& dst,
259 constexpr Index extra = ((size < 8) ? 8 : size);
260 while (i + size <= rows) {
261 PacketBlock<Packet8bf, (size + 7) / 8> r32;
262 r32.packet[0] = convertF32toBF16(result + i + 0);
264 r32.packet[1] = convertF32toBF16(result + i + 8);
267 r32.packet[2] = convertF32toBF16(result + i + 16);
268 r32.packet[3] = convertF32toBF16(result + i + 24);
270 storeBF16fromResult<size, non_unit_stride, 0>(dst, r32.packet[0], resInc, rows & 7);
272 storeBF16fromResult<size, non_unit_stride, 8>(dst, r32.packet[1], resInc);
275 storeBF16fromResult<size, non_unit_stride, 16>(dst, r32.packet[2], resInc);
276 storeBF16fromResult<size, non_unit_stride, 24>(dst, r32.packet[3], resInc);
279 dst += extra * resInc;
280 if (size != 32)
break;
284template <
bool non_unit_str
ide = false>
285EIGEN_ALWAYS_INLINE
void convertArrayPointerF32toBF16(
float* result, Index rows, bfloat16* dst, Index resInc = 1) {
287 convertPointerF32toBF16<32, non_unit_stride>(i, result, rows, dst, resInc);
288 convertPointerF32toBF16<16, non_unit_stride>(i, result, rows, dst, resInc);
289 convertPointerF32toBF16<8, non_unit_stride>(i, result, rows, dst, resInc);
290 convertPointerF32toBF16<1, non_unit_stride>(i, result, rows, dst, resInc);
293template <
typename DataMapper>
294EIGEN_ALWAYS_INLINE
void convertArrayF32toBF16(
float* result, Index cols, Index rows,
const DataMapper& res) {
296 for (col = 0; col + 4 <= cols; col += 4) {
297 convertArrayF32toBF16Col<DataMapper, 4>(result, col, rows, res);
300 switch (cols - col) {
302 convertArrayF32toBF16Col<DataMapper, 1>(result, col, rows, res);
305 convertArrayF32toBF16Col<DataMapper, 2>(result, col, rows, res);
308 convertArrayF32toBF16Col<DataMapper, 3>(result, col, rows, res);
314EIGEN_ALWAYS_INLINE
void calcColLoops(
const bfloat16*& indexA, Index& row, Index depth, Index cols, Index rows,
315 const Packet4f& pAlpha,
const bfloat16* indexB, Index strideB, Index offsetA,
316 Index offsetB, Index bigSuffix,
float* result) {
317 if ((size == 16) || (rows & size)) {
318 indexA += size * offsetA;
319 colLoops<size>(depth, cols, rows, pAlpha, indexA, indexB, strideB, offsetB, result + row);
321 indexA += bigSuffix * size / 16;
325template <
typename DataMapper>
326void gemmMMAbfloat16(
const DataMapper& res,
const bfloat16* indexA,
const bfloat16* indexB, Index rows, Index depth,
327 Index cols, bfloat16 alpha, Index strideA, Index strideB, Index offsetA, Index offsetB) {
328 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
329 const Packet4f pAlpha = pset1<Packet4f>(falpha);
330 ei_declare_aligned_stack_constructed_variable(
float, result, cols* rows, 0);
332 convertArrayBF16toF32<DataMapper>(result, cols, rows, res);
334 if (strideA == -1) strideA = depth;
335 if (strideB == -1) strideB = depth;
344 Index bigSuffix = (2 * 8) * (strideA - offsetA);
345 indexB += 4 * offsetB;
350 while (row + 16 <= rows) {
351 calcColLoops<16>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
354 calcColLoops<8>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
356 calcColLoops<4>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
360 colLoops<4, true>(depth, cols, rows, pAlpha, indexA, indexB, strideB, offsetB, result + row);
364 convertArrayF32toBF16<DataMapper>(result, cols, rows, res);
367#undef MAX_BFLOAT16_ACC
369#if !EIGEN_ALTIVEC_DISABLE_MMA
370template <Index num_acc,
typename LhsMapper,
bool zero>
371EIGEN_ALWAYS_INLINE
void loadVecLoop(Index k, LhsMapper& lhs, Packet8bf (&a0)[num_acc], Packet8bf b1) {
372 a0[k + 0] = lhs.template loadPacket<Packet8bf>(k * 4, 0);
374 b1 = lhs.template loadPacket<Packet8bf>(k * 4, 1);
376 if (num_acc > (k + 1)) {
377 a0[k + 1] = vec_mergel(a0[k + 0].m_val, b1.m_val);
379 a0[k + 0] = vec_mergeh(a0[k + 0].m_val, b1.m_val);
382template <Index num_acc>
383EIGEN_ALWAYS_INLINE
void multVec(__vector_quad (&quad_acc)[num_acc],
const Packet8bf (&a0)[num_acc],
384 const Packet8bf& b0) {
386 for (Index k = 0; k < num_acc; k++) {
387 __builtin_mma_xvbf16ger2pp(&(quad_acc[k]),
reinterpret_cast<Packet16uc
>(b0.m_val),
388 reinterpret_cast<Packet16uc
>(a0[k].m_val));
392template <Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool zero,
bool linear>
393EIGEN_ALWAYS_INLINE
void vecColLoop(Index j, LhsMapper& lhs, RhsMapper& rhs, __vector_quad (&quad_acc)[num_acc]) {
394 Packet8bf a0[num_acc];
395 Packet8bf b1 = pset1<Packet8bf>(Eigen::bfloat16(0));
396 Packet8bf b0 = loadColData<RhsMapper, linear>(rhs, j);
399 b0 = vec_mergeh(b0.m_val, b1.m_val);
402 using LhsSubMapper =
typename LhsMapper::SubMapper;
404 LhsSubMapper lhs2 = lhs.getSubMapper(0, j);
406 for (Index k = 0; k < num_acc; k += 2) {
407 loadVecLoop<num_acc, LhsSubMapper, zero>(k, lhs2, a0, b1);
410 multVec<num_acc>(quad_acc, a0, b0);
413#define MAX_BFLOAT16_VEC_ACC 8
415template <const Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
416void colVecColLoopBody(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
418 constexpr Index step = (num_acc * 4);
419 const Index extra_rows = (extraRows) ? (rows & 3) : 0;
420 constexpr bool multiIters = !extraRows && (num_acc == MAX_BFLOAT16_VEC_ACC);
423 Packet4f acc[num_acc][4];
424 __vector_quad quad_acc[num_acc];
426 zeroAccumulators<num_acc>(quad_acc);
428 using LhsSubMapper =
typename LhsMapper::SubMapper;
430 LhsSubMapper lhs2 = lhs.getSubMapper(row, 0);
431 for (Index j = 0; j + 2 <= cend; j += 2) {
432 vecColLoop<num_acc, LhsSubMapper, RhsMapper, false, linear>(j, lhs2, rhs, quad_acc);
435 vecColLoop<num_acc, LhsSubMapper, RhsMapper, true, linear>(cend - 1, lhs2, rhs, quad_acc);
438 disassembleAccumulators<num_acc>(quad_acc, acc);
440 outputVecColResults<num_acc, extraRows>(acc, result, pAlpha, extra_rows);
443 }
while (multiIters && (step <= rows - (row += step)));
446template <const Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
447EIGEN_ALWAYS_INLINE
void colVecColLoopBodyExtraN(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
448 const Packet4f& pAlpha,
float* result) {
449 if (MAX_BFLOAT16_VEC_ACC > num_acc) {
450 colVecColLoopBody<num_acc + (extraRows ? 1 : 0), LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs,
455template <
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
456EIGEN_ALWAYS_INLINE
void colVecColLoopBodyExtra(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
457 const Packet4f& pAlpha,
float* result) {
458 switch ((rows - row) >> 2) {
460 colVecColLoopBodyExtraN<7, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
463 colVecColLoopBodyExtraN<6, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
466 colVecColLoopBodyExtraN<5, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
469 colVecColLoopBodyExtraN<4, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
472 colVecColLoopBodyExtraN<3, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
475 colVecColLoopBodyExtraN<2, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
478 colVecColLoopBodyExtraN<1, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
482 colVecColLoopBody<1, LhsMapper, RhsMapper, true, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
488template <
typename LhsMapper,
typename RhsMapper,
bool linear>
489EIGEN_ALWAYS_INLINE
void calcVecColLoops(Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
492 if (rows >= (MAX_BFLOAT16_VEC_ACC * 4)) {
493 colVecColLoopBody<MAX_BFLOAT16_VEC_ACC, LhsMapper, RhsMapper, false, linear>(row, cend, rows, lhs, rhs, pAlpha,
498 colVecColLoopBodyExtra<LhsMapper, RhsMapper, true, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
500 colVecColLoopBodyExtra<LhsMapper, RhsMapper, false, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
504template <
typename RhsMapper,
typename LhsMapper,
typename =
void>
505struct UseMMAStride : std::false_type {
506 static EIGEN_ALWAYS_INLINE
void run(Index j2, Index jend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
507 const Packet4f& pAlpha,
float* result) {
508 using RhsSubMapper =
typename RhsMapper::SubMapper;
510 RhsSubMapper rhs2 = rhs.getSubMapper(j2, 0);
511 calcVecColLoops<LhsMapper, RhsSubMapper, false>(jend - j2, rows, lhs, rhs2, pAlpha, result);
515template <
typename RhsMapper,
typename LhsMapper>
516struct UseMMAStride<RhsMapper, LhsMapper,
517 std::enable_if_t<std::is_member_function_pointer<decltype(&RhsMapper::stride)>::value>>
519 static EIGEN_ALWAYS_INLINE
void run(Index j2, Index jend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
520 const Packet4f& pAlpha,
float* result) {
521 using RhsSubMapper =
typename RhsMapper::SubMapper;
523 RhsSubMapper rhs2 = rhs.getSubMapper(j2, 0);
524 if (rhs.stride() == 1) {
525 calcVecColLoops<LhsMapper, RhsSubMapper, true>(jend - j2, rows, lhs, rhs2, pAlpha, result);
527 calcVecColLoops<LhsMapper, RhsSubMapper, false>(jend - j2, rows, lhs, rhs2, pAlpha, result);
532template <
typename LhsMapper,
typename RhsMapper>
533void gemvMMA_bfloat16_col(Index rows, Index cols,
const LhsMapper& alhs,
const RhsMapper& rhs, bfloat16* res,
534 Index resIncr, bfloat16 alpha) {
535 EIGEN_UNUSED_VARIABLE(resIncr);
536 eigen_internal_assert(resIncr == 1);
543 const Index lhsStride = lhs.stride();
546 const Index block_cols = cols < 128 ? cols : (lhsStride *
sizeof(bfloat16) < 16000 ? 16 : 8);
547 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
548 Packet4f pAlpha = pset1<Packet4f>(falpha);
550 ei_declare_aligned_stack_constructed_variable(
float, result, rows, 0);
552 convertArrayPointerBF16toF32(result, 1, rows, res);
554 for (Index j2 = 0; j2 < cols; j2 += block_cols) {
555 Index jend = numext::mini(j2 + block_cols, cols);
557 using LhsSubMapper =
typename LhsMapper::SubMapper;
559 LhsSubMapper lhs2 = lhs.getSubMapper(0, j2);
560 UseMMAStride<RhsMapper, LhsSubMapper>::run(j2, jend, rows, lhs2, rhs2, pAlpha, result);
563 convertArrayPointerF32toBF16(result, rows, res);
566static Packet16uc p16uc_ELEMENT_VEC3 = {0x0c, 0x0d, 0x0e, 0x0f, 0x1c, 0x1d, 0x1e, 0x1f,
567 0x0c, 0x0d, 0x0e, 0x0f, 0x1c, 0x1d, 0x1e, 0x1f};
569template <Index num_acc>
570EIGEN_ALWAYS_INLINE
void preduxVecResults2(Packet4f (&acc)[num_acc][4], Index k) {
571 if (num_acc > (k + 1)) {
572 acc[k][0] = vec_mergeh(acc[k][0], acc[k + 1][0]);
573 acc[k][1] = vec_mergeo(acc[k][1], acc[k + 1][1]);
574 acc[k][2] = vec_mergel(acc[k][2], acc[k + 1][2]);
575 acc[k][3] = vec_perm(acc[k][3], acc[k + 1][3], p16uc_ELEMENT_VEC3);
577 acc[k][0] = (acc[k][0] + acc[k][2]) + (acc[k][1] + acc[k][3]);
579 acc[k][0] = vec_mergeh(acc[k][0], acc[k][1]);
580 acc[k][0] += vec_mergel(acc[k][2], acc[k][3]);
582 acc[k][0] += vec_sld(acc[k][0], acc[k][0], 12);
584 acc[k][0] += vec_sld(acc[k][0], acc[k][0], 4);
589template <Index num_acc>
590EIGEN_ALWAYS_INLINE
void preduxVecResults(Packet4f (&acc)[num_acc][4]) {
592 for (Index k = 0; k < num_acc; k += 4) {
593 preduxVecResults2<num_acc>(acc, k + 0);
594 if (num_acc > (k + 2)) {
595 preduxVecResults2<num_acc>(acc, k + 2);
596 acc[k + 0][0] =
reinterpret_cast<Packet4f
>(
597 vec_mergeh(
reinterpret_cast<Packet2ul
>(acc[k + 0][0]),
reinterpret_cast<Packet2ul
>(acc[k + 2][0])));
602template <Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extra>
603EIGEN_ALWAYS_INLINE
void multVecLoop(__vector_quad (&quad_acc)[num_acc],
const LhsMapper& lhs, RhsMapper& rhs, Index j,
605 Packet8bf a0[num_acc], b0;
608 b0 = rhs.template loadPacketPartial<Packet8bf>(j, extra_cols);
610 b0 = rhs.template loadPacket<Packet8bf>(j);
613 const LhsMapper lhs2 = lhs.getSubMapper(0, j);
615 for (Index k = 0; k < num_acc; k++) {
617 a0[k] = lhs2.template loadPacketPartial<Packet8bf>(k, 0, extra_cols);
619 a0[k] = lhs2.template loadPacket<Packet8bf>(k, 0);
623 multVec<num_acc>(quad_acc, a0, b0);
626template <Index num_acc,
typename LhsMapper,
typename RhsMapper>
627EIGEN_ALWAYS_INLINE
void vecLoop(Index cols,
const LhsMapper& lhs, RhsMapper& rhs, __vector_quad (&quad_acc)[num_acc],
630 for (; j + 8 <= cols; j += 8) {
631 multVecLoop<num_acc, LhsMapper, RhsMapper, false>(quad_acc, lhs, rhs, j, extra_cols);
635 multVecLoop<num_acc, LhsMapper, RhsMapper, true>(quad_acc, lhs, rhs, j, extra_cols);
639template <const Index num_acc,
typename LhsMapper,
typename RhsMapper>
640void colVecLoopBody(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
642 constexpr bool multiIters = (num_acc == MAX_BFLOAT16_VEC_ACC);
643 const Index extra_cols = (cols & 7);
646 Packet4f acc[num_acc][4];
647 __vector_quad quad_acc[num_acc];
649 zeroAccumulators<num_acc>(quad_acc);
651 const LhsMapper lhs2 = lhs.getSubMapper(row, 0);
652 vecLoop<num_acc, LhsMapper, RhsMapper>(cols, lhs2, rhs, quad_acc, extra_cols);
654 disassembleAccumulators<num_acc>(quad_acc, acc);
656 preduxVecResults<num_acc>(acc);
658 outputVecResults<num_acc>(acc, result, pAlpha);
661 }
while (multiIters && (num_acc <= rows - (row += num_acc)));
664template <const Index num_acc,
typename LhsMapper,
typename RhsMapper>
665EIGEN_ALWAYS_INLINE
void colVecLoopBodyExtraN(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
666 const Packet4f& pAlpha,
float* result) {
667 if (MAX_BFLOAT16_VEC_ACC > num_acc) {
668 colVecLoopBody<num_acc, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
672template <
typename LhsMapper,
typename RhsMapper>
673EIGEN_ALWAYS_INLINE
void colVecLoopBodyExtra(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
674 const Packet4f& pAlpha,
float* result) {
675 switch (rows - row) {
677 colVecLoopBodyExtraN<7, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
680 colVecLoopBodyExtraN<6, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
683 colVecLoopBodyExtraN<5, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
686 colVecLoopBodyExtraN<4, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
689 colVecLoopBodyExtraN<3, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
692 colVecLoopBodyExtraN<2, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
695 colVecLoopBodyExtraN<1, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
700template <
typename LhsMapper,
typename RhsMapper>
701EIGEN_ALWAYS_INLINE
void calcVecLoops(Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
704 if (rows >= MAX_BFLOAT16_VEC_ACC) {
705 colVecLoopBody<MAX_BFLOAT16_VEC_ACC, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
708 colVecLoopBodyExtra<LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
711template <
typename LhsMapper,
typename RhsMapper>
712EIGEN_STRONG_INLINE
void gemvMMA_bfloat16_row(Index rows, Index cols,
const LhsMapper& alhs,
const RhsMapper& rhs,
713 bfloat16* res, Index resIncr, bfloat16 alpha) {
714 typedef typename RhsMapper::LinearMapper LinearMapper;
719 LinearMapper rhs2 = rhs.getLinearMapper(0, 0);
721 eigen_internal_assert(rhs.stride() == 1);
723 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
724 const Packet4f pAlpha = pset1<Packet4f>(falpha);
726 ei_declare_aligned_stack_constructed_variable(
float, result, rows, 0);
728 convertArrayPointerBF16toF32(result, 1, rows, res);
730 convertArrayPointerBF16toF32<true>(result, 1, rows, res, resIncr);
732 calcVecLoops<LhsMapper, LinearMapper>(cols, rows, lhs, rhs2, pAlpha, result);
734 convertArrayPointerF32toBF16(result, rows, res);
736 convertArrayPointerF32toBF16<true>(result, rows, res, resIncr);
741#undef MAX_BFLOAT16_VEC_ACC
742#undef BFLOAT16_UNROLL