

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


lowerCAmelCase							:  List[str]            =						"""hf-internal-testing/tiny-random-bert"""
lowerCAmelCase							:  str            =						os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowerCAmelCase							:  Dict            =						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""



class   __lowercase    (			unittest.TestCase     ):




			"""simple docstring"""







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

									# File is cached at the same place the second time.
									SCREAMING_SNAKE_CASE_:			str  		=	cached_file(lowerCAmelCase__       ,						lowerCAmelCase__)
									self.assertEqual(lowerCAmelCase__       ,						lowerCAmelCase__)

									# Using a specific revision to test the full commit hash.
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	cached_file(lowerCAmelCase__       ,						lowerCAmelCase__       ,						revision="9b8c223")
									self.assertEqual(lowerCAmelCase__       ,						os.path.join(lowerCAmelCase__       ,						"snapshots"       ,						lowerCAmelCase__       ,						lowerCAmelCase__))




			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									with self.assertRaisesRegex(lowerCAmelCase__       ,						"is not a valid model identifier"):
															SCREAMING_SNAKE_CASE_:			int  		=	cached_file("tiny-random-bert"       ,						lowerCAmelCase__)

									with self.assertRaisesRegex(lowerCAmelCase__       ,						"is not a valid git identifier"):
															SCREAMING_SNAKE_CASE_:			List[Any]  		=	cached_file(lowerCAmelCase__       ,						lowerCAmelCase__       ,						revision="aaaa")

									with self.assertRaisesRegex(lowerCAmelCase__       ,						"does not appear to have a file named"):
															SCREAMING_SNAKE_CASE_:			List[Any]  		=	cached_file(lowerCAmelCase__       ,						"conf")




			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									with self.assertRaisesRegex(lowerCAmelCase__       ,						"does not appear to have a file named"):
															SCREAMING_SNAKE_CASE_:			Any  		=	cached_file(lowerCAmelCase__       ,						"conf")

									with open(os.path.join(lowerCAmelCase__       ,						"refs"       ,						"main")) as f:
															SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	f.read()
									self.assertTrue(os.path.isfile(os.path.join(lowerCAmelCase__       ,						".no_exist"       ,						lowerCAmelCase__       ,						"conf")))

									SCREAMING_SNAKE_CASE_:			List[str]  		=	cached_file(lowerCAmelCase__       ,						"conf"       ,						_raise_exceptions_for_missing_entries=lowerCAmelCase__)
									self.assertIsNone(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:			List[str]  		=	cached_file(lowerCAmelCase__       ,						"conf"       ,						local_files_only=lowerCAmelCase__       ,						_raise_exceptions_for_missing_entries=lowerCAmelCase__)
									self.assertIsNone(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:			List[Any]  		=	mock.Mock()
									SCREAMING_SNAKE_CASE_:			Dict  		=	500
									SCREAMING_SNAKE_CASE_:			Tuple  		=	{}
									SCREAMING_SNAKE_CASE_:			str  		=	HTTPError
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	{}

									# Under the mock environment we get a 500 error when trying to reach the tokenizer.
									with mock.patch("requests.Session.request"       ,						return_value=lowerCAmelCase__) as mock_head:
															SCREAMING_SNAKE_CASE_:			List[Any]  		=	cached_file(lowerCAmelCase__       ,						"conf"       ,						_raise_exceptions_for_connection_errors=lowerCAmelCase__)
															self.assertIsNone(lowerCAmelCase__)
															# This check we did call the fake head request
															mock_head.assert_called()




			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"       ,						lowerCAmelCase__))
									self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"       ,						lowerCAmelCase__))
									self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"       ,						lowerCAmelCase__))




			def 	_SCREAMING_SNAKE_CASE      (			self  :					int):
									# `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(lowerCAmelCase__       ,						"is not a valid model identifier"):
															get_file_from_repo("bert-base-case"       ,						lowerCAmelCase__)

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

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




			def 	_SCREAMING_SNAKE_CASE      (			self  :					Dict):
									with tempfile.TemporaryDirectory() as tmp_dir:
															SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	Path(lowerCAmelCase__) / "a.txt"
															filename.touch()
															self.assertEqual(get_file_from_repo(lowerCAmelCase__       ,						"a.txt")       ,						str(lowerCAmelCase__))

															self.assertIsNone(get_file_from_repo(lowerCAmelCase__       ,						"b.txt"))









from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase							:  Optional[int]            =						logging.get_logger(__name__)

lowerCAmelCase							:  int            =						{
    """google/fnet-base""": """https://huggingface.co/google/fnet-base/resolve/main/config.json""",
    """google/fnet-large""": """https://huggingface.co/google/fnet-large/resolve/main/config.json"""
    # See all FNet models at https://huggingface.co/models?filter=fnet
}



class   __lowercase    (			UpperCAmelCase_     ):




			"""simple docstring"""







			_UpperCAmelCase       :							int      							=					'''fnet'''



			def __init__(			self  :					Optional[Any]       ,						lowerCAmelCase__  :					List[str]=3_2000       ,						lowerCAmelCase__  :					Union[str, Any]=768       ,						lowerCAmelCase__  :					Optional[Any]=12       ,						lowerCAmelCase__  :					Tuple=3072       ,						lowerCAmelCase__  :					Any="gelu_new"       ,						lowerCAmelCase__  :					Union[str, Any]=0.1       ,						lowerCAmelCase__  :					Optional[Any]=512       ,						lowerCAmelCase__  :					int=4       ,						lowerCAmelCase__  :					List[Any]=0.02       ,						lowerCAmelCase__  :					Union[str, Any]=1E-12       ,						lowerCAmelCase__  :					Optional[int]=False       ,						lowerCAmelCase__  :					Any=512       ,						lowerCAmelCase__  :					Optional[int]=3       ,						lowerCAmelCase__  :					int=1       ,						lowerCAmelCase__  :					List[Any]=2       ,						**lowerCAmelCase__  :					Optional[int]       ,						):
									super().__init__(pad_token_id=lowerCAmelCase__       ,						bos_token_id=lowerCAmelCase__       ,						eos_token_id=lowerCAmelCase__       ,						**lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:			str  		=	vocab_size
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	max_position_embeddings
									SCREAMING_SNAKE_CASE_:			Tuple  		=	hidden_size
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	num_hidden_layers
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	intermediate_size
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	hidden_act
									SCREAMING_SNAKE_CASE_:			Any  		=	hidden_dropout_prob
									SCREAMING_SNAKE_CASE_:			int  		=	initializer_range
									SCREAMING_SNAKE_CASE_:			Optional[int]  		=	type_vocab_size
									SCREAMING_SNAKE_CASE_:			int  		=	layer_norm_eps
									SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	use_tpu_fourier_optimizations
									SCREAMING_SNAKE_CASE_:			int  		=	tpu_short_seq_length






