





import argparse
import collections
import os
import re
import tempfile

import pandas as pd
from datasets import Dataset
from huggingface_hub import hf_hub_download, upload_folder

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/update_metadata.py
_a     :    str         =						'src/transformers'


# This is to make sure the transformers module imported is the one in the repo.
_a     :    int         =						direct_transformers_import(TRANSFORMERS_PATH)


# Regexes that match TF/Flax/PT model names.
_a     :    str         =						re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
_a     :    List[Any]         =						re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
_a     :    List[str]         =						re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# Fill this with tuples (pipeline_tag, model_mapping, auto_model)
_a     :    List[str]         =						[
    ('pretraining', 'MODEL_FOR_PRETRAINING_MAPPING_NAMES', 'AutoModelForPreTraining'),
    ('feature-extraction', 'MODEL_MAPPING_NAMES', 'AutoModel'),
    ('audio-classification', 'MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForAudioClassification'),
    ('text-generation', 'MODEL_FOR_CAUSAL_LM_MAPPING_NAMES', 'AutoModelForCausalLM'),
    ('automatic-speech-recognition', 'MODEL_FOR_CTC_MAPPING_NAMES', 'AutoModelForCTC'),
    ('image-classification', 'MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForImageClassification'),
    ('image-segmentation', 'MODEL_FOR_IMAGE_SEGMENTATION_MAPPING_NAMES', 'AutoModelForImageSegmentation'),
    ('fill-mask', 'MODEL_FOR_MASKED_LM_MAPPING_NAMES', 'AutoModelForMaskedLM'),
    ('object-detection', 'MODEL_FOR_OBJECT_DETECTION_MAPPING_NAMES', 'AutoModelForObjectDetection'),
    (
        'zero-shot-object-detection',
        'MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING_NAMES',
        'AutoModelForZeroShotObjectDetection',
    ),
    ('question-answering', 'MODEL_FOR_QUESTION_ANSWERING_MAPPING_NAMES', 'AutoModelForQuestionAnswering'),
    ('text2text-generation', 'MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING_NAMES', 'AutoModelForSeq2SeqLM'),
    ('text-classification', 'MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForSequenceClassification'),
    ('automatic-speech-recognition', 'MODEL_FOR_SPEECH_SEQ_2_SEQ_MAPPING_NAMES', 'AutoModelForSpeechSeq2Seq'),
    (
        'table-question-answering',
        'MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING_NAMES',
        'AutoModelForTableQuestionAnswering',
    ),
    ('token-classification', 'MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForTokenClassification'),
    ('multiple-choice', 'MODEL_FOR_MULTIPLE_CHOICE_MAPPING_NAMES', 'AutoModelForMultipleChoice'),
    (
        'next-sentence-prediction',
        'MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING_NAMES',
        'AutoModelForNextSentencePrediction',
    ),
    (
        'audio-frame-classification',
        'MODEL_FOR_AUDIO_FRAME_CLASSIFICATION_MAPPING_NAMES',
        'AutoModelForAudioFrameClassification',
    ),
    ('audio-xvector', 'MODEL_FOR_AUDIO_XVECTOR_MAPPING_NAMES', 'AutoModelForAudioXVector'),
    (
        'document-question-answering',
        'MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING_NAMES',
        'AutoModelForDocumentQuestionAnswering',
    ),
    (
        'visual-question-answering',
        'MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING_NAMES',
        'AutoModelForVisualQuestionAnswering',
    ),
    ('image-to-text', 'MODEL_FOR_FOR_VISION_2_SEQ_MAPPING_NAMES', 'AutoModelForVision2Seq'),
    (
        'zero-shot-image-classification',
        'MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING_NAMES',
        'AutoModelForZeroShotImageClassification',
    ),
    ('depth-estimation', 'MODEL_FOR_DEPTH_ESTIMATION_MAPPING_NAMES', 'AutoModelForDepthEstimation'),
    ('video-classification', 'MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING_NAMES', 'AutoModelForVideoClassification'),
    ('mask-generation', 'MODEL_FOR_MASK_GENERATION_MAPPING_NAMES', 'AutoModelForMaskGeneration'),
]
def 							a_	(    __magic_name__   )				->			Union[str, Any]:




				"""simple docstring"""






				snake_case   :					List[str]    						=				re.finditer('''.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)'''		,     __magic_name__   )
				return [m.group(0   ) for m in matches]
