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

Raise exceptions instead of using assertions for control flow #12789

Open
willfrey opened this issue Jul 19, 2021 · 18 comments
Open

Raise exceptions instead of using assertions for control flow #12789

willfrey opened this issue Jul 19, 2021 · 18 comments

Comments

@willfrey
Copy link
Contributor

@willfrey willfrey commented Jul 19, 2021

assert batch_size > 0, "batch_size has to be defined and > 0"

Assertions can't be relied upon for control flow because they can be disabled, as per the following:

$ python --help
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
...
-O     : remove assert and __debug__-dependent statements; add .opt-1 before
         .pyc extension; also PYTHONOPTIMIZE=x
...

From my understanding, this is why mypy has no qualms about using them to narrow types because you can turn them off at runtime and so they incur zero cost.

Would you be open to me changing these assertions to other appropriate exceptions as I encounter them?

@sgugger
Copy link
Member

@sgugger sgugger commented Jul 21, 2021

That is an excellent point, and we welcome any PR that changes assert's to proper exception. We have been doing that for long error messages anyway, and I agree with you it's a better choice (as long as we raise the appropriate exception of course).

Loading

@sgugger
Copy link
Member

@sgugger sgugger commented Jul 27, 2021

I'm adding the good first issue label, this way if someone wants to take care of one file to remove all asserts and replace them with proper exceptions, they can make a PR with it!

(Don't try to do all files of the library at one ;-) )

Loading

@Josh1108
Copy link

@Josh1108 Josh1108 commented Aug 6, 2021

Hi @sgugger,

I'd like to take up one of the files to start. Should I pick up modelling_gpt2.py and go ahead?

Loading

@sgugger
Copy link
Member

@sgugger sgugger commented Aug 9, 2021

Any of the files with assert statements is fair game :-)

Loading

@AmbiTyga
Copy link
Contributor

@AmbiTyga AmbiTyga commented Aug 19, 2021

Hi @sgugger,
I'd be happy to do this. So I need to change assert to 'Exception' type error for only one file?
(Not allowed to do this for every file?)

Loading

@willfrey
Copy link
Contributor Author

@willfrey willfrey commented Aug 19, 2021

Loading

@AmbiTyga
Copy link
Contributor

@AmbiTyga AmbiTyga commented Aug 19, 2021

I have created PR #13184 for this issue. Kindly go through it.
Thanks

Loading

@jeffin07
Copy link

@jeffin07 jeffin07 commented Aug 23, 2021

@sgugger is this still open ? If so any files with assertion came under the issue right ? Can i join this issue if it's not completed

Loading

@sgugger
Copy link
Member

@sgugger sgugger commented Aug 30, 2021

@jeffin07 You can pick any file that has not already been treated.

Loading

@litanlitudan
Copy link

@litanlitudan litanlitudan commented Sep 20, 2021

@sgugger I am also going to pick several files to kill my time.

Loading

MocktaiLEngineer pushed a commit to MocktaiLEngineer/transformers that referenced this issue Sep 22, 2021
sgugger added a commit that referenced this issue Sep 22, 2021
* Raise exceptions instead of using assertions for control flow #12789

* # coding=utf-8

* Raise exceptions instead of using assertions for control flow

* Raise exceptions instead of using assertions for control flow

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Suraj Patil <surajp815@gmail.com>

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* Raise exceptions instead of using assertions for control flow

* test

* Raise exceptions instead of using assertions for control flow

Co-authored-by: MocktaiLEngineer <kavinarasu22@gmail.com>
Co-authored-by: Suraj Patil <surajp815@gmail.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Narsil added a commit to Narsil/transformers that referenced this issue Sep 25, 2021
* Raise exceptions instead of using assertions for control flow huggingface#12789

* # coding=utf-8

* Raise exceptions instead of using assertions for control flow

* Raise exceptions instead of using assertions for control flow

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Suraj Patil <surajp815@gmail.com>

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* Raise exceptions instead of using assertions for control flow

* test

* Raise exceptions instead of using assertions for control flow

