Skip to content

Fix gamma-zero focal loss NaN - #576

Open
steve5636 wants to merge 1 commit into
facebookresearch:mainfrom
steve5636:fix/focal-gamma0-nan
Open

Fix gamma-zero focal loss NaN#576
steve5636 wants to merge 1 commit into
facebookresearch:mainfrom
steve5636:fix/focal-gamma0-nan

Conversation

@steve5636

Copy link
Copy Markdown

Fixes #575

Summary

This PR fixes two related numerical issues around the SAM3 presence loss path:

  • route sigmoid_focal_loss(..., gamma=0) away from the reduced Triton focal kernel and through the existing PyTorch fallback path
  • make the intended presence-logit clamp effective by assigning the result of clamp(...)

For gamma=0, sigmoid focal loss is equivalent to alpha-weighted BCE. The reduced Triton backward currently computes (1 - p_t) ** (gamma - 1), which becomes 0 ** -1 = inf for saturated correct predictions, and later produces 0 * inf = NaN.

Verification

  • python -m py_compile sam3/train/loss/loss_fns.py sam3/model/decoder.py
  • CUDA smoke test with sigmoid_focal_loss(x=[[18.0]], y=[[1.0]], alpha=0.5, gamma=0.0) now returns a finite gradient ([[0.0]]).

I also verified this locally on a SAM3 fine-tuning run where the original issue first appeared as a NaN gradient in SigmoidFocalLossReducedBackward; after the fix, the epoch-6 canary reached the final dataloader step with finite loss and outputs.

@meta-cla

meta-cla Bot commented Jun 5, 2026

Copy link
Copy Markdown

Hi @steve5636!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jun 5, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduced Triton sigmoid focal loss returns NaN gradients for gamma=0 when logits saturate

1 participant