
import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest





class 							UpperCAmelCase      (  __SCREAMING_SNAKE_CASE  ):




	'''simple docstring'''





	snake_case_					    =     (KDPMaDiscreteScheduler,)
	snake_case_					    =     10






	def 				UpperCamelCase_  (       self					: Optional[int]							,**A					: int ):
					__A   			= {
					    "num_train_timesteps": 11_00,
					    "beta_start": 0.00_01,
					    "beta_end": 0.02,
					    "beta_schedule": "linear",
					}

					config.update(**A )
					return config






	def 				UpperCamelCase_  (       self					: Dict ):
					for timesteps in [10, 50, 1_00, 10_00]:
									self.check_over_configs(num_train_timesteps=A )






	def 				UpperCamelCase_  (       self					: Dict ):
					for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]							,[0.00_02, 0.0_02, 0.02] ):
									self.check_over_configs(beta_start=A							,beta_end=A )






	def 				UpperCamelCase_  (       self					: Tuple ):
					for schedule in ["linear", "scaled_linear"]:
									self.check_over_configs(beta_schedule=A )






	def 				UpperCamelCase_  (       self					: int ):
					for prediction_type in ["epsilon", "v_prediction"]:
									self.check_over_configs(prediction_type=A )






	def 				UpperCamelCase_  (       self					: str ):
					__A   			= self.scheduler_classes[0]
					__A   			= self.get_scheduler_config(prediction_type="v_prediction" )
					__A   			= scheduler_class(**A )

					scheduler.set_timesteps(self.num_inference_steps )

					__A   			= self.dummy_model()
					__A   			= self.dummy_sample_deter * scheduler.init_noise_sigma
					__A   			= sample.to(A )

					for i, t in enumerate(scheduler.timesteps ):
									__A   			= scheduler.scale_model_input(A							,A )

									__A   			= model(A							,A )

									__A   			= scheduler.step(A							,A							,A )
									__A   			= output.prev_sample

					__A   			= torch.sum(torch.abs(A ) )
					__A   			= torch.mean(torch.abs(A ) )

					if torch_device in ["cpu", "mps"]:
									assert abs(result_sum.item() - 4.6934E-07 ) < 1E-2
									assert abs(result_mean.item() - 6.1112E-10 ) < 1E-3
					else:
									# CUDA
									assert abs(result_sum.item() - 4.693428650170972E-07 ) < 1E-2
									assert abs(result_mean.item() - 0.00_02 ) < 1E-3






	def 				UpperCamelCase_  (       self					: Optional[Any] ):
					if torch_device == "mps":
									return
					__A   			= self.scheduler_classes[0]
					__A   			= self.get_scheduler_config()
					__A   			= scheduler_class(**A )

					scheduler.set_timesteps(self.num_inference_steps )

					__A   			= self.dummy_model()
					__A   			= self.dummy_sample_deter * scheduler.init_noise_sigma
					__A   			= sample.to(A )

					for i, t in enumerate(scheduler.timesteps ):
									__A   			= scheduler.scale_model_input(A							,A )

									__A   			= model(A							,A )

									__A   			= scheduler.step(A							,A							,A )
									__A   			= output.prev_sample

					__A   			= torch.sum(torch.abs(A ) )
					__A   			= torch.mean(torch.abs(A ) )

					if torch_device in ["cpu", "mps"]:
									assert abs(result_sum.item() - 20.41_25 ) < 1E-2
									assert abs(result_mean.item() - 0.02_66 ) < 1E-3
					else:
									# CUDA
									assert abs(result_sum.item() - 20.41_25 ) < 1E-2
									assert abs(result_mean.item() - 0.02_66 ) < 1E-3







	def 				UpperCamelCase_  (       self					: Dict ):
					if torch_device == "mps":
									return
					__A   			= self.scheduler_classes[0]
					__A   			= self.get_scheduler_config()
					__A   			= scheduler_class(**A )

					scheduler.set_timesteps(self.num_inference_steps							,device=A )

					__A   			= self.dummy_model()
					__A   			= self.dummy_sample_deter.to(A ) * scheduler.init_noise_sigma

					for t in scheduler.timesteps:
									__A   			= scheduler.scale_model_input(A							,A )

									__A   			= model(A							,A )

									__A   			= scheduler.step(A							,A							,A )
									__A   			= output.prev_sample

					__A   			= torch.sum(torch.abs(A ) )
					__A   			= torch.mean(torch.abs(A ) )

					if str(A ).startswith("cpu" ):
									# The following sum varies between 148 and 156 on mps. Why?
									assert abs(result_sum.item() - 20.41_25 ) < 1E-2
									assert abs(result_mean.item() - 0.02_66 ) < 1E-3
					else:
									# CUDA
									assert abs(result_sum.item() - 20.41_25 ) < 1E-2
									assert abs(result_mean.item() - 0.02_66 ) < 1E-3




"""simple docstring"""



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				:







   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__=512						,	UpperCamelCase__=0.02						,	UpperCamelCase__=0						,	UpperCamelCase__=None						,	)	->		Dict:

       '''simple docstring'''




       snake_case    :      Optional[Any]      					=    parent
       snake_case    :      Dict      					=    batch_size
       snake_case    :      List[str]      					=    seq_length
       snake_case    :      Dict      					=    is_training
       snake_case    :      Optional[Any]      					=    use_input_mask
       snake_case    :      Optional[int]      					=    use_labels
       snake_case    :      Tuple      					=    vocab_size
       snake_case    :      Optional[Any]      					=    hidden_size
       snake_case    :      Optional[Any]      					=    projection_dim
       snake_case    :      List[Any]      					=    num_hidden_layers
       snake_case    :      List[Any]      					=    num_attention_heads
       snake_case    :      int      					=    intermediate_size
       snake_case    :      str      					=    dropout
       snake_case    :      List[Any]      					=    attention_dropout
       snake_case    :      Any      					=    max_position_embeddings
       snake_case    :      List[Any]      					=    initializer_range
       snake_case    :      Any      					=    scope
       snake_case    :      Union[str, Any]      					=    bos_token_id







   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       snake_case    :      str      					=    ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size						)

       snake_case    :      int      					=    None
       if self.use_input_mask:
           snake_case    :      Optional[int]      					=    random_attention_mask([self.batch_size, self.seq_length]						)

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

       snake_case    :      Union[str, Any]      					=    self.get_config()

       return config, input_ids, tf.convert_to_tensor(UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Union[str, Any]:

       '''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  lowerCamelCase		(	self						,	UpperCamelCase__						,	UpperCamelCase__						,	UpperCamelCase__						)	->		Tuple:

       '''simple docstring'''




       snake_case    :      str      					=    TFBlipTextModel(config=UpperCamelCase__						)
       snake_case    :      Any      					=    model(UpperCamelCase__						,	attention_mask=UpperCamelCase__						,	training=UpperCamelCase__						)
       snake_case    :      Optional[int]      					=    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  lowerCamelCase		(	self						)	->		List[str]:

       '''simple docstring'''




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







@require_tf
class _lowerCAmelCase				(      snake_case_    ,							unittest.TestCase	):
   __UpperCAmelCase :       Any							  =  (TFBlipTextModel,) if is_tf_available() else ()
   __UpperCAmelCase :       Any							  =  False
   __UpperCAmelCase :       Dict							  =  False
   __UpperCAmelCase :       List[Any]							  =  False







   def  lowerCamelCase		(	self						)	->		List[str]:

       '''simple docstring'''




       snake_case    :      List[Any]      					=    BlipTextModelTester(self						)
       snake_case    :      Optional[int]      					=    ConfigTester(self						,	config_class=UpperCamelCase__						,	hidden_size=37						)







   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       self.config_tester.run_common_tests()







   def  lowerCamelCase		(	self						)	->		str:

       '''simple docstring'''




       snake_case    :      List[str]      					=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCamelCase__						)







   def  lowerCamelCase		(	self						)	->		Union[str, Any]:

       '''simple docstring'''




       pass







   def  lowerCamelCase		(	self						)	->		List[str]:

       '''simple docstring'''




       pass







   @unittest.skip(reason="Blip does not use inputs_embeds"						)
   def  lowerCamelCase		(	self						)	->		Tuple:

       '''simple docstring'''




       pass







   @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"						)
   def  lowerCamelCase		(	self						)	->		Optional[Any]:

       '''simple docstring'''




       pass







   @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"						)
   def  lowerCamelCase		(	self						)	->		Any:

       '''simple docstring'''




       pass







   @slow
   def  lowerCamelCase		(	self						)	->		int:

       '''simple docstring'''




       for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           snake_case    :      List[str]      					=    TFBlipTextModel.from_pretrained(UpperCamelCase__						)
           self.assertIsNotNone(UpperCamelCase__						)







   def  lowerCamelCase		(	self						,	UpperCamelCase__=True						)	->		Optional[int]:

       '''simple docstring'''




       super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCamelCase__						)





