Skip to content

Preserve PyTorch autograd in partially wrapped normal PDFs - #5252

Open
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/preserve-partially-wrapped-normal-autograd
Open

Preserve PyTorch autograd in partially wrapped normal PDFs#5252
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/preserve-partially-wrapped-normal-autograd

Conversation

@FlorianPfaff

Copy link
Copy Markdown
Owner

Bug

PartiallyWrappedNormalDistribution.pdf(...) evaluated every wrapped Gaussian image through scipy.stats.multivariate_normal.

When the active backend is PyTorch and query points require gradients, SciPy attempts to convert the tensor to NumPy. This raises:

RuntimeError: Can't call numpy() on Tensor that requires grad.

Even for tensors without gradients, the SciPy boundary discards the active backend's dtype, device, and differentiation semantics before converting the result back.

Fix

Evaluate the Gaussian images through PyRecEst's existing backend-native GaussianDistribution.pdf(...) implementation. The wrapping, truncation order, covariance, and summation logic remain unchanged.

This uses SciPy for the NumPy backend, torch.distributions.MultivariateNormal for PyTorch, and jax.scipy.stats.multivariate_normal for JAX through the established Gaussian implementation.

Regression coverage

Add a PyTorch-backend regression that:

  • evaluates a two-dimensional partially wrapped normal at two off-boundary points with requires_grad=True;
  • verifies tensor type, output shape, dtype, device, positivity, and finiteness;
  • backpropagates through the wrapped density;
  • verifies finite, nonzero gradients with respect to the query points.

Validation

  • branch is based directly on current main (f196ec41d1d8681e279936db9f1a0a3abfa276e7);
  • branch is 2 commits ahead and 0 behind;
  • final diff is limited to 4 changed production lines and one focused 45-line regression test;
  • GitHub Actions is authoritative for the full backend, lint, packaging, documentation, and integration matrix.

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

MegaLinter analysis: Success

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ COPYPASTE jscpd yes no no 23.6s
✅ JSON prettier 7 0 0 0 0.93s
✅ JSON v8r 7 0 0 3.64s
✅ MARKDOWN markdownlint 68 0 0 0 1.86s
✅ MARKDOWN markdown-table-formatter 68 0 0 0 0.79s
✅ PYTHON black 1867 88 0 0 89.55s
✅ PYTHON isort 1867 142 0 0 2.75s
✅ REPOSITORY betterleaks yes no no 2.41s
✅ REPOSITORY checkov yes no no 50.63s
✅ REPOSITORY gitleaks yes no no 17.77s
✅ REPOSITORY git_diff yes no no 0.27s
✅ REPOSITORY secretlint yes no no 81.6s
✅ REPOSITORY syft yes no no 5.64s
✅ REPOSITORY trivy-sbom yes no no 4.93s
✅ REPOSITORY trufflehog yes no no 31.75s
✅ YAML prettier 11 0 0 0 0.73s
✅ YAML v8r 11 0 0 12.43s
✅ YAML yamllint 11 0 0 0.62s

Notices

📣 MegaLinter 9.5.0 is out! Discover the new features and security recommendations in the release announcement. (Skip this info by defining SECURITY_SUGGESTIONS: false)

See detailed reports in MegaLinter artifacts

Your project could benefit from a custom flavor, which would allow you to run only the linters you need, and thus improve runtime performances. (Skip this info by defining FLAVOR_SUGGESTIONS: false)

  • Documentation: Custom Flavors
  • Command: npx mega-linter-runner@9.6.0 --custom-flavor-setup --custom-flavor-linters PYTHON_BLACK,PYTHON_ISORT,COPYPASTE_JSCPD,JSON_V8R,JSON_PRETTIER,MARKDOWN_MARKDOWNLINT,MARKDOWN_MARKDOWN_TABLE_FORMATTER,REPOSITORY_CHECKOV,REPOSITORY_GIT_DIFF,REPOSITORY_GITLEAKS,REPOSITORY_BETTERLEAKS,REPOSITORY_SECRETLINT,REPOSITORY_SYFT,REPOSITORY_TRIVY_SBOM,REPOSITORY_TRUFFLEHOG,YAML_PRETTIER,YAML_YAMLLINT,YAML_V8R

MegaLinter is graciously provided by OX Security
Show us your support by starring ⭐ the repository

@FlorianPfaff
FlorianPfaff marked this pull request as ready for review August 7, 2026 17:55
@FlorianPfaff
FlorianPfaff enabled auto-merge (squash) August 7, 2026 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant