




import tensorflow as tf

from ...tf_utils import shape_list


class 							__UpperCamelCase			( tf.keras.layers.Layer  ):





				'''simple docstring'''



				def __init__( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_=1       , UpperCAmelCase_=False       , **UpperCAmelCase_						):
											super().__init__(**__SCREAMING_SNAKE_CASE						)

											lowerCAmelCase   =		vocab_size
											lowerCAmelCase   =		d_embed
											lowerCAmelCase   =		d_proj

											lowerCAmelCase   =		cutoffs + [vocab_size]
											lowerCAmelCase   =		[0] + self.cutoffs
											lowerCAmelCase   =		div_val

											lowerCAmelCase   =		self.cutoffs[0]
											lowerCAmelCase   =		len(self.cutoffs						) - 1
											lowerCAmelCase   =		self.shortlist_size + self.n_clusters
											lowerCAmelCase   =		keep_order

											lowerCAmelCase   =		[]
											lowerCAmelCase   =		[]



				def       __snake_case		( self       , UpperCAmelCase_						):

											if self.n_clusters > 0:
																		lowerCAmelCase   =		self.add_weight(
																		    shape=(self.n_clusters, self.d_embed)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name='''cluster_weight'''						)
																		lowerCAmelCase   =		self.add_weight(
																		    shape=(self.n_clusters,)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name='''cluster_bias'''						)

											if self.div_val == 1:
																		for i in range(len(self.cutoffs						)						):
																									if self.d_proj != self.d_embed:
																																lowerCAmelCase   =		self.add_weight(
																																    shape=(self.d_embed, self.d_proj)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_projs_._{i}"""       , )
																																self.out_projs.append(__SCREAMING_SNAKE_CASE						)
																									else:
																																self.out_projs.append(__SCREAMING_SNAKE_CASE						)
																									lowerCAmelCase   =		self.add_weight(
																									    shape=(self.vocab_size, self.d_embed)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_layers_._{i}_._weight"""       , )
																									lowerCAmelCase   =		self.add_weight(
																									    shape=(self.vocab_size,)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_layers_._{i}_._bias"""       , )
																									self.out_layers.append((weight, bias)						)
											else:
																		for i in range(len(self.cutoffs						)						):
																									lowerCAmelCase   ,     lowerCAmelCase   =		self.cutoff_ends[i], self.cutoff_ends[i + 1]
																									lowerCAmelCase   =		self.d_embed // (self.div_val**i)

																									lowerCAmelCase   =		self.add_weight(
																									    shape=(d_emb_i, self.d_proj)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_projs_._{i}"""						)
																									self.out_projs.append(__SCREAMING_SNAKE_CASE						)
																									lowerCAmelCase   =		self.add_weight(
																									    shape=(r_idx - l_idx, d_emb_i)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_layers_._{i}_._weight"""       , )
																									lowerCAmelCase   =		self.add_weight(
																									    shape=(r_idx - l_idx,)       , initializer='''zeros'''       , trainable=__SCREAMING_SNAKE_CASE       , name=F"""out_layers_._{i}_._bias"""       , )
																									self.out_layers.append((weight, bias)						)
											super().build(__SCREAMING_SNAKE_CASE						)



				@staticmethod
				def       __snake_case		( UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_=None						):
											lowerCAmelCase   =		x
											if proj is not None:
																		lowerCAmelCase   =		tf.einsum('''ibd,ed->ibe'''       , __SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
											return tf.einsum('''ibd,nd->ibn'''       , __SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						) + b



				@staticmethod
				def       __snake_case		( UpperCAmelCase_       , UpperCAmelCase_						):
											lowerCAmelCase   =		shape_list(__SCREAMING_SNAKE_CASE						)
											lowerCAmelCase   =		tf.range(lp_size[0]       , dtype=target.dtype						)
											lowerCAmelCase   =		tf.stack([r, target]       , 1						)
											return tf.gather_nd(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)






				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_=True       , UpperCAmelCase_=False						):
											lowerCAmelCase   =		0
											if self.n_clusters == 0:
																		lowerCAmelCase   =		self._logit(__SCREAMING_SNAKE_CASE       , self.out_layers[0][0]       , self.out_layers[0][1]       , self.out_projs[0]						)
																		if target is not None:
																									lowerCAmelCase   =		tf.nn.sparse_softmax_cross_entropy_with_logits(labels=__SCREAMING_SNAKE_CASE       , logits=__SCREAMING_SNAKE_CASE						)
																		lowerCAmelCase   =		tf.nn.log_softmax(__SCREAMING_SNAKE_CASE       , axis=-1						)
											else:
																		lowerCAmelCase   =		shape_list(__SCREAMING_SNAKE_CASE						)
																		lowerCAmelCase   =		[]
																		lowerCAmelCase   =		tf.zeros(hidden_sizes[:2]						)
																		for i in range(len(self.cutoffs						)						):
																									lowerCAmelCase   ,     lowerCAmelCase   =		self.cutoff_ends[i], self.cutoff_ends[i + 1]
																									if target is not None:
																																lowerCAmelCase   =		(target >= l_idx) & (target < r_idx)
																																lowerCAmelCase   =		tf.where(__SCREAMING_SNAKE_CASE						)
																																lowerCAmelCase   =		tf.boolean_mask(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						) - l_idx

																									if self.div_val == 1:
																																lowerCAmelCase   =		self.out_layers[0][0][l_idx:r_idx]
																																lowerCAmelCase   =		self.out_layers[0][1][l_idx:r_idx]
																									else:
																																lowerCAmelCase   =		self.out_layers[i][0]
																																lowerCAmelCase   =		self.out_layers[i][1]

																									if i == 0:
																																lowerCAmelCase   =		tf.concat([cur_W, self.cluster_weight]       , 0						)
																																lowerCAmelCase   =		tf.concat([cur_b, self.cluster_bias]       , 0						)

																																lowerCAmelCase   =		self._logit(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE       , self.out_projs[0]						)
																																lowerCAmelCase   =		tf.nn.log_softmax(__SCREAMING_SNAKE_CASE						)
																																out.append(head_logprob[..., : self.cutoffs[0]]						)
																																if target is not None:
																																							lowerCAmelCase   =		tf.boolean_mask(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
																																							lowerCAmelCase   =		self._gather_logprob(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
																									else:
																																lowerCAmelCase   =		self._logit(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE       , self.out_projs[i]						)
																																lowerCAmelCase   =		tf.nn.log_softmax(__SCREAMING_SNAKE_CASE						)
																																lowerCAmelCase   =		self.cutoffs[0] + i - 1  # No probability for the head cluster
																																lowerCAmelCase   =		head_logprob[..., cluster_prob_idx, None] + tail_logprob
																																out.append(__SCREAMING_SNAKE_CASE						)
																																if target is not None:
																																							lowerCAmelCase   =		tf.boolean_mask(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
																																							lowerCAmelCase   =		tf.boolean_mask(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
																																							lowerCAmelCase   =		self._gather_logprob(__SCREAMING_SNAKE_CASE       , __SCREAMING_SNAKE_CASE						)
																																							cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
																									if target is not None:
																																loss += tf.scatter_nd(__SCREAMING_SNAKE_CASE       , -cur_logprob       , shape_list(__SCREAMING_SNAKE_CASE						)						)
																		lowerCAmelCase   =		tf.concat(__SCREAMING_SNAKE_CASE       , axis=-1						)

											if target is not None:
																		if return_mean:
																									lowerCAmelCase   =		tf.reduce_mean(__SCREAMING_SNAKE_CASE						)
																		# Add the training-time loss value to the layer using `self.add_loss()`.
																		self.add_loss(__SCREAMING_SNAKE_CASE						)

																		# Log the loss as a metric (we could log arbitrary metrics,
																		# including different metrics for training and inference.
																		self.add_metric(__SCREAMING_SNAKE_CASE       , name=self.name       , aggregation='''mean''' if return_mean else ''''''						)

											return out









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


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


class 							__UpperCamelCase			( unittest.TestCase  ):





				'''simple docstring'''



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

											# File is cached at the same place the second time.
											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , UpperCAmelCase_						)
											self.assertEqual(UpperCAmelCase_       , UpperCAmelCase_						)

											# Using a specific revision to test the full commit hash.
											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , UpperCAmelCase_       , revision='''9b8c223'''						)
											self.assertEqual(UpperCAmelCase_       , os.path.join(UpperCAmelCase_       , '''snapshots'''       , UpperCAmelCase_       , UpperCAmelCase_						)						)



				def       __snake_case		( self						):
											with self.assertRaisesRegex(UpperCAmelCase_       , '''is not a valid model identifier'''						):
																		lowerCAmelCase   =		cached_file('''tiny-random-bert'''       , UpperCAmelCase_						)

											with self.assertRaisesRegex(UpperCAmelCase_       , '''is not a valid git identifier'''						):
																		lowerCAmelCase   =		cached_file(UpperCAmelCase_       , UpperCAmelCase_       , revision='''aaaa'''						)

											with self.assertRaisesRegex(UpperCAmelCase_       , '''does not appear to have a file named'''						):
																		lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''						)



				def       __snake_case		( self						):
											with self.assertRaisesRegex(UpperCAmelCase_       , '''does not appear to have a file named'''						):
																		lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''						)

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

											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''       , _raise_exceptions_for_missing_entries=UpperCAmelCase_						)
											self.assertIsNone(UpperCAmelCase_						)

											lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''       , local_files_only=UpperCAmelCase_       , _raise_exceptions_for_missing_entries=UpperCAmelCase_						)
											self.assertIsNone(UpperCAmelCase_						)

											lowerCAmelCase   =		mock.Mock()
											lowerCAmelCase   =		5_00
											lowerCAmelCase   =		{}
											lowerCAmelCase   =		HTTPError
											lowerCAmelCase   =		{}

											# Under the mock environment we get a 500 error when trying to reach the tokenizer.
											with mock.patch('''requests.Session.request'''       , return_value=UpperCAmelCase_						) as mock_head:
																		lowerCAmelCase   =		cached_file(UpperCAmelCase_       , '''conf'''       , _raise_exceptions_for_connection_errors=UpperCAmelCase_						)
																		self.assertIsNone(UpperCAmelCase_						)
																		# 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'''       , UpperCAmelCase_						)						)
											self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''       , UpperCAmelCase_						)						)
											self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''       , UpperCAmelCase_						)						)



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

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

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






				def       __snake_case		( self						):
											with tempfile.TemporaryDirectory() as tmp_dir:
																		lowerCAmelCase   =		Path(UpperCAmelCase_						) / '''a.txt'''
																		filename.touch()
																		self.assertEqual(get_file_from_repo(UpperCAmelCase_       , '''a.txt'''						)       , str(UpperCAmelCase_						)						)

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



