"""simple docstring"""
import argparse
import os
import re

import packaging.version


SCREAMING_SNAKE_CASE__					  =     "examples/"
SCREAMING_SNAKE_CASE__					  =     {
    "examples": (re.compile(R"^check_min_version\(\"[^\"]+\"\)\s*$", re.MULTILINE), "check_min_version(\"VERSION\")\n"),
    "init": (re.compile(R"^__version__\s+=\s+\"([^\"]+)\"\s*$", re.MULTILINE), "__version__ = \"VERSION\"\n"),
    "setup": (re.compile(R"^(\s*)version\s*=\s*\"[^\"]+\",", re.MULTILINE), R"\1version=\"VERSION\","),
    "doc": (re.compile(R"^(\s*)release\s*=\s*\"[^\"]+\"$", re.MULTILINE), "release = \"VERSION\"\n"),
}
SCREAMING_SNAKE_CASE__					  =     {
    "init": "src/transformers/__init__.py",
    "setup": "setup.py",
}
SCREAMING_SNAKE_CASE__					  =     "README.md"





def       lowerCAmelCase__			(	_UpperCamelCase				: int       ,	_UpperCamelCase				: List[Any]       ,	_UpperCamelCase				: Tuple    )    -> Dict:





						"""simple docstring"""






						with open(A__       ,	'r'       ,	encoding='utf-8'       ,	newline='\n'    ) as f:
												snake_case	    =	f.read()
						snake_case    ,snake_case	    =	REPLACE_PATTERNS[pattern]
						snake_case	    =	replace.replace('VERSION'       ,	A__    )
						snake_case	    =	re_pattern.sub(A__       ,	A__    )
						with open(A__       ,	'w'       ,	encoding='utf-8'       ,	newline='\n'    ) as f:
												f.write(A__    )





def       lowerCAmelCase__			(	_UpperCamelCase				: Dict    )    -> Any:





						"""simple docstring"""






						for folder, directories, fnames in os.walk(A__    ):
												# Removing some of the folders with non-actively maintained examples from the walk
												if "research_projects" in directories:
																		directories.remove('research_projects'    )
												if "legacy" in directories:
																		directories.remove('legacy'    )
												for fname in fnames:
																		if fname.endswith('.py'    ):
																								update_version_in_file(os.path.join(A__       ,	A__    )       ,	A__       ,	pattern='examples'    )





def       lowerCAmelCase__			(	_UpperCamelCase				: Any       ,	_UpperCamelCase				: Tuple=False    )    -> Tuple:





						"""simple docstring"""






						for pattern, fname in REPLACE_FILES.items():
												update_version_in_file(A__       ,	A__       ,	A__    )
						if not patch:
												update_version_in_examples(A__    )





def       lowerCAmelCase__			(	)    -> Tuple:





						"""simple docstring"""






						snake_case	    =	'🤗 Transformers currently provides the following architectures'
						snake_case	    =	'1. Want to contribute a new model?'
						with open(A__       ,	'r'       ,	encoding='utf-8'       ,	newline='\n'    ) as f:
												snake_case	    =	f.readlines()

						# Find the start of the list.
						snake_case	    =	0
						while not lines[start_index].startswith(_start_prompt    ):
												start_index += 1
						start_index += 1

						snake_case	    =	start_index
						# Update the lines in the model list.
						while not lines[index].startswith(_end_prompt    ):
												if lines[index].startswith('1.'    ):
																		snake_case	    =	lines[index].replace(
																		    'https://huggingface.co/docs/transformers/main/model_doc'       ,	'https://huggingface.co/docs/transformers/model_doc'       ,	)
												index += 1

						with open(A__       ,	'w'       ,	encoding='utf-8'       ,	newline='\n'    ) as f:
												f.writelines(A__    )





def       lowerCAmelCase__			(	)    -> List[str]:





						"""simple docstring"""






						with open(REPLACE_FILES['init']       ,	'r'    ) as f:
												snake_case	    =	f.read()
						snake_case	    =	REPLACE_PATTERNS['init'][0].search(A__    ).groups()[0]
						return packaging.version.parse(A__    )





