"qa/L1_jax_distributed_unittest/test.sh" did not exist on "ea43b18e666b2f21a40815297fb209e963816d06"
index.rst 17.5 KB
Newer Older
1
2
3
4
5
6
7
8
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
..
    Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

    See LICENSE for license information.

Getting Started
===============

Overview
--------

Transformer Engine (TE) is a library for accelerating Transformer models on NVIDIA GPUs,
providing better performance with lower memory utilization in both training and inference.
It provides support for 8-bit floating point (FP8) precision on Hopper and Ada GPUs, as well as
8-bit and 4-bit floating point (NVFP4) precision on Blackwell GPUs.

TE implements a collection of highly optimized building blocks for popular Transformer
architectures and exposes an automatic-mixed-precision-like API that can be used seamlessly
with your deep learning code.


Currently two frameworks are supported: PyTorch and JAX.

.. tabs::

   .. tab:: PyTorch

      Basic knowledge of PyTorch is recommended:

      - `PyTorch Tutorials <https://pytorch.org/tutorials/>`_
      - `PyTorch Documentation <https://pytorch.org/docs/stable/index.html>`_

   .. tab:: JAX

      We recommend understanding the basics of JAX first:

      - `Thinking in JAX <https://docs.jax.dev/en/latest/notebooks/thinking_in_jax.html>`_
      - `JAX 101 <https://docs.jax.dev/en/latest/jax-101.html>`_
      - `Key concepts in JAX <https://docs.jax.dev/en/latest/key-concepts.html>`_
      - `Flax 101 <https://flax-linen.readthedocs.io/en/latest/guides/flax_fundamentals/index.html>`_


Baseline: Pure Framework Implementation
---------------------------------------

Let's build a Transformer decoder layer!

We'll create a basic GPT-style layer with causal masking,
which prevents each position from attending to future positions. This will be our baseline
for later comparisons with Transformer Engine.

.. raw:: html
   :file: transformer_layer.svg

.. raw:: html

   <p style="text-align: center; font-style: italic; color: #666;">Structure of a GPT decoder layer</p>

We construct the components as follows:

.. tabs::

   .. tab:: PyTorch

      * **LayerNorm**: ``torch.nn.LayerNorm``
      * **QKV Projection**: ``torch.nn.Linear`` (fused Q, K, V into single layer 3x larger)
      * **DotProductAttention**: Custom implementation using ``torch.bmm``
      * **Projection**: ``torch.nn.Linear``
      * **Dropout**: ``torch.nn.Dropout``
      * **MLP**: Two ``torch.nn.Linear`` layers with ``torch.nn.functional.gelu`` activation

   .. tab:: JAX

      * **LayerNorm**: ``nn.LayerNorm``
      * **QKV Projection**: ``nn.Dense`` (fused Q, K, V into single layer 3x larger)
      * **DotProductAttention**: ``nn.dot_product_attention``
      * **Projection**: ``nn.Dense``
      * **Dropout**: ``nn.Dropout``
      * **MLP**: Two ``nn.Dense`` layers with ``nn.gelu`` activation

Putting it all together:

.. tabs::

   .. tab:: PyTorch

      First, define the MLP block:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BASELINE_MLP_START
         :end-before: # BASELINE_MLP_END

      Now, putting it all together into a GPT decoder layer:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BASELINE_LAYER_START
         :end-before: # BASELINE_LAYER_END

      Benchmark the baseline implementation:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_BASELINE_START
         :end-before: # BENCHMARK_BASELINE_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_BASELINE_OUTPUT_START
            :end-before: # BENCHMARK_BASELINE_OUTPUT_END

   .. tab:: JAX

      First, define the MLP block:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BASELINE_MLP_START
         :end-before: # BASELINE_MLP_END

      Now, putting it all together into a GPT decoder layer:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BASELINE_LAYER_START
         :end-before: # BASELINE_LAYER_END

      Benchmark the baseline implementation:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_BASELINE_START
         :end-before: # BENCHMARK_BASELINE_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_BASELINE_OUTPUT_START
            :end-before: # BENCHMARK_BASELINE_OUTPUT_END


TE Unfused: Basic TE Modules
----------------------------

Now let's replace the standard framework modules with TE equivalents.
This is the simplest way to start using Transformer Engine.

