
'''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 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   def __init__(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  []







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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







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

      '''simple docstring'''







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




@require_torch
class 						lowercase__	(  unittest.TestCase   ):






   '''simple docstring'''







   def       UpperCAmelCase      (			self   ):

      '''simple docstring'''







      UpperCamelCase       =  tempfile.mkdtemp()







   def       UpperCAmelCase      (			self   ):

      '''simple docstring'''







      shutil.rmtree(self.output_dir   )







   def       UpperCAmelCase      (			self ,   lowerCamelCase__=0 ,   lowerCamelCase__=0 ,   lowerCamelCase__=6_4 ,   lowerCamelCase__=6_4 ,   lowerCamelCase__=None ,   lowerCamelCase__=False ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  RegressionDataset(length=lowerCamelCase__   )
      UpperCamelCase       =  RegressionDataset(length=lowerCamelCase__   )
      UpperCamelCase       =  RegressionModelConfig(a=lowerCamelCase__ ,   b=lowerCamelCase__   )
      UpperCamelCase       =  RegressionPreTrainedModel(lowerCamelCase__   )

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







   def       UpperCAmelCase      (			self ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      self.assertEqual(len(lowerCamelCase__   ) ,   len(lowerCamelCase__   )   )

      # Order doesn't matter
      UpperCamelCase       =  sorted(lowerCamelCase__ ,   key=lambda lowerCamelCase__   : cb.__name__ if isinstance(lowerCamelCase__ ,   lowerCamelCase__   ) else cb.__class__.__name__   )
      UpperCamelCase       =  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       UpperCAmelCase      (			self ,   lowerCamelCase__   ):

      '''simple docstring'''







      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(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       UpperCAmelCase      (			self   ):

      '''simple docstring'''







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

      # Callbacks passed at init are added to the default callbacks
      UpperCamelCase       =  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
      UpperCamelCase       =  self.get_trainer(disable_tqdm=lowerCamelCase__   )
      UpperCamelCase       =  DEFAULT_CALLBACKS.copy() + [PrinterCallback]
      self.check_callbacks_equality(trainer.callback_handler.callbacks ,   lowerCamelCase__   )







   def       UpperCAmelCase      (			self   ):

      '''simple docstring'''







      UpperCamelCase       =  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
      UpperCamelCase       =  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__   )

      UpperCamelCase       =  self.get_trainer()
      UpperCamelCase       =  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
      UpperCamelCase       =  self.get_trainer()
      UpperCamelCase       =  trainer.callback_handler.callbacks[0]
      trainer.remove_callback(lowerCamelCase__   )
      expected_callbacks.remove(lowerCamelCase__   )
      self.check_callbacks_equality(trainer.callback_handler.callbacks ,   lowerCamelCase__   )

      UpperCamelCase       =  self.get_trainer()
      UpperCamelCase       =  trainer.callback_handler.callbacks[0]
      UpperCamelCase       =  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       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=lowerCamelCase__   )

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

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

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

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

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

      # A bit of everything
      UpperCamelCase       =  self.get_trainer(
          callbacks=[MyTestTrainerCallback] ,   logging_steps=3 ,   save_steps=1_0 ,   eval_steps=5 ,   evaluation_strategy='''steps''' ,   )
      trainer.train()
      UpperCamelCase       =  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:
         UpperCamelCase       =  self.get_trainer(
             callbacks=[MyTestTrainerCallback, MyTestTrainerCallback] ,   )
         assert str(lowerCamelCase__   ) in warn_mock.call_args[0][0]







'''simple docstring'''


import os
from typing import List, Optional, Union

from ...image_processing_utils import BatchFeature
from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
from ..auto import AutoTokenizer


class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   _snake_case		   =							['''image_processor''', '''tokenizer''']
   _snake_case		   =							'''BlipImageProcessor'''
   _snake_case		   =							'''AutoTokenizer'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      super().__init__(lowerCamelCase__ ,   lowerCamelCase__   )

      # add QFormer tokenizer
      UpperCamelCase       =  qformer_tokenizer







   def __call__(			self ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = True ,   lowerCamelCase__ = False ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = 0 ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = True ,   lowerCamelCase__ = None ,   **lowerCamelCase__ ,   ):

      '''simple docstring'''







      if images is None and text is None:
         raise ValueError('''You have to specify at least images or text.'''   )

      UpperCamelCase       =  BatchFeature()

      if text is not None:
         UpperCamelCase       =  self.tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         encoding.update(lowerCamelCase__   )
         UpperCamelCase       =  self.qformer_tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         UpperCamelCase       =  qformer_text_encoding.pop('''input_ids'''   )
         UpperCamelCase       =  qformer_text_encoding.pop('''attention_mask'''   )

      if images is not None:
         UpperCamelCase       =  self.image_processor(lowerCamelCase__ ,   return_tensors=lowerCamelCase__   )
         encoding.update(lowerCamelCase__   )

      return encoding







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

      '''simple docstring'''







      return self.tokenizer.batch_decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







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

      '''simple docstring'''







      return self.tokenizer.decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







   @property
   # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.tokenizer.model_input_names
      UpperCamelCase       =  self.image_processor.model_input_names
      return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )







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

      '''simple docstring'''







      if os.path.isfile(lowerCamelCase__   ):
         raise ValueError(f'Provided path ({save_directory}) should be a directory, not a file'   )
      os.makedirs(lowerCamelCase__ ,   exist_ok=lowerCamelCase__   )
      UpperCamelCase       =  os.path.join(lowerCamelCase__ ,   '''qformer_tokenizer'''   )
      self.qformer_tokenizer.save_pretrained(lowerCamelCase__   )
      return super().save_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )







   @classmethod
   def       UpperCAmelCase				(			cls ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  AutoTokenizer.from_pretrained(lowerCamelCase__ ,   subfolder='''qformer_tokenizer'''   )
      UpperCamelCase       =  cls._get_arguments_from_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )
      args.append(lowerCamelCase__   )
      return cls(*lowerCamelCase__   )





