mma_sm70.h 15.7 KB
Newer Older
1
2
3
4
#pragma once

#include "../common.h"

5
#ifndef __CUDACC_RTC__
6
7
#include <type_traits>
#include <utility>
8
#endif
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

namespace tl {

#ifndef TL_ALWAYS_FALSE_V_DEFINED
#define TL_ALWAYS_FALSE_V_DEFINED
template <class> inline constexpr bool always_false_v = false;
#endif

namespace detail {

// SM70 MMA Instruction Traits and Implementations
// SM70 supports m16n16k4 (m8n8k4 instruction at warp level) with FP16/FP32
// accumulation

// Base template for SM70 MMA implementation
template <DataType AType, DataType BType, DataType CType, bool TransA,
          bool TransB>
struct MmaSm70Impl {
  // Default: unsupported configuration
  static constexpr bool kSupported = false;

  static TL_DEVICE void exec(void *, const void *, const void *, const void *) {
    static_assert(always_false_v<std::integral_constant<bool, TransA>>,
                  "tl::mma_sync_sm70: unsupported configuration");
  }
};

// FP16 inputs, FP16 accumulation - col.col (TransA=true, TransB=true)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat16,
                   true, true> {
  using DRegisters = unsigned[4];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = unsigned[4];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(unsigned &d0, unsigned &d1, unsigned &d2,
                            unsigned &d3, unsigned a0, unsigned a1, unsigned b0,
                            unsigned b1, unsigned c0, unsigned c1, unsigned c2,
                            unsigned c3) {
    asm volatile("mma.sync.aligned.m8n8k4.col.col.f16.f16.f16.f16 "
                 "{%0,%1,%2,%3}, {%4,%5}, {%6,%7}, {%8,%9,%10,%11};\n"
                 : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "r"(c0), "r"(c1),
                   "r"(c2), "r"(c3));
  }
};

// FP16 inputs, FP16 accumulation - col.row (TransA=true, TransB=false)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat16,
                   true, false> {
  using DRegisters = unsigned[4];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = unsigned[4];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(unsigned &d0, unsigned &d1, unsigned &d2,
                            unsigned &d3, unsigned a0, unsigned a1, unsigned b0,
                            unsigned b1, unsigned c0, unsigned c1, unsigned c2,
                            unsigned c3) {
    asm volatile("mma.sync.aligned.m8n8k4.col.row.f16.f16.f16.f16 "
                 "{%0,%1,%2,%3}, {%4,%5}, {%6,%7}, {%8,%9,%10,%11};\n"
                 : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "r"(c0), "r"(c1),
                   "r"(c2), "r"(c3));
  }
};

// FP16 inputs, FP16 accumulation - row.col (TransA=false, TransB=true)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat16,
                   false, true> {
  using DRegisters = unsigned[4];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = unsigned[4];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(unsigned &d0, unsigned &d1, unsigned &d2,
                            unsigned &d3, unsigned a0, unsigned a1, unsigned b0,
                            unsigned b1, unsigned c0, unsigned c1, unsigned c2,
                            unsigned c3) {
    asm volatile("mma.sync.aligned.m8n8k4.row.col.f16.f16.f16.f16 "
                 "{%0,%1,%2,%3}, {%4,%5}, {%6,%7}, {%8,%9,%10,%11};\n"
                 : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "r"(c0), "r"(c1),
                   "r"(c2), "r"(c3));
  }
};

// FP16 inputs, FP16 accumulation - row.row (TransA=false, TransB=false)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat16,
                   false, false> {
  using DRegisters = unsigned[4];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = unsigned[4];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(unsigned &d0, unsigned &d1, unsigned &d2,
                            unsigned &d3, unsigned a0, unsigned a1, unsigned b0,
                            unsigned b1, unsigned c0, unsigned c1, unsigned c2,
                            unsigned c3) {
    asm volatile("mma.sync.aligned.m8n8k4.row.row.f16.f16.f16.f16 "
                 "{%0,%1,%2,%3}, {%4,%5}, {%6,%7}, {%8,%9,%10,%11};\n"
                 : "=r"(d0), "=r"(d1), "=r"(d2), "=r"(d3)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "r"(c0), "r"(c1),
                   "r"(c2), "r"(c3));
  }
};

// FP16 inputs, FP32 accumulation - col.col (TransA=true, TransB=true)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat32,
                   true, true> {
  using DRegisters = float[8];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = float[8];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(float &d0, float &d1, float &d2, float &d3,
                            float &d4, float &d5, float &d6, float &d7,
                            unsigned a0, unsigned a1, unsigned b0, unsigned b1,
                            float c0, float c1, float c2, float c3, float c4,
                            float c5, float c6, float c7) {
    asm volatile("mma.sync.aligned.m8n8k4.col.col.f32.f16.f16.f32 "
                 "{%0,%1,%2,%3,%4,%5,%6,%7}, {%8,%9}, {%10,%11}, "
                 "{%12,%13,%14,%15,%16,%17,%18,%19};\n"
                 : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), "=f"(d4), "=f"(d5),
                   "=f"(d6), "=f"(d7)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "f"(c0), "f"(c1),
                   "f"(c2), "f"(c3), "f"(c4), "f"(c5), "f"(c6), "f"(c7));
  }
};

// FP16 inputs, FP32 accumulation - col.row (TransA=true, TransB=false)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat32,
                   true, false> {
  using DRegisters = float[8];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = float[8];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(float &d0, float &d1, float &d2, float &d3,
                            float &d4, float &d5, float &d6, float &d7,
                            unsigned a0, unsigned a1, unsigned b0, unsigned b1,
                            float c0, float c1, float c2, float c3, float c4,
                            float c5, float c6, float c7) {
    asm volatile("mma.sync.aligned.m8n8k4.col.row.f32.f16.f16.f32 "
                 "{%0,%1,%2,%3,%4,%5,%6,%7}, {%8,%9}, {%10,%11}, "
                 "{%12,%13,%14,%15,%16,%17,%18,%19};\n"
                 : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), "=f"(d4), "=f"(d5),
                   "=f"(d6), "=f"(d7)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "f"(c0), "f"(c1),
                   "f"(c2), "f"(c3), "f"(c4), "f"(c5), "f"(c6), "f"(c7));
  }
};

