



"""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__			:

			"""simple docstring"""


			A__	:					List[str]							  =     XGLMConfig
			A__	:					List[Any]							  =     {}
			A__	:					Optional[Any]							  =     "gelu"


			def __init__(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__=14						,	SCREAMING_SNAKE_CASE__=7						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=True						,	SCREAMING_SNAKE_CASE__=99						,	SCREAMING_SNAKE_CASE__=32						,	SCREAMING_SNAKE_CASE__=2						,	SCREAMING_SNAKE_CASE__=4						,	SCREAMING_SNAKE_CASE__=37						,	SCREAMING_SNAKE_CASE__="gelu"						,	SCREAMING_SNAKE_CASE__=0.1						,	SCREAMING_SNAKE_CASE__=0.1						,	SCREAMING_SNAKE_CASE__=512						,	SCREAMING_SNAKE_CASE__=0.0_2						,	)      ->	int:
				A__											=					parent
				A__											=					batch_size
				A__											=					seq_length
				A__											=					is_training
				A__											=					use_input_mask
				A__											=					use_labels
				A__											=					vocab_size
				A__											=					d_model
				A__											=					num_hidden_layers
				A__											=					num_attention_heads
				A__											=					ffn_dim
				A__											=					activation_function
				A__											=					activation_dropout
				A__											=					attention_dropout
				A__											=					max_position_embeddings
				A__											=					initializer_range
				A__											=					None
				A__											=					0
				A__											=					2
				A__											=					1


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


			def   snake_case__	(      self		)      ->	int:
				A__											=					tf.clip_by_value(
				    ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size		)						,	clip_value_min=0						,	clip_value_max=3		)

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

				A__											=					self.get_config()

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

				return (
				    config,
				    input_ids,
				    input_mask,
				    head_mask,
				)


			def   snake_case__	(      self		)      ->	str:
				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=SCREAMING_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=SCREAMING_SNAKE_CASE__						,	)







			def   snake_case__	(      self		)      ->	List[str]:
				A__											=					self.prepare_config_and_inputs()

				(
				    (
				    A__
				)							,							(
				    A__
				)							,							(
				    A__
				)							,							(
				    A__
				)							,							
				)											=					config_and_inputs

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

				return config, inputs_dict






@require_tf
class       UpperCamelCase__			(   _lowerCAmelCase       ,		_lowerCAmelCase       ,		unittest.TestCase      ):

			"""simple docstring"""


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


			def   snake_case__	(      self		)      ->	Optional[int]:
				A__											=					TFXGLMModelTester(self		)
				A__											=					ConfigTester(self						,	config_class=SCREAMING_SNAKE_CASE__						,	n_embd=37		)


			def   snake_case__	(      self		)      ->	Tuple:
				self.config_tester.run_common_tests()


			@slow
			def   snake_case__	(      self		)      ->	Tuple:
				for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					A__											=					TFXGLMModel.from_pretrained(SCREAMING_SNAKE_CASE__		)
					self.assertIsNotNone(SCREAMING_SNAKE_CASE__		)







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






@require_tf
class       UpperCamelCase__			(   unittest.TestCase      ):

			"""simple docstring"""


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


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

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

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







			@slow
			def   snake_case__	(      self		)      ->	int:
				A__											=					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"		)
				A__											=					XGLMTokenizer.from_pretrained("facebook/xglm-564M"		)

				A__											=					"left"

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

				A__											=					tokenizer(SCREAMING_SNAKE_CASE__						,	return_tensors="tf"						,	padding=SCREAMING_SNAKE_CASE__		)
				A__											=					inputs["input_ids"]

				A__											=					model.generate(input_ids=SCREAMING_SNAKE_CASE__						,	attention_mask=inputs["attention_mask"]						,	max_new_tokens=12		)

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

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

				A__											=					tokenizer.batch_decode(SCREAMING_SNAKE_CASE__						,	skip_special_tokens=SCREAMING_SNAKE_CASE__		)
				A__											=					tokenizer.decode(output_non_padded[0]						,	skip_special_tokens=SCREAMING_SNAKE_CASE__		)
				A__											=					tokenizer.decode(output_padded[0]						,	skip_special_tokens=SCREAMING_SNAKE_CASE__		)

				A__											=					[
				    "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(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)
				self.assertListEqual(SCREAMING_SNAKE_CASE__						,	[non_padded_sentence, padded_sentence]		)





'''simple docstring'''






from typing import Dict, List

from nltk.translate import gleu_score

import datasets
from datasets import MetricInfo


_UpperCAmelCase    :     Any         =       '''\
@misc{wu2016googles,
      title={Google\'s Neural Machine Translation System: Bridging the Gap between Human and Machine Translation},
      author={Yonghui Wu and Mike Schuster and Zhifeng Chen and Quoc V. Le and Mohammad Norouzi and Wolfgang Macherey
              and Maxim Krikun and Yuan Cao and Qin Gao and Klaus Macherey and Jeff Klingner and Apurva Shah and Melvin
              Johnson and Xiaobing Liu and Łukasz Kaiser and Stephan Gouws and Yoshikiyo Kato and Taku Kudo and Hideto
              Kazawa and Keith Stevens and George Kurian and Nishant Patil and Wei Wang and Cliff Young and
              Jason Smith and Jason Riesa and Alex Rudnick and Oriol Vinyals and Greg Corrado and Macduff Hughes
              and Jeffrey Dean},
      year={2016},
      eprint={1609.08144},
      archivePrefix={arXiv},
      primaryClass={cs.CL}
}
'''

_UpperCAmelCase    :     str         =       '''\
The BLEU score has some undesirable properties when used for single
sentences, as it was designed to be a corpus measure. We therefore
use a slightly different score for our RL experiments which we call
the \'GLEU score\'. For the GLEU score, we record all sub-sequences of
1, 2, 3 or 4 tokens in output and target sequence (n-grams). We then
compute a recall, which is the ratio of the number of matching n-grams
to the number of total n-grams in the target (ground truth) sequence,
and a precision, which is the ratio of the number of matching n-grams
to the number of total n-grams in the generated output sequence. Then
GLEU score is simply the minimum of recall and precision. This GLEU
score\'s range is always between 0 (no matches) and 1 (all match) and
it is symmetrical when switching output and target. According to
our experiments, GLEU score correlates quite well with the BLEU
metric on a corpus level but does not have its drawbacks for our per
sentence reward objective.
'''

_UpperCAmelCase    :     Union[str, Any]         =       '''\
Computes corpus-level Google BLEU (GLEU) score of translated segments against one or more references.
Instead of averaging the sentence level GLEU scores (i.e. macro-average precision), Wu et al. (2016) sum up the matching
tokens and the max of hypothesis and reference tokens for each sentence, then compute using the aggregate values.

Args:
    predictions (list of str): list of translations to score.
        Each translation should be tokenized into a list of tokens.
    references (list of list of str): list of lists of references for each translation.
        Each reference should be tokenized into a list of tokens.
    min_len (int): The minimum order of n-gram this function should extract. Defaults to 1.
    max_len (int): The maximum order of n-gram this function should extract. Defaults to 4.

Returns:
    \'google_bleu\': google_bleu score

Examples:
    Example 1:
        >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',
        ...         \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',
        ...         \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']
        >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',
        ...          \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',
        ...          \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']

        >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',
        ...         \'interested\', \'in\', \'world\', \'history\']
        >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',
        ...          \'because\', \'he\', \'read\', \'the\', \'book\']

        >>> list_of_references = [[ref1a], [ref2a]]
        >>> hypotheses = [hyp1, hyp2]
        >>> google_bleu = datasets.load_metric("google_bleu")
        >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)
        >>> print(round(results["google_bleu"], 2))
        0.44

    Example 2:
        >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',
        ...         \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',
        ...         \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']
        >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',
        ...          \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',
        ...          \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']
        >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',
        ...          \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',
        ...          \'heed\', \'the\', \'cat\', \'commands\']
        >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',
        ...          \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',
        ...          \'of\', \'the\', \'cat\']

        >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',
        ...         \'interested\', \'in\', \'world\', \'history\']
        >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',
        ...          \'because\', \'he\', \'read\', \'the\', \'book\']

        >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]
        >>> hypotheses = [hyp1, hyp2]
        >>> google_bleu = datasets.load_metric("google_bleu")
        >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references)
        >>> print(round(results["google_bleu"], 2))
        0.61

    Example 3:
        >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',
        ...         \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',
        ...         \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']
        >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',
        ...          \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',
        ...          \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']
        >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',
        ...          \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',
        ...          \'heed\', \'the\', \'cat\', \'commands\']
        >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',
        ...          \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',
        ...          \'of\', \'the\', \'cat\']

        >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',
        ...         \'interested\', \'in\', \'world\', \'history\']
        >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',
        ...          \'because\', \'he\', \'read\', \'the\', \'book\']

        >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]
        >>> hypotheses = [hyp1, hyp2]
        >>> google_bleu = datasets.load_metric("google_bleu")
        >>> results = google_bleu.compute(predictions=hypotheses, references=list_of_references, min_len=2)
        >>> print(round(results["google_bleu"], 2))
        0.53

    Example 4:
        >>> hyp1 = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'which\',
        ...         \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'always\',
        ...         \'disobeys\', \'the\', \'commands\', \'of\', \'the\', \'cat\']
        >>> ref1a = [\'It\', \'is\', \'the\', \'guiding\', \'principle\', \'which\',
        ...          \'guarantees\', \'the\', \'rubber\', \'duck\', \'forces\', \'never\',
        ...          \'being\', \'under\', \'the\', \'command\', \'of\', \'the\', \'cat\']
        >>> ref1b = [\'It\', \'is\', \'a\', \'guide\', \'to\', \'action\', \'that\',
        ...          \'ensures\', \'that\', \'the\', \'rubber\', \'duck\', \'will\', \'never\',
        ...          \'heed\', \'the\', \'cat\', \'commands\']
        >>> ref1c = [\'It\', \'is\', \'the\', \'practical\', \'guide\', \'for\', \'the\',
        ...          \'rubber\', \'duck\', \'army\', \'never\', \'to\', \'heed\', \'the\', \'directions\',
        ...          \'of\', \'the\', \'cat\']

        >>> hyp2 = [\'he\', \'read\', \'the\', \'book\', \'because\', \'he\', \'was\',
        ...         \'interested\', \'in\', \'world\', \'history\']
        >>> ref2a = [\'he\', \'was\', \'interested\', \'in\', \'world\', \'history\',
        ...          \'because\', \'he\', \'read\', \'the\', \'book\']

        >>> list_of_references = [[ref1a, ref1b, ref1c], [ref2a]]
        >>> hypotheses = [hyp1, hyp2]
        >>> google_bleu = datasets.load_metric("google_bleu")
        >>> results = google_bleu.compute(predictions=hypotheses,references=list_of_references, min_len=2, max_len=6)
        >>> print(round(results["google_bleu"], 2))
        0.4
'''







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    ,    _KWARGS_DESCRIPTION			)
class lowercase_       (		datasets.Metric			):







   """simple docstring"""

   def 	__UpperCAmelCase   (      self      :			List[Any]     )  ->			MetricInfo:
        return datasets.MetricInfo(
            description=_DESCRIPTION, citation=_CITATION, inputs_description=_KWARGS_DESCRIPTION, features=datasets.Features(
                {
                    'predictions': datasets.Sequence(datasets.Value('string', id='token'     ), id='sequence'     ),
                    'references': datasets.Sequence(
                        datasets.Sequence(datasets.Value('string', id='token'     ), id='sequence'     ), id='references'     ),
                }     ), )

   def 	__UpperCAmelCase   (      self      :			Dict, UpperCamelCase__      :			List[List[List[str]]], UpperCamelCase__      :			List[List[str]], UpperCamelCase__      :			int = 1, UpperCamelCase__      :			int = 4, )  ->			Dict[str, float]:
        return {
            "google_bleu": gleu_score.corpus_gleu(
                list_of_references=UpperCamelCase__, hypotheses=UpperCamelCase__, min_len=UpperCamelCase__, max_len=UpperCamelCase__     )
        }



