


from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
   import tensorflow as tf

   from transformers import TFBlipTextModel
   from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST







class lowerCAmelCase_		:

 '''simple docstring'''

 def __init__(			self	,   _UpperCAmelCase	,   _UpperCAmelCase=12	,   _UpperCAmelCase=7	,   _UpperCAmelCase=True	,   _UpperCAmelCase=True	,   _UpperCAmelCase=True	,   _UpperCAmelCase=99	,   _UpperCAmelCase=32	,   _UpperCAmelCase=32	,   _UpperCAmelCase=2	,   _UpperCAmelCase=4	,   _UpperCAmelCase=37	,   _UpperCAmelCase=0.1	,   _UpperCAmelCase=0.1	,   _UpperCAmelCase=5_12	,   _UpperCAmelCase=0.02	,   _UpperCAmelCase=0	,   _UpperCAmelCase=None	,   ):
       snake_case_ =   parent
       snake_case_ =   batch_size
       snake_case_ =   seq_length
       snake_case_ =   is_training
       snake_case_ =   use_input_mask
       snake_case_ =   use_labels
       snake_case_ =   vocab_size
       snake_case_ =   hidden_size
       snake_case_ =   projection_dim
       snake_case_ =   num_hidden_layers
       snake_case_ =   num_attention_heads
       snake_case_ =   intermediate_size
       snake_case_ =   dropout
       snake_case_ =   attention_dropout
       snake_case_ =   max_position_embeddings
       snake_case_ =   initializer_range
       snake_case_ =   scope
       snake_case_ =   bos_token_id



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

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

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

       snake_case_ =   self.get_config()

       return config, input_ids, tf.convert_to_tensor(_UpperCAmelCase      )



 def 		UpperCamelCase__  (			self      ):
       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 		UpperCamelCase__  (			self	,   _UpperCAmelCase	,   _UpperCAmelCase	,   _UpperCAmelCase      ):
       snake_case_ =   TFBlipTextModel(config=_UpperCAmelCase      )
       snake_case_ =   model(_UpperCAmelCase	,   attention_mask=_UpperCAmelCase	,   training=_UpperCAmelCase      )
       snake_case_ =   model(_UpperCAmelCase	,   training=_UpperCAmelCase      )
       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 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.prepare_config_and_inputs()
       snake_case_ ,					snake_case_ ,					snake_case_ =   config_and_inputs
       snake_case_ =   {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict







@require_tf
class lowerCAmelCase_		(				lowerCamelCase__      ,		unittest.TestCase ):

 '''simple docstring'''

 __snake_case				=						(TFBlipTextModel,) if is_tf_available() else ()
 __snake_case				=						False
 __snake_case				=						False
 __snake_case				=						False



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   BlipTextModelTester(self      )
       snake_case_ =   ConfigTester(self	,   config_class=_UpperCAmelCase	,   hidden_size=37      )



 def 		UpperCamelCase__  (			self      ):
       self.config_tester.run_common_tests()



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase      )



 def 		UpperCamelCase__  (			self      ):
       pass



 def 		UpperCamelCase__  (			self      ):
       pass



 @unittest.skip(reason='''Blip does not use inputs_embeds'''      )
 def 		UpperCamelCase__  (			self      ):
       pass



 @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''      )
 def 		UpperCamelCase__  (			self      ):
       pass



 @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''      )
 def 		UpperCamelCase__  (			self      ):
       pass



 @slow
 def 		UpperCamelCase__  (			self      ):
       for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case_ =   TFBlipTextModel.from_pretrained(_UpperCAmelCase      )
             self.assertIsNotNone(_UpperCAmelCase      )





 def 		UpperCamelCase__  (			self	,   _UpperCAmelCase=True      ):
       super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCAmelCase      )



import unittest

import numpy as np

from diffusers import LMSDiscreteScheduler, OnnxStableDiffusionInpaintPipeline
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)

from ..test_pipelines_onnx_common import OnnxPipelineTesterMixin


if is_onnx_available():
   import onnxruntime as ort







class lowerCAmelCase_		(				lowerCamelCase__      ,		unittest.TestCase ):

 '''simple docstring'''

 pass







@nightly
@require_onnxruntime
@require_torch_gpu
class lowerCAmelCase_		(				unittest.TestCase ):

 '''simple docstring'''

 @property
 def 		UpperCamelCase__  (			self      ):
       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )



 @property
 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   ort.SessionOptions()
       snake_case_ =   False
       return options



 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/in_paint/overture-creations-5sI6fQgYIuo.png'''      )
       snake_case_ =   load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''      )
       snake_case_ =   OnnxStableDiffusionInpaintPipeline.from_pretrained(
           '''runwayml/stable-diffusion-inpainting'''	,   revision='''onnx'''	,   safety_checker=_UpperCAmelCase	,   feature_extractor=_UpperCAmelCase	,   provider=self.gpu_provider	,   sess_options=self.gpu_options	,   )
       pipe.set_progress_bar_config(disable=_UpperCAmelCase      )

       snake_case_ =   '''A red cat sitting on a park bench'''

       snake_case_ =   np.random.RandomState(0      )
       snake_case_ =   pipe(
           prompt=_UpperCAmelCase	,   image=_UpperCAmelCase	,   mask_image=_UpperCAmelCase	,   guidance_scale=7.5	,   num_inference_steps=10	,   generator=_UpperCAmelCase	,   output_type='''np'''	,   )
       snake_case_ =   output.images
       snake_case_ =   images[0, 2_55:2_58, 2_55:2_58, -1]

       assert images.shape == (1, 5_12, 5_12, 3)
       snake_case_ =   np.array([0.2_514, 0.3_007, 0.3_517, 0.1_790, 0.2_382, 0.3_167, 0.1_944, 0.2_273, 0.2_464]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3





 def 		UpperCamelCase__  (			self      ):
       snake_case_ =   load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/in_paint/overture-creations-5sI6fQgYIuo.png'''      )
       snake_case_ =   load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/in_paint/overture-creations-5sI6fQgYIuo_mask.png'''      )
       snake_case_ =   LMSDiscreteScheduler.from_pretrained(
           '''runwayml/stable-diffusion-inpainting'''	,   subfolder='''scheduler'''	,   revision='''onnx'''      )
       snake_case_ =   OnnxStableDiffusionInpaintPipeline.from_pretrained(
           '''runwayml/stable-diffusion-inpainting'''	,   revision='''onnx'''	,   scheduler=_UpperCAmelCase	,   safety_checker=_UpperCAmelCase	,   feature_extractor=_UpperCAmelCase	,   provider=self.gpu_provider	,   sess_options=self.gpu_options	,   )
       pipe.set_progress_bar_config(disable=_UpperCAmelCase      )

       snake_case_ =   '''A red cat sitting on a park bench'''

       snake_case_ =   np.random.RandomState(0      )
       snake_case_ =   pipe(
           prompt=_UpperCAmelCase	,   image=_UpperCAmelCase	,   mask_image=_UpperCAmelCase	,   guidance_scale=7.5	,   num_inference_steps=20	,   generator=_UpperCAmelCase	,   output_type='''np'''	,   )
       snake_case_ =   output.images
       snake_case_ =   images[0, 2_55:2_58, 2_55:2_58, -1]

       assert images.shape == (1, 5_12, 5_12, 3)
       snake_case_ =   np.array([0.0_086, 0.0_077, 0.0_083, 0.0_093, 0.0_107, 0.0_139, 0.0_094, 0.0_097, 0.0_125]      )

       assert np.abs(image_slice.flatten() - expected_slice      ).max() < 1E-3