






"""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 			UpperCamelCase_       :
	__magic_name__	      =				XGLMConfig
	__magic_name__	      =				{}
	__magic_name__	      =				'''gelu'''





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





	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			int:
				return XGLMConfig.from_pretrained("facebook/xglm-564M"						)





	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			List[str]:
				UpperCAmelCase_     : Dict			     =					tf.clip_by_value(
				    ids_tensor([self.batch_size, self.seq_length]	,	self.vocab_size						)	,	clip_value_min=0	,	clip_value_max=3						)

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

				UpperCAmelCase_     : int			     =					self.get_config()

				UpperCAmelCase_     : Dict			     =					floats_tensor([self.num_hidden_layers, self.num_attention_heads]	,	2						)

				return (
				    config,
				    input_ids,
				    input_mask,
				    head_mask,
				)





	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			Tuple:
				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=__a	,	bos_token_id=self.bos_token_id	,	eos_token_id=self.eos_token_id	,	pad_token_id=self.pad_token_id	,	return_dict=__a	,	)





	def      _SCREAMING_SNAKE_CASE      (      self					: str						)						->			Tuple:
				UpperCAmelCase_     : Optional[int]			     =					self.prepare_config_and_inputs()

				(
				    UpperCAmelCase_    
				)     : List[Any]			     =					config_and_inputs

				UpperCAmelCase_     : List[Any]			     =					{
				    "input_ids": input_ids,
				    "head_mask": head_mask,
				}

				return config, inputs_dict







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





	def      _SCREAMING_SNAKE_CASE      (      self					: Optional[Any]						)						->			Union[str, Any]:
				UpperCAmelCase_     : List[str]			     =					TFXGLMModelTester(self						)
				UpperCAmelCase_     : int			     =					ConfigTester(self	,	config_class=__a	,	n_embd=37						)





	def      _SCREAMING_SNAKE_CASE      (      self					: List[str]						)						->			Optional[int]:
				self.config_tester.run_common_tests()





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple						)						->			str:

				for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							UpperCAmelCase_     : Any			     =					TFXGLMModel.from_pretrained(__a						)
							self.assertIsNotNone(__a						)





	@unittest.skip(reason="Currently, model embeddings are going to undergo a major refactor."						)
	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			Optional[int]:
				super().test_resize_token_embeddings()







@require_tf
class 			UpperCamelCase_       (unittest.TestCase     ):





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Tuple	,	lowerCAmelCase_					: Dict=True						)						->			Optional[Any]:
				UpperCAmelCase_     : Optional[int]			     =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"						)
				UpperCAmelCase_     : Optional[Any]			     =					tf.convert_to_tensor([[2, 268, 9_865]]	,	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
				UpperCAmelCase_     : Tuple			     =					[2, 268, 9_865, 67, 11, 1_988, 57_252, 9_865, 5, 984, 67, 1_988, 213_838, 1_658, 53, 70_446, 33, 6_657, 278, 1_581]
				# fmt: on
				UpperCAmelCase_     : Tuple			     =					model.generate(__a	,	do_sample=__a	,	num_beams=1						)
				if verify_outputs:
							self.assertListEqual(output_ids[0].numpy().tolist()	,	__a						)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			str:
				UpperCAmelCase_     : Union[str, Any]			     =					XGLMTokenizer.from_pretrained("facebook/xglm-564M"						)
				UpperCAmelCase_     : List[str]			     =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"						)

				tf.random.set_seed(0						)
				UpperCAmelCase_     : Union[str, Any]			     =					tokenizer("Today is a nice day and"	,	return_tensors="tf"						)
				UpperCAmelCase_     : Union[str, Any]			     =					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"						):
							UpperCAmelCase_     : Tuple			     =					model.generate(__a	,	do_sample=__a	,	seed=[7, 0]						)
				UpperCAmelCase_     : Optional[Any]			     =					tokenizer.decode(output_ids[0]	,	skip_special_tokens=__a						)

				UpperCAmelCase_     : Optional[Any]			     =					(
				    "Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due"
				)
				self.assertEqual(__a	,	__a						)





	@slow
	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			Dict:
				UpperCAmelCase_     : Optional[Any]			     =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"						)
				UpperCAmelCase_     : Any			     =					XGLMTokenizer.from_pretrained("facebook/xglm-564M"						)

				UpperCAmelCase_     : Union[str, Any]			     =					"left"

				# use different length sentences to test batching
				UpperCAmelCase_     : 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",
				]

				UpperCAmelCase_     : Tuple			     =					tokenizer(__a	,	return_tensors="tf"	,	padding=__a						)
				UpperCAmelCase_     : List[str]			     =					inputs["input_ids"]

				UpperCAmelCase_     : Any			     =					model.generate(input_ids=__a	,	attention_mask=inputs["attention_mask"]	,	max_new_tokens=12						)

				UpperCAmelCase_     : List[Any]			     =					tokenizer(sentences[0]	,	return_tensors="tf"						).input_ids
				UpperCAmelCase_     : int			     =					model.generate(input_ids=__a	,	max_new_tokens=12						)

				UpperCAmelCase_     : List[Any]			     =					tokenizer(sentences[1]	,	return_tensors="tf"						).input_ids
				UpperCAmelCase_     : Optional[Any]			     =					model.generate(input_ids=__a	,	max_new_tokens=12						)

				UpperCAmelCase_     : List[Any]			     =					tokenizer.batch_decode(__a	,	skip_special_tokens=__a						)
				UpperCAmelCase_     : Union[str, Any]			     =					tokenizer.decode(output_non_padded[0]	,	skip_special_tokens=__a						)
				UpperCAmelCase_     : Union[str, Any]			     =					tokenizer.decode(output_padded[0]	,	skip_special_tokens=__a						)

				UpperCAmelCase_     : Union[str, Any]			     =					[
				    "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(__a	,	__a						)
				self.assertListEqual(__a	,	[non_padded_sentence, padded_sentence]						)








import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_snake_case		=  logging.get_logger(__name__)

_snake_case		=  {
    "facebook/wav2vec2-base-960h": "https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json",
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}



class   UpperCAmelCase_    (       a):
							lowerCamelCase__   			=		'wav2vec2'







							def __init__(   self,			__a=32,			__a=768,			__a=12,			__a=12,			__a=3072,			__a="gelu",			__a=0.1,			__a=0.1,			__a=0.1,			__a=0.0,			__a=0.0,			__a=0.1,			__a=0.1,			__a=0.02,			__a=1E-5,			__a="group",			__a="gelu",			__a=(512, 512, 512, 512, 512, 512, 512),			__a=(5, 2, 2, 2, 2, 2, 2),			__a=(10, 3, 3, 3, 3, 2, 2),			__a=False,			__a=128,			__a=16,			__a=False,			__a=True,			__a=0.05,			__a=10,			__a=2,			__a=0.0,			__a=10,			__a=0,			__a=320,			__a=2,			__a=0.1,			__a=100,			__a=256,			__a=256,			__a=0.1,			__a="sum",			__a=False,			__a=False,			__a=256,			__a=(512, 512, 512, 512, 1500),			__a=(5, 3, 3, 1, 1),			__a=(1, 2, 3, 1, 1),			__a=512,			__a=0,			__a=1,			__a=2,			__a=False,			__a=3,			__a=2,			__a=3,			__a=None,			__a=None,			**__a,			):

														'''simple docstring'''

														super().__init__(**__a,			pad_token_id=__a,			bos_token_id=__a,			eos_token_id=__a)
														_lowerCAmelCase     :							str    =    hidden_size
														_lowerCAmelCase     :							Optional[int]    =    feat_extract_norm
														_lowerCAmelCase     :							Union[str, Any]    =    feat_extract_activation
														_lowerCAmelCase     :							Optional[Any]    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    list(__a)
														_lowerCAmelCase     :							str    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    conv_bias
														_lowerCAmelCase     :							str    =    num_conv_pos_embeddings
														_lowerCAmelCase     :							List[Any]    =    num_conv_pos_embedding_groups
														_lowerCAmelCase     :							str    =    len(self.conv_dim)
														_lowerCAmelCase     :							List[str]    =    num_hidden_layers
														_lowerCAmelCase     :							str    =    intermediate_size
														_lowerCAmelCase     :							Any    =    hidden_act
														_lowerCAmelCase     :							int    =    num_attention_heads
														_lowerCAmelCase     :							Optional[Any]    =    hidden_dropout
														_lowerCAmelCase     :							List[str]    =    attention_dropout
														_lowerCAmelCase     :							Tuple    =    activation_dropout
														_lowerCAmelCase     :							int    =    feat_proj_dropout
														_lowerCAmelCase     :							List[str]    =    final_dropout
														_lowerCAmelCase     :							int    =    layerdrop
														_lowerCAmelCase     :							int    =    layer_norm_eps
														_lowerCAmelCase     :							Union[str, Any]    =    initializer_range
														_lowerCAmelCase     :							str    =    vocab_size
														_lowerCAmelCase     :							Optional[Any]    =    do_stable_layer_norm
														_lowerCAmelCase     :							Any    =    use_weighted_layer_sum

														if (
														    (len(self.conv_stride) != self.num_feat_extract_layers)
														    or (len(self.conv_kernel) != self.num_feat_extract_layers)
														    or (len(self.conv_dim) != self.num_feat_extract_layers)
														):
																					raise ValueError(
																					    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
																					    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
																					    f" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,"
																					    f" `len(config.conv_kernel) = {len(self.conv_kernel)}`.")

														# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
														_lowerCAmelCase     :							str    =    apply_spec_augment
														_lowerCAmelCase     :							Optional[Any]    =    mask_time_prob
														_lowerCAmelCase     :							Optional[int]    =    mask_time_length
														_lowerCAmelCase     :							List[str]    =    mask_time_min_masks
														_lowerCAmelCase     :							Optional[int]    =    mask_feature_prob
														_lowerCAmelCase     :							Optional[int]    =    mask_feature_length
														_lowerCAmelCase     :							List[str]    =    mask_feature_min_masks

														# parameters for pretraining with codevector quantized representations
														_lowerCAmelCase     :							Union[str, Any]    =    num_codevectors_per_group
														_lowerCAmelCase     :							str    =    num_codevector_groups
														_lowerCAmelCase     :							Optional[int]    =    contrastive_logits_temperature
														_lowerCAmelCase     :							Optional[int]    =    feat_quantizer_dropout
														_lowerCAmelCase     :							Optional[int]    =    num_negatives
														_lowerCAmelCase     :							Union[str, Any]    =    codevector_dim
														_lowerCAmelCase     :							Any    =    proj_codevector_dim
														_lowerCAmelCase     :							Optional[int]    =    diversity_loss_weight

														# ctc loss
														_lowerCAmelCase     :							Tuple    =    ctc_loss_reduction
														_lowerCAmelCase     :							Tuple    =    ctc_zero_infinity

														# adapter
														_lowerCAmelCase     :							List[Any]    =    add_adapter
														_lowerCAmelCase     :							List[str]    =    adapter_kernel_size
														_lowerCAmelCase     :							str    =    adapter_stride
														_lowerCAmelCase     :							List[str]    =    num_adapter_layers
														_lowerCAmelCase     :							str    =    output_hidden_size or hidden_size
														_lowerCAmelCase     :							Tuple    =    adapter_attn_dim

														# SequenceClassification-specific parameter. Feel free to ignore for other classes.
														_lowerCAmelCase     :							str    =    classifier_proj_size

														# XVector-specific parameters. Feel free to ignore for other classes.
														_lowerCAmelCase     :							str    =    list(__a)
														_lowerCAmelCase     :							Union[str, Any]    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    list(__a)
														_lowerCAmelCase     :							Tuple    =    xvector_output_dim







							@property
							def    snake_case__      (   self):

														'''simple docstring'''

														return functools.reduce(operator.mul,			self.conv_stride,			1)
