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, convert_index<mapper_index_t<DataMapper>>(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>(convert_index<mapper_index_t<DataMapper>>(row), 0, block);
249 for (
int j = 0; j < size; j++) {
250 Packet8bf fp16 = convertF32toBF16(result2 + j * rows);
251 res2.template storePacketPartial<Packet8bf>(convert_index<mapper_index_t<DataMapper>>(row), j, fp16,
252 convert_index<mapper_index_t<DataMapper>>(rows & 7));
257template <const Index size,
bool non_unit_str
ide = false>
258EIGEN_ALWAYS_INLINE
void convertPointerF32toBF16(Index& i,
float* result, Index rows, bfloat16*& dst,
260 constexpr Index extra = ((size < 8) ? 8 : size);
261 while (i + size <= rows) {
262 PacketBlock<Packet8bf, (size + 7) / 8> r32;
263 r32.packet[0] = convertF32toBF16(result + i + 0);
265 r32.packet[1] = convertF32toBF16(result + i + 8);
268 r32.packet[2] = convertF32toBF16(result + i + 16);
269 r32.packet[3] = convertF32toBF16(result + i + 24);
271 storeBF16fromResult<size, non_unit_stride, 0>(dst, r32.packet[0], resInc, rows & 7);
273 storeBF16fromResult<size, non_unit_stride, 8>(dst, r32.packet[1], resInc);
276 storeBF16fromResult<size, non_unit_stride, 16>(dst, r32.packet[2], resInc);
277 storeBF16fromResult<size, non_unit_stride, 24>(dst, r32.packet[3], resInc);
280 dst += extra * resInc;
281 if (size != 32)
break;
285template <
bool non_unit_str
ide = false>
286EIGEN_ALWAYS_INLINE
void convertArrayPointerF32toBF16(
float* result, Index rows, bfloat16* dst, Index resInc = 1) {
288 convertPointerF32toBF16<32, non_unit_stride>(i, result, rows, dst, resInc);
289 convertPointerF32toBF16<16, non_unit_stride>(i, result, rows, dst, resInc);
290 convertPointerF32toBF16<8, non_unit_stride>(i, result, rows, dst, resInc);
291 convertPointerF32toBF16<1, non_unit_stride>(i, result, rows, dst, resInc);
294template <
typename DataMapper>
295EIGEN_ALWAYS_INLINE
void convertArrayF32toBF16(
float* result, Index cols, Index rows,
const DataMapper& res) {
297 for (col = 0; col + 4 <= cols; col += 4) {
298 convertArrayF32toBF16Col<DataMapper, 4>(result, col, rows, res);
301 switch (cols - col) {
303 convertArrayF32toBF16Col<DataMapper, 1>(result, col, rows, res);
306 convertArrayF32toBF16Col<DataMapper, 2>(result, col, rows, res);
309 convertArrayF32toBF16Col<DataMapper, 3>(result, col, rows, res);
315EIGEN_ALWAYS_INLINE
void calcColLoops(
const bfloat16*& indexA, Index& row, Index depth, Index cols, Index rows,
316 const Packet4f& pAlpha,
const bfloat16* indexB, Index strideB, Index offsetA,
317 Index offsetB, Index bigSuffix,
float* result) {
318 if ((size == 16) || (rows & size)) {
319 indexA += size * offsetA;
320 colLoops<size>(depth, cols, rows, pAlpha, indexA, indexB, strideB, offsetB, result + row);
322 indexA += bigSuffix * size / 16;
326template <
typename DataMapper>
327void gemmMMAbfloat16(
const DataMapper& res,
const bfloat16* indexA,
const bfloat16* indexB, Index rows, Index depth,
328 Index cols, bfloat16 alpha, Index strideA, Index strideB, Index offsetA, Index offsetB) {
329 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
330 const Packet4f pAlpha = pset1<Packet4f>(falpha);
331 ei_declare_aligned_stack_constructed_variable(
float, result, cols* rows, 0);
333 convertArrayBF16toF32<DataMapper>(result, cols, rows, res);
335 if (strideA == -1) strideA = depth;
336 if (strideB == -1) strideB = depth;
345 Index bigSuffix = (2 * 8) * (strideA - offsetA);
346 indexB += 4 * offsetB;
351 while (row + 16 <= rows) {
352 calcColLoops<16>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
355 calcColLoops<8>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
357 calcColLoops<4>(indexA, row, depth, cols, rows, pAlpha, indexB, strideB, offsetA, offsetB, bigSuffix, result);
361 colLoops<4, true>(depth, cols, rows, pAlpha, indexA, indexB, strideB, offsetB, result + row);
365 convertArrayF32toBF16<DataMapper>(result, cols, rows, res);
368#undef MAX_BFLOAT16_ACC
370#if !EIGEN_ALTIVEC_DISABLE_MMA
371template <Index num_acc,
typename LhsMapper,
bool zero>
372EIGEN_ALWAYS_INLINE
void loadVecLoop(Index k, LhsMapper& lhs, Packet8bf (&a0)[num_acc], Packet8bf b1) {
373 const auto row = convert_index<mapper_index_t<LhsMapper>>(k * 4);
374 a0[k + 0] = lhs.template loadPacket<Packet8bf>(row, 0);
376 b1 = lhs.template loadPacket<Packet8bf>(row, 1);
378 if (num_acc > (k + 1)) {
379 a0[k + 1] = vec_mergel(a0[k + 0].m_val, b1.m_val);
381 a0[k + 0] = vec_mergeh(a0[k + 0].m_val, b1.m_val);
384template <Index num_acc>
385EIGEN_ALWAYS_INLINE
void multVec(__vector_quad (&quad_acc)[num_acc],
const Packet8bf (&a0)[num_acc],
386 const Packet8bf& b0) {
388 for (Index k = 0; k < num_acc; k++) {
389 __builtin_mma_xvbf16ger2pp(&(quad_acc[k]),
reinterpret_cast<Packet16uc
>(b0.m_val),
390 reinterpret_cast<Packet16uc
>(a0[k].m_val));
394template <Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool zero,
bool linear>
395EIGEN_ALWAYS_INLINE
void vecColLoop(Index j, LhsMapper& lhs, RhsMapper& rhs, __vector_quad (&quad_acc)[num_acc]) {
396 Packet8bf a0[num_acc];
397 Packet8bf b1 = pset1<Packet8bf>(Eigen::bfloat16(0));
398 const auto col = convert_index<mapper_index_t<LhsMapper>>(j);
399 Packet8bf b0 = loadColData<RhsMapper, linear>(rhs, col);
402 b0 = vec_mergeh(b0.m_val, b1.m_val);
405 using LhsSubMapper =
typename LhsMapper::SubMapper;
407 LhsSubMapper lhs2 = lhs.getSubMapper(0, col);
409 for (Index k = 0; k < num_acc; k += 2) {
410 loadVecLoop<num_acc, LhsSubMapper, zero>(k, lhs2, a0, b1);
413 multVec<num_acc>(quad_acc, a0, b0);
416#define MAX_BFLOAT16_VEC_ACC 8
418template <const Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
419void colVecColLoopBody(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
421 constexpr Index step = (num_acc * 4);
422 const Index extra_rows = (extraRows) ? (rows & 3) : 0;
423 constexpr bool multiIters = !extraRows && (num_acc == MAX_BFLOAT16_VEC_ACC);
426 Packet4f acc[num_acc][4];
427 __vector_quad quad_acc[num_acc];
429 zeroAccumulators<num_acc>(quad_acc);
431 using LhsSubMapper =
typename LhsMapper::SubMapper;
433 LhsSubMapper lhs2 = lhs.getSubMapper(convert_index<mapper_index_t<LhsMapper>>(row), 0);
434 for (Index j = 0; j + 2 <= cend; j += 2) {
435 vecColLoop<num_acc, LhsSubMapper, RhsMapper, false, linear>(j, lhs2, rhs, quad_acc);
438 vecColLoop<num_acc, LhsSubMapper, RhsMapper, true, linear>(cend - 1, lhs2, rhs, quad_acc);
441 disassembleAccumulators<num_acc>(quad_acc, acc);
443 outputVecColResults<num_acc, extraRows>(acc, result, pAlpha, extra_rows);
446 }
while (multiIters && (step <= rows - (row += step)));
449template <const Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
450EIGEN_ALWAYS_INLINE
void colVecColLoopBodyExtraN(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
451 const Packet4f& pAlpha,
float* result) {
452 if (MAX_BFLOAT16_VEC_ACC > num_acc) {
453 colVecColLoopBody<num_acc + (extraRows ? 1 : 0), LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs,
458template <
typename LhsMapper,
typename RhsMapper,
bool extraRows,
bool linear>
459EIGEN_ALWAYS_INLINE
void colVecColLoopBodyExtra(Index& row, Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
460 const Packet4f& pAlpha,
float* result) {
461 switch ((rows - row) >> 2) {
463 colVecColLoopBodyExtraN<7, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
466 colVecColLoopBodyExtraN<6, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
469 colVecColLoopBodyExtraN<5, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
472 colVecColLoopBodyExtraN<4, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
475 colVecColLoopBodyExtraN<3, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
478 colVecColLoopBodyExtraN<2, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
481 colVecColLoopBodyExtraN<1, LhsMapper, RhsMapper, extraRows, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
485 colVecColLoopBody<1, LhsMapper, RhsMapper, true, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
491template <
typename LhsMapper,
typename RhsMapper,
bool linear>
492EIGEN_ALWAYS_INLINE
void calcVecColLoops(Index cend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
495 if (rows >= (MAX_BFLOAT16_VEC_ACC * 4)) {
496 colVecColLoopBody<MAX_BFLOAT16_VEC_ACC, LhsMapper, RhsMapper, false, linear>(row, cend, rows, lhs, rhs, pAlpha,
501 colVecColLoopBodyExtra<LhsMapper, RhsMapper, true, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
503 colVecColLoopBodyExtra<LhsMapper, RhsMapper, false, linear>(row, cend, rows, lhs, rhs, pAlpha, result);
507template <
typename RhsMapper,
typename LhsMapper,
typename =
void>
508struct UseMMAStride : std::false_type {
509 static EIGEN_ALWAYS_INLINE
void run(Index j2, Index jend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
510 const Packet4f& pAlpha,
float* result) {
511 using RhsSubMapper =
typename RhsMapper::SubMapper;
513 RhsSubMapper rhs2 = rhs.getSubMapper(convert_index<mapper_index_t<LhsMapper>>(j2), 0);
514 calcVecColLoops<LhsMapper, RhsSubMapper, false>(jend - j2, rows, lhs, rhs2, pAlpha, result);
518template <
typename RhsMapper,
typename LhsMapper>
519struct UseMMAStride<RhsMapper, LhsMapper,
520 std::enable_if_t<std::is_member_function_pointer<decltype(&RhsMapper::stride)>::value>>
522 static EIGEN_ALWAYS_INLINE
void run(Index j2, Index jend, Index rows, LhsMapper& lhs, RhsMapper& rhs,
523 const Packet4f& pAlpha,
float* result) {
524 using RhsSubMapper =
typename RhsMapper::SubMapper;
526 RhsSubMapper rhs2 = rhs.getSubMapper(convert_index<mapper_index_t<LhsMapper>>(j2), 0);
527 if (rhs.stride() == 1) {
528 calcVecColLoops<LhsMapper, RhsSubMapper, true>(jend - j2, rows, lhs, rhs2, pAlpha, result);
530 calcVecColLoops<LhsMapper, RhsSubMapper, false>(jend - j2, rows, lhs, rhs2, pAlpha, result);
535template <
typename LhsMapper,
typename RhsMapper>
536void gemvMMA_bfloat16_col(Index rows, Index cols,
const LhsMapper& alhs,
const RhsMapper& rhs, bfloat16* res,
537 Index resIncr, bfloat16 alpha) {
538 EIGEN_UNUSED_VARIABLE(resIncr);
539 eigen_internal_assert(resIncr == 1);
546 const Index lhsStride = lhs.stride();
549 const Index block_cols = cols < 128 ? cols : (lhsStride *
sizeof(bfloat16) < 16000 ? 16 : 8);
550 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
551 Packet4f pAlpha = pset1<Packet4f>(falpha);
553 ei_declare_aligned_stack_constructed_variable(
float, result, rows, 0);
555 convertArrayPointerBF16toF32(result, 1, rows, res);
557 for (Index j2 = 0; j2 < cols; j2 += block_cols) {
558 Index jend = numext::mini(j2 + block_cols, cols);
560 using LhsSubMapper =
typename LhsMapper::SubMapper;
562 LhsSubMapper lhs2 = lhs.getSubMapper(0, convert_index<mapper_index_t<LhsMapper>>(j2));
563 UseMMAStride<RhsMapper, LhsSubMapper>::run(j2, jend, rows, lhs2, rhs2, pAlpha, result);
566 convertArrayPointerF32toBF16(result, rows, res);
569static Packet16uc p16uc_ELEMENT_VEC3 = {0x0c, 0x0d, 0x0e, 0x0f, 0x1c, 0x1d, 0x1e, 0x1f,
570 0x0c, 0x0d, 0x0e, 0x0f, 0x1c, 0x1d, 0x1e, 0x1f};
572template <Index num_acc>
573EIGEN_ALWAYS_INLINE
void preduxVecResults2(Packet4f (&acc)[num_acc][4], Index k) {
574 if (num_acc > (k + 1)) {
575 acc[k][0] = vec_mergeh(acc[k][0], acc[k + 1][0]);
576 acc[k][1] = vec_mergeo(acc[k][1], acc[k + 1][1]);
577 acc[k][2] = vec_mergel(acc[k][2], acc[k + 1][2]);
578 acc[k][3] = vec_perm(acc[k][3], acc[k + 1][3], p16uc_ELEMENT_VEC3);
580 acc[k][0] = (acc[k][0] + acc[k][2]) + (acc[k][1] + acc[k][3]);
582 acc[k][0] = vec_mergeh(acc[k][0], acc[k][1]);
583 acc[k][0] += vec_mergel(acc[k][2], acc[k][3]);
585 acc[k][0] += vec_sld(acc[k][0], acc[k][0], 12);
587 acc[k][0] += vec_sld(acc[k][0], acc[k][0], 4);
592template <Index num_acc>
593EIGEN_ALWAYS_INLINE
void preduxVecResults(Packet4f (&acc)[num_acc][4]) {
595 for (Index k = 0; k < num_acc; k += 4) {
596 preduxVecResults2<num_acc>(acc, k + 0);
597 if (num_acc > (k + 2)) {
598 preduxVecResults2<num_acc>(acc, k + 2);
599 acc[k + 0][0] =
reinterpret_cast<Packet4f
>(
600 vec_mergeh(
reinterpret_cast<Packet2ul
>(acc[k + 0][0]),
reinterpret_cast<Packet2ul
>(acc[k + 2][0])));
605template <Index num_acc,
typename LhsMapper,
typename RhsMapper,
bool extra>
606EIGEN_ALWAYS_INLINE
void multVecLoop(__vector_quad (&quad_acc)[num_acc],
const LhsMapper& lhs, RhsMapper& rhs, Index j,
608 Packet8bf a0[num_acc], b0;
609 using MapperIndex = mapper_index_t<LhsMapper>;
610 const MapperIndex col = convert_index<MapperIndex>(j);
611 const MapperIndex extraCols = convert_index<MapperIndex>(extra_cols);
614 b0 = rhs.template loadPacketPartial<Packet8bf>(col, extraCols);
616 b0 = rhs.template loadPacket<Packet8bf>(col);
619 const LhsMapper lhs2 = lhs.getSubMapper(0, col);
621 for (
int k = 0; k < num_acc; k++) {
623 a0[k] = lhs2.template loadPacketPartial<Packet8bf>(k, 0, extraCols);
625 a0[k] = lhs2.template loadPacket<Packet8bf>(k, 0);
629 multVec<num_acc>(quad_acc, a0, b0);
632template <Index num_acc,
typename LhsMapper,
typename RhsMapper>
633EIGEN_ALWAYS_INLINE
void vecLoop(Index cols,
const LhsMapper& lhs, RhsMapper& rhs, __vector_quad (&quad_acc)[num_acc],
636 for (; j + 8 <= cols; j += 8) {
637 multVecLoop<num_acc, LhsMapper, RhsMapper, false>(quad_acc, lhs, rhs, j, extra_cols);
641 multVecLoop<num_acc, LhsMapper, RhsMapper, true>(quad_acc, lhs, rhs, j, extra_cols);
645template <const Index num_acc,
typename LhsMapper,
typename RhsMapper>
646void colVecLoopBody(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
648 constexpr bool multiIters = (num_acc == MAX_BFLOAT16_VEC_ACC);
649 const Index extra_cols = (cols & 7);
652 Packet4f acc[num_acc][4];
653 __vector_quad quad_acc[num_acc];
655 zeroAccumulators<num_acc>(quad_acc);
657 const LhsMapper lhs2 = lhs.getSubMapper(convert_index<mapper_index_t<LhsMapper>>(row), 0);
658 vecLoop<num_acc, LhsMapper, RhsMapper>(cols, lhs2, rhs, quad_acc, extra_cols);
660 disassembleAccumulators<num_acc>(quad_acc, acc);
662 preduxVecResults<num_acc>(acc);
664 outputVecResults<num_acc>(acc, result, pAlpha);
667 }
while (multiIters && (num_acc <= rows - (row += num_acc)));
670template <const Index num_acc,
typename LhsMapper,
typename RhsMapper>
671EIGEN_ALWAYS_INLINE
void colVecLoopBodyExtraN(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
672 const Packet4f& pAlpha,
float* result) {
673 if (MAX_BFLOAT16_VEC_ACC > num_acc) {
674 colVecLoopBody<num_acc, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
678template <
typename LhsMapper,
typename RhsMapper>
679EIGEN_ALWAYS_INLINE
void colVecLoopBodyExtra(Index& row, Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
680 const Packet4f& pAlpha,
float* result) {
681 switch (rows - row) {
683 colVecLoopBodyExtraN<7, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
686 colVecLoopBodyExtraN<6, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
689 colVecLoopBodyExtraN<5, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
692 colVecLoopBodyExtraN<4, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
695 colVecLoopBodyExtraN<3, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
698 colVecLoopBodyExtraN<2, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
701 colVecLoopBodyExtraN<1, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
706template <
typename LhsMapper,
typename RhsMapper>
707EIGEN_ALWAYS_INLINE
void calcVecLoops(Index cols, Index rows, LhsMapper& lhs, RhsMapper& rhs,
const Packet4f& pAlpha,
710 if (rows >= MAX_BFLOAT16_VEC_ACC) {
711 colVecLoopBody<MAX_BFLOAT16_VEC_ACC, LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
714 colVecLoopBodyExtra<LhsMapper, RhsMapper>(row, cols, rows, lhs, rhs, pAlpha, result);
717template <
typename LhsMapper,
typename RhsMapper>
718EIGEN_STRONG_INLINE
void gemvMMA_bfloat16_row(Index rows, Index cols,
const LhsMapper& alhs,
const RhsMapper& rhs,
719 bfloat16* res, Index resIncr, bfloat16 alpha) {
720 using LinearMapper =
typename RhsMapper::LinearMapper;
725 LinearMapper rhs2 = rhs.getLinearMapper(0, 0);
727 eigen_internal_assert(rhs.stride() == 1);
729 float falpha = Eigen::bfloat16_impl::bfloat16_to_float(alpha);
730 const Packet4f pAlpha = pset1<Packet4f>(falpha);
732 ei_declare_aligned_stack_constructed_variable(
float, result, rows, 0);
734 convertArrayPointerBF16toF32(result, 1, rows, res);
736 convertArrayPointerBF16toF32<true>(result, 1, rows, res, resIncr);
738 calcVecLoops<LhsMapper, LinearMapper>(cols, rows, lhs, rhs2, pAlpha, result);
740 convertArrayPointerF32toBF16(result, rows, res);
742 convertArrayPointerF32toBF16<true>(result, rows, res, resIncr);
747#undef MAX_BFLOAT16_VEC_ACC
748#undef BFLOAT16_UNROLL