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







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







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







   def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
          lowercase_      :    Optional[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_      :    Any       		= random_attention_mask([self.batch_size, self.seq_length]					)

          lowercase_      :    str       		= self.get_config()

          lowercase_      :    List[Any]       		= floats_tensor([self.num_hidden_layers, self.num_attention_heads]       ,    2					)

          return (
              config,
              input_ids,
              input_mask,
              head_mask,
          )







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







   def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
          lowercase_      :    Any       		= self.prepare_config_and_inputs()

          (
              (
              lowercase_     
          )		,		(
              lowercase_     
          )		,		(
              lowercase_     
          )		,		(
              lowercase_     
          )		,		     
          )      :    str       		= config_and_inputs

          lowercase_      :    List[str]       		= {
              """input_ids""": input_ids,
              """head_mask""": head_mask,
          }

          return config, inputs_dict





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







   def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
          lowercase_      :    List[Any]       		= TFXGLMModelTester(self					)
          lowercase_      :    Any       		= ConfigTester(self       ,    config_class=lowercase_       ,    n_embd=37					)







   def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
          self.config_tester.run_common_tests()







   @slow
   def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):

          for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowercase_      :    str       		= TFXGLMModel.from_pretrained(lowercase_					)
                 self.assertIsNotNone(lowercase_					)







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





@require_tf
class 					__magic_name__		(					unittest.TestCase):







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







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

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

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







   @slow
   def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
          lowercase_      :    Tuple       		= TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""					)
          lowercase_      :    Any       		= XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""					)

          lowercase_      :    Tuple       		= """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_      :    List[Any]       		= tokenizer(lowercase_       ,    return_tensors="""tf"""       ,    padding=lowercase_					)
          lowercase_      :    int       		= inputs["""input_ids"""]

          lowercase_      :    Optional[int]       		= model.generate(input_ids=lowercase_       ,    attention_mask=inputs["""attention_mask"""]       ,    max_new_tokens=12					)

          lowercase_      :    Dict       		= tokenizer(sentences[0]       ,    return_tensors="""tf"""					).input_ids
          lowercase_      :    Union[str, Any]       		= model.generate(input_ids=lowercase_       ,    max_new_tokens=12					)

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

          lowercase_      :    int       		= tokenizer.batch_decode(lowercase_       ,    skip_special_tokens=lowercase_					)
          lowercase_      :    str       		= tokenizer.decode(output_non_padded[0]       ,    skip_special_tokens=lowercase_					)
          lowercase_      :    str       		= tokenizer.decode(output_padded[0]       ,    skip_special_tokens=lowercase_					)

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






'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_lowercase		:				Dict	       =		{
    "configuration_bloom": ["BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP", "BloomConfig", "BloomOnnxConfig"],
}
try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase		:				Dict	       =		["BloomTokenizerFast"]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase		:				Dict	       =		[
						    "BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "BloomForCausalLM",
						    "BloomModel",
						    "BloomPreTrainedModel",
						    "BloomForSequenceClassification",
						    "BloomForTokenClassification",
						    "BloomForQuestionAnswering",
						]

if TYPE_CHECKING:
						from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_bloom_fast import BloomTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_bloom import (
												    BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    BloomForCausalLM,
												    BloomForQuestionAnswering,
												    BloomForSequenceClassification,
												    BloomForTokenClassification,
												    BloomModel,
												    BloomPreTrainedModel,
												)

else:
						import sys

						_lowercase		:				Optional[int]	       =		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





