



'''simple docstring'''





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 DeiTImageProcessor





class 							lowerCAmelCase_		(	unittest.TestCase						):





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



							'''simple docstring'''







							snake_case   :							int						  =size if size is not None else {'''height''': 20, '''width''': 20}
							snake_case   :							Any						  =crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

							snake_case   :							List[str]						  =parent
							snake_case   :							Any						  =batch_size
							snake_case   :							int						  =num_channels
							snake_case   :							Dict						  =image_size
							snake_case   :							str						  =min_resolution
							snake_case   :							Dict						  =max_resolution
							snake_case   :							int						  =do_resize
							snake_case   :							Tuple						  =size
							snake_case   :							Tuple						  =do_center_crop
							snake_case   :							Tuple						  =crop_size
							snake_case   :							Optional[Any]						  =do_normalize
							snake_case   :							Dict						  =image_mean
							snake_case   :							str						  =image_std






				def 					__snake_case       (    self    :    Optional[int]     ):



							'''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,
							}





@require_torch
@require_vision
class 							lowerCAmelCase_		(	lowercase__  ,      unittest.TestCase						):
				__UpperCAmelCase							=	DeiTImageProcessor if is_vision_available() else None
				__UpperCAmelCase							=	True





				def 					__snake_case       (    self    :    Optional[int]     ):



							'''simple docstring'''







							snake_case   :							Optional[int]						  =DeiTImageProcessingTester(self     )





				@property
				def 					__snake_case       (    self    :    Optional[int]     ):



							'''simple docstring'''







							return self.image_processor_tester.prepare_image_processor_dict()





				def 					__snake_case       (    self    :    Optional[int]     ):



							'''simple docstring'''







							snake_case   :							Tuple						  =self.image_processing_class(**self.image_processor_dict     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''do_resize'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''size'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''do_center_crop'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''center_crop'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''do_normalize'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''image_mean'''     )     )
							self.assertTrue(hasattr(lowerCAmelCase__,    '''image_std'''     )     )





				def 					__snake_case       (    self    :    str     ):



							'''simple docstring'''







							snake_case   :							Optional[int]						  =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}     )

							snake_case   :							Union[str, Any]						  =self.image_processing_class.from_dict(self.image_processor_dict,    size=42,    crop_size=84     )
							self.assertEqual(image_processor.size,    {'''height''': 42, '''width''': 42}     )
							self.assertEqual(image_processor.crop_size,    {'''height''': 84, '''width''': 84}     )





				def 					__snake_case       (    self    :    List[str]     ):



							'''simple docstring'''







							pass





				def 					__snake_case       (    self    :    Dict     ):



							'''simple docstring'''







							snake_case   :							Any						  =self.image_processing_class(**self.image_processor_dict     )
							# create random PIL images
							snake_case   :							Optional[int]						  =prepare_image_inputs(self.image_processor_tester,    equal_resolution=lowerCAmelCase__     )
							for image in image_inputs:
										self.assertIsInstance(lowerCAmelCase__,    Image.Image     )

							# Test not batched input
							snake_case   :							int						  =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
							snake_case   :							Dict						  =image_processing(lowerCAmelCase__,    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 					__snake_case       (    self    :    Any     ):



							'''simple docstring'''







							snake_case   :							Optional[int]						  =self.image_processing_class(**self.image_processor_dict     )
							# create random numpy tensors
							snake_case   :							List[str]						  =prepare_image_inputs(self.image_processor_tester,    equal_resolution=lowerCAmelCase__,    numpify=lowerCAmelCase__     )
							for image in image_inputs:
										self.assertIsInstance(lowerCAmelCase__,    np.ndarray     )

							# Test not batched input
							snake_case   :							Union[str, Any]						  =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
							snake_case   :							Dict						  =image_processing(lowerCAmelCase__,    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 					__snake_case       (    self    :    Union[str, Any]     ):



							'''simple docstring'''







							snake_case   :							Optional[int]						  =self.image_processing_class(**self.image_processor_dict     )
							# create random PyTorch tensors
							snake_case   :							Tuple						  =prepare_image_inputs(self.image_processor_tester,    equal_resolution=lowerCAmelCase__,    torchify=lowerCAmelCase__     )
							for image in image_inputs:
										self.assertIsInstance(lowerCAmelCase__,    torch.Tensor     )

							# Test not batched input
							snake_case   :							List[str]						  =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
							snake_case   :							Optional[int]						  =image_processing(lowerCAmelCase__,    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'''],
							    ),    )










'''simple docstring'''





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 							lowerCAmelCase_		(	unittest.TestCase						):





    @slow
    @unittest.skip('''Model is not available.'''     )
    def 					__snake_case       (    self    :    List[Any]     ):



       '''simple docstring'''







       snake_case   :							Any						  ='''nvidia/megatron-gpt2-345m/'''
       if "MYDIR" in os.environ:
          snake_case   :							Any						  =os.path.join(os.environ['''MYDIR'''],    _snake_case     )
       snake_case   :							Optional[Any]						  =GPTaLMHeadModel.from_pretrained(_snake_case     )
       model.to(_snake_case     )
       model.half()

       snake_case   :							int						  =torch.tensor(
           [[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]],    device=_snake_case,    dtype=torch.long,    )

       with torch.no_grad():
          snake_case   :							Dict						  =model(_snake_case     ).logits

       snake_case   :							Dict						  =torch.Size((1, 9, 50_257)     )
       self.assertEqual(output.shape,    _snake_case     )

       snake_case   :							str						  =torch.tensor(
           [
               4.9414,
               -0.2920,
               -1.2148,
               -4.0273,
               -0.5161,
               -5.2109,
               -1.2412,
               -1.8301,
               -1.7734,
               -4.7148,
               -0.2317,
               -1.0811,
               -2.1777,
               0.4141,
               -3.7969,
               -4.0586,
               -2.5332,
               -3.3809,
               4.3867,
           ],    device=_snake_case,    dtype=torch.half,    )

       for i in range(19     ):
          snake_case  ,    snake_case   :							Tuple						  =8 * i // 17, 2_792 * i  # along the diagonal
          snake_case  ,    snake_case   :							List[str]						  =output[0, r, c], expected_diag[i]
          snake_case   :							Optional[int]						  =f'''row={r} col={c} computed={computed} expected={expected}'''
          self.assertAlmostEqual(_snake_case,    _snake_case,    delta=1E-4,    msg=_snake_case     )





