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

Add BetterTransformer support for FlavaModel #538

Conversation

katiele47
Copy link

@katiele47 katiele47 commented Dec 2, 2022

What does this PR do?

Fixes #20372

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?
  • [Other] Applied make style

Questions

  • Should I make this PR (and future PRs that aren't ready for review but are worth discussing) into a draft PR? There are some questions I'd like to confirm with you in advance.
  • Would it be necessary to add the BetterTransformersFlavaTest or should I reuse BetterTransformersViltTest for FlavaModel? If so, should I update the name to BetterTransformersVisionTextTest?
  • I ran into the following issues when testing the conversion of FlavaModel with both pytest and running this script
from transformers import AutoModel
from optimum.bettertransformer import BetterTransformer

model_id = "hf-internal-testing/tiny-random-FlavaModel"
model = AutoModel.from_pretrained(model_id)
bt_model = BetterTransformer.transform(model)

Error:

 ~/optimum/optimum/bettertransformer/models/base.py:81 in validate_bettertransformer    │
│                                                                                                  │
│    78 │   │                                                                                      │
│    79 │   │   # Check activation function                                                        │
│    80 │   │   if self.act_fn not in SUPPORTED_ACTIVATION_FUNCTIONS:                              │
│ ❱  81 │   │   │   raise ValueError(                                                              │
│    82 │   │   │   │   f"Activation function {self.act_fn} not supported" " for `BetterTransfor   │
│    83 │   │   │   )                                                                              │
│    84 │   │   self.use_gelu = (self.act_fn == "gelu") or (self.act_fn == "gelu_new")             │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
ValueError: Activation function None not supported for `BetterTransformer` integration.

I couldn't find where the self.act_fn get defined inside the forward method after following this guide. Would appreciate any help!

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.

Hi @katiele47 !
Thanks so much for your very clean and great PR! Everything looks perfect 🔥
Regarding FLAVA, I quickly dived into it and the fix seems to be relatively simple. The model has 2 components, image and text encoders. Each of these submodules has its own config file that you can access with .image_config or .text_config. I think that the fix that I propose here is fine, as for both sub-configs the same hyper parameters seems to be used (i.e. same hidden_size, same hidden_act, etc.). Check for example this config file

After accepting my suggestion, you can run pytest tests/bettertransformer/test_bettertransformer_vision.py:: BetterTransformersFlavaTest to make sure the test pass ;)

optimum/bettertransformer/models/encoder_models.py Outdated Show resolved Hide resolved
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
@katiele47
Copy link
Author

Thanks a lot for your insight @younesbelkada! I will run the test and update this PR

@katiele47
Copy link
Author

The test script now worked with the following output, but I encountered 2 failed tests from BetterTransformersViltTest and BetterTransformerFlavaTest. Do you think I should use a different resized image here? It seems weird how the BetterTransformersViltTest previously passed with the current input image.

FAILED tests/bettertransformer/test_bettertransformer_vision.py::BetterTransformersViLTTest::test_logits - ValueError: Input image size (224*224) doesn't match model (30*30).
FAILED tests/bettertransformer/test_bettertransformer_vision.py::BetterTransformersFlavaTest::test_logits - ValueError: Input image size (224*224) doesn't match model (30*30).

Test script output:

FlavaModel(
  (text_model): FlavaTextModel(
    (embeddings): FlavaTextEmbeddings(
      (word_embeddings): Embedding(1124, 32, padding_idx=0)
      (position_embeddings): Embedding(512, 32)
      (token_type_embeddings): Embedding(2, 32)
      (LayerNorm): LayerNorm((32,), eps=1e-12, elementwise_affine=True)
      (dropout): Dropout(p=0.0, inplace=False)
    )
    (encoder): FlavaEncoder(
      (layer): ModuleList(
        (0): FlavaLayerBetterTransformer()
        (1): FlavaLayerBetterTransformer()
        (2): FlavaLayerBetterTransformer()
        (3): FlavaLayerBetterTransformer()
        (4): FlavaLayerBetterTransformer()
      )
    )
    (layernorm): LayerNorm((32,), eps=1e-12, elementwise_affine=True)
    (pooler): FlavaPooler(
      (dense): Linear(in_features=32, out_features=32, bias=True)
      (activation): Tanh()
    )
  )
  (image_model): FlavaImageModel(
    (embeddings): FlavaImageEmbeddings(
      (patch_embeddings): PatchEmbeddings(
        (projection): Conv2d(3, 32, kernel_size=(2, 2), stride=(2, 2))
      )
      (dropout): Dropout(p=0.0, inplace=False)
    )
    (encoder): FlavaEncoder(
      (layer): ModuleList(
        (0): FlavaLayerBetterTransformer()
        (1): FlavaLayerBetterTransformer()
        (2): FlavaLayerBetterTransformer()
        (3): FlavaLayerBetterTransformer()
        (4): FlavaLayerBetterTransformer()
      )
    )
    (layernorm): LayerNorm((32,), eps=1e-12, elementwise_affine=True)
    (pooler): FlavaPooler(
      (dense): Linear(in_features=32, out_features=32, bias=True)
      (activation): Tanh()
    )
  )
  (multimodal_model): FlavaMultimodalModel(
    (encoder): FlavaEncoder(
      (layer): ModuleList(
        (0): FlavaLayerBetterTransformer()
        (1): FlavaLayerBetterTransformer()
        (2): FlavaLayerBetterTransformer()
        (3): FlavaLayerBetterTransformer()
        (4): FlavaLayerBetterTransformer()
      )
    )
    (layernorm): LayerNorm((32,), eps=1e-12, elementwise_affine=True)
    (pooler): FlavaPooler(
      (dense): Linear(in_features=32, out_features=32, bias=True)
      (activation): Tanh()
    )
  )
  (image_projection): Linear(in_features=32, out_features=32, bias=True)
  (text_projection): Linear(in_features=32, out_features=32, bias=True)
  (image_to_mm_projection): Linear(in_features=32, out_features=32, bias=True)
  (text_to_mm_projection): Linear(in_features=32, out_features=32, bias=True)
)

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 so much for digging into the issue!
I think that there is a problem with the feature extractor in hf-internal-testing/tiny-random-FlavaModel , can you please try with a new model that I created? (Suggestion below) Thanks!

