





'''simple docstring'''

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 							__lowerCAmelCase	:






       """simple docstring"""





       def __init__(				self  :						List[Any]			,					lowerCAmelCase__  :						Any			,					lowerCAmelCase__  :						Dict=13			,					lowerCAmelCase__  :						Optional[Any]=7			,					lowerCAmelCase__  :						Union[str, Any]=True			,					lowerCAmelCase__  :						List[Any]=True			,					lowerCAmelCase__  :						int=99			,					lowerCAmelCase__  :						Optional[Any]=32			,					lowerCAmelCase__  :						int=5			,					lowerCAmelCase__  :						int=4			,					lowerCAmelCase__  :						Union[str, Any]=37			,					lowerCAmelCase__  :						Dict="gelu"			,					lowerCAmelCase__  :						Dict=0.1			,					lowerCAmelCase__  :						Optional[int]=0.1			,					lowerCAmelCase__  :						Tuple=50			,					lowerCAmelCase__  :						Tuple=0.02			,					lowerCAmelCase__  :						List[Any]=True			,					lowerCAmelCase__  :						str=None			,					)       ->	Dict:

            '''simple docstring'''




            _UpperCamelCase							       =			parent
            _UpperCamelCase							       =			batch_size
            _UpperCamelCase							       =			seq_length
            _UpperCamelCase							       =			is_training
            _UpperCamelCase							       =			use_input_mask
            _UpperCamelCase							       =			vocab_size
            _UpperCamelCase							       =			hidden_size
            _UpperCamelCase							       =			num_hidden_layers
            _UpperCamelCase							       =			num_attention_heads
            _UpperCamelCase							       =			intermediate_size
            _UpperCamelCase							       =			hidden_act
            _UpperCamelCase							       =			hidden_dropout_prob
            _UpperCamelCase							       =			attention_probs_dropout_prob
            _UpperCamelCase							       =			max_position_embeddings
            _UpperCamelCase							       =			initializer_range
            _UpperCamelCase							       =			use_labels
            _UpperCamelCase							       =			scope





       def snake_case__		(				self  :						Dict	)       ->	Tuple:

            '''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 self.use_labels:
                 _UpperCamelCase							       =			ids_tensor([self.batch_size, self.seq_length]			,					self.vocab_size	)

            _UpperCamelCase							       =			self.get_config()

            return config, input_ids, input_mask, token_labels





       def snake_case__		(				self  :						Optional[int]	)       ->	Any:

            '''simple docstring'''




            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 snake_case__		(				self  :						Union[str, Any]	)       ->	Optional[int]:

            '''simple docstring'''




            (
                (
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							
            )							       =			self.prepare_config_and_inputs()

            _UpperCamelCase							       =			True
            _UpperCamelCase							       =			floats_tensor([self.batch_size, self.seq_length, self.hidden_size]	)
            _UpperCamelCase							       =			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 snake_case__		(				self  :						int			,					lowerCAmelCase__  :						Union[str, Any]			,					lowerCAmelCase__  :						str			,					lowerCAmelCase__  :						List[str]			,					lowerCAmelCase__  :						Optional[int]			,					**lowerCAmelCase__  :						int			,					)       ->	Tuple:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationEncoder(config=lowerCAmelCase__	)
            model.to(lowerCAmelCase__	)
            model.eval()
            _UpperCamelCase							       =			model(lowerCAmelCase__			,					attention_mask=lowerCAmelCase__	)
            _UpperCamelCase							       =			model(lowerCAmelCase__	)
            self.parent.assertEqual(result.last_hidden_state.shape			,					(self.batch_size, self.seq_length, self.hidden_size)	)





       def snake_case__		(				self  :						int			,					lowerCAmelCase__  :						Union[str, Any]			,					lowerCAmelCase__  :						Union[str, Any]			,					lowerCAmelCase__  :						List[str]			,					lowerCAmelCase__  :						Optional[int]			,					lowerCAmelCase__  :						List[Any]			,					lowerCAmelCase__  :						Optional[Any]			,					**lowerCAmelCase__  :						List[str]			,					)       ->	List[Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			True
            _UpperCamelCase							       =			BertGenerationEncoder(config=lowerCAmelCase__	)
            model.to(lowerCAmelCase__	)
            model.eval()
            _UpperCamelCase							       =			model(
                lowerCAmelCase__			,					attention_mask=lowerCAmelCase__			,					encoder_hidden_states=lowerCAmelCase__			,					encoder_attention_mask=lowerCAmelCase__			,					)
            _UpperCamelCase							       =			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 snake_case__		(				self  :						List[Any]			,					lowerCAmelCase__  :						Dict			,					lowerCAmelCase__  :						List[str]			,					lowerCAmelCase__  :						Any			,					lowerCAmelCase__  :						int			,					lowerCAmelCase__  :						Optional[int]			,					lowerCAmelCase__  :						str			,					**lowerCAmelCase__  :						Dict			,					)       ->	int:

            '''simple docstring'''




            _UpperCamelCase							       =			True
            _UpperCamelCase							       =			True
            _UpperCamelCase							       =			BertGenerationDecoder(config=lowerCAmelCase__	).to(lowerCAmelCase__	).eval()

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

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

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

            _UpperCamelCase							       =			model(
                lowerCAmelCase__			,					attention_mask=lowerCAmelCase__			,					encoder_hidden_states=lowerCAmelCase__			,					encoder_attention_mask=lowerCAmelCase__			,					output_hidden_states=lowerCAmelCase__			,					)['''hidden_states'''][0]
            _UpperCamelCase							       =			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
            _UpperCamelCase							       =			ids_tensor((1,)			,					output_from_past.shape[-1]	).item()
            _UpperCamelCase							       =			output_from_no_past[:, -3:, random_slice_idx].detach()
            _UpperCamelCase							       =			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 snake_case__		(				self  :						List[Any]			,					lowerCAmelCase__  :						Tuple			,					lowerCAmelCase__  :						Any			,					lowerCAmelCase__  :						Dict			,					lowerCAmelCase__  :						List[Any]			,					*lowerCAmelCase__  :						List[Any]			,					)       ->	List[str]:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationDecoder(lowerCAmelCase__	)
            model.to(lowerCAmelCase__	)
            model.eval()
            _UpperCamelCase							       =			model(lowerCAmelCase__			,					attention_mask=lowerCAmelCase__			,					labels=lowerCAmelCase__	)
            self.parent.assertEqual(result.logits.shape			,					(self.batch_size, self.seq_length, self.vocab_size)	)







       def snake_case__		(				self  :						Optional[Any]	)       ->	List[str]:

            '''simple docstring'''




            _UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase							       =			self.prepare_config_and_inputs()
            _UpperCamelCase							       =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
            return config, inputs_dict

@require_torch
class 							__lowerCAmelCase	(       __magic_name__		,		__magic_name__		,		__magic_name__		,		unittest.TestCase					):






       """simple docstring"""
       _snake_case      :	Union[str, Any]									=       (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
       _snake_case      :	Optional[int]									=       (BertGenerationDecoder,) if is_torch_available() else ()
       _snake_case      :	Dict									=       (
           {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
           if is_torch_available()
           else {}
       )





       def snake_case__		(				self  :						List[Any]	)       ->	Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationEncoderTester(self	)
            _UpperCamelCase							       =			ConfigTester(self			,					config_class=lowerCAmelCase__			,					hidden_size=37	)





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

            '''simple docstring'''




            self.config_tester.run_common_tests()





       def snake_case__		(				self  :						Any	)       ->	Optional[int]:

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*lowerCAmelCase__	)





       def snake_case__		(				self  :						Tuple	)       ->	str:

            '''simple docstring'''




            _UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs()
            _UpperCamelCase							       =			'''bert'''
            self.model_tester.create_and_check_model(lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__	)





       def snake_case__		(				self  :						List[str]	)       ->	Any:

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase__	)





       def snake_case__		(				self  :						int	)       ->	Any:

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowerCAmelCase__	)





       def snake_case__		(				self  :						Optional[int]	)       ->	List[str]:

            '''simple docstring'''




            (
                (
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							(
                _UpperCamelCase
            )       ,							
            )							       =			self.model_tester.prepare_config_and_inputs_for_decoder()

            _UpperCamelCase							       =			None

            self.model_tester.create_and_check_model_as_decoder(
                lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__			,					lowerCAmelCase__			,					)





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

            '''simple docstring'''




            _UpperCamelCase							       =			self.model_tester.prepare_config_and_inputs_for_decoder()
            self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase__	)







       @slow
       def snake_case__		(				self  :						Optional[Any]	)       ->	Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
            self.assertIsNotNone(lowerCAmelCase__	)

@require_torch
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





       @slow
       def snake_case__		(				self  :						List[str]	)       ->	Optional[Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
            _UpperCamelCase							       =			torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]	)
            with torch.no_grad():
                 _UpperCamelCase							       =			model(lowerCAmelCase__	)[0]
            _UpperCamelCase							       =			torch.Size([1, 8, 1024]	)
            self.assertEqual(output.shape			,					lowerCAmelCase__	)
            _UpperCamelCase							       =			torch.tensor(
                [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]	)
            self.assertTrue(torch.allclose(output[:, :3, :3]			,					lowerCAmelCase__			,					atol=1e-4	)	)

@require_torch
class 							__lowerCAmelCase	(       unittest.TestCase					):






       """simple docstring"""





       @slow
       def snake_case__		(				self  :						List[str]	)       ->	Tuple:

            '''simple docstring'''




            _UpperCamelCase							       =			BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''	)
            _UpperCamelCase							       =			torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]]	)
            with torch.no_grad():
                 _UpperCamelCase							       =			model(lowerCAmelCase__	)[0]
            _UpperCamelCase							       =			torch.Size([1, 8, 50358]	)
            self.assertEqual(output.shape			,					lowerCAmelCase__	)
            _UpperCamelCase							       =			torch.tensor(
                [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]	)
            self.assertTrue(torch.allclose(output[:, :3, :3]			,					lowerCAmelCase__			,					atol=1e-4	)	)








import warnings

from ...utils import logging
from .image_processing_clip import CLIPImageProcessor


SCREAMING_SNAKE_CASE__   			=   logging.get_logger(__name__)


class   __lowerCAmelCase  (		UpperCAmelCase_   ):
					"""simple docstring"""






					def __init__(  self       :							Dict					,       *_snake_case       :							int					,       **_snake_case       :							Optional[int]       ):






								"""simple docstring"""


								warnings.warn(
								    'The class CLIPFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
								    ' use CLIPImageProcessor instead.'					,       _snake_case					,       )
								super().__init__(*_snake_case					,       **_snake_case       )




