Lightning-AI / Lightning-AI/pytorch-lightning

Transformer Engine plugin fails to check weight exists for LayerNorm

Open Beginner friendly
#21,755 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug ver: 2.6.x
Dominant language
Python
Stars
31.4k
Forks
3.8k
Avg merge
6d 7h
Merged PRs (30d)
6

Description

Bug description

At https://github.com/Lightning-AI/pytorch-lightning/blob/master/src/lightning/fabric/plugins/precision/transformer_engine.py#L173

There is no check that the weights of the LayerNorm layer are not None

This means that if a LayerNorm layer is created using elementwise_affine=False we get the following error

Traceback (most recent call last):
  File "/usr/lib/python3.14/pdb.py", line 3656, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "/usr/lib/python3.14/pdb.py", line 2566, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "/usr/lib/python3.14/bdb.py", line 913, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "/home/nvme/budget-flow-matching/src/main.py", line 212, in <module>
    trainer.fit(model, dataloader, val_dataloaders=val_dataloader, ckpt_path=args.continue_from)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/pytorch/trainer/trainer.py", line 584, in fit
    call._call_and_handle_interrupt(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self,
        ^^^^^
    ...<6 lines>...
        weights_only,
        ^^^^^^^^^^^^^
    )
    ^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/pytorch/trainer/call.py", line 49, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/pytorch/trainer/trainer.py", line 630, in _fit_impl
    self._run(model, ckpt_path=ckpt_path, weights_only=weights_only)
    ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/pytorch/trainer/trainer.py", line 1053, in _run
    self.strategy.setup(self)
    ~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/pytorch/strategies/strategy.py", line 154, in setup
    self.model = self.precision_plugin.convert_module(self.model)
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py", line 105, in convert_module
    _convert_layers(module)
    ~~~~~~~~~~~~~~~^^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py", line 182, in _convert_layers
    _convert_layers(child)
    ~~~~~~~~~~~~~~~^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py", line 182, in _convert_layers
    _convert_layers(child)
    ~~~~~~~~~~~~~~~^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py", line 182, in _convert_layers
    _convert_layers(child)
    ~~~~~~~~~~~~~~~^^^^^^^
  File "/home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py", line 173, in _convert_layers
    replacement.weight.data = child.weight.data.clone()
                              ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'data'
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/henry/.local/share/virtualenvs/budget-flow-matching-u9VKU3BC/lib/python3.14/site-packages/lightning/fabric/plugins/precision/transformer_engine.py(173)_convert_layers()
-> replacement.weight.data = child.weight.data.clone()
(Pdb) child
LayerNorm((896,), eps=1e-06, elementwise_affine=False, bias=False)
What version are you seeing the problem on?

v2.6

Reproduced in studio

No response

How to reproduce the bug
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset

import lightning as L


class DummyDataset(Dataset):
    def __len__(self):
        return 8

    def __getitem__(self, idx):
        return torch.randn(16), torch.randn(2)

class BuggyModel(L.LightningModule):
    def __init__(self):
        super().__init__()
        # elementwise_affine=False sets weight and bias to None, triggering the bug
        self.layer_norm = nn.LayerNorm(16, elementwise_affine=False)
        self.linear = nn.Linear(16, 2)

    def forward(self, x):
        return self.linear(self.layer_norm(x))

    def training_step(self, batch, batch_idx):
        x, y = batch
        loss = nn.functional.mse_loss(self(x), y)
        return loss

    def configure_optimizers(self):
        return torch.optim.Adam(self.parameters(), lr=0.001)

if __name__ == "__main__":
    model = BuggyModel()
    dataset = DummyDataset()
    dataloader = DataLoader(dataset, batch_size=2)

    # Initialize the trainer with the Transformer Engine precision Lugin
    trainer = L.Trainer(
        max_epochs=1,
        accelerator="cuda",
        devices=1,
        precision="transformer-engine"
    )

    print("Starting trainer.fit()...")
    # This will crash inside trainer.fit() during the strategy setup phase
    trainer.fit(model, dataloader)
    def __getitem__(self, idx):
        return torch.randn(16), torch.randn(2)