.. tabs::

   .. tab:: PyTorch

      Replace PyTorch modules with TE equivalents:

      .. code-block:: python

         import transformer_engine.pytorch as te

      Mapping:

      * ``torch.nn.Linear`` → ``te.Linear``
      * ``torch.nn.LayerNorm`` → ``te.LayerNorm``

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # TE_UNFUSED_MLP_START
         :end-before: # TE_UNFUSED_MLP_END

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # TE_UNFUSED_LAYER_START
         :end-before: # TE_UNFUSED_LAYER_END

      Benchmark the TE unfused implementation:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_START
         :end-before: # BENCHMARK_TE_UNFUSED_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_OUTPUT_END

   .. tab:: JAX

      Replace Flax modules with TE equivalents:

      .. code-block:: python

         import transformer_engine.jax as te
         import transformer_engine.jax.flax as te_flax

      Mapping:

      * ``nn.Dense`` → ``te_flax.DenseGeneral``
      * ``nn.LayerNorm`` → ``te_flax.LayerNorm``

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # TE_UNFUSED_MLP_START
         :end-before: # TE_UNFUSED_MLP_END

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # TE_UNFUSED_LAYER_START
         :end-before: # TE_UNFUSED_LAYER_END

      Benchmark the TE unfused implementation:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_START
         :end-before: # BENCHMARK_TE_UNFUSED_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_OUTPUT_END


TE Unfused + TE Attention
-------------------------

Now let's also replace the attention mechanism with TE's optimized ``DotProductAttention``.
TE's attention automatically selects the best available backend — for example, FlashAttention or cuDNN fused attention — based on your hardware and input configuration,
delivering optimal performance without manual tuning.

.. tabs::

   .. tab:: PyTorch

      Replace the custom attention with TE's optimized implementation:

      * Custom ``DotProductAttention`` → ``te.DotProductAttention``

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # TE_UNFUSED_ATTN_LAYER_START
         :end-before: # TE_UNFUSED_ATTN_LAYER_END

      Benchmark TE Unfused with TE Attention:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_ATTN_START
         :end-before: # BENCHMARK_TE_UNFUSED_ATTN_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_ATTN_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_ATTN_OUTPUT_END

   .. tab:: JAX

      Replace Flax's attention with TE's optimized implementation:

      * ``nn.dot_product_attention`` → ``te_flax.DotProductAttention``

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # TE_UNFUSED_ATTN_LAYER_START
         :end-before: # TE_UNFUSED_ATTN_LAYER_END

      Benchmark TE Unfused with TE Attention:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_ATTN_START
         :end-before: # BENCHMARK_TE_UNFUSED_ATTN_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_ATTN_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_ATTN_OUTPUT_END


TE Unfused + TE Attention + FP8
-------------------------------

Now let's combine TE modules with TE Attention and enable FP8 precision.
Wrap your code within an ``autocast`` context manager to enable FP8.
This provides significant speedups on supported hardware (Hopper, Ada, Blackwell GPUs).

.. tabs::

   .. tab:: PyTorch

      .. code-block:: python

         from transformer_engine.common.recipe import Format, DelayedScaling

         recipe = DelayedScaling(
             fp8_format=Format.HYBRID,
             amax_history_len=16,
             amax_compute_algo="max"
         )

         with te.autocast(enabled=True, recipe=recipe):
             y = te_unfused(x, attention_mask=None)

      .. note::

         The ``autocast`` should only wrap the forward pass and must exit before
         starting a backward pass.

      Benchmark TE Unfused with FP8:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_FP8_START
         :end-before: # BENCHMARK_TE_UNFUSED_FP8_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_FP8_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_FP8_OUTPUT_END

   .. tab:: JAX

      .. code-block:: python

         from transformer_engine.common.recipe import Format, DelayedScaling

         recipe = DelayedScaling(
             fp8_format=Format.HYBRID,
             amax_history_len=16,
             amax_compute_algo="max"
         )

         with te.autocast(enabled=True, recipe=recipe):
             params = te_unfused.init(key, x, deterministic=False)
             y = te_unfused.apply(params, x, deterministic=True)

      .. important::

         When using FP8 in JAX, the model **must be initialized within the autocast context**
         to create the ``fp8_metas`` collection.

      Benchmark TE Unfused with FP8:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_TE_UNFUSED_FP8_START
         :end-before: # BENCHMARK_TE_UNFUSED_FP8_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_TE_UNFUSED_FP8_OUTPUT_START
            :end-before: # BENCHMARK_TE_UNFUSED_FP8_OUTPUT_END


