





'''simple docstring'''




import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
  from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
  import torch_xla.core.xla_model as xm






def        _lowerCAmelCase			(  __snake_case  : Any			)     ->     Union[str, Any]:
     if is_torch_version('<' ,							'2.0.0'			) or not hasattr(__snake_case ,							'_dynamo'			):
          return False
     return isinstance(__snake_case ,							torch._dynamo.eval_frame.OptimizedModule			)






def        _lowerCAmelCase			(  __snake_case  : Dict ,							__snake_case  : bool = True			)     ->     Tuple:
     __A						: Union[str, Any]      =       (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

     __A						: Tuple      =       is_compiled_module(__snake_case			)
     if is_compiled:
          __A						: int      =       model
          __A						: str      =       model._orig_mod

     if is_deepspeed_available():
          options += (DeepSpeedEngine,)

     while isinstance(__snake_case ,							__snake_case			):
          __A						: List[str]      =       model.module

     if not keep_fpaa_wrapper:
          __A						: Optional[Any]      =       getattr(__snake_case ,							'forward'			)
          __A						: Dict      =       model.__dict__.pop('_original_forward' ,							__snake_case			)
          if original_forward is not None:
               while hasattr(__snake_case ,							'__wrapped__'			):
                    __A						: Tuple      =       forward.__wrapped__
                    if forward == original_forward:
                         break
               __A						: Any      =       forward
          if getattr(__snake_case ,							'_converted_to_transformer_engine' ,							__snake_case			):
               convert_model(__snake_case ,							to_transformer_engine=__snake_case			)

     if is_compiled:
          __A						: Tuple      =       model
          __A						: List[Any]      =       compiled_model

     return model






def        _lowerCAmelCase			(  )     ->     List[Any]:
     PartialState().wait_for_everyone()






def        _lowerCAmelCase			(  __snake_case  : List[Any] ,							__snake_case  : List[str]			)     ->     Union[str, Any]:
     if PartialState().distributed_type == DistributedType.TPU:
          xm.save(__snake_case ,							__snake_case			)
     elif PartialState().local_process_index == 0:
          torch.save(__snake_case ,							__snake_case			)






@contextmanager
def        _lowerCAmelCase			(  **__snake_case  : Union[str, Any]			)     ->     Optional[Any]:
     for key, value in kwargs.items():
          __A						: Union[str, Any]      =       str(__snake_case			)

     yield

     for key in kwargs:
          if key.upper() in os.environ:
               del os.environ[key.upper()]






def        _lowerCAmelCase			(  __snake_case  : List[str]			)     ->     int:
     if not hasattr(__snake_case ,							'__qualname__'			) and not hasattr(__snake_case ,							'__name__'			):
          __A						: Optional[int]      =       getattr(__snake_case ,							'__class__' ,							__snake_case			)
     if hasattr(__snake_case ,							'__qualname__'			):
          return obj.__qualname__
     if hasattr(__snake_case ,							'__name__'			):
          return obj.__name__
     return str(__snake_case			)






def        _lowerCAmelCase			(  __snake_case  : Dict ,							__snake_case  : Tuple			)     ->     Optional[int]:
     for key, value in source.items():
          if isinstance(__snake_case ,							__snake_case			):
               __A						: str      =       destination.setdefault(__snake_case ,							{}			)
               merge_dicts(__snake_case ,							__snake_case			)
          else:
               __A						: Dict      =       value

     return destination







def        _lowerCAmelCase			(  __snake_case  : int = None			)     ->     bool:
     if port is None:
          __A						: Optional[Any]      =       2_95_00
     with socket.socket(socket.AF_INET ,							socket.SOCK_STREAM			) as s:
          return s.connect_ex(('localhost', port)			) == 0






'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast


@require_vision
class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Dict      =       tempfile.mkdtemp()

          __A						: Optional[int]      =       BlipImageProcessor()
          __A						: List[str]      =       BertTokenizer.from_pretrained('hf-internal-testing/tiny-random-BertModel')

          __A						: Any      =       BlipProcessor(_UpperCAmelCase						,	_UpperCAmelCase)

          processor.save_pretrained(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self						,	**_UpperCAmelCase):

          '''simple docstring'''






          return AutoProcessor.from_pretrained(self.tmpdirname						,	**_UpperCAmelCase).tokenizer


     def 							SCREAMING_SNAKE_CASE  (						self						,	**_UpperCAmelCase):

          '''simple docstring'''






          return AutoProcessor.from_pretrained(self.tmpdirname						,	**_UpperCAmelCase).image_processor


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          shutil.rmtree(self.tmpdirname)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       [np.random.randint(255						,	size=(3, 30, 400)						,	dtype=np.uinta)]

          __A						: Union[str, Any]      =       [Image.fromarray(np.moveaxis(_UpperCAmelCase						,	0						,	-1)) for x in image_inputs]

          return image_inputs


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       BlipProcessor(tokenizer=self.get_tokenizer()						,	image_processor=self.get_image_processor())
          processor.save_pretrained(self.tmpdirname)

          __A						: int      =       self.get_tokenizer(bos_token='(BOS)'						,	eos_token='(EOS)')
          __A						: List[Any]      =       self.get_image_processor(do_normalize=_UpperCAmelCase						,	padding_value=1.0)

          __A						: Tuple      =       BlipProcessor.from_pretrained(
              self.tmpdirname						,	bos_token='(BOS)'						,	eos_token='(EOS)'						,	do_normalize=_UpperCAmelCase						,	padding_value=1.0)

          self.assertEqual(processor.tokenizer.get_vocab()						,	tokenizer_add_kwargs.get_vocab())
          self.assertIsInstance(processor.tokenizer						,	_UpperCAmelCase)

          self.assertEqual(processor.image_processor.to_json_string()						,	image_processor_add_kwargs.to_json_string())
          self.assertIsInstance(processor.image_processor						,	_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[str]      =       self.get_image_processor()
          __A						: str      =       self.get_tokenizer()

          __A						: int      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: List[str]      =       self.prepare_image_inputs()

          __A						: str      =       image_processor(_UpperCAmelCase						,	return_tensors='np')
          __A						: str      =       processor(images=_UpperCAmelCase						,	return_tensors='np')

          for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()						,	input_processor[key].sum()						,	delta=1e-2)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.get_image_processor()
          __A						: Tuple      =       self.get_tokenizer()

          __A						: Union[str, Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: str      =       'lower newer'

          __A						: Dict      =       processor(text=_UpperCAmelCase)

          __A						: Tuple      =       tokenizer(_UpperCAmelCase						,	return_token_type_ids=_UpperCAmelCase)

          for key in encoded_tok.keys():
               self.assertListEqual(encoded_tok[key]						,	encoded_processor[key])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: List[Any]      =       self.get_image_processor()
          __A						: Optional[Any]      =       self.get_tokenizer()

          __A						: Optional[Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: Dict      =       'lower newer'
          __A						: int      =       self.prepare_image_inputs()

          __A						: List[Any]      =       processor(text=_UpperCAmelCase						,	images=_UpperCAmelCase)

          self.assertListEqual(list(inputs.keys())						,	['pixel_values', 'input_ids', 'attention_mask'])

          # test if it raises when no input is passed
          with pytest.raises(_UpperCAmelCase):
               processor()


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Union[str, Any]      =       self.get_image_processor()
          __A						: Optional[Any]      =       self.get_tokenizer()

          __A						: Union[str, Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: Tuple      =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          __A						: List[str]      =       processor.batch_decode(_UpperCAmelCase)
          __A						: List[str]      =       tokenizer.batch_decode(_UpperCAmelCase)

          self.assertListEqual(_UpperCAmelCase						,	_UpperCAmelCase)


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Any      =       self.get_image_processor()
          __A						: Union[str, Any]      =       self.get_tokenizer()

          __A						: Optional[Any]      =       BlipProcessor(tokenizer=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)

          __A						: str      =       'lower newer'
          __A						: str      =       self.prepare_image_inputs()

          __A						: Optional[int]      =       processor(text=_UpperCAmelCase						,	images=_UpperCAmelCase)

          # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
          self.assertListEqual(list(inputs.keys())						,	['pixel_values', 'input_ids', 'attention_mask'])