Skip to content

Preserve PyTorch autograd in Mardia-Sutton PDFs - #5247

Open
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/preserve-mardia-sutton-pdf-autograd
Open

Preserve PyTorch autograd in Mardia-Sutton PDFs#5247
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/preserve-mardia-sutton-pdf-autograd

Conversation

@FlorianPfaff

Copy link
Copy Markdown
Owner

Bug

MardiaSuttonDistribution.pdf(...) evaluated its conditional Gaussian factor with scipy.stats.norm.pdf(...).

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 needlessly moves the Gaussian calculation outside the active backend.

Fix

Evaluate the same univariate Gaussian density directly with PyRecEst backend primitives:

exp(-0.5 * ((linear - muc) / sigmac) ** 2) / (sqrt(2 * pi) * sigmac)

The von-Mises factor and distribution parameters are unchanged. The PDF now preserves backend tensor type, dtype, device, and autograd history.

Regression coverage

Add a PyTorch-backend regression that:

  • evaluates two off-mode query points with requires_grad=True;
  • verifies finite, positive tensor-valued densities with matching dtype and device;
  • backpropagates through the PDF;
  • verifies finite, nonzero gradients with respect to the query points.

Validation

  • reproduced the old SciPy conversion failure with PyTorch 2.10;
  • compared the backend-native formula against SciPy for the regression parameters with zero observed absolute difference;
  • verified finite, nonzero gradients for both circular and linear coordinates;
  • branch comparison: 2 commits ahead and 0 behind main;
  • final diff is limited to 3 added / 1 removed production lines and one focused regression file.

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 27.87s
✅ JSON prettier 7 0 0 0 1.13s
✅ JSON v8r 7 0 0 5.35s
✅ MARKDOWN markdownlint 68 0 0 0 2.52s
✅ MARKDOWN markdown-table-formatter 68 0 0 0 1.13s
✅ PYTHON black 1866 87 0 0 102.03s
✅ PYTHON isort 1866 140 0 0 2.82s
✅ REPOSITORY betterleaks yes no no 2.68s
✅ REPOSITORY checkov yes no no 55.02s
✅ REPOSITORY gitleaks yes no no 18.09s
✅ REPOSITORY git_diff yes no no 0.34s
✅ REPOSITORY secretlint yes no no 88.43s
✅ REPOSITORY syft yes no no 6.22s
✅ REPOSITORY trivy-sbom yes no no 6.17s
✅ REPOSITORY trufflehog yes no no 33.7s
✅ YAML prettier 11 0 0 0 1.05s
✅ YAML v8r 11 0 0 12.56s
✅ YAML yamllint 11 0 0 0.65s

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:56
@FlorianPfaff
FlorianPfaff enabled auto-merge (squash) August 7, 2026 17:56
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