Skip to content
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

Update projected_gradient_descent_pytorch.py [Solve non-writable NumPy array and device mismatch issues] #2455

Merged

Conversation

salomonhotegni
Copy link
Contributor

Description

This pull request addresses two issues in the ProjectedGradientDescent attack of the Adversarial Robustness Toolbox (ART):

  1. A UserWarning indicating that a given NumPy array is not writable, leading to potential undefined behavior when converting it to a PyTorch tensor.
  2. A RuntimeError caused by tensors being on different devices (CPU and CUDA) during the _projection function.

The solution involves copying the non-writable NumPy array to ensure it is writable and modifying the tensor conversion in the _projection function to ensure all tensors are on the same device.

Fixes # (issue)

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

I have performed the following tests to verify the changes:

  • Verified that the UserWarning about non-writable NumPy arrays no longer appears.
  • Confirmed that the RuntimeError related to device mismatch is resolved.

Test Configuration:

  • OS: Linux-5.15.0-91-generic-x86_64-with-glibc2.35
  • Python version: 3.9.7
  • ART version: 1.18.0
  • PyTorch version: 1.13.1+cu117

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

Copy link

codecov bot commented Jun 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.70%. Comparing base (a20c78f) to head (d79e663).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           dev_1.18.1    #2455   +/-   ##
===========================================
  Coverage       85.69%   85.70%           
===========================================
  Files             329      329           
  Lines           30075    30075           
  Branches         5559     5559           
===========================================
+ Hits            25773    25775    +2     
+ Misses           2871     2870    -1     
+ Partials         1431     1430    -1     
Files Coverage Δ
...ient_descent/projected_gradient_descent_pytorch.py 95.86% <100.00%> (ø)

... and 1 file with indirect coverage changes

@beat-buesser beat-buesser self-requested a review June 25, 2024 17:20
@beat-buesser beat-buesser self-assigned this Jun 25, 2024
@beat-buesser beat-buesser added bug Something isn't working improvement Improve implementation dependencies Pull requests that update a dependency file labels Jun 25, 2024
@beat-buesser beat-buesser added this to the ART 1.18.1 milestone Jun 25, 2024
@beat-buesser beat-buesser changed the base branch from main to dev_1.18.1 June 25, 2024 17:28
@beat-buesser
Copy link
Collaborator

Hi @salomonhotegni Could you please fix the DCO check?

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 @salomonhotegni Could you please also make the same change in line 500?

@beat-buesser
Copy link
Collaborator

Please run Python formatter black with version black==21.12b0 using command black --line-length 120 --check art/ to fix the failing style checks.

@salomonhotegni
Copy link
Contributor Author

Hi @salomonhotegni Could you please also make the same change in line 500?

Sure!

@beat-buesser
Copy link
Collaborator

Hi @salomonhotegni Thank you very much. To pass the DCO check, could you please add your signed-off-by line to every commit in this branch using the following commands:

  • In your local branch, run: git rebase HEAD~2 --signoff
  • Force push your changes to overwrite the branch: git push --force-with-lease origin my-feature-branch

@beat-buesser
Copy link
Collaborator

beat-buesser commented Jul 1, 2024

@salomonhotegni Could you please also take a look at updating the code to pass the style checks with the instructions above?

salomonhotegni and others added 4 commits July 1, 2024 20:17
Solve non-writable NumPy array and device mismatch issues.

Signed-off-by: salomonhotegni <salomon.hotegni@aims.ac.rw>
…y array and device mismatch issues]

Signed-off-by: salomonhotegni <salomon.hotegni@aims.ac.rw>
Signed-off-by: salomonhotegni <salomon.hotegni@aims.ac.rw>
…y array and device mismatch issues]

Signed-off-by: salomonhotegni <salomon.hotegni@aims.ac.rw>
@beat-buesser
Copy link
Collaborator

beat-buesser commented Jul 1, 2024

Hi @salomonhotegni Could you please run command black --line-length 120 --check art/ with version black==21.12b0 and commit/push the changes to make the style check pass? Please let me know if you need help from me.

@beat-buesser
Copy link
Collaborator

Thank you for fixing DCO.

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 @salomonhotegni Thank you very much for your first contribution to ART!

@beat-buesser beat-buesser merged commit 7044477 into Trusted-AI:dev_1.18.1 Jul 2, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file improvement Improve implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UserWarning and RuntimeError in ProjectedGradientDescent due to Non-Writable NumPy Array and Device Mismatch
2 participants