extensions.h 34.7 KB
Newer Older
Przemek Tredak's avatar
Przemek Tredak committed
1
/*************************************************************************
2
 * Copyright (c) 2022-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Przemek Tredak's avatar
Przemek Tredak committed
3
4
5
6
 *
 * See LICENSE for license information.
 ************************************************************************/

7
8
9
#ifndef TRANSFORMER_ENGINE_PYTORCH_CSRC_EXTENSIONS_H_
#define TRANSFORMER_ENGINE_PYTORCH_CSRC_EXTENSIONS_H_

Przemek Tredak's avatar
Przemek Tredak committed
10
#include "common.h"
Tim Moon's avatar
Tim Moon committed
11
#include "common/common.h"
Przemek Tredak's avatar
Przemek Tredak committed
12

13
14
15
16
/***************************************************************************************************
 * Attention
 **************************************************************************************************/

17
18
19
20
21
22
NVTE_Fused_Attn_Backend get_fused_attn_backend(
                const transformer_engine::DType q_dtype,
                const transformer_engine::DType kv_dtype,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
23
24
25
26
                float p_dropout,
                size_t num_attn_heads, size_t num_gqa_groups,
                size_t max_seqlen_q, size_t max_seqlen_kv,
                size_t head_dim);
cyanguwa's avatar
cyanguwa committed
27
28

std::vector<at::Tensor> fused_attn_fwd_qkvpacked(
29
                size_t max_seqlen, bool is_training,
30
31
32
33
                float attn_scale, float p_dropout, bool set_zero,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
cyanguwa's avatar
cyanguwa committed
34
35
36
                const at::Tensor cu_seqlens,
                const at::Tensor QKV,
                const transformer_engine::DType qkv_type,
37
38
39
40
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
cyanguwa's avatar
cyanguwa committed
41
                const c10::optional<at::Tensor> descale_QKV,
42
                const c10::optional<at::Tensor> descale_S,
cyanguwa's avatar
cyanguwa committed
43
44
45
46
47
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_O,
                c10::optional<at::Tensor> amax_S,
                c10::optional<at::Tensor> amax_O,
                const c10::optional<at::Tensor> Bias,
48
49
                const c10::optional<at::Generator> rng_gen,
                size_t rng_elts_per_thread);
cyanguwa's avatar
cyanguwa committed
50
51

std::vector<at::Tensor> fused_attn_bwd_qkvpacked(
52
                size_t max_seqlen, float attn_scale,
53
54
55
56
                float p_dropout, bool set_zero,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
cyanguwa's avatar
cyanguwa committed
57
58
59
60
61
                const at::Tensor cu_seqlens,
                const at::Tensor QKV,
                const at::Tensor O,
                const at::Tensor dO,
                const transformer_engine::DType qkv_type,
62
                const transformer_engine::DType dqkv_type,
cyanguwa's avatar
cyanguwa committed
63
                const std::vector<at::Tensor> Aux_CTX_Tensors,
64
65
66
67
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
cyanguwa's avatar
cyanguwa committed
68
69
70
71
                const c10::optional<at::Tensor> descale_QKV,
                const c10::optional<at::Tensor> descale_S,
                const c10::optional<at::Tensor> descale_O,
                const c10::optional<at::Tensor> descale_dO,
72
                const c10::optional<at::Tensor> descale_dP,
cyanguwa's avatar
cyanguwa committed
73
74
75
76
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_dP,
                const c10::optional<at::Tensor> scale_dQKV,
                c10::optional<at::Tensor> amax_dP,
77
                c10::optional<at::Tensor> amax_dQKV);
cyanguwa's avatar
cyanguwa committed
78
79

std::vector<at::Tensor> fused_attn_fwd_kvpacked(
80
                size_t max_seqlen_q, size_t max_seqlen_kv, bool is_training,
81
82
83
84
                float attn_scale, float p_dropout, bool set_zero,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
cyanguwa's avatar
cyanguwa committed
85
86
87
88
89
                const at::Tensor cu_seqlens_q,
                const at::Tensor cu_seqlens_kv,
                const at::Tensor Q,
                const at::Tensor KV,
                const transformer_engine::DType qkv_type,
90
91
92
93
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
cyanguwa's avatar
cyanguwa committed
94
                const c10::optional<at::Tensor> descale_QKV,
95
                const c10::optional<at::Tensor> descale_S,
cyanguwa's avatar
cyanguwa committed
96
97
98
99
100
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_O,
                c10::optional<at::Tensor> amax_S,
                c10::optional<at::Tensor> amax_O,
                const c10::optional<at::Tensor> Bias,
101
102
                const c10::optional<at::Generator> rng_gen,
                size_t rng_elts_per_thread);