def 							a_	(    )				->			List[str]:




				"""simple docstring"""






				snake_case   :					Tuple    						=				transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
				snake_case   :					Optional[Any]    						=				{
				    config.replace('''Config'''		,     ''''''   ): model_type for model_type, config in config_maping_names.items()
				}

				# Dictionaries flagging if each model prefix has a backend in PT/TF/Flax.
				snake_case   :					Optional[Any]    						=				collections.defaultdict(__magic_name__   )
				snake_case   :					Optional[int]    						=				collections.defaultdict(__magic_name__   )
				snake_case   :					Dict    						=				collections.defaultdict(__magic_name__   )

				# Let's lookup through all transformers object (once) and find if models are supported by a given backend.
				for attr_name in dir(__magic_name__   ):
								snake_case   :					List[Any]    						=				None
								if _re_tf_models.match(__magic_name__   ) is not None:
												snake_case   :					Tuple    						=				tf_models
												snake_case   :					Dict    						=				_re_tf_models.match(__magic_name__   ).groups()[0]
								elif _re_flax_models.match(__magic_name__   ) is not None:
												snake_case   :					List[Any]    						=				flax_models
												snake_case   :					List[Any]    						=				_re_flax_models.match(__magic_name__   ).groups()[0]
								elif _re_pt_models.match(__magic_name__   ) is not None:
												snake_case   :					Optional[Any]    						=				pt_models
												snake_case   :					List[str]    						=				_re_pt_models.match(__magic_name__   ).groups()[0]

								if lookup_dict is not None:
												while len(__magic_name__   ) > 0:
																if attr_name in model_prefix_to_model_type:
																				snake_case   :					Tuple    						=				True
																				break
																# Try again after removing the last word in the name
																snake_case   :					Any    						=				''''''.join(camel_case_split(__magic_name__   )[:-1]   )

				snake_case   :					Dict    						=				set(list(pt_models.keys()   ) + list(tf_models.keys()   ) + list(flax_models.keys()   )   )
				snake_case   :					List[Any]    						=				list(__magic_name__   )
				all_models.sort()

				snake_case   :					Optional[int]    						=				{'''model_type''': all_models}
				snake_case   :					List[Any]    						=				[pt_models[t] for t in all_models]
				snake_case   :					str    						=				[tf_models[t] for t in all_models]
				snake_case   :					Tuple    						=				[flax_models[t] for t in all_models]

				# Now let's use the auto-mapping names to make sure
				snake_case   :					str    						=				{}
				for t in all_models:
								if t in transformers_module.models.auto.processing_auto.PROCESSOR_MAPPING_NAMES:
												snake_case   :					Dict    						=				'''AutoProcessor'''
								elif t in transformers_module.models.auto.tokenization_auto.TOKENIZER_MAPPING_NAMES:
												snake_case   :					Optional[int]    						=				'''AutoTokenizer'''
								elif t in transformers_module.models.auto.feature_extraction_auto.FEATURE_EXTRACTOR_MAPPING_NAMES:
												snake_case   :					List[str]    						=				'''AutoFeatureExtractor'''
								else:
												# Default to AutoTokenizer if a model has nothing, for backward compatibility.
												snake_case   :					str    						=				'''AutoTokenizer'''

				snake_case   :					List[Any]    						=				[processors[t] for t in all_models]

				return pd.DataFrame(__magic_name__   )
def 							a_	(    __magic_name__   )				->			str:




				"""simple docstring"""






				snake_case   :					List[str]    						=				[
				    transformers_module.models.auto.modeling_auto,
				    transformers_module.models.auto.modeling_tf_auto,
				    transformers_module.models.auto.modeling_flax_auto,
				]
				for pipeline_tag, model_mapping, auto_class in PIPELINE_TAGS_AND_AUTO_MODELS:
								snake_case   :					Optional[Any]    						=				[model_mapping, F"TF_{model_mapping}", F"FLAX_{model_mapping}"]
								snake_case   :					List[str]    						=				[auto_class, F"TF_{auto_class}", F"Flax_{auto_class}"]
								# Loop through all three frameworks
								for module, cls, mapping in zip(__magic_name__		,     __magic_name__		,     __magic_name__   ):
												# The type of pipeline may not exist in this framework
												if not hasattr(__magic_name__		,     __magic_name__   ):
																continue
												# First extract all model_names
												snake_case   :					Tuple    						=				[]
												for name in getattr(__magic_name__		,     __magic_name__   ).values():
																if isinstance(__magic_name__		,     __magic_name__   ):
																				model_names.append(__magic_name__   )
																else:
																				model_names.extend(list(__magic_name__   )   )

																# Add pipeline tag and auto model class for those models
												table.update({model_name: (pipeline_tag, cls) for model_name in model_names}   )

				return table
