




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						:						int     		= XGLMConfig
					__a						:						Dict     		= {}
					__a						:						Tuple     		= 'gelu'
					def __init__(    self						,	__a						,	__a=14						,	__a=7						,	__a=True						,	__a=True						,	__a=True						,	__a=99						,	__a=32						,	__a=2						,	__a=4						,	__a=37						,	__a="gelu"						,	__a=0.1						,	__a=0.1						,	__a=512						,	__a=0.02						,	):
								__lowerCamelCase					:	Union[str, Any]	       =				parent
								__lowerCamelCase					:	Optional[int]	       =				batch_size
								__lowerCamelCase					:	Optional[Any]	       =				seq_length
								__lowerCamelCase					:	Tuple	       =				is_training
								__lowerCamelCase					:	Any	       =				use_input_mask
								__lowerCamelCase					:	str	       =				use_labels
								__lowerCamelCase					:	Any	       =				vocab_size
								__lowerCamelCase					:	str	       =				d_model
								__lowerCamelCase					:	List[Any]	       =				num_hidden_layers
								__lowerCamelCase					:	Union[str, Any]	       =				num_attention_heads
								__lowerCamelCase					:	Dict	       =				ffn_dim
								__lowerCamelCase					:	Dict	       =				activation_function
								__lowerCamelCase					:	Optional[int]	       =				activation_dropout
								__lowerCamelCase					:	Optional[Any]	       =				attention_dropout
								__lowerCamelCase					:	List[str]	       =				max_position_embeddings
								__lowerCamelCase					:	Any	       =				initializer_range
								__lowerCamelCase					:	int	       =				None
								__lowerCamelCase					:	Tuple	       =				0
								__lowerCamelCase					:	Optional[int]	       =				2
								__lowerCamelCase					:	str	       =				1
					def     snake_case_				(    self     ):
								return XGLMConfig.from_pretrained('facebook/xglm-564M'     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Any	       =				tf.clip_by_value(
								    ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size     )						,	clip_value_min=0						,	clip_value_max=3     )

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

								__lowerCamelCase					:	int	       =				self.get_config()

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

								return (
								    config,
								    input_ids,
								    input_mask,
								    head_mask,
								)
					def     snake_case_				(    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=__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     snake_case_				(    self     ):
								__lowerCamelCase					:	Any	       =				self.prepare_config_and_inputs()

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

								__lowerCamelCase					:	Dict	       =				{
								    'input_ids': input_ids,
								    'head_mask': head_mask,
								}

								return config, inputs_dict



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




					'''simple docstring'''


					__a						:						Optional[int]     		= (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
					__a						:						List[str]     		= (TFXGLMForCausalLM,) if is_tf_available() else ()
					__a						:						List[Any]     		= (
					    {'feature-extraction': TFXGLMModel, 'text-generation': TFXGLMForCausalLM} if is_tf_available() else {}
					)
					__a						:						Optional[int]     		= False
					__a						:						Union[str, Any]     		= False
					__a						:						int     		= False
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	str	       =				TFXGLMModelTester(self     )
								__lowerCamelCase					:	Optional[Any]	       =				ConfigTester(self						,	config_class=__a						,	n_embd=37     )
					def     snake_case_				(    self     ):
								self.config_tester.run_common_tests()
					@slow
					def     snake_case_				(    self     ):
								for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCamelCase					:	Union[str, Any]	       =				TFXGLMModel.from_pretrained(__a     )
											self.assertIsNotNone(__a     )





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



@require_tf
class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


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

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

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





					@slow
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	str	       =				TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'     )
								__lowerCamelCase					:	List[str]	       =				XGLMTokenizer.from_pretrained('facebook/xglm-564M'     )

								__lowerCamelCase					:	Optional[Any]	       =				'left'

								# use different length sentences to test batching
								__lowerCamelCase					:	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',
								]

								__lowerCamelCase					:	str	       =				tokenizer(__a						,	return_tensors='tf'						,	padding=__a     )
								__lowerCamelCase					:	Tuple	       =				inputs['input_ids']

								__lowerCamelCase					:	int	       =				model.generate(input_ids=__a						,	attention_mask=inputs['attention_mask']						,	max_new_tokens=12     )

								__lowerCamelCase					:	Tuple	       =				tokenizer(sentences[0]						,	return_tensors='tf'     ).input_ids
								__lowerCamelCase					:	str	       =				model.generate(input_ids=__a						,	max_new_tokens=12     )

								__lowerCamelCase					:	Union[str, Any]	       =				tokenizer(sentences[1]						,	return_tensors='tf'     ).input_ids
								__lowerCamelCase					:	Tuple	       =				model.generate(input_ids=__a						,	max_new_tokens=12     )

								__lowerCamelCase					:	Any	       =				tokenizer.batch_decode(__a						,	skip_special_tokens=__a     )
								__lowerCamelCase					:	Any	       =				tokenizer.decode(output_non_padded[0]						,	skip_special_tokens=__a     )
								__lowerCamelCase					:	Union[str, Any]	       =				tokenizer.decode(output_padded[0]						,	skip_special_tokens=__a     )

								__lowerCamelCase					:	List[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(__a						,	__a     )
								self.assertListEqual(__a						,	[non_padded_sentence, padded_sentence]     )









"""simple docstring"""
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


a_				:    Tuple										=     '''hf-internal-testing/tiny-random-bert'''
a_				:    List[Any]										=     os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
a_				:    Dict										=     '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''

class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	__a     )
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(__a     )     )
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
											self.assertTrue(os.path.isdir(os.path.join(__a						,	__a     )     )     )
								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Optional[int]	       =				f.read()
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
								self.assertTrue(os.path.isfile(__a     )     )

								# File is cached at the same place the second time.
								__lowerCamelCase					:	Optional[Any]	       =				cached_file(__a						,	__a     )
								self.assertEqual(__a						,	__a     )

								# Using a specific revision to test the full commit hash.
								__lowerCamelCase					:	Optional[int]	       =				cached_file(__a						,	__a						,	revision='9b8c223'     )
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											__lowerCamelCase					:	List[str]	       =				cached_file('tiny-random-bert'						,	__a     )

								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											__lowerCamelCase					:	int	       =				cached_file(__a						,	__a						,	revision='aaaa'     )

								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	'conf'     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	Dict	       =				cached_file(__a						,	'conf'     )

								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Any	       =				f.read()
								self.assertTrue(os.path.isfile(os.path.join(__a						,	'.no_exist'						,	__a						,	'conf'     )     )     )

								__lowerCamelCase					:	str	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	int	       =				cached_file(__a						,	'conf'						,	local_files_only=__a						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	List[str]	       =				mock.Mock()
								__lowerCamelCase					:	str	       =				500
								__lowerCamelCase					:	Union[str, Any]	       =				{}
								__lowerCamelCase					:	Dict	       =				HTTPError
								__lowerCamelCase					:	Any	       =				{}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch('requests.Session.request'						,	return_value=__a     ) as mock_head:
											__lowerCamelCase					:	Any	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_connection_errors=__a     )
											self.assertIsNone(__a     )
											# This check we did call the fake head request
											mock_head.assert_called()
					def     snake_case_				(    self     ):
								self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
					def     snake_case_				(    self     ):
								# `get_file_from_repo` returns None if the file does not exist
								self.assertIsNone(get_file_from_repo('bert-base-cased'						,	'ahah.txt'     )     )

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											get_file_from_repo('bert-base-case'						,	__a     )

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											get_file_from_repo('bert-base-cased'						,	__a						,	revision='ahaha'     )

								__lowerCamelCase					:	Optional[int]	       =				get_file_from_repo('bert-base-cased'						,	__a     )
								# The name is the cached name which is not very easy to test, so instead we load the content.
								__lowerCamelCase					:	int	       =				json.loads(open(__a						,	'r'     ).read()     )
								self.assertEqual(config['hidden_size']						,	768     )





					def     snake_case_				(    self     ):
								with tempfile.TemporaryDirectory() as tmp_dir:
											__lowerCamelCase					:	int	       =				Path(__a     ) / 'a.txt'
											filename.touch()
											self.assertEqual(get_file_from_repo(__a						,	'a.txt'     )						,	str(__a     )     )

											self.assertIsNone(get_file_from_repo(__a						,	'b.txt'     )     )



