


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					:	int          =				logging.getLogger(__name__)
def   snake_case_    (			):
   UpperCAmelCase_				:   Optional[int]							= argparse.ArgumentParser(description='''PyTorch Transformer Language Model'''							)
   parser.add_argument('''--model_name'''      ,							type=__snake_case      ,							default='''transfo-xl-wt103'''      ,							help='''pretrained model name'''							)
   parser.add_argument(
       '''--split'''      ,							type=__snake_case      ,							default='''test'''      ,							choices=['''all''', '''valid''', '''test''']      ,							help='''which split to evaluate'''							)
   parser.add_argument('''--batch_size'''      ,							type=__snake_case      ,							default=1_0      ,							help='''batch size'''							)
   parser.add_argument('''--tgt_len'''      ,							type=__snake_case      ,							default=1_2_8      ,							help='''number of tokens to predict'''							)
   parser.add_argument('''--ext_len'''      ,							type=__snake_case      ,							default=0      ,							help='''length of the extended context'''							)
   parser.add_argument('''--mem_len'''      ,							type=__snake_case      ,							default=1_6_0_0      ,							help='''length of the retained previous heads'''							)
   parser.add_argument('''--clamp_len'''      ,							type=__snake_case      ,							default=1_0_0_0      ,							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=__snake_case      ,							required=__snake_case      ,							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=__snake_case      ,							default=''''''      ,							help='''Can be used for distant debugging.'''							)
   parser.add_argument('''--server_port'''      ,							type=__snake_case      ,							default=''''''      ,							help='''Can be used for distant debugging.'''							)
   UpperCAmelCase_				:   str							= 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=__snake_case							)
      ptvsd.wait_for_attach()

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

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

   UpperCAmelCase_				:   Dict							= corpus.get_iterator('''valid'''      ,							args.batch_size      ,							args.tgt_len      ,							device=__snake_case      ,							ext_len=args.ext_len							)
   UpperCAmelCase_				:   Optional[int]							= corpus.get_iterator('''test'''      ,							args.batch_size      ,							args.tgt_len      ,							device=__snake_case      ,							ext_len=args.ext_len							)

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

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

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

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

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

   UpperCAmelCase_				:   Optional[Any]							= ''''''
   if valid_loss is not None:
      log_str += format_log(__snake_case      ,							'''valid'''							)
   if test_loss is not None:
      log_str += format_log(__snake_case      ,							'''test'''							)

   logger.info('''=''' * 1_0_0							)
   logger.info(__snake_case							)
   logger.info('''=''' * 1_0_0							)


if __name__ == "__main__":
       main()







"""simple docstring"""



import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand

class 			a	(  a_      ):







    @staticmethod
    def      UpperCamelCase_ (     _lowerCamelCase			):
      lowercase				       =			parser.add_parser('login'				,						help='Log in using the same credentials as on huggingface.co'			)
      login_parser.set_defaults(func=lambda _lowerCamelCase			: LoginCommand(_lowerCamelCase			)			)
      lowercase				       =			parser.add_parser('whoami'				,						help='Find out which huggingface.co account you are logged in as.'			)
      whoami_parser.set_defaults(func=lambda _lowerCamelCase			: WhoamiCommand(_lowerCamelCase			)			)
      lowercase				       =			parser.add_parser('logout'				,						help='Log out'			)
      logout_parser.set_defaults(func=lambda _lowerCamelCase			: LogoutCommand(_lowerCamelCase			)			)

      # new system: git-based repo system
      lowercase				       =			parser.add_parser(
          'repo'				,						help='Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.'				,						)
      lowercase				       =			repo_parser.add_subparsers(
          help='Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands'			)
      lowercase				       =			repo_subparsers.add_parser(
          'create'				,						help='Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co'			)
      repo_create_parser.add_argument(
          'name'				,						type=_lowerCamelCase				,						help='Name for your model\'s repo. Will be namespaced under your username to build the model id.'				,						)
      repo_create_parser.add_argument('--organization'				,						type=_lowerCamelCase				,						help='Optional: organization namespace.'			)
      repo_create_parser.add_argument('-y'				,						'--yes'				,						action='store_true'				,						help='Optional: answer Yes to the prompt'			)
      repo_create_parser.set_defaults(func=lambda _lowerCamelCase			: RepoCreateCommand(_lowerCamelCase			)			)


