1- // FP8 prefill on exact e4m3 weights: acc = fma(P, s, acc) per group, bias MMAs, row scale; no row affects another.
1+ // FP8 prefill on exact e4m3 weights (4-bit words, or staged e4m3 bytes with W8): acc = fma(P, s, acc) per group,
2+ // bias MMAs for 4-bit words, row scale; no row affects another.
23
34#include < ATen/ATen.h>
45#include < algorithm>
@@ -44,7 +45,7 @@ __device__ __forceinline__ void weights8(uint32_t w, uint32_t& b0, uint32_t& b1)
4445 b1 = __byte_perm (e4m3_pair (w, 8 ), e4m3_pair (w, 12 ), 0x5410 );
4546}
4647
47- template <int GS , int BM , int BN , int WM , int WN , int STAGES >
48+ template <int GS , int BM , int BN , int WM , int WN , int STAGES , bool W8 = false >
4849struct Tile {
4950 static constexpr int THREADS = WM * WN * 32 ;
5051 static constexpr int MT = BM / WM / 16 ;
@@ -54,7 +55,7 @@ struct Tile {
5455 static constexpr int CHUNKS = ROW / 16 ;
5556 static constexpr int PER128 = 128 / ROW ; // rows in 128 bytes of shared memory
5657 static constexpr int X = BM * ROW ;
57- static constexpr int W = BN * GS / 2 ;
58+ static constexpr int W = W8 ? BN * GS : BN * GS / 2 ; // e4m3 bytes, or 4-bit words
5859 static constexpr int S = BN * 2 ;
5960 static constexpr int STAGE = X + W + S;
6061 static constexpr int SMEM = STAGES * STAGE ;
@@ -64,12 +65,12 @@ struct Tile {
6465template <int CHUNKS , int PER128 >
6566__device__ __forceinline__ int swz8 (int r, int c) { return c ^ ((r / PER128 ) % CHUNKS ); }
6667
67- template <int GS , int BM , int BN , int WM , int WN , int STAGES , bool F32 >
68+ template <int GS , int BM , int BN , int WM , int WN , int STAGES , bool F32 , bool W8 >
6869__global__ void __launch_bounds__ (WM * WN * 32 , 2 ) prefill8_kernel(
6970 const uint8_t * __restrict__ x, const __nv_bfloat16* __restrict__ xs, const float * __restrict__ scale,
7071 const uint32_t * __restrict__ w, const __nv_bfloat16* __restrict__ scales,
7172 const __nv_bfloat16* __restrict__ biases, void * __restrict__ out, int M, int N, int K, int npad, int group) {
72- using T = Tile<GS , BM , BN , WM , WN , STAGES >;
73+ using T = Tile<GS , BM , BN , WM , WN , STAGES , W8 >;
7374 extern __shared__ __align__ (128 ) unsigned char buf[];
7475 const int tid = threadIdx .x , lane = tid & 31 , warp = tid >> 5 ;
7576 const int wm = warp / WN , wn = warp % WN ;
@@ -87,7 +88,7 @@ __global__ void __launch_bounds__(WM * WN * 32, 2) prefill8_kernel(
8788 x + static_cast <size_t >(min (m0 + r, M - 1 )) * K + g * GS + ch * 16 , m0 + r < M);
8889 }
8990 unsigned char * pw = p + T::X;
90- constexpr int TILE_BYTES = 64 * GS / 2 ;
91+ constexpr int TILE_BYTES = W8 ? 64 * GS : 64 * GS / 2 ;
9192#pragma unroll
9293 for (int c = tid; c < T::W / 16 ; c += T::THREADS ) {
9394 const int t = c / (TILE_BYTES / 16 ), off = c % (TILE_BYTES / 16 );
@@ -141,7 +142,13 @@ __global__ void __launch_bounds__(WM * WN * 32, 2) prefill8_kernel(
141142#pragma unroll
142143 for (int ks = 0 ; ks < T::KS ; ++ks) {
143144 uint32_t b0, b1;
144- weights8 (pw[wbase + j * 32 * T::KS + ks], b0, b1);
145+ if constexpr (W8 ) {
146+ const uint2 b = reinterpret_cast <const uint2 *>(pw)[wbase + j * 32 * T::KS + ks];
147+ b0 = b.x ;
148+ b1 = b.y ;
149+ } else {
150+ weights8 (pw[wbase + j * 32 * T::KS + ks], b0, b1);
151+ }
145152#pragma unroll
146153 for (int i = 0 ; i < T::MT ; ++i) {
147154 if (ks == 0 ) mma8z (cur[i], a[ks][i], b0, b1);
@@ -167,7 +174,7 @@ __global__ void __launch_bounds__(WM * WN * 32, 2) prefill8_kernel(
167174 }
168175 wait<0 >();
169176 // bias term: acc += xs (bf16, (M, KG)) times biases (bf16, (KG, npad)) as k16 MMAs over the groups (zeros past KG)
170- {
177+ if constexpr (! W8 ) {
171178 const int c = lane & 3 , gr = lane >> 2 ;
172179 const unsigned short * xb = reinterpret_cast <const unsigned short *>(xs);
173180 const unsigned short * bb = reinterpret_cast <const unsigned short *>(biases);
@@ -226,12 +233,12 @@ __global__ void __launch_bounds__(WM * WN * 32, 2) prefill8_kernel(
226233 }
227234}
228235
229- template <int GS , int BM , int BN , int WM , int WN , int STAGES , bool F32 >
236+ template <int GS , int BM , int BN , int WM , int WN , int STAGES , bool F32 , bool W8 = false >
230237void launch (const at::Tensor& x, const at::Tensor& xs, const at::Tensor& scale, const at::Tensor& w,
231238 const at::Tensor& scales, const at::Tensor& biases, at::Tensor& out, int N) {
232- using T = Tile<GS , BM , BN , WM , WN , STAGES >;
239+ using T = Tile<GS , BM , BN , WM , WN , STAGES , W8 >;
233240 const int M = x.size (0 ), K = x.size (1 );
234- auto kernel = prefill8_kernel<GS , BM , BN , WM , WN , STAGES , F32 >;
241+ auto kernel = prefill8_kernel<GS , BM , BN , WM , WN , STAGES , F32 , W8 >;
235242 static bool configured = false ;
236243 if (!configured) {
237244 cudaFuncSetAttribute (kernel, cudaFuncAttributeMaxDynamicSharedMemorySize, T::SMEM );
@@ -240,9 +247,10 @@ void launch(const at::Tensor& x, const at::Tensor& xs, const at::Tensor& scale,
240247 const int rows_t = (M + BM - 1 ) / BM ;
241248 const int group = std::max (1 , std::min (rows_t , static_cast <int >((12LL << 20 ) / (static_cast <long long >(BM ) * K))));
242249 kernel<<<rows_t * ((N + BN - 1 ) / BN ), T::THREADS , T::SMEM , at::cuda::getCurrentCUDAStream()>>> (
243- reinterpret_cast <const uint8_t *>(x.data_ptr ()), reinterpret_cast <const __nv_bfloat16*>(xs.data_ptr ()),
244- scale.data_ptr <float >(), reinterpret_cast <const uint32_t *>(w.data_ptr ()),
245- reinterpret_cast <const __nv_bfloat16*>(scales.data_ptr ()), reinterpret_cast <const __nv_bfloat16*>(biases.data_ptr ()),
250+ reinterpret_cast <const uint8_t *>(x.data_ptr ()),
251+ W8 ? nullptr : reinterpret_cast <const __nv_bfloat16*>(xs.data_ptr ()), scale.data_ptr <float >(),
252+ reinterpret_cast <const uint32_t *>(w.data_ptr ()), reinterpret_cast <const __nv_bfloat16*>(scales.data_ptr ()),
253+ W8 ? nullptr : reinterpret_cast <const __nv_bfloat16*>(biases.data_ptr ()),
246254 out.data_ptr (), M, N, K, static_cast <int >(scales.size (1 )), group);
247255 C10_CUDA_KERNEL_LAUNCH_CHECK ();
248256}
@@ -259,10 +267,27 @@ void dispatch(int tile, const at::Tensor& x, const at::Tensor& xs, const at::Ten
259267
260268} // namespace
261269
270+ template <int GS , bool F32 >
271+ void dispatch_w8 (int tile, const at::Tensor& x, const at::Tensor& a, const at::Tensor& w, const at::Tensor& s,
272+ at::Tensor& out, int N) {
273+ switch (tile) {
274+ case 1 : launch<GS , 64 , 128 , 1 , 4 , 4 , F32 , true >(x, x, a, w, s, s, out, N); break ;
275+ case 2 : launch<GS , 128 , 128 , 2 , 4 , 3 , F32 , true >(x, x, a, w, s, s, out, N); break ;
276+ default : launch<GS , 128 , 128 , 2 , 2 , 3 , F32 , true >(x, x, a, w, s, s, out, N); break ;
277+ }
278+ }
279+
262280// ``tile`` (0: 128x128, four 64x64 warps; 1: 64x128; 2: 128x128, eight 64x32 warps) never changes a row's bits.
263281void qmm_prefill8_cuda (const at::Tensor& x, const at::Tensor& xs, const at::Tensor& a, const at::Tensor& w,
264282 const at::Tensor& scales, const at::Tensor& biases, at::Tensor& out, int N, int gs, bool f32 ,
265283 int tile) {
266284 if (gs == 64 ) { if (f32 ) dispatch<64 , true >(tile, x, xs, a, w, scales, biases, out, N); else dispatch<64 , false >(tile, x, xs, a, w, scales, biases, out, N); }
267285 else { if (f32 ) dispatch<32 , true >(tile, x, xs, a, w, scales, biases, out, N); else dispatch<32 , false >(tile, x, xs, a, w, scales, biases, out, N); }
268286}
287+
288+ // The same over e4m3 weight bytes in fragment order with a bf16 scale per (group of 32 or 64 inputs, column), no bias.
289+ void qmm_prefill8w_cuda (const at::Tensor& x, const at::Tensor& a, const at::Tensor& w, const at::Tensor& scales,
290+ at::Tensor& out, int N, int gs, bool f32 , int tile) {
291+ if (gs == 32 ) { if (f32 ) dispatch_w8<32 , true >(tile, x, a, w, scales, out, N); else dispatch_w8<32 , false >(tile, x, a, w, scales, out, N); }
292+ else { if (f32 ) dispatch_w8<64 , true >(tile, x, a, w, scales, out, N); else dispatch_w8<64 , false >(tile, x, a, w, scales, out, N); }
293+ }
0 commit comments