Skip to content

fix(augmentation): keep inverse mask sampling flags local - #5300

Merged
ducha-aiki merged 2 commits into
kornia:mainfrom
Ai-Eastern:fix/inverse-mask-flags-5290
Oct 2, 2026
Merged

ducha-aiki merged 2 commits into
kornia:mainfrom
Ai-Eastern:fix/inverse-mask-flags-5290

Conversation

@Ai-Eastern

Copy link
Copy Markdown
Contributor

Fixes #5290
Copy the flags dictionary inside inverse_masks so temporary mask sampling overrides cannot leak into the caller’s state, including when inverse_inputs raises. Preserve normal interpolation, explicit resample overrides, align_corners=None normalization and the original exception. This does not change the container data-key handling in #5149.
Real regressions cover mask-first and mask-only container inverses, direct calls with shared/separate flags, image processing after an exception, and successful nearest/bilinear inverse sampling with None/False/True align_corners overrides.
Validation by Codex on Linux CPU, Python 3.12.14 and PyTorch 2.9.1+cpu:

  • Before the fix: 16 failed, 12 passed
  • Final patch: all 28 focused regressions and full pre-commit passed
  • Container suite: 1633 passed, 464 skipped
  • Augmentation suite: 4325 passed, 3406 skipped, 27 xfailed, 5 xpassed
  • Augmentation doctests: 84 passed; ty 0.0.9, changelog preview and diff checks passed
    The two broader suites ran before a final test-style-only correction; the runtime implementation was unchanged. Focused tests and pre-commit were rerun on the final files. Individual XPASS cases and skip reasons were not captured, and no untouched-base comparison was run. Full-repository tests, GPU/MPS, half precision and Inductor were not run. Pixi was unavailable, so equivalent underlying commands ran in an isolated virtualenv. Remote CI remains unverified.
    AI use: Codex investigated the issue, wrote the fix/tests/changelog, ran the listed checks, reviewed the diff and prepared this description. The contributor authorized the scope and publication; the checks were performed by Codex.

@Ai-Eastern
Ai-Eastern marked this pull request as ready for review October 1, 2026 17:03

@ducha-aiki ducha-aiki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Ai-Eastern. Copying flags at the top of inverse_masks is the first fix option from #5290, and it covers both overrides: resample and align_corners. On the success path the output is unchanged. inverse_inputs already merges kwargs into a copy (override_parameters(..., in_place=False)), so nothing outside the call relied on the in-place writes.

Measured on this branch merged with main 8ee1a67 (torch 2.14, CPU), using the repro from #5290:

  • On main, after the raising inverse, the module keeps Resample.NEAREST, and the next image differs from a fresh container by 0.7621. This happens with call-time keys ["mask", "input"] and with ["mask"].
  • On this PR, the flag stays BILINEAR and the difference is 0.
  • A direct inverse_masks(..., align_corners=None/False/True) call that raises leaves flags unchanged. On main it leaves resample at nearest and, for False, align_corners at False.

On the tests:

  • With your tests overlaid on main, 8 of the 14 fail. The 6 success-path cases pass there, as expected, because main restores the flags when the inverse does not raise.
  • On the merged tree all 14 pass on CPU f32, f64, f16 and bf16 (with the known-failure profiles) and on MPS f32.
  • tests/augmentation/container/, test_base.py and test_half_precision_ci.py give 1893 passed. That includes the RNG audit; your tests sit below the audited line.
  • Four one-token mutants of inverse_masks are killed by your tests: dropping the copy, nearest → bilinear, dropping the align_corners=None normalisation, and normalising to False.

The branch merges cleanly with main, and CI is green. Approving.

Posted on behalf of @ducha-aiki by Claude (Opus 5.5).

@ducha-aiki
ducha-aiki enabled auto-merge (squash) October 2, 2026 08:57
@ducha-aiki
ducha-aiki merged commit fbb1dd1 into kornia:main Oct 2, 2026
33 checks passed
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.

GeometricAugmentationBase2D.inverse_masks leaves the module's resample flag at nearest when the inverse raises

2 participants