def 							a_	(    __magic_name__		,     __magic_name__   )				->			List[Any]:




				"""simple docstring"""






				snake_case   :					Tuple    						=				get_frameworks_table()
				snake_case   :					Dict    						=				Dataset.from_pandas(__magic_name__   )

				snake_case   :					Optional[int]    						=				hf_hub_download(
				    '''huggingface/transformers-metadata'''		,     '''pipeline_tags.json'''		,     repo_type='''dataset'''		,     token=__magic_name__   )
				snake_case   :					List[Any]    						=				Dataset.from_json(__magic_name__   )
				snake_case   :					Optional[int]    						=				{
				    tags_dataset[i]['''model_class''']: (tags_dataset[i]['''pipeline_tag'''], tags_dataset[i]['''auto_class'''])
				    for i in range(len(__magic_name__   )   )
				}
				snake_case   :					int    						=				update_pipeline_and_auto_class_table(__magic_name__   )

				# Sort the model classes to avoid some nondeterministic updates to create false update commits.
				snake_case   :					Optional[Any]    						=				sorted(table.keys()   )
				snake_case   :					Optional[int]    						=				pd.DataFrame(
				    {
				        '''model_class''': model_classes,
				        '''pipeline_tag''': [table[m][0] for m in model_classes],
				        '''auto_class''': [table[m][1] for m in model_classes],
				    }   )
				snake_case   :					Tuple    						=				Dataset.from_pandas(__magic_name__   )

				with tempfile.TemporaryDirectory() as tmp_dir:
								frameworks_dataset.to_json(os.path.join(__magic_name__		,     '''frameworks.json'''   )   )
								tags_dataset.to_json(os.path.join(__magic_name__		,     '''pipeline_tags.json'''   )   )

								if commit_sha is not None:
												snake_case   :					Tuple    						=				(
												    F"Update with commit {commit_sha}\n\nSee: "
												    F"https://github.com/huggingface/transformers/commit/{commit_sha}"
												)
								else:
												snake_case   :					Any    						=				'''Update'''

								upload_folder(
								    repo_id='''huggingface/transformers-metadata'''		,     folder_path=__magic_name__		,     repo_type='''dataset'''		,     token=__magic_name__		,     commit_message=__magic_name__		,     )
def 							a_	(    )				->			Union[str, Any]:




				"""simple docstring"""






				snake_case   :					str    						=				{tag: cls for tag, _, cls in PIPELINE_TAGS_AND_AUTO_MODELS}
				snake_case   :					int    						=				transformers_module.pipelines.SUPPORTED_TASKS
				snake_case   :					List[Any]    						=				[]
				for key in pipeline_tasks:
								if key not in in_table:
												snake_case   :					List[Any]    						=				pipeline_tasks[key]['''pt''']
												if isinstance(__magic_name__		,     (list, tuple)   ):
																snake_case   :					int    						=				model[0]
												snake_case   :					List[Any]    						=				model.__name__
												if model not in in_table.values():
																missing.append(__magic_name__   )

				if len(__magic_name__   ) > 0:
								snake_case   :					List[Any]    						=				''', '''.join(__magic_name__   )
								raise ValueError(
								    '''The following pipeline tags are not present in the `PIPELINE_TAGS_AND_AUTO_MODELS` constant inside '''
								    F"`utils/update_metadata.py`: {msg}. Please add them!"   )


if __name__ == "__main__":
			_a     :    str         =						argparse.ArgumentParser()
			parser.add_argument('--token', type=str, help='The token to use to push to the transformers-metadata dataset.')
			parser.add_argument('--commit_sha', type=str, help='The sha of the commit going with this update.')
			parser.add_argument('--check-only', action='store_true', help='Activate to just check all pipelines are present.')
			_a     :    Any         =						parser.parse_args()

			if args.check_only:
						check_pipeline_tags()
			else:
						update_metadata(args.token, args.commit_sha)






'''simple docstring'''







import argparse
from typing import List

import evaluate
import numpy as np
import torch
from datasets import DatasetDict, load_dataset

# New Code #
# We'll be using StratifiedKFold for this example
from sklearn.model_selection import StratifiedKFold
from torch.optim import AdamW
from torch.utils.data import DataLoader
from transformers import AutoModelForSequenceClassification, AutoTokenizer, get_linear_schedule_with_warmup, set_seed

from accelerate import Accelerator, DistributedType


