


'''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     lowercase__  :





							'''simple docstring'''


							A_							:      Dict      	=  XGLMConfig
							A_							:      List[Any]      	=  {}
							A_							:      Optional[int]      	=  """gelu"""






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






							def UpperCAmelCase_	(    self  ):
											return XGLMConfig.from_pretrained("""facebook/xglm-564M"""  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      tf.clip_by_value(
											    ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size  )    ,					clip_value_min=0    ,					clip_value_max=3  )

											_SCREAMING_SNAKE_CASE :       Any     		=      None
											if self.use_input_mask:
															_SCREAMING_SNAKE_CASE :       Tuple     		=      random_attention_mask([self.batch_size, self.seq_length]  )

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.get_config()

											_SCREAMING_SNAKE_CASE :       Tuple     		=      floats_tensor([self.num_hidden_layers, self.num_attention_heads]    ,					2  )

											return (
											    config,
											    input_ids,
											    input_mask,
											    head_mask,
											)






							def UpperCAmelCase_	(    self  ):
											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=__snake_case    ,					bos_token_id=self.bos_token_id    ,					eos_token_id=self.eos_token_id    ,					pad_token_id=self.pad_token_id    ,					return_dict=__snake_case    ,					)






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Dict     		=      self.prepare_config_and_inputs()

											(
											    (
											    _SCREAMING_SNAKE_CASE
											)		,       (
											    _SCREAMING_SNAKE_CASE
											)		,       (
											    _SCREAMING_SNAKE_CASE
											)		,       (
											    _SCREAMING_SNAKE_CASE
											)		,       
											) :       Union[str, Any]     		=      config_and_inputs

											_SCREAMING_SNAKE_CASE :       Dict     		=      {
											    """input_ids""": input_ids,
											    """head_mask""": head_mask,
											}

											return config, inputs_dict





@require_tf
class     lowercase__  (						_snake_case     ,     _snake_case     ,     unittest.TestCase					):





							'''simple docstring'''


							A_							:      List[Any]      	=  (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							A_							:      Union[str, Any]      	=  (TFXGLMForCausalLM,) if is_tf_available() else ()
							A_							:      List[str]      	=  (
							    {"""feature-extraction""": TFXGLMModel, """text-generation""": TFXGLMForCausalLM} if is_tf_available() else {}
							)
							A_							:      Union[str, Any]      	=  False
							A_							:      Optional[Any]      	=  False
							A_							:      Union[str, Any]      	=  False






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      TFXGLMModelTester(self  )
											_SCREAMING_SNAKE_CASE :       Any     		=      ConfigTester(self    ,					config_class=__snake_case    ,					n_embd=37  )






							def UpperCAmelCase_	(    self  ):
											self.config_tester.run_common_tests()






							@slow
							def UpperCAmelCase_	(    self  ):
											for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      TFXGLMModel.from_pretrained(__snake_case  )
															self.assertIsNotNone(__snake_case  )






							@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""  )
							def UpperCAmelCase_	(    self  ):
											super().test_resize_token_embeddings()





@require_tf
class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							@slow
							def UpperCAmelCase_	(    self    ,					__snake_case=True  ):
											_SCREAMING_SNAKE_CASE :       str     		=      TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""  )
											_SCREAMING_SNAKE_CASE :       Dict     		=      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
											_SCREAMING_SNAKE_CASE :       Dict     		=      [2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581]
											# fmt: on
											_SCREAMING_SNAKE_CASE :       int     		=      model.generate(__snake_case    ,					do_sample=__snake_case    ,					num_beams=1  )
											if verify_outputs:
															self.assertListEqual(output_ids[0].numpy().tolist()    ,					__snake_case  )






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

											tf.random.set_seed(0  )
											_SCREAMING_SNAKE_CASE :       int     		=      tokenizer("""Today is a nice day and"""    ,					return_tensors="""tf"""  )
											_SCREAMING_SNAKE_CASE :       Optional[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"""  ):
															_SCREAMING_SNAKE_CASE :       Dict     		=      model.generate(__snake_case    ,					do_sample=__snake_case    ,					seed=[7, 0]  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      tokenizer.decode(output_ids[0]    ,					skip_special_tokens=__snake_case  )

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






							@slow
							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       str     		=      TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""  )
											_SCREAMING_SNAKE_CASE :       Any     		=      XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""  )

											_SCREAMING_SNAKE_CASE :       Dict     		=      """left"""

											# use different length sentences to test batching
											_SCREAMING_SNAKE_CASE :       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""",
											    """Hello, my dog is a little""",
											]

											_SCREAMING_SNAKE_CASE :       str     		=      tokenizer(__snake_case    ,					return_tensors="""tf"""    ,					padding=__snake_case  )
											_SCREAMING_SNAKE_CASE :       List[str]     		=      inputs["""input_ids"""]

											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      model.generate(input_ids=__snake_case    ,					attention_mask=inputs["""attention_mask"""]    ,					max_new_tokens=12  )

											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      tokenizer(sentences[0]    ,					return_tensors="""tf"""  ).input_ids
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      model.generate(input_ids=__snake_case    ,					max_new_tokens=12  )

											_SCREAMING_SNAKE_CASE :       int     		=      tokenizer(sentences[1]    ,					return_tensors="""tf"""  ).input_ids
											_SCREAMING_SNAKE_CASE :       Dict     		=      model.generate(input_ids=__snake_case    ,					max_new_tokens=12  )

											_SCREAMING_SNAKE_CASE :       str     		=      tokenizer.batch_decode(__snake_case    ,					skip_special_tokens=__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      tokenizer.decode(output_non_padded[0]    ,					skip_special_tokens=__snake_case  )
											_SCREAMING_SNAKE_CASE :       List[str]     		=      tokenizer.decode(output_padded[0]    ,					skip_special_tokens=__snake_case  )

											_SCREAMING_SNAKE_CASE :       Optional[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(__snake_case    ,					__snake_case  )
											self.assertListEqual(__snake_case    ,					[non_padded_sentence, padded_sentence]  )








'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_		:   Union[str, Any]							   =		logging.get_logger(__name__)

UpperCAmelCase_		:   Optional[int]							   =		{
    'google/bigbird-roberta-base': 'https://huggingface.co/google/bigbird-roberta-base/resolve/main/config.json',
    'google/bigbird-roberta-large': 'https://huggingface.co/google/bigbird-roberta-large/resolve/main/config.json',
    'google/bigbird-base-trivia-itc': 'https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/config.json',
    # See all BigBird models at https://huggingface.co/models?filter=big_bird
}





class     lowercase__  (						_snake_case					):





							'''simple docstring'''


							A_							:      str      	=  """big_bird"""






							def __init__(    self    ,					__snake_case=5_0358    ,					__snake_case=768    ,					__snake_case=12    ,					__snake_case=12    ,					__snake_case=3072    ,					__snake_case="gelu_new"    ,					__snake_case=0.1    ,					__snake_case=0.1    ,					__snake_case=4096    ,					__snake_case=2    ,					__snake_case=0.02    ,					__snake_case=1e-12    ,					__snake_case=True    ,					__snake_case=0    ,					__snake_case=1    ,					__snake_case=2    ,					__snake_case=66    ,					__snake_case="block_sparse"    ,					__snake_case=True    ,					__snake_case=False    ,					__snake_case=64    ,					__snake_case=3    ,					__snake_case=None    ,					**__snake_case    ,					):
											super().__init__(
											    pad_token_id=__snake_case    ,					bos_token_id=__snake_case    ,					eos_token_id=__snake_case    ,					sep_token_id=__snake_case    ,					**__snake_case    ,					)

											_SCREAMING_SNAKE_CASE :       str     		=      vocab_size
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      max_position_embeddings
											_SCREAMING_SNAKE_CASE :       List[str]     		=      hidden_size
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      num_hidden_layers
											_SCREAMING_SNAKE_CASE :       Any     		=      num_attention_heads
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      intermediate_size
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      hidden_act
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      hidden_dropout_prob
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE :       Tuple     		=      initializer_range
											_SCREAMING_SNAKE_CASE :       Any     		=      type_vocab_size
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      layer_norm_eps
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      use_cache

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      rescale_embeddings
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      attention_type
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      use_bias
											_SCREAMING_SNAKE_CASE :       int     		=      block_size
											_SCREAMING_SNAKE_CASE :       Any     		=      num_random_blocks
											_SCREAMING_SNAKE_CASE :       List[str]     		=      classifier_dropout





class     lowercase__  (						_snake_case					):





							'''simple docstring'''


							@property
							def UpperCAmelCase_	(    self  ):
											if self.task == "multiple-choice":
															_SCREAMING_SNAKE_CASE :       int     		=      {0: """batch""", 1: """choice""", 2: """sequence"""}
											else:
															_SCREAMING_SNAKE_CASE :       Optional[int]     		=      {0: """batch""", 1: """sequence"""}
											return OrderedDict(
											    [
											        ("""input_ids""", dynamic_axis),
											        ("""attention_mask""", dynamic_axis),
											    ]  )




