






"""simple docstring"""


from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
       import torch

lowercase_								=	logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
       from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):



       def    lowerCAmelCase							(       cls					):




         """simple docstring"""







         return cls



def    lowerCAmelCase							(       __UpperCamelCase						,			__UpperCamelCase = None						,			__UpperCamelCase = None						,			__UpperCamelCase = None						,			__UpperCamelCase = None						,			):




  """simple docstring"""







  __A		     =					device
  __A		     =					shape[0]

  __A		     =					layout or torch.strided
  __A		     =					device or torch.device('''cpu'''					)

  if generator is not None:
    __A		     =					generator.device.type if not isinstance(__UpperCamelCase						,			__UpperCamelCase					) else generator[0].device.type
    if gen_device_type != device.type and gen_device_type == "cpu":
      __A		     =					'''cpu'''
      if device != "mps":
        logger.info(
            f'The passed generator was created on \'cpu\' even though a tensor on {device} was expected.'
            f' Tensors will be created on \'cpu\' and then moved to {device}. Note that one can probably'
            f' slighly speed up this function by passing a generator that was created on the {device} device.'					)
    elif gen_device_type != device.type and gen_device_type == "cuda":
      raise ValueError(f'Cannot generate a {device} tensor from a generator of type {gen_device_type}.'					)

  if isinstance(__UpperCamelCase						,			__UpperCamelCase					):
    __A		     =					(1,) + shape[1:]
    __A		     =					[
        torch.randn(__UpperCamelCase						,			generator=generator[i]						,			device=__UpperCamelCase						,			dtype=__UpperCamelCase						,			layout=__UpperCamelCase					)
        for i in range(__UpperCamelCase					)
    ]
    __A		     =					torch.cat(__UpperCamelCase						,			dim=0					).to(__UpperCamelCase					)
  else:
    __A		     =					torch.randn(__UpperCamelCase						,			generator=__UpperCamelCase						,			device=__UpperCamelCase						,			dtype=__UpperCamelCase						,			layout=__UpperCamelCase					).to(__UpperCamelCase					)

  return latents





def    lowerCAmelCase							(       __UpperCamelCase					):




  """simple docstring"""







  if is_torch_version('''<'''						,			'''2.0.0'''					) or not hasattr(__UpperCamelCase						,			'''_dynamo'''					):
    return False
  return isinstance(__UpperCamelCase						,			torch._dynamo.eval_frame.OptimizedModule					)












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


   '''simple docstring'''

   @slow
   @unittest.skip('''Model is not available.'''    )
   def 							_SCREAMING_SNAKE_CASE							(      self					:	int    ):

     '''simple docstring'''
     __A		     =					'''nvidia/megatron-gpt2-345m/'''
     if "MYDIR" in os.environ:
       __A		     =					os.path.join(os.environ['''MYDIR'''],			_lowerCamelCase    )
     __A		     =					GPTaLMHeadModel.from_pretrained(_lowerCamelCase    )
     model.to(_lowerCamelCase    )
     model.half()

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

     with torch.no_grad():
       __A		     =					model(_lowerCamelCase    ).logits

     __A		     =					torch.Size((1, 9, 5_02_57)    )
     self.assertEqual(output.shape,			_lowerCamelCase    )

     __A		     =					torch.tensor(
         [
             4.94_14,
             -0.29_20,
             -1.21_48,
             -4.02_73,
             -0.51_61,
             -5.21_09,
             -1.24_12,
             -1.83_01,
             -1.77_34,
             -4.71_48,
             -0.23_17,
             -1.08_11,
             -2.17_77,
             0.41_41,
             -3.79_69,
             -4.05_86,
             -2.53_32,
             -3.38_09,
             4.38_67,
         ],			device=_lowerCamelCase,			dtype=torch.half,			)

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




