

import tensorflow as tf

from ...tf_utils import shape_list





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


							"""simple docstring"""





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

												__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        A__  ( self					,						_A							):
												if self.n_clusters > 0:
																	__lowerCAmelCase					=							self.add_weight(
																	    shape=(self.n_clusters, self.d_embed)					,						initializer='zeros'					,						trainable=_A					,						name='cluster_weight'							)
																	__lowerCAmelCase					=							self.add_weight(
																	    shape=(self.n_clusters,)					,						initializer='zeros'					,						trainable=_A					,						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=_A					,						name=F"""out_projs_._{i}"""					,						)
																											self.out_projs.append(_A							)
																						else:
																											self.out_projs.append(_A							)
																						__lowerCAmelCase					=							self.add_weight(
																						    shape=(self.vocab_size, self.d_embed)					,						initializer='zeros'					,						trainable=_A					,						name=F"""out_layers_._{i}_._weight"""					,						)
																						__lowerCAmelCase					=							self.add_weight(
																						    shape=(self.vocab_size,)					,						initializer='zeros'					,						trainable=_A					,						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=_A					,						name=F"""out_projs_._{i}"""							)
																						self.out_projs.append(_A							)
																						__lowerCAmelCase					=							self.add_weight(
																						    shape=(r_idx - l_idx, d_emb_i)					,						initializer='zeros'					,						trainable=_A					,						name=F"""out_layers_._{i}_._weight"""					,						)
																						__lowerCAmelCase					=							self.add_weight(
																						    shape=(r_idx - l_idx,)					,						initializer='zeros'					,						trainable=_A					,						name=F"""out_layers_._{i}_._bias"""					,						)
																						self.out_layers.append((weight, bias)							)
												super().build(_A							)




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




							@staticmethod
							def        A__  ( _A					,						_A							):
												__lowerCAmelCase					=							shape_list(_A							)
												__lowerCAmelCase					=							tf.range(lp_size[0]					,						dtype=target.dtype							)
												__lowerCAmelCase					=							tf.stack([r, target]					,						1							)
												return tf.gather_nd(_A					,						_A							)




							def        A__  ( self					,						_A					,						_A					,						_A=True					,						_A=False							):
												__lowerCAmelCase					=							0
												if self.n_clusters == 0:
																	__lowerCAmelCase					=							self._logit(_A					,						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=_A					,						logits=_A							)
																	__lowerCAmelCase					=							tf.nn.log_softmax(_A					,						axis=-1							)
												else:
																	__lowerCAmelCase					=							shape_list(_A							)
																	__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(_A							)
																											__lowerCAmelCase					=							tf.boolean_mask(_A					,						_A							) - 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(_A					,						_A					,						_A					,						self.out_projs[0]							)
																											__lowerCAmelCase					=							tf.nn.log_softmax(_A							)
																											out.append(head_logprob[..., : self.cutoffs[0]]							)
																											if target is not None:
																																__lowerCAmelCase					=							tf.boolean_mask(_A					,						_A							)
																																__lowerCAmelCase					=							self._gather_logprob(_A					,						_A							)
																						else:
																											__lowerCAmelCase					=							self._logit(_A					,						_A					,						_A					,						self.out_projs[i]							)
																											__lowerCAmelCase					=							tf.nn.log_softmax(_A							)
																											__lowerCAmelCase					=							self.cutoffs[0] + i - 1  # No probability for the head cluster
																											__lowerCAmelCase					=							head_logprob[..., cluster_prob_idx, None] + tail_logprob
																											out.append(_A							)
																											if target is not None:
																																__lowerCAmelCase					=							tf.boolean_mask(_A					,						_A							)
																																__lowerCAmelCase					=							tf.boolean_mask(_A					,						_A							)
																																__lowerCAmelCase					=							self._gather_logprob(_A					,						_A							)
																																cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
																						if target is not None:
																											loss += tf.scatter_nd(_A					,						-cur_logprob					,						shape_list(_A							)							)
																	__lowerCAmelCase					=							tf.concat(_A					,						axis=-1							)

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

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

												return out









import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import ViTImageProcessor





class     snake_case_			(				unittest.TestCase							):


							"""simple docstring"""





							def __init__( self					,						_A					,						_A=1_3					,						_A=3					,						_A=2_2_4					,						_A=3_0					,						_A=4_0_0					,						_A=True					,						_A=None					,						_A=True					,						_A=[0.5, 0.5, 0.5]					,						_A=[0.5, 0.5, 0.5]					,						):
												__lowerCAmelCase					=							size if size is not None else {'height': 1_8, 'width': 1_8}
												__lowerCAmelCase					=							parent
												__lowerCAmelCase					=							batch_size
												__lowerCAmelCase					=							num_channels
												__lowerCAmelCase					=							image_size
												__lowerCAmelCase					=							min_resolution
												__lowerCAmelCase					=							max_resolution
												__lowerCAmelCase					=							do_resize
												__lowerCAmelCase					=							size
												__lowerCAmelCase					=							do_normalize
												__lowerCAmelCase					=							image_mean
												__lowerCAmelCase					=							image_std




							def        A__  ( self							):
												return {
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_normalize": self.do_normalize,
												    "do_resize": self.do_resize,
												    "size": self.size,
												}





@require_torch
@require_vision
class     snake_case_			(				_a			, unittest.TestCase							):


							"""simple docstring"""





							__UpperCAmelCase       =ViTImageProcessor if is_vision_available() else None




							def        A__  ( self							):
												__lowerCAmelCase					=							EfficientFormerImageProcessorTester(self							)




							@property
							def        A__  ( self							):
												return self.image_proc_tester.prepare_image_processor_dict()




							def        A__  ( self							):
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												self.assertTrue(hasattr(_A					,						'image_mean'							)							)
												self.assertTrue(hasattr(_A					,						'image_std'							)							)
												self.assertTrue(hasattr(_A					,						'do_normalize'							)							)
												self.assertTrue(hasattr(_A					,						'do_resize'							)							)
												self.assertTrue(hasattr(_A					,						'size'							)							)




							def        A__  ( self							):
												pass




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PIL images
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						Image.Image							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random numpy tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						numpify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						np.ndarray							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PyTorch tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						torchify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						torch.Tensor							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)






