

'''simple docstring'''

import argparse
import os
import re

import packaging.version


UpperCAmelCase       =     '''examples/'''
UpperCAmelCase       =     {
    '''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'''),
}
UpperCAmelCase       =     {
    '''init''': '''src/transformers/__init__.py''',
    '''setup''': '''setup.py''',
}
UpperCAmelCase       =     '''README.md'''

def  __UpperCamelCase							(		lowercase__  :     int,    lowercase__  :     Any,    lowercase__  :     int       ):






     '''simple docstring'''





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

def  __UpperCamelCase							(		lowercase__  :     int       ):






     '''simple docstring'''





     for folder, directories, fnames in os.walk(__snake_case       ):
          # 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(__snake_case,    __snake_case       ),    __snake_case,    pattern='examples'       )

def  __UpperCamelCase							(		lowercase__  :     Optional[Any],    lowercase__  :     Optional[Any]=False       ):






     '''simple docstring'''





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

def  __UpperCamelCase							(		):






     '''simple docstring'''





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

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

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

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

def  __UpperCamelCase							(		):






     '''simple docstring'''





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

def  __UpperCamelCase							(		lowercase__  :     Optional[Any]=False       ):






     '''simple docstring'''





     __lowercase							=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:
          __lowercase							=default_version.base_version
     elif patch:
          __lowercase							=F'''{default_version.major}.{default_version.minor}.{default_version.micro + 1}'''
     else:
          __lowercase							=F'''{default_version.major}.{default_version.minor + 1}.0'''

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

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

def  __UpperCamelCase							(		):






     '''simple docstring'''





     __lowercase							=get_version()
     __lowercase							=F'''{current_version.major}.{current_version.minor + 1}.0.dev0'''
     __lowercase							=current_version.base_version

     # Check with the user we got that right.
     __lowercase							=input(F'''Which version are we developing now? [{dev_version}]'''       )
     if len(__snake_case       ) == 0:
          __lowercase							=dev_version

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


if __name__ == "__main__":
       UpperCAmelCase       =     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.''')
       UpperCAmelCase       =     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"""



# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
						import torch





class 					_UpperCAmelCase       ( TensorFormatter[Mapping, "torch.Tensor", Mapping]							):




					def __init__(				self     :						Any    ,						A     :						int=None    ,						**A     :						str   )  ->  Union[str, Any]:
							super().__init__(features=A   )
							lowercase_ : Union[str, Any]							       =   torch_tensor_kwargs
							import torch  # noqa import torch at initialization




					def   A  (				self     :						Dict    ,						A     :						int   )  ->  List[Any]:
							import torch

							if isinstance(A    ,						A   ) and column:
									if all(
									    isinstance(A    ,						torch.Tensor   ) and x.shape == column[0].shape and x.dtype == column[0].dtype
									    for x in column   ):
											return torch.stack(A   )
							return column




					def   A  (				self     :						int    ,						A     :						Any   )  ->  Optional[Any]:
							import torch

							if isinstance(A    ,						(str, bytes, type(A   ))   ):
									return value
							elif isinstance(A    ,						(np.character, np.ndarray)   ) and np.issubdtype(value.dtype    ,						np.character   ):
									return value.tolist()

							lowercase_ : Any							       =   {}

							if isinstance(A    ,						(np.number, np.ndarray)   ) and np.issubdtype(value.dtype    ,						np.integer   ):
									lowercase_ : Any							       =   {'''dtype''': torch.intaa}
							elif isinstance(A    ,						(np.number, np.ndarray)   ) and np.issubdtype(value.dtype    ,						np.floating   ):
									lowercase_ : Dict							       =   {'''dtype''': torch.floataa}
							elif config.PIL_AVAILABLE and "PIL" in sys.modules:
									import PIL.Image

									if isinstance(A    ,						PIL.Image.Image   ):
											lowercase_ : Dict							       =   np.asarray(A   )
							return torch.tensor(A    ,						**{**default_dtype, **self.torch_tensor_kwargs}   )




					def   A  (				self     :						Union[str, Any]    ,						A     :						Optional[int]   )  ->  str:
							import torch

							# support for torch, tf, jax etc.
							if hasattr(A    ,						'''__array__'''   ) and not isinstance(A    ,						torch.Tensor   ):
									lowercase_ : Optional[int]							       =   data_struct.__array__()
							# support for nested types like struct of list of struct
							if isinstance(A    ,						np.ndarray   ):
									if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
											return self._consolidate([self.recursive_tensorize(A   ) for substruct in data_struct]   )
							elif isinstance(A    ,						(list, tuple)   ):
									return self._consolidate([self.recursive_tensorize(A   ) for substruct in data_struct]   )
							return self._tensorize(A   )




					def   A  (				self     :						Dict    ,						A     :						dict   )  ->  Tuple:
							return map_nested(self._recursive_tensorize    ,						A    ,						map_list=A   )




					def   A  (				self     :						str    ,						A     :						pa.Table   )  ->  Mapping:
							lowercase_ : Optional[Any]							       =   self.numpy_arrow_extractor().extract_row(A   )
							lowercase_ : str							       =   self.python_features_decoder.decode_row(A   )
							return self.recursive_tensorize(A   )




					def   A  (				self     :						List[Any]    ,						A     :						pa.Table   )  ->  "torch.Tensor":
							lowercase_ : List[str]							       =   self.numpy_arrow_extractor().extract_column(A   )
							lowercase_ : str							       =   self.python_features_decoder.decode_column(A    ,						pa_table.column_names[0]   )
							lowercase_ : Optional[int]							       =   self.recursive_tensorize(A   )
							lowercase_ : Any							       =   self._consolidate(A   )
							return column






					def   A  (				self     :						List[str]    ,						A     :						pa.Table   )  ->  Mapping:
							lowercase_ : Optional[int]							       =   self.numpy_arrow_extractor().extract_batch(A   )
							lowercase_ : int							       =   self.python_features_decoder.decode_batch(A   )
							lowercase_ : Dict							       =   self.recursive_tensorize(A   )
							for column_name in batch:
									lowercase_ : Optional[Any]							       =   self._consolidate(batch[column_name]   )
							return batch



