

import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
    from transformers.trainer import DEFAULT_CALLBACKS

    from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel
class  _A		(  __magic_name__):

  def __init__(				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     []

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_init_end'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_train_begin'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_train_end'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_epoch_begin'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_epoch_end'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_step_begin'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_step_end'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_evaluate'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_predict'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_save'					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_log'					)



  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.events.append('on_prediction_step'					)

@require_torch
class  _A		(  unittest.TestCase):

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     tempfile.mkdtemp()

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        shutil.rmtree(self.output_dir					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE=0 ,					_SCREAMING_SNAKE_CASE=0 ,					_SCREAMING_SNAKE_CASE=64 ,					_SCREAMING_SNAKE_CASE=64 ,					_SCREAMING_SNAKE_CASE=None ,					_SCREAMING_SNAKE_CASE=False ,					**_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     RegressionDataset(length=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     RegressionDataset(length=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     RegressionModelConfig(a=_SCREAMING_SNAKE_CASE ,					b=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  List[str]           =     RegressionPreTrainedModel(_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  List[str]           =     TrainingArguments(self.output_dir ,					disable_tqdm=_SCREAMING_SNAKE_CASE ,					report_to=[] ,					**_SCREAMING_SNAKE_CASE					)
        return Trainer(
            _SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE ,					train_dataset=_SCREAMING_SNAKE_CASE ,					eval_dataset=_SCREAMING_SNAKE_CASE ,					callbacks=_SCREAMING_SNAKE_CASE ,					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        self.assertEqual(len(_SCREAMING_SNAKE_CASE					) ,					len(_SCREAMING_SNAKE_CASE					)					)

        # Order doesn't matter
        SCREAMING_SNAKE_CASE_				:  List[str]           =     sorted(_SCREAMING_SNAKE_CASE ,					key=lambda _SCREAMING_SNAKE_CASE					: cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					) else cb.__class__.__name__					)
        SCREAMING_SNAKE_CASE_				:  Any           =     sorted(_SCREAMING_SNAKE_CASE ,					key=lambda _SCREAMING_SNAKE_CASE					: cb.__name__ if isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					) else cb.__class__.__name__					)

        for cba, cba in zip(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
              if isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					) and isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
                    self.assertEqual(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
              elif isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					) and not isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
                    self.assertEqual(_SCREAMING_SNAKE_CASE ,					cba.__class__					)
              elif not isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					) and isinstance(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					):
                    self.assertEqual(cba.__class__ ,					_SCREAMING_SNAKE_CASE					)
              else:
                    self.assertEqual(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     ['on_init_end', 'on_train_begin']
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     0
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     len(trainer.get_eval_dataloader()					)
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     ['on_prediction_step'] * len(trainer.get_eval_dataloader()					) + ['on_log', 'on_evaluate']
        for _ in range(trainer.state.num_train_epochs					):
              expected_events.append('on_epoch_begin'					)
              for _ in range(_SCREAMING_SNAKE_CASE					):
                    step += 1
                    expected_events += ["on_step_begin", "on_step_end"]
                    if step % trainer.args.logging_steps == 0:
                          expected_events.append('on_log'					)
                    if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
                          expected_events += evaluation_events.copy()
                    if step % trainer.args.save_steps == 0:
                          expected_events.append('on_save'					)
              expected_events.append('on_epoch_end'					)
              if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
                    expected_events += evaluation_events.copy()
        expected_events += ["on_log", "on_train_end"]
        return expected_events

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  Dict           =     self.get_trainer()
        SCREAMING_SNAKE_CASE_				:  Tuple           =     DEFAULT_CALLBACKS.copy() + [ProgressCallback]
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        # Callbacks passed at init are added to the default callbacks
        SCREAMING_SNAKE_CASE_				:  int           =     self.get_trainer(callbacks=[MyTestTrainerCallback]					)
        expected_callbacks.append(_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     self.get_trainer(disable_tqdm=_SCREAMING_SNAKE_CASE					)
        SCREAMING_SNAKE_CASE_				:  str           =     DEFAULT_CALLBACKS.copy() + [PrinterCallback]
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        SCREAMING_SNAKE_CASE_				:  List[Any]           =     DEFAULT_CALLBACKS.copy() + [ProgressCallback]
        SCREAMING_SNAKE_CASE_				:  Optional[Any]           =     self.get_trainer()

        # We can add, pop, or remove by class name
        trainer.remove_callback(_SCREAMING_SNAKE_CASE					)
        expected_callbacks.remove(_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  str           =     self.get_trainer()
        SCREAMING_SNAKE_CASE_				:  Union[str, Any]           =     trainer.pop_callback(_SCREAMING_SNAKE_CASE					)
        self.assertEqual(cb.__class__ ,					_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        trainer.add_callback(_SCREAMING_SNAKE_CASE					)
        expected_callbacks.insert(0 ,					_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        # We can also add, pop, or remove by instance
        SCREAMING_SNAKE_CASE_				:  str           =     self.get_trainer()
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     trainer.callback_handler.callbacks[0]
        trainer.remove_callback(_SCREAMING_SNAKE_CASE					)
        expected_callbacks.remove(_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.get_trainer()
        SCREAMING_SNAKE_CASE_				:  Tuple           =     trainer.callback_handler.callbacks[0]
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     trainer.pop_callback(_SCREAMING_SNAKE_CASE					)
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)

        trainer.add_callback(_SCREAMING_SNAKE_CASE					)
        expected_callbacks.insert(0 ,					_SCREAMING_SNAKE_CASE					)
        self.check_callbacks_equality(trainer.callback_handler.callbacks ,					_SCREAMING_SNAKE_CASE					)



  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        import warnings

        # XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
        warnings.simplefilter(action='ignore' ,					category=_SCREAMING_SNAKE_CASE					)

        SCREAMING_SNAKE_CASE_				:  int           =     self.get_trainer(callbacks=[MyTestTrainerCallback]					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        # Independent log/save/eval
        SCREAMING_SNAKE_CASE_				:  Dict           =     self.get_trainer(callbacks=[MyTestTrainerCallback] ,					logging_steps=5					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  Any           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        SCREAMING_SNAKE_CASE_				:  Any           =     self.get_trainer(callbacks=[MyTestTrainerCallback] ,					save_steps=5					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  int           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.get_trainer(callbacks=[MyTestTrainerCallback] ,					eval_steps=5 ,					evaluation_strategy='steps'					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        SCREAMING_SNAKE_CASE_				:  List[Any]           =     self.get_trainer(callbacks=[MyTestTrainerCallback] ,					evaluation_strategy='epoch'					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  Optional[int]           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        # A bit of everything
        SCREAMING_SNAKE_CASE_				:  List[str]           =     self.get_trainer(
            callbacks=[MyTestTrainerCallback] ,					logging_steps=3 ,					save_steps=10 ,					eval_steps=5 ,					evaluation_strategy='steps' ,					)
        trainer.train()
        SCREAMING_SNAKE_CASE_				:  int           =     trainer.callback_handler.callbacks[-2].events
        self.assertEqual(_SCREAMING_SNAKE_CASE ,					self.get_expected_events(_SCREAMING_SNAKE_CASE					)					)

        # warning should be emitted for duplicated callbacks
        with patch('transformers.trainer_callback.logger.warning'					) as warn_mock:
              SCREAMING_SNAKE_CASE_				:  Dict           =     self.get_trainer(
                  callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] ,					)
              assert str(_SCREAMING_SNAKE_CASE					) in warn_mock.call_args[0][0]









import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, Features, Value
from .base import TaskTemplate
@dataclass(frozen=__magic_name__)
class  _A		(  __magic_name__):
  SCREAMING_SNAKE_CASE    :      str       =  field(default='''automatic-speech-recognition'''   ,     metadata={'''include_in_asdict_even_if_is_default''': True})
  SCREAMING_SNAKE_CASE    :      ClassVar[Features]       =  Features({'''audio''': Audio()})
  SCREAMING_SNAKE_CASE    :      ClassVar[Features]       =  Features({'''transcription''': Value('''string''')})
  SCREAMING_SNAKE_CASE    :      str       =  "audio"
  SCREAMING_SNAKE_CASE    :      str       =  "transcription"

  def  UpperCAmelCase    (				self ,					_SCREAMING_SNAKE_CASE					):





        """simple docstring"""







        if self.audio_column not in features:
              raise ValueError(f"Column {self.audio_column} is not present in features."					)
        if not isinstance(features[self.audio_column] ,					_SCREAMING_SNAKE_CASE					):
              raise ValueError(f"Column {self.audio_column} is not an Audio type."					)
        SCREAMING_SNAKE_CASE_				:  int           =     copy.deepcopy(self					)
        SCREAMING_SNAKE_CASE_				:  int           =     self.input_schema.copy()
        SCREAMING_SNAKE_CASE_				:  List[Any]           =     features[self.audio_column]
        SCREAMING_SNAKE_CASE_				:  List[str]           =     input_schema
        return task_template



  @property
  def  UpperCAmelCase    (				self					):





        """simple docstring"""







        return {self.audio_column: "audio", self.transcription_column: "transcription"}






