


import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


__UpperCamelCase      						=    [nn.LayerNorm]

__UpperCamelCase      						=    logging.get_logger(__name__)

__UpperCamelCase      						=    version.parse(version.parse(torch.__version__).base_version)

__UpperCamelCase      						=    parsed_torch_version_base >= version.parse('2.0')
__UpperCamelCase      						=    parsed_torch_version_base >= version.parse('1.12')
__UpperCamelCase      						=    parsed_torch_version_base >= version.parse('1.11')
__UpperCamelCase      						=    parsed_torch_version_base < version.parse('1.11')
def     UpperCamelCase_(			_A				:Dict			,       _A				:Any			,       _A				:Optional[int]			,       _A				:List[Any]			,       self				:Dict					)->						List[Any]:
			from torch import _softmax_backward_data

			if is_torch_less_than_1_11:
						return _softmax_backward_data(_A			,       _A			,       parent.dim			,       self					)
			else:
						return _softmax_backward_data(_A			,       _A			,       parent.dim			,       self.dtype					)
def     UpperCamelCase_(			_A				:nn.Linear			,       _A				:torch.LongTensor			,       _A				:int = 0					)->						nn.Linear:
			UpperCamelCase__   =    index.to(layer.weight.device					)
			UpperCamelCase__   =    layer.weight.index_select(_A			,       _A					).clone().detach()
			if layer.bias is not None:
						if dim == 1:
									UpperCamelCase__   =    layer.bias.clone().detach()
						else:
									UpperCamelCase__   =    layer.bias[index].clone().detach()
			UpperCamelCase__   =    list(layer.weight.size()					)
			UpperCamelCase__   =    len(_A					)
			UpperCamelCase__   =    nn.Linear(new_size[1]			,       new_size[0]			,       bias=layer.bias is not None					).to(layer.weight.device					)
			UpperCamelCase__   =    False
			new_layer.weight.copy_(W.contiguous()					)
			UpperCamelCase__   =    True
			if layer.bias is not None:
						UpperCamelCase__   =    False
						new_layer.bias.copy_(b.contiguous()					)
						UpperCamelCase__   =    True
			return new_layer







