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

Validates number of dimensions in input to ht.sparse.sparse_csr_matrix #1098

Conversation

Ishaan-Chandak
Copy link
Contributor

@Ishaan-Chandak Ishaan-Chandak commented Feb 8, 2023

Description

ht.sparse.sparse_csr_matrix should accept only 2-D input. But it accepts (and crashes) when the input is not 2 dimensional.

The input must be properly validated to ensure only 2-D input is accepted. Or else an error with the message "The number of dimensions must be 2, found ndim" must be raised.

Changes proposed

Using tensor.ndim to find the dimension of the tensor and then using if-else statements to check the dimension of the input and raise the exception.

Fixes: #1091

@Mystic-Slice
Copy link
Collaborator

@Ishaan-Chandak Good work so far!
But there is a small problem with this fix. This will allow tensors with layout torch.sparse_csr that are not 2-dimensional. We have to prevent that too.

For example, try running this code,

import torch
import heat as ht

a = torch.tensor([[[0, 1]]], dtype=torch.float64).to_sparse_csr()
t = ht.sparse.sparse_csr_matrix(a)
print(t + t)

The number of dimensions has to be checked irrespective of the layout of the input object.
So, check the number of dimensions before checking the layout attribute.

@Mystic-Slice Mystic-Slice self-requested a review February 8, 2023 15:31
@Mystic-Slice
Copy link
Collaborator

Try to provide some supporting unit tests for this change once you fix this.
And of course, don't hesitate to ask here if you have any doubts.

@Ishaan-Chandak
Copy link
Contributor Author

Hi, @Mystic-Slice I made the necessary changes please review the PR, I also ran all the unittests as mentioned in the documentation provided.
Thank you

@Ishaan-Chandak
Copy link
Contributor Author

I am extremely sorry for this commit I messed up the code at a point I ll make the changes as soon as possible
Sorry @Mystic-Slice

heat/sparse/factories.py Outdated Show resolved Hide resolved
@Mystic-Slice
Copy link
Collaborator

@Ishaan-Chandak New tests need to be added to the existing unit-tests to ensure the check for dimensionality works properly.
You can create the new tests in heat/core/tests/test_factories.py

@Ishaan-Chandak
Copy link
Contributor Author

I ll be adding the tests soon @Mystic-Slice

@Ishaan-Chandak
Copy link
Contributor Author

Ishaan-Chandak commented Feb 9, 2023

Hi @Mystic-Slice
`
def test_dimensions(self):
# for a two dimension tensor no error is raised

    # more than two dimension tensors with sparse_csr layout 
    a = torch.tensor([[[0, 1]]], dtype=torch.float64).to_sparse_csr()
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, a)

    # for one dimension 
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, torch.tensor([0, 1]))
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, torch.tensor([1, 0, 3]))
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, torch.tensor([ ]))
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, torch.tensor([1, 0, 3, 4]))

    # for more than one dimension
    self.assertRaises(ValueError, ht.sparse.sparse_csr_matrix, torch.tensor([[[1, 0, 3]]]))

`
Are these many unit-tests enough ?

@Mystic-Slice
Copy link
Collaborator

@Ishaan-Chandak One test for 1-D input and one test for > 2-D input is enough. Add these tests at the end of test_sparse_csr_matrix method. No need to create a new function.
Also, I linked the wrong file in my previous comment. Sorry about that. These tests are to be added to heat/sparse/tests/test_factories.py.

@Ishaan-Chandak
Copy link
Contributor Author

@Mystic-Slice Is test for 2-D input necessary because when the input is 2-D the programs keeps on running without any interruptions.
So according to me it will be better to have two inputs one for greater than 2 and one for less than 2

@Mystic-Slice
Copy link
Collaborator

Yes, I meant exactly that by > 2-D (greater than 2-D). :)

@Ishaan-Chandak
Copy link
Contributor Author

Hi @Mystic-Slice I have added the tests and made the changes as you told me to do please review the commit.

