

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


_SCREAMING_SNAKE_CASE							 =			logging.get_logger(__name__)


if is_vision_available():
      import PIL


class      lowerCamelCase_ (			_lowerCAmelCase):
 _lowerCAmelCase									=	['''pixel_values''']




 def __init__(			self  ,  _UpperCAmelCase = True  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = PILImageResampling.BILINEAR  ,  _UpperCAmelCase = True  ,  _UpperCAmelCase = False  ,  _UpperCAmelCase = True  ,  _UpperCAmelCase = True  ,  _UpperCAmelCase = 1 / 255  ,  _UpperCAmelCase = True  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  **_UpperCAmelCase  ,  ):
   super().__init__(**_UpperCAmelCase							)

   __UpperCAmelCase	:  List[str]		=     size if size is not None else {'height': 2_560, 'width': 1_920}
   if isinstance(_UpperCAmelCase  ,  (tuple, list)							):
     # The previous feature extractor size parameter was in (width, height) format
     __UpperCAmelCase	:  Union[str, Any]		=     size[::-1]
   __UpperCAmelCase	:  Any		=     get_size_dict(_UpperCAmelCase							)

   __UpperCAmelCase	:  List[str]		=     do_resize
   __UpperCAmelCase	:  List[Any]		=     size
   __UpperCAmelCase	:  Any		=     resample
   __UpperCAmelCase	:  List[str]		=     do_thumbnail
   __UpperCAmelCase	:  Tuple		=     do_align_long_axis
   __UpperCAmelCase	:  List[str]		=     do_pad
   __UpperCAmelCase	:  Union[str, Any]		=     do_rescale
   __UpperCAmelCase	:  Union[str, Any]		=     rescale_factor
   __UpperCAmelCase	:  str		=     do_normalize
   __UpperCAmelCase	:  Any		=     image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
   __UpperCAmelCase	:  Any		=     image_std if image_std is not None else IMAGENET_STANDARD_STD




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None							):
   __UpperCAmelCase,			__UpperCAmelCase	:  List[Any]		=     get_image_size(_UpperCAmelCase							)
   __UpperCAmelCase,			__UpperCAmelCase	:  Tuple		=     size['height'], size['width']

   if (output_width < output_height and input_width > input_height) or (
       output_width > output_height and input_width < input_height
   ):
     __UpperCAmelCase	:  Any		=     np.rotaa(_UpperCAmelCase  ,  3							)

   if data_format is not None:
     __UpperCAmelCase	:  Optional[Any]		=     to_channel_dimension_format(_UpperCAmelCase  ,  _UpperCAmelCase							)

   return image




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = False  ,  _UpperCAmelCase = None  ,  ):
   __UpperCAmelCase,			__UpperCAmelCase	:  Dict		=     size['height'], size['width']
   __UpperCAmelCase,			__UpperCAmelCase	:  str		=     get_image_size(_UpperCAmelCase							)

   __UpperCAmelCase	:  Optional[Any]		=     output_width - input_width
   __UpperCAmelCase	:  Optional[int]		=     output_height - input_height

   if random_padding:
     __UpperCAmelCase	:  Optional[Any]		=     np.random.randint(low=0  ,  high=delta_height + 1							)
     __UpperCAmelCase	:  Optional[int]		=     np.random.randint(low=0  ,  high=delta_width + 1							)
   else:
     __UpperCAmelCase	:  Dict		=     delta_height // 2
     __UpperCAmelCase	:  List[Any]		=     delta_width // 2

   __UpperCAmelCase	:  Optional[Any]		=     delta_height - pad_top
   __UpperCAmelCase	:  Any		=     delta_width - pad_left

   __UpperCAmelCase	:  int		=     ((pad_top, pad_bottom), (pad_left, pad_right))
   return pad(_UpperCAmelCase  ,  _UpperCAmelCase  ,  data_format=_UpperCAmelCase							)




 def     a       (			self  ,  *_UpperCAmelCase  ,  **_UpperCAmelCase							):
   logger.info('pad is deprecated and will be removed in version 4.27. Please use pad_image instead.'							)
   return self.pad_image(*_UpperCAmelCase  ,  **_UpperCAmelCase							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = PILImageResampling.BICUBIC  ,  _UpperCAmelCase = None  ,  **_UpperCAmelCase  ,  ):
   __UpperCAmelCase,			__UpperCAmelCase	:  Tuple		=     get_image_size(_UpperCAmelCase							)
   __UpperCAmelCase,			__UpperCAmelCase	:  Optional[int]		=     size['height'], size['width']

   # We always resize to the smallest of either the input or output size.
   __UpperCAmelCase	:  List[Any]		=     min(_UpperCAmelCase  ,  _UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     min(_UpperCAmelCase  ,  _UpperCAmelCase							)

   if height == input_height and width == input_width:
     return image

   if input_height > input_width:
     __UpperCAmelCase	:  Optional[Any]		=     int(input_width * height / input_height							)
   elif input_width > input_height:
     __UpperCAmelCase	:  List[str]		=     int(input_height * width / input_width							)

   return resize(
       _UpperCAmelCase  ,  size=(height, width)  ,  resample=_UpperCAmelCase  ,  reducing_gap=2.0  ,  data_format=_UpperCAmelCase  ,  **_UpperCAmelCase							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = PILImageResampling.BICUBIC  ,  _UpperCAmelCase = None  ,  **_UpperCAmelCase  ,  ):
   __UpperCAmelCase	:  List[str]		=     get_size_dict(_UpperCAmelCase							)
   __UpperCAmelCase	:  List[str]		=     min(size['height']  ,  size['width']							)
   __UpperCAmelCase	:  List[str]		=     get_resize_output_image_size(_UpperCAmelCase  ,  size=_UpperCAmelCase  ,  default_to_square=_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     resize(_UpperCAmelCase  ,  size=_UpperCAmelCase  ,  resample=_UpperCAmelCase  ,  data_format=_UpperCAmelCase  ,  **_UpperCAmelCase							)
   return resized_image




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None  ,  **_UpperCAmelCase  ,  ):
   return rescale(_UpperCAmelCase  ,  scale=_UpperCAmelCase  ,  data_format=_UpperCAmelCase  ,  **_UpperCAmelCase							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None  ,  **_UpperCAmelCase  ,  ):
   return normalize(_UpperCAmelCase  ,  mean=_UpperCAmelCase  ,  std=_UpperCAmelCase  ,  data_format=_UpperCAmelCase  ,  **_UpperCAmelCase							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = False  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = None  ,  _UpperCAmelCase = ChannelDimension.FIRST  ,  **_UpperCAmelCase  ,  ):
   __UpperCAmelCase	:  str		=     do_resize if do_resize is not None else self.do_resize
   __UpperCAmelCase	:  str		=     size if size is not None else self.size
   if isinstance(_UpperCAmelCase  ,  (tuple, list)							):
     # Previous feature extractor had size in (width, height) format
     __UpperCAmelCase	:  Union[str, Any]		=     size[::-1]
   __UpperCAmelCase	:  Dict		=     get_size_dict(_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     resample if resample is not None else self.resample
   __UpperCAmelCase	:  List[str]		=     do_thumbnail if do_thumbnail is not None else self.do_thumbnail
   __UpperCAmelCase	:  Optional[int]		=     do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
   __UpperCAmelCase	:  Optional[int]		=     do_pad if do_pad is not None else self.do_pad
   __UpperCAmelCase	:  List[Any]		=     do_rescale if do_rescale is not None else self.do_rescale
   __UpperCAmelCase	:  Any		=     rescale_factor if rescale_factor is not None else self.rescale_factor
   __UpperCAmelCase	:  str		=     do_normalize if do_normalize is not None else self.do_normalize
   __UpperCAmelCase	:  Dict		=     image_mean if image_mean is not None else self.image_mean
   __UpperCAmelCase	:  Dict		=     image_std if image_std is not None else self.image_std

   __UpperCAmelCase	:  Optional[int]		=     make_list_of_images(_UpperCAmelCase							)

   if not valid_images(_UpperCAmelCase							):
     raise ValueError(
         'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
         'torch.Tensor, tf.Tensor or jax.ndarray.'							)

   if do_resize and size is None:
     raise ValueError('Size must be specified if do_resize is True.'							)

   if do_rescale and rescale_factor is None:
     raise ValueError('Rescale factor must be specified if do_rescale is True.'							)

   if do_pad and size is None:
     raise ValueError('Size must be specified if do_pad is True.'							)

   if do_normalize and (image_mean is None or image_std is None):
     raise ValueError('Image mean and std must be specified if do_normalize is True.'							)

   # All transformations expect numpy arrays.
   __UpperCAmelCase	:  List[str]		=     [to_numpy_array(_UpperCAmelCase							) for image in images]

   if do_align_long_axis:
     __UpperCAmelCase	:  Dict		=     [self.align_long_axis(_UpperCAmelCase  ,  size=_UpperCAmelCase							) for image in images]

   if do_resize:
     __UpperCAmelCase	:  List[str]		=     [self.resize(image=_UpperCAmelCase  ,  size=_UpperCAmelCase  ,  resample=_UpperCAmelCase							) for image in images]

   if do_thumbnail:
     __UpperCAmelCase	:  Tuple		=     [self.thumbnail(image=_UpperCAmelCase  ,  size=_UpperCAmelCase							) for image in images]

   if do_pad:
     __UpperCAmelCase	:  str		=     [self.pad_image(image=_UpperCAmelCase  ,  size=_UpperCAmelCase  ,  random_padding=_UpperCAmelCase							) for image in images]

   if do_rescale:
     __UpperCAmelCase	:  Optional[int]		=     [self.rescale(image=_UpperCAmelCase  ,  scale=_UpperCAmelCase							) for image in images]

   if do_normalize:
     __UpperCAmelCase	:  List[str]		=     [self.normalize(image=_UpperCAmelCase  ,  mean=_UpperCAmelCase  ,  std=_UpperCAmelCase							) for image in images]

   __UpperCAmelCase	:  List[str]		=     [to_channel_dimension_format(_UpperCAmelCase  ,  _UpperCAmelCase							) for image in images]

   __UpperCAmelCase	:  List[str]		=     {'pixel_values': images}
   return BatchFeature(data=_UpperCAmelCase  ,  tensor_type=_UpperCAmelCase							)







import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
					import torch

					from transformers import GPTaLMHeadModel







@require_torch
@require_sentencepiece
@require_tokenizers
class 			UpperCamelCase__		(     unittest.TestCase ):







	'''simple docstring'''





	@slow
	@unittest.skip('''Model is not available.'''		)
	def 	__snake_case					(      self		):
					A__	:						int								=				'''nvidia/megatron-gpt2-345m/'''
					if "MYDIR" in os.environ:
									A__	:						Union[str, Any]								=				os.path.join(os.environ['''MYDIR''']					,							UpperCamelCase__		)
					A__	:						Union[str, Any]								=				GPTaLMHeadModel.from_pretrained(UpperCamelCase__		)
					model.to(UpperCamelCase__		)
					model.half()

					A__	:						Dict								=				torch.tensor(
					    [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]					,							device=UpperCamelCase__					,							dtype=torch.long					,							)

					with torch.no_grad():
									A__	:						str								=				model(UpperCamelCase__		).logits

					A__	:						int								=				torch.Size((1, 9, 5_0257)		)
					self.assertEqual(output.shape					,							UpperCamelCase__		)

					A__	:						Dict								=				torch.tensor(
					    [
					        4.9_4_1_4,
					        -0.2_9_2_0,
					        -1.2_1_4_8,
					        -4.0_2_7_3,
					        -0.5_1_6_1,
					        -5.2_1_0_9,
					        -1.2_4_1_2,
					        -1.8_3_0_1,
					        -1.7_7_3_4,
					        -4.7_1_4_8,
					        -0.2_3_1_7,
					        -1.0_8_1_1,
					        -2.1_7_7_7,
					        0.4_1_4_1,
					        -3.7_9_6_9,
					        -4.0_5_8_6,
					        -2.5_3_3_2,
					        -3.3_8_0_9,
					        4.3_8_6_7,
					    ]					,							device=UpperCamelCase__					,							dtype=torch.half					,							)

					for i in range(19		):
									A__							,				A__	:						int								=				8 * i // 17, 2792 * i  # along the diagonal
									A__							,				A__	:						Optional[Any]								=				output[0, r, c], expected_diag[i]
									A__	:						Dict								=				F"row={r} col={c} computed={computed} expected={expected}"
									self.assertAlmostEqual(UpperCamelCase__					,							UpperCamelCase__					,							delta=1e-4					,							msg=UpperCamelCase__		)