
"""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 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




 @slow
 @unittest.skip("""Model is not available.""" )
 def   a      ( self       : List[str] )->					Optional[int]:



      """simple docstring"""

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

      UpperCAmelCase_						:    Any    					=  torch.tensor(
          [[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]     ,   device=a_     ,   dtype=torch.long     ,   )

      with torch.no_grad():
           UpperCAmelCase_						:    Optional[Any]    					=  model(a_ ).logits

      UpperCAmelCase_						:    Tuple    					=  torch.Size((1, 9, 5_02_57) )
      self.assertEqual(output.shape     ,   a_ )

      UpperCAmelCase_						:    Tuple    					=  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=a_     ,   dtype=torch.half     ,   )

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



"""simple docstring"""







import unittest

from transformers.image_processing_utils import get_size_dict



class 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




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



      """simple docstring"""

      UpperCAmelCase_						:    Any    					=  {"""wrong_key""": 2_24}
      with self.assertRaises(a_ ):
           get_size_dict(a_ )

      UpperCAmelCase_						:    Union[str, Any]    					=  {"""height""": 2_24}
      with self.assertRaises(a_ ):
           get_size_dict(a_ )

      UpperCAmelCase_						:    Union[str, Any]    					=  {"""width""": 2_24, """shortest_edge""": 2_24}
      with self.assertRaises(a_ ):
           get_size_dict(a_ )

      # Test a dict with the correct keys is returned as is
      UpperCAmelCase_						:    int    					=  {"""height""": 2_24, """width""": 2_24}
      UpperCAmelCase_						:    Dict    					=  get_size_dict(a_ )
      self.assertEqual(a_     ,   a_ )

      UpperCAmelCase_						:    Optional[int]    					=  {"""shortest_edge""": 2_24}
      UpperCAmelCase_						:    Tuple    					=  get_size_dict(a_ )
      self.assertEqual(a_     ,   {"""shortest_edge""": 2_24} )

      UpperCAmelCase_						:    str    					=  {"""longest_edge""": 2_24, """shortest_edge""": 2_24}
      UpperCAmelCase_						:    Any    					=  get_size_dict(a_ )
      self.assertEqual(a_     ,   {"""longest_edge""": 2_24, """shortest_edge""": 2_24} )

      # Test a single int value which  represents (size, size)
      UpperCAmelCase_						:    Optional[Any]    					=  get_size_dict(2_24 )
      self.assertEqual(a_     ,   {"""height""": 2_24, """width""": 2_24} )

      # Test a single int value which represents the shortest edge
      UpperCAmelCase_						:    List[str]    					=  get_size_dict(2_24     ,   default_to_square=a_ )
      self.assertEqual(a_     ,   {"""shortest_edge""": 2_24} )

      # Test a tuple of ints which represents (height, width)
      UpperCAmelCase_						:    List[Any]    					=  get_size_dict((1_50, 2_00) )
      self.assertEqual(a_     ,   {"""height""": 1_50, """width""": 2_00} )

      # Test a tuple of ints which represents (width, height)
      UpperCAmelCase_						:    List[str]    					=  get_size_dict((1_50, 2_00)     ,   height_width_order=a_ )
      self.assertEqual(a_     ,   {"""height""": 2_00, """width""": 1_50} )

      # Test an int representing the shortest edge and max_size which represents the longest edge
      UpperCAmelCase_						:    List[Any]    					=  get_size_dict(2_24     ,   max_size=2_56     ,   default_to_square=a_ )
      self.assertEqual(a_     ,   {"""shortest_edge""": 2_24, """longest_edge""": 2_56} )

      # Test int with default_to_square=True and max_size fails
      with self.assertRaises(a_ ):
           get_size_dict(2_24     ,   max_size=2_56     ,   default_to_square=a_ )