class 			a	:
    UpperCAmelCase_					:	Union[str, Any]      	="\u001b[1m"
    UpperCAmelCase_					:	Optional[Any]      	="\u001b[31m"
    UpperCAmelCase_					:	List[str]      	="\u001b[90m"
    UpperCAmelCase_					:	str      	="\u001b[0m"







    @classmethod
    def      UpperCamelCase_ (     cls				,						_lowerCamelCase			):
      return F'{cls._bold}{s}{cls._reset}'







    @classmethod
    def      UpperCamelCase_ (     cls				,						_lowerCamelCase			):
      return F'{cls._bold}{cls._red}{s}{cls._reset}'







    @classmethod
    def      UpperCamelCase_ (     cls				,						_lowerCamelCase			):
      return F'{cls._gray}{s}{cls._reset}'


def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    List[List[Union[str, int]]]						,   __snake_case     :    List[str]       ):





  '''simple docstring'''





  lowercase				       =			[max(len(str(__snake_case       )       ) for x in col       ) for col in zip(*__snake_case						,   __snake_case       )]
  lowercase				       =			('{{:{}}} ' * len(__snake_case       )).format(*__snake_case       )
  lowercase				       =			[]
  lines.append(row_format.format(*__snake_case       )       )
  lines.append(row_format.format(*['-' * w for w in col_widths]       )       )
  for row in rows:
    lines.append(row_format.format(*__snake_case       )       )
  return "\n".join(__snake_case       )

class 			a	:







    def __init__(     self				,						_lowerCamelCase			):
      lowercase				       =			args


class 			a	(  a_      ):







    def      UpperCamelCase_ (     self			):
      print(
          ANSI.red(
              'ERROR! `huggingface-cli login` uses an outdated login mechanism '
              'that is not compatible with the Hugging Face Hub backend anymore. '
              'Please use `huggingface-cli login instead.'			)			)


class 			a	(  a_      ):







    def      UpperCamelCase_ (     self			):
      print(
          ANSI.red(
              'WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use '
              '`huggingface-cli whoami` instead.'			)			)
      lowercase				       =			HfFolder.get_token()
      if token is None:
        print('Not logged in'			)
        exit()
      try:
        lowercase       ,	lowercase				       =			whoami(_lowerCamelCase			)
        print(_lowerCamelCase			)
        if orgs:
          print(ANSI.bold('orgs: '			)				,						','.join(_lowerCamelCase			)			)
      except HTTPError as e:
        print(_lowerCamelCase			)
        print(ANSI.red(e.response.text			)			)
        exit(1			)


class 			a	(  a_      ):







    def      UpperCamelCase_ (     self			):
      print(
          ANSI.red(
              'ERROR! `transformers-cli logout` uses an outdated logout mechanism '
              'that is not compatible with the Hugging Face Hub backend anymore. '
              'Please use `huggingface-cli logout instead.'			)			)


class 			a	(  a_      ):







    def      UpperCamelCase_ (     self			):
      print(
          ANSI.red(
              'WARNING! Managing repositories through transformers-cli is deprecated. '
              'Please use `huggingface-cli` instead.'			)			)
      lowercase				       =			HfFolder.get_token()
      if token is None:
        print('Not logged in'			)
        exit(1			)
      try:
        lowercase				       =			subprocess.check_output(['git', '--version']			).decode('utf-8'			)
        print(ANSI.gray(stdout.strip()			)			)
      except FileNotFoundError:
        print('Looks like you do not have git installed, please install.'			)

      try:
        lowercase				       =			subprocess.check_output(['git-lfs', '--version']			).decode('utf-8'			)
        print(ANSI.gray(stdout.strip()			)			)
      except FileNotFoundError:
        print(
            ANSI.red(
                'Looks like you do not have git-lfs installed, please install.'
                ' You can install from https://git-lfs.github.com/.'
                ' Then run `git lfs install` (you only have to do this once).'			)			)
      print(''			)

      lowercase       ,	lowercase				       =			whoami(_lowerCamelCase			)
      lowercase				       =			self.args.organization if self.args.organization is not None else user
      lowercase				       =			F'{namespace}/{self.args.name}'
      print(F'You are about to create {ANSI.bold(_lowerCamelCase			)}'			)

      if not self.args.yes:
        lowercase				       =			input('Proceed? [Y/n] '			).lower()
        if not (choice == "" or choice == "y" or choice == "yes"):
          print('Abort'			)
          exit()
      try:
        lowercase				       =			create_repo(_lowerCamelCase				,						name=self.args.name				,						organization=self.args.organization			)
      except HTTPError as e:
        print(_lowerCamelCase			)
        print(ANSI.red(e.response.text			)			)
        exit(1			)
      print('\nYour repo now lives at:'			)
      print(F'  {ANSI.bold(_lowerCamelCase			)}'			)
      print('\nYou can clone it locally with the command below, and commit/push as usual.'			)
      print(F'\n  git clone {url}'			)
      print(''			)


