





import tensorflow as tf

from ...tf_utils import shape_list
class        SCREAMING_SNAKE_CASE     (tf.keras.layers.Layer   ):


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





     """simple docstring"""







     super().__init__(**a )

     lowercase__		    =						vocab_size
     lowercase__		    =						d_embed
     lowercase__		    =						d_proj

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

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

     lowercase__		    =						[]
     lowercase__		    =						[]


   def  SCREAMING_SNAKE_CASE_							(					self    :       Union[str, Any]    ,  a    :       str )->     List[str]:





     """simple docstring"""







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

         lowercase__		    =						self.add_weight(
             shape=(d_emb_i, self.d_proj)    ,  initializer='zeros'    ,  trainable=a    ,  name=f"""out_projs_._{i}""" )
         self.out_projs.append(a )
         lowercase__		    =						self.add_weight(
             shape=(r_idx - l_idx, d_emb_i)    ,  initializer='zeros'    ,  trainable=a    ,  name=f"""out_layers_._{i}_._weight"""    ,  )
         lowercase__		    =						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  SCREAMING_SNAKE_CASE_							(					a    :       List[str]    ,  a    :       str    ,  a    :       str    ,  a    :       Optional[Any]=None )->     str:





     """simple docstring"""







     lowercase__		    =						x
     if proj is not None:
       lowercase__		    =						tf.einsum('ibd,ed->ibe'    ,  a    ,  a )
     return tf.einsum('ibd,nd->ibn'    ,  a    ,  a ) + b


   @staticmethod
   def  SCREAMING_SNAKE_CASE_							(					a    :       Union[str, Any]    ,  a    :       List[str] )->     Optional[Any]:





     """simple docstring"""







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


   def  SCREAMING_SNAKE_CASE_							(					self    :       int    ,  a    :       str    ,  a    :       Tuple    ,  a    :       List[str]=True    ,  a    :       List[str]=False )->     str:





     """simple docstring"""







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

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

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

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

     if target is not None:
       if return_mean:
         lowercase__		    =						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        SCREAMING_SNAKE_CASE     (unittest.TestCase   ):


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





     """simple docstring"""







     lowercase__			    =						parent
     lowercase__			    =						batch_size
     lowercase__			    =						num_channels
     lowercase__			    =						image_size
     lowercase__			    =						min_resolution
     lowercase__			    =						max_resolution
     lowercase__			    =						do_resize
     lowercase__			    =						size if size is not None else {'height': 18, 'width': 20}
     lowercase__			    =						do_thumbnail
     lowercase__			    =						do_align_axis
     lowercase__			    =						do_pad
     lowercase__			    =						do_normalize
     lowercase__			    =						image_mean
     lowercase__			    =						image_std


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     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        SCREAMING_SNAKE_CASE     (UpperCAmelCase    ,			unittest.TestCase   ):
   _UpperCamelCase					:	Optional[Any]				      = DonutImageProcessor if is_vision_available() else None


   def  SCREAMING_SNAKE_CASE_							(					self    :       int )->     List[Any]:





     """simple docstring"""







     lowercase__			    =						DonutImageProcessingTester(self )


   @property
   def  SCREAMING_SNAKE_CASE_							(					self    :       Union[str, Any] )->     Optional[int]:





     """simple docstring"""







     return self.image_processor_tester.prepare_image_processor_dict()


   def  SCREAMING_SNAKE_CASE_							(					self    :       Any )->     int:





     """simple docstring"""







     lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     Dict:





     """simple docstring"""







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

     lowercase__			    =						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
     lowercase__			    =						self.image_processing_class.from_dict(self.image_processor_dict    ,  size=(42, 84) )
     self.assertEqual(image_processor.size    ,  {'height': 84, 'width': 42} )


   def  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     Dict:





     """simple docstring"""







     pass


   @is_flaky()
   def  SCREAMING_SNAKE_CASE_							(					self    :       str )->     Optional[int]:





     """simple docstring"""







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

     # Test not batched input
     lowercase__			    =						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
     lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any] )->     Tuple:





     """simple docstring"""







     lowercase__			    =						self.image_processing_class(**self.image_processor_dict )
     # create random numpy tensors
     lowercase__			    =						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
     lowercase__			    =						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
     lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     Dict:





     """simple docstring"""







     lowercase__			    =						self.image_processing_class(**self.image_processor_dict )
     # create random PyTorch tensors
     lowercase__			    =						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
     lowercase__			    =						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
     lowercase__			    =						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'],
         )    ,  )