def       lowerCAmelCase__			(	_UpperCamelCase				: Tuple=False    )    -> Union[str, Any]:





						"""simple docstring"""






						snake_case	    =	get_version()
						if patch and default_version.is_devrelease:
												raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!'    )
						if default_version.is_devrelease:
												snake_case	    =	default_version.base_version
						elif patch:
												snake_case	    =	f"""{default_version.major}.{default_version.minor}.{default_version.micro + 1}"""
						else:
												snake_case	    =	f"""{default_version.major}.{default_version.minor + 1}.0"""

						# Now let's ask nicely if that's the right one.
						snake_case	    =	input(f"""Which version are you releasing? [{default_version}]"""    )
						if len(A__    ) == 0:
												snake_case	    =	default_version

						print(f"""Updating version to {version}."""    )
						global_version_update(A__       ,	patch=A__    )
						if not patch:
												print('Cleaning main README, don\'t forget to run `make fix-copies`.'    )
												clean_main_ref_in_model_list()





def       lowerCAmelCase__			(	)    -> Union[str, Any]:





						"""simple docstring"""






						snake_case	    =	get_version()
						snake_case	    =	f"""{current_version.major}.{current_version.minor + 1}.0.dev0"""
						snake_case	    =	current_version.base_version

						# Check with the user we got that right.
						snake_case	    =	input(f"""Which version are we developing now? [{dev_version}]"""    )
						if len(A__    ) == 0:
												snake_case	    =	dev_version

						print(f"""Updating version to {version}."""    )
						global_version_update(A__    )
						print('Cleaning main README, don\'t forget to run `make fix-copies`.'    )
						clean_main_ref_in_model_list()


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE__					  =     argparse.ArgumentParser()
					parser.add_argument("--post_release", action="store_true", help="Whether this is pre or post release.")
					parser.add_argument("--patch", action="store_true", help="Whether or not this is a patch release.")
					SCREAMING_SNAKE_CASE__					  =     parser.parse_args()
					if not args.post_release:
										pre_release_work(patch=args.patch)
					elif args.patch:
										print("Nothing to do after a patch :-)")
					else:
										post_release_work()





'''simple docstring'''






import torch
from torch import nn






