



"""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  A_     :



						"""simple docstring"""






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






						def       UpperCAmelCase__	(   self					:str   )							->     Optional[int]:
												UpperCAmelCase			 =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

												UpperCAmelCase			 =					self.get_config()

												return config, pixel_values, labels






						def       UpperCAmelCase__	(   self					:Union[str, Any]   )							->     str:
												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					:Dict  ,    lowercase_					:Dict  ,    lowercase_					:Tuple  ,    lowercase_					:Optional[Any]   )							->     Any:
												UpperCAmelCase			 =					TFConvNextModel(config=lowercase_   )
												UpperCAmelCase			 =					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_					:Optional[int]  ,    lowercase_					:List[Any]  ,    lowercase_					:Optional[Any]   )							->     Optional[int]:
												UpperCAmelCase			 =					self.type_sequence_label_size
												UpperCAmelCase			 =					TFConvNextForImageClassification(lowercase_   )
												UpperCAmelCase			 =					model(lowercase_  ,    labels=lowercase_  ,    training=lowercase_   )
												self.parent.assertEqual(result.logits.shape  ,    (self.batch_size, self.type_sequence_label_size)   )






						def       UpperCAmelCase__	(   self					:int   )							->     Dict:
												UpperCAmelCase			 =					self.prepare_config_and_inputs()
												UpperCAmelCase ,       UpperCAmelCase ,       UpperCAmelCase			 =					config_and_inputs
												UpperCAmelCase			 =					{'pixel_values': pixel_values}
												return config, inputs_dict







@require_tf
class  A_     (	SCREAMING_SNAKE_CASE_      ,  SCREAMING_SNAKE_CASE_      ,  unittest.TestCase							):



						"""simple docstring"""
						__UpperCamelCase													=				(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
						__UpperCamelCase													=				(
						    {"""feature-extraction""": TFConvNextModel, """image-classification""": TFConvNextForImageClassification}
						    if is_tf_available()
						    else {}
						)

						__UpperCamelCase													=				False
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False
						__UpperCamelCase													=				False






						def       UpperCAmelCase__	(   self					:List[Any]   )							->     str:
												UpperCAmelCase			 =					TFConvNextModelTester(self   )
												UpperCAmelCase			 =					ConfigTester(
												    self  ,    config_class=lowercase_  ,    has_text_modality=lowercase_  ,    hidden_size=37  ,    )






						@unittest.skip(reason='ConvNext does not use inputs_embeds'   )
						def       UpperCAmelCase__	(   self					:Any   )							->     Optional[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					:Any   )							->     Optional[int]:
												super().test_keras_fit()






						@unittest.skip(reason='ConvNext does not support input and output embeddings'   )
						def       UpperCAmelCase__	(   self					:List[str]   )							->     List[Any]:
												pass






						def       UpperCAmelCase__	(   self					:str   )							->     Optional[int]:
												UpperCAmelCase ,       UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs_for_common()

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

																		UpperCAmelCase			 =					['pixel_values']
																		self.assertListEqual(arg_names[:1]  ,    lowercase_   )






						def       UpperCAmelCase__	(   self					:List[Any]   )							->     Dict:
												UpperCAmelCase			 =					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[str]   )							->     str:
												super().test_dataset_conversion()






						def       UpperCAmelCase__	(   self					:Dict   )							->     Any:
												def check_hidden_states_output(lowercase_					:Dict  ,    lowercase_					:str  ,    lowercase_					:Optional[Any]   ):
																		UpperCAmelCase			 =					model_class(lowercase_   )

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

																		UpperCAmelCase			 =					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]  ,    )

												UpperCAmelCase ,       UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs_for_common()

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

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

																		check_hidden_states_output(lowercase_  ,    lowercase_  ,    lowercase_   )






						def       UpperCAmelCase__	(   self					:Optional[int]   )							->     Optional[int]:
												UpperCAmelCase ,       UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs_for_common()

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

																		def recursive_check(lowercase_					:Optional[int]  ,    lowercase_					:Union[str, Any]   ):
																								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:
																		UpperCAmelCase			 =					model_class(lowercase_   )

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

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

																		UpperCAmelCase			 =					self._prepare_for_class(lowercase_  ,    lowercase_   )
																		UpperCAmelCase			 =					self._prepare_for_class(lowercase_  ,    lowercase_   )
																		check_equivalence(lowercase_  ,    lowercase_  ,    lowercase_  ,    {'output_hidden_states': True}   )

																		UpperCAmelCase			 =					self._prepare_for_class(lowercase_  ,    lowercase_  ,    return_labels=lowercase_   )
																		UpperCAmelCase			 =					self._prepare_for_class(lowercase_  ,    lowercase_  ,    return_labels=lowercase_   )
																		check_equivalence(lowercase_  ,    lowercase_  ,    lowercase_  ,    {'output_hidden_states': True}   )






						def       UpperCAmelCase__	(   self					:Tuple   )							->     List[str]:
												UpperCAmelCase			 =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*lowercase_   )






						@slow
						def       UpperCAmelCase__	(   self					:Tuple   )							->     Union[str, Any]:
												UpperCAmelCase			 =					TFConvNextModel.from_pretrained('facebook/convnext-tiny-224'   )
												self.assertIsNotNone(lowercase_   )


def 					_lowerCAmelCase (  ):
						UpperCAmelCase			 =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
						return image







@require_tf
@require_vision
class  A_     (	unittest.TestCase							):



						"""simple docstring"""






						@cached_property
						def       UpperCAmelCase__	(   self					:Union[str, Any]   )							->     Dict:
												return ConvNextImageProcessor.from_pretrained('facebook/convnext-tiny-224'   ) if is_vision_available() else None






						@slow
						def       UpperCAmelCase__	(   self					:Any   )							->     Optional[Any]:
												UpperCAmelCase			 =					TFConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'   )

												UpperCAmelCase			 =					self.default_image_processor
												UpperCAmelCase			 =					prepare_img()
												UpperCAmelCase			 =					image_processor(images=lowercase_  ,    return_tensors='tf'   )

												# forward pass
												UpperCAmelCase			 =					model(**lowercase_   )

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

												UpperCAmelCase			 =					tf.constant([-0.0260, -0.4739, 0.1911]   )

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









'''simple docstring'''

import argparse
import json
import logging
import os
import sys
from unittest import skip
from unittest.mock import patch

import tensorflow as tf

from transformers.testing_utils import TestCasePlus, get_gpu_count, slow


_UpperCAmelCase					:			str				     =					[
    os.path.join(os.path.dirname(__file__), dirname)
    for dirname in [
        """text-generation""",
        """text-classification""",
        """token-classification""",
        """language-modeling""",
        """multiple-choice""",
        """question-answering""",
        """summarization""",
        """translation""",
        """image-classification""",
    ]
]
sys.path.extend(SRC_DIRS)


if SRC_DIRS is not None:
	import run_clm
	import run_image_classification
	import run_mlm
	import run_ner
	import run_qa as run_squad
	import run_summarization
	import run_swag
	import run_text_classification
	import run_translation


logging.basicConfig(level=logging.DEBUG)

_UpperCAmelCase					:			Optional[Any]				     =					logging.getLogger()

def 				__magic_name__(    ):
						__lowerCAmelCase											=					argparse.ArgumentParser()
						parser.add_argument('''-f''')
						__lowerCAmelCase											=					parser.parse_args()
						return args.f

def 				__magic_name__(    lowerCamelCase):
						__lowerCAmelCase											=					{}
						__lowerCAmelCase											=					os.path.join(lowerCamelCase,					'''all_results.json''')
						if os.path.exists(lowerCamelCase):
												with open(lowerCamelCase,					'''r''') as f:
																		__lowerCAmelCase											=					json.load(lowerCamelCase)
						else:
												raise ValueError(F"""can't find {path}""")
						return results

