
"""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       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       def __init__(  self							)  ->						Any:





         """simple docstring"""




         _lowercase:      str   			=  []
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Tuple:





         """simple docstring"""




         self.events.append('''on_init_end'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[Any]:





         """simple docstring"""




         self.events.append('''on_train_begin'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						List[str]:





         """simple docstring"""




         self.events.append('''on_train_end'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						List[Any]:





         """simple docstring"""




         self.events.append('''on_epoch_begin'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						List[Any]:





         """simple docstring"""




         self.events.append('''on_epoch_end'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[int]:





         """simple docstring"""




         self.events.append('''on_step_begin'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[Any]:





         """simple docstring"""




         self.events.append('''on_step_end'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Tuple:





         """simple docstring"""




         self.events.append('''on_evaluate'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Tuple:





         """simple docstring"""




         self.events.append('''on_predict'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[Any]:





         """simple docstring"""




         self.events.append('''on_save'''							)
       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[int]:





         """simple docstring"""




         self.events.append('''on_log'''							)



       def 				lowercase_				(  self			,					A_			,					A_			,					A_			,					**A_							)  ->						Optional[int]:





         """simple docstring"""




         self.events.append('''on_prediction_step'''							)


@require_torch
class       __magic_name__							(							unittest.TestCase			):
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         _lowercase:      Tuple   			=  tempfile.mkdtemp()
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         shutil.rmtree(self.output_dir							)
       def 				lowercase_				(  self			,					A_=0			,					A_=0			,					A_=64			,					A_=64			,					A_=None			,					A_=False			,					**A_							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Tuple   			=  RegressionDataset(length=A_							)
         _lowercase:      List[str]   			=  RegressionDataset(length=A_							)
         _lowercase:      Any   			=  RegressionModelConfig(a=A_			,					b=A_							)
         _lowercase:      Union[str, Any]   			=  RegressionPreTrainedModel(A_							)

         _lowercase:      str   			=  TrainingArguments(self.output_dir			,					disable_tqdm=A_			,					report_to=[]			,					**A_							)
         return Trainer(
             A_			,					A_			,					train_dataset=A_			,					eval_dataset=A_			,					callbacks=A_			,					)
       def 				lowercase_				(  self			,					A_			,					A_							)  ->						int:





         """simple docstring"""




         self.assertEqual(len(A_							)			,					len(A_							)							)

         # Order doesn't matter
         _lowercase:      Any   			=  sorted(A_			,					key=lambda A_							: cb.__name__ if isinstance(A_			,					A_							) else cb.__class__.__name__							)
         _lowercase:      Optional[Any]   			=  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 				lowercase_				(  self			,					A_							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Tuple   			=  ['''on_init_end''', '''on_train_begin''']
         _lowercase:      Dict   			=  0
         _lowercase:      Optional[int]   			=  len(trainer.get_eval_dataloader()							)
         _lowercase:      str   			=  ['''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 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Union[str, Any]   			=  self.get_trainer()
         _lowercase:      Union[str, Any]   			=  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
         self.check_callbacks_equality(trainer.callback_handler.callbacks			,					A_							)

         # Callbacks passed at init are added to the default callbacks
         _lowercase:      Any   			=  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
         _lowercase:      str   			=  self.get_trainer(disable_tqdm=A_							)
         _lowercase:      List[str]   			=  DEFAULT_CALLBACKS.copy() + [PrinterCallback]
         self.check_callbacks_equality(trainer.callback_handler.callbacks			,					A_							)
       def 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      Tuple   			=  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
         _lowercase:      Dict   			=  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_							)

         _lowercase:      int   			=  self.get_trainer()
         _lowercase:      Optional[Any]   			=  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
         _lowercase:      Optional[int]   			=  self.get_trainer()
         _lowercase:      Tuple   			=  trainer.callback_handler.callbacks[0]
         trainer.remove_callback(A_							)
         expected_callbacks.remove(A_							)
         self.check_callbacks_equality(trainer.callback_handler.callbacks			,					A_							)

         _lowercase:      Union[str, Any]   			=  self.get_trainer()
         _lowercase:      Optional[int]   			=  trainer.callback_handler.callbacks[0]
         _lowercase:      int   			=  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 				lowercase_				(  self							)  ->						Tuple:





         """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=A_							)

         _lowercase:      List[Any]   			=  self.get_trainer(callbacks=[MyTestTrainerCallback]							)
         trainer.train()
         _lowercase:      List[str]   			=  trainer.callback_handler.callbacks[-2].events
         self.assertEqual(A_			,					self.get_expected_events(A_							)							)

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

         _lowercase:      str   			=  self.get_trainer(callbacks=[MyTestTrainerCallback]			,					save_steps=5							)
         trainer.train()
         _lowercase:      Optional[Any]   			=  trainer.callback_handler.callbacks[-2].events
         self.assertEqual(A_			,					self.get_expected_events(A_							)							)

         _lowercase:      int   			=  self.get_trainer(callbacks=[MyTestTrainerCallback]			,					eval_steps=5			,					evaluation_strategy='''steps'''							)
         trainer.train()
         _lowercase:      Tuple   			=  trainer.callback_handler.callbacks[-2].events
         self.assertEqual(A_			,					self.get_expected_events(A_							)							)

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

         # A bit of everything
         _lowercase:      List[Any]   			=  self.get_trainer(
             callbacks=[MyTestTrainerCallback]			,					logging_steps=3			,					save_steps=10			,					eval_steps=5			,					evaluation_strategy='''steps'''			,					)
         trainer.train()
         _lowercase:      int   			=  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:
           _lowercase:      Dict   			=  self.get_trainer(
               callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]			,					)
           assert str(A_							) in warn_mock.call_args[0][0]


"""simple docstring"""




import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import DonutImageProcessor