TE Fused + TE Attention + FP8: Optimized Modules
------------------------------------------------

Fused modules use kernel fusion to combine multiple operations.
While speedups are modest on a single GPU, they scale better in multi-GPU setups.
Combined with TE Attention and FP8, this delivers peak performance.

.. tabs::

   .. tab:: PyTorch

      Fused modules available:

      * ``te.LayerNormLinear`` - fuses LayerNorm + Linear
      * ``te.LayerNormMLP`` - fuses LayerNorm + MLP

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # TE_FUSED_LAYER_START
         :end-before: # TE_FUSED_LAYER_END

      Benchmark TE Fused with FP8:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_TE_FUSED_FP8_START
         :end-before: # BENCHMARK_TE_FUSED_FP8_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_TE_FUSED_FP8_OUTPUT_START
            :end-before: # BENCHMARK_TE_FUSED_FP8_OUTPUT_END

   .. tab:: JAX

      Fused modules available:

      * ``te_flax.LayerNormDenseGeneral`` - fuses LayerNorm + Dense
      * ``te_flax.LayerNormMLP`` - fuses LayerNorm + MLP

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # TE_FUSED_LAYER_START
         :end-before: # TE_FUSED_LAYER_END

      Benchmark TE Fused with FP8:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_TE_FUSED_FP8_START
         :end-before: # BENCHMARK_TE_FUSED_FP8_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_TE_FUSED_FP8_OUTPUT_START
            :end-before: # BENCHMARK_TE_FUSED_FP8_OUTPUT_END


TE TransformerLayer + FP8: Ready-to-use Module
----------------------------------------------

For the simplest integration, Transformer Engine provides a ready-to-use ``TransformerLayer``
module that includes all optimizations out of the box.

.. tabs::

   .. tab:: PyTorch

      Just use ``te.TransformerLayer`` - it handles everything for you:

      .. literalinclude:: getting_started_pytorch.py
         :language: python
         :start-after: # BENCHMARK_TE_TRANSFORMER_LAYER_START
         :end-before: # BENCHMARK_TE_TRANSFORMER_LAYER_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_pytorch.out
            :language: text
            :start-after: # BENCHMARK_TE_TRANSFORMER_LAYER_OUTPUT_START
            :end-before: # BENCHMARK_TE_TRANSFORMER_LAYER_OUTPUT_END

   .. tab:: JAX

      Just use ``te_flax.TransformerLayer`` - it handles everything for you:

      .. literalinclude:: getting_started_jax.py
         :language: python
         :start-after: # BENCHMARK_TE_TRANSFORMER_LAYER_START
         :end-before: # BENCHMARK_TE_TRANSFORMER_LAYER_END

      .. raw:: html

         <div style="background: #f5f5f5; border-left: 3px solid #9ca3af; padding: 4px 12px; font-size: 12px; color: #6b7280; margin-top: -16px;">
            Output:
         </div>

      .. container:: program-output

         .. literalinclude:: getting_started_jax.out
            :language: text
            :start-after: # BENCHMARK_TE_TRANSFORMER_LAYER_OUTPUT_START
            :end-before: # BENCHMARK_TE_TRANSFORMER_LAYER_OUTPUT_END


Benchmark Summary
-----------------

The table below summarizes the performance improvements achieved with Transformer Engine
on an NVIDIA H100 GPU. Results may vary depending on hardware and configuration. While this
tutorial focuses on a simple single-GPU scenario, features like fused layers can provide
additional benefits in more complex setups such as multi-GPU training.

.. tabs::

   .. tab:: PyTorch

      .. csv-table::
         :header-rows: 1
         :widths: 40, 20, 20
         :file: getting_started_pytorch_summary.csv

   .. tab:: JAX

      .. csv-table::
         :header-rows: 1
         :widths: 40, 20, 20
         :file: getting_started_jax_summary.csv