





'''simple docstring'''

from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
  from PIL import Image

  from transformers import ConvNextImageProcessor






class      UpperCAmelCase			:
 def __init__(       self   :Optional[Any]		, lowercase_   :str		, lowercase_   :List[Any]=13		, lowercase_   :List[str]=32		, lowercase_   :Union[str, Any]=3		, lowercase_   :int=4		, lowercase_   :Any=[10, 20, 30, 40]		, lowercase_   :Optional[int]=[2, 2, 3, 2]		, lowercase_   :Optional[int]=True		, lowercase_   :int=True		, lowercase_   :Optional[Any]=37		, lowercase_   :Union[str, Any]="gelu"		, lowercase_   :Optional[int]=10		, lowercase_   :Union[str, Any]=0.0_2		, lowercase_   :List[Any]=3		, lowercase_   :str=None		, )->							Optional[int]:
     A__       =    parent
     A__       =    batch_size
     A__       =    image_size
     A__       =    num_channels
     A__       =    num_stages
     A__       =    hidden_sizes
     A__       =    depths
     A__       =    is_training
     A__       =    use_labels
     A__       =    intermediate_size
     A__       =    hidden_act
     A__       =    type_sequence_label_size
     A__       =    initializer_range
     A__       =    scope
 def   UpperCAmelCase_							(       self   :Tuple							)->							List[Any]:
     A__       =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

     A__       =    None
     if self.use_labels:
         A__       =    ids_tensor([self.batch_size]		, self.type_sequence_label_size							)

     A__       =    self.get_config()

     return config, pixel_values, labels
 def   UpperCAmelCase_							(       self   :List[Any]							)->							List[Any]:
     return ConvNextConfig(
         num_channels=self.num_channels		, hidden_sizes=self.hidden_sizes		, depths=self.depths		, num_stages=self.num_stages		, hidden_act=self.hidden_act		, is_decoder=lowercase_		, initializer_range=self.initializer_range		, )
 def   UpperCAmelCase_							(       self   :str		, lowercase_   :str		, lowercase_   :Optional[Any]		, lowercase_   :Union[str, Any]							)->							Optional[Any]:
     A__       =    TFConvNextModel(config=lowercase_							)
     A__       =    model(lowercase_		, training=lowercase_							)
     # expected last hidden states: B, C, H // 32, W // 32
     self.parent.assertEqual(
         result.last_hidden_state.shape		, (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		, )
 def   UpperCAmelCase_							(       self   :Optional[Any]		, lowercase_   :Dict		, lowercase_   :List[str]		, lowercase_   :List[Any]							)->							Dict:
     A__       =    self.type_sequence_label_size
     A__       =    TFConvNextForImageClassification(lowercase_							)
     A__       =    model(lowercase_		, labels=lowercase_		, training=lowercase_							)
     self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.type_sequence_label_size)							)



 def   UpperCAmelCase_							(       self   :str							)->							List[Any]:
     A__       =    self.prepare_config_and_inputs()
     A__,			A__,			A__       =    config_and_inputs
     A__       =    {"pixel_values": pixel_values}
     return config, inputs_dict






