




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE__ :   int						=     {
    'configuration_musicgen': [
        'MUSICGEN_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MusicgenConfig',
        'MusicgenDecoderConfig',
    ],
    'processing_musicgen': ['MusicgenProcessor'],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__ :   Union[str, Any]						=     [
           'MUSICGEN_PRETRAINED_MODEL_ARCHIVE_LIST',
           'MusicgenForConditionalGeneration',
           'MusicgenForCausalLM',
           'MusicgenModel',
           'MusicgenPreTrainedModel',
       ]

if TYPE_CHECKING:
       from .configuration_musicgen import (
           MUSICGEN_PRETRAINED_CONFIG_ARCHIVE_MAP,
           MusicgenConfig,
           MusicgenDecoderConfig,
       )
       from .processing_musicgen import MusicgenProcessor

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_musicgen import (
                  MUSICGEN_PRETRAINED_MODEL_ARCHIVE_LIST,
                  MusicgenForCausalLM,
                  MusicgenForConditionalGeneration,
                  MusicgenModel,
                  MusicgenPreTrainedModel,
              )

else:
       import sys

       SCREAMING_SNAKE_CASE__ :   Union[str, Any]						=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)












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

           lowerCamelCase     :   Dict    				=      torch.tensor(
               [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  ,						device=UpperCamelCase__  ,						dtype=torch.long  ,						)

           with torch.no_grad():
                  lowerCamelCase     :   Any    				=      model(UpperCamelCase__  ).logits

           lowerCamelCase     :   Union[str, Any]    				=      torch.Size((1, 9, 5_0257)  )
           self.assertEqual(output.shape  ,						UpperCamelCase__  )

           lowerCamelCase     :   List[str]    				=      torch.tensor(
               [
                   4.9414,
                   -0.2920,
                   -1.2148,
                   -4.0273,
                   -0.5161,
                   -5.2109,
                   -1.2412,
                   -1.8301,
                   -1.7734,
                   -4.7148,
                   -0.2317,
                   -1.0811,
                   -2.1777,
                   0.4141,
                   -3.7969,
                   -4.0586,
                   -2.5332,
                   -3.3809,
                   4.3867,
               ]  ,						device=UpperCamelCase__  ,						dtype=torch.half  ,						)

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






