import logging

# standard libraries
import sys

import numpy as np
import pytest  # type: ignore

# Custom/local libraries
from matrix import matrix_operation as matop

UpperCAmelCase							=       [[1_2, 1_0], [3, 9]]
UpperCAmelCase							=       [[3, 4], [7, 4]]
UpperCAmelCase							=       [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
UpperCAmelCase							=       [[3, 0, -2], [2, 0, 2], [0, 1, 1]]
UpperCAmelCase							=       [[3, 0, 2], [2, 0, -2], [0, 1, 1], [2, 0, -2]]
UpperCAmelCase							=       [1]
UpperCAmelCase							=       [2]

UpperCAmelCase							=       logging.getLogger()
UpperCAmelCase							=       logging.DEBUG
UpperCAmelCase							=       logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)






@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2") ,	[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]     )
def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: str ,	lowerCAmelCase_: Tuple     ):
      if (np.array(lowerCAmelCase_     )).shape < (2, 2) or (np.array(lowerCAmelCase_     )).shape < (2, 2):
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(f"\n\t{test_addition.__name__} returned integer"     )
                  matop.add(lowerCAmelCase_ ,	lowerCAmelCase_     )
      elif (np.array(lowerCAmelCase_     )).shape == (np.array(lowerCAmelCase_     )).shape:
            logger.info(f"\n\t{test_addition.__name__} with same matrix dims"     )
            snake_case_     :			Union[str, Any]       =       (np.array(lowerCAmelCase_     ) + np.array(lowerCAmelCase_     )).tolist()
            snake_case_     :			Optional[Any]       =       matop.add(lowerCAmelCase_ ,	lowerCAmelCase_     )
            assert theo == act
      else:
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(f"\n\t{test_addition.__name__} with different matrix dims"     )
                  matop.add(lowerCAmelCase_ ,	lowerCAmelCase_     )






@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2") ,	[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]     )
def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: Tuple ,	lowerCAmelCase_: Optional[int]     ):
      if (np.array(lowerCAmelCase_     )).shape < (2, 2) or (np.array(lowerCAmelCase_     )).shape < (2, 2):
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(f"\n\t{test_subtraction.__name__} returned integer"     )
                  matop.subtract(lowerCAmelCase_ ,	lowerCAmelCase_     )
      elif (np.array(lowerCAmelCase_     )).shape == (np.array(lowerCAmelCase_     )).shape:
            logger.info(f"\n\t{test_subtraction.__name__} with same matrix dims"     )
            snake_case_     :			Any       =       (np.array(lowerCAmelCase_     ) - np.array(lowerCAmelCase_     )).tolist()
            snake_case_     :			Optional[int]       =       matop.subtract(lowerCAmelCase_ ,	lowerCAmelCase_     )
            assert theo == act
      else:
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(f"\n\t{test_subtraction.__name__} with different matrix dims"     )
                  assert matop.subtract(lowerCAmelCase_ ,	lowerCAmelCase_     )






@pytest.mark.mat_ops
@pytest.mark.parametrize(
    ("mat1", "mat2") ,	[(mat_a, mat_b), (mat_c, mat_d), (mat_d, mat_e), (mat_f, mat_h)]     )
def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: int ,	lowerCAmelCase_: Dict     ):
      if (np.array(lowerCAmelCase_     )).shape < (2, 2) or (np.array(lowerCAmelCase_     )).shape < (2, 2):
            logger.info(f"\n\t{test_multiplication.__name__} returned integer"     )
            with pytest.raises(lowerCAmelCase_     ):
                  matop.add(lowerCAmelCase_ ,	lowerCAmelCase_     )
      elif (np.array(lowerCAmelCase_     )).shape == (np.array(lowerCAmelCase_     )).shape:
            logger.info(f"\n\t{test_multiplication.__name__} meets dim requirements"     )
            snake_case_     :			Optional[int]       =       (np.matmul(lowerCAmelCase_ ,	lowerCAmelCase_     )).tolist()
            snake_case_     :			Optional[int]       =       matop.multiply(lowerCAmelCase_ ,	lowerCAmelCase_     )
            assert theo == act
      else:
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(
                      f"\n\t{test_multiplication.__name__} does not meet dim requirements"     )
                  assert matop.subtract(lowerCAmelCase_ ,	lowerCAmelCase_     )






@pytest.mark.mat_ops
def 					SCREAMING_SNAKE_CASE_			(		):
      snake_case_     :			Dict       =       (3.5 * np.array(lowerCAmelCase_     )).tolist()
      snake_case_     :			str       =       matop.scalar_multiply(lowerCAmelCase_ ,	3.5     )
      assert theo == act






@pytest.mark.mat_ops
def 					SCREAMING_SNAKE_CASE_			(		):
      snake_case_     :			Optional[Any]       =       (np.identity(5     )).tolist()
      snake_case_     :			Optional[Any]       =       matop.identity(5     )
      assert theo == act






@pytest.mark.mat_ops
@pytest.mark.parametrize("mat" ,	[mat_a, mat_b, mat_c, mat_d, mat_e, mat_f]     )
def 					SCREAMING_SNAKE_CASE_			(		lowerCAmelCase_: int     ):
      if (np.array(lowerCAmelCase_     )).shape < (2, 2):
            with pytest.raises(lowerCAmelCase_     ):
                  logger.info(f"\n\t{test_transpose.__name__} returned integer"     )
                  matop.transpose(lowerCAmelCase_     )
      else:
            snake_case_     :			Optional[int]       =       (np.transpose(lowerCAmelCase_     )).tolist()
            snake_case_     :			Optional[int]       =       matop.transpose(lowerCAmelCase_ ,	return_map=lowerCAmelCase_     )
            assert theo == act




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



   @slow
   @unittest.skip("Model is not available."   )
   def 					UpperCAmelCase__       (				self   :   Dict   )							->     Optional[Any]:




         '''simple docstring'''




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

         snake_case_     :			Optional[int]       =       torch.tensor(
             [[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]			,	device=A__			,	dtype=torch.long			,	)

         with torch.no_grad():
               snake_case_     :			Union[str, Any]       =       model(A__   ).logits

         snake_case_     :			List[str]       =       torch.Size((1, 9, 5_02_57)   )
         self.assertEqual(output.shape			,	A__   )

         snake_case_     :			Dict       =       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=A__			,	dtype=torch.half			,	)

         for i in range(19   ):
               snake_case_  ,snake_case_     :			Dict       =       8 * i // 17, 27_92 * i  # along the diagonal
               snake_case_  ,snake_case_     :			Any       =       output[0, r, c], expected_diag[i]
               snake_case_     :			Dict       =       f"row={r} col={c} computed={computed} expected={expected}"
               self.assertAlmostEqual(A__			,	A__			,	delta=1E-4			,	msg=A__   )



