




'''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       A   (		_a   ):







							def __init__(  self	:  Tuple )      ->							str:


														"""simple docstring"""


														_a     					=    []







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Optional[int]							,      **lowerCAmelCase_	:  List[Any] )      ->							int:


														"""simple docstring"""


														self.events.append('''on_init_end''' )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Union[str, Any]							,      **lowerCAmelCase_	:  Any )      ->							Optional[int]:


														"""simple docstring"""


														self.events.append('''on_train_begin''' )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  Union[str, Any]							,      **lowerCAmelCase_	:  Dict )      ->							Tuple:


														"""simple docstring"""


														self.events.append('''on_train_end''' )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  List[Any]							,      **lowerCAmelCase_	:  Optional[Any] )      ->							Union[str, Any]:


														"""simple docstring"""


														self.events.append('''on_epoch_begin''' )







							def        __lowerCAmelCase      (  self	:  Union[str, Any]							,      lowerCAmelCase_	:  Union[str, Any]							,      lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  int							,      **lowerCAmelCase_	:  int )      ->							str:


														"""simple docstring"""


														self.events.append('''on_epoch_end''' )







							def        __lowerCAmelCase      (  self	:  List[Any]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      **lowerCAmelCase_	:  Union[str, Any] )      ->							Optional[Any]:


														"""simple docstring"""


														self.events.append('''on_step_begin''' )







							def        __lowerCAmelCase      (  self	:  str							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str]							,      **lowerCAmelCase_	:  Dict )      ->							Any:


														"""simple docstring"""


														self.events.append('''on_step_end''' )







							def        __lowerCAmelCase      (  self	:  Optional[int]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  str							,      **lowerCAmelCase_	:  Any )      ->							int:


														"""simple docstring"""


														self.events.append('''on_evaluate''' )







							def        __lowerCAmelCase      (  self	:  List[str]							,      lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Optional[Any]							,      **lowerCAmelCase_	:  Any )      ->							int:


														"""simple docstring"""


														self.events.append('''on_predict''' )







							def        __lowerCAmelCase      (  self	:  Optional[int]							,      lowerCAmelCase_	:  Optional[int]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  List[str]							,      **lowerCAmelCase_	:  Optional[Any] )      ->							Optional[Any]:


														"""simple docstring"""


														self.events.append('''on_save''' )







							def        __lowerCAmelCase      (  self	:  Optional[int]							,      lowerCAmelCase_	:  List[str]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Dict							,      **lowerCAmelCase_	:  int )      ->							Optional[int]:


														"""simple docstring"""


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







							def        __lowerCAmelCase      (  self	:  Union[str, Any]							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Union[str, Any]							,      **lowerCAmelCase_	:  List[Any] )      ->							Union[str, Any]:


														"""simple docstring"""


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



@require_torch
class       A   (		unittest.TestCase   ):







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Tuple:


														"""simple docstring"""


														_a     					=    tempfile.mkdtemp()







							def        __lowerCAmelCase      (  self	:  List[Any] )      ->							Union[str, Any]:


														"""simple docstring"""


														shutil.rmtree(self.output_dir )







							def        __lowerCAmelCase      (  self	:  Union[str, Any]							,      lowerCAmelCase_	:  Dict=0							,      lowerCAmelCase_	:  Tuple=0							,      lowerCAmelCase_	:  Optional[Any]=64							,      lowerCAmelCase_	:  Optional[Any]=64							,      lowerCAmelCase_	:  Optional[Any]=None							,      lowerCAmelCase_	:  Optional[int]=False							,      **lowerCAmelCase_	:  List[str] )      ->							Optional[Any]:


														"""simple docstring"""


														_a     					=    RegressionDataset(length=lowerCAmelCase_ )
														_a     					=    RegressionDataset(length=lowerCAmelCase_ )
														_a     					=    RegressionModelConfig(a=lowerCAmelCase_							,      b=lowerCAmelCase_ )
														_a     					=    RegressionPreTrainedModel(lowerCAmelCase_ )

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







							def        __lowerCAmelCase      (  self	:  Tuple							,      lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  List[str] )      ->							Dict:


														"""simple docstring"""


														self.assertEqual(len(lowerCAmelCase_ )							,      len(lowerCAmelCase_ ) )

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

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







							def        __lowerCAmelCase      (  self	:  Dict							,      lowerCAmelCase_	:  List[Any] )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    ['''on_init_end''', '''on_train_begin''']
														_a     					=    0
														_a     					=    len(trainer.get_eval_dataloader() )
														_a     					=    ['''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(lowerCAmelCase_ ):
																												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        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Union[str, Any]:


														"""simple docstring"""


														_a     					=    self.get_trainer()
														_a     					=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
														self.check_callbacks_equality(trainer.callback_handler.callbacks							,      lowerCAmelCase_ )

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

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







							def        __lowerCAmelCase      (  self	:  Optional[Any] )      ->							Dict:


														"""simple docstring"""


														_a     					=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
														_a     					=    self.get_trainer()

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

														_a     					=    self.get_trainer()
														_a     					=    trainer.pop_callback(lowerCAmelCase_ )
														self.assertEqual(cb.__class__							,      lowerCAmelCase_ )
														self.check_callbacks_equality(trainer.callback_handler.callbacks							,      lowerCAmelCase_ )

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

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

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

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







							def        __lowerCAmelCase      (  self	:  Optional[int] )      ->							List[str]:


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

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

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

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

														_a     					=    self.get_trainer(callbacks=[MyTestTrainerCallback]							,      eval_steps=5							,      evaluation_strategy='''steps''' )
														trainer.train()
														_a     					=    trainer.callback_handler.callbacks[-2].events
														self.assertEqual(lowerCAmelCase_							,      self.get_expected_events(lowerCAmelCase_ ) )

														_a     					=    self.get_trainer(callbacks=[MyTestTrainerCallback]							,      evaluation_strategy='''epoch''' )
														trainer.train()
														_a     					=    trainer.callback_handler.callbacks[-2].events
														self.assertEqual(lowerCAmelCase_							,      self.get_expected_events(lowerCAmelCase_ ) )

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

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







'''simple docstring'''


from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import numpy as np
import PIL
from PIL import Image

from ...utils import BaseOutput, is_torch_available, is_transformers_available

@dataclass
class       A   (		_a   ):
							lowercase_          =    42
							lowercase_          =    42


if is_transformers_available() and is_torch_available():
				from .pipeline_semantic_stable_diffusion import SemanticStableDiffusionPipeline

