Skip to content

Adding DocTest to TrOCR#16398

Merged
ydshieh merged 18 commits intohuggingface:mainfrom
arnaudstiegler:AS/TrOCR
Mar 29, 2022
Merged

Adding DocTest to TrOCR#16398
ydshieh merged 18 commits intohuggingface:mainfrom
arnaudstiegler:AS/TrOCR

Conversation

@arnaudstiegler
Copy link
Contributor

@arnaudstiegler arnaudstiegler commented Mar 24, 2022

Adding TrOCR to DocTests

For this model, there was actually a single docstring in the entire file (for the forward method).

A couple of comments:

  • As far as I'm aware, there is no TF version of this model
  • TrOCR is an edge case because it's meant to be used as the decoder for a VisionEncoderDecoder. So the forward function of the TrOCR is not meant to be called directly. As a result, I gave some example code to run a forward pass with TrOCR within a VisionEncoderDecoder

Let me know if the docstring is relevant to the problem. I can also revert adapt it to actually just showcase the forward for the TrOCRForCausalLM outside of a VisionEncoderDecoder

@patrickvonplaten @ydshieh @patil-suraj

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Mar 24, 2022

The documentation is not available anymore as the PR was closed or merged.

@arnaudstiegler arnaudstiegler marked this pull request as draft March 24, 2022 22:34
@arnaudstiegler arnaudstiegler marked this pull request as ready for review March 24, 2022 22:36
@ydshieh
Copy link
Collaborator

ydshieh commented Mar 24, 2022

I took a quick look for now, and think it is indeed a nice addition, considering there is currently no example in modeling_trocr.py at all.
Thank you, @arnaudstiegler!

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

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

@arnaudstiegler

Very nice & clean! I left a few tiny comments, but this PR is ready to be merged!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be great to avoid import VisionEncoderDecoderModel twice

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Removed it

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is better to be after showing the dummy model, i.e. after the line

>>> model = VisionEncoderDecoderModel(encoder=encoder, decoder=decoder)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add a comment here

>>> # init vision2text model with random weights

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe put this line just before

>>> text = "hello world"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Collaborator

Choose a reason for hiding this comment

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

just a nit: pixel_values before text 🙂

The reason is: pixel_values is the encoder part, and text is the decoder part. In encoder-decoder architecture, encoder is run before the decoder.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nice ❤️!

Are you able to run the doctest locally and get it pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, runs fine locally! I ran the test again after addressing your comments

@arnaudstiegler
Copy link
Contributor Author

arnaudstiegler commented Mar 25, 2022

Addressed the comments and re-ran the test locally!
There's one thing though: make fixup messes up the formatting of the docstring, and removes the additional blank at the end of the docstring (here). The issue is that without the additional line, doctest will fail when comparing outputs. Not sure why to be honest, but do you know how to prevent that make fixup from doing this? Otherwise, next time the file is touched, make fixup will introduce a bug in the docstring.

Here's the failure:

Expected:
    ['industry, " Mr. Brown commented icily. " Let us have a']
    ```
Got:
    ['industry, " Mr. Brown commented icily. " Let us have a']

Essentially, without the additional line, the test consider that ``` is part of the expected output. But that additional line gets removed by make fixup. I haven't found a good way around this yet

@ydshieh

@ydshieh
Copy link
Collaborator

ydshieh commented Mar 25, 2022

Those blank lines issues should be treated by the places regarding utils/prepare_for_doc_test.py as shown in this guide doc.

This means you don't need to add extra blank line in the file. Did you run python utils/prepare_for_doc_test.py as in the guide before and after running the doctest?

@arnaudstiegler
Copy link
Contributor Author

Got it! I did run the script and it seems like it's not being applied to the trocr (but I can see the added lines on other model files). I'll debug that today

@ydshieh
Copy link
Collaborator

ydshieh commented Mar 25, 2022

