

'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers.models.xglm.modeling_tf_xglm import (
							    TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFXGLMForCausalLM,
							    TFXGLMModel,
							)





@require_tf
class 		__SCREAMING_SNAKE_CASE       :
				lowerCamelCase_							=						XGLMConfig
				lowerCamelCase_							=						{}
				lowerCamelCase_							=						'gelu'







				def __init__(					self    :		Dict		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		Any=14		,				UpperCAmelCase__    :		Dict=7		,				UpperCAmelCase__    :		Optional[int]=True		,				UpperCAmelCase__    :		List[str]=True		,				UpperCAmelCase__    :		List[str]=True		,				UpperCAmelCase__    :		Optional[Any]=99		,				UpperCAmelCase__    :		Tuple=32		,				UpperCAmelCase__    :		Tuple=2		,				UpperCAmelCase__    :		Tuple=4		,				UpperCAmelCase__    :		str=37		,				UpperCAmelCase__    :		Union[str, Any]="gelu"		,				UpperCAmelCase__    :		int=0.1		,				UpperCAmelCase__    :		Optional[int]=0.1		,				UpperCAmelCase__    :		List[Any]=512		,				UpperCAmelCase__    :		Dict=0.02		,				):
										'''simple docstring'''
										lowercase   :       str								=parent
										lowercase   :       int								=batch_size
										lowercase   :       Any								=seq_length
										lowercase   :       Dict								=is_training
										lowercase   :       Union[str, Any]								=use_input_mask
										lowercase   :       Optional[Any]								=use_labels
										lowercase   :       List[str]								=vocab_size
										lowercase   :       str								=d_model
										lowercase   :       Union[str, Any]								=num_hidden_layers
										lowercase   :       Optional[Any]								=num_attention_heads
										lowercase   :       Optional[int]								=ffn_dim
										lowercase   :       int								=activation_function
										lowercase   :       int								=activation_dropout
										lowercase   :       str								=attention_dropout
										lowercase   :       Tuple								=max_position_embeddings
										lowercase   :       List[str]								=initializer_range
										lowercase   :       Optional[int]								=None
										lowercase   :       Dict								=0
										lowercase   :       Any								=2
										lowercase   :       Tuple								=1







				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										return XGLMConfig.from_pretrained('''facebook/xglm-564M'''      )







				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       int								=tf.clip_by_value(
										    ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size      )		,				clip_value_min=0		,				clip_value_max=3      )

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

										lowercase   :       str								=self.get_config()

										lowercase   :       List[str]								=floats_tensor([self.num_hidden_layers, self.num_attention_heads]		,				2      )

										return (
										    config,
										    input_ids,
										    input_mask,
										    head_mask,
										)







				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										return XGLMConfig(
										    vocab_size=self.vocab_size		,				d_model=self.hidden_size		,				num_layers=self.num_hidden_layers		,				attention_heads=self.num_attention_heads		,				ffn_dim=self.ffn_dim		,				activation_function=self.activation_function		,				activation_dropout=self.activation_dropout		,				attention_dropout=self.attention_dropout		,				max_position_embeddings=self.max_position_embeddings		,				initializer_range=self.initializer_range		,				use_cache=UpperCAmelCase__		,				bos_token_id=self.bos_token_id		,				eos_token_id=self.eos_token_id		,				pad_token_id=self.pad_token_id		,				return_dict=UpperCAmelCase__		,				)







				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										lowercase   :       Any								=self.prepare_config_and_inputs()

										(
										    (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  
										)   :       Optional[Any]								=config_and_inputs

										lowercase   :       str								={
										    '''input_ids''': input_ids,
										    '''head_mask''': head_mask,
										}

										return config, inputs_dict





@require_tf
class 		__SCREAMING_SNAKE_CASE       ( lowercase__	,       lowercase__	,       unittest.TestCase			):
				lowerCamelCase_							=						(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
				lowerCamelCase_							=						(TFXGLMForCausalLM,) if is_tf_available() else ()
				lowerCamelCase_							=						(
				    {'feature-extraction': TFXGLMModel, 'text-generation': TFXGLMForCausalLM} if is_tf_available() else {}
				)
				lowerCamelCase_							=						False
				lowerCamelCase_							=						False
				lowerCamelCase_							=						False







				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										lowercase   :       Tuple								=TFXGLMModelTester(self      )
										lowercase   :       Any								=ConfigTester(self		,				config_class=UpperCAmelCase__		,				n_embd=37      )







				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				@slow
				def    lowerCamelCase_  (					self    :		Tuple      ):
										'''simple docstring'''
										for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																lowercase   :       Dict								=TFXGLMModel.from_pretrained(UpperCAmelCase__      )
																self.assertIsNotNone(UpperCAmelCase__      )







				@unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.'''      )
				def    lowerCamelCase_  (					self    :		List[Any]      ):
										'''simple docstring'''
										super().test_resize_token_embeddings()





@require_tf
class 		__SCREAMING_SNAKE_CASE       ( unittest.TestCase			):







				@slow
				def    lowerCamelCase_  (					self    :		List[str]		,				UpperCAmelCase__    :		Union[str, Any]=True      ):
										'''simple docstring'''
										lowercase   :       Dict								=TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )
										lowercase   :       Any								=tf.convert_to_tensor([[2, 268, 9865]]		,				dtype=tf.intaa      )  # The dog
										# </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
										# fmt: off
										lowercase   :       Tuple								=[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581]
										# fmt: on
										lowercase   :       Any								=model.generate(UpperCAmelCase__		,				do_sample=UpperCAmelCase__		,				num_beams=1      )
										if verify_outputs:
																self.assertListEqual(output_ids[0].numpy().tolist()		,				UpperCAmelCase__      )







				@slow
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										lowercase   :       int								=XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''      )
										lowercase   :       List[Any]								=TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )

										tf.random.set_seed(0      )
										lowercase   :       str								=tokenizer('''Today is a nice day and'''		,				return_tensors='''tf'''      )
										lowercase   :       Optional[int]								=tokenized.input_ids
										# forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
										with tf.device(''':/CPU:0'''      ):
																lowercase   :       Optional[Any]								=model.generate(UpperCAmelCase__		,				do_sample=UpperCAmelCase__		,				seed=[7, 0]      )
										lowercase   :       Optional[Any]								=tokenizer.decode(output_ids[0]		,				skip_special_tokens=UpperCAmelCase__      )

										lowercase   :       Tuple								=(
										    '''Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'''
										)
										self.assertEqual(UpperCAmelCase__		,				UpperCAmelCase__      )







				@slow
				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										lowercase   :       Any								=TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''      )
										lowercase   :       List[str]								=XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''      )

										lowercase   :       Optional[Any]								='''left'''

										# use different length sentences to test batching
										lowercase   :       Tuple								=[
										    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
										    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
										    '''regardless of whether left padding is applied or not. When''',
										    '''Hello, my dog is a little''',
										]

										lowercase   :       Optional[Any]								=tokenizer(UpperCAmelCase__		,				return_tensors='''tf'''		,				padding=UpperCAmelCase__      )
										lowercase   :       str								=inputs['''input_ids''']

										lowercase   :       Dict								=model.generate(input_ids=UpperCAmelCase__		,				attention_mask=inputs['''attention_mask''']		,				max_new_tokens=12      )

										lowercase   :       Tuple								=tokenizer(sentences[0]		,				return_tensors='''tf'''      ).input_ids
										lowercase   :       List[Any]								=model.generate(input_ids=UpperCAmelCase__		,				max_new_tokens=12      )

										lowercase   :       str								=tokenizer(sentences[1]		,				return_tensors='''tf'''      ).input_ids
										lowercase   :       Dict								=model.generate(input_ids=UpperCAmelCase__		,				max_new_tokens=12      )

										lowercase   :       Union[str, Any]								=tokenizer.batch_decode(UpperCAmelCase__		,				skip_special_tokens=UpperCAmelCase__      )
										lowercase   :       int								=tokenizer.decode(output_non_padded[0]		,				skip_special_tokens=UpperCAmelCase__      )
										lowercase   :       Optional[Any]								=tokenizer.decode(output_padded[0]		,				skip_special_tokens=UpperCAmelCase__      )

										lowercase   :       int								=[
										    '''This is an extremelly long sentence that only exists to test the ability of the model to cope with '''
										    '''left-padding, such as in batched generation. The output for the sequence below should be the same '''
										    '''regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '''
										    '''a single''',
										    '''Hello, my dog is a little bit of a shy one, but he is very friendly''',
										]
										self.assertListEqual(UpperCAmelCase__		,				UpperCAmelCase__      )
										self.assertListEqual(UpperCAmelCase__		,				[non_padded_sentence, padded_sentence]      )








'''simple docstring'''





import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
							from ... import PreTrainedTokenizerBase, TensorType

UpperCamelCase_							=					logging.get_logger(__name__)





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						'vision-encoder-decoder'
				lowerCamelCase_							=						True







				def __init__(					self    :		Optional[int]		,				**UpperCAmelCase__    :		Tuple      ):
										'''simple docstring'''
										super().__init__(**UpperCAmelCase__      )
										if "encoder" not in kwargs or "decoder" not in kwargs:
																raise ValueError(
																    F'''A configuraton of type {self.model_type} cannot be instantiated because '''
																    F'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}'''      )

										lowercase   :       Optional[Any]								=kwargs.pop('''encoder'''      )
										lowercase   :       List[Any]								=encoder_config.pop('''model_type'''      )
										lowercase   :       List[str]								=kwargs.pop('''decoder'''      )
										lowercase   :       Dict								=decoder_config.pop('''model_type'''      )

										lowercase   :       Union[str, Any]								=AutoConfig.for_model(UpperCAmelCase__		,				**UpperCAmelCase__      )
										lowercase   :       List[str]								=AutoConfig.for_model(UpperCAmelCase__		,				**UpperCAmelCase__      )
										lowercase   :       str								=True







				@classmethod
				def    lowerCamelCase_  (					cls    :		List[str]		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		PretrainedConfig		,				**UpperCAmelCase__    :		Optional[int]      ):
										'''simple docstring'''
										logger.info('''Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''      )
										lowercase   :       int								=True
										lowercase   :       Optional[Any]								=True

										return cls(encoder=encoder_config.to_dict()		,				decoder=decoder_config.to_dict()		,				**UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       int								=copy.deepcopy(self.__dict__      )
										lowercase   :       Union[str, Any]								=self.encoder.to_dict()
										lowercase   :       Union[str, Any]								=self.decoder.to_dict()
										lowercase   :       int								=self.__class__.model_type
										return output





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):
				lowerCamelCase_							=						version.parse('1.11'			)







				@property
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										return OrderedDict(
										    [
										        ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
										    ]      )







				@property
				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										return 1E-4







				@property
				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										return OrderedDict({'''last_hidden_state''': {0: '''batch''', 1: '''encoder_sequence'''}}      )





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):







				@property
				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										lowercase   :       List[str]								=OrderedDict()
										lowercase   :       Tuple								={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
										lowercase   :       Optional[int]								={0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
										lowercase   :       int								={0: '''batch''', 1: '''encoder_sequence'''}

										return common_inputs







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		"PreTrainedTokenizerBase"		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		int = -1		,				UpperCAmelCase__    :		bool = False		,				UpperCAmelCase__    :		Optional["TensorType"] = None		,				):
										'''simple docstring'''
										import torch

										lowercase   :       Optional[Any]								=OrderedDict()

										lowercase   :       List[Any]								=super().generate_dummy_inputs(
										    UpperCAmelCase__		,				batch_size=UpperCAmelCase__		,				seq_length=UpperCAmelCase__		,				is_pair=UpperCAmelCase__		,				framework=UpperCAmelCase__      )

										lowercase		,  lowercase   :       Optional[int]								=dummy_input['''input_ids'''].shape
										lowercase   :       Union[str, Any]								=(batch, encoder_sequence, self._config.encoder_hidden_size)
										lowercase   :       List[str]								=dummy_input.pop('''input_ids'''      )
										lowercase   :       Tuple								=dummy_input.pop('''attention_mask'''      )
										lowercase   :       Union[str, Any]								=torch.zeros(UpperCAmelCase__      )

										return common_inputs





class 		__SCREAMING_SNAKE_CASE       ( lowercase__			):







				@property
				def    lowerCamelCase_  (					self    :		int      ):
										'''simple docstring'''
										pass







				def    lowerCamelCase_  (					self    :		List[Any]		,				UpperCAmelCase__    :		PretrainedConfig      ):
										'''simple docstring'''
										return VisionEncoderDecoderEncoderOnnxConfig(UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		PretrainedConfig		,				UpperCAmelCase__    :		str = "default"      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=encoder_config.hidden_size
										return VisionEncoderDecoderDecoderOnnxConfig(UpperCAmelCase__		,				UpperCAmelCase__      )





