






"""simple docstring"""







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  lowerCamelCase__ (	snake_case			):






    def __init__(			self ):
          UpperCAmelCase          =	[]






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_init_end""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_train_begin""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_train_end""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_epoch_begin""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_epoch_end""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_step_begin""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_step_end""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_evaluate""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_predict""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_save""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_log""" )






    def     _UpperCamelCase							(			self     ,A     ,A     ,A     ,**A ):
          self.events.append("""on_prediction_step""" )







@require_torch
class  lowerCamelCase__ (	unittest.TestCase			):






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	tempfile.mkdtemp()






    def     _UpperCamelCase							(			self ):
          shutil.rmtree(self.output_dir )






    def     _UpperCamelCase							(			self     ,A=0     ,A=0     ,A=64     ,A=64     ,A=None     ,A=False     ,**A ):
          # disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
          # its set to False since the tests later on depend on its value.
          UpperCAmelCase          =	RegressionDataset(length=A )
          UpperCAmelCase          =	RegressionDataset(length=A )
          UpperCAmelCase          =	RegressionModelConfig(a=A     ,b=A )
          UpperCAmelCase          =	RegressionPreTrainedModel(A )

          UpperCAmelCase          =	TrainingArguments(self.output_dir     ,disable_tqdm=A     ,report_to=[]     ,**A )
          return Trainer(
              A     ,A     ,train_dataset=A     ,eval_dataset=A     ,callbacks=A     ,)






    def     _UpperCamelCase							(			self     ,A     ,A ):
          self.assertEqual(len(A )     ,len(A ) )

          # Order doesn't matter
          UpperCAmelCase          =	sorted(A     ,key=lambda A : cb.__name__ if isinstance(A     ,A ) else cb.__class__.__name__ )
          UpperCAmelCase          =	sorted(A     ,key=lambda A : cb.__name__ if isinstance(A     ,A ) else cb.__class__.__name__ )

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






    def     _UpperCamelCase							(			self     ,A ):
          UpperCAmelCase          =	["""on_init_end""", """on_train_begin"""]
          UpperCAmelCase          =	0
          UpperCAmelCase          =	len(trainer.get_eval_dataloader() )
          UpperCAmelCase          =	["""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(A ):
                      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 ):
          UpperCAmelCase          =	self.get_trainer()
          UpperCAmelCase          =	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
          self.check_callbacks_equality(trainer.callback_handler.callbacks     ,A )

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

          # TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
          UpperCAmelCase          =	self.get_trainer(disable_tqdm=A )
          UpperCAmelCase          =	DEFAULT_CALLBACKS.copy() + [PrinterCallback]
          self.check_callbacks_equality(trainer.callback_handler.callbacks     ,A )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	DEFAULT_CALLBACKS.copy() + [ProgressCallback]
          UpperCAmelCase          =	self.get_trainer()

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

          UpperCAmelCase          =	self.get_trainer()
          UpperCAmelCase          =	trainer.pop_callback(A )
          self.assertEqual(cb.__class__     ,A )
          self.check_callbacks_equality(trainer.callback_handler.callbacks     ,A )

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

          # We can also add, pop, or remove by instance
          UpperCAmelCase          =	self.get_trainer()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[0]
          trainer.remove_callback(A )
          expected_callbacks.remove(A )
          self.check_callbacks_equality(trainer.callback_handler.callbacks     ,A )

          UpperCAmelCase          =	self.get_trainer()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[0]
          UpperCAmelCase          =	trainer.pop_callback(A )
          self.assertEqual(A     ,A )
          self.check_callbacks_equality(trainer.callback_handler.callbacks     ,A )

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






    def     _UpperCamelCase							(			self ):
          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=A )

          UpperCAmelCase          =	self.get_trainer(callbacks=[MyTestTrainerCallback] )
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

          # Independent log/save/eval
          UpperCAmelCase          =	self.get_trainer(callbacks=[MyTestTrainerCallback]     ,logging_steps=5 )
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

          UpperCAmelCase          =	self.get_trainer(callbacks=[MyTestTrainerCallback]     ,save_steps=5 )
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

          UpperCAmelCase          =	self.get_trainer(callbacks=[MyTestTrainerCallback]     ,eval_steps=5     ,evaluation_strategy="""steps""" )
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

          UpperCAmelCase          =	self.get_trainer(callbacks=[MyTestTrainerCallback]     ,evaluation_strategy="""epoch""" )
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

          # A bit of everything
          UpperCAmelCase          =	self.get_trainer(
              callbacks=[MyTestTrainerCallback]     ,logging_steps=3     ,save_steps=10     ,eval_steps=5     ,evaluation_strategy="""steps"""     ,)
          trainer.train()
          UpperCAmelCase          =	trainer.callback_handler.callbacks[-2].events
          self.assertEqual(A     ,self.get_expected_events(A ) )

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











"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_UpperCamelCase					      =  {"""configuration_mbart""": ["""MBART_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MBartConfig""", """MBartOnnxConfig"""]}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  ["""MBartTokenizer"""]

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  ["""MBartTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """MBART_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """MBartForCausalLM""",
      """MBartForConditionalGeneration""",
      """MBartForQuestionAnswering""",
      """MBartForSequenceClassification""",
      """MBartModel""",
      """MBartPreTrainedModel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """TFMBartForConditionalGeneration""",
      """TFMBartModel""",
      """TFMBartPreTrainedModel""",
  ]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _UpperCamelCase					      =  [
      """FlaxMBartForConditionalGeneration""",
      """FlaxMBartForQuestionAnswering""",
      """FlaxMBartForSequenceClassification""",
      """FlaxMBartModel""",
      """FlaxMBartPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_mbart import MBART_PRETRAINED_CONFIG_ARCHIVE_MAP, MBartConfig, MBartOnnxConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_mbart import MBartTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_mbart_fast import MBartTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_mbart import (
        MBART_PRETRAINED_MODEL_ARCHIVE_LIST,
        MBartForCausalLM,
        MBartForConditionalGeneration,
        MBartForQuestionAnswering,
        MBartForSequenceClassification,
        MBartModel,
        MBartPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_mbart import TFMBartForConditionalGeneration, TFMBartModel, TFMBartPreTrainedModel

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_mbart import (
        FlaxMBartForConditionalGeneration,
        FlaxMBartForQuestionAnswering,
        FlaxMBartForSequenceClassification,
        FlaxMBartModel,
        FlaxMBartPreTrainedModel,
    )

else:
  import sys

  _UpperCamelCase					      =  _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



