



"""simple docstring"""






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, prepare_video_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import VivitImageProcessor

class        __SCREAMING_SNAKE_CASE	(      unittest.TestCase   ):


					'''simple docstring'''





					def __init__(       self							:  str,  lowerCamelCase							:  Union[str, Any],  lowerCamelCase							:  Optional[Any]=7,  lowerCamelCase							:  str=3,  lowerCamelCase							:  Any=10,  lowerCamelCase							:  Dict=18,  lowerCamelCase							:  int=30,  lowerCamelCase							:  Union[str, Any]=400,  lowerCamelCase							:  Optional[Any]=True,  lowerCamelCase							:  List[Any]=None,  lowerCamelCase							:  List[str]=True,  lowerCamelCase							:  List[str]=[0.5, 0.5, 0.5],  lowerCamelCase							:  int=[0.5, 0.5, 0.5],  lowerCamelCase							:  Tuple=None,  )->					Union[str, Any]:
								lowerCamelCase__		:		Union[str, Any]			=size if size is not None else {'''shortest_edge''': 18}
								lowerCamelCase__		:		Union[str, Any]			=crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

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




					def 		snake_case       (       self							:  Tuple     )->					Tuple:
								return {
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_normalize": self.do_normalize,
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "crop_size": self.crop_size,
								}





@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE	(      lowerCAmelCase_		,   unittest.TestCase   ):


					'''simple docstring'''





					_a   			=							VivitImageProcessor if is_vision_available() else None



					def 		snake_case       (       self							:  Tuple     )->					Tuple:
								lowerCamelCase__		:		str			=VivitImageProcessingTester(self     )



					@property
					def 		snake_case       (       self							:  Any     )->					Optional[int]:
								return self.image_processor_tester.prepare_image_processor_dict()



					def 		snake_case       (       self							:  str     )->					List[str]:
								lowerCamelCase__		:		List[str]			=self.image_processing_class(**self.image_processor_dict     )
								self.assertTrue(hasattr(lowerCamelCase,  '''image_mean'''     )     )
								self.assertTrue(hasattr(lowerCamelCase,  '''image_std'''     )     )
								self.assertTrue(hasattr(lowerCamelCase,  '''do_normalize'''     )     )
								self.assertTrue(hasattr(lowerCamelCase,  '''do_resize'''     )     )
								self.assertTrue(hasattr(lowerCamelCase,  '''do_center_crop'''     )     )
								self.assertTrue(hasattr(lowerCamelCase,  '''size'''     )     )



					def 		snake_case       (       self							:  Union[str, Any]     )->					Dict:
								lowerCamelCase__		:		Optional[Any]			=self.image_processing_class.from_dict(self.image_processor_dict     )
								self.assertEqual(image_processor.size,  {'''shortest_edge''': 18}     )
								self.assertEqual(image_processor.crop_size,  {'''height''': 18, '''width''': 18}     )

								lowerCamelCase__		:		List[Any]			=self.image_processing_class.from_dict(self.image_processor_dict,  size=42,  crop_size=84     )
								self.assertEqual(image_processor.size,  {'''shortest_edge''': 42}     )
								self.assertEqual(image_processor.crop_size,  {'''height''': 84, '''width''': 84}     )



					def 		snake_case       (       self							:  str     )->					int:
								# Initialize image_processing
								lowerCamelCase__		:		Any			=self.image_processing_class(**self.image_processor_dict     )
								# create random PIL videos
								lowerCamelCase__		:		Tuple			=prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCamelCase     )
								for video in video_inputs:
											self.assertIsInstance(lowerCamelCase,  lowerCamelCase     )
											self.assertIsInstance(video[0],  Image.Image     )

								# Test not batched input
								lowerCamelCase__		:		Optional[Any]			=image_processing(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
								lowerCamelCase__		:		Tuple			=image_processing(lowerCamelCase,  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							:  str     )->					Dict:
								# Initialize image_processing
								lowerCamelCase__		:		List[Any]			=self.image_processing_class(**self.image_processor_dict     )
								# create random numpy tensors
								lowerCamelCase__		:		Union[str, Any]			=prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCamelCase,  numpify=lowerCamelCase     )
								for video in video_inputs:
											self.assertIsInstance(lowerCamelCase,  lowerCamelCase     )
											self.assertIsInstance(video[0],  np.ndarray     )

								# Test not batched input
								lowerCamelCase__		:		Tuple			=image_processing(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
								lowerCamelCase__		:		str			=image_processing(lowerCamelCase,  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							:  Tuple     )->					Dict:
								# Initialize image_processing
								lowerCamelCase__		:		Optional[int]			=self.image_processing_class(**self.image_processor_dict     )
								# create random PyTorch tensors
								lowerCamelCase__		:		Any			=prepare_video_inputs(self.image_processor_tester,  equal_resolution=lowerCamelCase,  torchify=lowerCamelCase     )
								for video in video_inputs:
											self.assertIsInstance(lowerCamelCase,  lowerCamelCase     )
											self.assertIsInstance(video[0],  torch.Tensor     )

								# Test not batched input
								lowerCamelCase__		:		Any			=image_processing(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
								lowerCamelCase__		:		List[Any]			=image_processing(lowerCamelCase,  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'''],
								    ),  )









"""simple docstring"""






import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()






def      snake_case__   (		__lowerCamelCase				:    List[Any]							,		__lowerCamelCase				:    List[str]							,		__lowerCamelCase				:    List[Any]	):
			"""simple docstring"""
			# Construct model
			if gpta_config_file == "":
						lowerCamelCase__		:		Dict			=GPTaConfig()
			else:
						lowerCamelCase__		:		Tuple			=GPTaConfig.from_json_file(__lowerCamelCase	)
			lowerCamelCase__		:		Optional[Any]			=GPTaModel(__lowerCamelCase	)

			# Load weights from numpy
			load_tf_weights_in_gpta(__lowerCamelCase							,		__lowerCamelCase							,		__lowerCamelCase	)

			# Save pytorch-model
			lowerCamelCase__		:		List[str]			=pytorch_dump_folder_path + '''/''' + WEIGHTS_NAME
			lowerCamelCase__		:		int			=pytorch_dump_folder_path + '''/''' + CONFIG_NAME
			print(f'''Save PyTorch model to {pytorch_weights_dump_path}'''	)
			torch.save(model.state_dict()							,		__lowerCamelCase	)
			print(f'''Save configuration file to {pytorch_config_dump_path}'''	)
			with open(__lowerCamelCase							,		'''w'''							,		encoding='''utf-8'''	) as f:
						f.write(config.to_json_string()	)


if __name__ == "__main__":
		_lowercase : Optional[Any]      						=						argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    "--gpt2_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
		)
		parser.add_argument(
		    "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
		)
		parser.add_argument(
		    "--gpt2_config_file",
		    default="",
		    type=str,
		    help=(
		        "An optional config json file corresponding to the pre-trained OpenAI model. \n"
		        "This specifies the model architecture."
		    ),
		)
		_lowercase : Any      						=						parser.parse_args()
		convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)




