

import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs






class      lowerCamelCase_      (	_lowercase	):




	def __init__(     self						:     int					,      __A						:     List[str]					,      __A						:     Optional[int]=None					,      __A						:     Any=True					,      __A						:     List[Any]=None					,      **__A						:     List[str]							):
		__A :			str		    =			parent
		__A :			str		    =			config_class
		__A :			List[str]		    =			has_text_modality
		__A :			int		    =			kwargs
		__A :			str		    =			common_properties




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			List[Any]		    =			self.config_class(**self.inputs_dict							)
		__A :			List[Any]		    =			(
		    ["""hidden_size""", """num_attention_heads""", """num_hidden_layers"""]
		    if self.common_properties is None
		    else self.common_properties
		)

		# Add common fields for text models
		if self.has_text_modality:
			common_properties.extend(["""vocab_size"""]							)

		# Test that config has the common properties as getters
		for prop in common_properties:
			self.parent.assertTrue(hasattr(__A					,      __A							)					,      msg=F"""`{prop}` does not exist"""							)

		# Test that config has the common properties as setter
		for idx, name in enumerate(__A							):
			try:
				setattr(__A					,      __A					,      __A							)
				self.parent.assertEqual(
				    getattr(__A					,      __A							)					,      __A					,      msg=F"""`{name} value {idx} expected, but was {getattr(__A					,      __A							)}"""							)
			except NotImplementedError:
				# Some models might not be able to implement setters for common_properties
				# In that case, a NotImplementedError is raised
				pass

        # Test if config class can be called with Config(prop_name=..)
		for idx, name in enumerate(__A							):
			try:
				__A :			Dict		    =			self.config_class(**{name: idx}							)
				self.parent.assertEqual(
				    getattr(__A					,      __A							)					,      __A					,      msg=F"""`{name} value {idx} expected, but was {getattr(__A					,      __A							)}"""							)
			except NotImplementedError:
				# Some models might not be able to implement setters for common_properties
				# In that case, a NotImplementedError is raised
				pass




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		__A :			List[str]		    =			self.config_class(**self.inputs_dict							)
		__A :			Optional[Any]		    =			json.loads(config.to_json_string()							)
		for key, value in self.inputs_dict.items():
			self.parent.assertEqual(obj[key]					,      __A							)




	def        lowerCAmelCase_       (     self						:     List[str]							):
		__A :			List[str]		    =			self.config_class(**self.inputs_dict							)

		with tempfile.TemporaryDirectory() as tmpdirname:
			__A :			List[Any]		    =			os.path.join(__A					,      """config.json"""							)
			config_first.to_json_file(__A							)
			__A :			List[str]		    =			self.config_class.from_json_file(__A							)

		self.parent.assertEqual(config_second.to_dict()					,      config_first.to_dict()							)




	def        lowerCAmelCase_       (     self						:     List[Any]							):
		__A :			Tuple		    =			self.config_class(**self.inputs_dict							)

		with tempfile.TemporaryDirectory() as tmpdirname:
			config_first.save_pretrained(__A							)
			__A :			str		    =			self.config_class.from_pretrained(__A							)

		self.parent.assertEqual(config_second.to_dict()					,      config_first.to_dict()							)




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		__A :			str		    =			self.config_class(**self.inputs_dict							)

		__A :			Dict		    =			"""test"""
		with tempfile.TemporaryDirectory() as tmpdirname:
			__A :			Union[str, Any]		    =			os.path.join(__A					,      __A							)
			config_first.save_pretrained(__A							)
			__A :			Dict		    =			self.config_class.from_pretrained(__A					,      subfolder=__A							)

		self.parent.assertEqual(config_second.to_dict()					,      config_first.to_dict()							)




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		__A :			Optional[Any]		    =			self.config_class(**self.inputs_dict					,      num_labels=5							)
		self.parent.assertEqual(len(config.idalabel							)					,      5							)
		self.parent.assertEqual(len(config.labelaid							)					,      5							)

		__A :			str		    =			3
		self.parent.assertEqual(len(config.idalabel							)					,      3							)
		self.parent.assertEqual(len(config.labelaid							)					,      3							)




	def        lowerCAmelCase_       (     self						:     Union[str, Any]							):
		if self.config_class.is_composition:
			return
		__A :			List[str]		    =			self.config_class()
		self.parent.assertIsNotNone(__A							)




	def        lowerCAmelCase_       (     self						:     Any							):
		__A :			Optional[Any]		    =			copy.deepcopy(__A							)
		__A :			Optional[int]		    =			self.config_class(**__A							)
		__A :			List[str]		    =			[]
		for key, value in config_common_kwargs.items():
			if key == "torch_dtype":
				if not is_torch_available():
					continue
				else:
					import torch

					if config.torch_dtype != torch.floataa:
						wrong_values.append(("""torch_dtype""", config.torch_dtype, torch.floataa)							)
			elif getattr(__A					,      __A							) != value:
				wrong_values.append((key, getattr(__A					,      __A							), value)							)

		if len(__A							) > 0:
			__A :			Dict		    =			"""\n""".join([F"""- {v[0]}: got {v[1]} instead of {v[2]}""" for v in wrong_values]							)
			raise ValueError(F"""The following keys were not properly set in the config:\n{errors}"""							)




	def        lowerCAmelCase_       (     self						:     Dict							):
		self.create_and_test_config_common_properties()
		self.create_and_test_config_to_json_string()
		self.create_and_test_config_to_json_file()
		self.create_and_test_config_from_and_save_pretrained()
		self.create_and_test_config_from_and_save_pretrained_subfolder()
		self.create_and_test_config_with_num_labels()
		self.check_config_can_be_init_without_params()
		self.check_config_arguments_init()







