



import tensorflow as tf

from ...tf_utils import shape_list

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



       def __init__(	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  str  ,	__SCREAMING_SNAKE_CASE							:  Any  ,	__SCREAMING_SNAKE_CASE							:  Dict  ,	__SCREAMING_SNAKE_CASE							:  int  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=1  ,	__SCREAMING_SNAKE_CASE							:  Dict=False  ,	**__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           super().__init__(**__SCREAMING_SNAKE_CASE)

           __a		 =       vocab_size
           __a		 =       d_embed
           __a		 =       d_proj

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

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

           __a		 =       []
           __a		 =       []



       def 	_lowerCamelCase    (	self							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  str):


           '''simple docstring'''

           if self.n_clusters > 0:
               __a		 =       self.add_weight(
                   shape=(self.n_clusters, self.d_embed)  ,	initializer='''zeros'''  ,	trainable=__SCREAMING_SNAKE_CASE  ,	name='''cluster_weight''')
               __a		 =       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:
                       __a		 =       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)
                   __a		 =       self.add_weight(
                       shape=(self.vocab_size, self.d_embed)  ,	initializer='''zeros'''  ,	trainable=__SCREAMING_SNAKE_CASE  ,	name=F'out_layers_._{i}_._weight'  ,	)
                   __a		 =       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)):
                   __a ,     __a		 =       self.cutoff_ends[i], self.cutoff_ends[i + 1]
                   __a		 =       self.d_embed // (self.div_val**i)

                   __a		 =       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)
                   __a		 =       self.add_weight(
                       shape=(r_idx - l_idx, d_emb_i)  ,	initializer='''zeros'''  ,	trainable=__SCREAMING_SNAKE_CASE  ,	name=F'out_layers_._{i}_._weight'  ,	)
                   __a		 =       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 	_lowerCamelCase    (	__SCREAMING_SNAKE_CASE							:  str  ,	__SCREAMING_SNAKE_CASE							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=None):


           '''simple docstring'''

           __a		 =       x
           if proj is not None:
               __a		 =       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 	_lowerCamelCase    (	__SCREAMING_SNAKE_CASE							:  Tuple  ,	__SCREAMING_SNAKE_CASE							:  Tuple):


           '''simple docstring'''

           __a		 =       shape_list(__SCREAMING_SNAKE_CASE)
           __a		 =       tf.range(lp_size[0]  ,	dtype=target.dtype)
           __a		 =       tf.stack([r, target]  ,	1)
           return tf.gather_nd(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)





       def 	_lowerCamelCase    (	self							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Any=True  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=False):


           '''simple docstring'''

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

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

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

                       __a		 =       self._logit(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	self.out_projs[0])
                       __a		 =       tf.nn.log_softmax(__SCREAMING_SNAKE_CASE)
                       out.append(head_logprob[..., : self.cutoffs[0]])
                       if target is not None:
                           __a		 =       tf.boolean_mask(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
                           __a		 =       self._gather_logprob(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
                   else:
                       __a		 =       self._logit(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	self.out_projs[i])
                       __a		 =       tf.nn.log_softmax(__SCREAMING_SNAKE_CASE)
                       __a		 =       self.cutoffs[0] + i - 1  # No probability for the head cluster
                       __a		 =       head_logprob[..., cluster_prob_idx, None] + tail_logprob
                       out.append(__SCREAMING_SNAKE_CASE)
                       if target is not None:
                           __a		 =       tf.boolean_mask(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
                           __a		 =       tf.boolean_mask(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
                           __a		 =       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))
               __a		 =       tf.concat(__SCREAMING_SNAKE_CASE  ,	axis=-1)

           if target is not None:
               if return_mean:
                   __a		 =       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 unittest

import numpy as np
from datasets import load_dataset

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 BeitImageProcessor

class 					_A (			unittest.TestCase					):



       def __init__(	self							:  Union[str, Any]  ,	__SCREAMING_SNAKE_CASE							:  List[str]  ,	__SCREAMING_SNAKE_CASE							:  int=7  ,	__SCREAMING_SNAKE_CASE							:  Tuple=3  ,	__SCREAMING_SNAKE_CASE							:  List[Any]=18  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=30  ,	__SCREAMING_SNAKE_CASE							:  int=400  ,	__SCREAMING_SNAKE_CASE							:  Dict=True  ,	__SCREAMING_SNAKE_CASE							:  str=None  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=None  ,	__SCREAMING_SNAKE_CASE							:  str=True  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=[0.5, 0.5, 0.5]  ,	__SCREAMING_SNAKE_CASE							:  Any=[0.5, 0.5, 0.5]  ,	__SCREAMING_SNAKE_CASE							:  List[str]=False  ,	):


           '''simple docstring'''

           __a		 =       size if size is not None else {'''height''': 20, '''width''': 20}
           __a		 =       crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
           __a		 =       parent
           __a		 =       batch_size
           __a		 =       num_channels
           __a		 =       image_size
           __a		 =       min_resolution
           __a		 =       max_resolution
           __a		 =       do_resize
           __a		 =       size
           __a		 =       do_center_crop
           __a		 =       crop_size
           __a		 =       do_normalize
           __a		 =       image_mean
           __a		 =       image_std
           __a		 =       do_reduce_labels





       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           return {
               "do_resize": self.do_resize,
               "size": self.size,
               "do_center_crop": self.do_center_crop,
               "crop_size": self.crop_size,
               "do_normalize": self.do_normalize,
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_reduce_labels": self.do_reduce_labels,
           }





def   __snake_case			(    ):
    __a		 =       load_dataset('''hf-internal-testing/fixtures_ade20k'''     ,				split='''test'''       )

    __a		 =       Image.open(dataset[0]['''file''']       )
    __a		 =       Image.open(dataset[1]['''file''']       )

    return image, map





def   __snake_case			(    ):
    __a		 =       load_dataset('''hf-internal-testing/fixtures_ade20k'''     ,				split='''test'''       )

    __a		 =       Image.open(ds[0]['''file''']       )
    __a		 =       Image.open(ds[1]['''file''']       )
    __a		 =       Image.open(ds[2]['''file''']       )
    __a		 =       Image.open(ds[3]['''file''']       )

    return [imagea, imagea], [mapa, mapa]

@require_torch
@require_vision
class 					_A (			__UpperCAmelCase   ,unittest.TestCase					):
       UpperCamelCase__						:					Union[str, Any]		      =		BeitImageProcessor if is_vision_available() else None



       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           __a		 =       BeitImageProcessingTester(self)



       @property
       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           return self.image_processor_tester.prepare_image_processor_dict()



       def 	_lowerCamelCase    (	self							:  Optional[Any]):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''do_resize'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''size'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''do_center_crop'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''center_crop'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''do_normalize'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''image_mean'''))
           self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE  ,	'''image_std'''))



       def 	_lowerCamelCase    (	self							:  str):


           '''simple docstring'''

           __a		 =       self.image_processing_class.from_dict(self.image_processor_dict)
           self.assertEqual(image_processor.size  ,	{'''height''': 20, '''width''': 20})
           self.assertEqual(image_processor.crop_size  ,	{'''height''': 18, '''width''': 18})
           self.assertEqual(image_processor.do_reduce_labels  ,	__SCREAMING_SNAKE_CASE)

           __a		 =       self.image_processing_class.from_dict(
               self.image_processor_dict  ,	size=42  ,	crop_size=84  ,	reduce_labels=__SCREAMING_SNAKE_CASE)
           self.assertEqual(image_processor.size  ,	{'''height''': 42, '''width''': 42})
           self.assertEqual(image_processor.crop_size  ,	{'''height''': 84, '''width''': 84})
           self.assertEqual(image_processor.do_reduce_labels  ,	__SCREAMING_SNAKE_CASE)



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           pass



       def 	_lowerCamelCase    (	self							:  Tuple):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)
           # create random PIL images
           __a		 =       prepare_image_inputs(self.image_processor_tester  ,	equal_resolution=__SCREAMING_SNAKE_CASE)
           for image in image_inputs:
               self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,	Image.Image)

           # Test not batched input
           __a		 =       image_processing(image_inputs[0]  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)

           # Test batched
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)



       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)
           # create random numpy tensors
           __a		 =       prepare_image_inputs(self.image_processor_tester  ,	equal_resolution=__SCREAMING_SNAKE_CASE  ,	numpify=__SCREAMING_SNAKE_CASE)
           for image in image_inputs:
               self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,	np.ndarray)

           # Test not batched input
           __a		 =       image_processing(image_inputs[0]  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)

           # Test batched
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)



       def 	_lowerCamelCase    (	self							:  List[Any]):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)
           # create random PyTorch tensors
           __a		 =       prepare_image_inputs(self.image_processor_tester  ,	equal_resolution=__SCREAMING_SNAKE_CASE  ,	torchify=__SCREAMING_SNAKE_CASE)
           for image in image_inputs:
               self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,	torch.Tensor)

           # Test not batched input
           __a		 =       image_processing(image_inputs[0]  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)

           # Test batched
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''').pixel_values
           self.assertEqual(
               encoded_images.shape  ,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)



       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)
           # create random PyTorch tensors
           __a		 =       prepare_image_inputs(self.image_processor_tester  ,	equal_resolution=__SCREAMING_SNAKE_CASE  ,	torchify=__SCREAMING_SNAKE_CASE)
           __a		 =       []
           for image in image_inputs:
               self.assertIsInstance(__SCREAMING_SNAKE_CASE  ,	torch.Tensor)
               maps.append(torch.zeros(image.shape[-2:]).long())

           # Test not batched input
           __a		 =       image_processing(image_inputs[0]  ,	maps[0]  ,	return_tensors='''pt''')
           self.assertEqual(
               encoding['''pixel_values'''].shape  ,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(
               encoding['''labels'''].shape  ,	(
                   1,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(encoding['''labels'''].dtype  ,	torch.long)
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 255)

           # Test batched
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''')
           self.assertEqual(
               encoding['''pixel_values'''].shape  ,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(
               encoding['''labels'''].shape  ,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(encoding['''labels'''].dtype  ,	torch.long)
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 255)

           # Test not batched input (PIL images)
           __a ,     __a		 =       prepare_semantic_single_inputs()

           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''')
           self.assertEqual(
               encoding['''pixel_values'''].shape  ,	(
                   1,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(
               encoding['''labels'''].shape  ,	(
                   1,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(encoding['''labels'''].dtype  ,	torch.long)
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 255)

           # Test batched input (PIL images)
           __a ,     __a		 =       prepare_semantic_batch_inputs()

           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''')
           self.assertEqual(
               encoding['''pixel_values'''].shape  ,	(
                   2,
                   self.image_processor_tester.num_channels,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(
               encoding['''labels'''].shape  ,	(
                   2,
                   self.image_processor_tester.crop_size['''height'''],
                   self.image_processor_tester.crop_size['''width'''],
               )  ,	)
           self.assertEqual(encoding['''labels'''].dtype  ,	torch.long)
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 255)





       def 	_lowerCamelCase    (	self							:  Dict):


           '''simple docstring'''

           __a		 =       self.image_processing_class(**self.image_processor_dict)

           # ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
           __a ,     __a		 =       prepare_semantic_single_inputs()
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''')
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 150)

           __a		 =       True
           __a		 =       image_processing(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	return_tensors='''pt''')
           self.assertTrue(encoding['''labels'''].min().item() >= 0)
           self.assertTrue(encoding['''labels'''].max().item() <= 255)





