
'''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


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import CLIPImageProcessor


class        SCREAMING_SNAKE_CASE__	(			unittest.TestCase):







				def __init__(    self		,							A_		,							A_=7		,							A_=3		,							A_=18		,							A_=30		,							A_=400		,							A_=True		,							A_=None		,							A_=True		,							A_=None		,							A_=True		,							A_=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,							A_=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,							A_=True		,							)->						List[Any]:







											'''simple docstring'''






											UpperCamelCase					       =			size if size is not None else {'shortest_edge': 20}
											UpperCamelCase					       =			crop_size if crop_size is not None else {'height': 18, 'width': 18}
											UpperCamelCase					       =			parent
											UpperCamelCase					       =			batch_size
											UpperCamelCase					       =			num_channels
											UpperCamelCase					       =			image_size
											UpperCamelCase					       =			min_resolution
											UpperCamelCase					       =			max_resolution
											UpperCamelCase					       =			do_resize
											UpperCamelCase					       =			size
											UpperCamelCase					       =			do_center_crop
											UpperCamelCase					       =			crop_size
											UpperCamelCase					       =			do_normalize
											UpperCamelCase					       =			image_mean
											UpperCamelCase					       =			image_std
											UpperCamelCase					       =			do_convert_rgb







				def 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






											return {
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "do_center_crop": self.do_center_crop,
											    "crop_size": self.crop_size,
											    "do_normalize": self.do_normalize,
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_convert_rgb": self.do_convert_rgb,
											}







				def 		UpperCAmelCase_  (    self		,							A_=False		,							A_=False		,							A_=False			)->						Optional[int]:







											'''simple docstring'''






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

											if equal_resolution:
																		UpperCamelCase					       =			[]
																		for i in range(self.batch_size			):
																									image_inputs.append(
																									    np.random.randint(
																									        255		,							size=(self.num_channels, self.max_resolution, self.max_resolution)		,							dtype=np.uinta			)			)
											else:
																		UpperCamelCase					       =			[]
																		for i in range(self.batch_size			):
																									UpperCamelCase       ,     UpperCamelCase					       =			np.random.choice(np.arange(self.min_resolution		,							self.max_resolution			)		,							2			)
																									image_inputs.append(np.random.randint(255		,							size=(self.num_channels, width, height)		,							dtype=np.uinta			)			)

											if not numpify and not torchify:
																		# PIL expects the channel dimension as last dimension
																		UpperCamelCase					       =			[Image.fromarray(np.moveaxis(A_		,							0		,							-1			)			) for x in image_inputs]

											if torchify:
																		UpperCamelCase					       =			[torch.from_numpy(A_			) for x in image_inputs]

											return image_inputs


@require_torch
@require_vision
class        SCREAMING_SNAKE_CASE__	(			snake_case_				,    unittest.TestCase):
				lowerCAmelCase_			       =				CLIPImageProcessor if is_vision_available() else None







				def 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






											UpperCamelCase					       =			CLIPImageProcessingTester(self			)







				@property
				def 		UpperCAmelCase_  (    self			)->						List[str]:







											'''simple docstring'''






											return self.image_processor_tester.prepare_image_processor_dict()







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											self.assertTrue(hasattr(A_		,							'do_resize'			)			)
											self.assertTrue(hasattr(A_		,							'size'			)			)
											self.assertTrue(hasattr(A_		,							'do_center_crop'			)			)
											self.assertTrue(hasattr(A_		,							'center_crop'			)			)
											self.assertTrue(hasattr(A_		,							'do_normalize'			)			)
											self.assertTrue(hasattr(A_		,							'image_mean'			)			)
											self.assertTrue(hasattr(A_		,							'image_std'			)			)
											self.assertTrue(hasattr(A_		,							'do_convert_rgb'			)			)







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class.from_dict(self.image_processor_dict			)
											self.assertEqual(image_processor.size		,							{'shortest_edge': 20}			)
											self.assertEqual(image_processor.crop_size		,							{'height': 18, 'width': 18}			)

											UpperCamelCase					       =			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 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






											pass







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											# create random PIL images
											UpperCamelCase					       =			self.image_processor_tester.prepare_inputs(equal_resolution=A_			)
											for image in image_inputs:
																		self.assertIsInstance(A_		,							Image.Image			)

											# Test not batched input
											UpperCamelCase					       =			image_processing(image_inputs[0]		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)

											# Test batched
											UpperCamelCase					       =			image_processing(A_		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											# create random numpy tensors
											UpperCamelCase					       =			self.image_processor_tester.prepare_inputs(equal_resolution=A_		,							numpify=A_			)
											for image in image_inputs:
																		self.assertIsInstance(A_		,							np.ndarray			)

											# Test not batched input
											UpperCamelCase					       =			image_processing(image_inputs[0]		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)

											# Test batched
											UpperCamelCase					       =			image_processing(A_		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											# create random PyTorch tensors
											UpperCamelCase					       =			self.image_processor_tester.prepare_inputs(equal_resolution=A_		,							torchify=A_			)
											for image in image_inputs:
																		self.assertIsInstance(A_		,							torch.Tensor			)

											# Test not batched input
											UpperCamelCase					       =			image_processing(image_inputs[0]		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)

											# Test batched
											UpperCamelCase					       =			image_processing(A_		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)


@require_torch
@require_vision
class        SCREAMING_SNAKE_CASE__	(			snake_case_				,    unittest.TestCase):
				lowerCAmelCase_			       =				CLIPImageProcessor if is_vision_available() else None







				def 		UpperCAmelCase_  (    self			)->						int:







											'''simple docstring'''






											UpperCamelCase					       =			CLIPImageProcessingTester(self		,							num_channels=4			)
											UpperCamelCase					       =			3







				@property
				def 		UpperCAmelCase_  (    self			)->						Tuple:







											'''simple docstring'''






											return self.image_processor_tester.prepare_image_processor_dict()







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







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											self.assertTrue(hasattr(A_		,							'do_resize'			)			)
											self.assertTrue(hasattr(A_		,							'size'			)			)
											self.assertTrue(hasattr(A_		,							'do_center_crop'			)			)
											self.assertTrue(hasattr(A_		,							'center_crop'			)			)
											self.assertTrue(hasattr(A_		,							'do_normalize'			)			)
											self.assertTrue(hasattr(A_		,							'image_mean'			)			)
											self.assertTrue(hasattr(A_		,							'image_std'			)			)
											self.assertTrue(hasattr(A_		,							'do_convert_rgb'			)			)







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







											'''simple docstring'''






											pass







				def 		UpperCAmelCase_  (    self			)->						Dict:







											'''simple docstring'''






											UpperCamelCase					       =			self.image_processing_class(**self.image_processor_dict			)
											# create random PIL images
											UpperCamelCase					       =			self.image_processor_tester.prepare_inputs(equal_resolution=A_			)
											for image in image_inputs:
																		self.assertIsInstance(A_		,							Image.Image			)

											# Test not batched input
											UpperCamelCase					       =			image_processing(image_inputs[0]		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        1,
											        self.expected_encoded_image_num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)

											# Test batched
											UpperCamelCase					       =			image_processing(A_		,							return_tensors='pt'			).pixel_values
											self.assertEqual(
											    encoded_images.shape		,							(
											        self.image_processor_tester.batch_size,
											        self.expected_encoded_image_num_channels,
											        self.image_processor_tester.crop_size['height'],
											        self.image_processor_tester.crop_size['width'],
											    )		,							)






'''simple docstring'''
import argparse
import logging
import math
import time

import torch

from transformers import TransfoXLCorpus, TransfoXLLMHeadModel


logging.basicConfig(
    format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S', level=logging.INFO
)
lowerCAmelCase :  Optional[Any]      =      logging.getLogger(__name__)







def 						A_(			):
							UpperCamelCase					       =			argparse.ArgumentParser(description='PyTorch Transformer Language Model')
							parser.add_argument('--model_name'						,			type=A						,			default='transfo-xl-wt103'						,			help='pretrained model name')
							parser.add_argument(
							    '--split'						,			type=A						,			default='test'						,			choices=['all', 'valid', 'test']						,			help='which split to evaluate')
							parser.add_argument('--batch_size'						,			type=A						,			default=10						,			help='batch size')
							parser.add_argument('--tgt_len'						,			type=A						,			default=128						,			help='number of tokens to predict')
							parser.add_argument('--ext_len'						,			type=A						,			default=0						,			help='length of the extended context')
							parser.add_argument('--mem_len'						,			type=A						,			default=1600						,			help='length of the retained previous heads')
							parser.add_argument('--clamp_len'						,			type=A						,			default=1000						,			help='max positional embedding index')
							parser.add_argument('--no_cuda'						,			action='store_true'						,			help='Do not use CUDA even though CUA is available')
							parser.add_argument('--work_dir'						,			type=A						,			required=A						,			help='path to the work_dir')
							parser.add_argument('--no_log'						,			action='store_true'						,			help='do not log the eval result')
							parser.add_argument('--same_length'						,			action='store_true'						,			help='set same length attention with masking')
							parser.add_argument('--server_ip'						,			type=A						,			default=''						,			help='Can be used for distant debugging.')
							parser.add_argument('--server_port'						,			type=A						,			default=''						,			help='Can be used for distant debugging.')
							UpperCamelCase					       =			parser.parse_args()
							assert args.ext_len >= 0, "extended context length must be non-negative"

							if args.server_ip and args.server_port:
														# Distant debugging - see https://code.visualstudio.com/docs/python/debugging#_attach-to-a-local-script
														import ptvsd

														print('Waiting for debugger attach')
														ptvsd.enable_attach(address=(args.server_ip, args.server_port)						,			redirect_output=A)
														ptvsd.wait_for_attach()

							UpperCamelCase					       =			torch.device('cuda' if torch.cuda.is_available() and not args.no_cuda else 'cpu')
							logger.info('device: {}'.format(A))

							# Load a pre-processed dataset
							# You can also build the corpus yourself using TransfoXLCorpus methods
							# The pre-processing involve computing word frequencies to prepare the Adaptive input and SoftMax
							# and tokenizing the dataset
							# The pre-processed corpus is a convertion (using the conversion script )
							UpperCamelCase					       =			TransfoXLCorpus.from_pretrained(args.model_name)

							UpperCamelCase					       =			corpus.get_iterator('valid'						,			args.batch_size						,			args.tgt_len						,			device=A						,			ext_len=args.ext_len)
							UpperCamelCase					       =			corpus.get_iterator('test'						,			args.batch_size						,			args.tgt_len						,			device=A						,			ext_len=args.ext_len)

							# Load a pre-trained model
							UpperCamelCase					       =			TransfoXLLMHeadModel.from_pretrained(args.model_name)
							model.to(A)

							logger.info(
							    'Evaluating with bsz {} tgt_len {} ext_len {} mem_len {} clamp_len {}'.format(
							        args.batch_size						,			args.tgt_len						,			args.ext_len						,			args.mem_len						,			args.clamp_len))

							model.reset_memory_length(args.mem_len)
							if args.clamp_len > 0:
														UpperCamelCase					       =			args.clamp_len
							if args.same_length:
														UpperCamelCase					       =			True

							###############################################################################
							# Evaluation code
							###############################################################################
							def evaluate(A		:						Dict):
														# Turn on evaluation mode which disables dropout.
														model.eval()
														UpperCamelCase       ,     UpperCamelCase					       =			0, 0.0
														UpperCamelCase					       =			time.time()
														with torch.no_grad():
																					UpperCamelCase					       =			None
																					for idx, (data, target, seq_len) in enumerate(A):
																												UpperCamelCase					       =			model(A						,			lm_labels=A						,			mems=A)
																												UpperCamelCase       ,     UpperCamelCase       ,     UpperCamelCase					       =			ret
																												UpperCamelCase					       =			loss.mean()
																												total_loss += seq_len * loss.item()
																												total_len += seq_len
																					UpperCamelCase					       =			time.time() - start_time
														logger.info('Time : {:.2f}s, {:.2f}ms/segment'.format(A						,			1000 * total_time / (idx + 1)))
														return total_loss / total_len

							# Run on test data.
							if args.split == "all":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			evaluate(A)
							elif args.split == "valid":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			None
							elif args.split == "test":
														UpperCamelCase					       =			evaluate(A)
														UpperCamelCase					       =			None

							def format_log(A		:						Any						,			A		:						Optional[Any]):
														UpperCamelCase					       =			'| {0} loss {1:5.2f} | {0} ppl {2:9.3f} '.format(A						,			A						,			math.exp(A))
														return log_str

							UpperCamelCase					       =			''
							if valid_loss is not None:
														log_str += format_log(A						,			'valid')
							if test_loss is not None:
														log_str += format_log(A						,			'test')

							logger.info('=' * 100)
							logger.info(A)
							logger.info('=' * 100)


if __name__ == "__main__":
						main()