import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
    from transformers import TvltImageProcessor

if is_speech_available():
    from transformers import TvltFeatureExtractor

from transformers import TvltProcessor




@require_torch
class 					_lowerCamelCase			(   unittest.TestCase			):





     """simple docstring"""







     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__     : int											=					'''ZinengTang/tvlt-base'''
           UpperCamelCase__     : Optional[Any]											=					tempfile.mkdtemp()






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> Tuple:




           """simple docstring"""







           return TvltImageProcessor.from_pretrained(self.checkpoint    ,    **__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    **__SCREAMING_SNAKE_CASE	)	-> str:




           """simple docstring"""







           return TvltFeatureExtractor.from_pretrained(self.checkpoint    ,    **__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           shutil.rmtree(self.tmpdirname	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Any:




           """simple docstring"""







           UpperCamelCase__     : Optional[Any]											=					self.get_image_processor()
           UpperCamelCase__     : Tuple											=					self.get_feature_extractor()

           UpperCamelCase__     : Dict											=					TvltProcessor(image_processor=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE	)
           processor.save_pretrained(self.tmpdirname	)
           UpperCamelCase__     : Optional[Any]											=					TvltProcessor.from_pretrained(self.tmpdirname	)

           self.assertIsInstance(processor.feature_extractor    ,    __SCREAMING_SNAKE_CASE	)
           self.assertIsInstance(processor.image_processor    ,    __SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[str]:




           """simple docstring"""







           UpperCamelCase__     : Union[str, Any]											=					self.get_image_processor()
           UpperCamelCase__     : int											=					self.get_feature_extractor()

           UpperCamelCase__     : Union[str, Any]											=					TvltProcessor(image_processor=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : str											=					np.ones([1_2_0_0_0]	)

           UpperCamelCase__     : Dict											=					feature_extractor(__SCREAMING_SNAKE_CASE    ,    return_tensors='''np'''	)
           UpperCamelCase__     : Dict											=					processor(audio=__SCREAMING_SNAKE_CASE    ,    return_tensors='''np'''	)

           for key in audio_dict.keys():
                 self.assertAlmostEqual(audio_dict[key].sum()    ,    input_processor[key].sum()    ,    delta=1e-2	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Optional[int]:




           """simple docstring"""







           UpperCamelCase__     : Tuple											=					self.get_image_processor()
           UpperCamelCase__     : Tuple											=					self.get_feature_extractor()

           UpperCamelCase__     : str											=					TvltProcessor(image_processor=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : List[Any]											=					np.ones([3, 2_2_4, 2_2_4]	)

           UpperCamelCase__     : Optional[int]											=					image_processor(__SCREAMING_SNAKE_CASE    ,    return_tensors='''np'''	)
           UpperCamelCase__     : Tuple											=					processor(images=__SCREAMING_SNAKE_CASE    ,    return_tensors='''np'''	)

           for key in image_dict.keys():
                 self.assertAlmostEqual(image_dict[key].sum()    ,    input_processor[key].sum()    ,    delta=1e-2	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> str:




           """simple docstring"""







           UpperCamelCase__     : List[str]											=					self.get_image_processor()
           UpperCamelCase__     : Optional[Any]											=					self.get_feature_extractor()

           UpperCamelCase__     : Union[str, Any]											=					TvltProcessor(image_processor=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE	)

           UpperCamelCase__     : Dict											=					np.ones([1_2_0_0_0]	)
           UpperCamelCase__     : str											=					np.ones([3, 2_2_4, 2_2_4]	)

           UpperCamelCase__     : List[Any]											=					processor(audio=__SCREAMING_SNAKE_CASE    ,    images=__SCREAMING_SNAKE_CASE	)

           self.assertListEqual(list(inputs.keys()	)    ,    ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']	)

           # test if it raises when no input is passed
           with pytest.raises(__SCREAMING_SNAKE_CASE	):
                 processor()






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           UpperCamelCase__     : int											=					self.get_image_processor()
           UpperCamelCase__     : str											=					self.get_feature_extractor()

           UpperCamelCase__     : Dict											=					TvltProcessor(image_processor=__SCREAMING_SNAKE_CASE    ,    feature_extractor=__SCREAMING_SNAKE_CASE	)

           self.assertListEqual(
               processor.model_input_names    ,    image_processor.model_input_names + feature_extractor.model_input_names    ,    msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''    ,    )


