[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputs

オープン 初心者向け
#1,676 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

@shotsan がすでに取り組んでいます。

2026年11月24日 から。

  • #1682 @shotsan による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

liger_kernel/ops/group_norm.py:242-255 の group_norm_backward を読み、forward が空間次元をどのように平坦化しているかと比較してください。投稿された再現スクリプトを GPU 上で実行し、DX.view(*shape) でのクラッシュを確認します。そのうえで、hidden_size がすべての空間次元をカバーするように修正し(例: math.prod(shape[2:]))、tl.bfloat16 を固定で使うのではなく torch.float16 を tl.float16 に対応させます。4D の backward が F.group_norm と一致する (N, C, H, W) の勾配を返し、fp16 の重みとバイアスの勾配が bfloat16 を経由して変換されなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

Description

LigerGroupNormFunction has two bugs in group_norm_backward (liger_kernel/ops/group_norm.py:242-255):

  1. 4D backward truncation crash: On any standard 4D (N, C, H, W) tensor with H > 1, forward succeeds and matches F.group_norm, while .backward() crashes with RuntimeError: shape '[2, 16, 8, 8]' is invalid for input of size 256.
  2. Hardcoded tl.bfloat16 for float16 inputs: Line 255 sets triton_dtype = tl.float32 if X.dtype == torch.float32 else tl.bfloat16, silently casting torch.float16 weight/bias gradients through bfloat16 (7-bit mantissa instead of 10-bit).
  • Affected Version: liger-kernel==0.8.3 (torch==2.15.0+cu132, triton==3.9.0; also reproduced on Colab L4 sm_89 with torch==2.14.1)
  • Component: liger_kernel/ops/group_norm.py:242-255 (group_norm_backward)
Minimal Reproducer
import torch
import torch.nn.functional as F
from liger_kernel.ops import LigerGroupNormFunction

N, C, H, W, G = 2, 16, 8, 8, 4
x = torch.randn((N, C, H, W), device="cuda", dtype=torch.float32, requires_grad=True)
w = torch.randn((C,), device="cuda", dtype=torch.float32, requires_grad=True)
b = torch.randn((C,), device="cuda", dtype=torch.float32, requires_grad=True)

y = LigerGroupNormFunction.apply(x, w, b, C, G, 1e-5)
y_ref = F.group_norm(x.detach(), G, w.detach(), b.detach(), 1e-5)
print(f"4D Forward PASS: shape={tuple(y.shape)}, fwd_err={(y.detach() - y_ref).abs().max().item():.3e}")

# Crashes with: RuntimeError: shape '[2, 16, 8, 8]' is invalid for input of size 256
y.sum().backward()
Root Cause & Suggested Fix

In group_norm_backward (group_norm.py:243-250), hidden_size = dY.shape[-1] is read before flattening spatial dimensions (dY = dY.view(batch_size, num_groups, -1)), capturing only W (8) instead of H * W (64). Consequently DX is allocated with size B * C * W (256) instead of B * C * H * W (2048), the Triton kernel only processes a truncated slice, and DX.view(*shape) crashes on return.

Computing hidden_size = math.prod(shape[2:]) (or reading it after flattening spatial dimensions) and mapping torch.float16 -> tl.float16 (via torch_to_triton_dtype) fixes both issues.

主要言語
Python
スター
6.7k
フォーク
673
平均マージ
4日 20時間
マージ済み PR(30日)
19

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

linkedin/Liger-Kernel のほかの issue

linkedin/Liger-Kernel の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。