



'''simple docstring'''






import argparse

import evaluate
import torch
from datasets import load_dataset
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
#
# 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 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:      Union[str, Any]		 =		1_6
lowerCAmelCase:      List[str]		 =		3_2
def    lowerCamelCase__ (		_A				,    _A = 16       ):
  a		:      Optional[int]       =  AutoTokenizer.from_pretrained('bert-base-cased'       )
  a		:      Dict       =  load_dataset('glue'				,    'mrpc'       )

  def tokenize_function(_A       ):
    # max_length=None => use the model max length (it's actually the default)
    a		:      Union[str, Any]       =  tokenizer(examples['sentence1']				,    examples['sentence2']				,    truncation=_A				,    max_length=_A       )
    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		:      Optional[Any]       =  datasets.map(
        _A				,    batched=_A				,    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		:      str       =  tokenized_datasets.rename_column('label'				,    'labels'       )

  def collate_fn(_A       ):
    # On TPU it's best to pad everything to the same length or training will be very slow.
    a		:      Optional[Any]       =  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		:      Tuple       =  16
    elif accelerator.mixed_precision != "no":
      a		:      int       =  8
    else:
      a		:      Any       =  None

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

  # Instantiate dataloaders.
  a		:      str       =  DataLoader(
      tokenized_datasets['train']				,    shuffle=_A				,    collate_fn=_A				,    batch_size=_A				,    drop_last=_A       )
  a		:      Tuple       =  DataLoader(
      tokenized_datasets['validation']				,    shuffle=_A				,    collate_fn=_A				,    batch_size=_A				,    drop_last=(accelerator.mixed_precision == 'fp8')				,    )

  return train_dataloader, eval_dataloader
def    lowerCamelCase__ (		_A				,    _A       ):
  # Initialize accelerator
  a		:      str       =  Accelerator(cpu=args.cpu				,    mixed_precision=args.mixed_precision       )
  # Sample hyper-parameters for learning rate, batch size, seed and a few other HPs
  a		:      Optional[Any]       =  config['lr']
  a		:      Tuple       =  int(config['num_epochs']       )
  a		:      Optional[Any]       =  int(config['seed']       )
  a		:      str       =  int(config['batch_size']       )

  a		:      str       =  evaluate.load('glue'				,    'mrpc'       )

  # If the batch size is too big we use gradient accumulation
  a		:      Dict       =  1
  if batch_size > MAX_GPU_BATCH_SIZE and accelerator.distributed_type != DistributedType.TPU:
    a		:      List[Any]       =  batch_size // MAX_GPU_BATCH_SIZE
    a		:      List[Any]       =  MAX_GPU_BATCH_SIZE

  set_seed(_A       )
  a     ,			a		:      int       =  get_dataloaders(_A				,    _A       )
  # Instantiate the model (we build the model here so that the seed also control new weights initialization)
  a		:      Union[str, Any]       =  AutoModelForSequenceClassification.from_pretrained('bert-base-cased'				,    return_dict=_A       )

  # 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		:      Optional[Any]       =  model.to(accelerator.device       )
  # Instantiate optimizer
  a		:      str       =  AdamW(params=model.parameters()				,    lr=_A       )

  # Instantiate scheduler
  a		:      List[str]       =  get_linear_schedule_with_warmup(
      optimizer=_A				,    num_warmup_steps=100				,    num_training_steps=(len(_A       ) * 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		:      int       =  accelerator.prepare(
      _A				,    _A				,    _A				,    _A				,    _A       )

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

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

    a		:      Union[str, Any]       =  metric.compute()
    # Use accelerator.print to print only on the main process.
    accelerator.print(f"""epoch {epoch}:"""				,    _A       )
def    lowerCamelCase__ (		):
  a		:      int       =  argparse.ArgumentParser(description='Simple example of training script.'       )
  parser.add_argument(
      '--mixed_precision'				,    type=_A				,    default=_A				,    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.'       )
  a		:      List[Any]       =  parser.parse_args()
  a		:      Optional[int]       =  {'lr': 2E-5, 'num_epochs': 3, 'seed': 42, 'batch_size': 16}
  training_function(_A				,    _A       )


if __name__ == "__main__":
     main()




'''simple docstring'''






from collections import UserDict
from typing import Union

import numpy as np
import requests

from ..utils import (
    add_end_docstrings,
    logging,
)
from .audio_classification import ffmpeg_read
from .base import PIPELINE_INIT_ARGS, Pipeline


lowerCAmelCase:      Optional[Any]		 =		logging.get_logger(__name__)

@add_end_docstrings(lowerCamelCase__			)
class 							a__(      lowerCamelCase__			):







    def __init__(   self				:						Union[str, Any]		,    **__snake_case				:						List[str]			):
      super().__init__(**__snake_case			)

      if self.framework != "pt":
        raise ValueError(F"""The {self.__class__} is only available in PyTorch."""			)
      # No specific FOR_XXX available yet







    def __call__(   self				:						Any		,    __snake_case				:						Union[np.ndarray, bytes, str]		,    **__snake_case				:						Optional[Any]			):
      return super().__call__(__snake_case		,    **__snake_case			)







    def     lowercase_ (   self				:						Dict		,    **__snake_case				:						Optional[int]			):
      a		:      str       =  {}
      if "candidate_labels" in kwargs:
        a		:      Optional[int]       =  kwargs['candidate_labels']
      if "hypothesis_template" in kwargs:
        a		:      Any       =  kwargs['hypothesis_template']

      return preprocess_params, {}, {}







    def     lowercase_ (   self				:						int		,    __snake_case				:						Optional[Any]		,    __snake_case				:						List[Any]=None		,    __snake_case				:						List[str]="This is a sound of {}."			):
      if isinstance(__snake_case		,    __snake_case			):
        if audio.startswith('http://'			) or audio.startswith('https://'			):
          # We need to actually check for a real protocol, otherwise it's impossible to use a local file
          # like http_huggingface_co.png
          a		:      List[Any]       =  requests.get(__snake_case			).content
        else:
          with open(__snake_case		,    'rb'			) as f:
            a		:      List[str]       =  f.read()

      if isinstance(__snake_case		,    __snake_case			):
        a		:      Optional[Any]       =  ffmpeg_read(__snake_case		,    self.feature_extractor.sampling_rate			)

      if not isinstance(__snake_case		,    np.ndarray			):
        raise ValueError('We expect a numpy ndarray as input'			)
      if len(audio.shape			) != 1:
        raise ValueError('We expect a single channel audio input for ZeroShotAudioClassificationPipeline'			)

      a		:      str       =  self.feature_extractor(
          [audio]		,    sampling_rate=self.feature_extractor.sampling_rate		,    return_tensors='pt'			)
      a		:      Tuple       =  candidate_labels
      a		:      Union[str, Any]       =  [hypothesis_template.format(__snake_case			) for x in candidate_labels]
      a		:      int       =  self.tokenizer(__snake_case		,    return_tensors=self.framework		,    padding=__snake_case			)
      a		:      Any       =  [text_inputs]
      return inputs







    def     lowercase_ (   self				:						Optional[Any]		,    __snake_case				:						str			):
      a		:      Any       =  model_inputs.pop('candidate_labels'			)
      a		:      List[Any]       =  model_inputs.pop('text_inputs'			)
      if isinstance(text_inputs[0]		,    __snake_case			):
        a		:      str       =  text_inputs[0]
      else:
        # Batching case.
        a		:      Dict       =  text_inputs[0][0]

      a		:      List[Any]       =  self.model(**__snake_case		,    **__snake_case			)

      a		:      Any       =  {
          'candidate_labels': candidate_labels,
          'logits': outputs.logits_per_audio,
      }
      return model_outputs







    def     lowercase_ (   self				:						Union[str, Any]		,    __snake_case				:						str			):
      a		:      Optional[int]       =  model_outputs.pop('candidate_labels'			)
      a		:      List[str]       =  model_outputs['logits'][0]

      if self.framework == "pt":
        a		:      str       =  logits.softmax(dim=0			)
        a		:      List[Any]       =  probs.tolist()
      else:
        raise ValueError('`tf` framework not supported.'			)

      a		:      Dict       =  [
          {'score': score, 'label': candidate_label}
          for score, candidate_label in sorted(zip(__snake_case		,    __snake_case			)		,    key=lambda __snake_case			: -x[0]			)
      ]
      return result