// FP16 inputs, FP32 accumulation - row.col (TransA=false, TransB=true)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat32,
                   false, true> {
  using DRegisters = float[8];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = float[8];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(float &d0, float &d1, float &d2, float &d3,
                            float &d4, float &d5, float &d6, float &d7,
                            unsigned a0, unsigned a1, unsigned b0, unsigned b1,
                            float c0, float c1, float c2, float c3, float c4,
                            float c5, float c6, float c7) {
    asm volatile("mma.sync.aligned.m8n8k4.row.col.f32.f16.f16.f32 "
                 "{%0,%1,%2,%3,%4,%5,%6,%7}, {%8,%9}, {%10,%11}, "
                 "{%12,%13,%14,%15,%16,%17,%18,%19};\n"
                 : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), "=f"(d4), "=f"(d5),
                   "=f"(d6), "=f"(d7)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "f"(c0), "f"(c1),
                   "f"(c2), "f"(c3), "f"(c4), "f"(c5), "f"(c6), "f"(c7));
  }
};

// FP16 inputs, FP32 accumulation - row.row (TransA=false, TransB=false)
template <>
struct MmaSm70Impl<DataType::kFloat16, DataType::kFloat16, DataType::kFloat32,
                   false, false> {
  using DRegisters = float[8];
  using ARegisters = unsigned[2];
  using BRegisters = unsigned[2];
  using CRegisters = float[8];

  static constexpr bool kSupported = true;

  static TL_DEVICE void fma(float &d0, float &d1, float &d2, float &d3,
                            float &d4, float &d5, float &d6, float &d7,
                            unsigned a0, unsigned a1, unsigned b0, unsigned b1,
                            float c0, float c1, float c2, float c3, float c4,
                            float c5, float c6, float c7) {
    asm volatile("mma.sync.aligned.m8n8k4.row.row.f32.f16.f16.f32 "
                 "{%0,%1,%2,%3,%4,%5,%6,%7}, {%8,%9}, {%10,%11}, "
                 "{%12,%13,%14,%15,%16,%17,%18,%19};\n"
                 : "=f"(d0), "=f"(d1), "=f"(d2), "=f"(d3), "=f"(d4), "=f"(d5),
                   "=f"(d6), "=f"(d7)
                 : "r"(a0), "r"(a1), "r"(b0), "r"(b1), "f"(c0), "f"(c1),
                   "f"(c2), "f"(c3), "f"(c4), "f"(c5), "f"(c6), "f"(c7));
  }
};

// Helper to extract register types
template <class Impl> struct MmaSm70ImplTraits {
  using DReg = std::remove_extent_t<typename Impl::DRegisters>;
  using AReg = std::remove_extent_t<typename Impl::ARegisters>;
  using BReg = std::remove_extent_t<typename Impl::BRegisters>;
  using CReg = std::remove_extent_t<typename Impl::CRegisters>;

  static constexpr int kDRegs = std::extent_v<typename Impl::DRegisters>;
  static constexpr int kARegs = std::extent_v<typename Impl::ARegisters>;
  static constexpr int kBRegs = std::extent_v<typename Impl::BRegisters>;
  static constexpr int kCRegs = std::extent_v<typename Impl::CRegisters>;
};

// Dispatcher for SM70 MMA operations
template <DataType AType, DataType BType, DataType CType, int M, int N, int K,
          bool TransA, bool TransB>
struct MmaSm70Dispatcher {
  using CRegType = void;
  using ARegType = void;
  using BRegType = void;

  static TL_DEVICE void exec(CRegType *, const ARegType *, const BRegType *,
                             const CRegType *) {
    static_assert(always_false_v<std::integral_constant<int, M>>,
                  "tl::mma_sync_sm70: unsupported configuration. "
                  "SM70 only supports m16n16k4 with FP16 inputs and FP16/FP32 "
                  "accumulation.");
  }
};

