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 						__UpperCAmelCase							(						self							:					Union[str, Any])		->     Any:


   """simple docstring"""







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

   _UpperCamelCase				=  torch.tensor(
       [[101, 7110, 1005, 1056, 2023, 11333, 17413, 1029, 102]]				,      device=lowercase_				,      dtype=torch.long				,      )

   with torch.no_grad():
     _UpperCamelCase				=  model(lowercase_).logits

   _UpperCamelCase				=  torch.Size((1, 9, 50257))
   self.assertEqual(output.shape				,      lowercase_)

   _UpperCamelCase				=  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=lowercase_				,      dtype=torch.half				,      )

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





lowerCamelCase__   = '''
# Transformers installation
! pip install transformers datasets
# To install from source instead of the last release, comment the command above and uncomment the following one.
# ! pip install git+https://github.com/huggingface/transformers.git
'''

lowerCamelCase__   = [{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
lowerCamelCase__   = {
    '''{processor_class}''': '''FakeProcessorClass''',
    '''{model_class}''': '''FakeModelClass''',
    '''{object_class}''': '''FakeObjectClass''',
}




