
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import TvltImageProcessor

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    List[str]				,    __UpperCamelCase			:    str=10				,    __UpperCamelCase			:    List[str]=10				,    __UpperCamelCase			:    Any=False				,    __UpperCamelCase			:    int=False							)			->							List[str]:

				"""simple docstring"""







				A__	:						Tuple								=				[]
				for i in range(image_processor_tester.num_frames							):
								video.append(np.random.randint(2_55				,    size=(image_processor_tester.num_channels, width, height)				,    dtype=np.uinta							)							)

				if not numpify and not torchify:
								# PIL expects the channel dimension as last dimension
								A__	:						int								=				[Image.fromarray(np.moveaxis(__UpperCamelCase				,    0				,    -1							)							) for frame in video]

				if torchify:
								A__	:						Optional[Any]								=				[torch.from_numpy(__UpperCamelCase							) for frame in video]

				return video

def 					SCREAMING_SNAKE_CASE  (   __UpperCamelCase			:    Optional[int]				,    __UpperCamelCase			:    Optional[Any]=False				,    __UpperCamelCase			:    str=False				,    __UpperCamelCase			:    Dict=False							)			->							Any:

				"""simple docstring"""







				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				A__	:						int								=				[]
				for i in range(image_processor_tester.batch_size							):
								if equal_resolution:
												A__	:						Optional[int]								=				image_processor_tester.max_resolution
								else:
												A__							,				A__	:						str								=				np.random.choice(
												    np.arange(image_processor_tester.min_resolution				,    image_processor_tester.max_resolution							)				,    2							)
												A__	:						Dict								=				prepare_video(
												    image_processor_tester=__UpperCamelCase				,    width=__UpperCamelCase				,    height=__UpperCamelCase				,    numpify=__UpperCamelCase				,    torchify=__UpperCamelCase				,    )
								video_inputs.append(__UpperCamelCase							)

				return video_inputs







class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	def __init__(      self					,							UpperCamelCase__					,							UpperCamelCase__=7					,							UpperCamelCase__=3					,							UpperCamelCase__=4					,							UpperCamelCase__=18					,							UpperCamelCase__=30					,							UpperCamelCase__=400					,							UpperCamelCase__=True					,							UpperCamelCase__=None					,							UpperCamelCase__=True					,							UpperCamelCase__=[0.5, 0.5, 0.5]					,							UpperCamelCase__=[0.5, 0.5, 0.5]					,							UpperCamelCase__=True					,							UpperCamelCase__=None					,							):
					A__	:						Union[str, Any]								=				size if size is not None else {'''shortest_edge''': 18}
					A__	:						List[Any]								=				crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

					A__	:						List[str]								=				parent
					A__	:						Optional[Any]								=				batch_size
					A__	:						Union[str, Any]								=				num_channels
					A__	:						Optional[int]								=				num_frames
					A__	:						Optional[int]								=				image_size
					A__	:						Union[str, Any]								=				min_resolution
					A__	:						List[str]								=				max_resolution
					A__	:						Dict								=				do_resize
					A__	:						Tuple								=				size
					A__	:						Optional[int]								=				do_normalize
					A__	:						Optional[int]								=				image_mean
					A__	:						Optional[Any]								=				image_std
					A__	:						str								=				do_center_crop
					A__	:						List[Any]								=				crop_size





	def 	__snake_case					(      self		):
					return {
					    "image_mean": self.image_mean,
					    "image_std": self.image_std,
					    "do_normalize": self.do_normalize,
					    "do_resize": self.do_resize,
					    "size": self.size,
					    "do_center_crop": self.do_center_crop,
					    "crop_size": self.crop_size,
					}







