
import tensorflow as tf

from ...tf_utils import shape_list


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



		def __init__(				self       ,   A_       ,   A_       ,   A_       ,   A_       ,   A_=1       ,   A_=False       ,   **A_ )						->      Union[str, Any]:
				"""simple docstring"""
				super().__init__(**A_ )

				UpperCamelCase						=  vocab_size
				UpperCamelCase						=  d_embed
				UpperCamelCase						=  d_proj

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

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

				UpperCamelCase						=  []
				UpperCamelCase						=  []



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

								UpperCamelCase						=  self.add_weight(
								    shape=(d_emb_i, self.d_proj)       ,   initializer='zeros'       ,   trainable=A_       ,   name=F'''out_projs_._{i}''' )
								self.out_projs.append(A_ )
								UpperCamelCase						=  self.add_weight(
								    shape=(r_idx - l_idx, d_emb_i)       ,   initializer='zeros'       ,   trainable=A_       ,   name=F'''out_layers_._{i}_._weight'''       ,   )
								UpperCamelCase						=  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 __UpperCamelCase				(				A_       ,   A_       ,   A_       ,   A_=None )						->      int:
				"""simple docstring"""
				UpperCamelCase						=  x
				if proj is not None:
						UpperCamelCase						=  tf.einsum('ibd,ed->ibe'       ,   A_       ,   A_ )
				return tf.einsum('ibd,nd->ibn'       ,   A_       ,   A_ ) + b



		@staticmethod
		def __UpperCamelCase				(				A_       ,   A_ )						->      Dict:
				"""simple docstring"""
				UpperCamelCase						=  shape_list(A_ )
				UpperCamelCase						=  tf.range(lp_size[0]       ,   dtype=target.dtype )
				UpperCamelCase						=  tf.stack([r, target]       ,   1 )
				return tf.gather_nd(A_       ,   A_ )



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

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

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

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

				if target is not None:
						if return_mean:
								UpperCamelCase						=  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 is_flaky, 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 DonutImageProcessor


class        lowercase      (	unittest.TestCase     ):



		def __init__(				self       ,   A_       ,   A_=7       ,   A_=3       ,   A_=18       ,   A_=30       ,   A_=400       ,   A_=True       ,   A_=None       ,   A_=True       ,   A_=False       ,   A_=True       ,   A_=True       ,   A_=[0.5, 0.5, 0.5]       ,   A_=[0.5, 0.5, 0.5]       ,   )						->      Tuple:
				"""simple docstring"""
				UpperCamelCase						=  parent
				UpperCamelCase						=  batch_size
				UpperCamelCase						=  num_channels
				UpperCamelCase						=  image_size
				UpperCamelCase						=  min_resolution
				UpperCamelCase						=  max_resolution
				UpperCamelCase						=  do_resize
				UpperCamelCase						=  size if size is not None else {'height': 18, 'width': 20}
				UpperCamelCase						=  do_thumbnail
				UpperCamelCase						=  do_align_axis
				UpperCamelCase						=  do_pad
				UpperCamelCase						=  do_normalize
				UpperCamelCase						=  image_mean
				UpperCamelCase						=  image_std



		def __UpperCamelCase				(				self )						->      Tuple:
				"""simple docstring"""
				return {
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_thumbnail": self.do_thumbnail,
				    "do_align_long_axis": self.do_align_axis,
				    "do_pad": self.do_pad,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				}






@require_torch
@require_vision
class        lowercase      (	_SCREAMING_SNAKE_CASE						,     unittest.TestCase     ):
		__lowercase : Optional[int]			  =       DonutImageProcessor if is_vision_available() else None



		def __UpperCamelCase				(				self )						->      List[Any]:
				"""simple docstring"""
				UpperCamelCase						=  DonutImageProcessingTester(self )



		@property
		def __UpperCamelCase				(				self )						->      Any:
				"""simple docstring"""
				return self.image_processor_tester.prepare_image_processor_dict()



		def __UpperCamelCase				(				self )						->      int:
				"""simple docstring"""
				UpperCamelCase						=  self.image_processing_class(**self.image_processor_dict )
				self.assertTrue(hasattr(A_       ,   'do_resize' ) )
				self.assertTrue(hasattr(A_       ,   'size' ) )
				self.assertTrue(hasattr(A_       ,   'do_thumbnail' ) )
				self.assertTrue(hasattr(A_       ,   'do_align_long_axis' ) )
				self.assertTrue(hasattr(A_       ,   'do_pad' ) )
				self.assertTrue(hasattr(A_       ,   'do_normalize' ) )
				self.assertTrue(hasattr(A_       ,   'image_mean' ) )
				self.assertTrue(hasattr(A_       ,   'image_std' ) )



		def __UpperCamelCase				(				self )						->      str:
				"""simple docstring"""
				UpperCamelCase						=  self.image_processing_class.from_dict(self.image_processor_dict )
				self.assertEqual(image_processor.size       ,   {'height': 18, 'width': 20} )

				UpperCamelCase						=  self.image_processing_class.from_dict(self.image_processor_dict       ,   size=42 )
				self.assertEqual(image_processor.size       ,   {'height': 42, 'width': 42} )

				# Previous config had dimensions in (width, height) order
				UpperCamelCase						=  self.image_processing_class.from_dict(self.image_processor_dict       ,   size=(42, 84) )
				self.assertEqual(image_processor.size       ,   {'height': 84, 'width': 42} )



		def __UpperCamelCase				(				self )						->      Optional[Any]:
				"""simple docstring"""
				pass



		@is_flaky()
		def __UpperCamelCase				(				self )						->      int:
				"""simple docstring"""
				# Initialize image_processing
				UpperCamelCase						=  self.image_processing_class(**self.image_processor_dict )
				# create random PIL images
				UpperCamelCase						=  prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=A_ )
				for image in image_inputs:
						self.assertIsInstance(A_       ,   Image.Image )

				# Test not batched input
				UpperCamelCase						=  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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )

				# Test batched
				UpperCamelCase						=  image_processing(A_       ,   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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )



		@is_flaky()
		def __UpperCamelCase				(				self )						->      Any:
				"""simple docstring"""
				# Initialize image_processing
				UpperCamelCase						=  self.image_processing_class(**self.image_processor_dict )
				# create random numpy tensors
				UpperCamelCase						=  prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=A_       ,   numpify=A_ )
				for image in image_inputs:
						self.assertIsInstance(A_       ,   np.ndarray )

				# Test not batched input
				UpperCamelCase						=  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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )

				# Test batched
				UpperCamelCase						=  image_processing(A_       ,   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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )



		@is_flaky()
		def __UpperCamelCase				(				self )						->      Union[str, Any]:
				"""simple docstring"""
				# Initialize image_processing
				UpperCamelCase						=  self.image_processing_class(**self.image_processor_dict )
				# create random PyTorch tensors
				UpperCamelCase						=  prepare_image_inputs(self.image_processor_tester       ,   equal_resolution=A_       ,   torchify=A_ )
				for image in image_inputs:
						self.assertIsInstance(A_       ,   torch.Tensor )

				# Test not batched input
				UpperCamelCase						=  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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )

				# Test batched
				UpperCamelCase						=  image_processing(A_       ,   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.size['height'],
				        self.image_processor_tester.size['width'],
				    )       ,   )
