'''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__( lowerCAmelCase__     ):


			'''simple docstring'''






			@staticmethod
			def 							a_	(       __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  parser.add_parser("""login"""	,   help="""Log in using the same credentials as on huggingface.co""")
					login_parser.set_defaults(func=lambda __lowerCAmelCase: LoginCommand(__lowerCAmelCase))
					lowerCAmelCase        =  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))
					lowerCAmelCase        =  parser.add_parser("""logout"""	,   help="""Log out""")
					logout_parser.set_defaults(func=lambda __lowerCAmelCase: LogoutCommand(__lowerCAmelCase))

					# new system: git-based repo system
					lowerCAmelCase        =  parser.add_parser(
					    """repo"""	,   help="""Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."""	,   )
					lowerCAmelCase        =  repo_parser.add_subparsers(
					    help="""Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands""")
					lowerCAmelCase        =  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__:


			'''simple docstring'''






			UpperCAmelCase_      :					str								=	'''\u001b[1m'''
			UpperCAmelCase_      :					Tuple								=	'''\u001b[31m'''
			UpperCAmelCase_      :					List[Any]								=	'''\u001b[90m'''
			UpperCAmelCase_      :					str								=	'''\u001b[0m'''



			@classmethod
			def 							a_	(       cls	,   __lowerCAmelCase):




					"""simple docstring"""


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



			@classmethod
			def 							a_	(       cls	,   __lowerCAmelCase):




					"""simple docstring"""


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







			@classmethod
			def 							a_	(       cls	,   __lowerCAmelCase):




					"""simple docstring"""


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







def     snake_case__							(      _A: List[List[Union[str, int]]]				,       _A: List[str]			)   ->     str:

		'''simple docstring'''


		lowerCAmelCase        =  [max(len(str(_A			)			) for x in col			) for col in zip(*_A				,       _A			)]
		lowerCAmelCase        =  ("""{{:{}}} """ * len(_A			)).format(*_A			)
		lowerCAmelCase        =  []
		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     a__:


			'''simple docstring'''






			def __init__(       self	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  args







class     a__( lowerCAmelCase__     ):


			'''simple docstring'''






			def 							a_	(       self):




					"""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     a__( lowerCAmelCase__     ):


			'''simple docstring'''






			def 							a_	(       self):




					"""simple docstring"""


					print(
					    ANSI.red(
					        """WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use """
					        """`huggingface-cli whoami` instead."""))
					lowerCAmelCase        =  HfFolder.get_token()
					if token is None:
							print("""Not logged in""")
							exit()
					try:
							lowerCAmelCase ,					lowerCAmelCase        =  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__( lowerCAmelCase__     ):


			'''simple docstring'''






			def 							a_	(       self):




					"""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     a__( lowerCAmelCase__     ):


			'''simple docstring'''






			def 							a_	(       self):




					"""simple docstring"""


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

					lowerCAmelCase ,					lowerCAmelCase        =  whoami(__lowerCAmelCase)
					lowerCAmelCase        =  self.args.organization if self.args.organization is not None else user
					lowerCAmelCase        =  f"{namespace}/{self.args.name}"
					print(f"You are about to create {ANSI.bold(__lowerCAmelCase)}")

					if not self.args.yes:
							lowerCAmelCase        =  input("""Proceed? [Y/n] """).lower()
							if not (choice == "" or choice == "y" or choice == "yes"):
									print("""Abort""")
									exit()
					try:
							lowerCAmelCase        =  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("""""")





'''simple docstring'''




import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaTokenizer, GPTNeoConfig, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.modeling_flax_pytorch_utils import (
						    convert_pytorch_state_dict_to_flax,
						    load_flax_weights_in_pytorch_model,
						)
						from transformers.models.gpt_neo.modeling_flax_gpt_neo import FlaxGPTNeoForCausalLM, FlaxGPTNeoModel

if is_torch_available():
						import torch






class     a__:


			'''simple docstring'''






			def __init__(       self	,   __lowerCAmelCase	,   __lowerCAmelCase=14	,   __lowerCAmelCase=7	,   __lowerCAmelCase=True	,   __lowerCAmelCase=True	,   __lowerCAmelCase=False	,   __lowerCAmelCase=True	,   __lowerCAmelCase=99	,   __lowerCAmelCase=32	,   __lowerCAmelCase=4	,   __lowerCAmelCase=4	,   __lowerCAmelCase=[[["global", "local"], 2]]	,   __lowerCAmelCase=37	,   __lowerCAmelCase="gelu"	,   __lowerCAmelCase=0.1	,   __lowerCAmelCase=0.1	,   __lowerCAmelCase=512	,   __lowerCAmelCase=7	,   __lowerCAmelCase=0.02	,   ):




					"""simple docstring"""


					lowerCAmelCase        =  parent
					lowerCAmelCase        =  batch_size
					lowerCAmelCase        =  seq_length
					lowerCAmelCase        =  is_training
					lowerCAmelCase        =  use_input_mask
					lowerCAmelCase        =  use_token_type_ids
					lowerCAmelCase        =  use_labels
					lowerCAmelCase        =  vocab_size
					lowerCAmelCase        =  hidden_size
					lowerCAmelCase        =  num_hidden_layers
					lowerCAmelCase        =  num_attention_heads
					lowerCAmelCase        =  attention_types
					lowerCAmelCase        =  intermediate_size
					lowerCAmelCase        =  hidden_act
					lowerCAmelCase        =  hidden_dropout_prob
					lowerCAmelCase        =  attention_probs_dropout_prob
					lowerCAmelCase        =  max_position_embeddings
					lowerCAmelCase        =  window_size
					lowerCAmelCase        =  initializer_range
					lowerCAmelCase        =  None
					lowerCAmelCase        =  vocab_size - 1
					lowerCAmelCase        =  vocab_size - 1
					lowerCAmelCase        =  vocab_size - 1



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size)

					lowerCAmelCase        =  None
					if self.use_input_mask:
							lowerCAmelCase        =  random_attention_mask([self.batch_size, self.seq_length])

					lowerCAmelCase        =  GPTNeoConfig(
					    vocab_size=self.vocab_size	,   hidden_size=self.hidden_size	,   num_layers=self.num_hidden_layers	,   num_heads=self.num_attention_heads	,   max_position_embeddings=self.max_position_embeddings	,   use_cache=__lowerCAmelCase	,   bos_token_id=self.bos_token_id	,   eos_token_id=self.eos_token_id	,   pad_token_id=self.pad_token_id	,   window_size=self.window_size	,   attention_types=self.attention_types	,   )

					return (config, input_ids, input_mask)



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  self.prepare_config_and_inputs()
					lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  config_and_inputs
					lowerCAmelCase        =  {"""input_ids""": input_ids, """attention_mask""": attention_mask}
					return config, inputs_dict



			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  20
					lowerCAmelCase        =  model_class_name(__lowerCAmelCase)

					lowerCAmelCase        =  model.init_cache(input_ids.shape[0]	,   __lowerCAmelCase)
					lowerCAmelCase        =  jnp.ones((input_ids.shape[0], max_decoder_length)	,   dtype="""i4""")

					lowerCAmelCase        =  jnp.broadcast_to(
					    jnp.arange(input_ids.shape[-1] - 1)[None, :]	,   (input_ids.shape[0], input_ids.shape[-1] - 1))
					lowerCAmelCase        =  model(
					    input_ids[:, :-1]	,   attention_mask=__lowerCAmelCase	,   past_key_values=__lowerCAmelCase	,   position_ids=__lowerCAmelCase	,   )

					lowerCAmelCase        =  jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,   dtype="""i4""")
					lowerCAmelCase        =  model(
					    input_ids[:, -1:]	,   attention_mask=__lowerCAmelCase	,   past_key_values=outputs_cache.past_key_values	,   position_ids=__lowerCAmelCase	,   )

					lowerCAmelCase        =  model(__lowerCAmelCase)

					lowerCAmelCase        =  np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
					self.parent.assertTrue(diff < 1E-3	,   msg=f"Max diff is {diff}")







			def 							a_	(       self	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase        =  20
					lowerCAmelCase        =  model_class_name(__lowerCAmelCase)

					lowerCAmelCase        =  jnp.concatenate(
					    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]))]	,   axis=-1	,   )

					lowerCAmelCase        =  model.init_cache(input_ids.shape[0]	,   __lowerCAmelCase)
					lowerCAmelCase        =  jnp.broadcast_to(
					    jnp.arange(input_ids.shape[-1] - 1)[None, :]	,   (input_ids.shape[0], input_ids.shape[-1] - 1))

					lowerCAmelCase        =  model(
					    input_ids[:, :-1]	,   attention_mask=__lowerCAmelCase	,   past_key_values=__lowerCAmelCase	,   position_ids=__lowerCAmelCase	,   )
					lowerCAmelCase        =  jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]	,   dtype="""i4""")
					lowerCAmelCase        =  model(
					    input_ids[:, -1:]	,   past_key_values=outputs_cache.past_key_values	,   attention_mask=__lowerCAmelCase	,   position_ids=__lowerCAmelCase	,   )

					lowerCAmelCase        =  model(__lowerCAmelCase	,   attention_mask=__lowerCAmelCase)

					lowerCAmelCase        =  np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
					self.parent.assertTrue(diff < 1E-3	,   msg=f"Max diff is {diff}")







@require_flax
class     a__( lowerCAmelCase__			,						lowerCAmelCase__			,						unittest.TestCase     ):


			'''simple docstring'''






			UpperCAmelCase_      :					str								=	(FlaxGPTNeoModel, FlaxGPTNeoForCausalLM) if is_flax_available() else ()
			UpperCAmelCase_      :					Optional[Any]								=	(FlaxGPTNeoForCausalLM,) if is_flax_available() else ()



			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  FlaxGPTNeoModelTester(self)



			def 							a_	(       self):




					"""simple docstring"""


					for model_class_name in self.all_model_classes:
							lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.check_use_cache_forward(__lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase)



			def 							a_	(       self):




					"""simple docstring"""


					for model_class_name in self.all_model_classes:
							lowerCAmelCase ,					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.check_use_cache_forward_with_attn_mask(
							    __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase	,   __lowerCAmelCase)



			@slow
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase        =  GPTaTokenizer.from_pretrained("""gpt2"""	,   pad_token="""<|endoftext|>"""	,   padding_side="""left""")
					lowerCAmelCase        =  tokenizer(["""Hello this is a long string""", """Hey"""]	,   return_tensors="""np"""	,   padding=__lowerCAmelCase	,   truncation=__lowerCAmelCase)

					lowerCAmelCase        =  FlaxGPTNeoForCausalLM.from_pretrained("""EleutherAI/gpt-neo-125M""")
					lowerCAmelCase        =  False
					lowerCAmelCase        =  model.config.eos_token_id

					lowerCAmelCase        =  jax.jit(model.generate)

					lowerCAmelCase        =  jit_generate(
					    inputs["""input_ids"""]	,   attention_mask=inputs["""attention_mask"""]	,   pad_token_id=tokenizer.pad_token_id).sequences

					lowerCAmelCase        =  tokenizer.batch_decode(__lowerCAmelCase	,   skip_special_tokens=__lowerCAmelCase)

					lowerCAmelCase        =  [
					    """Hello this is a long string of text.\n\nI'm trying to get the text of the""",
					    """Hey, I'm a little late to the party. I'm going to""",
					]

					self.assertListEqual(__lowerCAmelCase	,   __lowerCAmelCase)



			@is_pt_flax_cross_test
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							with self.subTest(model_class.__name__):
									# prepare inputs
									lowerCAmelCase        =  self._prepare_for_class(__lowerCAmelCase	,   __lowerCAmelCase)
									lowerCAmelCase        =  {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

									# load corresponding PyTorch class
									lowerCAmelCase        =  model_class.__name__[4:]  # Skip the "Flax" at the beginning
									lowerCAmelCase        =  getattr(__lowerCAmelCase	,   __lowerCAmelCase)

									lowerCAmelCase ,					lowerCAmelCase        =  pt_inputs["""input_ids"""].shape
									lowerCAmelCase        =  np.random.randint(0	,   seq_length - 1	,   size=(batch_size,))
									for batch_idx, start_index in enumerate(__lowerCAmelCase):
											lowerCAmelCase        =  0
											lowerCAmelCase        =  1
											lowerCAmelCase        =  0
											lowerCAmelCase        =  1
									lowerCAmelCase        =  pt_model_class(__lowerCAmelCase).eval()
									lowerCAmelCase        =  model_class(__lowerCAmelCase	,   dtype=jnp.floataa)

									lowerCAmelCase        =  convert_pytorch_state_dict_to_flax(pt_model.state_dict()	,   __lowerCAmelCase)
									lowerCAmelCase        =  fx_state

									with torch.no_grad():
											lowerCAmelCase        =  pt_model(**__lowerCAmelCase).to_tuple()

									lowerCAmelCase        =  fx_model(**__lowerCAmelCase).to_tuple()
									self.assertEqual(len(__lowerCAmelCase)	,   len(__lowerCAmelCase)	,   """Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(__lowerCAmelCase	,   __lowerCAmelCase):
											self.assert_almost_equals(fx_output[:, -1]	,   pt_output[:, -1].numpy()	,   4E-2)

									with tempfile.TemporaryDirectory() as tmpdirname:
											pt_model.save_pretrained(__lowerCAmelCase)
											lowerCAmelCase        =  model_class.from_pretrained(__lowerCAmelCase	,   from_pt=__lowerCAmelCase)

									lowerCAmelCase        =  fx_model_loaded(**__lowerCAmelCase).to_tuple()
									self.assertEqual(
									    len(__lowerCAmelCase)	,   len(__lowerCAmelCase)	,   """Output lengths differ between Flax and PyTorch""")
									for fx_output_loaded, pt_output in zip(__lowerCAmelCase	,   __lowerCAmelCase):
											self.assert_almost_equals(fx_output_loaded[:, -1]	,   pt_output[:, -1].numpy()	,   4E-2)



			@is_pt_flax_cross_test
			def 							a_	(       self):




					"""simple docstring"""


					lowerCAmelCase ,					lowerCAmelCase        =  self.model_tester.prepare_config_and_inputs_for_common()
					for model_class in self.all_model_classes:
							with self.subTest(model_class.__name__):
									# prepare inputs
									lowerCAmelCase        =  self._prepare_for_class(__lowerCAmelCase	,   __lowerCAmelCase)
									lowerCAmelCase        =  {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

									# load corresponding PyTorch class
									lowerCAmelCase        =  model_class.__name__[4:]  # Skip the "Flax" at the beginning
									lowerCAmelCase        =  getattr(__lowerCAmelCase	,   __lowerCAmelCase)

									lowerCAmelCase        =  pt_model_class(__lowerCAmelCase).eval()
									lowerCAmelCase        =  model_class(__lowerCAmelCase	,   dtype=jnp.floataa)

									lowerCAmelCase        =  load_flax_weights_in_pytorch_model(__lowerCAmelCase	,   fx_model.params)
									lowerCAmelCase ,					lowerCAmelCase        =  pt_inputs["""input_ids"""].shape
									lowerCAmelCase        =  np.random.randint(0	,   seq_length - 1	,   size=(batch_size,))
									for batch_idx, start_index in enumerate(__lowerCAmelCase):
											lowerCAmelCase        =  0
											lowerCAmelCase        =  1
											lowerCAmelCase        =  0
											lowerCAmelCase        =  1

									# make sure weights are tied in PyTorch
									pt_model.tie_weights()

									with torch.no_grad():
											lowerCAmelCase        =  pt_model(**__lowerCAmelCase).to_tuple()

									lowerCAmelCase        =  fx_model(**__lowerCAmelCase).to_tuple()
									self.assertEqual(len(__lowerCAmelCase)	,   len(__lowerCAmelCase)	,   """Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(__lowerCAmelCase	,   __lowerCAmelCase):
											self.assert_almost_equals(fx_output[:, -1]	,   pt_output[:, -1].numpy()	,   4E-2)

									with tempfile.TemporaryDirectory() as tmpdirname:
											fx_model.save_pretrained(__lowerCAmelCase)
											lowerCAmelCase        =  pt_model_class.from_pretrained(__lowerCAmelCase	,   from_flax=__lowerCAmelCase)

									with torch.no_grad():
											lowerCAmelCase        =  pt_model_loaded(**__lowerCAmelCase).to_tuple()

									self.assertEqual(
									    len(__lowerCAmelCase)	,   len(__lowerCAmelCase)	,   """Output lengths differ between Flax and PyTorch""")
									for fx_output, pt_output in zip(__lowerCAmelCase	,   __lowerCAmelCase):
											self.assert_almost_equals(fx_output[:, -1]	,   pt_output[:, -1].numpy()	,   4E-2)







			@slow
			def 							a_	(       self):




					"""simple docstring"""


					for model_class_name in self.all_model_classes:
							lowerCAmelCase        =  model_class_name.from_pretrained("""EleutherAI/gpt-neo-125M""")
							lowerCAmelCase        =  model(np.ones((1, 1)))
							self.assertIsNotNone(__lowerCAmelCase)