########################################################################
# This is a fully working simple example to use Accelerate,
# specifically showcasing how to perform Cross Validation,
# and builds off the `nlp_example.py` script.
#
# This example trains a Bert base model on GLUE MRPC
# in any of the following settings (with the same script):
#   - single CPU or single GPU
#   - multi GPUS (using PyTorch distributed mode)
#   - (multi) TPUs
#   - fp16 (mixed-precision) or fp32 (normal precision)
#
# To help focus on the differences in the code, building `DataLoaders`
# was refactored into its own function.
# New additions from the base script can be found quickly by
# looking for the # New Code # tags
#
# To run it in each of these various modes, follow the instructions
# in the readme for examples:
# https://github.com/huggingface/accelerate/tree/main/examples
#
########################################################################


lowerCAmelCase_     						=				1_6
lowerCAmelCase_     						=				3_2







def 						_A      ( UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase = 16						):






   '''simple docstring'''







   A__       =     AutoTokenizer.from_pretrained('bert-base-cased'						)
   A__       =     DatasetDict(
       {
           'train': dataset['train'].select(UpperCAmelCase						),
           'validation': dataset['train'].select(UpperCAmelCase						),
           'test': dataset['validation'],
       }						)

   def tokenize_function(UpperCAmelCase						):
      # max_length=None => use the model max length (it's actually the default)
      A__       =     tokenizer(examples['sentence1']      ,examples['sentence2']      ,truncation=UpperCAmelCase      ,max_length=UpperCAmelCase						)
      return outputs

   # Apply the method we just defined to all the examples in all the splits of the dataset
   # starting with the main process first:
   with accelerator.main_process_first():
      A__       =     datasets.map(
          UpperCAmelCase      ,batched=UpperCAmelCase      ,remove_columns=['idx', 'sentence1', 'sentence2']      ,)

   # We also rename the 'label' column to 'labels' which is the expected name for labels by the models of the
   # transformers library
   A__       =     tokenized_datasets.rename_column('label'      ,'labels'						)

   def collate_fn(UpperCAmelCase						):
      # On TPU it's best to pad everything to the same length or training will be very slow.
      A__       =     128 if accelerator.distributed_type == DistributedType.TPU else None
      # When using mixed precision we want round multiples of 8/16
      if accelerator.mixed_precision == "fp8":
         A__       =     16
      elif accelerator.mixed_precision != "no":
         A__       =     8
      else:
         A__       =     None

      return tokenizer.pad(
          UpperCAmelCase      ,padding='longest'      ,max_length=UpperCAmelCase      ,pad_to_multiple_of=UpperCAmelCase      ,return_tensors='pt'      ,)

   # Instantiate dataloaders.
   A__       =     DataLoader(
       tokenized_datasets['train']      ,shuffle=UpperCAmelCase      ,collate_fn=UpperCAmelCase      ,batch_size=UpperCAmelCase						)
   A__       =     DataLoader(
       tokenized_datasets['validation']      ,shuffle=UpperCAmelCase      ,collate_fn=UpperCAmelCase      ,batch_size=UpperCAmelCase						)

   A__       =     DataLoader(
       tokenized_datasets['test']      ,shuffle=UpperCAmelCase      ,collate_fn=UpperCAmelCase      ,batch_size=UpperCAmelCase						)

   return train_dataloader, eval_dataloader, test_dataloader







