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

Port IDEFICS to tensorflow #26870

Merged
merged 119 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
61a1519
Initial commit
a8nova Oct 17, 2023
a976da1
Just a copy of modeling_idefics.py that will be ported to TF
a8nova Oct 17, 2023
1873605
- Prepend TF to the name of all classes
a8nova Oct 20, 2023
c7b8dbe
Add TF imports
a8nova Oct 20, 2023
9060913
Add autotranslated files
Rocketknight1 Oct 20, 2023
fccfbb0
Add TF classes to model_tf_auto.py
a8nova Oct 22, 2023
87dd0f9
Add the TF classes in model_doc
a8nova Oct 22, 2023
3c2309d
include auto-translated code
a8nova Oct 25, 2023
4eaf3f3
Adopted from auto-translated version
a8nova Oct 27, 2023
a8fabec
Add a forgotten super().build
a8nova Oct 30, 2023
2115f97
Add test code for TF version.
a8nova Nov 4, 2023
3a41a10
Fix indentation and load pytorch weights for now
a8nova Nov 5, 2023
411c02f
Some fixes. Many tests are still failing but some are passing now.
a8nova Nov 21, 2023
5da7317
Add ALL_LAYERNORM_LAYERS to match pytorch
a8nova Nov 21, 2023
6e356c5
Revert "Add ALL_LAYERNORM_LAYERS to match pytorch"
a8nova Nov 22, 2023
bdc06fe
Fix freeze_relevant_params()
a8nova Nov 22, 2023
3643fe8
Some more fixes
a8nova Nov 25, 2023
a8b4b4a
Fix test_attention_outputs
a8nova Nov 25, 2023
5d9e29f
Add tf stuff to processing_idefics.py
a8nova Nov 25, 2023
e5aef04
Pass return_tensors to image processing code and fix test
a8nova Dec 2, 2023
f468187
Pass return_tensors to the image processor __init__
a8nova Dec 8, 2023
74fbec8
Fix several test cases
a8nova Dec 8, 2023
1840c19
Some more fixes forgotten in last commit
a8nova Dec 9, 2023
e05549e
Fix processing code and vision_tf.py
a8nova Dec 27, 2023
39ed34f
Fix perceiver bug
a8nova Dec 31, 2023
10a54a1
Import from
a8nova Jan 9, 2024
e058b2c
Auto-add build() methods + style pass
Rocketknight1 Jan 16, 2024
5ba6381
Fix build() errors due to `None` being passed as shape to some layers
a8nova Jan 20, 2024
443a276
Change name in TFIdeficsForVisionText2Text to attribute in IdeficsFor…
a8nova Jan 20, 2024
d066a76
Fix pytorch weights load for tf2
a8nova Jan 24, 2024
0059e3e
Attempt to fix CI
a8nova Jan 24, 2024
4b153f5
Add back accidently removed line
a8nova Jan 24, 2024
f4ef81e
Remove torch-specific stuff from the TF test file
Rocketknight1 Jan 24, 2024
f446d48
make fix-copies, make style, remove autotranslated files
Rocketknight1 Jan 24, 2024
2bdd087
Fixes to imports/docstrings
Rocketknight1 Jan 24, 2024
bf1bbaf
Let's try the from future import in desperation
Rocketknight1 Jan 24, 2024
3ba416b
Fix the core random_attention_mask fn to match the torch/flax behaviour
Rocketknight1 Jan 24, 2024
576699f
Clean random_attention_mask up correctly
Rocketknight1 Jan 24, 2024
a25b241
Remove torch-only test
Rocketknight1 Jan 24, 2024
e1caf35
Fix loss shape, couple of nits
Rocketknight1 Jan 24, 2024
3b1ea02
make style
Rocketknight1 Jan 24, 2024
c8dd00c
Don't test for OOB embeddings because IDEFICS uses those deliberately
Rocketknight1 Jan 24, 2024
bf16b5e
Fix loss computation to handle masking
Rocketknight1 Jan 24, 2024
5709929
Fix test failures when flattening
a8nova Jan 24, 2024
7bd30ea
Fix some test failures
a8nova Jan 29, 2024
a2178ec
Add a proper stateless scaled_dot_product_attention
Rocketknight1 Jan 31, 2024
f195048
make style
Rocketknight1 Jan 31, 2024
5de955a
Adding missing attribute from the PyTorch version
Rocketknight1 Feb 1, 2024
3b95a14
Small cleanups to decoupledlinearlayer in case that helps
Rocketknight1 Feb 2, 2024
2d7199a
Pass epsilon to LayerNormalization
a8nova Feb 5, 2024
34e866d
Attemp to fix pytorch weight cross-loading for TFIdeficsEmbedding
a8nova Feb 5, 2024
1aba914
Fix a bug in TFIdeficsGatedCrossAttentionLayer
a8nova Feb 5, 2024
18ae095
Patching up build() methods
Rocketknight1 Feb 6, 2024
16a3274
Constant self.inv_freq
Rocketknight1 Feb 6, 2024
1fb31d6
Constant self.inv_freq
Rocketknight1 Feb 6, 2024
3ae9fcc
First working version
a8nova Feb 17, 2024
0fd2639
Fix some test failures
a8nova Feb 21, 2024
e767798
remove print statement
a8nova Feb 21, 2024
b2da9c2
Fix return_tensors
a8nova Feb 21, 2024
29f102c
Fix CI test failure check_code_quality
a8nova Feb 23, 2024
fdc4d2a
Attempt to fix CI failures by running `make fixup`
a8nova Feb 23, 2024
7a374b0
Attempt to fix tests_pr_documentation_tests
a8nova Feb 25, 2024
8e9c5b5
Fix a test failure in test_image_processing_idefics.py
a8nova Feb 26, 2024
ac96b55
Fix test test_pt_tf_model_equivalence
a8nova Mar 25, 2024
834b37f
Fix a few failures
a8nova Mar 28, 2024
3393ccb
Tiny fix
a8nova Apr 1, 2024
d075840
Some minor fixes
a8nova Apr 10, 2024
bb23c7c
Remove a duplicate test
a8nova Apr 10, 2024
92f78c8
Override a few test failures for IDEFICS
a8nova Apr 11, 2024
447fb88
Fix processing_idefics.py after rebase
a8nova Apr 11, 2024
9c548a1
Guard import keras with is_tf_available
a8nova Apr 11, 2024
100592b
fix check code quality
a8nova Apr 11, 2024
7f72162
fix check code quality
a8nova Apr 11, 2024
f4913ef
Minor fixes
a8nova Apr 12, 2024
596e06d
Skip test_save_load temporarily
a8nova Apr 14, 2024
ac9e72c
Run `ruff format tests src utils`
a8nova Apr 14, 2024
77a779f
Fix last failing test, `test_compile_tf_model`
a8nova Apr 16, 2024
e99fa82
Add fixes for vision_tf.py
a8nova Apr 17, 2024
c45a780
Minor fixes
a8nova Apr 17, 2024
0e59b95
Replace "<<<" with "<<" for doc tests
a8nova Apr 17, 2024
81232e9
Make code more readable
a8nova Apr 18, 2024
8ddb167
Fix bug after code review
a8nova Apr 19, 2024
3259268
Fix after code review
a8nova Apr 19, 2024
67bd686
Keep PyTorch as the default return_tensors
a8nova Apr 19, 2024
5502db4
Fixes to modeling_tf after code review
a8nova Apr 19, 2024
4b6084f
Fixes from code review
a8nova Apr 23, 2024
1fbae25
Run ruff
a8nova Apr 23, 2024
601100d
Undo a change
a8nova Apr 23, 2024
4483691
Refactor processing code after Matt's suggestion
a8nova Apr 23, 2024
832b2cd
Remove TODO's that aren't needed anymore
a8nova Apr 23, 2024
ecbb417
For pytorch, Use original pytorch processing code from main
a8nova Apr 23, 2024
c01e5a0
Update tests/models/idefics/test_modeling_idefics.py
Rocketknight1 Apr 23, 2024
29490c3
Update tests/models/idefics/test_modeling_tf_idefics.py
Rocketknight1 Apr 23, 2024
c2c097d
Add missing imports for is_pt_tf_cross_test
Rocketknight1 Apr 23, 2024
6179fe8
[DO NOT MERGE]: This is a commit for debugging and will be reverted
a8nova Apr 30, 2024
c7ddd5b
Revert "[DO NOT MERGE]: This is a commit for debugging and will be re…
a8nova Apr 30, 2024
c6bcbd9
[DO NOT MERGE]: This commit is for debugging a CI failure and will be…
a8nova May 3, 2024
a93bbe8
[DO NOT MERGE]: This commit is for debugging a CI failure and will be…
a8nova May 3, 2024
2e75279
Revert "[DO NOT MERGE]: This commit is for debugging a CI failure and…
a8nova May 3, 2024
e5e6200
Revert "[DO NOT MERGE]: This commit is for debugging a CI failure and…
a8nova May 3, 2024
b51b2f1
Don't skip test_save_load
a8nova May 4, 2024
19c7cc2
Debugging commit, will be reverted
a8nova May 5, 2024
63d44e5
Revert "Debugging commit, will be reverted"
a8nova May 5, 2024
fd76004
Override `test_save_load` and push model to save
a8nova May 5, 2024
8af771b
pass my repo_id
a8nova May 5, 2024
23878f1
add endpoint
a8nova May 5, 2024
f11e065
Pass a temp (write) token just for this CI
a8nova May 5, 2024
8963dba
Undo last few commits, still pushing to hub for model debugging
a8nova May 5, 2024
779ccda
Add logging to modeling tf utils, will be reverted just for debugging
a8nova May 5, 2024
5dadf2e
Debugging, will revert
a8nova May 5, 2024
f13bded
Revert "Debugging, will revert"
a8nova May 5, 2024
bda9fd8
Revert "Add logging to modeling tf utils, will be reverted just for d…
a8nova May 5, 2024
c2b3da8
Remove `test_save_load`
a8nova May 11, 2024
f492a0c
Run make fix-copies
a8nova May 11, 2024
6ef4954
Run ruff format tests src utils
a8nova May 11, 2024
4f27ec0
Debugging commit, will be reverted
a8nova May 12, 2024
c09ba32
Run ruff, also trigger CI run
a8nova May 12, 2024
8c8a879
Run ruff again
a8nova May 12, 2024
59c5f56
Undo debugging commit
a8nova May 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Flax), PyTorch, and/or TensorFlow.
| [HerBERT](model_doc/herbert) | ✅ | ✅ | ✅ |
| [Hubert](model_doc/hubert) | ✅ | ✅ | ❌ |
| [I-BERT](model_doc/ibert) | ✅ | ❌ | ❌ |
| [IDEFICS](model_doc/idefics) | ✅ | | ❌ |
| [IDEFICS](model_doc/idefics) | ✅ | | ❌ |
| [Idefics2](model_doc/idefics2) | ✅ | ❌ | ❌ |
| [ImageGPT](model_doc/imagegpt) | ✅ | ❌ | ❌ |
| [Informer](model_doc/informer) | ✅ | ❌ | ❌ |
Expand Down
10 changes: 10 additions & 0 deletions docs/source/en/model_doc/idefics.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ To train a new IDEFICS model from scratch use the m4 codebase (a link will be pr
[[autodoc]] IdeficsForVisionText2Text
- forward

## TFIdeficsModel

[[autodoc]] TFIdeficsModel
- call

## TFIdeficsForVisionText2Text

[[autodoc]] TFIdeficsForVisionText2Text
- call

## IdeficsImageProcessor

[[autodoc]] IdeficsImageProcessor
Expand Down
14 changes: 14 additions & 0 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3862,6 +3862,15 @@
"TFHubertPreTrainedModel",
]
)