def 				__magic_name__(    ):
						return bool(tf.config.list_physical_devices('''GPU'''))


_UpperCAmelCase					:			str				     =					logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)




class    a__					(			__A   ):







				"""simple docstring"""



				@skip('''Skipping until shape inference for to_tf_dataset PR is merged.'''		)
				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_text_classification.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --do_train
            --do_eval
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --max_steps=10
            --warmup_steps=2
            --seed=42
            --max_seq_length=128
            """.split()

										if is_cuda_available():
																testargs.append('''--fp16'''		)

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_text_classification.main()
																# Reset the mixed precision policy so we don't break other tests
																tf.keras.mixed_precision.set_global_policy('''float32'''		)
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''eval_accuracy''']							,      0.7_5		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_clm.py
            --model_name_or_path distilgpt2
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --do_train
            --do_eval
            --block_size 128
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --num_train_epochs 2
            --output_dir {tmp_dir}
            --overwrite_output_dir
            """.split()

										if len(tf.config.list_physical_devices('''GPU'''		)		) > 1:
																# Skipping because there are not enough batches to train the model + would need a drop_last to work.
																return

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_clm.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertLess(result['''eval_perplexity''']							,      1_00		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_mlm.py
            --model_name_or_path distilroberta-base
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --max_seq_length 64
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --do_train
            --do_eval
            --prediction_loss_only
            --num_train_epochs=1
            --learning_rate=1e-4
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_mlm.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertLess(result['''eval_perplexity''']							,      42		)




				def        _snake_case	(self		):
										# with so little data distributed training needs more epochs to get the score on par with 0/1 gpu
										__lowerCAmelCase											=					7 if get_gpu_count() > 1 else 2

										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_ner.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/conll/sample.json
            --validation_file tests/fixtures/tests_samples/conll/sample.json
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --do_train
            --do_eval
            --warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=2
            --num_train_epochs={epochs}
            --seed 7
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_ner.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''accuracy''']							,      0.7_5		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_qa.py
            --model_name_or_path bert-base-uncased
            --version_2_with_negative
            --train_file tests/fixtures/tests_samples/SQUAD/sample.json
            --validation_file tests/fixtures/tests_samples/SQUAD/sample.json
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --max_steps=10
            --warmup_steps=2
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_squad.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''f1''']							,      30		)
																self.assertGreaterEqual(result['''exact''']							,      30		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_swag.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/swag/sample.json
            --validation_file tests/fixtures/tests_samples/swag/sample.json
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --max_steps=20
            --warmup_steps=2
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_swag.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''val_accuracy''']							,      0.8		)




				@slow
				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_summarization.py
            --model_name_or_path t5-small
            --train_file tests/fixtures/tests_samples/xsum/sample.json
            --validation_file tests/fixtures/tests_samples/xsum/sample.json
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --max_steps=50
            --warmup_steps=8
            --do_train
            --do_eval
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_summarization.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''rouge1''']							,      10		)
																self.assertGreaterEqual(result['''rouge2''']							,      2		)
																self.assertGreaterEqual(result['''rougeL''']							,      7		)
																self.assertGreaterEqual(result['''rougeLsum''']							,      7		)




				@slow
				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_translation.py
            --model_name_or_path Rocketknight1/student_marian_en_ro_6_1
            --source_lang en
            --target_lang ro
            --train_file tests/fixtures/tests_samples/wmt16/sample.json
            --validation_file tests/fixtures/tests_samples/wmt16/sample.json
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --warmup_steps=8
            --do_train
            --do_eval
            --learning_rate=3e-3
            --num_train_epochs 12
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --source_lang en_XX
            --target_lang ro_RO
        """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_translation.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''bleu''']							,      30		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_auto_remove_tmp_dir()
										__lowerCAmelCase											=					F"""
            run_image_classification.py
            --dataset_name hf-internal-testing/cats_vs_dogs_sample
            --model_name_or_path microsoft/resnet-18
            --do_train
            --do_eval
            --learning_rate 1e-4
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --output_dir {tmp_dir}
            --overwrite_output_dir
            --dataloader_num_workers 16
            --num_train_epochs 2
            --train_val_split 0.1
            --seed 42
            --ignore_mismatched_sizes True
            """.split()

										with patch.object(__lowercase							,      '''argv'''							,      __lowercase		):
																run_image_classification.main()
																__lowerCAmelCase											=					get_results(__lowercase		)
																self.assertGreaterEqual(result['''accuracy''']							,      0.7		)

