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

[MRG] Debug convolutional methods that compute barycenters to work with different devices. #533

Merged
merged 12 commits into from
Oct 18, 2023

Conversation

framunoz
Copy link
Contributor

@framunoz framunoz commented Oct 13, 2023

Types of changes

  • The linspace method of the backends now has the type_as argument to convert to the same dtype and device.
  • The convolutional_barycenter2d and convolutional_barycenter2d_debiased functions now work with different devices.

Motivation and context / Related issue

The problem that the PR solves is that the convolutional_barycenter2d and convolutional_barycenter2d functions were not working on different devices (e.g. using PyTorch with CUDA). It was detected that the error came from the linspace method of the backends not having a type_as argument.

How has this been tested (if it applies)

  • Add a test in test_backend.py that checks if the type_as argument in the linspace method works.
  • Add the tests test_wasserstein_bary_2d_d_dtype_device, test_wasserstein_bary_2d_device_tf, test_wasserstein_bary_2d_debiased_dtype_device and test_wasserstein_bary_2d_debiased_device_tf which mirror the test_sinkhorn2_variants_dtype_device and test_sinkhorn2_variants_device_tf tests to check if the barycenter work with different dtypes and devices.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added the PR and Issue fix to the RELEASES.md file.

@framunoz framunoz changed the title Framunoz/backend refactor [WIP] Debug the convolutional methods that computes the barycenters to work with different devices Oct 13, 2023
@framunoz framunoz changed the title [WIP] Debug the convolutional methods that computes the barycenters to work with different devices [MRG] Debug the convolutional methods that computes the barycenters to work with different devices Oct 13, 2023
@framunoz framunoz marked this pull request as ready for review October 13, 2023 23:00
@framunoz framunoz changed the title [MRG] Debug the convolutional methods that computes the barycenters to work with different devices [MRG] Debug convolutional methods that compute barycenters to work with different devices. Oct 13, 2023
@codecov
Copy link

codecov bot commented Oct 13, 2023

Codecov Report

Merging #533 (16987c8) into master (ffdd1cf) will increase coverage by 0.05%.
The diff coverage is 99.31%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #533      +/-   ##
==========================================
+ Coverage   96.26%   96.31%   +0.05%     
==========================================
  Files          67       67              
  Lines       14043    14136      +93     
==========================================
+ Hits        13518    13615      +97     
+ Misses        525      521       -4     

Copy link
Collaborator

@rflamary rflamary left a comment

Choose a reason for hiding this comment

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

Thanks @framunoz for you PR, it looks great. Could you answer my question and comment please?

@@ -352,13 +352,15 @@ def test_sinkhorn2_variants_device_tf(method):
nx.assert_same_dtype_device(Mb, Gb)
nx.assert_same_dtype_device(Mb, lossb)

# Check that everything happens on the GPU
Copy link
Collaborator

Choose a reason for hiding this comment

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

why did you move this outside of the GPU test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because Codecov does not test these lines, since it works on cpu, giving less coverage to the overall code. When I leave the device verification to the end, Codecov will give coverage to all lines except the last one. Otherwise, it will not give coverage to most of the lines after the if as it was before.

To summarize, it is rather a trick to increase the coverage, which were cases that can still be included, but due to limitations of the Codecov machine, it gives the impression that it worsens the coverage.

# Using the Tensorflow backend
nx = ot.backend.TensorflowBackend()

rng = np.random.RandomState(42)
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you factorize all this data initialization in a function ad call it each time please (there are other plces where it will be used)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I included a function that initializes the matrix A, and replaced it each time it is called.

@framunoz
Copy link
Contributor Author

The extension that reformats the code to comply with the PEP 8 format made many changes to the code. Do I leave it as it was before the commit? Or is the new style preferred?

@rflamary
Copy link
Collaborator

Thanks for the changes, I would prefer we stick to the standard from autopep8 (make aautopep8 does tyhe job in POT main folder once autopep8 is installed) style at the moment if possible. Dis you use black on the test file or did autopep8 decided to change everything? We might one day switch to lack for POT but we need to do this well to keep the authors for code correct.

@framunoz
Copy link
Contributor Author

I used black in the tests, although because of the vscode extension, the formatting was applied automatically.

In these last commits I left the formatting of the rest of the tests as they were before, also, I added a new line in the .gitignore to ignore the .venv.

@rflamary rflamary merged commit 8a4a5a6 into PythonOT:master Oct 18, 2023
13 checks passed
@framunoz framunoz deleted the framunoz/backend-refactor branch October 18, 2023 16:07
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.

2 participants