




'''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
)
a__         =     logging.getLogger(__name__)




def    snake_case__     (  )			->       Optional[int]:





			'''simple docstring'''






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

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

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

			snake_case__          =						corpus.get_iterator("""valid"""       ,      args.batch_size       ,      args.tgt_len       ,      device=_UpperCamelCase       ,      ext_len=args.ext_len	)
			snake_case__          =						corpus.get_iterator("""test"""       ,      args.batch_size       ,      args.tgt_len       ,      device=_UpperCamelCase       ,      ext_len=args.ext_len	)

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

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

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

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

			def format_log(a       ,      a	):
						snake_case__          =						"""| {0} loss {1:5.2f} | {0} ppl {2:9.3f} """.format(_UpperCamelCase       ,      _UpperCamelCase       ,      math.exp(_UpperCamelCase	)	)
						return log_str

			snake_case__          =						""""""
			if valid_loss is not None:
						log_str += format_log(_UpperCamelCase       ,      """valid"""	)
			if test_loss is not None:
						log_str += format_log(_UpperCamelCase       ,      """test"""	)

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


if __name__ == "__main__":
		main()



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


if is_torch_available():
       import torch

if is_vision_available():
       from PIL import Image

       from transformers import OwlViTImageProcessor







class 			UpperCAmelCase			(							unittest.TestCase):

 def __init__(			self			:						Optional[Any],       a_			:						Optional[int],       a_			:						Dict=7,       a_			:						List[str]=3,       a_			:						int=18,       a_			:						Tuple=30,       a_			:						Any=400,       a_			:						Optional[int]=True,       a_			:						Optional[int]=None,       a_			:						Any=True,       a_			:						Optional[int]=None,       a_			:						int=True,       a_			:						Optional[int]=[0.48_145_466, 0.4_578_275, 0.40_821_073],       a_			:						Optional[Any]=[0.26_862_954, 0.26_130_258, 0.27_577_711],       a_			:						Optional[int]=True,       ):





    """simple docstring"""



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

 def 		lowercase_    (			self			:						List[str]       ):





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







@require_torch
@require_vision
class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__		,      unittest.TestCase):
 _lowerCamelCase			:       Tuple            =       OwlViTImageProcessor if is_vision_available() else None

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    OwlViTImageProcessingTester(self       )

 @property
 def 		lowercase_    (			self			:						str       ):





    """simple docstring"""



    return self.image_processor_tester.prepare_image_processor_dict()

 def 		lowercase_    (			self			:						Tuple       ):





    """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 		lowercase_    (			self			:						Any       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.image_processing_class.from_dict(self.image_processor_dict       )
    self.assertEqual(image_processor.size,       {"height": 18, "width": 18}       )
    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,       {"height": 42, "width": 42}       )
    self.assertEqual(image_processor.crop_size,       {"height": 84, "width": 84}       )

 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.image_processing_class(**self.image_processor_dict       )
    # create random PIL images
    UpperCamelCase__			    =    prepare_image_inputs(self.image_processor_tester,       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 		lowercase_    (			self			:						Any       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.image_processing_class(**self.image_processor_dict       )
    # create random numpy tensors
    UpperCamelCase__			    =    prepare_image_inputs(self.image_processor_tester,       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 		lowercase_    (			self			:						Any       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.image_processing_class(**self.image_processor_dict       )
    # create random PyTorch tensors
    UpperCamelCase__			    =    prepare_image_inputs(self.image_processor_tester,       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"],
        ),       )