Skip to content

Conversation

jdsgomes
Copy link
Contributor

Changes most asserts with exceptions
adresses #5494

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Mar 10, 2022

💊 CI failures summary and remediations

As of commit d77739b (more details on the Dr. CI page):


💚 💚 Looks good so far! There are no failures yet. 💚 💚


This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

@jdsgomes jdsgomes changed the title [WIP] replace most asserts with exceptions Replace asserts with exceptions Mar 13, 2022
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @jdsgomes , I only took a brief look at the first few changes. I feel like the translation of assert X to if not X: raise... can feel a bit unnatural in some places, because there is often a more Pythonic way to write not X. I made some comments below. I won't push too hard for this so if you feel like it's not worth it, that's fine. LMK what you think

jdsgomes and others added 5 commits March 14, 2022 11:31
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @jdsgomes , I checked all if ... and they look good. I made some comments regarding the use of RuntimeError vs ValueError. Looks like we're a bit inconsistent throughout the code base, but I think most of these should be ValueError. I also made some comments about some else: blocks that can potentially be remove to simplify the PR, LMK what you think

assert num_classes > 0, "Please provide a valid positive value for the num_classes."
assert alpha > 0, "Alpha param can't be zero."

if num_classes <= 1:
Copy link
Member

Choose a reason for hiding this comment

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

The original is equivalent to if num_classes <= 0: but I assume this is actually a fix?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an error, I don't think it is a bug so I will change it

self.split = "unknown"
assert not download, "Cannot download the videos using legacy_structure."
if download:
raise RuntimeError("Cannot download the videos using legacy_structure.")
Copy link
Member

Choose a reason for hiding this comment

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

I think this should be a ValueError rather than a RuntimeError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agree - thanks for spotting these inconsistencies

)
# Check must specify return nodes
with pytest.raises(AssertionError):
with pytest.raises(RuntimeError):
Copy link
Member

Choose a reason for hiding this comment

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

I wonder whether a ValueError makes more sense here, and almost everywhere else a RuntimeError is raised? Usually when there's an issue with user-provided input, it's either a TypeError or a ValueError.

Also, it helps a lot to use the match=expected_err_msg parameter of pytest.raises: we can make sure that the exception that gets raise is indeed the one we expect, and it's helpful to document the tests as well. We don't have to match the entire error message, sometimes just matching the relevant part is good enough. We could leave that part for a follow up though.

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 was in doubt in this one. I was using a strict definition of ValueError: "Raised when an operation or function receives an argument that has the right type but an inappropriate value". And in this case each argument might have an appropriate value, but the combination of them is not appropriate. But I am ok with your interpretation as these are user inputs so still fall under ValueError.

As for the expected error messages I would leave it for another PR as it is outside the scope of this one.

)
if scales is None or mapper is None:
raise ValueError("scales and mapper should not be None")
else:
Copy link
Member

Choose a reason for hiding this comment

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

same about else

jdsgomes and others added 12 commits March 15, 2022 10:27
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Copy link
Contributor

@datumbox datumbox left a comment

Choose a reason for hiding this comment

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

There are a few potential conflicts with multi-weights porting noted below. This is FYI.

Edit:
@jdsgomes With the exception of the first comment below, everything else will be removed by the porting. Shall I cherry pick the first in my branch and then undo the rest to avoid conflicts? No strong opinion if you want to keep them as you are likely to merge faster than I will.

if len(out_channels) != len(anchor_generator.aspect_ratios):
raise RuntimeError(
f"The length of the output channels from the backbone {len(out_channels)} do not match the length of the anchor generator aspect ratios {len(anchor_generator.aspect_ratios)}"
)
Copy link
Contributor

Choose a reason for hiding this comment

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

That's going to conflict with the weight porting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

please se my comment about this conflicts. I reverted all others but will leave this one let me know if you agree with this approach

else:
assert pretrained in [True, False]
if pretrained not in [True, False]:
raise ValueError(f"For non quantized models, pretrained should be a bollean value instead of {pretrained}")
Copy link
Contributor

Choose a reason for hiding this comment

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

This one too.

else:
assert pretrained in [True, False]
if pretrained not in [True, False]:
raise ValueError(f"For non quantized models, pretrained should be a bollean value instead of {pretrained}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same.

else:
assert pretrained in [True, False]
if pretrained not in [True, False]:
raise ValueError(f"For non quantized models, pretrained should be a bollean value instead of {pretrained}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same.

else:
assert pretrained in [True, False]
if pretrained not in [True, False]:
raise ValueError(f"For non quantized models, pretrained should be a bollean value instead of {pretrained}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same.

@jdsgomes
Copy link
Contributor Author

jdsgomes commented Mar 15, 2022

@datumbox I will revert the others and keep the first one then. You can cherrypick it or resolve later if this lands first.

@jdsgomes
Copy link
Contributor Author

thank you @NicolasHug for all the comments! I think i've addressed everything but let me know if it looks good!

Copy link
Member

@NicolasHug NicolasHug left a comment

Choose a reason for hiding this comment

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

Thanks @jdsgomes, LGTM

I still see a few RuntimeError that could be ValueError, from #5587 (comment) I'm not sure whether you were planning to change them here in this PR, or later, or at all. Either way we are not consistent regarding this in the code-base so I will approve regardless. Thanks for the many changes!

Comment on lines +447 to +448
if not ((return_nodes is None) ^ (train_return_nodes is None)):
raise ValueError("If `train_return_nodes` and `eval_return_nodes` are specified, then both should be specified")
Copy link
Member

Choose a reason for hiding this comment

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

Should we fix that now, or do that in another PR?
Previous comment was #5587 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry you are right, this should be done in this one. Done now. I will wait for the tests to pass and merge. Thanks for the heads up

@jdsgomes jdsgomes merged commit 289fce2 into pytorch:main Mar 15, 2022
@github-actions
Copy link

Hey @jdsgomes!

You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py

facebook-github-bot pushed a commit that referenced this pull request Apr 5, 2022
Summary:
* replace most asserts with exceptions

* fix formating issues

* fix linting and remove more asserts

* fix regresion

* fix regresion

* fix bug

* apply ufmt

* apply ufmt

* fix tests

* fix format

* fix None check

* fix detection models tests

* non scriptable any

* add more checks for None values

* fix retinanet test

* fix retinanet test

* Update references/classification/transforms.py

* Update references/classification/transforms.py

* Update references/optical_flow/transforms.py

* Update references/optical_flow/transforms.py

* Update references/optical_flow/transforms.py

* make value checks more pythonic:

* Update references/optical_flow/transforms.py

* make value checks more pythonic

* make more checks pythonic

* fix bug

* appy ufmt

* fix tracing issues

* fib typos

* fix lint

* remove unecessary f-strings

* fix bug

* Update torchvision/datasets/mnist.py

* Update torchvision/datasets/mnist.py

* Update torchvision/ops/boxes.py

* Update torchvision/ops/poolers.py

* Update torchvision/utils.py

* address PR comments

* Update torchvision/io/_video_opt.py

* Update torchvision/models/detection/generalized_rcnn.py

* Update torchvision/models/feature_extraction.py

* Update torchvision/models/optical_flow/raft.py

* address PR comments

* addressing further pr comments

* fix bug

* remove unecessary else

* apply ufmt

* last pr comment

* replace RuntimeErrors

(Note: this ignores all push blocking failures!)

Reviewed By: datumbox, YosuaMichael

Differential Revision: D35216782

fbshipit-source-id: 5c0aeede7e114e3951fb6867a525162b706f5a60

Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Co-authored-by: Nicolas Hug <contact@nicolas-hug.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants