




import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import BertGenerationDecoder, BertGenerationEncoder




class 					snake_case__							:
				"""simple docstring"""



				def __init__(   self       :  Optional[int]      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Union[str, Any]=13      ,   __lowerCamelCase       :  Optional[int]=7      ,   __lowerCamelCase       :  Optional[Any]=True      ,   __lowerCamelCase       :  Tuple=True      ,   __lowerCamelCase       :  str=99      ,   __lowerCamelCase       :  Optional[Any]=32      ,   __lowerCamelCase       :  Union[str, Any]=5      ,   __lowerCamelCase       :  Dict=4      ,   __lowerCamelCase       :  List[Any]=37      ,   __lowerCamelCase       :  Tuple="gelu"      ,   __lowerCamelCase       :  List[Any]=0.1      ,   __lowerCamelCase       :  Optional[Any]=0.1      ,   __lowerCamelCase       :  int=50      ,   __lowerCamelCase       :  List[str]=0.02      ,   __lowerCamelCase       :  int=True      ,   __lowerCamelCase       :  Optional[int]=None      ,   )      ->  Union[str, Any]:
							a					=    parent
							a					=    batch_size
							a					=    seq_length
							a					=    is_training
							a					=    use_input_mask
							a					=    vocab_size
							a					=    hidden_size
							a					=    num_hidden_layers
							a					=    num_attention_heads
							a					=    intermediate_size
							a					=    hidden_act
							a					=    hidden_dropout_prob
							a					=    attention_probs_dropout_prob
							a					=    max_position_embeddings
							a					=    initializer_range
							a					=    use_labels
							a					=    scope





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Tuple:
							a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size							)

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

							if self.use_labels:
										a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size							)

							a					=    self.get_config()

							return config, input_ids, input_mask, token_labels





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  int:
							return BertGenerationConfig(
							    vocab_size=self.vocab_size      ,   hidden_size=self.hidden_size      ,   num_hidden_layers=self.num_hidden_layers      ,   num_attention_heads=self.num_attention_heads      ,   intermediate_size=self.intermediate_size      ,   hidden_act=self.hidden_act      ,   hidden_dropout_prob=self.hidden_dropout_prob      ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,   max_position_embeddings=self.max_position_embeddings      ,   is_decoder=__lowerCamelCase      ,   initializer_range=self.initializer_range      ,   )





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  List[Any]:
							(
							    (
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,		
							)					=    self.prepare_config_and_inputs()

							a					=    True
							a					=    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
							a					=    ids_tensor([self.batch_size, self.seq_length]      ,   vocab_size=2							)

							return (
							    config,
							    input_ids,
							    input_mask,
							    token_labels,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)





				def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Optional[Any]      ,   **__lowerCamelCase       :  Any      ,   )      ->  Optional[int]:
							a					=    BertGenerationEncoder(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(__lowerCamelCase      ,   attention_mask=__lowerCamelCase							)
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  str      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   **__lowerCamelCase       :  Union[str, Any]      ,   )      ->  List[Any]:
							a					=    True
							a					=    BertGenerationEncoder(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   )
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   )
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  str      ,   **__lowerCamelCase       :  str      ,   )      ->  Any:
							a					=    True
							a					=    True
							a					=    BertGenerationDecoder(config=__lowerCamelCase							).to(__lowerCamelCase							).eval()

							# first forward pass
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   use_cache=__lowerCamelCase      ,   )
							a					=    outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							a					=    ids_tensor((self.batch_size, 3)      ,   config.vocab_size							)
							a					=    ids_tensor((self.batch_size, 3)      ,   vocab_size=2							)

							# append to next input_ids and
							a					=    torch.cat([input_ids, next_tokens]      ,   dim=-1							)
							a					=    torch.cat([input_mask, next_mask]      ,   dim=-1							)

							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   output_hidden_states=__lowerCamelCase      ,   )["hidden_states"][0]
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   past_key_values=__lowerCamelCase      ,   output_hidden_states=__lowerCamelCase      ,   )["hidden_states"][0]

							# select random slice
							a					=    ids_tensor((1,)      ,   output_from_past.shape[-1]							).item()
							a					=    output_from_no_past[:, -3:, random_slice_idx].detach()
							a					=    output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]							)

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(__lowerCamelCase      ,   __lowerCamelCase      ,   atol=1e-3							)							)





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  List[Any]      ,   *__lowerCamelCase       :  Dict      ,   )      ->  List[str]:
							a					=    BertGenerationDecoder(__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(__lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   labels=__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.vocab_size)							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Optional[int]:
							a					,	a					,	a					,	a					=    self.prepare_config_and_inputs()
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict





@require_torch
class 					snake_case__							(_UpperCamelCase					, _UpperCamelCase					, _UpperCamelCase					, unittest.TestCase     ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							List[Any]											=      (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							Optional[int]											=      (BertGenerationDecoder,) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							Optional[Any]											=      (
				    {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
				    if is_torch_available()
				    else {}
				)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Optional[Any]:
							a					=    BertGenerationEncoderTester(self							)
							a					=    ConfigTester(self      ,   config_class=__lowerCamelCase      ,   hidden_size=37							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Dict:
							self.config_tester.run_common_tests()





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  str:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  List[str]:
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs()
							a					=    "bert"
							self.model_tester.create_and_check_model(__lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  str							)      ->  Optional[Any]:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  List[Any]:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Union[str, Any]:
							# This regression test was failing with PyTorch < 1.3
							(
							    (
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,	(
							    a	
							)					,		
							)					=    self.model_tester.prepare_config_and_inputs_for_decoder()

							a					=    None

							self.model_tester.create_and_check_model_as_decoder(
							    __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   )





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  Any:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(   self       :  Any							)      ->  Optional[int]:
							a					=    BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							self.assertIsNotNone(__lowerCamelCase							)





@require_torch
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				@slow
				def 							__UpperCAmelCase							(   self       :  str							)      ->  List[Any]:
							a					=    BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							a					=    torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]							)
							with torch.no_grad():
										a					=    model(__lowerCamelCase							)[0]
							a					=    torch.Size([1, 8, 10_24]							)
							self.assertEqual(output.shape      ,   __lowerCamelCase							)
							a					=    torch.tensor(
							    [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)





@require_torch
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				@slow
				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Dict:
							a					=    BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							a					=    torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]							)
							with torch.no_grad():
										a					=    model(__lowerCamelCase							)[0]
							a					=    torch.Size([1, 8, 5_03_58]							)
							self.assertEqual(output.shape      ,   __lowerCamelCase							)
							a					=    torch.tensor(
							    [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


__lowerCAmelCase	:       Dict     =	{
    'configuration_blip': [
        'BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'BlipConfig',
        'BlipTextConfig',
        'BlipVisionConfig',
    ],
    'processing_blip': ['BlipProcessor'],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase	:       Optional[Any]     =	['BlipImageProcessor']


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase	:       List[Any]     =	[
		    'BLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'BlipModel',
		    'BlipPreTrainedModel',
		    'BlipForConditionalGeneration',
		    'BlipForQuestionAnswering',
		    'BlipVisionModel',
		    'BlipTextModel',
		    'BlipForImageTextRetrieval',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCAmelCase	:       Any     =	[
		    'TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFBlipModel',
		    'TFBlipPreTrainedModel',
		    'TFBlipForConditionalGeneration',
		    'TFBlipForQuestionAnswering',
		    'TFBlipVisionModel',
		    'TFBlipTextModel',
		    'TFBlipForImageTextRetrieval',
		]

if TYPE_CHECKING:
		from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig
		from .processing_blip import BlipProcessor

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .image_processing_blip import BlipImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_blip import (
				    BLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    BlipForConditionalGeneration,
				    BlipForImageTextRetrieval,
				    BlipForQuestionAnswering,
				    BlipModel,
				    BlipPreTrainedModel,
				    BlipTextModel,
				    BlipVisionModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_blip import (
				    TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFBlipForConditionalGeneration,
				    TFBlipForImageTextRetrieval,
				    TFBlipForQuestionAnswering,
				    TFBlipModel,
				    TFBlipPreTrainedModel,
				    TFBlipTextModel,
				    TFBlipVisionModel,
				)

else:
		import sys

		__lowerCAmelCase	:       List[str]     =	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






