



'''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      lowerCamelCase__     (     unittest.TestCase				):






	'''simple docstring'''

	def __init__(  self		:  Union[str, Any]   ,		__A		:  Any   ,		__A		:  Tuple=7   ,		__A		:  Tuple=3   ,		__A		:  List[Any]=18   ,		__A		:  List[str]=30   ,		__A		:  Any=400   ,		__A		:  Optional[int]=True   ,		__A		:  List[str]=None   ,		__A		:  List[Any]=True   ,		__A		:  Union[str, Any]=None   ,		__A		:  Optional[int]=True   ,		__A		:  str=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]   ,		__A		:  Optional[Any]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]   ,		__A		:  Optional[Any]=True   ,		)     ->			Dict:




								'''simple docstring'''





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




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




								'''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      lowercase__							(  self		:  List[Any]   ,		__A		:  Optional[Any]=False   ,		__A		:  Tuple=False   ,		__A		:  str=False					)     ->			List[Any]:




								'''simple docstring'''





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

								if equal_resolution:
															lowerCAmelCase__  =   []
															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:
															lowerCAmelCase__  =   []
															for i in range(self.batch_size					):
																						lowerCAmelCase__  =   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
															lowerCAmelCase__  =   [Image.fromarray(np.moveaxis(_lowerCAmelCase   ,		0   ,		-1					)					) for x in image_inputs]

								if torchify:
															lowerCAmelCase__  =   [torch.from_numpy(_lowerCAmelCase					) for x in image_inputs]

								return image_inputs





@require_torch
@require_vision
class      lowerCamelCase__     (     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	CLIPImageProcessor if is_vision_available() else None




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




								'''simple docstring'''





								lowerCAmelCase__  =   CLIPImageProcessingTester(self					)




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




								'''simple docstring'''





								return self.image_processor_tester.prepare_image_processor_dict()




	def      lowercase__							(  self		:  Tuple					)     ->			Any:




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class(**self.image_processor_dict					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_resize"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""size"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_center_crop"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""center_crop"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_normalize"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""image_mean"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""image_std"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_convert_rgb"""					)					)




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




								'''simple docstring'''





								lowerCAmelCase__  =   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}					)

								lowerCAmelCase__  =   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      lowercase__							(  self		:  List[str]					)     ->			List[Any]:




								'''simple docstring'''





								pass




	def      lowercase__							(  self		:  List[str]					)     ->			int:




								'''simple docstring'''





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

								# Test not batched input
								lowerCAmelCase__  =   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
								lowerCAmelCase__  =   image_processing(_lowerCAmelCase   ,		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      lowercase__							(  self		:  Optional[int]					)     ->			Optional[int]:




								'''simple docstring'''





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

								# Test not batched input
								lowerCAmelCase__  =   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
								lowerCAmelCase__  =   image_processing(_lowerCAmelCase   ,		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      lowercase__							(  self		:  Dict					)     ->			Tuple:




								'''simple docstring'''





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

								# Test not batched input
								lowerCAmelCase__  =   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
								lowerCAmelCase__  =   image_processing(_lowerCAmelCase   ,		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      lowerCamelCase__     (     _A,     unittest.TestCase				):






	'''simple docstring'''

	A__		=	CLIPImageProcessor if is_vision_available() else None




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




								'''simple docstring'''





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




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




								'''simple docstring'''





								return self.image_processor_tester.prepare_image_processor_dict()




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




								'''simple docstring'''





								lowerCAmelCase__  =   self.image_processing_class(**self.image_processor_dict					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_resize"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""size"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_center_crop"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""center_crop"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_normalize"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""image_mean"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""image_std"""					)					)
								self.assertTrue(hasattr(_lowerCAmelCase   ,		"""do_convert_rgb"""					)					)




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




								'''simple docstring'''





								pass




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




								'''simple docstring'''





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

								# Test not batched input
								lowerCAmelCase__  =   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
								lowerCAmelCase__  =   image_processing(_lowerCAmelCase   ,		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"""],
								    )   ,		)











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
)
_UpperCAmelCase						:    Dict      =  logging.getLogger(__name__)



def    UpperCAmelCase__	(  ):
  lowercase     :int   						=    argparse.ArgumentParser(description="PyTorch Transformer Language Model" )
  parser.add_argument("--model_name",      type=lowerCamelCase,      default="transfo-xl-wt103",      help="pretrained model name" )
  parser.add_argument(
      "--split",      type=lowerCamelCase,      default="test",      choices=["all", "valid", "test"],      help="which split to evaluate" )
  parser.add_argument("--batch_size",      type=lowerCamelCase,      default=10,      help="batch size" )
  parser.add_argument("--tgt_len",      type=lowerCamelCase,      default=128,      help="number of tokens to predict" )
  parser.add_argument("--ext_len",      type=lowerCamelCase,      default=0,      help="length of the extended context" )
  parser.add_argument("--mem_len",      type=lowerCamelCase,      default=1600,      help="length of the retained previous heads" )
  parser.add_argument("--clamp_len",      type=lowerCamelCase,      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=lowerCamelCase,      required=lowerCamelCase,      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=lowerCamelCase,      default="",      help="Can be used for distant debugging." )
  parser.add_argument("--server_port",      type=lowerCamelCase,      default="",      help="Can be used for distant debugging." )
  lowercase     :Any   						=    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=lowerCamelCase )
    ptvsd.wait_for_attach()

  lowercase     :Any   						=    torch.device("cuda" if torch.cuda.is_available() and not args.no_cuda else "cpu" )
  logger.info("device: {}".format(lowerCamelCase ) )

  # 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 )
  lowercase     :Dict   						=    TransfoXLCorpus.from_pretrained(args.model_name )

  lowercase     :Optional[int]   						=    corpus.get_iterator("valid",      args.batch_size,      args.tgt_len,      device=lowerCamelCase,      ext_len=args.ext_len )
  lowercase     :Union[str, Any]   						=    corpus.get_iterator("test",      args.batch_size,      args.tgt_len,      device=lowerCamelCase,      ext_len=args.ext_len )

  # Load a pre-trained model
  lowercase     :List[Any]   						=    TransfoXLLMHeadModel.from_pretrained(args.model_name )
  model.to(lowerCamelCase )

  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:
    lowercase     :Tuple   						=    args.clamp_len
  if args.same_length:
    lowercase     :Tuple   						=    True

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

  # Run on test data.
  if args.split == "all":
    lowercase     :Union[str, Any]   						=    evaluate(lowerCamelCase )
    lowercase     :List[str]   						=    evaluate(lowerCamelCase )
  elif args.split == "valid":
    lowercase     :Optional[int]   						=    evaluate(lowerCamelCase )
    lowercase     :Dict   						=    None
  elif args.split == "test":
    lowercase     :int   						=    evaluate(lowerCamelCase )
    lowercase     :Optional[Any]   						=    None

  def format_log(lowerCamelCase,      lowerCamelCase ):
    lowercase     :List[str]   						=    "| {0} loss {1:5.2f} | {0} ppl {2:9.3f} ".format(lowerCamelCase,      lowerCamelCase,      math.exp(lowerCamelCase ) )
    return log_str

  lowercase     :Optional[int]   						=    ""
  if valid_loss is not None:
    log_str += format_log(lowerCamelCase,      "valid" )
  if test_loss is not None:
    log_str += format_log(lowerCamelCase,      "test" )

  logger.info("=" * 100 )
  logger.info(lowerCamelCase )
  logger.info("=" * 100 )


if __name__ == "__main__":
     main()



