-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Replace asserts with exceptions #5587
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
Replace asserts with exceptions #5587
Conversation
💊 CI failures summary and remediationsAs 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. |
…/vision into replace_asserts_with_exceptions
…/vision into replace_asserts_with_exceptions
There was a problem hiding this 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
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>
There was a problem hiding this 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: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
torchvision/datasets/kinetics.py
Outdated
self.split = "unknown" | ||
assert not download, "Cannot download the videos using legacy_structure." | ||
if download: | ||
raise RuntimeError("Cannot download the videos using legacy_structure.") |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
test/test_backbone_utils.py
Outdated
) | ||
# Check must specify return nodes | ||
with pytest.raises(AssertionError): | ||
with pytest.raises(RuntimeError): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
torchvision/ops/poolers.py
Outdated
) | ||
if scales is None or mapper is None: | ||
raise ValueError("scales and mapper should not be None") | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same about else
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>
There was a problem hiding this 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)}" | ||
) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
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}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same.
@datumbox I will revert the others and keep the first one then. You can cherrypick it or resolve later if this lands first. |
thank you @NicolasHug for all the comments! I think i've addressed everything but let me know if it looks good! |
There was a problem hiding this 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!
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") |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
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 |
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>
Changes most asserts with exceptions
adresses #5494