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






 '''simple docstring'''






 @slow
 @unittest.skip('Model is not available.')
 def 		__snake_case						(			self					:  Union[str, Any]):







        '''simple docstring'''



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

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

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

        lowerCAmelCase__     	=					torch.Size((1, 9, 50_257))
        self.assertEqual(output.shape			,    lowercase_)

        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=lowercase_			,    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(lowercase_			,    lowercase_			,    delta=1e-4			,    msg=lowercase_)

from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available, is_torch_available


lowerCamelCase__   = {
    '''configuration_ernie_m''': ['''ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ErnieMConfig'''],
}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__   = ['''ErnieMTokenizer''']

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase__   = [
       '''ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST''',
       '''ErnieMForMultipleChoice''',
       '''ErnieMForQuestionAnswering''',
       '''ErnieMForSequenceClassification''',
       '''ErnieMForTokenClassification''',
       '''ErnieMModel''',
       '''ErnieMPreTrainedModel''',
       '''ErnieMForInformationExtraction''',
   ]


if TYPE_CHECKING:
   from .configuration_ernie_m import ERNIE_M_PRETRAINED_CONFIG_ARCHIVE_MAP, ErnieMConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_ernie_m import ErnieMTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_ernie_m import (
          ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST,
          ErnieMForInformationExtraction,
          ErnieMForMultipleChoice,
          ErnieMForQuestionAnswering,
          ErnieMForSequenceClassification,
          ErnieMForTokenClassification,
          ErnieMModel,
          ErnieMPreTrainedModel,
      )


else:
   import sys

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




