





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







	'''simple docstring'''

	def __init__(self     ,_lowerCamelCase     ,_lowerCamelCase=13     ,_lowerCamelCase=7     ,_lowerCamelCase=True     ,_lowerCamelCase=True     ,_lowerCamelCase=99     ,_lowerCamelCase=32     ,_lowerCamelCase=5     ,_lowerCamelCase=4     ,_lowerCamelCase=37     ,_lowerCamelCase="gelu"     ,_lowerCamelCase=0.1     ,_lowerCamelCase=0.1     ,_lowerCamelCase=50     ,_lowerCamelCase=0.0_2     ,_lowerCamelCase=True     ,_lowerCamelCase=None     ,)							->							int:







				'''simple docstring'''





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


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







				'''simple docstring'''





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

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

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

				__lowercase										=							self.get_config()

				return config, input_ids, input_mask, token_labels


	def 						_UpperCAmelCase     (self )							->							Dict:







				'''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 						_UpperCAmelCase     (self )							->							Union[str, Any]:







				'''simple docstring'''





				(
				    (
				    __lowercase		
				)						,		(
				    __lowercase		
				)						,		(
				    __lowercase		
				)						,		(
				    __lowercase		
				)						,				
				)										=							self.prepare_config_and_inputs()

				__lowercase										=							True
				__lowercase										=							floats_tensor([self.batch_size, self.seq_length, self.hidden_size] )
				__lowercase										=							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     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,**_lowerCamelCase     ,)							->							Tuple:







				'''simple docstring'''





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


	def 						_UpperCAmelCase     (self     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,**_lowerCamelCase     ,)							->							Any:







				'''simple docstring'''





				__lowercase										=							True
				__lowercase										=							BertGenerationEncoder(config=_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()
				__lowercase										=							model(
				    _lowerCamelCase     ,attention_mask=_lowerCamelCase     ,encoder_hidden_states=_lowerCamelCase     ,encoder_attention_mask=_lowerCamelCase     ,)
				__lowercase										=							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     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,**_lowerCamelCase     ,)							->							List[str]:







				'''simple docstring'''





				__lowercase										=							True
				__lowercase										=							True
				__lowercase										=							BertGenerationDecoder(config=_lowerCamelCase ).to(_lowerCamelCase ).eval()

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

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

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

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







				'''simple docstring'''





				__lowercase										=							BertGenerationDecoder(_lowerCamelCase )
				model.to(_lowerCamelCase )
				model.eval()
				__lowercase										=							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 )							->							Union[str, Any]:







				'''simple docstring'''





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






@require_torch
class 							__lowercase       (      lowerCAmelCase__ ,      lowerCAmelCase__ ,      lowerCAmelCase__ ,      unittest.TestCase						):







	'''simple docstring'''

	a    :				str											=       (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
	a    :				Union[str, Any]											=       (BertGenerationDecoder,) if is_torch_available() else ()
	a    :				List[Any]											=       (
	    {"feature-extraction": BertGenerationEncoder, "text-generation": BertGenerationDecoder}
	    if is_torch_available()
	    else {}
	)


	def 						_UpperCAmelCase     (self )							->							Tuple:







				'''simple docstring'''





				__lowercase										=							BertGenerationEncoderTester(self )
				__lowercase										=							ConfigTester(self     ,config_class=_lowerCamelCase     ,hidden_size=37 )


	def 						_UpperCAmelCase     (self )							->							Optional[Any]:







				'''simple docstring'''





				self.config_tester.run_common_tests()


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







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_lowerCamelCase )


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







				'''simple docstring'''





				__lowercase						,		__lowercase						,		__lowercase						,		__lowercase										=							self.model_tester.prepare_config_and_inputs()
				__lowercase										=							'''bert'''
				self.model_tester.create_and_check_model(_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							int:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(*_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							Optional[int]:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_decoder_model_past_large_inputs(*_lowerCamelCase )


	def 						_UpperCAmelCase     (self )							->							List[Any]:







				'''simple docstring'''





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

				__lowercase										=							None

				self.model_tester.create_and_check_model_as_decoder(
				    _lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,_lowerCamelCase     ,)


	def 						_UpperCAmelCase     (self )							->							Any:







				'''simple docstring'''





				__lowercase										=							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_for_causal_lm(*_lowerCamelCase )


	@slow
	def 						_UpperCAmelCase     (self )							->							Optional[int]:







				'''simple docstring'''





				__lowercase										=							BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' )
				self.assertIsNotNone(_lowerCamelCase )






@require_torch
class 							__lowercase       (      unittest.TestCase						):







	'''simple docstring'''

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







				'''simple docstring'''





				__lowercase										=							BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' )
				__lowercase										=							torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]] )
				with torch.no_grad():
							__lowercase										=							model(_lowerCamelCase )[0]
				__lowercase										=							torch.Size([1, 8, 1024] )
				self.assertEqual(output.shape     ,_lowerCamelCase )
				__lowercase										=							torch.tensor(
				    [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]] )
				self.assertTrue(torch.allclose(output[:, :3, :3]     ,_lowerCamelCase     ,atol=1E-4 ) )






@require_torch
class 							__lowercase       (      unittest.TestCase						):







	'''simple docstring'''

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







				'''simple docstring'''





				__lowercase										=							BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder''' )
				__lowercase										=							torch.tensor([[101, 7592, 1010, 2026, 3899, 2003, 10140, 102]] )
				with torch.no_grad():
							__lowercase										=							model(_lowerCamelCase )[0]
				__lowercase										=							torch.Size([1, 8, 50358] )
				self.assertEqual(output.shape     ,_lowerCamelCase )
				__lowercase										=							torch.tensor(
				    [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]] )
				self.assertTrue(torch.allclose(output[:, :3, :3]     ,_lowerCamelCase     ,atol=1E-4 ) )












'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_SCREAMING_SNAKE_CASE					=    {
    '''configuration_clipseg''': [
        '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPSegConfig''',
        '''CLIPSegTextConfig''',
        '''CLIPSegVisionConfig''',
    ],
    '''processing_clipseg''': ['''CLIPSegProcessor'''],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	_SCREAMING_SNAKE_CASE					=    [
	    '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''',
	    '''CLIPSegModel''',
	    '''CLIPSegPreTrainedModel''',
	    '''CLIPSegTextModel''',
	    '''CLIPSegVisionModel''',
	    '''CLIPSegForImageSegmentation''',
	]

if TYPE_CHECKING:
	from .configuration_clipseg import (
	    CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    CLIPSegConfig,
	    CLIPSegTextConfig,
	    CLIPSegVisionConfig,
	)
	from .processing_clipseg import CLIPSegProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_clipseg import (
		    CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
		    CLIPSegForImageSegmentation,
		    CLIPSegModel,
		    CLIPSegPreTrainedModel,
		    CLIPSegTextModel,
		    CLIPSegVisionModel,
		)

else:
	import sys

	_SCREAMING_SNAKE_CASE					=    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