class       __magic_name__							(							unittest.TestCase			):
       def __init__(  self			,					A_			,					A_=7			,					A_=3			,					A_=18			,					A_=30			,					A_=400			,					A_=True			,					A_=None			,					A_=True			,					A_=False			,					A_=True			,					A_=True			,					A_=[0.5, 0.5, 0.5]			,					A_=[0.5, 0.5, 0.5]			,					)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  parent
         _lowercase:      str   			=  batch_size
         _lowercase:      Any   			=  num_channels
         _lowercase:      Any   			=  image_size
         _lowercase:      Any   			=  min_resolution
         _lowercase:      int   			=  max_resolution
         _lowercase:      Union[str, Any]   			=  do_resize
         _lowercase:      List[Any]   			=  size if size is not None else {'''height''': 18, '''width''': 20}
         _lowercase:      Optional[int]   			=  do_thumbnail
         _lowercase:      List[str]   			=  do_align_axis
         _lowercase:      int   			=  do_pad
         _lowercase:      Optional[Any]   			=  do_normalize
         _lowercase:      List[Any]   			=  image_mean
         _lowercase:      Union[str, Any]   			=  image_std



       def 				lowercase_				(  self							)  ->						List[str]:





         """simple docstring"""




         return {
             "do_resize": self.do_resize,
             "size": self.size,
             "do_thumbnail": self.do_thumbnail,
             "do_align_long_axis": self.do_align_axis,
             "do_pad": self.do_pad,
             "do_normalize": self.do_normalize,
             "image_mean": self.image_mean,
             "image_std": self.image_std,
         }


@require_torch
@require_vision
class       __magic_name__							(							SCREAMING_SNAKE_CASE__ ,       unittest.TestCase			):
       UpperCamelCase_ =		DonutImageProcessor if is_vision_available() else None
       def 				lowercase_				(  self							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      str   			=  DonutImageProcessingTester(self							)
       @property
       def 				lowercase_				(  self							)  ->						int:





         """simple docstring"""




         return self.image_processor_tester.prepare_image_processor_dict()
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self.image_processing_class(**self.image_processor_dict							)
         self.assertTrue(hasattr(A_			,					'''do_resize'''							)							)
         self.assertTrue(hasattr(A_			,					'''size'''							)							)
         self.assertTrue(hasattr(A_			,					'''do_thumbnail'''							)							)
         self.assertTrue(hasattr(A_			,					'''do_align_long_axis'''							)							)
         self.assertTrue(hasattr(A_			,					'''do_pad'''							)							)
         self.assertTrue(hasattr(A_			,					'''do_normalize'''							)							)
         self.assertTrue(hasattr(A_			,					'''image_mean'''							)							)
         self.assertTrue(hasattr(A_			,					'''image_std'''							)							)
       def 				lowercase_				(  self							)  ->						List[Any]:





         """simple docstring"""




         _lowercase:      Optional[Any]   			=  self.image_processing_class.from_dict(self.image_processor_dict							)
         self.assertEqual(image_processor.size			,					{'''height''': 18, '''width''': 20}							)

         _lowercase:      List[str]   			=  self.image_processing_class.from_dict(self.image_processor_dict			,					size=42							)
         self.assertEqual(image_processor.size			,					{'''height''': 42, '''width''': 42}							)

         # Previous config had dimensions in (width, height) order
         _lowercase:      Any   			=  self.image_processing_class.from_dict(self.image_processor_dict			,					size=(42, 84)							)
         self.assertEqual(image_processor.size			,					{'''height''': 84, '''width''': 42}							)
       def 				lowercase_				(  self							)  ->						Union[str, Any]:





         """simple docstring"""




         pass
       @is_flaky()
       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         _lowercase:      str   			=  self.image_processing_class(**self.image_processor_dict							)
         # create random PIL images
         _lowercase:      Dict   			=  prepare_image_inputs(self.image_processor_tester			,					equal_resolution=A_							)
         for image in image_inputs:
           self.assertIsInstance(A_			,					Image.Image							)

         # Test not batched input
         _lowercase:      Tuple   			=  image_processing(image_inputs[0]			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)

         # Test batched
         _lowercase:      List[Any]   			=  image_processing(A_			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)
       @is_flaky()
       def 				lowercase_				(  self							)  ->						Any:





         """simple docstring"""




         _lowercase:      str   			=  self.image_processing_class(**self.image_processor_dict							)
         # create random numpy tensors
         _lowercase:      str   			=  prepare_image_inputs(self.image_processor_tester			,					equal_resolution=A_			,					numpify=A_							)
         for image in image_inputs:
           self.assertIsInstance(A_			,					np.ndarray							)

         # Test not batched input
         _lowercase:      Union[str, Any]   			=  image_processing(image_inputs[0]			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)

         # Test batched
         _lowercase:      Dict   			=  image_processing(A_			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)



       @is_flaky()
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         _lowercase:      List[Any]   			=  self.image_processing_class(**self.image_processor_dict							)
         # create random PyTorch tensors
         _lowercase:      List[Any]   			=  prepare_image_inputs(self.image_processor_tester			,					equal_resolution=A_			,					torchify=A_							)
         for image in image_inputs:
           self.assertIsInstance(A_			,					torch.Tensor							)

         # Test not batched input
         _lowercase:      Union[str, Any]   			=  image_processing(image_inputs[0]			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 1,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)

         # Test batched
         _lowercase:      Dict   			=  image_processing(A_			,					return_tensors='''pt'''							).pixel_values
         self.assertEqual(
             encoded_images.shape			,					(
                 self.image_processor_tester.batch_size,
                 self.image_processor_tester.num_channels,
                 self.image_processor_tester.size['''height'''],
                 self.image_processor_tester.size['''width'''],
             )			,					)