// Helper to call fma with unpacked register arrays
template <class Impl, size_t... DIdx, size_t... AIdx, size_t... BIdx,
          size_t... CIdx>
TL_DEVICE void
call_fma_impl_sm70(typename MmaSm70ImplTraits<Impl>::DReg *d,
                   const typename MmaSm70ImplTraits<Impl>::AReg *a,
                   const typename MmaSm70ImplTraits<Impl>::BReg *b,
                   const typename MmaSm70ImplTraits<Impl>::CReg *c,
                   std::index_sequence<DIdx...>, std::index_sequence<AIdx...>,
                   std::index_sequence<BIdx...>, std::index_sequence<CIdx...>) {
  Impl::fma(d[DIdx]..., a[AIdx]..., b[BIdx]..., c[CIdx]...);
}

template <class Impl>
TL_DEVICE void call_fma_sm70(typename MmaSm70ImplTraits<Impl>::DReg *d,
                             const typename MmaSm70ImplTraits<Impl>::AReg *a,
                             const typename MmaSm70ImplTraits<Impl>::BReg *b,
                             const typename MmaSm70ImplTraits<Impl>::CReg *c) {
  call_fma_impl_sm70<Impl>(
      d, a, b, c, std::make_index_sequence<MmaSm70ImplTraits<Impl>::kDRegs>{},
      std::make_index_sequence<MmaSm70ImplTraits<Impl>::kARegs>{},
      std::make_index_sequence<MmaSm70ImplTraits<Impl>::kBRegs>{},
      std::make_index_sequence<MmaSm70ImplTraits<Impl>::kCRegs>{});
}

// Define dispatchers for all supported SM70 configurations
// Note: m8n8k4 instruction computes m16n16k4 at warp level
#define TL_DEFINE_MMA_SM70_DISPATCHER(ATypeEnum, BTypeEnum, CTypeEnum,         \
                                      TransAValue, TransBValue)                \
  template <>                                                                  \
  struct MmaSm70Dispatcher<DataType::ATypeEnum, DataType::BTypeEnum,           \
                           DataType::CTypeEnum, 16, 16, 4, TransAValue,        \
                           TransBValue> {                                      \
    using Impl = MmaSm70Impl<DataType::ATypeEnum, DataType::BTypeEnum,         \
                             DataType::CTypeEnum, TransAValue, TransBValue>;   \
    using Traits = MmaSm70ImplTraits<Impl>;                                    \
    using CRegType = typename Traits::DReg;                                    \
    using ARegType = typename Traits::AReg;                                    \
    using BRegType = typename Traits::BReg;                                    \
    static_assert(                                                             \
        std::is_same_v<typename Traits::DReg, typename Traits::CReg>,          \
        "tl::mma_sync_sm70 requires matching accumulator/output regs");        \
    static TL_DEVICE void exec(CRegType *d, const ARegType *a,                 \
                               const BRegType *b, const CRegType *c) {         \
      call_fma_sm70<Impl>(d, a, b, c);                                         \
    }                                                                          \
  };

// FP16 inputs with FP16 accumulation (all layout combinations)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat16, true, true)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat16, true, false)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat16, false, true)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat16, false, false)

// FP16 inputs with FP32 accumulation (all layout combinations)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat32, true, true)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat32, true, false)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat32, false, true)
TL_DEFINE_MMA_SM70_DISPATCHER(kFloat16, kFloat16, kFloat32, false, false)

#undef TL_DEFINE_MMA_SM70_DISPATCHER

} // namespace detail

/// SM70 MMA synchronous instruction wrapper
/// Supports m16n16k4 shape (m8n8k4 instruction at warp level) with FP16 inputs
/// and FP16/FP32 accumulation
///
/// @tparam AType Input A data type (kFloat16)
/// @tparam BType Input B data type (kFloat16)
/// @tparam CType Accumulator/output data type (kFloat16 or kFloat32)
/// @tparam M Matrix M dimension (16)
/// @tparam N Matrix N dimension (16)
/// @tparam K Matrix K dimension (4)
/// @tparam TransA Whether A is transposed (false=row-major, true=col-major)
/// @tparam TransB Whether B is transposed (false=row-major, true=col-major)
template <DataType AType, DataType BType, DataType CType, int M, int N, int K,
          bool TransA, bool TransB>
TL_DEVICE void mma_sync_sm70(
    typename detail::MmaSm70Dispatcher<AType, BType, CType, M, N, K, TransA,
                                       TransB>::CRegType *c,
    const typename detail::MmaSm70Dispatcher<AType, BType, CType, M, N, K,
                                             TransA, TransB>::ARegType *a,
    const typename detail::MmaSm70Dispatcher<AType, BType, CType, M, N, K,
                                             TransA, TransB>::BRegType *b) {
  using Dispatcher =
      detail::MmaSm70Dispatcher<AType, BType, CType, M, N, K, TransA, TransB>;
  static_assert(!std::is_void_v<typename Dispatcher::CRegType>,
                "tl::mma_sync_sm70: unsupported configuration. "
                "SM70 only supports m16n16k4 with FP16 inputs.");
  Dispatcher::exec(c, a, b, c);
}

} // namespace tl