def 						_A      ( UpperCAmelCase      ,UpperCAmelCase						):






   '''simple docstring'''







   A__       =     []
   # Download the dataset
   A__       =     load_dataset('glue'      ,'mrpc'						)
   # Create our splits
   A__       =     StratifiedKFold(n_splits=int(args.num_folds						)						)
   # Initialize accelerator
   A__       =     Accelerator(cpu=args.cpu      ,mixed_precision=args.mixed_precision						)
   # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
   A__       =     config['lr']
   A__       =     int(config['num_epochs']						)
   A__       =     int(config['seed']						)
   A__       =     int(config['batch_size']						)

   A__       =     evaluate.load('glue'      ,'mrpc'						)

   # If the batch size is too big we use gradient accumulation
   A__       =     1
   if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU:
      A__       =     batch_size // MAX_GPU_BATCH_SIZE
      A__       =     MAX_GPU_BATCH_SIZE

   set_seed(UpperCAmelCase						)

   # New Code #
   # Create our folds:
   A__       =     kfold.split(np.zeros(datasets['train'].num_rows						)      ,datasets['train']['label']						)
   A__       =     []
   # Iterate over them
   for i, (train_idxs, valid_idxs) in enumerate(UpperCAmelCase						):
      A__  ,			A__  ,			A__       =     get_fold_dataloaders(
          UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,)
      # Instantiate the model (we build the model here so that the seed also control new weights initialization)
      A__       =     AutoModelForSequenceClassification.from_pretrained('bert-base-cased'      ,return_dict=UpperCAmelCase						)

      # We could avoid this line since the accelerator is set with `device_placement=True` (default value).
      # Note that if you are placing tensors on devices manually, this line absolutely needs to be before the optimizer
      # creation otherwise training will not work on TPU (`accelerate` will kindly throw an error to make us aware of that).
      A__       =     model.to(accelerator.device						)

      # Instantiate optimizer
      A__       =     AdamW(params=model.parameters()      ,lr=UpperCAmelCase						)

      # Instantiate scheduler
      A__       =     get_linear_schedule_with_warmup(
          optimizer=UpperCAmelCase      ,num_warmup_steps=100      ,num_training_steps=(len(UpperCAmelCase						) * num_epochs) // gradient_accumulation_steps      ,)

      # Prepare everything
      # There is no specific order to remember, we just need to unpack the objects in the same order we gave them to the
      # prepare method.
      A__  ,			A__  ,			A__  ,			A__  ,			A__       =     accelerator.prepare(
          UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase      ,UpperCAmelCase						)

      # Now we train the model
      for epoch in range(UpperCAmelCase						):
         model.train()
         for step, batch in enumerate(UpperCAmelCase						):
            # We could avoid this line since we set the accelerator with `device_placement=True`.
            batch.to(accelerator.device						)
            A__       =     model(**UpperCAmelCase						)
            A__       =     outputs.loss
            A__       =     loss / gradient_accumulation_steps
            accelerator.backward(UpperCAmelCase						)
            if step % gradient_accumulation_steps == 0:
               optimizer.step()
               lr_scheduler.step()
               optimizer.zero_grad()

         model.eval()
         for step, batch in enumerate(UpperCAmelCase						):
            # We could avoid this line since we set the accelerator with `device_placement=True`.
            batch.to(accelerator.device						)
            with torch.no_grad():
               A__       =     model(**UpperCAmelCase						)
            A__       =     outputs.logits.argmax(dim=-1						)
            A__  ,			A__       =     accelerator.gather_for_metrics((predictions, batch['labels'])						)
            metric.add_batch(
                predictions=UpperCAmelCase      ,references=UpperCAmelCase      ,)

         A__       =     metric.compute()
         # Use accelerator.print to print only on the main process.
         accelerator.print(F"""epoch {epoch}:"""      ,UpperCAmelCase						)

      # New Code #
      # We also run predictions on the test set at the very end
      A__       =     []
      for step, batch in enumerate(UpperCAmelCase						):
         # We could avoid this line since we set the accelerator with `device_placement=True`.
         batch.to(accelerator.device						)
         with torch.no_grad():
            A__       =     model(**UpperCAmelCase						)
         A__       =     outputs.logits
         A__  ,			A__       =     accelerator.gather_for_metrics((predictions, batch['labels'])						)
         fold_predictions.append(predictions.cpu()						)
         if i == 0:
            # We need all of the test predictions
            test_references.append(references.cpu()						)
        # Use accelerator.print to print only on the main process.
      test_predictions.append(torch.cat(UpperCAmelCase      ,dim=0						)						)
      # We now need to release all our memory and get rid of the current model, optimizer, etc
      accelerator.free_memory()
   # New Code #
   # Finally we check the accuracy of our folded results:
   A__       =     torch.cat(UpperCAmelCase      ,dim=0						)
   A__       =     torch.stack(UpperCAmelCase      ,dim=0						).sum(dim=0						).div(int(args.num_folds						)						).argmax(dim=-1						)
   A__       =     metric.compute(predictions=UpperCAmelCase      ,references=UpperCAmelCase						)
   accelerator.print('Average test metrics from all folds:'      ,UpperCAmelCase						)







def 						_A      ( ):






   '''simple docstring'''







   A__       =     argparse.ArgumentParser(description='Simple example of training script.'						)
   parser.add_argument(
       '--mixed_precision'      ,type=UpperCAmelCase      ,default=UpperCAmelCase      ,choices=['no', 'fp16', 'bf16', 'fp8']      ,help='Whether to use mixed precision. Choose'
       'between fp16 and bf16 (bfloat16). Bf16 requires PyTorch >= 1.10.'
       'and an Nvidia Ampere GPU.'      ,)
   parser.add_argument('--cpu'      ,action='store_true'      ,help='If passed, will train on the CPU.'						)
   # New Code #
   parser.add_argument('--num_folds'      ,type=UpperCAmelCase      ,default=3      ,help='The number of splits to perform across the dataset'						)
   A__       =     parser.parse_args()
   A__       =     {'lr': 2e-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16}
   training_function(UpperCAmelCase      ,UpperCAmelCase						)


if __name__ == "__main__":
    main()


