Fix ZeroDivisionError in fused linear cross entropy when weight vocab dim is 0#1289
Open
nileshpatil6 wants to merge 1 commit into
Open
Fix ZeroDivisionError in fused linear cross entropy when weight vocab dim is 0#1289nileshpatil6 wants to merge 1 commit into
nileshpatil6 wants to merge 1 commit into
Conversation
… dim is 0 When weight has vocab dimension 0, for example when accessing a DeepSpeed ZeRO-3 partitioned parameter directly before it is gathered, inc_factor in fused_linear_cross_entropy_forward becomes 0 and the subsequent triton.cdiv(BT, inc_factor) call raises a bare ZeroDivisionError deep inside triton with no context about what went wrong. This adds an assertion that fails early with a message explaining the likely cause, and adds a regression test that reproduces the original crash and confirms the new error message. Fixes linkedin#767
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #767
Bug
fused_linear_cross_entropy_forwardcomputes:If
weighthas a vocab dimension of 0,inc_factorbecomes 0 and the nexttriton.cdivcall divides by zero, raising a bareZeroDivisionErrorfrom deep inside triton with no indication of what actually went wrong.Root cause
This happens when
weightis not actually materialized yet when it reaches the kernel. In the reported case, the user was training under DeepSpeed ZeRO-3 with parameter offload, andmodel.lm_head.weightwas an empty tensor at the point the Liger loss function accessed it directly (the raw parameter, not through the module forward that normally triggers the all-gather hook). Same class of bug would show up for any other reasonweightends up empty.Fix
Added an assertion right after
Vis computed that fails with a clear message pointing at the likely cause (unmaterialized weight, e.g. ZeRO-3 parameter accessed before it's gathered), instead of letting execution fall through to a cryptic ZeroDivisionError three calls deep in triton internals.Testing
Added
test_empty_weight_raises_clear_errorintest/transformers/test_fused_linear_cross_entropy.py. It callsfused_linear_cross_entropy_forwarddirectly with aweighttensor of shape(0, H)and checks the new assertion fires with the expected message.I confirmed this reproduces the exact bug from the issue: reverting just the source change and running the test gives:
With the fix applied:
Also ran
ruff checkandruff format --checkon both changed files, both clean. Full test file collection (138 tests) still succeeds with no import errors.Note: my dev box doesn't have the CUDA toolkit/MSVC set up for compiling triton kernels, so I could not run the full GPU correctness/convergence suite. The new test and the guard itself run entirely on the host before any kernel is launched, so this was verified directly.