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

[BT] Add Bettertransformer support for FSMT #494

Merged
merged 13 commits into from Nov 24, 2022

Conversation

Sumanth077
Copy link
Contributor

What does this PR do?

Fixes # (issue)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Thanks a lot for starting the PR!
First of all, I can see several "unneeded" files (.idea, *.xml files) would you mind removing them as they are not needed at all 🙏
Let me guide you here step by step:

Step 1: I have already identified for you the name of the layer that needs to be replaced. For FSMT the layer is called EncoderLayer

Step 2: In optimum/bettertransformer/models/encoder_models.py add a new class, here you can call it FSMTEncoderLayerBetterTransformer and follow the same template / logic as BertEncoderLayerBetterTransformer for assigning the new keys to the old one. The challenge here is to find out which key corresponds to exactly which part of the TransformerEncoderLayer. This step is described here in the documentation

Step 3: Build the forward pass, again you can follow what is done in BERT and make sure to respect what is said here

Step 4: Import you new class, so here FSMTEncoderLayerBetterTransformer inside optimum/bettertransformer/models/__init__.py, and add a new line inside BETTER_TRANFORMER_LAYERS_MAPPING_DICT, something that would look like: "EncoderLayer":FSMTEncoderLayerBetterTransformer

Step 5: You can check your conversion by doing something like:

from transformers import AutoModel
from optimum.bettertransformer import BetterTransformer

model = AutoModel.from_pretrained("hf-internal-testing/tiny-random-FSMTModel")
print(model)

And make sure your added layers are inside the model!
Let me know if there is anything unclear

@Sumanth077
Copy link
Contributor Author

Thankyou for the detailed breakdown @younesbelkada. I have followed and added all the things mentioned above.

  1. Created a BertEncoderLayerBetterTransformer Class and assigned all the keys required.
  2. Done Implementing the Forward Pass(Not sure whether this correct, but would love your detailed review and help here)
  3. Imported the new class and added (EncoderLayer":FSMTEncoderLayerBetterTransformer) to the dictionary
  4. Checked the conversion. The added layers are inside the model!
  5. Finally removed the Unnecessary files.

Kindly review the recent commits @younesbelkada. Thankyou ❤️

@younesbelkada
Copy link
Contributor

Hi @Sumanth077 !
Thanks a lot for your great work and your message! For now it looks very nice to me and we're close to merging that!
Now let's try to test your implementation before merging 💪
Would you mind adding the line "hf-internal-testing/tiny-random-FSMTModel" exactly here
Then you can just run pytest tests/bettertransformer//test_bettertransformer_encoder.py and let me know how it goes!

.gitignore Outdated Show resolved Hide resolved
@Sumanth077
Copy link
Contributor Author

Thankyou @younesbelkada. Unfortunately Out of 14, 4 Test Cases got passed, 6 Test cases Failed and 4 got ignored.

Including a screenshot for reference:
Screenshot 2022-11-22 at 5 47 17 PM

I will go through that. Also your assistance would be really great

@younesbelkada
Copy link
Contributor

younesbelkada commented Nov 22, 2022

Thanks! Could you please share with me the entire error trace?
Alternatively you can commit your changes (after removing the .gitignore and I can activate the tests here)

@Sumanth077
Copy link
Contributor Author

@younesbelkada Removed the .gitignorefile and also committed the remaining changes

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Nov 22, 2022

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

@younesbelkada
Copy link
Contributor

I have merged #501 , could you merge your branch with the current main branch ? 🙏 Thanks!

.gitignore Outdated Show resolved Hide resolved
@Sumanth077
Copy link
Contributor Author

@younesbelkada I have done the suggested changes. Could you kindly confirm it. I can merge the branch if everything is fine.

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your efforts!

It seems that we still need to address few points before merging 💪 Keep it up! And let me know if you face into any issue

.gitignore Outdated Show resolved Hide resolved
optimum/bettertransformer/models/encoder_models.py Outdated Show resolved Hide resolved
@Sumanth077
Copy link
Contributor Author

@younesbelkada Sorry for the confusion, I have modified the gitignore file and as well as re added the line in "test_better transformer".

And regarding the conversion, getting few errors after running the above script. I will go through and try to resolve that

@younesbelkada
Copy link
Contributor

Hi @Sumanth077 !
How is the integration going ? 💪 You are very close to merge this PR! Let us know here if you face into any issue!

@Sumanth077
Copy link
Contributor Author

Hi @younesbelkada I have fixed the changes and the conversion is fine.

Now I am not getting any errors while running the above script.

Now only 4 test cases are failing, previously it was 6.

@younesbelkada
Copy link
Contributor

Could you let me know which test is failing? and push your latest changes here? Thanks!

@Sumanth077
Copy link
Contributor Author

Hi @younesbelkada could you kindly help me in resolving the above Failed Test Cases.

One issue I guess is in the defining of Forward Pass.

@younesbelkada
Copy link
Contributor

Thanks for your patience @Sumanth077 !
This was slightly trickier than I have expected, I had to slightly refactor the testing suite :) The reason behind that is that encoder-decoder models expects decoder_inputs_ids as input together with input_ids. The best was to define a custom testing class for encoder-decoder based models as I suggested here.
I also ran styling with make style which is necessary before pushing any code here.
Apart from that, great job on converting the model! The PR is now ready for final review 💪

@younesbelkada younesbelkada marked this pull request as ready for review November 24, 2022 09:45
@younesbelkada younesbelkada changed the title Bettertransformer FSMT [BT] Add Bettertransformer support for FSMT Nov 24, 2022
Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Looking great! Thanks a lot for your clean implementation of FSMT integration for BetterTransformer ! 🎉
Leaving the last review for @fxmarty and/or @michaelbenayoun

@@ -70,7 +70,7 @@ def test_logits(self):
torch.manual_seed(0)
bt_hidden_states = converted_model(**inputs)[0]

if "gelu_new" in vars(random_config).values():
if "gelu_new" in list(random_config.to_dict().values()):
Copy link
Member

Choose a reason for hiding this comment

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

Great change, but no need to cast to list here!

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks! but I think the to_dict() is necessary (otherwise I get a VERY weird error from transformers)

Copy link
Member

Choose a reason for hiding this comment

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

Yes exactly, but accessing the value is enough here

@Sumanth077
Copy link
Contributor Author

Thankyou so much @younesbelkada. I couldn't have done it, without your Support. Really appreciate it❤️

Looking forward to contribute to more projects 🚀

@younesbelkada
Copy link
Contributor

Thanks a lot for your contribution @Sumanth077 !
One last thing, could you please add FSMT on the documentation together with the link to the paper: https://arxiv.org/abs/1907.06616 / the addition would go exactly here
Thanks!

@Sumanth077
Copy link
Contributor Author

Sumanth077 commented Nov 24, 2022

Hi @younesbelkada I have mistakenly called git pull origin local branch, now instead of one change I done in doc file. It is asking me commit all these files again.

Could you help me with this.

Screenshot 2022-11-24 at 7 14 15 PM

@younesbelkada
Copy link
Contributor

It is fine, I have just pushed it to the doc ;) Thanks again!

@michaelbenayoun michaelbenayoun merged commit 389a153 into huggingface:main Nov 24, 2022
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.

None yet

4 participants