class     lowerCamelCase__		(							nn.Module ):






			"""simple docstring"""






			def __init__(							self ,  snake_case ,  snake_case       ):

						'''simple docstring'''


						super().__init__()
						UpperCamelCase__   =    nf
						UpperCamelCase__   =    nn.Parameter(torch.empty(snake_case ,  snake_case       )       )
						UpperCamelCase__   =    nn.Parameter(torch.zeros(snake_case       )       )
						nn.init.normal_(self.weight ,  std=0.02       )







			def 			snake_case__			(							self ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    x.size()[:-1] + (self.nf,)
						UpperCamelCase__   =    torch.addmm(self.bias ,  x.view(-1 ,  x.size(-1       )       ) ,  self.weight       )
						UpperCamelCase__   =    x.view(snake_case       )
						return x



def     UpperCamelCase_(			_A				:ConvaD			,       _A				:torch.LongTensor			,       _A				:int = 1					)->						ConvaD:
			UpperCamelCase__   =    index.to(layer.weight.device					)
			UpperCamelCase__   =    layer.weight.index_select(_A			,       _A					).clone().detach()
			if dim == 0:
						UpperCamelCase__   =    layer.bias.clone().detach()
			else:
						UpperCamelCase__   =    layer.bias[index].clone().detach()
			UpperCamelCase__   =    list(layer.weight.size()					)
			UpperCamelCase__   =    len(_A					)
			UpperCamelCase__   =    ConvaD(new_size[1]			,       new_size[0]					).to(layer.weight.device					)
			UpperCamelCase__   =    False
			new_layer.weight.copy_(W.contiguous()					)
			UpperCamelCase__   =    True
			UpperCamelCase__   =    False
			new_layer.bias.copy_(b.contiguous()					)
			UpperCamelCase__   =    True
			return new_layer
def     UpperCamelCase_(			_A				:Union[nn.Linear, ConvaD]			,       _A				:torch.LongTensor			,       _A				:Optional[int] = None					)->						Union[nn.Linear, ConvaD]:
			if isinstance(_A			,       nn.Linear					):
						return prune_linear_layer(_A			,       _A			,       dim=0 if dim is None else dim					)
			elif isinstance(_A			,       _A					):
						return prune_convad_layer(_A			,       _A			,       dim=1 if dim is None else dim					)
			else:
						raise ValueError(F'''Can\'t prune layer of class {layer.__class__}'''					)
def     UpperCamelCase_(			_A				:Callable[..., torch.Tensor]			,       _A				:int			,       _A				:int			,       *_A				:str					)->						torch.Tensor:
			assert len(_A					) > 0, F'''{input_tensors} has to be a tuple/list of tensors'''

			# inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
			UpperCamelCase__   =    len(inspect.signature(_A					).parameters					)
			if num_args_in_forward_chunk_fn != len(_A					):
						raise ValueError(
						    F'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(_A					)} input '''
						    "tensors are given"					)

			if chunk_size > 0:
						UpperCamelCase__   =    input_tensors[0].shape[chunk_dim]
						for input_tensor in input_tensors:
									if input_tensor.shape[chunk_dim] != tensor_shape:
												raise ValueError(
												    F'''All input tenors have to be of the same shape: {tensor_shape}, '''
												    F'''found shape {input_tensor.shape[chunk_dim]}'''					)

						if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
									raise ValueError(
									    F'''The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk '''
									    F'''size {chunk_size}'''					)

						UpperCamelCase__   =    input_tensors[0].shape[chunk_dim] // chunk_size

						# chunk input tensor into tuples
						UpperCamelCase__   =    tuple(input_tensor.chunk(_A			,       dim=_A					) for input_tensor in input_tensors					)
						# apply forward fn to every tuple
						UpperCamelCase__   =    tuple(forward_fn(*_A					) for input_tensors_chunk in zip(*_A					)					)
						# concatenate output at same dimension
						return torch.cat(_A			,       dim=_A					)

			return forward_fn(*_A					)
def     UpperCamelCase_(			_A				:List[int]			,       _A				:int			,       _A				:int			,       _A				:Set[int]					)->						Tuple[Set[int], torch.LongTensor]:
			UpperCamelCase__   =    torch.ones(_A			,       _A					)
			UpperCamelCase__   =    set(_A					) - already_pruned_heads  # Convert to set and remove already pruned heads
			for head in heads:
						# Compute how many pruned heads are before the head and move the index accordingly
						UpperCamelCase__   =    head - sum(1 if h < head else 0 for h in already_pruned_heads					)
						UpperCamelCase__   =    0
			UpperCamelCase__   =    mask.view(-1					).contiguous().eq(1					)
			UpperCamelCase__   =    torch.arange(len(_A					)					)[mask].long()
			return heads, index
def     UpperCamelCase_(			*_A				:Union[torch.Tensor, List[torch.Tensor]]			,       _A				:Optional[str] = None					)->						Tuple[torch.Tensor, ...]:
			return torch.meshgrid(*_A			,       indexing=_A					)







def     UpperCamelCase_(			_A				:torch.Tensor					)->						Tuple[torch.device, int, int]:
			return tensor.device, storage_ptr(_A					), storage_size(_A					)






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     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			@staticmethod
			def 			snake_case__			(							snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    parser.add_parser("login" ,  help="Log in using the same credentials as on huggingface.co"       )
						login_parser.set_defaults(func=lambda snake_case       : LoginCommand(snake_case       )       )
						UpperCamelCase__   =    parser.add_parser("whoami" ,  help="Find out which huggingface.co account you are logged in as."       )
						whoami_parser.set_defaults(func=lambda snake_case       : WhoamiCommand(snake_case       )       )
						UpperCamelCase__   =    parser.add_parser("logout" ,  help="Log out"       )
						logout_parser.set_defaults(func=lambda snake_case       : LogoutCommand(snake_case       )       )

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







class     lowerCamelCase__		:






			"""simple docstring"""






			_UpperCamelCase						:   int					=						'\u001b[1m'
			_UpperCamelCase						:   List[Any]					=						'\u001b[31m'
			_UpperCamelCase						:   Tuple					=						'\u001b[90m'
			_UpperCamelCase						:   Tuple					=						'\u001b[0m'




			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


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




			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


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







			@classmethod
			def 			snake_case__			(							cls ,  snake_case       ):

						'''simple docstring'''


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



def     UpperCamelCase_(			_A				:List[List[Union[str, int]]]			,       _A				:List[str]					)->						str:
			UpperCamelCase__   =    [max(len(str(_A					)					) for x in col					) for col in zip(*_A			,       _A					)]
			UpperCamelCase__   =    ("{{:{}}} " * len(_A					)).format(*_A					)
			UpperCamelCase__   =    []
			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     lowerCamelCase__		:






			"""simple docstring"""






			def __init__(							self ,  snake_case       ):

						'''simple docstring'''


						UpperCamelCase__   =    args







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							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     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


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







class     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							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     lowerCamelCase__		(							UpperCAmelCase ):






			"""simple docstring"""






			def 			snake_case__			(							self       ):

						'''simple docstring'''


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

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

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


