

import math
import os
import re
import sys
import unittest
from pathlib import Path
from typing import Tuple
from unittest.mock import patch

from parameterized import parameterized

from transformers.testing_utils import (
    CaptureStderr,
    ExtendSysPath,
    TestCasePlus,
    execute_subprocess_async,
    get_gpu_count,
    get_torch_dist_unique_port,
    require_apex,
    require_bitsandbytes,
    require_fairscale,
    require_torch,
    require_torch_gpu,
    require_torch_multi_gpu,
    require_torch_non_multi_gpu,
    slow,
)
from transformers.trainer_callback import TrainerState
from transformers.trainer_utils import set_seed


a_						     =    os.path.abspath(os.path.dirname(__file__))
with ExtendSysPath(F'''{bindir}/../../examples/pytorch/translation'''):
			from run_translation import main  # noqa


set_seed(42)
a_						     =    'sshleifer/student_marian_en_ro_6_1'
a_						     =    'sshleifer/tiny-mbart'






@require_torch
class _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			def 							__UpperCamelCase (			self							: Optional[int]      ,      a							: int=False      ,      a							: str=None      ,      a							: List[str]=True      ,      a							: int=True      ,      a							: Dict=True      ,      a							: List[str]=True      ,      )							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       self.run_trainer(
				    eval_steps=1      ,      max_len=12      ,      model_name=a      ,      num_train_epochs=1      ,      distributed=a      ,      extra_args_str=a      ,      predict_with_generate=a      ,      do_train=a      ,      do_eval=a      ,      do_predict=a      ,      )
				SCREAMING_SNAKE_CASE   :      Any				     =       TrainerState.load_from_json(os.path.join(a      ,      "trainer_state.json"	)	).log_history

				if not do_eval:
					return

				SCREAMING_SNAKE_CASE   :      int				     =       [log for log in logs if "eval_loss" in log.keys()]

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       eval_metrics[0]
				if predict_with_generate:
					assert "eval_bleu" in first_step_stats

					SCREAMING_SNAKE_CASE   :      List[str]				     =       eval_metrics[-1]
					assert isinstance(last_step_stats["eval_bleu"]      ,      a	)
					assert not math.isnan(float(last_step_stats["eval_loss"]	)	), "eval_loss must not be `nan`"
			@require_torch_non_multi_gpu
			def 							__UpperCamelCase (			self							: List[str]	)							->       int:

				"""simple docstring"""

				self.run_seqaseq_quick()
			@require_torch_multi_gpu
			def 							__UpperCamelCase (			self							: Any	)							->       Dict:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a	)
			@require_torch_multi_gpu
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       int:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a	)
			@unittest.skip("Requires an update of the env running those tests"	)
			@require_torch_multi_gpu
			@require_fairscale
			def 							__UpperCamelCase (			self							: List[Any]	)							->       Optional[Any]:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a      ,      extra_args_str="--sharded_ddp simple"	)
			@unittest.skip("Requires an update of the env running those tests"	)
			@require_torch_multi_gpu
			@require_fairscale
			def 							__UpperCamelCase (			self							: int	)							->       List[Any]:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a      ,      extra_args_str="--sharded_ddp simple --fp16"	)
			@unittest.skip("Requires an update of the env running those tests"	)
			@require_torch_multi_gpu
			@require_fairscale
			def 							__UpperCamelCase (			self							: Any	)							->       Union[str, Any]:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a      ,      extra_args_str="--sharded_ddp zero_dp_2"      ,      predict_with_generate=a	)
			@unittest.skip("Requires an update of the env running those tests"	)
			@require_torch_multi_gpu
			@require_fairscale
			def 							__UpperCamelCase (			self							: str	)							->       int:

				"""simple docstring"""

				self.run_seqaseq_quick(
				    distributed=a      ,      extra_args_str="--sharded_ddp zero_dp_2 --fp16"      ,      predict_with_generate=a	)
			@require_apex
			@require_torch_gpu
			def 							__UpperCamelCase (			self							: List[Any]	)							->       List[Any]:

				"""simple docstring"""

				self.run_seqaseq_quick(distributed=a      ,      extra_args_str="--fp16 --fp16_backend=apex"	)
				# test 2nd time - was getting eval_loss': nan'
				# to reproduce the problem set distributed=False
				self.run_seqaseq_quick(distributed=a      ,      extra_args_str="--fp16 --fp16_backend=apex"	)
			@parameterized.expand(["base", "low", "high", "mixed"]	)
			@require_torch_multi_gpu
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: List[str]	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       {
				    # test with the default log_level - should be info and thus log info once
				    "base": {"extra_args_str": "", "n_matches": 1},
				    # test with low log_level and log_level_replica - should be noisy on all processes
				    # now the info string should appear twice on 2 processes
				    "low": {"extra_args_str": "--log_level debug --log_level_replica debug", "n_matches": 2},
				    # test with high log_level and low log_level_replica
				    # now the info string should appear once only on the replica
				    "high": {"extra_args_str": "--log_level error --log_level_replica debug", "n_matches": 1},
				    # test with high log_level and log_level_replica - should be quiet on all processes
				    "mixed": {"extra_args_str": "--log_level error --log_level_replica error", "n_matches": 0},
				}

				SCREAMING_SNAKE_CASE   :      str				     =       experiments[experiment_id]
				SCREAMING_SNAKE_CASE   :      int				     =       {"distributed": True, "predict_with_generate": False, "do_eval": False, "do_predict": False}
				SCREAMING_SNAKE_CASE   :      Dict				     =       "Running training"
				with CaptureStderr() as cl:
					self.run_seqaseq_quick(**a      ,      extra_args_str=data["extra_args_str"]	)
				SCREAMING_SNAKE_CASE   :      Dict				     =       len(re.findall(a      ,      cl.err	)	)
				self.assertEqual(a      ,      data["n_matches"]	)
			@slow
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.run_trainer(
				    eval_steps=2      ,      max_len=128      ,      model_name=a      ,      learning_rate=3e-4      ,      num_train_epochs=10      ,      distributed=a      ,      )

				# Check metrics
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       TrainerState.load_from_json(os.path.join(a      ,      "trainer_state.json"	)	).log_history
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       [log for log in logs if "eval_loss" in log.keys()]
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       eval_metrics[0]
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       eval_metrics[-1]

				assert first_step_stats["eval_loss"] > last_step_stats["eval_loss"], "model learned nothing"
				assert isinstance(last_step_stats["eval_bleu"]      ,      a	)

				# test if do_predict saves generations and metrics
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       os.listdir(a	)
				SCREAMING_SNAKE_CASE   :      str				     =       {os.path.basename(a	) for p in contents}
				assert "generated_predictions.txt" in contents
				assert "predict_results.json" in contents
			@slow
			@require_bitsandbytes
			def 							__UpperCamelCase (			self							: Dict	)							->       Optional[Any]:

				"""simple docstring"""

				from transformers.training_args import OptimizerNames

				def train_and_return_metrics(a							: str	) -> Tuple[int, float]:
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       "--skip_memory_metrics 0"

					SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.run_trainer(
					    max_len=128      ,      model_name=a      ,      learning_rate=3e-4      ,      num_train_epochs=1      ,      optim=a      ,      distributed=a      ,      extra_args_str=a      ,      do_eval=a      ,      do_predict=a      ,      n_gpus_to_use=1      ,      )

					# Check metrics
					SCREAMING_SNAKE_CASE   :      Tuple				     =       TrainerState.load_from_json(Path(a      ,      "trainer_state.json"	)	).log_history
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       int(logs[0]["train_mem_gpu_peaked_delta"] / 2**20	)
					SCREAMING_SNAKE_CASE   :      str				     =       int(logs[0]["train_mem_gpu_alloc_delta"] / 2**20	)

					SCREAMING_SNAKE_CASE   :      str				     =       logs[0]["train_loss"]
					return gpu_peak_mem_mb, gpu_alloc_mem_mb, loss

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Tuple				     =       train_and_return_metrics(OptimizerNames.ADAMW_TORCH.value	)
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[str]				     =       train_and_return_metrics(OptimizerNames.ADAMW_BNB.value	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       gpu_alloc_mem_orig - gpu_alloc_mem_bnb

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       gpu_peak_mem_orig + gpu_alloc_mem_orig
				SCREAMING_SNAKE_CASE   :      str				     =       gpu_peak_mem_bnb + gpu_alloc_mem_bnb
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       gpu_total_mem_orig - gpu_total_mem_bnb

				# sshleifer/student_marian_en_ro_6_1 has 54M parameter, 29M of which is `nn.Embedding` which
				# doesn't get quantized and remains in fp32. Therefore we only have 25M parameters quantized
				# in 2 bytes and the diff in optim memory usage is derived as so:
				#
				# - normal 25*8=~200MB (8 bytes per param)
				# - bnb    25*2= ~50MB (2 bytes per param)
				#
				# Thus we should expect ~150MB total memory saved.
				#
				# Peak memory should be the same - the total should be different by about that same margin
				#
				# After leaving a small margin to accommodate for differences between gpus let's check
				# that we have at least 120MB in savings
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       120

				# uncomment the following if this test starts failing - requires py38 for a new print feature
				# gpu_peak_mem_diff = gpu_peak_mem_orig - gpu_peak_mem_bnb
				# print(f"{gpu_alloc_mem_orig=}MB {gpu_peak_mem_orig=}MB {gpu_alloc_mem_orig+gpu_peak_mem_orig=}MB")
				# print(f" {gpu_alloc_mem_bnb=}MB  {gpu_peak_mem_bnb=}MB  {gpu_alloc_mem_bnb+gpu_peak_mem_bnb=}MB")
				# print(f"{gpu_alloc_mem_diff=}MB")
				# print(f"{gpu_peak_mem_diff=}MB")
				# print(f"{gpu_total_mem_orig=}MB, {gpu_total_mem_bnb=}MB")
				# print(f"{gpu_total_mem_diff=}MB, {gpu_total_mem_diff=}MB")

				self.assertGreater(
				    a      ,      a      ,      "should use ~150MB less alloc gpu memory with BNB, compared to without it for this model but got"
				    F" a difference of {gpu_alloc_mem_diff}MB, with gpu_alloc_mem_orig={gpu_alloc_mem_orig}MB and"
				    F" gpu_alloc_mem_bnb={gpu_alloc_mem_bnb}MB"      ,      )

				self.assertGreater(
				    a      ,      a      ,      "should use ~150MB less total gpu memory with BNB, compared to without it for this model but got"
				    F" a difference of {gpu_total_mem_diff}MB, with gpu_total_mem_orig={gpu_total_mem_orig}MB and"
				    F" gpu_total_mem_bnb={gpu_total_mem_bnb}MB"      ,      )

				self.assertEqual(
				    a      ,      a      ,      F"loss should be the same, but got loss_orig={loss_orig}, loss_bnb={loss_bnb}"	)






			def 							__UpperCamelCase (			self							: Any      ,      a							: int      ,      a							: str      ,      a							: int      ,      a							: float = 3e-3      ,      a							: str = "adafactor"      ,      a							: bool = False      ,      a							: str = None      ,      a							: int = 0      ,      a							: bool = True      ,      a							: bool = True      ,      a							: bool = True      ,      a							: bool = True      ,      a							: int = None      ,      )							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       self.test_file_dir / "../fixtures/tests_samples/wmt_en_ro"
				SCREAMING_SNAKE_CASE   :      Dict				     =       self.get_auto_remove_tmp_dir()
				SCREAMING_SNAKE_CASE   :      Dict				     =       F"\n            --model_name_or_path {model_name}\n            --train_file {data_dir}/train.json\n            --validation_file {data_dir}/val.json\n            --test_file {data_dir}/test.json\n            --output_dir {output_dir}\n            --overwrite_output_dir\n            --max_train_samples 8\n            --max_source_length {max_len}\n            --max_target_length {max_len}\n            --do_train\n            --num_train_epochs {str(a	)}\n            --per_device_train_batch_size 4\n            --learning_rate {learning_rate}\n            --warmup_steps 8\n            --logging_steps 0\n            --logging_strategy no\n            --save_steps {str(a	)}\n            --group_by_length\n            --label_smoothing_factor 0.1\n            --target_lang ro_RO\n            --source_lang en_XX\n        ".split()

				SCREAMING_SNAKE_CASE   :      Tuple				     =       F"\n            --do_eval\n            --per_device_eval_batch_size 4\n            --max_eval_samples 8\n            --val_max_target_length {max_len}\n            --evaluation_strategy steps\n            --eval_steps {str(a	)}\n        ".split()

				SCREAMING_SNAKE_CASE   :      List[str]				     =       "\n            --do_predict\n        ".split()

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       []
				if do_train:
					args += args_train

				if do_eval:
					args += args_eval

				if do_predict:
					args += args_predict

				if predict_with_generate:
					args += "--predict_with_generate".split()

				if do_train:
					if optim == "adafactor":
						args += "--adafactor".split()
					else:
						args += F"--optim {optim}".split()

				if extra_args_str is not None:
					args += extra_args_str.split()

				if distributed:
					if n_gpus_to_use is None:
						SCREAMING_SNAKE_CASE   :      Tuple				     =       get_gpu_count()
					SCREAMING_SNAKE_CASE   :      int				     =       get_torch_dist_unique_port()
					SCREAMING_SNAKE_CASE   :      str				     =       F"\n                -m torch.distributed.run\n                --nproc_per_node={n_gpus_to_use}\n                --master_port={master_port}\n                {self.examples_dir_str}/pytorch/translation/run_translation.py\n            ".split()
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       [sys.executable] + distributed_args + args
					# keep for quick debug
					# print(" ".join([f"\nPYTHONPATH={self.src_dir_str}"] +cmd)); die
					execute_subprocess_async(a      ,      env=self.get_env()	)
				else:
					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       ["run_translation.py"] + args
					with patch.object(a      ,      "argv"      ,      a	):
						main()

				return output_dir


from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
			from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor






class _UpperCamelCase							(							__A   ):
			'''simple docstring'''







			def 							__UpperCamelCase (			self							: Any	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.config_class(**self.inputs_dict	)
				self.parent.assertTrue(hasattr(a      ,      "embed_dim"	)	)
				self.parent.assertTrue(hasattr(a      ,      "num_heads"	)	)






class _UpperCamelCase							:
			'''simple docstring'''







			def __init__(			self							: Dict      ,      a							: int      ,      a							: List[str]=13      ,      a							: Optional[int]=64      ,      a							: List[str]=3      ,      a							: Dict=[16, 48, 96]      ,      a							: List[Any]=[1, 3, 6]      ,      a							: int=[1, 2, 10]      ,      a							: List[str]=[7, 3, 3]      ,      a							: List[Any]=[4, 2, 2]      ,      a							: Tuple=[2, 1, 1]      ,      a							: Union[str, Any]=[2, 2, 2]      ,      a							: List[str]=[False, False, True]      ,      a							: int=[0.0, 0.0, 0.0]      ,      a							: Dict=0.02      ,      a							: List[Any]=1e-12      ,      a							: Tuple=True      ,      a							: Optional[int]=True      ,      a							: Union[str, Any]=2      ,      )							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       parent
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       batch_size
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       image_size
				SCREAMING_SNAKE_CASE   :      Dict				     =       patch_sizes
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       patch_stride
				SCREAMING_SNAKE_CASE   :      str				     =       patch_padding
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       is_training
				SCREAMING_SNAKE_CASE   :      Dict				     =       use_labels
				SCREAMING_SNAKE_CASE   :      List[str]				     =       num_labels
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       num_channels
				SCREAMING_SNAKE_CASE   :      Any				     =       embed_dim
				SCREAMING_SNAKE_CASE   :      str				     =       num_heads
				SCREAMING_SNAKE_CASE   :      Any				     =       stride_kv
				SCREAMING_SNAKE_CASE   :      Tuple				     =       depth
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       cls_token
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       attention_drop_rate
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      Tuple				     =       layer_norm_eps
			def 							__UpperCamelCase (			self							: str	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

				SCREAMING_SNAKE_CASE   :      Any				     =       None
				if self.use_labels:
					# create a random int32 tensor of given shape
					SCREAMING_SNAKE_CASE   :      str				     =       ids_tensor([self.batch_size]      ,      self.num_labels	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.get_config()
				return config, pixel_values, labels
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Optional[int]:

				"""simple docstring"""

				return CvtConfig(
				    image_size=self.image_size      ,      num_labels=self.num_labels      ,      num_channels=self.num_channels      ,      embed_dim=self.embed_dim      ,      num_heads=self.num_heads      ,      patch_sizes=self.patch_sizes      ,      patch_padding=self.patch_padding      ,      patch_stride=self.patch_stride      ,      stride_kv=self.stride_kv      ,      depth=self.depth      ,      cls_token=self.cls_token      ,      attention_drop_rate=self.attention_drop_rate      ,      initializer_range=self.initializer_range      ,      )
			def 							__UpperCamelCase (			self							: Tuple      ,      a							: str      ,      a							: List[str]      ,      a							: str	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      str				     =       TFCvtModel(config=a	)
				SCREAMING_SNAKE_CASE   :      Any				     =       model(a      ,      training=a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       (self.image_size, self.image_size)
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Any				     =       image_size[0], image_size[1]
				for i in range(len(self.depth	)	):
					SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1	)
				self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.embed_dim[-1], height, width)	)
			def 							__UpperCamelCase (			self							: Union[str, Any]      ,      a							: int      ,      a							: List[Any]      ,      a							: List[str]	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       TFCvtForImageClassification(a	)
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       model(a      ,      labels=a      ,      training=a	)
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)	)






			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[str]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      str				     =       {"pixel_values": pixel_values}
				return config, inputs_dict






@require_tf
class _UpperCamelCase							(							__A							,	__A							,	unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
			lowerCamelCase__           =(
			    {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
			    if is_tf_available()
			    else {}
			)
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: List[Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       TFCvtModelTester(self	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       TFCvtConfigTester(self      ,      config_class=a      ,      has_text_modality=a      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

				self.config_tester.create_and_test_config_common_properties()
				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()
			@unittest.skip(reason="Cvt does not output attentions"	)
			def 							__UpperCamelCase (			self							: str	)							->       Optional[int]:

				"""simple docstring"""

				pass
			@unittest.skip(reason="Cvt does not use inputs_embeds"	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

				pass
			@unittest.skip(reason="Cvt does not support input and output embeddings"	)
			def 							__UpperCamelCase (			self							: List[Any]	)							->       Dict:

				"""simple docstring"""

				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."      ,      )
			def 							__UpperCamelCase (			self							: int	)							->       Tuple:

				"""simple docstring"""

				super().test_dataset_conversion()
			@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							: List[str]	)							->       str:

				"""simple docstring"""

				super().test_keras_fit()
			@unittest.skip(reason="Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       tf.keras.mixed_precision.Policy("mixed_float16"	)
				tf.keras.mixed_precision.set_global_policy(a	)
				super().test_keras_fit()
				tf.keras.mixed_precision.set_global_policy("float32"	)
			def 							__UpperCamelCase (			self							: List[str]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       model_class(a	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       inspect.signature(model.call	)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					SCREAMING_SNAKE_CASE   :      Tuple				     =       [*signature.parameters.keys()]

					SCREAMING_SNAKE_CASE   :      Tuple				     =       ["pixel_values"]
					self.assertListEqual(arg_names[:1]      ,      a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       str:

				"""simple docstring"""

				def check_hidden_states_output(a							: List[str]      ,      a							: Union[str, Any]      ,      a							: List[str]	):
					SCREAMING_SNAKE_CASE   :      Tuple				     =       model_class(a	)

					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(**self._prepare_for_class(a      ,      a	)	)
					SCREAMING_SNAKE_CASE   :      List[str]				     =       outputs.hidden_states

					SCREAMING_SNAKE_CASE   :      int				     =       len(self.model_tester.depth	)
					self.assertEqual(len(a	)      ,      a	)

					# verify the first hidden states (first block)
					self.assertListEqual(
					    list(hidden_states[0].shape[-3:]	)      ,      [
					        self.model_tester.embed_dim[0],
					        self.model_tester.image_size // 4,
					        self.model_tester.image_size // 4,
					    ]      ,      )

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       True
					check_hidden_states_output(a      ,      a      ,      a	)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					SCREAMING_SNAKE_CASE   :      str				     =       True

					check_hidden_states_output(a      ,      a      ,      a	)
			def 							__UpperCamelCase (			self							: Any	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*a	)






			@slow
			def 							__UpperCamelCase (			self							: Tuple	)							->       Optional[int]:

				"""simple docstring"""

				for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      Tuple				     =       TFCvtModel.from_pretrained(a	)
					self.assertIsNotNone(a	)

def  lowerCamelCase__ (					):
	SCREAMING_SNAKE_CASE   :      Optional[int]				     =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
	return image






@require_tf
@require_vision
class _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			@cached_property
			def 							__UpperCamelCase (			self							: Tuple	)							->       Tuple:

				"""simple docstring"""

				return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)






			@slow
			def 							__UpperCamelCase (			self							: Dict	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.default_image_processor
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       prepare_img()
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       image_processor(images=a      ,      return_tensors="tf"	)

				# forward pass
				SCREAMING_SNAKE_CASE   :      List[str]				     =       model(**a	)

				# verify the logits
				SCREAMING_SNAKE_CASE   :      Dict				     =       tf.TensorShape((1, 1000)	)
				self.assertEqual(outputs.logits.shape      ,      a	)

				SCREAMING_SNAKE_CASE   :      Tuple				     =       tf.constant([0.9285, 0.9015, -0.3150]	)
				self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()      ,      a      ,      atol=1e-4	)	)