_import_structure["models.idefics"].extend(
[
"TFIdeficsForVisionText2Text",
"TFIdeficsModel",
"TFIdeficsPreTrainedModel",
]
)

_import_structure["models.layoutlm"].extend(
[
"TFLayoutLMForMaskedLM",
Expand Down Expand Up @@ -7905,6 +7914,11 @@
TFHubertModel,
TFHubertPreTrainedModel,
)
from .models.idefics import (
TFIdeficsForVisionText2Text,
TFIdeficsModel,
TFIdeficsPreTrainedModel,
)
from .models.layoutlm import (
TFLayoutLMForMaskedLM,
TFLayoutLMForQuestionAnswering,
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/auto/modeling_tf_auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
("gptj", "TFGPTJModel"),
("groupvit", "TFGroupViTModel"),
("hubert", "TFHubertModel"),
("idefics", "TFIdeficsModel"),
("layoutlm", "TFLayoutLMModel"),
("layoutlmv3", "TFLayoutLMv3Model"),
("led", "TFLEDModel"),
Expand Down Expand Up @@ -112,6 +113,7 @@
("funnel", "TFFunnelForPreTraining"),
("gpt-sw3", "TFGPT2LMHeadModel"),
("gpt2", "TFGPT2LMHeadModel"),
("idefics", "TFIdeficsForVisionText2Text"),
("layoutlm", "TFLayoutLMForMaskedLM"),
("lxmert", "TFLxmertForPreTraining"),
("mobilebert", "TFMobileBertForPreTraining"),
Expand Down
30 changes: 29 additions & 1 deletion src/transformers/models/idefics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available
from ...utils import (
OptionalDependencyNotAvailable,
_LazyModule,
is_tf_available,
is_torch_available,
is_vision_available,
)


_import_structure = {"configuration_idefics": ["IdeficsConfig"]}
Expand All @@ -39,6 +45,17 @@
]
_import_structure["processing_idefics"] = ["IdeficsProcessor"]

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["modeling_tf_idefics"] = [
"TFIdeficsForVisionText2Text",
"TFIdeficsModel",
"TFIdeficsPreTrainedModel",
]

if TYPE_CHECKING:
from .configuration_idefics import IdeficsConfig
Expand All @@ -64,6 +81,17 @@
)
from .processing_idefics import IdeficsProcessor

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .modeling_tf_idefics import (
TFIdeficsForVisionText2Text,
TFIdeficsModel,
TFIdeficsPreTrainedModel,
)

else:
import sys
Expand Down
6 changes: 3 additions & 3 deletions src/transformers/models/idefics/image_processing_idefics.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ def preprocess(
image_mean: Optional[Union[float, List[float]]] = None,
image_std: Optional[Union[float, List[float]]] = None,
transform: Callable = None,
return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH,
**kwargs,
) -> TensorType.PYTORCH:
) -> TensorType:
"""
Preprocess a batch of images.

Expand Down Expand Up @@ -162,7 +163,6 @@ def preprocess(
images = [self.rescale(image=image, scale=1 / 255) for image in images]
images = [self.normalize(x, mean=image_mean, std=image_std) for x in images]
images = [to_channel_dimension_format(x, ChannelDimension.FIRST) for x in images]
# TODO: this converts to torch tensors - switch to convert_to_tensors once it becomes available
images = BatchFeature(data={"pixel_values": images}, tensor_type=TensorType.PYTORCH)["pixel_values"]
images = BatchFeature(data={"pixel_values": images}, tensor_type=return_tensors)["pixel_values"]

return images
Loading
Loading