
'''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     __UpperCAmelCase      ( self):
          lowerCamelCase__				   =      "nvidia/megatron-gpt2-345m/"
          if "MYDIR" in os.environ:
                 lowerCamelCase__				   =      os.path.join(os.environ["MYDIR"]			,   UpperCamelCase)
          lowerCamelCase__				   =      GPTaLMHeadModel.from_pretrained(UpperCamelCase)
          model.to(UpperCamelCase)
          model.half()

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

          with torch.no_grad():
                 lowerCamelCase__				   =      model(UpperCamelCase).logits

          lowerCamelCase__				   =      torch.Size((1, 9, 5_02_57))
          self.assertEqual(output.shape			,   UpperCamelCase)

          lowerCamelCase__				   =      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):
                 lowerCamelCase__   ,		lowerCamelCase__				   =      8 * i // 17, 27_92 * i  # along the diagonal
                 lowerCamelCase__   ,		lowerCamelCase__				   =      output[0, r, c], expected_diag[i]
                 lowerCamelCase__				   =      f"""row={r} col={c} computed={computed} expected={expected}"""
                 self.assertAlmostEqual(UpperCamelCase			,   UpperCamelCase			,   delta=1E-4			,   msg=UpperCamelCase)



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

lowerCAmelCase_      =   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    :  Dict      ):







           '''simple docstring'''






           return cls







def   lowerCAmelCase(  a__    :  Union[Tuple, List]      , a__    :  Optional[Union[List["torch.Generator"], "torch.Generator"]] = None      , a__    :  Optional["torch.device"] = None      , a__    :  Optional["torch.dtype"] = None      , a__    :  Optional["torch.layout"] = None      , ):







       '''simple docstring'''






       lowerCamelCase__				   =      device
       lowerCamelCase__				   =      shape[0]

       lowerCamelCase__				   =      layout or torch.strided
       lowerCamelCase__				   =      device or torch.device("cpu"      )

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

       return latents







def   lowerCAmelCase(  a__    :  Union[str, Any]      ):







       '''simple docstring'''






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

