Skip to content

Total Variance Minimization with PyTorch support #2673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 29, 2025

Conversation

MatteoFasulo
Copy link

Description

Implemented Total Variation Minimization using PyTorch with GPU support, closely modeled after the existing CPU-based scipy.optimize.minimize approach. The CPU version iteratively minimizes over each image in the batch and each channel individually, resulting in time complexity of O(B × C) where B is the batch size and C the number of channels. Additionally, loss computation on the CPU introduces extra latency per iteration. The new PyTorch-based implementation enables parallelized, accelerated computation directly on the GPU, significantly improving runtime performance.

Type of change

Please check all relevant options.

  • Improvement (non-breaking)
  • Bug fix (non-breaking)
  • New feature (non-breaking)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Testing

Extensive test coverage was added via

pytest tests/defences/preprocessor/test_variance_minimization_pytorch.py --framework="pytorch"
  • test_variance_minimization_pytorch.py

validating:

  • Memory efficiency
  • Input dimension and shape handling
  • Channel ordering (first vs. last)
  • Output clipping within expected value ranges
  • Convergence behavior
  • Reproducibility across multiple runs with consistent seeds

Test Configuration:

  • OS: Ubuntu 24.04 LTS
  • Python version: Python 3.10.15
  • ART version or commit number: 1.19.2
  • TensorFlow / Keras / PyTorch / MXNet version: PyTorch

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • My changes have been tested using both CPU and GPU devices

…t cases assessing memory usage, input size, channel dimension (first/last), clip values range, convergence, reproducibility and consistency across runs with same seed. L1 norm has a good convergence with torch LBFGS, L2 hyperparameters must be selected carefully

Signed-off-by: Matteo Fasulo <[email protected]>
@MatteoFasulo MatteoFasulo changed the base branch from main to dev_1.20.0 June 19, 2025 13:53
@beat-buesser
Copy link
Collaborator

Hi @MatteoFasulo Thank you for your pull request, I'll add a review in the next few days.

@beat-buesser beat-buesser self-requested a review June 24, 2025 17:59
@beat-buesser beat-buesser self-assigned this Jun 24, 2025
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

❌ Patch coverage is 92.13483% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.18%. Comparing base (df6b772) to head (8449450).
⚠️ Report is 9 commits behind head on dev_1.21.0.

Files with missing lines Patch % Lines
...nces/preprocessor/variance_minimization_pytorch.py 92.13% 4 Missing and 3 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           dev_1.21.0    #2673   +/-   ##
===========================================
  Coverage       85.17%   85.18%           
===========================================
  Files             329      330    +1     
  Lines           29731    29820   +89     
  Branches         4991     5007   +16     
===========================================
+ Hits            25324    25402   +78     
- Misses           2976     2983    +7     
- Partials         1431     1435    +4     
Files with missing lines Coverage Δ
...nces/preprocessor/variance_minimization_pytorch.py 92.13% <92.13%> (ø)

... and 4 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@beat-buesser
Copy link
Collaborator

@MatteoFasulo Could you please fix the style checks and CodeQL alerts above? Run Black formatter (version black==25.1.0) with black --line-length 120 art/ and black --line-length 120 tests/ to fix the formatting.

@MatteoFasulo
Copy link
Author

@MatteoFasulo Could you please fix the style checks and CodeQL alerts above? Run Black formatter (version black==25.1.0) with black --line-length 120 art/ and black --line-length 120 tests/ to fix the formatting.

Sure, I will do that in the next few days.

@beat-buesser beat-buesser added this to the ART 1.21.0 milestone Jun 30, 2025
@beat-buesser beat-buesser added the enhancement New feature or request label Jun 30, 2025
@beat-buesser beat-buesser changed the base branch from dev_1.20.0 to dev_1.21.0 June 30, 2025 12:45
@beat-buesser beat-buesser moved this to In Progress in ART 1.21.0 Jun 30, 2025
… variable in test variance minimization pytorch

Signed-off-by: Matteo Fasulo <[email protected]>
Copy link
Collaborator

@beat-buesser beat-buesser left a comment

Choose a reason for hiding this comment

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

Hi @MatteoFasulo Thank you very much for your pull request. I have added 3 minor change requests, could you please push the changes?

@MatteoFasulo
Copy link
Author

Hi @MatteoFasulo Thank you very much for your pull request. I have added 3 minor change requests, could you please push the changes?

Sure, I added the three suggested changes in the last commit. I removed the unused numpy import, added the eps parameter type hint and documentation.

Comment on lines +22 to +23
if TYPE_CHECKING:
from art.utils import CLIP_VALUES_TYPE
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if TYPE_CHECKING:
from art.utils import CLIP_VALUES_TYPE
if TYPE_CHECKING:
import torch
from art.utils import CLIP_VALUES_TYPE

@beat-buesser
Copy link
Collaborator

Hi @MatteoFasulo Thank you! I have missed this before, an additional import of torch is necessary for the typing to pass the Style Checks. Please add the code suggestion above and run Black formatter again to ensure correct line formatting. Afterwards we should be good to merge.

@MatteoFasulo
Copy link
Author

Hi @MatteoFasulo Thank you! I have missed this before, an additional import of torch is necessary for the typing to pass the Style Checks. Please add the code suggestion above and run Black formatter again to ensure correct line formatting. Afterwards we should be good to merge.

Hi @beat-buesser,
I added the missing import of torch and checked with black formatter. It should be good right now. Feel free to reach out if there is something wrong.

Copy link
Collaborator

@beat-buesser beat-buesser left a comment

Choose a reason for hiding this comment

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

Hi @MatteoFasulo Thank you very much for your contribution to ART!

@beat-buesser beat-buesser merged commit 76c207b into Trusted-AI:dev_1.21.0 Jul 29, 2025
25 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in ART 1.21.0 Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants