Skip to content

Honor masks in PyTorch nonzero - #4967

Closed
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/fix-pytorch-nonzero-masked-arrays
Closed

Honor masks in PyTorch nonzero#4967
FlorianPfaff wants to merge 2 commits into
mainfrom
agent/fix-pytorch-nonzero-masked-arrays

Conversation

@FlorianPfaff

Copy link
Copy Markdown
Owner

Bug

The PyTorch backend wrapper converted NumPy MaskedArray inputs with torch.as_tensor. That conversion discards the mask and exposes the hidden payload, so nonzero could report masked entries as present.

For example, a masked 2×2 input whose only unmasked nonzero value is at (1, 0) returned (0, 1), (1, 0), and (1, 1) on the PyTorch backend.

Fix

  • fill masked entries with zero before converting to a PyTorch tensor;
  • preserve the existing rejection of zero-dimensional inputs;
  • preserve ordinary list, NumPy-array, and tensor handling;
  • add regression coverage for both the public and raw PyTorch backend helpers.

Validation

  • reproduced the pre-fix mask loss with NumPy 2.3.5 and PyTorch 2.10.0;
  • verified the patched helper matches numpy.nonzero for the masked input;
  • verified existing scalar rejection and ordinary matrix behavior in the isolated harness;
  • syntax-compiled both modified files;
  • branch comparison: 2 commits ahead, 0 behind main, with only 2 files changed.

The full supported backend and Python-version matrix is delegated to GitHub Actions.

@github-actions

Copy link
Copy Markdown
Contributor

MegaLinter analysis: Success

Descriptor Linter Files Fixed Errors Warnings Elapsed time
✅ COPYPASTE jscpd yes no no 20.05s
✅ JSON prettier 7 0 0 0 0.79s
✅ JSON v8r 7 0 0 5.0s
✅ MARKDOWN markdownlint 68 0 0 0 1.24s
✅ MARKDOWN markdown-table-formatter 68 0 0 0 0.58s
✅ PYTHON black 1809 58 0 0 64.98s
✅ PYTHON isort 1809 90 0 0 2.45s
✅ REPOSITORY betterleaks yes no no 1.95s
✅ REPOSITORY checkov yes no no 38.81s
✅ REPOSITORY gitleaks yes no no 11.35s
✅ REPOSITORY git_diff yes no no 0.11s
✅ REPOSITORY secretlint yes no no 59.25s
✅ REPOSITORY syft yes no no 4.48s
✅ REPOSITORY trivy-sbom yes no no 4.47s
✅ REPOSITORY trufflehog yes no no 25.18s
✅ YAML prettier 11 0 0 0 0.63s
✅ YAML v8r 11 0 0 9.2s
✅ YAML yamllint 11 0 0 0.42s

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

Copy link
Copy Markdown
Owner Author

Closing as low value. This introduces NumPy masked-array semantics into the PyTorch nonzero compatibility layer, while masked arrays are not a native PyTorch input contract and are not supported consistently across PyRecEst. Isolated mask handling here would make backend behavior less coherent.

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