





"""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       _SCREAMING_SNAKE_CASE(     A						):




       @staticmethod
       def 					_UpperCamelCase  (		SCREAMING_SNAKE_CASE__							)    ->   Tuple:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :List[str]	       =						parser.add_parser('''login'''	,help='''Log in using the same credentials as on huggingface.co'''							)
             login_parser.set_defaults(func=lambda SCREAMING_SNAKE_CASE__							: LoginCommand(SCREAMING_SNAKE_CASE__							)							)
             __SCREAMING_SNAKE_CASE     :str	       =						parser.add_parser('''whoami'''	,help='''Find out which huggingface.co account you are logged in as.'''							)
             whoami_parser.set_defaults(func=lambda SCREAMING_SNAKE_CASE__							: WhoamiCommand(SCREAMING_SNAKE_CASE__							)							)
             __SCREAMING_SNAKE_CASE     :List[str]	       =						parser.add_parser('''logout'''	,help='''Log out'''							)
             logout_parser.set_defaults(func=lambda SCREAMING_SNAKE_CASE__							: LogoutCommand(SCREAMING_SNAKE_CASE__							)							)

             # new system: git-based repo system
             __SCREAMING_SNAKE_CASE     :Any	       =						parser.add_parser(
                 '''repo'''	,help='''Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.'''	,)
             __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						repo_parser.add_subparsers(
                 help='''Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands'''							)
             __SCREAMING_SNAKE_CASE     :Tuple	       =						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=SCREAMING_SNAKE_CASE__	,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=SCREAMING_SNAKE_CASE__	,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 SCREAMING_SNAKE_CASE__							: RepoCreateCommand(SCREAMING_SNAKE_CASE__							)							)






class       _SCREAMING_SNAKE_CASE:
       SCREAMING_SNAKE_CASE_     :   Any					=						'''\u001b[1m'''
       SCREAMING_SNAKE_CASE_     :   Tuple					=						'''\u001b[31m'''
       SCREAMING_SNAKE_CASE_     :   Optional[int]					=						'''\u001b[90m'''
       SCREAMING_SNAKE_CASE_     :   Any					=						'''\u001b[0m'''




       @classmethod
       def 					_UpperCamelCase  (		cls	,SCREAMING_SNAKE_CASE__							)    ->   Union[str, Any]:





             """simple docstring"""






             return f'''{cls._bold}{s}{cls._reset}'''




       @classmethod
       def 					_UpperCamelCase  (		cls	,SCREAMING_SNAKE_CASE__							)    ->   int:





             """simple docstring"""






             return f'''{cls._bold}{cls._red}{s}{cls._reset}'''




       @classmethod
       def 					_UpperCamelCase  (		cls	,SCREAMING_SNAKE_CASE__							)    ->   List[str]:





             """simple docstring"""






             return f'''{cls._gray}{s}{cls._reset}'''




def   __lowerCamelCase     (							a_  :	List[List[Union[str, int]]]				,     a_  :	List[str]							)    ->   str:
      __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						[max(len(str(a_							)							) for x in col							) for col in zip(*a_				,     a_							)]
      __SCREAMING_SNAKE_CASE     :Dict	       =						('''{{:{}}} ''' * len(a_							)).format(*a_							)
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						[]
      lines.append(row_format.format(*a_							)							)
      lines.append(row_format.format(*['''-''' * w for w in col_widths]							)							)
      for row in rows:
            lines.append(row_format.format(*a_							)							)
      return "\n".join(a_							)






class       _SCREAMING_SNAKE_CASE:




       def __init__(		self	,SCREAMING_SNAKE_CASE__							)    ->   Any:





             """simple docstring"""






             __SCREAMING_SNAKE_CASE     :Tuple	       =						args






class       _SCREAMING_SNAKE_CASE(     A						):




       def 					_UpperCamelCase  (		self							)    ->   Any:





             """simple docstring"""






             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       _SCREAMING_SNAKE_CASE(     A						):




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





             """simple docstring"""






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






class       _SCREAMING_SNAKE_CASE(     A						):




       def 					_UpperCamelCase  (		self							)    ->   Any:





             """simple docstring"""






             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       _SCREAMING_SNAKE_CASE(     A						):




       def 					_UpperCamelCase  (		self							)    ->   Any:





             """simple docstring"""






             print(
                 ANSI.red(
                     '''WARNING! Managing repositories through transformers-cli is deprecated. '''
                     '''Please use `huggingface-cli` instead.'''							)							)
             __SCREAMING_SNAKE_CASE     :Tuple	       =						HfFolder.get_token()
             if token is None:
                   print('''Not logged in'''							)
                   exit(1							)
             try:
                   __SCREAMING_SNAKE_CASE     :Optional[int]	       =						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:
                   __SCREAMING_SNAKE_CASE     :Any	       =						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(''''''							)

             __SCREAMING_SNAKE_CASE       ,__SCREAMING_SNAKE_CASE     :Tuple	       =						whoami(SCREAMING_SNAKE_CASE__							)
             __SCREAMING_SNAKE_CASE     :Any	       =						self.args.organization if self.args.organization is not None else user
             __SCREAMING_SNAKE_CASE     :Dict	       =						f'''{namespace}/{self.args.name}'''
             print(f'''You are about to create {ANSI.bold(SCREAMING_SNAKE_CASE__							)}'''							)

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






"""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_          =	logging.getLogger(__name__)



def   __lowerCamelCase     (							)    ->   Optional[Any]:
      __SCREAMING_SNAKE_CASE     :Union[str, Any]	       =						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=1_28				,     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=16_00				,     help='''length of the retained previous heads'''							)
      parser.add_argument('''--clamp_len'''				,     type=a_				,     default=10_00				,     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.'''							)
      __SCREAMING_SNAKE_CASE     :Optional[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=a_							)
            ptvsd.wait_for_attach()

      __SCREAMING_SNAKE_CASE     :Tuple	       =						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 )
      __SCREAMING_SNAKE_CASE     :Optional[int]	       =						TransfoXLCorpus.from_pretrained(args.model_name							)

      __SCREAMING_SNAKE_CASE     :Tuple	       =						corpus.get_iterator('''valid'''				,     args.batch_size				,     args.tgt_len				,     device=a_				,     ext_len=args.ext_len							)
      __SCREAMING_SNAKE_CASE     :Tuple	       =						corpus.get_iterator('''test'''				,     args.batch_size				,     args.tgt_len				,     device=a_				,     ext_len=args.ext_len							)

      # Load a pre-trained model
      __SCREAMING_SNAKE_CASE     :int	       =						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:
            __SCREAMING_SNAKE_CASE     :Dict	       =						args.clamp_len
      if args.same_length:
            __SCREAMING_SNAKE_CASE     :int	       =						True

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

      # Run on test data.
      if args.split == "all":
            __SCREAMING_SNAKE_CASE     :Optional[Any]	       =						evaluate(a_							)
            __SCREAMING_SNAKE_CASE     :Any	       =						evaluate(a_							)
      elif args.split == "valid":
            __SCREAMING_SNAKE_CASE     :Tuple	       =						evaluate(a_							)
            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						None
      elif args.split == "test":
            __SCREAMING_SNAKE_CASE     :Optional[int]	       =						evaluate(a_							)
            __SCREAMING_SNAKE_CASE     :List[Any]	       =						None

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

      __SCREAMING_SNAKE_CASE     :Tuple	       =						''''''
      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('''=''' * 1_00							)
      logger.info(a_							)
      logger.info('''=''' * 1_00							)


if __name__ == "__main__":
  main()