
'''simple docstring'''


import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp



def  __snake_case     (    ):
   UpperCamelCase       =  ArgumentParser(
       description=(
           '''PyTorch TPU distributed training launch '''
           '''helper utility that will spawn up '''
           '''multiple distributed processes'''
       ))

   # Optional arguments for the launch helper
   parser.add_argument('''--num_cores''',      type=_UpperCAmelCase,      default=1,      help='''Number of TPU cores to use (1 or 8).''')

   # positional
   parser.add_argument(
       '''training_script''',      type=_UpperCAmelCase,      help=(
           '''The full path to the single TPU training '''
           '''program/script to be launched in parallel, '''
           '''followed by all the arguments for the '''
           '''training script'''
       ),      )

   # rest from the training program
   parser.add_argument('''training_script_args''',      nargs=_UpperCAmelCase)

   return parser.parse_args()



def  __snake_case     (    ):
   UpperCamelCase       =  parse_args()

   # Import training_script as a module.
   UpperCamelCase       =  Path(args.training_script)
   sys.path.append(str(script_fpath.parent.resolve()))
   UpperCamelCase       =  script_fpath.stem
   UpperCamelCase       =  importlib.import_module(_UpperCAmelCase)

   # Patch sys.argv
   UpperCamelCase       =  [args.training_script] + args.training_script_args + ['''--tpu_num_cores''', str(args.num_cores)]
   xmp.spawn(mod._mp_fn,      args=(),      nprocs=args.num_cores)


if __name__ == "__main__":
   main()







'''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__   )