@Ishaan-Chandak
Copy link
Contributor Author

Hi @Mystic-Slice Can you please review this and tell me if any changes are needed.
Thank you

Copy link
Collaborator

@Mystic-Slice Mystic-Slice left a comment

Choose a reason for hiding this comment

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

Looks good to me

@Mystic-Slice
Copy link
Collaborator

@ClaudiaComito This PR is good to be merged. The pre-commit ci failure is not related to this PR.

And @Ishaan-Chandak Nice work!

@Mystic-Slice Mystic-Slice changed the title Validates number of dimensions in input Validates number of dimensions in input to ht.sparse.sparse_csr_matrix Feb 13, 2023
@Mystic-Slice Mystic-Slice added bug Something isn't working sparse labels Feb 13, 2023
Copy link
Contributor

@ClaudiaComito ClaudiaComito left a comment

Choose a reason for hiding this comment

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

Wonderful, thanks a lot @Ishaan-Chandak and @Mystic-Slice !

@ClaudiaComito
Copy link
Contributor

This PR needs CI. Waiting on developments in #1114

@Ishaan-Chandak
Copy link
Contributor Author

Hi @ClaudiaComito I am unable to understand what should I do now ? And can you please explain what exactly is CI

@Mystic-Slice
Copy link
Collaborator

@Ishaan-Chandak CI is Continuous Integration. It is the tests that run before the PR is allowed to be merged. You might have noticed that some tests do not run in your PR. That is because CI, as of now, does not work on external contributor forks. And the work to enable CI for external forks is going on in #1114. Your work here is complete. We will merge the PR once it is ready. Thanks for the fix. We will take care of the rest.

tldr, You do not need to do anything.

@ClaudiaComito
Copy link
Contributor

Hi @ClaudiaComito I am unable to understand what should I do now ? And can you please explain what exactly is CI

@Ishaan-Chandak oh I'm so sorry, that was more of an internal communication 😬 apologies. @Mystic-Slice thanks for explaining. Once we can run the tests, we'll know if more needs to be done. Nothing you can do about this @Ishaan-Chandak , thanks again for your time!

@Ishaan-Chandak
Copy link
Contributor Author

Hi @Mystic-Slice @ClaudiaComito I received a mail regarding some tests failing due to older version of heat so should i do something to rectify them ?

@github-actions
Copy link
Contributor

Thank you for the PR!

@codecov
Copy link

codecov bot commented Mar 17, 2023

Codecov Report

Merging #1098 (c12297e) into main (d744909) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #1098   +/-   ##
=======================================
  Coverage   91.80%   91.80%           
=======================================
  Files          72       72           
  Lines       10483    10485    +2     
=======================================
+ Hits         9624     9626    +2     
  Misses        859      859           
Flag Coverage Δ
unit 91.80% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
heat/sparse/factories.py 95.74% <100.00%> (+0.09%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@ClaudiaComito ClaudiaComito added this pull request to the merge queue Mar 17, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Mar 17, 2023
@ClaudiaComito
Copy link
Contributor

Hi @Mystic-Slice @ClaudiaComito I received a mail regarding some tests failing due to older version of heat so should i do something to rectify them ?

@Ishaan-Chandak no that was still our old tests. We have merged the new ones now, I'm about to merge this PR. 👏🏼 Thanks @Ishaan-Chandak @mtar and @Mystic-Slice for all the work!

@ClaudiaComito ClaudiaComito added this pull request to the merge queue Mar 20, 2023
@ClaudiaComito ClaudiaComito removed this pull request from the merge queue due to a manual request Mar 20, 2023
@ClaudiaComito ClaudiaComito added this pull request to the merge queue Mar 20, 2023
@ClaudiaComito ClaudiaComito merged commit a5515f0 into helmholtz-analytics:main Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sparse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: ht.sparse.sparse_csr_matrix does not validate number of dimensions in input
3 participants