tests/bettertransformer/test_bettertransformer_vision.py Outdated Show resolved Hide resolved
katiele47 and others added 3 commits December 5, 2022 16:54
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
…d-better-transformers-support-for-flava
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

@katiele47
Copy link
Author

Thanks again for your time and suggestion @younesbelkada! I have made the changes accordingly, and all tests passed. I'll keep an eye on any arising CircleCI issues in the meantime.

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.

Hi @katiele47 !
I left a couple of comments, this should fix the failing tests! ;)
Thanks again

michaelbenayoun and others added 15 commits December 6, 2022 10:13
* ORTQuantizer saves model config when possible

* Fix docstring

* ORTModel can load any name for the ONNX model

* Use regex now

* Almost ready

* modeling_ort

* test

* Works for ORTModel

* Fixed bugs

* remove pdb

* Remove unnecessary attribute

* ORTDecoderModel ready

* Fix issue

* Make style

* fix issues

* Rebase and style

* Fix

* Fix issues

* Apply suggestions

Co-authored-by: Michael Benayoun <michael@huggingface.co>
* Add IO binding for ORTModelForCustomTasks

* Add test

* Fix test model

* Force inputs to be contiguous

* Fix docstring

* Nits
* Update README

* Change section

* Add links to examples

* Modify BetterTransformer documentation section name

* Add link to the documentation

* Rephrasing

* Update README.md

Co-authored-by: fxmarty <9808326+fxmarty@users.noreply.github.com>

* Update readme

Co-authored-by: fxmarty <9808326+fxmarty@users.noreply.github.com>
* Fix issues

* Fix issues

* Apply suggestion

Co-authored-by: Michael Benayoun <michael@huggingface.co>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
…ransformers 4.25.1 (huggingface#561)

* Fix wrap

* Improve import check

* Improve import check
* add clip

* style and test

* better support

* debug

* style

* support clip vision model

* typo

* fix test

* fix test

* add clip in doc

* fix test

* remove comment

* Update optimum/bettertransformer/models/encoder_models.py

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* fix style

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
…nx` in `ORTDecoder` (huggingface#554)

* add support

* cleaning

* remove useless import

* remove print

* styling post merge

* Update optimum/onnxruntime/modeling_decoder.py

Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>

* simplify multiple ONNX export

* fix path

* avoid code duplicate, better doc

* fix and simplify test

Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>
* enable FP16Optimizer for fp16 deepspeed training

* wrap apex optimizer

* Update optimum/onnxruntime/trainer.py

Co-authored-by: Jingya HUANG <44135271+JingyaHuang@users.noreply.github.com>

* Update optimum/onnxruntime/trainer.py

Co-authored-by: Jingya HUANG <44135271+JingyaHuang@users.noreply.github.com>

* formating trainer.py

* reformat using black 22.6

Co-authored-by: Jingya HUANG <44135271+JingyaHuang@users.noreply.github.com>
Co-authored-by: Adam Louly <adamlouly@microsoft.com@orttrainingdev9.d32nl1ml4oruzj4qz3bqlggovf.px.internal.cloudapp.net>
@katiele47
Copy link
Author

@younesbelkada Sorry for the multiple file changes, I think it's due to merging conflicts with upstream main, but I'm not entirely sure. Please let me know if this affect the original flava changes.

@katiele47 katiele47 mentioned this pull request Mar 22, 2023
3 tasks
@katiele47
Copy link
Author

Hi @younesbelkada I have created a new PR here that replaces this outdated PR. Please review and let me know if I should close this one. Thanks a lot!

@fxmarty fxmarty closed this Aug 11, 2023
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.

Community contribution - BetterTransformer integration for more models!
8 participants