Co-authored-by: MocktaiLEngineer <kavinarasu22@gmail.com>
Co-authored-by: Suraj Patil <surajp815@gmail.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
@m5l14i11
Copy link
Contributor

@m5l14i11 m5l14i11 commented Oct 5, 2021

I've created PR #13894 . Please take a look.

Loading

@djroxx2000
Copy link
Contributor

@djroxx2000 djroxx2000 commented Oct 7, 2021

I've created PR #13909 towards this issue. Please take a look

Loading

sgugger pushed a commit that referenced this issue Oct 7, 2021
* #12789 Replace assert statements with exceptions

* fix-copies: made copy changes to utils_qa.py in examples/pytorch/question-answering and examples/tensorflow/question-answering

* minor refactor for clarity
@kushagrabhushan
Copy link

@kushagrabhushan kushagrabhushan commented Oct 7, 2021

Hello, I'm new to open source. I wanted to know if this issue is still open and if so how do i check which files have not been edited already and which files require changes still?

Loading

@LysandreJik
Copy link
Member

@LysandreJik LysandreJik commented Oct 8, 2021

Hello @kushagrabhushan! You can do a search across the repository and look for the assert keyword. Here's an example:

assert self.is_decoder, f"{self} should be used as a decoder model if cross attention is added"

I recommend looking only for files in src/transformers and in tests. In src/transformers, it would be nice to replace them by ValueErrors, and in tests, it would be ideal to replace them by unittest assertions. These can be self.assertEqual, self.assertDictEqual, etc, according to what the assertion is supposed to do.

I would recommend cloning the repository and using a tool to search for these, as the GitHub search isn't ideal for a search like this.

Loading

@killazz67
Copy link
Contributor

@killazz67 killazz67 commented Oct 8, 2021

I would also like to help on this issue. If i understood it right, i should make a separate PR for every single file right?

Loading

@sgugger sgugger reopened this Oct 11, 2021
stas00 added a commit to stas00/transformers that referenced this issue Oct 12, 2021
* Raise exceptions instead of using assertions for control flow huggingface#12789

* # coding=utf-8

* Raise exceptions instead of using assertions for control flow

* Raise exceptions instead of using assertions for control flow

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Suraj Patil <surajp815@gmail.com>

* Update src/transformers/tokenization_utils.py

Raise exceptions instead of using assertions for control flow

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* Raise exceptions instead of using assertions for control flow

* test

* Raise exceptions instead of using assertions for control flow

Co-authored-by: MocktaiLEngineer <kavinarasu22@gmail.com>
Co-authored-by: Suraj Patil <surajp815@gmail.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
@sgugger sgugger reopened this Oct 14, 2021
@skpig skpig mentioned this issue Oct 21, 2021
5 tasks
@huberemanuel
Copy link
Contributor

@huberemanuel huberemanuel commented Oct 24, 2021

In cases of assertions being made inside for loops, what would be the best practice?

for sentence in sentences:
    if len(sentence["tokens"]) != len(sentence["labels"]):
        raise ValueError(f"Number of tokens {len(words)} and labels tokens {len(labels)} mismatch")

or

for sentence in sentences:
    if len(sentence["tokens"]) != len(sentence["labels"]):
        raise ValueError(f"Number of tokens {len(words)} and labels tokens {len(labels)} mismatch in sentence {sentence}")

The second approach seems to be a friendly solution for the user since it will show exactly what was the problematic sentence, although, I have seen the first approach used in the PRs. This isn't a critique, I am trying to understand if the first option is better or not.

Loading

@sgugger
Copy link
Member

@sgugger sgugger commented Oct 25, 2021

The second approach is more informative indeed!

Loading

LysandreJik pushed a commit that referenced this issue Oct 26, 2021
Updated masked-language modeling examples in pytorch
with convention defined by #12789
nbertagnolli added a commit to nbertagnolli/transformers that referenced this issue Nov 13, 2021
sgugger pushed a commit that referenced this issue Nov 14, 2021
…14386)

* Raise exceptions instead of using asserts for control flow in modeling_openai #12789

* reformatted file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment