


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




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




							def   __a   (     self:    Optional[Any]   ):
										return XGLMConfig.from_pretrained("""facebook/xglm-564M"""   )




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

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

										__lowerCamelCase          =      self.get_config()

										__lowerCamelCase          =      floats_tensor([self.num_hidden_layers, self.num_attention_heads]     ,							2   )

										return (
										    config,
										    input_ids,
										    input_mask,
										    head_mask,
										)




							def   __a   (     self:    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   __a   (     self:    Union[str, Any]   ):
										__lowerCamelCase          =      self.prepare_config_and_inputs()

										(
										    (
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,(
										    __lowerCamelCase
										)     ,
										)          =      config_and_inputs

										__lowerCamelCase          =      {
										    """input_ids""": input_ids,
										    """head_mask""": head_mask,
										}

										return config, inputs_dict




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




							def   __a   (     self:    int   ):
										__lowerCamelCase          =      TFXGLMModelTester(self   )
										__lowerCamelCase          =      ConfigTester(self     ,							config_class=A_     ,							n_embd=37   )




							def   __a   (     self:    Optional[int]   ):
										self.config_tester.run_common_tests()




							@slow
							def   __a   (     self:    Optional[Any]   ):
										for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowerCamelCase          =      TFXGLMModel.from_pretrained(A_   )
													self.assertIsNotNone(A_   )




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




@require_tf
class 			__snake_case   (unittest.TestCase  ):




							@slow
							def   __a   (     self:    Optional[Any]     ,							A_:    int=True   ):
										__lowerCamelCase          =      TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""   )
										__lowerCamelCase          =      tf.convert_to_tensor([[2, 2_68, 98_65]]     ,							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
										__lowerCamelCase          =      [2, 2_68, 98_65, 67, 11, 19_88, 5_72_52, 98_65, 5, 9_84, 67, 19_88, 21_38_38, 16_58, 53, 7_04_46, 33, 66_57, 2_78, 15_81]
										# fmt: on
										__lowerCamelCase          =      model.generate(A_     ,							do_sample=A_     ,							num_beams=1   )
										if verify_outputs:
													self.assertListEqual(output_ids[0].numpy().tolist()     ,							A_   )




							@slow
							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      XGLMTokenizer.from_pretrained("""facebook/xglm-564M"""   )
										__lowerCamelCase          =      TFXGLMForCausalLM.from_pretrained("""facebook/xglm-564M"""   )

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

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




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

										__lowerCamelCase          =      """left"""

										# use different length sentences to test batching
										__lowerCamelCase          =      [
										    """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""",
										]

										__lowerCamelCase          =      tokenizer(A_     ,							return_tensors="""tf"""     ,							padding=A_   )
										__lowerCamelCase          =      inputs["""input_ids"""]

										__lowerCamelCase          =      model.generate(input_ids=A_     ,							attention_mask=inputs["""attention_mask"""]     ,							max_new_tokens=12   )

										__lowerCamelCase          =      tokenizer(sentences[0]     ,							return_tensors="""tf"""   ).input_ids
										__lowerCamelCase          =      model.generate(input_ids=A_     ,							max_new_tokens=12   )

										__lowerCamelCase          =      tokenizer(sentences[1]     ,							return_tensors="""tf"""   ).input_ids
										__lowerCamelCase          =      model.generate(input_ids=A_     ,							max_new_tokens=12   )

										__lowerCamelCase          =      tokenizer.batch_decode(A_     ,							skip_special_tokens=A_   )
										__lowerCamelCase          =      tokenizer.decode(output_non_padded[0]     ,							skip_special_tokens=A_   )
										__lowerCamelCase          =      tokenizer.decode(output_padded[0]     ,							skip_special_tokens=A_   )

										__lowerCamelCase          =      [
										    """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]   )








"""simple docstring"""




from collections.abc import Callable





def       a_ (	lowercase__ :Callable[[float], float],			lowercase__ :float,			lowercase__ :float     ):
			__lowerCamelCase          =      a
			__lowerCamelCase          =      b
			if function(lowercase__     ) == 0:  # one of the a or b is a root for the function
						return a
			elif function(lowercase__     ) == 0:
						return b
			elif (
			    function(lowercase__     ) * function(lowercase__     ) > 0
			):  # if none of these are root and they are both positive or negative,
						# then this algorithm can't find the root
						raise ValueError("""could not find root in given interval."""     )
			else:
						__lowerCamelCase          =      start + (end - start) / 2.0
						while abs(start - mid     ) > 10**-7:  # until precisely equals to 10^-7
									if function(lowercase__     ) == 0:
												return mid
									elif function(lowercase__     ) * function(lowercase__     ) < 0:
												__lowerCamelCase          =      mid
									else:
												__lowerCamelCase          =      mid
									__lowerCamelCase          =      start + (end - start) / 2.0
						return mid





def       a_ (	lowercase__ :float     ):
			return x**3 - 2 * x - 5


if __name__ == "__main__":
					print(bisection(f, 1, 1_0_0_0))

					import doctest

					doctest.testmod()