OK, thank you. Don't hesitate to report it if you find this is some bug in prepare_for_doc_test.py.

@arnaudstiegler
Copy link
Contributor Author

arnaudstiegler commented Mar 25, 2022

OK, thank you. Don't hesitate to report it if you find this is some bug in prepare_for_doc_test.py.

Figured out the issue: there was some formatting issue in one of the docstring (see last commit) that prevented the script to correctly add a line. From utils.prepare_for_doc_test.process_doc_file

splits = code.split("```")
splits = [s if i % 2 == 0 else process_code_block(s, add_new_line=add_new_line) for i, s in enumerate(splits)]

splits were incorrect because of that, and the script wouldn't add an additional \n
Now the doctest runs fine post make fixup
Should be good to go

@arnaudstiegler
Copy link
Contributor Author

@ydshieh Is there anything else to do? I don't have writing access here, so I can't merge this

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

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

Hi, @arnaudstiegler,

I have left a few more comments: most of them are just some nits.
This PR is really ready, and I will merge once you can apply the suggestions 💯
Thank you so much!

Copy link
Collaborator

Choose a reason for hiding this comment

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

just a nit: pixel_values before text 🙂

The reason is: pixel_values is the encoder part, and text is the decoder part. In encoder-decoder architecture, encoder is run before the decoder.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would be great if you could add the expected loss here ❤️

Copy link
Contributor Author

@arnaudstiegler arnaudstiegler Mar 29, 2022

Choose a reason for hiding this comment

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

Added the loss. I actually changed the text to reflect the actual text on the image because the loss was super high with the dummy text (loss=22.00 vs loss=4.00). Let me know what's best

Also added rounding + setting the torch seed just in case

Copy link
Collaborator

Choose a reason for hiding this comment

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

Very nice for the change of target text! Awesome 👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

very nice!

@arnaudstiegler
Copy link
Contributor Author

Rebased on latest main and addressed the comments, the failing test is coming from main (failing on main as well)

@ydshieh
Copy link
Collaborator

ydshieh commented Mar 29, 2022

Hi, the failed check seems irrelevant to your PR. No need to fix it here 😃. I will give a final look and merge. Thank you 💗

@arnaudstiegler
Copy link
Contributor Author

Alright, thank you!

@ydshieh
Copy link
Collaborator

ydshieh commented Mar 29, 2022

By the way, may I wonder why you choose to display 3 decimal numbers for the loss value? I remember we use 2 decimal numbers in doc.py. I will check once I am available.

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

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

Just added 3 final suggestions

  • No (real) need to set the seed
  • display only 2 decimal numbers

This is more about to be aligned with the code sample in doc.py.

@arnaudstiegler
Copy link
Contributor Author

Applied your changes, tested them locally, and ran make fixup as this was needed after the changes

arnaudstiegler and others added 8 commits March 29, 2022 09:43
Co-authored-by: Yih-Dar <2521628+ydshieh@users.noreply.github.com>
Co-authored-by: Yih-Dar <2521628+ydshieh@users.noreply.github.com>
Co-authored-by: Yih-Dar <2521628+ydshieh@users.noreply.github.com>
Co-authored-by: Yih-Dar <2521628+ydshieh@users.noreply.github.com>
@ydshieh
Copy link
Collaborator

ydshieh commented Mar 29, 2022

If the remaining failed checks are build_pr_documentation and Add new model like template tests, you can keep the PR as it is. It is irrelevant I think.

@arnaudstiegler
Copy link
Contributor Author

Ok, I tried rebasing on latest master and it doesn't seem to be doing the trick. Not sure what's causing that unfortunately, but unlikely to be due to the code changes I've done :)

@ydshieh ydshieh merged commit ed31ab3 into huggingface:main Mar 29, 2022
@ydshieh
Copy link
Collaborator

ydshieh commented Mar 29, 2022

Thank you again @arnaudstiegler ! (also for your patience)

I merge this PR now ❤️.

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.

3 participants