
"""simple docstring"""







import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


lowerCamelCase__        =		logging.get_logger(__name__)
def    __lowerCAmelCase    (_UpperCamelCase		):
	__lowerCAmelCase      :						List[Any]						      =				r'\w+[.]\d+'
	__lowerCAmelCase      :						Any						      =				re.findall(_UpperCamelCase	,  _UpperCamelCase		)
	for pat in pats:
		__lowerCAmelCase      :						Any						      =				key.replace(_UpperCamelCase	,  '_'.join(pat.split('.'		)		)		)
	return key
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase		):
	__lowerCAmelCase      :						Optional[Any]						      =				pt_tuple_key[:-1] + ('scale',)
	if (
	    any('norm' in str_ for str_ in pt_tuple_key		)
	    and (pt_tuple_key[-1] == "bias")
	    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
	    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
	):
		__lowerCAmelCase      :						Tuple						      =				pt_tuple_key[:-1] + ('scale',)
		return renamed_pt_tuple_key, pt_tensor
	elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
		__lowerCAmelCase      :						List[str]						      =				pt_tuple_key[:-1] + ('scale',)
		return renamed_pt_tuple_key, pt_tensor

	# embedding
	if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
		__lowerCAmelCase      :						Union[str, Any]						      =				pt_tuple_key[:-1] + ('embedding',)
		return renamed_pt_tuple_key, pt_tensor

	# conv layer
	__lowerCAmelCase      :						Dict						      =				pt_tuple_key[:-1] + ('kernel',)
	if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
		__lowerCAmelCase      :						List[Any]						      =				pt_tensor.transpose(2	,  3	,  1	,  0		)
		return renamed_pt_tuple_key, pt_tensor

	# linear layer
	__lowerCAmelCase      :						List[str]						      =				pt_tuple_key[:-1] + ('kernel',)
	if pt_tuple_key[-1] == "weight":
		__lowerCAmelCase      :						List[str]						      =				pt_tensor.T
		return renamed_pt_tuple_key, pt_tensor

	# old PyTorch layer norm weight
	__lowerCAmelCase      :						Any						      =				pt_tuple_key[:-1] + ('weight',)
	if pt_tuple_key[-1] == "gamma":
		return renamed_pt_tuple_key, pt_tensor

	# old PyTorch layer norm bias
	__lowerCAmelCase      :						List[str]						      =				pt_tuple_key[:-1] + ('bias',)
	if pt_tuple_key[-1] == "beta":
		return renamed_pt_tuple_key, pt_tensor

	return pt_tuple_key, pt_tensor
def    __lowerCAmelCase    (_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase=42		):
	# Step 1: Convert pytorch tensor to numpy
	__lowerCAmelCase      :						str						      =				{k: v.numpy() for k, v in pt_state_dict.items()}

	# Step 2: Since the model is stateless, get random Flax params
	__lowerCAmelCase      :						Optional[int]						      =				flax_model.init_weights(PRNGKey(_UpperCamelCase		)		)

	__lowerCAmelCase      :						List[str]						      =				flatten_dict(_UpperCamelCase		)
	__lowerCAmelCase      :						Tuple						      =				{}

	# Need to change some parameters name to match Flax names
	for pt_key, pt_tensor in pt_state_dict.items():
		__lowerCAmelCase      :						Union[str, Any]						      =				rename_key(_UpperCamelCase		)
		__lowerCAmelCase      :						Dict						      =				tuple(renamed_pt_key.split('.'		)		)

		# Correctly rename weight parameters
		__lowerCAmelCase      ,       __lowerCAmelCase      :						Dict						      =				rename_key_and_reshape_tensor(_UpperCamelCase	,  _UpperCamelCase	,  _UpperCamelCase		)

		if flax_key in random_flax_state_dict:
			if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
				raise ValueError(
				    F"PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape "
				    F"{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."		)

        # also add unexpected weight so that warning is thrown
		__lowerCAmelCase      :						Tuple						      =				jnp.asarray(_UpperCamelCase		)

	return unflatten_dict(_UpperCamelCase		)

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


lowerCamelCase__        =		"""hf-internal-testing/tiny-random-bert"""
lowerCamelCase__        =		os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowerCamelCase__        =		"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""




class 			A__	(     unittest.TestCase):

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

			# File is cached at the same place the second time.
			__lowerCAmelCase      :						Union[str, Any]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)
			self.assertEqual(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)

			# Using a specific revision to test the full commit hash.
			__lowerCAmelCase      :						Union[str, Any]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		revision='9b8c223'			)
			self.assertEqual(_SCREAMING_SNAKE_CASE	,		os.path.join(_SCREAMING_SNAKE_CASE	,		'snapshots'	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE			)			)

		def 							__lowerCamelCase							(				self			):
			with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE	,		'is not a valid model identifier'			):
				__lowerCAmelCase      :						Optional[Any]						      =				cached_file('tiny-random-bert'	,		_SCREAMING_SNAKE_CASE			)

			with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE	,		'is not a valid git identifier'			):
				__lowerCAmelCase      :						str						      =				cached_file(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		revision='aaaa'			)

			with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE	,		'does not appear to have a file named'			):
				__lowerCAmelCase      :						Optional[Any]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		'conf'			)

		def 							__lowerCamelCase							(				self			):
			with self.assertRaisesRegex(_SCREAMING_SNAKE_CASE	,		'does not appear to have a file named'			):
				__lowerCAmelCase      :						Optional[int]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		'conf'			)

			with open(os.path.join(_SCREAMING_SNAKE_CASE	,		'refs'	,		'main'			)			) as f:
				__lowerCAmelCase      :						Tuple						      =				f.read()
			self.assertTrue(os.path.isfile(os.path.join(_SCREAMING_SNAKE_CASE	,		'.no_exist'	,		_SCREAMING_SNAKE_CASE	,		'conf'			)			)			)

			__lowerCAmelCase      :						List[Any]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		'conf'	,		_raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE			)
			self.assertIsNone(_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						List[str]						      =				cached_file(_SCREAMING_SNAKE_CASE	,		'conf'	,		local_files_only=_SCREAMING_SNAKE_CASE	,		_raise_exceptions_for_missing_entries=_SCREAMING_SNAKE_CASE			)
			self.assertIsNone(_SCREAMING_SNAKE_CASE			)

			__lowerCAmelCase      :						List[Any]						      =				mock.Mock()
			__lowerCAmelCase      :						Tuple						      =				5_00
			__lowerCAmelCase      :						List[Any]						      =				{}
			__lowerCAmelCase      :						Dict						      =				HTTPError
			__lowerCAmelCase      :						str						      =				{}

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

		def 							__lowerCamelCase							(				self			):
			self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'	,		_SCREAMING_SNAKE_CASE			)			)
			self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'	,		_SCREAMING_SNAKE_CASE			)			)
			self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'	,		_SCREAMING_SNAKE_CASE			)			)

		def 							__lowerCamelCase							(				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(_SCREAMING_SNAKE_CASE	,		'is not a valid model identifier'			):
				get_file_from_repo('bert-base-case'	,		_SCREAMING_SNAKE_CASE			)

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

			__lowerCAmelCase      :						Union[str, Any]						      =				get_file_from_repo('bert-base-cased'	,		_SCREAMING_SNAKE_CASE			)
			# The name is the cached name which is not very easy to test, so instead we load the content.
			__lowerCAmelCase      :						List[Any]						      =				json.loads(open(_SCREAMING_SNAKE_CASE	,		'r'			).read()			)
			self.assertEqual(config['hidden_size']	,		7_68			)







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

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