



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       A__    (    self:     Dict							)	->      Union[str, Any]:
       UpperCAmelCase_     :       Any  		=					"""nvidia/megatron-gpt2-345m/"""
       if "MYDIR" in os.environ:
           UpperCAmelCase_     :       List[str]  		=					os.path.join(os.environ["""MYDIR"""]  ,lowerCamelCase_							)
       UpperCAmelCase_     :       Optional[int]  		=					GPTaLMHeadModel.from_pretrained(lowerCamelCase_							)
       model.to(lowerCamelCase_							)
       model.half()

       UpperCAmelCase_     :       str  		=					torch.tensor(
           [[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]  ,device=lowerCamelCase_  ,dtype=torch.long  ,)

       with torch.no_grad():
           UpperCAmelCase_     :       str  		=					model(lowerCamelCase_							).logits

       UpperCAmelCase_     :       Union[str, Any]  		=					torch.Size((1, 9, 50257)							)
       self.assertEqual(output.shape  ,lowerCamelCase_							)

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

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



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

UpperCAmelCase__			:  int					     =     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        __lowercase   (  cls      )  ->    Any:
        return cls






def        __lowercase   (  _A    ,     _A = None    ,     _A = None    ,     _A = None    ,     _A = None    ,     )  ->    Tuple:
      SCREAMING_SNAKE_CASE			:   Optional[Any]	  = device
      SCREAMING_SNAKE_CASE			:   Tuple	  = shape[0]

      SCREAMING_SNAKE_CASE			:   Optional[int]	  = layout or torch.strided
      SCREAMING_SNAKE_CASE			:   Dict	  = device or torch.device("""cpu"""      )

      if generator is not None:
            SCREAMING_SNAKE_CASE			:   Optional[Any]	  = generator.device.type if not isinstance(_A    ,     _A      ) else generator[0].device.type
            if gen_device_type != device.type and gen_device_type == "cpu":
                  SCREAMING_SNAKE_CASE			:   Tuple	  = """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(_A    ,     _A      ):
            SCREAMING_SNAKE_CASE			:   int	  = (1,) + shape[1:]
            SCREAMING_SNAKE_CASE			:   Any	  = [
                torch.randn(_A    ,     generator=generator[i]    ,     device=_A    ,     dtype=_A    ,     layout=_A      )
                for i in range(_A      )
            ]
            SCREAMING_SNAKE_CASE			:   Any	  = torch.cat(_A    ,     dim=0      ).to(_A      )
      else:
            SCREAMING_SNAKE_CASE			:   int	  = torch.randn(_A    ,     generator=_A    ,     device=_A    ,     dtype=_A    ,     layout=_A      ).to(_A      )

      return latents







def        __lowercase   (  _A      )  ->    Dict:
      if is_torch_version("""<"""    ,     """2.0.0"""      ) or not hasattr(_A    ,     """_dynamo"""      ):
            return False
      return isinstance(_A    ,     torch._dynamo.eval_frame.OptimizedModule      )




