





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    _a     :






							"""simple docstring"""

							snake_case_							 =						XGLMConfig
							snake_case_							 =						{}
							snake_case_							 =						"gelu"

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

							def      A_  (      self				:     Optional[Any]    )	->Union[str, Any]:
													return XGLMConfig.from_pretrained("facebook/xglm-564M"    )

							def      A_  (      self				:     Tuple    )	->Union[str, Any]:
													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__     :				Optional[int]   =					random_attention_mask([self.batch_size, self.seq_length]    )

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

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

													return (
													    config,
													    input_ids,
													    input_mask,
													    head_mask,
													)

							def      A_  (      self				:     Optional[int]    )	->int:
													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				:     Optional[int]    )	->Tuple:
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					self.prepare_config_and_inputs()

													(
													    (
													    SCREAMING_SNAKE_CASE__						
													),							(
													    SCREAMING_SNAKE_CASE__						
													),							(
													    SCREAMING_SNAKE_CASE__						
													),							(
													    SCREAMING_SNAKE_CASE__						
													),													
													)     :				Optional[Any]   =					config_and_inputs

													SCREAMING_SNAKE_CASE__     :				Tuple   =					{
													    "input_ids": input_ids,
													    "head_mask": head_mask,
													}

													return config, inputs_dict







@require_tf
class    _a     (				_SCREAMING_SNAKE_CASE					,							_SCREAMING_SNAKE_CASE					,							unittest.TestCase							):






							"""simple docstring"""

							snake_case_							 =						(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
							snake_case_							 =						(TFXGLMForCausalLM,) if is_tf_available() else ()
							snake_case_							 =						(
							    {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
							)
							snake_case_							 =						False
							snake_case_							 =						False
							snake_case_							 =						False

							def      A_  (      self				:     Optional[Any]    )	->Tuple:
													SCREAMING_SNAKE_CASE__     :				List[str]   =					TFXGLMModelTester(self    )
													SCREAMING_SNAKE_CASE__     :				Any   =					ConfigTester(self							,	config_class=A_							,	n_embd=37    )

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

							@slow
							def      A_  (      self				:     Optional[int]    )	->Dict:
													for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			SCREAMING_SNAKE_CASE__     :				List[str]   =					TFXGLMModel.from_pretrained(A_    )
																			self.assertIsNotNone(A_    )






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







@require_tf
class    _a     (				unittest.TestCase							):






							"""simple docstring"""

							@slow
							def      A_  (      self				:     str							,	a				:     Any=True    )	->List[str]:
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"    )
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					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
													SCREAMING_SNAKE_CASE__     :				Any   =					[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
													SCREAMING_SNAKE_CASE__     :				Dict   =					model.generate(A_							,	do_sample=A_							,	num_beams=1    )
													if verify_outputs:
																			self.assertListEqual(output_ids[0].numpy().tolist()							,	A_    )

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

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

													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(A_							,	A_    )






							@slow
							def      A_  (      self				:     Tuple    )	->Tuple:
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					TFXGLMForCausalLM.from_pretrained("facebook/xglm-564M"    )
													SCREAMING_SNAKE_CASE__     :				List[str]   =					XGLMTokenizer.from_pretrained("facebook/xglm-564M"    )

													SCREAMING_SNAKE_CASE__     :				List[str]   =					"left"

													# use different length sentences to test batching
													SCREAMING_SNAKE_CASE__     :				Dict   =					[
													    "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__     :				List[str]   =					tokenizer(A_							,	return_tensors="tf"							,	padding=A_    )
													SCREAMING_SNAKE_CASE__     :				Union[str, Any]   =					inputs["input_ids"]

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

													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					tokenizer(sentences[0]							,	return_tensors="tf"    ).input_ids
													SCREAMING_SNAKE_CASE__     :				Any   =					model.generate(input_ids=A_							,	max_new_tokens=12    )

													SCREAMING_SNAKE_CASE__     :				str   =					tokenizer(sentences[1]							,	return_tensors="tf"    ).input_ids
													SCREAMING_SNAKE_CASE__     :				int   =					model.generate(input_ids=A_							,	max_new_tokens=12    )

													SCREAMING_SNAKE_CASE__     :				Dict   =					tokenizer.batch_decode(A_							,	skip_special_tokens=A_    )
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					tokenizer.decode(output_non_padded[0]							,	skip_special_tokens=A_    )
													SCREAMING_SNAKE_CASE__     :				int   =					tokenizer.decode(output_padded[0]							,	skip_special_tokens=A_    )

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













from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

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


if TYPE_CHECKING:
				from ...onnx.config import PatchingSpec
				from ...tokenization_utils_base import PreTrainedTokenizerBase


__lowercase       :List[Any]       =  logging.get_logger(__name__)

__lowercase       :Optional[int]       =  {
    "allenai/longformer-base-4096": "https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json",
    "allenai/longformer-large-4096": "https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json",
    "allenai/longformer-large-4096-finetuned-triviaqa": (
        "https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json"
    ),
    "allenai/longformer-base-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
    "allenai/longformer-large-4096-extra.pos.embd.only": (
        "https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json"
    ),
}



class    _a     (				lowercase__							):






							"""simple docstring"""

							snake_case_							 =						"longformer"

							def __init__(      self				:     List[str]							,	a				:     Union[List[int], int] = 5_12							,	a				:     int = 2							,	a				:     int = 1							,	a				:     int = 0							,	a				:     int = 2							,	a				:     int = 3_05_22							,	a				:     int = 7_68							,	a				:     int = 12							,	a				:     int = 12							,	a				:     int = 30_72							,	a				:     str = "gelu"							,	a				:     float = 0.1							,	a				:     float = 0.1							,	a				:     int = 5_12							,	a				:     int = 2							,	a				:     float = 0.02							,	a				:     float = 1E-12							,	a				:     bool = False							,	**a				:     Dict							,	)	->Tuple:
													super().__init__(pad_token_id=a							,	**a    )

													SCREAMING_SNAKE_CASE__     :				int   =					attention_window
													SCREAMING_SNAKE_CASE__     :				Any   =					sep_token_id
													SCREAMING_SNAKE_CASE__     :				str   =					bos_token_id
													SCREAMING_SNAKE_CASE__     :				List[str]   =					eos_token_id
													SCREAMING_SNAKE_CASE__     :				List[str]   =					vocab_size
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					hidden_size
													SCREAMING_SNAKE_CASE__     :				List[str]   =					num_hidden_layers
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					num_attention_heads
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					hidden_act
													SCREAMING_SNAKE_CASE__     :				Optional[int]   =					intermediate_size
													SCREAMING_SNAKE_CASE__     :				List[str]   =					hidden_dropout_prob
													SCREAMING_SNAKE_CASE__     :				Dict   =					attention_probs_dropout_prob
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					max_position_embeddings
													SCREAMING_SNAKE_CASE__     :				str   =					type_vocab_size
													SCREAMING_SNAKE_CASE__     :				Any   =					initializer_range
													SCREAMING_SNAKE_CASE__     :				List[Any]   =					layer_norm_eps
													SCREAMING_SNAKE_CASE__     :				Any   =					onnx_export







class    _a     (				lowercase__							):






							"""simple docstring"""

							def __init__(      self				:     int							,	a				:     "PretrainedConfig"							,	a				:     str = "default"							,	a				:     "List[PatchingSpec]" = None    )	->str:
													super().__init__(a							,	a							,	a    )
													SCREAMING_SNAKE_CASE__     :				Any   =					True

							@property
							def      A_  (      self				:     int    )	->Mapping[str, Mapping[int, str]]:
													if self.task == "multiple-choice":
																			SCREAMING_SNAKE_CASE__     :				int   =					{0: "batch", 1: "choice", 2: "sequence"}
													else:
																			SCREAMING_SNAKE_CASE__     :				str   =					{0: "batch", 1: "sequence"}
													return OrderedDict(
													    [
													        ("input_ids", dynamic_axis),
													        ("attention_mask", dynamic_axis),
													        ("global_attention_mask", dynamic_axis),
													    ]    )

							@property
							def      A_  (      self				:     Optional[Any]    )	->Mapping[str, Mapping[int, str]]:
													SCREAMING_SNAKE_CASE__     :				Optional[Any]   =					super().outputs
													if self.task == "default":
																			SCREAMING_SNAKE_CASE__     :				List[str]   =					{0: "batch"}
													return outputs

							@property
							def      A_  (      self				:     str    )	->float:
													return 1E-4

							@property
							def      A_  (      self				:     Any    )	->int:
													# needs to be >= 14 to support tril operator
													return max(super().default_onnx_opset							,	14    )






							def      A_  (      self				:     str							,	a				:     "PreTrainedTokenizerBase"							,	a				:     int = -1							,	a				:     int = -1							,	a				:     bool = False							,	a				:     Optional[TensorType] = None							,	)	->Mapping[str, Any]:
													SCREAMING_SNAKE_CASE__     :				Tuple   =					super().generate_dummy_inputs(
													    preprocessor=a							,	batch_size=a							,	seq_length=a							,	is_pair=a							,	framework=a    )
													import torch

													# for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
													# makes the export fail randomly
													SCREAMING_SNAKE_CASE__     :				Any   =					torch.zeros_like(inputs["input_ids"]    )
													# make every second token global
													SCREAMING_SNAKE_CASE__     :				str   =					1

													return inputs