


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 							A   (	unittest.TestCase							):
					'''simple docstring'''






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




							"""simple docstring"""


							lowercase__					      =		"""ZinengTang/tvlt-base"""
							lowercase__					      =		tempfile.mkdtemp()

					def   lowerCamelCase__					(self   :						str				, **_UpperCAmelCase   :						Optional[Any]		)						->       List[str]:




							"""simple docstring"""


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

					def   lowerCamelCase__					(self   :						List[Any]				, **_UpperCAmelCase   :						Union[str, Any]		)						->       Optional[int]:




							"""simple docstring"""


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

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




							"""simple docstring"""


							shutil.rmtree(self.tmpdirname		)

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




							"""simple docstring"""


							lowercase__					      =		self.get_image_processor()
							lowercase__					      =		self.get_feature_extractor()

							lowercase__					      =		TvltProcessor(image_processor=_UpperCAmelCase				, feature_extractor=_UpperCAmelCase		)
							processor.save_pretrained(self.tmpdirname		)
							lowercase__					      =		TvltProcessor.from_pretrained(self.tmpdirname		)

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

					def   lowerCamelCase__					(self   :						List[Any]		)						->       Tuple:




							"""simple docstring"""


							lowercase__					      =		self.get_image_processor()
							lowercase__					      =		self.get_feature_extractor()

							lowercase__					      =		TvltProcessor(image_processor=_UpperCAmelCase				, feature_extractor=_UpperCAmelCase		)

							lowercase__					      =		np.ones([1_2000]		)

							lowercase__					      =		feature_extractor(_UpperCAmelCase				, return_tensors="""np"""		)
							lowercase__					      =		processor(audio=_UpperCAmelCase				, return_tensors="""np"""		)

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

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




							"""simple docstring"""


							lowercase__					      =		self.get_image_processor()
							lowercase__					      =		self.get_feature_extractor()

							lowercase__					      =		TvltProcessor(image_processor=_UpperCAmelCase				, feature_extractor=_UpperCAmelCase		)

							lowercase__					      =		np.ones([3, 224, 224]		)

							lowercase__					      =		image_processor(_UpperCAmelCase				, return_tensors="""np"""		)
							lowercase__					      =		processor(images=_UpperCAmelCase				, return_tensors="""np"""		)

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

					def   lowerCamelCase__					(self   :						Optional[int]		)						->       Dict:




							"""simple docstring"""


							lowercase__					      =		self.get_image_processor()
							lowercase__					      =		self.get_feature_extractor()

							lowercase__					      =		TvltProcessor(image_processor=_UpperCAmelCase				, feature_extractor=_UpperCAmelCase		)

							lowercase__					      =		np.ones([1_2000]		)
							lowercase__					      =		np.ones([3, 224, 224]		)

							lowercase__					      =		processor(audio=_UpperCAmelCase				, images=_UpperCAmelCase		)

							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(_UpperCAmelCase		):
									processor()


					def   lowerCamelCase__					(self   :						str		)						->       List[Any]:




							"""simple docstring"""


							lowercase__					      =		self.get_image_processor()
							lowercase__					      =		self.get_feature_extractor()

							lowercase__					      =		TvltProcessor(image_processor=_UpperCAmelCase				, feature_extractor=_UpperCAmelCase		)

							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"""				, )





from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow


if is_tf_available():
	import numpy as np
	import tensorflow as tf

	from transformers import TFXLMRobertaModel



@require_tf
@require_sentencepiece
@require_tokenizers
class 							A   (	unittest.TestCase							):
					'''simple docstring'''






					@slow
					def   lowerCamelCase__					(self   :						Any		)						->       Union[str, Any]:




							"""simple docstring"""


							lowercase__					      =		TFXLMRobertaModel.from_pretrained("""jplu/tf-xlm-roberta-base"""		)

							lowercase__					      =		{
							    """input_ids""": tf.convert_to_tensor([[0, 2646, 1_0269, 83, 9_9942, 2]]				, dtype=tf.intaa		),  # "My dog is cute"
							    """attention_mask""": tf.convert_to_tensor([[1, 1, 1, 1, 1, 1]]				, dtype=tf.intaa		),
							}

							lowercase__					      =		model(_UpperCAmelCase		)["""last_hidden_state"""]
							lowercase__					      =		tf.TensorShape((1, 6, 768)		)
							self.assertEqual(output.shape				, _UpperCAmelCase		)
							# compare the actual values for a slice.
							lowercase__					      =		tf.convert_to_tensor(
							    [
							        [
							            [0.0_681_762, 0.10_894_451, 0.06_772_504],
							            [-0.06_423_668, 0.02_366_615, 0.04_329_344],
							            [-0.06_057_295, 0.09_974_135, -0.00_070_584],
							        ]
							    ]				, dtype=tf.floataa				, )

							self.assertTrue(np.allclose(output[:, :3, :3].numpy()				, expected_slice.numpy()				, atol=1E-4		)		)

