



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					):





   @slow
   @unittest.skip('''Model is not available.'''      )
   def 						__lowerCAmelCase				(						self      ):
         _lowerCAmelCase			      =	'''nvidia/megatron-gpt2-345m/'''
         if "MYDIR" in os.environ:
               _lowerCAmelCase			      =	os.path.join(os.environ['''MYDIR''']							,  _lowerCamelCase      )
         _lowerCAmelCase			      =	GPTaLMHeadModel.from_pretrained(_lowerCamelCase      )
         model.to(_lowerCamelCase      )
         model.half()

         _lowerCAmelCase			      =	torch.tensor(
             [[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]							,  device=_lowerCamelCase							,  dtype=torch.long							,  )

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

         _lowerCAmelCase			      =	torch.Size((1, 9, 50_257)      )
         self.assertEqual(output.shape							,  _lowerCamelCase      )

         _lowerCAmelCase			      =	torch.tensor(
             [
                 4.9_414,
                 -0.2_920,
                 -1.2_148,
                 -4.0_273,
                 -0.5_161,
                 -5.2_109,
                 -1.2_412,
                 -1.8_301,
                 -1.7_734,
                 -4.7_148,
                 -0.2_317,
                 -1.0_811,
                 -2.1_777,
                 0.4_141,
                 -3.7_969,
                 -4.0_586,
                 -2.5_332,
                 -3.3_809,
                 4.3_867,
             ]							,  device=_lowerCamelCase							,  dtype=torch.half							,  )

         for i in range(19      ):
               _lowerCAmelCase      , _lowerCAmelCase			      =	8 * i // 17, 2_792 * 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(_lowerCamelCase							,  _lowerCamelCase							,  delta=1E-4							,  msg=_lowerCamelCase      )







"""simple docstring"""


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


lowercase_								=	{
    'configuration_gptsan_japanese': ['GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP', 'GPTSanJapaneseConfig'],
    'tokenization_gptsan_japanese': ['GPTSanJapaneseTokenizer'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowercase_								=	[
           'GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST',
           'GPTSanJapaneseForConditionalGeneration',
           'GPTSanJapaneseModel',
           'GPTSanJapanesePreTrainedModel',
       ]
       lowercase_								=	[
           'GPTSanJapaneseTokenizer',
       ]


if TYPE_CHECKING:
       from .configuration_gptsan_japanese import GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTSanJapaneseConfig
       from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_gptsan_japanese import (
                  GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST,
                  GPTSanJapaneseForConditionalGeneration,
                  GPTSanJapaneseModel,
                  GPTSanJapanesePreTrainedModel,
              )
              from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer


else:
       import sys

       lowercase_								=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