cyanguwa's avatar
cyanguwa committed
103
104

std::vector<at::Tensor> fused_attn_bwd_kvpacked(
105
106
                size_t max_seqlen_q, size_t max_seqlen_kv,
                float attn_scale, float p_dropout, bool set_zero,
107
108
109
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
cyanguwa's avatar
cyanguwa committed
110
111
112
113
114
115
116
                const at::Tensor cu_seqlens_q,
                const at::Tensor cu_seqlens_kv,
                const at::Tensor Q,
                const at::Tensor KV,
                const at::Tensor O,
                const at::Tensor dO,
                const transformer_engine::DType qkv_type,
117
                const transformer_engine::DType dqkv_type,
cyanguwa's avatar
cyanguwa committed
118
                const std::vector<at::Tensor> Aux_CTX_Tensors,
119
120
121
122
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
cyanguwa's avatar
cyanguwa committed
123
124
                const c10::optional<at::Tensor> descale_QKV,
                const c10::optional<at::Tensor> descale_S,
125
126
                const c10::optional<at::Tensor> descale_O,
                const c10::optional<at::Tensor> descale_dO,
127
                const c10::optional<at::Tensor> descale_dP,
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_dP,
                const c10::optional<at::Tensor> scale_dQKV,
                c10::optional<at::Tensor> amax_dP,
                c10::optional<at::Tensor> amax_dQKV);

std::vector<at::Tensor> fused_attn_fwd(
                size_t max_seqlen_q, size_t max_seqlen_kv, bool is_training,
                float attn_scale, float p_dropout, bool set_zero,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
                const at::Tensor cu_seqlens_q,
                const at::Tensor cu_seqlens_kv,
                const at::Tensor Q,
                const at::Tensor K,
                const at::Tensor V,
                const transformer_engine::DType qkv_type,
146
147
148
149
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
150
                const c10::optional<at::Tensor> descale_QKV,
151
                const c10::optional<at::Tensor> descale_S,
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_O,
                c10::optional<at::Tensor> amax_S,
                c10::optional<at::Tensor> amax_O,
                const c10::optional<at::Tensor> Bias,
                const c10::optional<at::Generator> rng_gen,
                size_t rng_elts_per_thread);

std::vector<at::Tensor> fused_attn_bwd(
                size_t max_seqlen_q, size_t max_seqlen_kv,
                float attn_scale, float p_dropout, bool set_zero,
                NVTE_QKV_Layout qkv_layout,
                NVTE_Bias_Type bias_type,
                NVTE_Mask_Type attn_mask_type,
                const at::Tensor cu_seqlens_q,
                const at::Tensor cu_seqlens_kv,
                const at::Tensor Q,
                const at::Tensor K,
                const at::Tensor V,
                const at::Tensor O,
                const at::Tensor dO,
                const transformer_engine::DType qkv_type,
174
                const transformer_engine::DType dqkv_type,
175
                const std::vector<at::Tensor> Aux_CTX_Tensors,
176
177
178
179
                const c10::optional<at::Tensor> seq_offsets_q,
                const c10::optional<at::Tensor> seq_offsets_k,
                const c10::optional<at::Tensor> seq_offsets_v,
                const c10::optional<at::Tensor> seq_offsets_o,
180
181
                const c10::optional<at::Tensor> descale_QKV,
                const c10::optional<at::Tensor> descale_S,
cyanguwa's avatar
cyanguwa committed
182
183
                const c10::optional<at::Tensor> descale_O,
                const c10::optional<at::Tensor> descale_dO,
184
                const c10::optional<at::Tensor> descale_dP,
cyanguwa's avatar
cyanguwa committed
185
186
187
188
                const c10::optional<at::Tensor> scale_S,
                const c10::optional<at::Tensor> scale_dP,
                const c10::optional<at::Tensor> scale_dQKV,
                c10::optional<at::Tensor> amax_dP,
189
                c10::optional<at::Tensor> amax_dQKV);
Przemek Tredak's avatar
Przemek Tredak committed
190