class 			lowercase_	(nn.Module      ):




  """simple docstring"""




  def __init__(      self   :     Optional[int]					,lowercase__   :     List[str]					,lowercase__   :     Any					,lowercase__   :     Union[str, Any]					,lowercase__   :     Optional[int]					,lowercase__   :     Optional[int]=1					,lowercase__   :     Optional[Any]=False			):
   super().__init__()

   __lowercase							  = n_token
   __lowercase							  = d_embed
   __lowercase							  = d_proj

   __lowercase							  = cutoffs + [n_token]
   __lowercase							  = [0] + self.cutoffs
   __lowercase							  = div_val

   __lowercase							  = self.cutoffs[0]
   __lowercase							  = len(self.cutoffs			) - 1
   __lowercase							  = self.shortlist_size + self.n_clusters

   if self.n_clusters > 0:
    __lowercase							  = nn.Parameter(torch.zeros(self.n_clusters					,self.d_embed			)			)
    __lowercase							  = nn.Parameter(torch.zeros(self.n_clusters			)			)

   __lowercase							  = nn.ModuleList()
   __lowercase							  = nn.ParameterList()

   if div_val == 1:
    for i in range(len(self.cutoffs			)			):
     if d_proj != d_embed:
      self.out_projs.append(nn.Parameter(torch.FloatTensor(lowercase__					,lowercase__			)			)			)
     else:
      self.out_projs.append(lowercase__			)

    self.out_layers.append(nn.Linear(lowercase__					,lowercase__			)			)
   else:
    for i in range(len(self.cutoffs			)			):
     __lowercase  ,	__lowercase							  = self.cutoff_ends[i], self.cutoff_ends[i + 1]
     __lowercase							  = d_embed // (div_val**i)

     self.out_projs.append(nn.Parameter(torch.FloatTensor(lowercase__					,lowercase__			)			)			)

     self.out_layers.append(nn.Linear(lowercase__					,r_idx - l_idx			)			)

   __lowercase							  = keep_order






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]					,lowercase__   :     List[Any]					,lowercase__   :     Dict					,lowercase__   :     List[Any]					,lowercase__   :     Any			):
   if proj is None:
    __lowercase							  = nn.functional.linear(lowercase__					,lowercase__					,bias=lowercase__			)
   else:
    # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
    __lowercase							  = nn.functional.linear(lowercase__					,proj.t().contiguous()			)
    __lowercase							  = nn.functional.linear(lowercase__					,lowercase__					,bias=lowercase__			)
    # else:
    #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
    #     if bias is not None:
    #         logit = logit + bias

   return logit






  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]					,lowercase__   :     Optional[Any]					,lowercase__   :     Any=None					,lowercase__   :     List[str]=False			):

   if labels is not None:
    # Shift so that tokens < n predict n
    __lowercase							  = hidden[..., :-1, :].contiguous()
    __lowercase							  = labels[..., 1:].contiguous()
    __lowercase							  = hidden.view(-1					,hidden.size(-1			)			)
    __lowercase							  = labels.view(-1			)
    if hidden.size(0			) != labels.size(0			):
     raise RuntimeError('''Input and labels should have the same size in the batch dimension.'''			)
   else:
    __lowercase							  = hidden.view(-1					,hidden.size(-1			)			)

   if self.n_clusters == 0:
    __lowercase							  = self._compute_logit(lowercase__					,self.out_layers[0].weight					,self.out_layers[0].bias					,self.out_projs[0]			)
    if labels is not None:
     __lowercase							  = labels != -1_0_0
     __lowercase							  = torch.zeros_like(lowercase__					,dtype=hidden.dtype					,device=hidden.device			)
     __lowercase							  = (
         -nn.functional.log_softmax(lowercase__					,dim=-1			)[mask].gather(1					,labels[mask].unsqueeze(1			)			).squeeze(1			)
     )
    else:
     __lowercase							  = nn.functional.log_softmax(lowercase__					,dim=-1			)
   else:
    # construct weights and biases
    __lowercase  ,	__lowercase							  = [], []
    for i in range(len(self.cutoffs			)			):
     if self.div_val == 1:
      __lowercase  ,	__lowercase							  = self.cutoff_ends[i], self.cutoff_ends[i + 1]
      __lowercase							  = self.out_layers[0].weight[l_idx:r_idx]
      __lowercase							  = self.out_layers[0].bias[l_idx:r_idx]
     else:
      __lowercase							  = self.out_layers[i].weight
      __lowercase							  = self.out_layers[i].bias

     if i == 0:
      __lowercase							  = torch.cat([weight_i, self.cluster_weight]					,dim=0			)
      __lowercase							  = torch.cat([bias_i, self.cluster_bias]					,dim=0			)

     weights.append(lowercase__			)
     biases.append(lowercase__			)

    __lowercase  ,	__lowercase  ,	__lowercase							  = weights[0], biases[0], self.out_projs[0]

    __lowercase							  = self._compute_logit(lowercase__					,lowercase__					,lowercase__					,lowercase__			)
    __lowercase							  = nn.functional.log_softmax(lowercase__					,dim=1			)

    if labels is None:
     __lowercase							  = hidden.new_empty((head_logit.size(0			), self.n_token)			)
    else:
     __lowercase							  = torch.zeros_like(lowercase__					,dtype=hidden.dtype					,device=hidden.device			)

    __lowercase							  = 0
    __lowercase							  = [0] + self.cutoffs
    for i in range(len(lowercase__			) - 1			):
     __lowercase  ,	__lowercase							  = cutoff_values[i], cutoff_values[i + 1]

     if labels is not None:
      __lowercase							  = (labels >= l_idx) & (labels < r_idx)
      __lowercase							  = mask_i.nonzero().squeeze()

      if indices_i.numel() == 0:
       continue

      __lowercase							  = labels.index_select(0					,lowercase__			) - l_idx
      __lowercase							  = head_logprob.index_select(0					,lowercase__			)
      __lowercase							  = hidden.index_select(0					,lowercase__			)
     else:
      __lowercase							  = hidden

     if i == 0:
      if labels is not None:
       __lowercase							  = head_logprob_i.gather(1					,target_i[:, None]			).squeeze(1			)
      else:
       __lowercase							  = head_logprob[:, : self.cutoffs[0]]
     else:
      __lowercase  ,	__lowercase  ,	__lowercase							  = weights[i], biases[i], self.out_projs[i]

      __lowercase							  = self._compute_logit(lowercase__					,lowercase__					,lowercase__					,lowercase__			)
      __lowercase							  = nn.functional.log_softmax(lowercase__					,dim=1			)
      __lowercase							  = self.cutoffs[0] + i - 1  # No probability for the head cluster
      if labels is not None:
       __lowercase							  = head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
           1					,target_i[:, None]			).squeeze(1			)
      else:
       __lowercase							  = head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
       __lowercase							  = logprob_i

     if labels is not None:
      if (hasattr(self					,'''keep_order'''			) and self.keep_order) or keep_order:
       out.index_copy_(0					,lowercase__					,-logprob_i			)
      else:
       out[offset : offset + logprob_i.size(0			)].copy_(-logprob_i			)
      offset += logprob_i.size(0			)

   return out






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any					,lowercase__   :     Union[str, Any]			):
   if self.n_clusters == 0:
    __lowercase							  = self._compute_logit(lowercase__					,self.out_layers[0].weight					,self.out_layers[0].bias					,self.out_projs[0]			)
    return nn.functional.log_softmax(lowercase__					,dim=-1			)
   else:
    # construct weights and biases
    __lowercase  ,	__lowercase							  = [], []
    for i in range(len(self.cutoffs			)			):
     if self.div_val == 1:
      __lowercase  ,	__lowercase							  = self.cutoff_ends[i], self.cutoff_ends[i + 1]
      __lowercase							  = self.out_layers[0].weight[l_idx:r_idx]
      __lowercase							  = self.out_layers[0].bias[l_idx:r_idx]
     else:
      __lowercase							  = self.out_layers[i].weight
      __lowercase							  = self.out_layers[i].bias

     if i == 0:
      __lowercase							  = torch.cat([weight_i, self.cluster_weight]					,dim=0			)
      __lowercase							  = torch.cat([bias_i, self.cluster_bias]					,dim=0			)

     weights.append(lowercase__			)
     biases.append(lowercase__			)

    __lowercase  ,	__lowercase  ,	__lowercase							  = weights[0], biases[0], self.out_projs[0]
    __lowercase							  = self._compute_logit(lowercase__					,lowercase__					,lowercase__					,lowercase__			)

    __lowercase							  = hidden.new_empty((head_logit.size(0			), self.n_token)			)
    __lowercase							  = nn.functional.log_softmax(lowercase__					,dim=1			)

    __lowercase							  = [0] + self.cutoffs
    for i in range(len(lowercase__			) - 1			):
     __lowercase  ,	__lowercase							  = cutoff_values[i], cutoff_values[i + 1]

     if i == 0:
      __lowercase							  = head_logprob[:, : self.cutoffs[0]]
     else:
      __lowercase  ,	__lowercase  ,	__lowercase							  = weights[i], biases[i], self.out_projs[i]

      __lowercase							  = self._compute_logit(lowercase__					,lowercase__					,lowercase__					,lowercase__			)
      __lowercase							  = nn.functional.log_softmax(lowercase__					,dim=1			)

      __lowercase							  = head_logprob[:, -i] + tail_logprob_i
      __lowercase							  = logprob_i

    return out

