


'''simple docstring'''

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
       import torch

       from transformers import BlipTextModel
       from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST







class 			UpperCAmelCase			:

 def __init__(			self			:						Any,       a_			:						Optional[Any],       a_			:						Any=12,       a_			:						Union[str, Any]=7,       a_			:						int=True,       a_			:						Dict=True,       a_			:						Any=True,       a_			:						Tuple=99,       a_			:						Tuple=32,       a_			:						Any=32,       a_			:						Optional[Any]=5,       a_			:						Tuple=4,       a_			:						List[str]=37,       a_			:						Optional[Any]=0.1,       a_			:						List[str]=0.1,       a_			:						List[str]=512,       a_			:						List[Any]=0.02,       a_			:						List[str]=0,       a_			:						int=None,       ):





    """simple docstring"""



    UpperCamelCase__			    =    parent
    UpperCamelCase__			    =    batch_size
    UpperCamelCase__			    =    seq_length
    UpperCamelCase__			    =    is_training
    UpperCamelCase__			    =    use_input_mask
    UpperCamelCase__			    =    use_labels
    UpperCamelCase__			    =    vocab_size
    UpperCamelCase__			    =    hidden_size
    UpperCamelCase__			    =    projection_dim
    UpperCamelCase__			    =    num_hidden_layers
    UpperCamelCase__			    =    num_attention_heads
    UpperCamelCase__			    =    intermediate_size
    UpperCamelCase__			    =    dropout
    UpperCamelCase__			    =    attention_dropout
    UpperCamelCase__			    =    max_position_embeddings
    UpperCamelCase__			    =    initializer_range
    UpperCamelCase__			    =    scope
    UpperCamelCase__			    =    bos_token_id

 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    UpperCamelCase__			    =    ids_tensor([self.batch_size, self.seq_length],       self.vocab_size       )

    UpperCamelCase__			    =    None
    if self.use_input_mask:
       UpperCamelCase__			    =    random_attention_mask([self.batch_size, self.seq_length]       )

    if input_mask is not None:
       UpperCamelCase__    ,		UpperCamelCase__			    =    input_mask.shape
       UpperCamelCase__			    =    np.random.randint(1,       seq_length - 1,       size=(batch_size,)       )
       for batch_idx, start_index in enumerate(a_       ):
          UpperCamelCase__			    =    1
          UpperCamelCase__			    =    0

    UpperCamelCase__			    =    self.get_config()

    return config, input_ids, input_mask

 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    return BlipTextConfig(
        vocab_size=self.vocab_size,       hidden_size=self.hidden_size,       projection_dim=self.projection_dim,       num_hidden_layers=self.num_hidden_layers,       num_attention_heads=self.num_attention_heads,       intermediate_size=self.intermediate_size,       dropout=self.dropout,       attention_dropout=self.attention_dropout,       max_position_embeddings=self.max_position_embeddings,       initializer_range=self.initializer_range,       bos_token_id=self.bos_token_id,       )

 def 		lowercase_    (			self			:						Any,       a_			:						Optional[int],       a_			:						Union[str, Any],       a_			:						Union[str, Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    BlipTextModel(config=a_       )
    model.to(a_       )
    model.eval()
    with torch.no_grad():
       UpperCamelCase__			    =    model(a_,       attention_mask=a_       )
       UpperCamelCase__			    =    model(a_       )
    self.parent.assertEqual(result.last_hidden_state.shape,       (self.batch_size, self.seq_length, self.hidden_size)       )
    self.parent.assertEqual(result.pooler_output.shape,       (self.batch_size, self.hidden_size)       )

 def 		lowercase_    (			self			:						Tuple       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.prepare_config_and_inputs()
    UpperCamelCase__    ,		UpperCamelCase__    ,		UpperCamelCase__			    =    config_and_inputs
    UpperCamelCase__			    =    {"input_ids": input_ids, "attention_mask": input_mask}
    return config, inputs_dict







@require_torch
class 			UpperCAmelCase			(							SCREAMING_SNAKE_CASE__		,      unittest.TestCase):
 _lowerCamelCase			:       Union[str, Any]            =       (BlipTextModel,) if is_torch_available() else ()
 _lowerCamelCase			:       int            =       False
 _lowerCamelCase			:       int            =       False
 _lowerCamelCase			:       Optional[int]            =       False

 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    UpperCamelCase__			    =    BlipTextModelTester(self       )
    UpperCamelCase__			    =    ConfigTester(self,       config_class=a_,       hidden_size=37       )

 def 		lowercase_    (			self			:						Tuple       ):





    """simple docstring"""



    self.config_tester.run_common_tests()

 def 		lowercase_    (			self			:						Optional[Any]       ):





    """simple docstring"""



    UpperCamelCase__			    =    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*a_       )

 def 		lowercase_    (			self			:						Any       ):





    """simple docstring"""



    pass

 def 		lowercase_    (			self			:						Optional[Any]       ):





    """simple docstring"""



    pass

 @unittest.skip(reason="Blip does not use inputs_embeds"       )
 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    pass

 @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"       )
 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    pass

 @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"       )
 def 		lowercase_    (			self			:						int       ):





    """simple docstring"""



    pass

 @slow
 def 		lowercase_    (			self			:						List[str]       ):





    """simple docstring"""



    for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCamelCase__			    =    BlipTextModel.from_pretrained(a_       )
       self.assertIsNotNone(a_       )

 def 		lowercase_    (			self			:						Dict       ):





    """simple docstring"""



    super().test_pt_tf_model_equivalence(allow_missing_keys=a_       )




"""simple docstring"""







import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


UpperCAmelCase:  Union[str, Any]			     =				namedtuple("""ExampleNamedTuple""", """a b c""")





class   UpperCamelCase	(     unittest.TestCase       ):




	"""simple docstring"""






	def    lowerCamelCase__					(     self   ):
			_lowercase				: List[Any]     =   torch.randn(5				,2   )
			_lowercase				: List[Any]     =   torch.device("""cuda"""   ) if torch.cuda.is_available() else torch.device("""cpu"""   )

			_lowercase				: int     =   send_to_device(UpperCAmelCase_				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta.cpu()				,UpperCAmelCase_   )   )

			_lowercase				: int     =   send_to_device((tensor, [tensor, tensor], 1)				,UpperCAmelCase_   )
			self.assertIsInstance(UpperCAmelCase_				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta[0].cpu()				,UpperCAmelCase_   )   )
			self.assertIsInstance(resulta[1]				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta[1][0].cpu()				,UpperCAmelCase_   )   )
			self.assertTrue(torch.equal(resulta[1][1].cpu()				,UpperCAmelCase_   )   )
			self.assertEqual(resulta[2]				,1   )

			_lowercase				: Any     =   send_to_device({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}				,UpperCAmelCase_   )
			self.assertIsInstance(UpperCAmelCase_				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta["""a"""].cpu()				,UpperCAmelCase_   )   )
			self.assertIsInstance(resulta["""b"""]				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta["""b"""][0].cpu()				,UpperCAmelCase_   )   )
			self.assertTrue(torch.equal(resulta["""b"""][1].cpu()				,UpperCAmelCase_   )   )
			self.assertEqual(resulta["""c"""]				,1   )

			_lowercase				: Optional[int]     =   send_to_device(ExampleNamedTuple(a=UpperCAmelCase_				,b=[tensor, tensor]				,c=1   )				,UpperCAmelCase_   )
			self.assertIsInstance(UpperCAmelCase_				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta.a.cpu()				,UpperCAmelCase_   )   )
			self.assertIsInstance(resulta.b				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta.b[0].cpu()				,UpperCAmelCase_   )   )
			self.assertTrue(torch.equal(resulta.b[1].cpu()				,UpperCAmelCase_   )   )
			self.assertEqual(resulta.c				,1   )

			_lowercase				: str     =   send_to_device(UserDict({"""a""": tensor, """b""": [tensor, tensor], """c""": 1}   )				,UpperCAmelCase_   )
			self.assertIsInstance(UpperCAmelCase_				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta["""a"""].cpu()				,UpperCAmelCase_   )   )
			self.assertIsInstance(resulta["""b"""]				,UpperCAmelCase_   )
			self.assertTrue(torch.equal(resulta["""b"""][0].cpu()				,UpperCAmelCase_   )   )
			self.assertTrue(torch.equal(resulta["""b"""][1].cpu()				,UpperCAmelCase_   )   )
			self.assertEqual(resulta["""c"""]				,1   )






	def    lowerCamelCase__					(     self   ):
			with self.assertRaises(UpperCAmelCase_   ) as cm:
					_lowercase				: str     =   recursively_apply(torch.tensor				,(torch.tensor(1   ), 1)				,error_on_other_type=UpperCAmelCase_   )
			self.assertEqual(
			    str(cm.exception   )				,"""Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."""				,)






	def    lowerCamelCase__					(     self   ):
			_lowercase				: Optional[int]     =   torch.tensor([1, 2, 3, 4, 5]   )
			self.assertEqual(listify(UpperCAmelCase_   )				,[1, 2, 3, 4, 5]   )

			_lowercase				: Optional[int]     =   torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )
			self.assertEqual(listify(UpperCAmelCase_   )				,[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]   )

			_lowercase				: Optional[Any]     =   torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )
			self.assertEqual(
			    listify(UpperCAmelCase_   )				,[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]   )






	def    lowerCamelCase__					(     self   ):
			with patch_environment(aa=1				,BB=2   ):
					self.assertEqual(os.environ.get("""AA"""   )				,"""1"""   )
					self.assertEqual(os.environ.get("""BB"""   )				,"""2"""   )

			self.assertNotIn("""AA"""				,os.environ   )
			self.assertNotIn("""BB"""				,os.environ   )






	def    lowerCamelCase__					(     self   ):
			_lowercase				: Optional[Any]     =   RegressionModel()
			_lowercase				: List[Any]     =   model.forward
			_lowercase				: Optional[Any]     =   convert_outputs_to_fpaa(model.forward   )
			_lowercase				: Any     =   extract_model_from_parallel(UpperCAmelCase_				,keep_fpaa_wrapper=UpperCAmelCase_   )
			_lowercase				: Tuple     =   pickle.dumps(UpperCAmelCase_   )






	@require_cuda
	def    lowerCamelCase__					(     self   ):
			_lowercase				: str     =   RegressionModel()
			_lowercase				: Any     =   model.forward
			_lowercase				: Any     =   torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
			_lowercase				: Optional[Any]     =   convert_outputs_to_fpaa(model.forward   )
			_lowercase				: Any     =   extract_model_from_parallel(UpperCAmelCase_				,keep_fpaa_wrapper=UpperCAmelCase_   )
			_lowercase				: Tuple     =   pickle.dumps(UpperCAmelCase_   )






	@require_cuda
	@require_torch_min_version(version="""2.0"""   )
	def    lowerCamelCase__					(     self   ):
			_lowercase				: str     =   RegressionModel()
			_lowercase				: Any     =   model.forward
			_lowercase				: Union[str, Any]     =   torch.cuda.amp.autocast(dtype=torch.floataa   )(model.forward   )
			_lowercase				: Optional[Any]     =   convert_outputs_to_fpaa(model.forward   )
			_lowercase				: Union[str, Any]     =   torch.compile(model.forward				,backend="""inductor"""   )
			_lowercase				: Any     =   torch.randn(4				,10   ).cuda()
			_lowercase				: int     =   model(UpperCAmelCase_   )






	def    lowerCamelCase__					(     self   ):
			_lowercase				: List[str]     =   RegressionModel()
			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			_lowercase				: Any     =   torch.nn.parallel.DataParallel(UpperCAmelCase_   )
			_lowercase				: List[Any]     =   extract_model_from_parallel(UpperCAmelCase_   )

			self.assertEqual(UpperCAmelCase_				,UpperCAmelCase_   )






	@require_torch_min_version(version="""2.0"""   )
	def    lowerCamelCase__					(     self   ):
			_lowercase				: int     =   RegressionModel()
			_lowercase				: List[str]     =   torch.compile(UpperCAmelCase_   )

			# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
			_lowercase				: Optional[int]     =   torch.nn.parallel.DataParallel(UpperCAmelCase_   )
			_lowercase				: int     =   torch.compile(UpperCAmelCase_   )
			_lowercase				: Dict     =   extract_model_from_parallel(UpperCAmelCase_   )

			self.assertEqual(compiled_model._orig_mod				,compiled_model_unwrapped._orig_mod   )






	def    lowerCamelCase__					(     self   ):
			self.assertEqual(find_device([1, """a""", torch.tensor([1, 2, 3]   )]   )				,torch.device("""cpu"""   )   )
			self.assertEqual(find_device({"""a""": 1, """b""": torch.tensor([1, 2, 3]   )}   )				,torch.device("""cpu"""   )   )
			self.assertIsNone(find_device([1, """a"""]   )   )