191
192
193
at::Tensor fa_prepare_fwd(at::Tensor qkvi);
at::Tensor fa_prepare_bwd(at::Tensor q, at::Tensor k, at::Tensor v);

194
195
196
197
/***************************************************************************************************
 * GEMM
 **************************************************************************************************/

Przemek Tredak's avatar
Przemek Tredak committed
198
199
200
201
202
203
204
205
206
void te_gemm(at::Tensor A,
             at::Tensor A_scale_inverse,
             transformer_engine::DType A_type,
             bool transa,
             at::Tensor B,
             at::Tensor B_scale_inverse,
             transformer_engine::DType B_type,
             bool transb,
             at::Tensor D,
207
             at::Tensor D_scale,
Przemek Tredak's avatar
Przemek Tredak committed
208
             transformer_engine::DType D_type,
209
             at::Tensor D_amax,
Przemek Tredak's avatar
Przemek Tredak committed
210
             at::Tensor bias,
211
             transformer_engine::DType bias_type,
Przemek Tredak's avatar
Przemek Tredak committed
212
213
214
215
216
             at::Tensor pre_gelu_out,
             bool grad,
             at::Tensor workspace,
             size_t workspaceSize,
             bool accumulate,
217
218
             bool use_split_accumulator,
             int math_sm_count
Przemek Tredak's avatar
Przemek Tredak committed
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
void te_atomic_gemm(at::Tensor A,
                    at::Tensor A_scale_inverse,
                    transformer_engine::DType A_type,
                    bool transa,
                    at::Tensor B,
                    at::Tensor B_scale_inverse,
                    transformer_engine::DType B_type,
                    bool transb,
                    at::Tensor D,
                    at::Tensor D_scale,
                    transformer_engine::DType D_type,
                    at::Tensor D_amax,
                    at::Tensor bias,
                    transformer_engine::DType bias_type,
                    at::Tensor pre_gelu_out,
                    bool grad,
                    at::Tensor workspace,
                    size_t workspaceSize,
                    bool accumulate,
                    bool use_split_accumulator,
                    int math_sm_count,
                    int m_split,
                    int n_split,
                    bool gemm_producer,
                    at::Tensor counter
);
Przemek Tredak's avatar
Przemek Tredak committed
247

248
249
250
251
/***************************************************************************************************
 * Transpose
 **************************************************************************************************/

Przemek Tredak's avatar
Przemek Tredak committed
252
253
254
255
256
257
258
259
260
261
void fused_cast_transpose(at::Tensor input,
                          at::Tensor scale,
                          at::Tensor amax,
                          at::Tensor scale_inv,
                          at::Tensor input_cast,
                          at::Tensor input_transpose,
                          transformer_engine::DType otype
);


262
263
264
265
266
267
268
void fused_cast_transpose_noop(at::Tensor input,
                               at::Tensor noop,
                               at::Tensor scale,
                               at::Tensor amax,
                               at::Tensor scale_inv,
                               at::Tensor input_cast,
                               at::Tensor input_transpose,
269
270
271
272
                               transformer_engine::DType otype,
                               int scale_offset = 0,
                               int amax_offset = 0,
                               int scale_inv_offset = 0
273
274
275
);


Przemek Tredak's avatar
Przemek Tredak committed
276
277
278
279
std::vector<at::Tensor> fused_cast_transpose_bgrad(at::Tensor grad_output,
                                                   at::Tensor scale,
                                                   at::Tensor amax,
                                                   at::Tensor scale_inv,
280
281
282
283
                                                   transformer_engine::DType otype,
                                                   int scale_offset = 0,
                                                   int amax_offset = 0,
                                                   int scale_inv_offset = 0
Przemek Tredak's avatar
Przemek Tredak committed
284
285
286
);


287
std::vector<at::Tensor> fused_fp8_transpose_bgrad(at::Tensor grad_output,
288
289
290
291
292
293
294
295
                                                  at::Tensor scale,
                                                  at::Tensor amax,
                                                  at::Tensor scale_inv,
                                                  transformer_engine::DType otype,
                                                  transformer_engine::DType grad_bias_type,
                                                  int scale_offset = 0,
                                                  int amax_offset = 0,
                                                  int scale_inv_offset = 0
296
297
298
);


Przemek Tredak's avatar
Przemek Tredak committed
299
300
301
302
303
std::vector<at::Tensor> fused_cast_transpose_bgrad_dgelu(at::Tensor grad_output,
                                                         at::Tensor gelu_input,
                                                         at::Tensor scale,
                                                         at::Tensor amax,
                                                         at::Tensor scale_inv,
304
305
306
307
                                                         transformer_engine::DType otype,
                                                         int scale_offset = 0,
                                                         int amax_offset = 0,
                                                         int scale_inv_offset = 0
Przemek Tredak's avatar
Przemek Tredak committed
308
309
310
);


Tim Moon's avatar
Tim Moon committed
311
312
313
314
315
316
317
318
319
320
void fused_multi_cast_transpose(std::vector<at::Tensor> input_list,
                                std::vector<at::Tensor> scale_list,
                                std::vector<at::Tensor> cast_output_list,
                                std::vector<at::Tensor> transposed_output_list,
                                std::vector<at::Tensor> amax_output_list,
                                std::vector<at::Tensor> scale_inv_output_list,
                                transformer_engine::DType otype
);


Przemek Tredak's avatar
Przemek Tredak committed
321
322
323
324
at::Tensor fp8_transpose(at::Tensor input,
                         transformer_engine::DType otype
);

325
326
327
328
329
330
331
332
333
334
335
void fp8_transpose_noalloc(at::Tensor input,
                           at::Tensor output,
                           transformer_engine::DType otype
);

void fp8_transpose_noalloc_noop(at::Tensor input,
                                at::Tensor output,
                                at::Tensor noop,
                                transformer_engine::DType otype
);

336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/***************************************************************************************************
 * Activations
 **************************************************************************************************/

at::Tensor gelu(at::Tensor input,
                at::Tensor scale,
                at::Tensor amax,
                at::Tensor scale_inv,
                transformer_engine::DType otype
);

at::Tensor relu(at::Tensor input,
                at::Tensor scale,
                at::Tensor amax,
                at::Tensor scale_inv,
                transformer_engine::DType otype
);

at::Tensor geglu(at::Tensor input,
                 at::Tensor scale,
                 at::Tensor amax,
                 at::Tensor scale_inv,
                 transformer_engine::DType otype
);

at::Tensor reglu(at::Tensor input,
                 at::Tensor scale,
                 at::Tensor amax,
                 at::Tensor scale_inv,
                 transformer_engine::DType otype
);

at::Tensor swiglu(at::Tensor input,
                  at::Tensor scale,
                  at::Tensor amax,
                  at::Tensor scale_inv,
                  transformer_engine::DType otype
);

375
376
377
378
379
380
381
at::Tensor qgelu(at::Tensor input,
                  at::Tensor scale,
                  at::Tensor amax,
                  at::Tensor scale_inv,
                  transformer_engine::DType otype
);

382
383
384
385
386
387
388
at::Tensor srelu(at::Tensor input,
                at::Tensor scale,
                at::Tensor amax,
                at::Tensor scale_inv,
                transformer_engine::DType otype
);

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
at::Tensor dgelu(at::Tensor grad,
                 at::Tensor input,
                 transformer_engine::DType otype
);

at::Tensor drelu(at::Tensor grad,
                 at::Tensor input,
                 transformer_engine::DType otype
);

at::Tensor dgeglu(at::Tensor grad,
                  at::Tensor input,
                  transformer_engine::DType otype
);

at::Tensor dreglu(at::Tensor grad,
                  at::Tensor input,
                  transformer_engine::DType otype
);
Przemek Tredak's avatar
Przemek Tredak committed
408

409
410
411
at::Tensor dswiglu(at::Tensor grad,
                   at::Tensor input,
                   transformer_engine::DType otype
Przemek Tredak's avatar
Przemek Tredak committed
412
413
);

414
415
416
417
418
at::Tensor dqgelu(at::Tensor grad,
                   at::Tensor input,
                   transformer_engine::DType otype
);

419
420
421
422
423
at::Tensor dsrelu(at::Tensor grad,
                 at::Tensor input,
                 transformer_engine::DType otype
);

424
425
426
/***************************************************************************************************
 * LayerNorm
 **************************************************************************************************/
Przemek Tredak's avatar
Przemek Tredak committed
427
428
429
430
431

std::vector<at::Tensor> layernorm_bwd(const at::Tensor &dz,
                                      const at::Tensor &x,
                                      const at::Tensor &mu,
                                      const at::Tensor &rsigma,
432
                                      const at::Tensor &gamma,
433
434
                                      const int sm_margin,
                                      const bool zero_centered_gamma
Przemek Tredak's avatar
Przemek Tredak committed
435
436
437
438
439
440
441
442
443
444
);


std::vector<at::Tensor> layernorm_fwd_fp8(const at::Tensor &input,
                                          const at::Tensor &weight,
                                          const at::Tensor &bias,
                                          float eps,
                                          at::Tensor scale,
                                          at::Tensor amax,
                                          at::Tensor scale_inv,
445
                                          transformer_engine::DType otype,
446
                                          const int sm_margin,
447
448
449
450
                                          const bool zero_centered_gamma,
                                          const int scale_offset = 0,
                                          const int amax_offset = 0,
                                          const int scale_inv_offset = 0
Przemek Tredak's avatar
Przemek Tredak committed
451
452
);

453
454
455
456
457
458
459
460
461
462
std::vector<at::Tensor> layernorm_fwd_fp8_noalloc(const at::Tensor &input,
                                                  const at::Tensor &weight,
                                                  const at::Tensor &bias,
                                                  float eps,
                                                  at::Tensor scale,
                                                  at::Tensor ln_out,
                                                  at::Tensor amax,
                                                  at::Tensor scale_inv,
                                                  transformer_engine::DType otype,
                                                  const int sm_margin,
463
464
465
466
                                                  const bool zero_centered_gamma,
                                                  const int scale_offset = 0,
                                                  const int amax_offset = 0,
                                                  const int scale_inv_offset = 0
467
468
);

469
470
471
472
473
474
475
at::Tensor layernorm_fwd_fp8_inf(const at::Tensor &input,
                                 const at::Tensor &weight,
                                 const at::Tensor &bias,
                                 float eps,
                                 at::Tensor scale,
                                 at::Tensor amax,
                                 at::Tensor scale_inv,
476
                                 transformer_engine::DType otype,
477
                                 const int sm_margin,
478
479
480
481
                                 const bool zero_centered_gamma,
                                 const int scale_offset = 0,
                                 const int amax_offset = 0,
                                 const int scale_inv_offset = 0
482
);
Przemek Tredak's avatar
Przemek Tredak committed
483
484
485
486

std::vector<at::Tensor> layernorm_fwd(const at::Tensor &input,
                                      const at::Tensor &weight,
                                      const at::Tensor &bias,
487
                                      float eps,
488
489
                                      const int sm_margin,
                                      const bool zero_centered_gamma
Przemek Tredak's avatar
Przemek Tredak committed
490
491
);

492
493
494
495
496
497
498
499
500
std::vector<at::Tensor> layernorm_fwd_noalloc(const at::Tensor &input,
                                      const at::Tensor &weight,
                                      const at::Tensor &bias,
                                      at::Tensor ln_out,
                                      float eps,
                                      const int sm_margin,
                                      const bool zero_centered_gamma
);

501
502
503
at::Tensor layernorm_fwd_inf(const at::Tensor &input,
                             const at::Tensor &weight,
                             const at::Tensor &bias,
504
                             float eps,
505
                             const int sm_margin,
506
                             const bool zero_centered_gamma
507
);
Przemek Tredak's avatar
Przemek Tredak committed
508

509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/***************************************************************************************************
 * RMSNorm
 **************************************************************************************************/

std::vector<at::Tensor> rmsnorm_bwd(const at::Tensor &dz,
                                    const at::Tensor &x,
                                    const at::Tensor &rsigma,
                                    const at::Tensor &gamma,
                                    const int sm_margin,
                                    const bool zero_centered_gamma
);


std::vector<at::Tensor> rmsnorm_fwd_fp8(const at::Tensor &input,
                                        const at::Tensor &weight,
                                        float eps,
                                        at::Tensor scale,
                                        at::Tensor amax,
                                        at::Tensor scale_inv,
                                        transformer_engine::DType otype,
                                        const int sm_margin,
530
531
532
533
                                        const bool zero_centered_gamma,
                                        const int scale_offset = 0,
                                        const int amax_offset = 0,
                                        const int scale_inv_offset = 0
534
535
536
537
538
539
540
541
542
543
544
);

std::vector<at::Tensor> rmsnorm_fwd_fp8_noalloc(const at::Tensor &input,
                                                const at::Tensor &weight,
                                                float eps,
                                                at::Tensor scale,
                                                at::Tensor ln_out,
                                                at::Tensor amax,
                                                at::Tensor scale_inv,
                                                transformer_engine::DType otype,
                                                const int sm_margin,
545
546
547
548
                                                const bool zero_centered_gamma,
                                                const int scale_offset = 0,
                                                const int amax_offset = 0,
                                                const int scale_inv_offset = 0
549
550
551
552
553
554
555
556
557
);

at::Tensor rmsnorm_fwd_fp8_inf(const at::Tensor &input,
                               const at::Tensor &weight,
                               float eps,
                               at::Tensor scale,
                               at::Tensor amax,
                               at::Tensor scale_inv,
                               transformer_engine::DType otype,
558
                               const int sm_margin,
559
560
561
562
                               const bool zero_centered_gamma,
                               const int scale_offset = 0,
                               const int amax_offset = 0,
                               const int scale_inv_offset = 0
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
);

std::vector<at::Tensor> rmsnorm_fwd(const at::Tensor &input,
                                    const at::Tensor &weight,
                                    float eps,
                                    const int sm_margin,
                                    const bool zero_centered_gamma
);

std::vector<at::Tensor> rmsnorm_fwd_noalloc(const at::Tensor &input,
                                    const at::Tensor &weight,
                                    at::Tensor ln_out,
                                    float eps,
                                    const int sm_margin,
                                    const bool zero_centered_gamma
);

at::Tensor rmsnorm_fwd_inf(const at::Tensor &input,
                           const at::Tensor &weight,
                           float eps,
583
                           const int sm_margin,
584
585
586
587
588
589
590
                           const bool zero_centered_gamma
);

/***************************************************************************************************
 * Cast
 **************************************************************************************************/

Przemek Tredak's avatar
Przemek Tredak committed
591
592
593
594
595
596
597
598
at::Tensor cast_to_fp8(const at::Tensor &input,
                       const at::Tensor &scale,
                       at::Tensor amax,
                       at::Tensor scale_inv,
                       transformer_engine::DType otype
);


599
600
601
602
603
604
605
606
607
void cast_to_fp8_noalloc(const at::Tensor &input,
                         const at::Tensor &scale,
                         at::Tensor output,
                         at::Tensor amax,
                         at::Tensor scale_inv,
                         transformer_engine::DType otype
);


Przemek Tredak's avatar
Przemek Tredak committed
608
609
610
611
612
at::Tensor cast_from_fp8(const at::Tensor &input,
                         const at::Tensor &scale_inv,
                         transformer_engine::DType itype,
                         transformer_engine::DType otype
);
613

614
615
616
/***************************************************************************************************
 * Softmax
 **************************************************************************************************/
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649

at::Tensor scaled_softmax_forward(at::Tensor input,
                                  float scale_factor
);


at::Tensor scaled_softmax_backward(at::Tensor output_grad_,
                                   at::Tensor softmax_results_,
                                   float scale_factor
);


at::Tensor scaled_masked_softmax_forward(at::Tensor input,
                                         at::Tensor mask,
                                         float scale_factor
);


at::Tensor scaled_masked_softmax_backward(at::Tensor output_grad_,
                                          at::Tensor softmax_results_,
                                          float scale_factor
);


at::Tensor scaled_upper_triang_masked_softmax_forward(at::Tensor input,
                                                      float scale_factor
);


at::Tensor scaled_upper_triang_masked_softmax_backward(at::Tensor output_grads_,
                                                       at::Tensor softmax_results_,
                                                       float scale_factor
);
650

651
652
653
654
655
656
657
658
659
660
661

at::Tensor scaled_aligned_causal_masked_softmax_forward(at::Tensor input,
                                                        float scale_factor
);


at::Tensor scaled_aligned_causal_masked_softmax_backward(at::Tensor output_grads_,
                                                         at::Tensor softmax_results_,
                                                         float scale_factor
);

662
663
664
665
/***************************************************************************************************
 * FP8 recipe
 **************************************************************************************************/

666
667
668
669
670
671
672
void fused_amax_and_scale_update_after_reduction(const at::Tensor &amax_reduction_buffer,
                                                 std::vector<at::Tensor> amax_histories,
                                                 std::vector<at::Tensor> scales,
                                                 std::vector<at::Tensor> scale_invs,
                                                 const std::string &amax_compute_algo,
                                                 transformer_engine::DType fp8_dtype,
                                                 float margin);
673

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/***************************************************************************************************
 * Rotary positional embedding
 **************************************************************************************************/

at::Tensor fused_rope_forward(const at::Tensor &input,
                              const at::Tensor &freqs,
                              const bool transpose_output_memory
);

at::Tensor fused_rope_backward(const at::Tensor &output_grads,
                               const at::Tensor &freqs,
                               const bool transpose_output_memory
);

at::Tensor fused_rope_thd_forward(const at::Tensor &input,
                                  const at::Tensor &cu_seqlens,
                                  const at::Tensor &freqs
);

at::Tensor fused_rope_thd_backward(const at::Tensor &output_grads,
                                   const at::Tensor &cu_seqlens,
                                   const at::Tensor &freqs
);

/***************************************************************************************************
699
 * Miscellaneous
700
701
 **************************************************************************************************/

702
703
size_t get_cublasLt_version();

704
705
size_t get_cudnn_version();

706
void placeholder();
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748


/***************************************************************************************************
 * Support THD format for Context Parallel
 **************************************************************************************************/

at::Tensor thd_read_half_tensor(const at::Tensor &tensor,
                                const at::Tensor &cu_seqlens,
                                int half_idx
);

void thd_second_half_lse_correction(at::Tensor lse,
                                    const at::Tensor &lse_per_step,
                                    const at::Tensor &cu_seqlens,
                                    int total_tokens
);

at::Tensor thd_read_second_half_lse(const at::Tensor &lse,
                                    const at::Tensor &cu_seqlens,
                                    int total_tokens
);

void thd_out_correction(at::Tensor out,
                        const at::Tensor &out_per_step,
                        const at::Tensor &lse,
                        const at::Tensor &lse_per_step,
                        const at::Tensor &cu_seqlens,
                        bool only_second_half
);

void thd_grad_correction(at::Tensor grad,
                         const at::Tensor &grad_per_step,
                         const at::Tensor &cu_seqlens,
                         const std::string &first_half,
                         const std::string &second_half
);

at::Tensor thd_get_partitioned_indices(const at::Tensor &cu_seqlens,
                                       int total_tokens,
                                       int world_size,
                                       int rank
);
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789


/***************************************************************************************************
 * multi_tensor_* kernels
 **************************************************************************************************/

void multi_tensor_scale_cuda(int chunk_size, at::Tensor noop_flag,
                             std::vector<std::vector<at::Tensor>> tensor_lists, float scale);

std::tuple<at::Tensor, at::Tensor> multi_tensor_l2norm_cuda(
    int chunk_size, at::Tensor noop_flag, std::vector<std::vector<at::Tensor>> tensor_lists,
    at::optional<bool> per_tensor_python);

std::tuple<at::Tensor, at::Tensor> multi_tensor_unscale_l2norm_cuda(
    int chunk_size, at::Tensor noop_flag, std::vector<std::vector<at::Tensor>> tensor_lists,
    at::Tensor inv_scale, at::optional<bool> per_tensor_python);

void multi_tensor_adam_cuda(int chunk_size, at::Tensor noop_flag,
                            std::vector<std::vector<at::Tensor>> tensor_lists, const float lr,
                            const float beta1, const float beta2, const float epsilon,
                            const int step, const int mode, const int bias_correction,
                            const float weight_decay);

void multi_tensor_adam_capturable_cuda(int chunk_size, at::Tensor noop_flag,
                                       std::vector<std::vector<at::Tensor>> tensor_lists,
                                       at::Tensor lr, const float beta1, const float beta2,
                                       const float epsilon, at::Tensor step, const int mode,
                                       const int bias_correction, const float weight_decay,
                                       at::Tensor inv_scale);

void multi_tensor_adam_capturable_master_cuda(int chunk_size, at::Tensor noop_flag,
                                              std::vector<std::vector<at::Tensor>> tensor_lists,
                                              at::Tensor lr, const float beta1, const float beta2,
                                              const float epsilon, at::Tensor step, const int mode,
                                              const int bias_correction, const float weight_decay,
                                              at::Tensor inv_scale);

void multi_tensor_sgd_cuda(int chunk_size, at::Tensor noop_flag,
                           std::vector<std::vector<at::Tensor>> tensor_lists, float wd,
                           float momentum, float dampening, float lr, bool nesterov, bool first_run,
                           bool wd_after_momentum, float scale);
790
791

#endif  // TRANSFORMER_ENGINE_PYTORCH_CSRC_EXTENSIONS_H_