@require_torch
@require_vision
class 			UpperCamelCase__		(     SCREAMING_SNAKE_CASE_,      unittest.TestCase ):







	'''simple docstring'''
	_lowerCAmelCase											=					TvltImageProcessor if is_vision_available() else None





	def 	__snake_case					(      self		):
					A__	:						Optional[Any]								=				TvltImageProcessorTester(self		)





	@property
	def 	__snake_case					(      self		):
					return self.image_processor_tester.prepare_image_processor_dict()





	def 	__snake_case					(      self		):
					A__	:						Optional[Any]								=				self.image_processing_class(**self.image_processor_dict		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_mean'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''image_std'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_normalize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_resize'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''do_center_crop'''		)		)
					self.assertTrue(hasattr(UpperCamelCase__					,							'''size'''		)		)





	def 	__snake_case					(      self		):
					# Initialize image_processor
					A__	:						Optional[int]								=				self.image_processing_class(**self.image_processor_dict		)
					# create random PIL videos
					A__	:						Optional[int]								=				prepare_video_inputs(self.image_processor_tester					,							equal_resolution=UpperCamelCase__		)
					for video in video_inputs:
									self.assertIsInstance(UpperCamelCase__					,							UpperCamelCase__		)
									self.assertIsInstance(video[0]					,							Image.Image		)

					# Test not batched input
					A__	:						Tuple								=				image_processor(video_inputs[0]					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        1,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						List[str]								=				image_processor(UpperCamelCase__					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)





	def 	__snake_case					(      self		):
					# Initialize image_processor
					A__	:						Optional[int]								=				self.image_processing_class(**self.image_processor_dict		)
					# create random numpy tensors
					A__	:						List[str]								=				prepare_video_inputs(self.image_processor_tester					,							equal_resolution=UpperCamelCase__					,							numpify=UpperCamelCase__		)
					for video in video_inputs:
									self.assertIsInstance(UpperCamelCase__					,							UpperCamelCase__		)
									self.assertIsInstance(video[0]					,							np.ndarray		)

					# Test not batched input
					A__	:						Any								=				image_processor(video_inputs[0]					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        1,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						Tuple								=				image_processor(UpperCamelCase__					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)





	def 	__snake_case					(      self		):
					# Initialize image_processor
					A__	:						Any								=				self.image_processing_class(**self.image_processor_dict		)
					# create random PyTorch tensors
					A__	:						Optional[int]								=				prepare_video_inputs(self.image_processor_tester					,							equal_resolution=UpperCamelCase__					,							torchify=UpperCamelCase__		)
					for video in video_inputs:
									self.assertIsInstance(UpperCamelCase__					,							UpperCamelCase__		)
									self.assertIsInstance(video[0]					,							torch.Tensor		)

					# Test not batched input
					A__	:						int								=				image_processor(video_inputs[0]					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        1,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

					# Test batched
					A__	:						Union[str, Any]								=				image_processor(UpperCamelCase__					,							return_tensors='''pt'''		).pixel_values
					self.assertEqual(
					    encoded_videos.shape					,							(
					        self.image_processor_tester.batch_size,
					        self.image_processor_tester.num_frames,
					        self.image_processor_tester.num_channels,
					        self.image_processor_tester.crop_size['''height'''],
					        self.image_processor_tester.crop_size['''width'''],
					    )					,							)

import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
					import torch

					from transformers import GPTaLMHeadModel







@require_torch
@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	@slow
	@unittest.skip('''Model is not available.'''		)
	def 	__snake_case					(      self		):
					A__	:						int								=				'''nvidia/megatron-gpt2-345m/'''
					if "MYDIR" in os.environ:
									A__	:						Union[str, Any]								=				os.path.join(os.environ['''MYDIR''']					,							UpperCamelCase__		)
					A__	:						Union[str, Any]								=				GPTaLMHeadModel.from_pretrained(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.half()

					A__	:						Dict								=				torch.tensor(
					    [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]					,							device=UpperCamelCase__					,							dtype=torch.long					,							)

					with torch.no_grad():
									A__	:						str								=				model(UpperCamelCase__		).logits

					A__	:						int								=				torch.Size((1, 9, 5_0257)		)
					self.assertEqual(output.shape					,							UpperCamelCase__		)

					A__	:						Dict								=				torch.tensor(
					    [
					        4.9_4_1_4,
					        -0.2_9_2_0,
					        -1.2_1_4_8,
					        -4.0_2_7_3,
					        -0.5_1_6_1,
					        -5.2_1_0_9,
					        -1.2_4_1_2,
					        -1.8_3_0_1,
					        -1.7_7_3_4,
					        -4.7_1_4_8,
					        -0.2_3_1_7,
					        -1.0_8_1_1,
					        -2.1_7_7_7,
					        0.4_1_4_1,
					        -3.7_9_6_9,
					        -4.0_5_8_6,
					        -2.5_3_3_2,
					        -3.3_8_0_9,
					        4.3_8_6_7,
					    ]					,							device=UpperCamelCase__					,							dtype=torch.half					,							)

					for i in range(19		):
									A__							,				A__	:						int								=				8 * i // 17, 2792 * i  # along the diagonal
									A__							,				A__	:						Optional[Any]								=				output[0, r, c], expected_diag[i]
									A__	:						Dict								=				F"row={r} col={c} computed={computed} expected={expected}"
									self.assertAlmostEqual(UpperCamelCase__					,							UpperCamelCase__					,							delta=1e-4					,							msg=UpperCamelCase__		)