@require_tf
class      UpperCAmelCase			( UpperCamelCase__    , UpperCamelCase__    , unittest.TestCase       ):
 __lowercase     	=       (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
 __lowercase     	=       (
     {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
     if is_tf_available()
     else {}
 )

 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 __lowercase     	=       False
 def   UpperCAmelCase_							(       self   :List[str]							)->							str:
     A__       =    TFConvNextModelTester(self							)
     A__       =    ConfigTester(
         self		, config_class=lowercase_		, has_text_modality=lowercase_		, hidden_size=37		, )
 @unittest.skip(reason="ConvNext does not use inputs_embeds"							)
 def   UpperCAmelCase_							(       self   :int							)->							int:
     pass
 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices("GPU"							)							) == 0		, reason="TF does not support backprop for grouped convolutions on CPU."		, )
 @slow
 def   UpperCAmelCase_							(       self   :str							)->							List[Any]:
     super().test_keras_fit()
 @unittest.skip(reason="ConvNext does not support input and output embeddings"							)
 def   UpperCAmelCase_							(       self   :int							)->							Optional[Any]:
     pass
 def   UpperCAmelCase_							(       self   :int							)->							Optional[Any]:
     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         A__       =    model_class(lowercase_							)
         A__       =    inspect.signature(model.call							)
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         A__       =    [*signature.parameters.keys()]

         A__       =    ["pixel_values"]
         self.assertListEqual(arg_names[:1]		, lowercase_							)
 def   UpperCAmelCase_							(       self   :Optional[int]							)->							List[str]:
     A__       =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_							)
 @unittest.skipIf(
     not is_tf_available() or len(tf.config.list_physical_devices("GPU"							)							) == 0		, reason="TF does not support backprop for grouped convolutions on CPU."		, )
 def   UpperCAmelCase_							(       self   :List[Any]							)->							Dict:
     super().test_dataset_conversion()
 def   UpperCAmelCase_							(       self   :List[str]							)->							Tuple:
     def check_hidden_states_output(lowercase_   :Dict		, lowercase_   :Dict		, lowercase_   :List[str]							):
         A__       =    model_class(lowercase_							)

         A__       =    model(**self._prepare_for_class(lowercase_		, lowercase_							)							)
         A__       =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         A__       =    self.model_tester.num_stages
         self.assertEqual(len(lowercase_							)		, expected_num_stages + 1							)

         # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
         self.assertListEqual(
             list(hidden_states[0].shape[-2:]							)		, [self.model_tester.image_size // 4, self.model_tester.image_size // 4]		, )

     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         A__       =    True
         check_hidden_states_output(lowercase_		, lowercase_		, lowercase_							)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         A__       =    True

         check_hidden_states_output(lowercase_		, lowercase_		, lowercase_							)
 def   UpperCAmelCase_							(       self   :Any							)->							Any:
     A__,			A__       =    self.model_tester.prepare_config_and_inputs_for_common()

     def check_equivalence(lowercase_   :List[str]		, lowercase_   :List[str]		, lowercase_   :Union[str, Any]		, lowercase_   :Any={}							):
         A__       =    model(lowercase_		, return_dict=lowercase_		, **lowercase_							)
         A__       =    model(lowercase_		, return_dict=lowercase_		, **lowercase_							).to_tuple()

         def recursive_check(lowercase_   :str		, lowercase_   :Dict							):
             if isinstance(lowercase_		, (List, Tuple)							):
                 for tuple_iterable_value, dict_iterable_value in zip(lowercase_		, lowercase_							):
                     recursive_check(lowercase_		, lowercase_							)
             elif tuple_object is None:
                 return
             else:
                 self.assertTrue(
                     all(tf.equal(lowercase_		, lowercase_							)							)		, msg=(
                         "Tuple and dict output are not equal. Difference:"
                         F" {tf.math.reduce_max(tf.abs(tuple_object - dict_object							)							)}"
                     )		, )

             recursive_check(lowercase_		, lowercase_							)

     for model_class in self.all_model_classes:
         A__       =    model_class(lowercase_							)

         A__       =    self._prepare_for_class(lowercase_		, lowercase_							)
         A__       =    self._prepare_for_class(lowercase_		, lowercase_							)
         check_equivalence(lowercase_		, lowercase_		, lowercase_							)

         A__       =    self._prepare_for_class(lowercase_		, lowercase_		, return_labels=lowercase_							)
         A__       =    self._prepare_for_class(lowercase_		, lowercase_		, return_labels=lowercase_							)
         check_equivalence(lowercase_		, lowercase_		, lowercase_							)

         A__       =    self._prepare_for_class(lowercase_		, lowercase_							)
         A__       =    self._prepare_for_class(lowercase_		, lowercase_							)
         check_equivalence(lowercase_		, lowercase_		, lowercase_		, {"output_hidden_states": True}							)

         A__       =    self._prepare_for_class(lowercase_		, lowercase_		, return_labels=lowercase_							)
         A__       =    self._prepare_for_class(lowercase_		, lowercase_		, return_labels=lowercase_							)
         check_equivalence(lowercase_		, lowercase_		, lowercase_		, {"output_hidden_states": True}							)
 def   UpperCAmelCase_							(       self   :Optional[int]							)->							Optional[Any]:
     A__       =    self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_							)



 @slow
 def   UpperCAmelCase_							(       self   :Optional[Any]							)->							Union[str, Any]:
     A__       =    TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"							)
     self.assertIsNotNone(lowercase_							)





def 		UpperCamelCase       (							):
    A__       =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
    return image






@require_tf
@require_vision
class      UpperCAmelCase			( unittest.TestCase       ):
 @cached_property
 def   UpperCAmelCase_							(       self   :List[Any]							)->							Dict:
     return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"							) if is_vision_available() else None



 @slow
 def   UpperCAmelCase_							(       self   :Union[str, Any]							)->							List[str]:
     A__       =    TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"							)

     A__       =    self.default_image_processor
     A__       =    prepare_img()
     A__       =    image_processor(images=lowercase_		, return_tensors="tf"							)

     # forward pass
     A__       =    model(**lowercase_							)

     # verify the logits
     A__       =    tf.TensorShape((1, 10_00)							)
     self.assertEqual(outputs.logits.shape		, lowercase_							)

     A__       =    tf.constant([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]							)

     tf.debugging.assert_near(outputs.logits[0, :3]		, lowercase_		, atol=1E-4							)










'''simple docstring'''

import os
import tempfile
import unittest
from typing import List

from transformers.models.esm.tokenization_esm import VOCAB_FILES_NAMES, EsmTokenizer
from transformers.testing_utils import require_tokenizers
from transformers.tokenization_utils import PreTrainedTokenizer
from transformers.tokenization_utils_base import PreTrainedTokenizerBase






@require_tokenizers
class      UpperCAmelCase			( unittest.TestCase       ):
 __lowercase     	=       EsmTokenizer
 def   UpperCAmelCase_							(       self   :Any							)->							int:
     super().setUp()
     A__       =    tempfile.mkdtemp()
     # fmt: off
     A__       =    ["<cls>", "<pad>", "<eos>", "<unk>", "L", "A", "G", "V", "S", "E", "R", "T", "I", "D", "P", "K", "Q", "N", "F", "Y", "M", "H", "W", "C", "X", "B", "U", "Z", "O", ".", "-", "<null_1>", "<mask>"]  # noqa: E501
     # fmt: on
     A__       =    os.path.join(self.tmpdirname		, VOCAB_FILES_NAMES["vocab_file"]							)
     with open(self.vocab_file		, "w"		, encoding="utf-8"							) as vocab_writer:
         vocab_writer.write("".join([x + "\n" for x in vocab_tokens]							)							)
 def   UpperCAmelCase_							(       self   :Any		, **lowercase_   :int							)->							List[PreTrainedTokenizerBase]:
     return [self.get_tokenizer(**lowercase_							)]
 def   UpperCAmelCase_							(       self   :Any		, **lowercase_   :Dict							)->							PreTrainedTokenizer:
     return self.tokenizer_class.from_pretrained(self.tmpdirname		, **lowercase_							)
 def   UpperCAmelCase_							(       self   :int							)->							Dict:
     A__       =    self.tokenizer_class(self.vocab_file							)

     A__       =    tokenizer.tokenize("LAGVS"							)
     self.assertListEqual(lowercase_		, ["L", "A", "G", "V", "S"]							)
     self.assertListEqual(tokenizer.convert_tokens_to_ids(lowercase_							)		, [4, 5, 6, 7, 8]							)
 def   UpperCAmelCase_							(       self   :Tuple							)->							Tuple:
     A__       =    self.tokenizer_class(self.vocab_file							)

     A__       =    "LAGVS"
     self.assertListEqual(tokenizer.encode(lowercase_							)		, [0, 4, 5, 6, 7, 8, 2]							)
 def   UpperCAmelCase_							(       self   :Any							)->							Tuple:
     A__       =    self.tokenizer_class(self.vocab_file							)

     A__       =    ["LAGVS", "WCB"]
     A__       =    tokenizer(lowercase_		, padding=lowercase_							)["input_ids"]

     self.assertListEqual(lowercase_		, [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2]]							)
 def   UpperCAmelCase_							(       self   :str							)->							Any:
     A__       =    self.tokenizer_class(self.vocab_file							)

     A__       =    ["LAGVS", "WCB"]
     A__       =    tokenizer(lowercase_		, padding=lowercase_							)["input_ids"]

     self.assertListEqual(lowercase_		, [[0, 4, 5, 6, 7, 8, 2], [0, 22, 23, 25, 2, 1, 1]]							)



 def   UpperCAmelCase_							(       self   :Optional[Any]							)->							List[str]:
     A__       =    self.get_tokenizers(fast=lowercase_							)
     for tokenizer in tokenizers:
         with self.subTest(F"{tokenizer.__class__.__name__}"							):
             A__       =    "<unk>"
             A__       =    "<mask>"

             A__       =    tokenizer.tokenize(lowercase_							)
             A__       =    tokenizer.tokenize(lowercase_							)

             self.assertEqual(len(lowercase_							)		, 1							)
             self.assertEqual(len(lowercase_							)		, 1							)
             self.assertEqual(token_a[0]		, lowercase_							)
             self.assertEqual(token_a[0]		, lowercase_							)



