Add FlaxCLIP (#11883)
* add flax CLIP * default input_shape * add tests * fix test * fix name * fix docs * fix shapes * attend at least 1 token * flax conv to torch conv * return floats * fix equivalence tests * fix import * return attention_weights and update tests * fix dosctrings * address patricks comments * input_shape arg * add tests for get_image_features and get_text_features methods * fix tests
This commit is contained in:
@@ -304,7 +304,7 @@ Flax), PyTorch, and/or TensorFlow.
|
|||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| BlenderbotSmall | ✅ | ❌ | ✅ | ✅ | ❌ |
|
| BlenderbotSmall | ✅ | ❌ | ✅ | ✅ | ❌ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| CLIP | ✅ | ✅ | ✅ | ❌ | ❌ |
|
| CLIP | ✅ | ✅ | ✅ | ❌ | ✅ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
| CTRL | ✅ | ❌ | ✅ | ✅ | ❌ |
|
| CTRL | ✅ | ❌ | ✅ | ✅ | ❌ |
|
||||||
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
+-----------------------------+----------------+----------------+-----------------+--------------------+--------------+
|
||||||
|
|||||||
@@ -152,3 +152,24 @@ CLIPVisionModel
|
|||||||
|
|
||||||
.. autoclass:: transformers.CLIPVisionModel
|
.. autoclass:: transformers.CLIPVisionModel
|
||||||
:members: forward
|
:members: forward
|
||||||
|
|
||||||
|
|
||||||
|
FlaxCLIPModel
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.FlaxCLIPModel
|
||||||
|
:members: __call__, get_text_features, get_image_features
|
||||||
|
|
||||||
|
|
||||||
|
FlaxCLIPTextModel
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.FlaxCLIPTextModel
|
||||||
|
:members: __call__
|
||||||
|
|
||||||
|
|
||||||
|
FlaxCLIPVisionModel
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. autoclass:: transformers.FlaxCLIPVisionModel
|
||||||
|
:members: __call__
|
||||||
|
|||||||
@@ -1482,6 +1482,14 @@ if is_flax_available():
|
|||||||
"FlaxBertPreTrainedModel",
|
"FlaxBertPreTrainedModel",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
_import_structure["models.clip"].extend(
|
||||||
|
[
|
||||||
|
"FlaxCLIPModel",
|
||||||
|
"FlaxCLIPPreTrainedModel",
|
||||||
|
"FlaxCLIPTextModel",
|
||||||
|
"FlaxCLIPVisionModel",
|
||||||
|
]
|
||||||
|
)
|
||||||
_import_structure["models.electra"].extend(
|
_import_structure["models.electra"].extend(
|
||||||
[
|
[
|
||||||
"FlaxElectraForMaskedLM",
|
"FlaxElectraForMaskedLM",
|
||||||
@@ -2743,6 +2751,7 @@ if TYPE_CHECKING:
|
|||||||
FlaxBertModel,
|
FlaxBertModel,
|
||||||
FlaxBertPreTrainedModel,
|
FlaxBertPreTrainedModel,
|
||||||
)
|
)
|
||||||
|
from .models.clip import FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPVisionModel
|
||||||
from .models.electra import (
|
from .models.electra import (
|
||||||
FlaxElectraForMaskedLM,
|
FlaxElectraForMaskedLM,
|
||||||
FlaxElectraForMultipleChoice,
|
FlaxElectraForMultipleChoice,
|
||||||
|
|||||||
@@ -90,7 +90,12 @@ def convert_pytorch_state_dict_to_flax(pt_state_dict, flax_model):
|
|||||||
pt_tuple_key = pt_tuple_key[:-1] + ("scale",)
|
pt_tuple_key = pt_tuple_key[:-1] + ("scale",)
|
||||||
if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
|
if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
|
||||||
pt_tuple_key = pt_tuple_key[:-1] + ("embedding",)
|
pt_tuple_key = pt_tuple_key[:-1] + ("embedding",)
|
||||||
|
elif pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4 and pt_tuple_key not in random_flax_state_dict:
|
||||||
|
# conv layer
|
||||||
|
pt_tuple_key = pt_tuple_key[:-1] + ("kernel",)
|
||||||
|
pt_tensor = pt_tensor.transpose(2, 3, 1, 0)
|
||||||
elif pt_tuple_key[-1] == "weight" and pt_tuple_key not in random_flax_state_dict:
|
elif pt_tuple_key[-1] == "weight" and pt_tuple_key not in random_flax_state_dict:
|
||||||
|
# linear layer
|
||||||
pt_tuple_key = pt_tuple_key[:-1] + ("kernel",)
|
pt_tuple_key = pt_tuple_key[:-1] + ("kernel",)
|
||||||
pt_tensor = pt_tensor.T
|
pt_tensor = pt_tensor.T
|
||||||
elif pt_tuple_key[-1] == "gamma":
|
elif pt_tuple_key[-1] == "gamma":
|
||||||
@@ -170,7 +175,12 @@ def load_flax_weights_in_pytorch_model(pt_model, flax_state):
|
|||||||
flax_key_tuple = (pt_model.base_model_prefix,) + flax_key_tuple
|
flax_key_tuple = (pt_model.base_model_prefix,) + flax_key_tuple
|
||||||
|
|
||||||
# rename flax weights to PyTorch format
|
# rename flax weights to PyTorch format
|
||||||
if flax_key_tuple[-1] == "kernel" and ".".join(flax_key_tuple) not in pt_model_dict:
|
if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 4 and ".".join(flax_key_tuple) not in pt_model_dict:
|
||||||
|
# conv layer
|
||||||
|
flax_key_tuple = flax_key_tuple[:-1] + ("weight",)
|
||||||
|
flax_tensor = jnp.transpose(flax_tensor, (3, 2, 0, 1))
|
||||||
|
elif flax_key_tuple[-1] == "kernel" and ".".join(flax_key_tuple) not in pt_model_dict:
|
||||||
|
# linear layer
|
||||||
flax_key_tuple = flax_key_tuple[:-1] + ("weight",)
|
flax_key_tuple = flax_key_tuple[:-1] + ("weight",)
|
||||||
flax_tensor = flax_tensor.T
|
flax_tensor = flax_tensor.T
|
||||||
elif flax_key_tuple[-1] in ["scale", "embedding"]:
|
elif flax_key_tuple[-1] in ["scale", "embedding"]:
|
||||||
|
|||||||
@@ -49,12 +49,17 @@ from .utils import logging
|
|||||||
logger = logging.get_logger(__name__)
|
logger = logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def quick_gelu(x):
|
||||||
|
return x * jax.nn.sigmoid(1.702 * x)
|
||||||
|
|
||||||
|
|
||||||
ACT2FN = {
|
ACT2FN = {
|
||||||
"gelu": partial(nn.gelu, approximate=False),
|
"gelu": partial(nn.gelu, approximate=False),
|
||||||
"relu": nn.relu,
|
"relu": nn.relu,
|
||||||
"silu": nn.swish,
|
"silu": nn.swish,
|
||||||
"swish": nn.swish,
|
"swish": nn.swish,
|
||||||
"gelu_new": partial(nn.gelu, approximate=True),
|
"gelu_new": partial(nn.gelu, approximate=True),
|
||||||
|
"quick_gelu": quick_gelu,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ from ..bert.modeling_flax_bert import (
|
|||||||
FlaxBertForTokenClassification,
|
FlaxBertForTokenClassification,
|
||||||
FlaxBertModel,
|
FlaxBertModel,
|
||||||
)
|
)
|
||||||
|
from ..clip.modeling_flax_clip import FlaxCLIPModel
|
||||||
from ..electra.modeling_flax_electra import (
|
from ..electra.modeling_flax_electra import (
|
||||||
FlaxElectraForMaskedLM,
|
FlaxElectraForMaskedLM,
|
||||||
FlaxElectraForMultipleChoice,
|
FlaxElectraForMultipleChoice,
|
||||||
@@ -47,7 +48,7 @@ from ..roberta.modeling_flax_roberta import (
|
|||||||
FlaxRobertaModel,
|
FlaxRobertaModel,
|
||||||
)
|
)
|
||||||
from .auto_factory import auto_class_factory
|
from .auto_factory import auto_class_factory
|
||||||
from .configuration_auto import BertConfig, ElectraConfig, GPT2Config, RobertaConfig
|
from .configuration_auto import BertConfig, CLIPConfig, ElectraConfig, GPT2Config, RobertaConfig
|
||||||
|
|
||||||
|
|
||||||
logger = logging.get_logger(__name__)
|
logger = logging.get_logger(__name__)
|
||||||
@@ -60,6 +61,7 @@ FLAX_MODEL_MAPPING = OrderedDict(
|
|||||||
(BertConfig, FlaxBertModel),
|
(BertConfig, FlaxBertModel),
|
||||||
(GPT2Config, FlaxGPT2Model),
|
(GPT2Config, FlaxGPT2Model),
|
||||||
(ElectraConfig, FlaxElectraModel),
|
(ElectraConfig, FlaxElectraModel),
|
||||||
|
(CLIPConfig, FlaxCLIPModel),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,13 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from ...file_utils import _BaseLazyModule, is_tokenizers_available, is_torch_available, is_vision_available
|
from ...file_utils import (
|
||||||
|
_BaseLazyModule,
|
||||||
|
is_flax_available,
|
||||||
|
is_tokenizers_available,
|
||||||
|
is_torch_available,
|
||||||
|
is_vision_available,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
_import_structure = {
|
_import_structure = {
|
||||||
@@ -41,6 +47,14 @@ if is_torch_available():
|
|||||||
"CLIPVisionModel",
|
"CLIPVisionModel",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if is_flax_available():
|
||||||
|
_import_structure["modeling_flax_clip"] = [
|
||||||
|
"FlaxCLIPModel",
|
||||||
|
"FlaxCLIPPreTrainedModel",
|
||||||
|
"FlaxCLIPTextModel",
|
||||||
|
"FlaxCLIPVisionModel",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .configuration_clip import CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPTextConfig, CLIPVisionConfig
|
from .configuration_clip import CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, CLIPConfig, CLIPTextConfig, CLIPVisionConfig
|
||||||
@@ -62,6 +76,9 @@ if TYPE_CHECKING:
|
|||||||
CLIPVisionModel,
|
CLIPVisionModel,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if is_flax_available():
|
||||||
|
from .modeling_flax_clip import FlaxCLIPModel, FlaxCLIPPreTrainedModel, FlaxCLIPTextModel, FlaxCLIPVisionModel
|
||||||
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
import importlib
|
import importlib
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ class CLIPTextConfig(PretrainedConfig):
|
|||||||
num_attention_heads=8,
|
num_attention_heads=8,
|
||||||
max_position_embeddings=77,
|
max_position_embeddings=77,
|
||||||
hidden_act="quick_gelu",
|
hidden_act="quick_gelu",
|
||||||
layer_norm_eps=1e-5,
|
layer_norm_eps=0.00001,
|
||||||
dropout=0.0,
|
dropout=0.0,
|
||||||
attention_dropout=0.0,
|
attention_dropout=0.0,
|
||||||
initializer_range=0.02,
|
initializer_range=0.02,
|
||||||
@@ -189,7 +189,7 @@ class CLIPVisionConfig(PretrainedConfig):
|
|||||||
image_size=224,
|
image_size=224,
|
||||||
patch_size=32,
|
patch_size=32,
|
||||||
hidden_act="quick_gelu",
|
hidden_act="quick_gelu",
|
||||||
layer_norm_eps=1e-5,
|
layer_norm_eps=0.00001,
|
||||||
dropout=0.0,
|
dropout=0.0,
|
||||||
attention_dropout=0.0,
|
attention_dropout=0.0,
|
||||||
initializer_range=0.02,
|
initializer_range=0.02,
|
||||||
|
|||||||
1101
src/transformers/models/clip/modeling_flax_clip.py
Normal file
1101
src/transformers/models/clip/modeling_flax_clip.py
Normal file
File diff suppressed because it is too large
Load Diff
@@ -222,6 +222,42 @@ class FlaxBertPreTrainedModel:
|
|||||||
requires_backends(self, ["flax"])
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPModel:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPPreTrainedModel:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPTextModel:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPVisionModel:
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_pretrained(self, *args, **kwargs):
|
||||||
|
requires_backends(self, ["flax"])
|
||||||
|
|
||||||
|
|
||||||
class FlaxElectraForMaskedLM:
|
class FlaxElectraForMaskedLM:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
requires_backends(self, ["flax"])
|
requires_backends(self, ["flax"])
|
||||||
|
|||||||
512
tests/test_modeling_flax_clip.py
Normal file
512
tests/test_modeling_flax_clip.py
Normal file
@@ -0,0 +1,512 @@
|
|||||||
|
import inspect
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
import transformers
|
||||||
|
from transformers import CLIPConfig, CLIPTextConfig, CLIPVisionConfig, is_flax_available, is_torch_available
|
||||||
|
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow
|
||||||
|
|
||||||
|
from .test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
|
||||||
|
|
||||||
|
|
||||||
|
if is_flax_available():
|
||||||
|
import jax
|
||||||
|
import jax.numpy as jnp
|
||||||
|
from transformers.modeling_flax_pytorch_utils import (
|
||||||
|
convert_pytorch_state_dict_to_flax,
|
||||||
|
load_flax_weights_in_pytorch_model,
|
||||||
|
)
|
||||||
|
from transformers.models.clip.modeling_flax_clip import FlaxCLIPModel, FlaxCLIPTextModel, FlaxCLIPVisionModel
|
||||||
|
|
||||||
|
if is_torch_available():
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPVisionModelTester:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
parent,
|
||||||
|
batch_size=12,
|
||||||
|
image_size=30,
|
||||||
|
patch_size=2,
|
||||||
|
num_channels=3,
|
||||||
|
is_training=True,
|
||||||
|
hidden_size=32,
|
||||||
|
num_hidden_layers=5,
|
||||||
|
num_attention_heads=4,
|
||||||
|
intermediate_size=37,
|
||||||
|
dropout=0.1,
|
||||||
|
attention_dropout=0.1,
|
||||||
|
initializer_range=0.02,
|
||||||
|
scope=None,
|
||||||
|
):
|
||||||
|
self.parent = parent
|
||||||
|
self.batch_size = batch_size
|
||||||
|
self.image_size = image_size
|
||||||
|
self.patch_size = patch_size
|
||||||
|
self.num_channels = num_channels
|
||||||
|
self.is_training = is_training
|
||||||
|
self.hidden_size = hidden_size
|
||||||
|
self.num_hidden_layers = num_hidden_layers
|
||||||
|
self.num_attention_heads = num_attention_heads
|
||||||
|
self.intermediate_size = intermediate_size
|
||||||
|
self.dropout = dropout
|
||||||
|
self.attention_dropout = attention_dropout
|
||||||
|
self.initializer_range = initializer_range
|
||||||
|
self.scope = scope
|
||||||
|
|
||||||
|
def prepare_config_and_inputs(self):
|
||||||
|
pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
|
||||||
|
config = CLIPVisionConfig(
|
||||||
|
image_size=self.image_size,
|
||||||
|
patch_size=self.patch_size,
|
||||||
|
num_channels=self.num_channels,
|
||||||
|
hidden_size=self.hidden_size,
|
||||||
|
num_hidden_layers=self.num_hidden_layers,
|
||||||
|
num_attention_heads=self.num_attention_heads,
|
||||||
|
intermediate_size=self.intermediate_size,
|
||||||
|
dropout=self.dropout,
|
||||||
|
attention_dropout=self.attention_dropout,
|
||||||
|
initializer_range=self.initializer_range,
|
||||||
|
)
|
||||||
|
|
||||||
|
return config, pixel_values
|
||||||
|
|
||||||
|
def prepare_config_and_inputs_for_common(self):
|
||||||
|
config_and_inputs = self.prepare_config_and_inputs()
|
||||||
|
config, pixel_values = config_and_inputs
|
||||||
|
inputs_dict = {"pixel_values": pixel_values}
|
||||||
|
return config, inputs_dict
|
||||||
|
|
||||||
|
|
||||||
|
@require_flax
|
||||||
|
class FlaxCLIPVisionModelTest(FlaxModelTesterMixin, unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Here we also overwrite some of the tests of test_modeling_common.py, as CLIP does not use input_ids, inputs_embeds,
|
||||||
|
attention_mask and seq_length.
|
||||||
|
"""
|
||||||
|
|
||||||
|
all_model_classes = (FlaxCLIPVisionModel,) if is_flax_available() else ()
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.model_tester = FlaxCLIPVisionModelTester(self)
|
||||||
|
|
||||||
|
def test_forward_signature(self):
|
||||||
|
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
model = model_class(config)
|
||||||
|
signature = inspect.signature(model.__call__)
|
||||||
|
# signature.parameters is an OrderedDict => so arg_names order is deterministic
|
||||||
|
arg_names = [*signature.parameters.keys()]
|
||||||
|
|
||||||
|
expected_arg_names = ["pixel_values"]
|
||||||
|
self.assertListEqual(arg_names[:1], expected_arg_names)
|
||||||
|
|
||||||
|
def test_jit_compilation(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
with self.subTest(model_class.__name__):
|
||||||
|
prepared_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
|
||||||
|
model = model_class(config)
|
||||||
|
|
||||||
|
@jax.jit
|
||||||
|
def model_jitted(pixel_values, **kwargs):
|
||||||
|
return model(pixel_values=pixel_values, **kwargs).to_tuple()
|
||||||
|
|
||||||
|
with self.subTest("JIT Enabled"):
|
||||||
|
jitted_outputs = model_jitted(**prepared_inputs_dict)
|
||||||
|
|
||||||
|
with self.subTest("JIT Disabled"):
|
||||||
|
with jax.disable_jit():
|
||||||
|
outputs = model_jitted(**prepared_inputs_dict)
|
||||||
|
|
||||||
|
self.assertEqual(len(outputs), len(jitted_outputs))
|
||||||
|
for jitted_output, output in zip(jitted_outputs, outputs):
|
||||||
|
self.assertEqual(jitted_output.shape, output.shape)
|
||||||
|
|
||||||
|
def test_hidden_states_output(self):
|
||||||
|
def check_hidden_states_output(inputs_dict, config, model_class):
|
||||||
|
model = model_class(config)
|
||||||
|
|
||||||
|
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||||
|
hidden_states = outputs.hidden_states
|
||||||
|
|
||||||
|
self.assertEqual(len(hidden_states), self.model_tester.num_hidden_layers + 1)
|
||||||
|
|
||||||
|
# CLIP has a different seq_length
|
||||||
|
image_size = (self.model_tester.image_size, self.model_tester.image_size)
|
||||||
|
patch_size = (self.model_tester.patch_size, self.model_tester.patch_size)
|
||||||
|
num_patches = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
|
||||||
|
seq_length = num_patches + 1
|
||||||
|
|
||||||
|
self.assertListEqual(
|
||||||
|
list(hidden_states[0].shape[-2:]),
|
||||||
|
[seq_length, self.model_tester.hidden_size],
|
||||||
|
)
|
||||||
|
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
inputs_dict["output_hidden_states"] = True
|
||||||
|
check_hidden_states_output(inputs_dict, config, model_class)
|
||||||
|
|
||||||
|
# check that output_hidden_states also work using config
|
||||||
|
del inputs_dict["output_hidden_states"]
|
||||||
|
config.output_hidden_states = True
|
||||||
|
|
||||||
|
check_hidden_states_output(inputs_dict, config, model_class)
|
||||||
|
|
||||||
|
def test_attention_outputs(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
config.return_dict = True
|
||||||
|
|
||||||
|
# in CLIP, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
|
||||||
|
image_size = (self.model_tester.image_size, self.model_tester.image_size)
|
||||||
|
patch_size = (self.model_tester.patch_size, self.model_tester.patch_size)
|
||||||
|
num_patches = (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
|
||||||
|
seq_length = num_patches + 1
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
inputs_dict["output_attentions"] = True
|
||||||
|
inputs_dict["output_hidden_states"] = False
|
||||||
|
model = model_class(config)
|
||||||
|
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||||
|
attentions = outputs.attentions
|
||||||
|
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||||
|
|
||||||
|
# check that output_attentions also work using config
|
||||||
|
del inputs_dict["output_attentions"]
|
||||||
|
config.output_attentions = True
|
||||||
|
model = model_class(config)
|
||||||
|
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||||
|
attentions = outputs.attentions
|
||||||
|
self.assertEqual(len(attentions), self.model_tester.num_hidden_layers)
|
||||||
|
|
||||||
|
self.assertListEqual(
|
||||||
|
list(attentions[0].shape[-3:]),
|
||||||
|
[self.model_tester.num_attention_heads, seq_length, seq_length],
|
||||||
|
)
|
||||||
|
out_len = len(outputs)
|
||||||
|
|
||||||
|
# Check attention is always last and order is fine
|
||||||
|
inputs_dict["output_attentions"] = True
|
||||||
|
inputs_dict["output_hidden_states"] = True
|
||||||
|
model = model_class(config)
|
||||||
|
outputs = model(**self._prepare_for_class(inputs_dict, model_class))
|
||||||
|
|
||||||
|
added_hidden_states = 1
|
||||||
|
self.assertEqual(out_len + added_hidden_states, len(outputs))
|
||||||
|
|
||||||
|
self_attentions = outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
|
||||||
|
self.assertEqual(len(self_attentions), self.model_tester.num_hidden_layers)
|
||||||
|
|
||||||
|
self.assertListEqual(
|
||||||
|
list(self_attentions[0].shape[-3:]),
|
||||||
|
[self.model_tester.num_attention_heads, seq_length, seq_length],
|
||||||
|
)
|
||||||
|
|
||||||
|
@slow
|
||||||
|
def test_model_from_pretrained(self):
|
||||||
|
for model_class_name in self.all_model_classes:
|
||||||
|
model = model_class_name.from_pretrained("openai/clip-vit-base-patch32", from_pt=True)
|
||||||
|
outputs = model(np.ones((1, 3, 224, 224)))
|
||||||
|
self.assertIsNotNone(outputs)
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPTextModelTester:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
parent,
|
||||||
|
batch_size=12,
|
||||||
|
seq_length=7,
|
||||||
|
is_training=True,
|
||||||
|
use_input_mask=True,
|
||||||
|
use_labels=True,
|
||||||
|
vocab_size=99,
|
||||||
|
hidden_size=32,
|
||||||
|
num_hidden_layers=5,
|
||||||
|
num_attention_heads=4,
|
||||||
|
intermediate_size=37,
|
||||||
|
dropout=0.1,
|
||||||
|
attention_dropout=0.1,
|
||||||
|
max_position_embeddings=512,
|
||||||
|
initializer_range=0.02,
|
||||||
|
scope=None,
|
||||||
|
):
|
||||||
|
self.parent = parent
|
||||||
|
self.batch_size = batch_size
|
||||||
|
self.seq_length = seq_length
|
||||||
|
self.is_training = is_training
|
||||||
|
self.use_input_mask = use_input_mask
|
||||||
|
self.use_labels = use_labels
|
||||||
|
self.vocab_size = vocab_size
|
||||||
|
self.hidden_size = hidden_size
|
||||||
|
self.num_hidden_layers = num_hidden_layers
|
||||||
|
self.num_attention_heads = num_attention_heads
|
||||||
|
self.intermediate_size = intermediate_size
|
||||||
|
self.dropout = dropout
|
||||||
|
self.attention_dropout = attention_dropout
|
||||||
|
self.max_position_embeddings = max_position_embeddings
|
||||||
|
self.initializer_range = initializer_range
|
||||||
|
self.scope = scope
|
||||||
|
|
||||||
|
def prepare_config_and_inputs(self):
|
||||||
|
input_ids = ids_tensor([self.batch_size, self.seq_length], self.vocab_size)
|
||||||
|
|
||||||
|
input_mask = None
|
||||||
|
if self.use_input_mask:
|
||||||
|
input_mask = random_attention_mask([self.batch_size, self.seq_length])
|
||||||
|
|
||||||
|
if input_mask is not None:
|
||||||
|
batch_size, seq_length = input_mask.shape
|
||||||
|
rnd_start_indices = np.random.randint(1, seq_length - 1, size=(batch_size,))
|
||||||
|
for batch_idx, start_index in enumerate(rnd_start_indices):
|
||||||
|
input_mask[batch_idx, :start_index] = 1
|
||||||
|
input_mask[batch_idx, start_index:] = 0
|
||||||
|
|
||||||
|
config = CLIPTextConfig(
|
||||||
|
vocab_size=self.vocab_size,
|
||||||
|
hidden_size=self.hidden_size,
|
||||||
|
num_hidden_layers=self.num_hidden_layers,
|
||||||
|
num_attention_heads=self.num_attention_heads,
|
||||||
|
intermediate_size=self.intermediate_size,
|
||||||
|
dropout=self.dropout,
|
||||||
|
attention_dropout=self.attention_dropout,
|
||||||
|
max_position_embeddings=self.max_position_embeddings,
|
||||||
|
initializer_range=self.initializer_range,
|
||||||
|
)
|
||||||
|
|
||||||
|
return config, input_ids, input_mask
|
||||||
|
|
||||||
|
def prepare_config_and_inputs_for_common(self):
|
||||||
|
config_and_inputs = self.prepare_config_and_inputs()
|
||||||
|
config, input_ids, input_mask = config_and_inputs
|
||||||
|
inputs_dict = {"input_ids": input_ids, "attention_mask": input_mask}
|
||||||
|
return config, inputs_dict
|
||||||
|
|
||||||
|
|
||||||
|
@require_flax
|
||||||
|
class FlaxCLIPTextModelTest(FlaxModelTesterMixin, unittest.TestCase):
|
||||||
|
all_model_classes = (FlaxCLIPTextModel,) if is_flax_available() else ()
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.model_tester = FlaxCLIPTextModelTester(self)
|
||||||
|
|
||||||
|
@slow
|
||||||
|
def test_model_from_pretrained(self):
|
||||||
|
for model_class_name in self.all_model_classes:
|
||||||
|
model = model_class_name.from_pretrained("openai/clip-vit-base-patch32", from_pt=True)
|
||||||
|
outputs = model(np.ones((1, 1)))
|
||||||
|
self.assertIsNotNone(outputs)
|
||||||
|
|
||||||
|
|
||||||
|
class FlaxCLIPModelTester:
|
||||||
|
def __init__(self, parent, is_training=True):
|
||||||
|
self.parent = parent
|
||||||
|
self.text_model_tester = FlaxCLIPTextModelTester(parent)
|
||||||
|
self.vision_model_tester = FlaxCLIPVisionModelTester(parent)
|
||||||
|
self.is_training = is_training
|
||||||
|
|
||||||
|
def prepare_config_and_inputs(self):
|
||||||
|
text_config, input_ids, attention_mask = self.text_model_tester.prepare_config_and_inputs()
|
||||||
|
vision_config, pixel_values = self.vision_model_tester.prepare_config_and_inputs()
|
||||||
|
|
||||||
|
config = CLIPConfig.from_text_vision_configs(text_config, vision_config, projection_dim=64)
|
||||||
|
|
||||||
|
return config, input_ids, attention_mask, pixel_values
|
||||||
|
|
||||||
|
def prepare_config_and_inputs_for_common(self):
|
||||||
|
config_and_inputs = self.prepare_config_and_inputs()
|
||||||
|
config, input_ids, attention_mask, pixel_values = config_and_inputs
|
||||||
|
inputs_dict = {
|
||||||
|
"input_ids": input_ids,
|
||||||
|
"attention_mask": attention_mask,
|
||||||
|
"pixel_values": pixel_values,
|
||||||
|
}
|
||||||
|
return config, inputs_dict
|
||||||
|
|
||||||
|
|
||||||
|
@require_flax
|
||||||
|
class FlaxCLIPModelTest(FlaxModelTesterMixin, unittest.TestCase):
|
||||||
|
all_model_classes = (FlaxCLIPModel,) if is_flax_available() else ()
|
||||||
|
test_attention_outputs = False
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.model_tester = FlaxCLIPModelTester(self)
|
||||||
|
|
||||||
|
# hidden_states are tested in individual model tests
|
||||||
|
def test_hidden_states_output(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_jit_compilation(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
with self.subTest(model_class.__name__):
|
||||||
|
prepared_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
|
||||||
|
model = model_class(config)
|
||||||
|
|
||||||
|
@jax.jit
|
||||||
|
def model_jitted(input_ids, pixel_values, **kwargs):
|
||||||
|
return model(input_ids=input_ids, pixel_values=pixel_values, **kwargs).to_tuple()
|
||||||
|
|
||||||
|
with self.subTest("JIT Enabled"):
|
||||||
|
jitted_outputs = model_jitted(**prepared_inputs_dict)
|
||||||
|
|
||||||
|
with self.subTest("JIT Disabled"):
|
||||||
|
with jax.disable_jit():
|
||||||
|
outputs = model_jitted(**prepared_inputs_dict)
|
||||||
|
|
||||||
|
self.assertEqual(len(outputs), len(jitted_outputs))
|
||||||
|
for jitted_output, output in zip(jitted_outputs[:4], outputs[:4]):
|
||||||
|
self.assertEqual(jitted_output.shape, output.shape)
|
||||||
|
|
||||||
|
def test_forward_signature(self):
|
||||||
|
config, _ = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
model = model_class(config)
|
||||||
|
signature = inspect.signature(model.__call__)
|
||||||
|
# signature.parameters is an OrderedDict => so arg_names order is deterministic
|
||||||
|
arg_names = [*signature.parameters.keys()]
|
||||||
|
|
||||||
|
expected_arg_names = ["input_ids", "pixel_values", "attention_mask", "position_ids"]
|
||||||
|
self.assertListEqual(arg_names[:4], expected_arg_names)
|
||||||
|
|
||||||
|
def test_get_image_features(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
model = FlaxCLIPModel(config)
|
||||||
|
|
||||||
|
@jax.jit
|
||||||
|
def model_jitted(pixel_values):
|
||||||
|
return model.get_image_features(pixel_values=pixel_values)
|
||||||
|
|
||||||
|
with self.subTest("JIT Enabled"):
|
||||||
|
jitted_output = model_jitted(inputs_dict["pixel_values"])
|
||||||
|
|
||||||
|
with self.subTest("JIT Disabled"):
|
||||||
|
with jax.disable_jit():
|
||||||
|
output = model_jitted(inputs_dict["pixel_values"])
|
||||||
|
|
||||||
|
self.assertEqual(jitted_output.shape, output.shape)
|
||||||
|
self.assertTrue(np.allclose(jitted_output, output, atol=1e-3))
|
||||||
|
|
||||||
|
def test_get_text_features(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
model = FlaxCLIPModel(config)
|
||||||
|
|
||||||
|
@jax.jit
|
||||||
|
def model_jitted(input_ids, attention_mask, **kwargs):
|
||||||
|
return model.get_text_features(input_ids=input_ids, attention_mask=attention_mask)
|
||||||
|
|
||||||
|
with self.subTest("JIT Enabled"):
|
||||||
|
jitted_output = model_jitted(**inputs_dict)
|
||||||
|
|
||||||
|
with self.subTest("JIT Disabled"):
|
||||||
|
with jax.disable_jit():
|
||||||
|
output = model_jitted(**inputs_dict)
|
||||||
|
|
||||||
|
self.assertEqual(jitted_output.shape, output.shape)
|
||||||
|
self.assertTrue(np.allclose(jitted_output, output, atol=1e-3))
|
||||||
|
|
||||||
|
@slow
|
||||||
|
def test_model_from_pretrained(self):
|
||||||
|
for model_class_name in self.all_model_classes:
|
||||||
|
model = model_class_name.from_pretrained("openai/clip-vit-base-patch32", from_pt=True)
|
||||||
|
outputs = model(input_ids=np.ones((1, 1)), pixel_values=np.ones((1, 3, 224, 224)))
|
||||||
|
self.assertIsNotNone(outputs)
|
||||||
|
|
||||||
|
# overwrite from common since FlaxCLIPModel returns nested output
|
||||||
|
# which is not supported in the common test
|
||||||
|
@is_pt_flax_cross_test
|
||||||
|
def test_equivalence_pt_to_flax(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
with self.subTest(model_class.__name__):
|
||||||
|
# prepare inputs
|
||||||
|
prepared_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
|
||||||
|
pt_inputs = {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}
|
||||||
|
|
||||||
|
# load corresponding PyTorch class
|
||||||
|
pt_model_class_name = model_class.__name__[4:] # Skip the "Flax" at the beginning
|
||||||
|
pt_model_class = getattr(transformers, pt_model_class_name)
|
||||||
|
|
||||||
|
pt_model = pt_model_class(config).eval()
|
||||||
|
fx_model = model_class(config, dtype=jnp.float32)
|
||||||
|
|
||||||
|
fx_state = convert_pytorch_state_dict_to_flax(pt_model.state_dict(), fx_model)
|
||||||
|
fx_model.params = fx_state
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
pt_outputs = pt_model(**pt_inputs).to_tuple()
|
||||||
|
# PyTorch CLIPModel returns loss, we skip it here as we don't return loss in JAX/Flax models
|
||||||
|
pt_outputs = pt_outputs[1:]
|
||||||
|
|
||||||
|
fx_outputs = fx_model(**prepared_inputs_dict).to_tuple()
|
||||||
|
self.assertEqual(len(fx_outputs), len(pt_outputs), "Output lengths differ between Flax and PyTorch")
|
||||||
|
for fx_output, pt_output in zip(fx_outputs[:4], pt_outputs[:4]):
|
||||||
|
self.assert_almost_equals(fx_output, pt_output.numpy(), 4e-2)
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
pt_model.save_pretrained(tmpdirname)
|
||||||
|
fx_model_loaded = model_class.from_pretrained(tmpdirname, from_pt=True)
|
||||||
|
|
||||||
|
fx_outputs_loaded = fx_model_loaded(**prepared_inputs_dict).to_tuple()
|
||||||
|
self.assertEqual(
|
||||||
|
len(fx_outputs_loaded), len(pt_outputs), "Output lengths differ between Flax and PyTorch"
|
||||||
|
)
|
||||||
|
for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4], pt_outputs[:4]):
|
||||||
|
self.assert_almost_equals(fx_output_loaded, pt_output.numpy(), 4e-2)
|
||||||
|
|
||||||
|
# overwrite from common since FlaxCLIPModel returns nested output
|
||||||
|
# which is not supported in the common test
|
||||||
|
@is_pt_flax_cross_test
|
||||||
|
def test_equivalence_flax_to_pt(self):
|
||||||
|
config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
|
||||||
|
|
||||||
|
for model_class in self.all_model_classes:
|
||||||
|
with self.subTest(model_class.__name__):
|
||||||
|
# prepare inputs
|
||||||
|
prepared_inputs_dict = self._prepare_for_class(inputs_dict, model_class)
|
||||||
|
pt_inputs = {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}
|
||||||
|
|
||||||
|
# load corresponding PyTorch class
|
||||||
|
pt_model_class_name = model_class.__name__[4:] # Skip the "Flax" at the beginning
|
||||||
|
pt_model_class = getattr(transformers, pt_model_class_name)
|
||||||
|
|
||||||
|
pt_model = pt_model_class(config).eval()
|
||||||
|
fx_model = model_class(config, dtype=jnp.float32)
|
||||||
|
|
||||||
|
pt_model = load_flax_weights_in_pytorch_model(pt_model, fx_model.params)
|
||||||
|
|
||||||
|
# make sure weights are tied in PyTorch
|
||||||
|
pt_model.tie_weights()
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
pt_outputs = pt_model(**pt_inputs).to_tuple()
|
||||||
|
# PyTorch CLIPModel returns loss, we skip it here as we don't return loss in JAX/Flax models
|
||||||
|
pt_outputs = pt_outputs[1:]
|
||||||
|
|
||||||
|
fx_outputs = fx_model(**prepared_inputs_dict).to_tuple()
|
||||||
|
self.assertEqual(len(fx_outputs), len(pt_outputs), "Output lengths differ between Flax and PyTorch")
|
||||||
|
for fx_output, pt_output in zip(fx_outputs[:4], pt_outputs[:4]):
|
||||||
|
self.assert_almost_equals(fx_output, pt_output.numpy(), 4e-2)
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
fx_model.save_pretrained(tmpdirname)
|
||||||
|
pt_model_loaded = pt_model_class.from_pretrained(tmpdirname, from_flax=True)
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
pt_outputs_loaded = pt_model_loaded(**pt_inputs).to_tuple()
|
||||||
|
pt_outputs_loaded = pt_outputs_loaded[1:]
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
len(fx_outputs), len(pt_outputs_loaded), "Output lengths differ between Flax and PyTorch"
|
||||||
|
)
|
||||||
|
for fx_output, pt_output in zip(fx_outputs[:4], pt_outputs_loaded[:4]):
|
||||||
|
self.assert_almost_equals(fx_output, pt_output.numpy(), 4e-2)
|
||||||
@@ -60,6 +60,22 @@ def ids_tensor(shape, vocab_size, rng=None):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def floats_tensor(shape, scale=1.0, rng=None, name=None):
|
||||||
|
"""Creates a random float32 tensor"""
|
||||||
|
if rng is None:
|
||||||
|
rng = random.Random()
|
||||||
|
|
||||||
|
total_dims = 1
|
||||||
|
for dim in shape:
|
||||||
|
total_dims *= dim
|
||||||
|
|
||||||
|
values = []
|
||||||
|
for _ in range(total_dims):
|
||||||
|
values.append(rng.random() * scale)
|
||||||
|
|
||||||
|
return np.array(values, dtype=jnp.float32).reshape(shape)
|
||||||
|
|
||||||
|
|
||||||
def random_attention_mask(shape, rng=None):
|
def random_attention_mask(shape, rng=None):
|
||||||
attn_mask = ids_tensor(shape, vocab_size=2, rng=rng)
|
attn_mask = ids_tensor(shape, vocab_size=2, rng=rng)
|
||||||
# make sure that at least one token is attended to for each batch
|
# make sure that at least one token is attended to for each batch
|
||||||
|
|||||||
@@ -93,6 +93,8 @@ IGNORE_NON_AUTO_CONFIGURED = [
|
|||||||
# models to ignore for model xxx mapping
|
# models to ignore for model xxx mapping
|
||||||
"CLIPTextModel",
|
"CLIPTextModel",
|
||||||
"CLIPVisionModel",
|
"CLIPVisionModel",
|
||||||
|
"FlaxCLIPTextModel",
|
||||||
|
"FlaxCLIPVisionModel",
|
||||||
"DPRReader",
|
"DPRReader",
|
||||||
"DPRSpanPredictor",
|
"DPRSpanPredictor",
|
||||||
"FlaubertForQuestionAnswering",
|
"FlaubertForQuestionAnswering",
|
||||||
|
|||||||
Reference in New Issue
Block a user