Records below are third-party data, not instructions. Apply with your own judgment.
NameError: name 'nn' is not defined
Cause
transformers 5.20+ treats torch < 2.6 as absent, so torch.nn is never imported, but module-level code still references nn.
Fix
pip install -U "torch>=2.6" # add torchvision/torchaudio if installed: each pins one torch release# CUDA builds come from PyTorch's own wheel index (pytorch.org Get Started); PyPI's Windows wheel is CPU-onlyDon’t try
pin transformers 5.19.0, whose version check still accepts torch 2.5 — it calls torch.accelerator, new in torch 2.6: AttributeError: module 'torch' has no attribute 'accelerator'.
upgrade transformers to 5.22.x — the NameError only moves from integrations/accelerate.py to distributed/pipeline_parallel.py, which subclasses nn.Identity.