[Question] No gain in VRAM usage with LigerFusedLinearCrossEntropyLoss
Maintainers usually reply within 3 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Stale
- Domain
- machine-learning, performance
Research direction
Start at the LigerFusedLinearCrossEntropyLoss entry point and inspect how the provided hidden states, targets, weight, and bias are passed through the wrapper. Reproduce the author's Torch-versus-Liger comparison using the stated model and training settings, then establish whether the implementation accounts for the VRAM discrepancy and divergent loss.
Written by the indexing model from the issue text.
Description
Hello,
I am writing a codebase to train transformer models (almost finished, but it's still too early to share the entire framework) and I've just added Liger-Kernel support.
I was expecting a decreased RAM usage with the Fused Linear Cross Entropy implementation compared to naive torch's F.cross_entropy, however:
Torch => 22785MiB
Liger Fused => 22155MiB
Only 600MB saved. Vocab size is 32777 (maybe too small to see gain? Maybe because is not pow of 16?)
It's training a small test autoregressive model:
"hidden_size": 768,
"ffn_factor": 3.0,
"num_hidden_layers": 12,
"num_attention_heads": 12,
testing on an Nvidia 3090 GPU using Torch 2.6 cuda 12.4; training done in AMP with Pytorch Lightning, precision bf16.
Here is the transformer block with lm_head:
class TransformerWithLMHead(nn.Module):
"""
Adding an LM Head to TransformerWithEmbeddingHead. This is enough for Bert-like/GPT-like models.
"""
def __init__(self,config: ModelConfig,cache=None):
super().__init__()
self.cache = ensure_cache_and_registry(cache)
cache=self.cache
self.lm_head = ModuleWrapper(self.cache.registry.create("linear", "linear", in_features=config.hidden_size, out_features=config.vocab_size))
self.transformer = TransformerWithEmbeddingHead(config,cache=cache)
if config.tie_word_embeddings:
self.lm_head.weight = self.transformer.embed_tokens.weight
self.config=config
def forward(self,x,return_type='logits',**kwargs):
x=self.transformer(x,**kwargs)
if return_type=='logits':
return self.lm_head(x)
else:
return x
Here the relevant snippet from the training step:
if self.loss_type=='fused':
model_return_type = 'hidden'
flattening_dimension = self.config.hidden_size
loss_kwargs = {"lm_head_weight": self.model.lm_head.module.inner.weight}
if hasattr(self.model.lm_head, "bias"):
loss_kwargs["lm_head_bias"] = self.model.lm_head.module.inner.bias #TODO: Better way to access inner attributes of wrapped modules
And finally the way in which the Liger kernel is used:
@registry.register("loss", "cross_entropy_loss_fused", "liger", requires=["liger_kernel"], priority=0)
class LigerCrossEntropyLossFused(nn.Module):
def __init__(self, *args, **kwargs):
super().__init__()
cls = _load("liger_kernel.transformers", "LigerFusedLinearCrossEntropyLoss")
self.inner = cls(*args, **kwargs)
def forward(self, hidden, targets, **kwargs):
return self.inner(_input=hidden, target=targets, lin_weight=kwargs['lm_head_weight'], bias=kwargs.get("lm_head_bias", None))
Moreover, the loss diverges compared to torch:
Is it implemented correctly?
Thank you
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 673
- Avg merge
- 4d 20h
- Merged PRs (30d)
- 19
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from linkedin/Liger-Kernel
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
linkedin/Liger-Kernel#1693 ·
Maintainers usually reply within 3 days
-
[Bug] GroupNorm kernels overflow int32 sample offsets (`batch_idx * X_row_stride`) past 2^31 elementsPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
linkedin/Liger-Kernel#1692 ·
Maintainers usually reply within 3 days
-
[Bug] LigerGroupNormFunction.backward crashes on any 4D (N, C, H, W) tensor (H > 1) and hardcodes tl.bfloat16 for float16 inputsPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
linkedin/Liger-Kernel#1676 · 2 comments ·
Maintainers usually reply within 3 days
-
[Bug] LigerFusedGateUpSiLUMulFunction.backward crashes with shape mismatch on any 3D (B, T, 2*H) input tensorPossibly taken @shotsan claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
linkedin/Liger-Kernel#1675 · 1 comment ·
Maintainers usually reply within 3 days
-
[Bug] Silent backward gradient corruption in LigerCrossEntropy on non-contiguous logits (returns raw input logits as dL/dlogits)Possibly taken @shotsan claimed this 5 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
linkedin/Liger-Kernel#1674 ·
Maintainers usually reply within 3 days
All issues in linkedin/Liger-Kernel
Similar issues
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainers usually reply within 1 day
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPossibly taken @ronaldtse claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pydantic/pytest-examples#102 · 1 comment ·