Error messages and logs
# Error messages and logs here please
Environment
Current environment
  • CUDA:
    • GPU:
      • NVIDIA GeForce RTX 4080
    • available: True
    • version: 13.2
  • Lightning:
    • lightning: 2.6.5
    • lightning-utilities: 0.15.3
    • pytorch-lightning: 2.6.5
    • torch: 2.12.0+cu132
    • torch_fidelity: 0.4.0
    • torchdiffeq: 0.2.5
    • torchmetrics: 1.9.0
    • torchvision: 0.27.0+cu132
    • transformer_engine_torch: 2.15.0
  • Packages:
    • GitPython: 3.1.50
    • Jinja2: 3.1.6
    • MarkupSafe: 3.0.3
    • PySocks: 1.7.1
    • PyYAML: 6.0.3
    • Pygments: 2.20.0
    • aiohappyeyeballs: 2.6.2
    • aiohttp: 3.14.0
    • aiosignal: 1.4.0
    • annotated-doc: 0.0.4
    • annotated-types: 0.7.0
    • anyio: 4.13.0
    • attrs: 26.1.0
    • beautifulsoup4: 4.14.3
    • braceexpand: 0.1.7
    • certifi: 2026.5.20
    • charset-normalizer: 3.4.7
    • click: 8.4.1
    • cuda-bindings: 13.3.1
    • cuda-pathfinder: 1.5.5
    • cuda-toolkit: 13.2.1
    • datasets: 4.8.4
    • diffusers: 0.38.0
    • dill: 0.4.1
    • einops: 0.8.2
    • filelock: 3.29.1
    • flash_attn_3: 3.0.0+cu132torch2.12gite2743ab
    • frozenlist: 1.8.0
    • fsspec: 2026.2.0
    • gdown: 6.1.0
    • gitdb: 4.0.12
    • h11: 0.16.0
    • hf-xet: 1.5.0
    • httpcore: 1.0.9
    • httpx: 0.28.1
    • huggingface_hub: 1.17.0
    • idna: 3.18
    • importlib_metadata: 9.0.0
    • joblib: 1.5.3
    • kernels: 0.14.1
    • kernels-data: 0.15.2
    • lightning: 2.6.5
    • lightning-utilities: 0.15.3
    • markdown-it-py: 4.2.0
    • mdurl: 0.1.2
    • ml_dtypes: 0.5.4
    • mpmath: 1.3.0
    • multidict: 6.7.1
    • multiprocess: 0.70.19
    • narwhals: 2.22.0
    • networkx: 3.6.1
    • ninja: 1.13.0
    • numpy: 2.4.6
    • nvdlfw_inspect: 0.2.2
    • nvidia-cublas: 13.4.0.1
    • nvidia-cuda-cupti: 13.2.75
    • nvidia-cuda-nvrtc: 13.2.78
    • nvidia-cuda-runtime: 13.2.75
    • nvidia-cudnn-cu13: 9.20.0.48
    • nvidia-cufft: 12.2.0.46
    • nvidia-cufile: 1.17.1.22
    • nvidia-curand: 10.4.2.55
    • nvidia-cusolver: 12.2.0.1
    • nvidia-cusparse: 12.7.10.1
    • nvidia-cusparselt-cu13: 0.8.1
    • nvidia-nccl-cu13: 2.29.7
    • nvidia-nvjitlink: 13.3.33
    • nvidia-nvshmem-cu13: 3.4.5
    • nvidia-nvtx: 13.2.75
    • onnx: 1.21.0
    • onnx-ir: 0.2.1
    • onnxscript: 0.7.0
    • packaging: 26.2
    • pandas: 3.0.3
    • pillow: 12.2.0
    • pip: 26.1.2
    • platformdirs: 4.10.0
    • propcache: 0.5.2
    • protobuf: 7.35.0
    • psutil: 7.2.2
    • pyarrow: 23.0.1
    • pydantic: 2.13.4
    • pydantic_core: 2.46.4
    • python-dateutil: 2.9.0.post0
    • pytorch-lightning: 2.6.5
    • regex: 2026.5.9
    • requests: 2.34.2
    • rich: 15.0.0
    • safetensors: 0.8.0rc1
    • scikit-learn: 1.9.0
    • scipy: 1.17.1
    • sentence-transformers: 5.5.1
    • sentry-sdk: 2.61.1
    • setuptools: 81.0.0
    • shellingham: 1.5.4
    • six: 1.17.0
    • smmap: 5.0.3
    • soupsieve: 2.8.4
    • sympy: 1.14.0
    • threadpoolctl: 3.6.0
    • tokenizers: 0.22.2
    • tomlkit: 0.15.0
    • torch: 2.12.0+cu132
    • torch_fidelity: 0.4.0
    • torchdiffeq: 0.2.5
    • torchmetrics: 1.9.0
    • torchvision: 0.27.0+cu132
    • tqdm: 4.67.3
    • transformer_engine: 2.15.0
    • transformer_engine_cu13: 2.15.0
    • transformer_engine_torch: 2.15.0
    • transformers: 5.9.0
    • triton: 3.7.0
    • typer: 0.25.1
    • typing-inspection: 0.4.2
    • typing_extensions: 4.15.0
    • urllib3: 2.7.0
    • wandb: 0.27.0
    • webdataset: 1.0.2
    • xxhash: 3.7.0
    • yarl: 1.24.2
    • zipp: 4.1.0
  • System:
    • OS: Linux
    • architecture:
      • 64bit
      • ELF
    • processor:
    • python: 3.14.5
    • release: 7.0.10-arch1-1
    • version: #1 SMP PREEMPT_DYNAMIC Sat, 23 May 2026 14:21:20 +0000
More info

Should be fixable with a simple

if child.weight is not None and replacement.weight is not None:

cc @ethanwharris

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/lightning/fabric/plugins/precision/transformer_engine.py at _convert_layers around line 173, then run the provided LayerNorm reproduction with elementwise_affine=False and transformer-engine precision. Confirm that conversion handles missing LayerNorm weights without the reported AttributeError and that the training setup completes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.