LigerPolyNorm defaults to in_place=True (docstring says False) and overwrites shared gradients

Aperta Adatta ai principianti
#1,647 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 3 giorni

@nazek52 ci sta già lavorando.

Dal 17/11/2026.

  • #1648 di @nazek52 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Inizia da src/liger_kernel/transformers/poly_norm.py e src/liger_kernel/ops/poly_norm.py, poi leggi test/transformers/test_poly_norm.py e controlla come copre il comportamento predefinito di in_place. Riproduci i casi del gradiente residuo e del gradiente del chiamante descritti nell’issue, quindi verifica che i gradienti attesi corrispondano al riferimento PyTorch e che il gradiente fornito rimanga invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Describe the bug

transformers/poly_norm.py#L20 documents in_place as "(default: False)", but #L24 (and ops/poly_norm.py#L356) use True. With in_place=True, backward stores dX into grad_output (ops/poly_norm.py#L296-L297).

So with default arguments:

  • in a residual connection y = norm(x) + x, autograd passes the same gradient tensor to both branches, and the residual branch receives dX instead of the upstream gradient;
  • y.backward(g) overwrites the caller's g.
Reproduce
import torch
from liger_kernel.transformers import LigerPolyNorm

dev = "cuda" if torch.cuda.is_available() else "cpu"

def ref(x, w, b, eps=1e-6):                           # PolyNorm, as in test/transformers/test_poly_norm.py
    n = lambda u: u * torch.rsqrt(u.pow(2).mean(-1, keepdim=True) + eps)
    return w[0] * n(x ** 3) + w[1] * n(x ** 2) + w[2] * n(x) + b

torch.manual_seed(0)
x = torch.randn(4, 64, device=dev)
g = torch.randn(4, 64, device=dev)
m = LigerPolyNorm().to(dev)                           # default arguments
print("in_place default:", m.in_place, "(docstring says: default False)")
w, b = m.weight.detach().double(), m.bias.detach().double()

x1 = x.clone().requires_grad_()
(m(x1) + x1).backward(g.clone())                      # residual connection
x2 = x.double().requires_grad_()
(ref(x2, w, b) + x2).backward(g.double())
print("y = norm(x) + x: max |x.grad - fp64 reference| =", (x1.grad.double() - x2.grad).abs().max().item())

x3, g3 = x.clone().requires_grad_(), g.clone()
m(x3).backward(g3)
print("y.backward(g): the caller's g changed by", (g3 - g).abs().max().item())

Output:

in_place default: True (docstring says: default False)
y = norm(x) + x: max |x.grad - fp64 reference| = 2.8915757511071902
y.backward(g): the caller's g changed by 2.891575813293457
Expected behavior

x.grad equal to the fp64 PyTorch PolyNorm reference (as in test/transformers/test_poly_norm.py), and g unchanged.

Suggested fix

Use in_place=False as the default (as documented), or detect a shared gradient.

Versions / environment
  • Liger-Kernel 83746f4 (the lines cited above are unchanged on main as of 2026-11-17).
  • torch 2.14.1, triton 3.8.0. Reproduced with Triton's interpreter mode (TRITON_INTERPRET=1).
  • The root cause is in host-side Python / integer index arithmetic, so it is the same on CUDA. The script runs on cuda when it is available.
Lingua principale
Python
Stelle
6.7k
Fork
668
Merge medio
4g 12h
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di linkedin/Liger-Kernel

Tutte le issue di linkedin/Liger-Kernel

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.