Skip to content

[BugFix] Exclude sparse padding from categorical entropy - #4513

Open
AHMETHAKANBEZIR1 wants to merge 1 commit into
pytorch:mainfrom
AHMETHAKANBEZIR1:fix/sparse-masked-entropy
Open

AHMETHAKANBEZIR1 wants to merge 1 commit into
pytorch:mainfrom
AHMETHAKANBEZIR1:fix/sparse-masked-entropy

Conversation

@AHMETHAKANBEZIR1

Copy link
Copy Markdown

Fixes #4512

Summary

Sparse MaskedCategorical.entropy() skipped masking entirely. Padded indices therefore produce NaN with neg_inf=-inf, or contribute to entropy when neg_inf is finite. A valid sparse action with probability zero has the same 0 * -inf problem.

Apply the existing finite-minimum masking and entropy renormalization to sparse padding and non-finite logits as well as dense invalid actions. Sampling, log_prob, mode and distribution construction are unchanged. This is independent of #4509 and #4511.

Tests in the existing distribution test file compare public entropy and gradients with compact torch.distributions.Categorical references. They cover both masked distribution classes, two padding sentinels, finite/infinite masking values, float32/float64, two batch dimensions and mixed padded/unpadded rows. Separate zero-probability tests include dense controls.

Validation

  • Current main 8f92a50: 18 new cases fail, 2 dense controls pass.
  • Final Windows / Python 3.12.14 / PyTorch 2.10 CPU, MaskedCategorical and MaskedOneHotCategorical selection: 88 passed, 917 deselected.
  • Both classes pass repeated torch.compile(backend="eager", fullgraph=True) entropy and gradient comparisons against the independent reference.
  • Full configured pre-commit run --all-files passes; git diff --check passes. The Windows symlink placeholder for AGENTS.md was normalized by the EOF hook during the initial run and restored afterward; it is not part of this change.
  • TorchRL Python sources are loaded from this checkout, using TensorDict 0.14.2's official wheel. The optional TorchRL C++ extension is unbuilt and emits its existing warning.
  • No CUDA/MPS/Inductor/C++ build, full test suite or full documentation build was run. No public API/documentation change or performance claim.

Issue #4512 was assigned to AHMETHAKANBEZIR1 through the repository's /assign workflow before implementation. Prepared with Codex assistance; the reproductions, independent reference/gradient checks, compile checks and lint checks were run locally, and Codex is recorded as co-author.

@pytorch-bot

pytorch-bot Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4513

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 16 Awaiting Approval

As of commit 85de661 with merge base 8f92a50 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 30, 2026

This branch has not been deployed

No deployments
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 Facebook bot. Authors need to sign the CLA before a PR can be reviewed. distributions Integrations/torch_geometric Integrations Modules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Sparse MaskedCategorical entropy includes padding or returns NaN

1 participant