


"""simple docstring"""




from __future__ import annotations

from typing import Any
def     UpperCAmelCase_ (							*__snake_case						)					->      list[list[int]]:







 """simple docstring"""


 if all(_check_not_integer(__snake_case						) for m in matrix_s						):
  for i in matrix_s[1:]:
   _verify_matrix_sizes(matrix_s[0]					,						__snake_case						)
  return [[sum(__snake_case						) for t in zip(*__snake_case						)] for m in zip(*__snake_case						)]
 raise TypeError('''Expected a matrix, got int/list instead'''						)
def     UpperCAmelCase_ (							__snake_case					,						__snake_case						)					->      list[list[int]]:







 """simple docstring"""


 if (
     _check_not_integer(__snake_case						)
     and _check_not_integer(__snake_case						)
     and _verify_matrix_sizes(__snake_case					,						__snake_case						)
 ):
  return [[i - j for i, j in zip(*__snake_case						)] for m in zip(__snake_case					,						__snake_case						)]
 raise TypeError('''Expected a matrix, got int/list instead'''						)
def     UpperCAmelCase_ (							__snake_case					,						__snake_case						)					->      list[list[float]]:







 """simple docstring"""


 return [[x * n for x in row] for row in matrix]
def     UpperCAmelCase_ (							__snake_case					,						__snake_case						)					->      list[list[int]]:







 """simple docstring"""


 if _check_not_integer(__snake_case						) and _check_not_integer(__snake_case						):
  _lowercase				,   _lowercase											=_verify_matrix_sizes(__snake_case					,						__snake_case						)

 if cols[0] != rows[1]:
  _lowercase											=(
      '''Cannot multiply matrix of dimensions '''
      F"({rows[0]},{cols[0]}) and ({rows[1]},{cols[1]})"
  )
  raise ValueError(__snake_case						)
 return [
     [sum(m * n for m, n in zip(__snake_case					,						__snake_case						)						) for j in zip(*__snake_case						)] for i in matrix_a
 ]
def     UpperCAmelCase_ (							__snake_case						)					->      list[list[int]]:







 """simple docstring"""


 _lowercase											=int(__snake_case						)
 return [[int(row == column						) for column in range(__snake_case						)] for row in range(__snake_case						)]
def     UpperCAmelCase_ (							__snake_case					,						__snake_case = True						)					->      list[list[int]] | map[list[int]]:







 """simple docstring"""


 if _check_not_integer(__snake_case						):
  if return_map:
   return map(__snake_case					,						zip(*__snake_case						)						)
  else:
   return list(map(__snake_case					,						zip(*__snake_case						)						)						)
 raise TypeError('''Expected a matrix, got int/list instead'''						)
def     UpperCAmelCase_ (							__snake_case					,						__snake_case					,						__snake_case						)					->      list[list[int]]:







 """simple docstring"""


 _lowercase											=matrix[:row] + matrix[row + 1 :]
 return [row[:column] + row[column + 1 :] for row in minor]
def     UpperCAmelCase_ (							__snake_case						)					->      Any:







 """simple docstring"""


 if len(__snake_case						) == 1:
  return matrix[0][0]

 return sum(
     x * determinant(minor(__snake_case					,						0					,						__snake_case						)						) * (-1) ** i
     for i, x in enumerate(matrix[0]						)						)
def     UpperCAmelCase_ (							__snake_case						)					->      list[list[float]] | None:







 """simple docstring"""


 _lowercase											=determinant(__snake_case						)
 if det == 0:
  return None

 _lowercase											=[
     [determinant(minor(__snake_case					,						__snake_case					,						__snake_case						)						) for j in range(len(__snake_case						)						)]
     for i in range(len(__snake_case						)						)
 ]

 _lowercase											=[
     [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row]						)]
     for row in range(len(__snake_case						)						)
 ]
 _lowercase											=list(transpose(__snake_case						)						)
 return scalar_multiply(__snake_case					,						1 / det						)
def     UpperCAmelCase_ (							__snake_case						)					->      bool:







 """simple docstring"""


 return not isinstance(__snake_case					,						__snake_case						) and not isinstance(matrix[0]					,						__snake_case						)
def     UpperCAmelCase_ (							__snake_case						)					->      tuple[int, int]:







 """simple docstring"""


 return len(__snake_case						), len(matrix[0]						)
def     UpperCAmelCase_ (							__snake_case					,						__snake_case						)					->      tuple[tuple[int, int], tuple[int, int]]:







 """simple docstring"""


 _lowercase											=_shape(__snake_case						) + _shape(__snake_case						)
 if shape[0] != shape[3] or shape[1] != shape[2]:
  _lowercase											=(
      '''operands could not be broadcast together with shape '''
      F"({shape[0], shape[1]}), ({shape[2], shape[3]})"
  )
  raise ValueError(__snake_case						)
 return (shape[0], shape[2]), (shape[1], shape[3])
def     UpperCAmelCase_ (							)					->      None:







 """simple docstring"""


 _lowercase											=[[12, 10], [3, 9]]
 _lowercase											=[[3, 4], [7, 4]]
 _lowercase											=[[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
 _lowercase											=[[3, 0, 2], [2, 0, -2], [0, 1, 1]]
 print(F"Add Operation, {add(__snake_case					,						__snake_case						) = } \n"						)
 print(
     F"Multiply Operation, {multiply(__snake_case					,						__snake_case						) = } \n"					,						)
 print(F"Identity: {identity(5						)}\n"						)
 print(F"Minor of {matrix_c} = {minor(__snake_case					,						1					,						2						)} \n"						)
 print(F"Determinant of {matrix_b} = {determinant(__snake_case						)} \n"						)
 print(F"Inverse of {matrix_d} = {inverse(__snake_case						)}\n"						)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()





from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers.models.openai.modeling_tf_openai import (
      TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFOpenAIGPTDoubleHeadsModel,
      TFOpenAIGPTForSequenceClassification,
      TFOpenAIGPTLMHeadModel,
      TFOpenAIGPTModel,
  )







class 				lowerCamelCase__       :





     def __init__(self		,					UpperCAmelCase		,					)					->       str:
      _lowercase											=parent
      _lowercase											=1_3
      _lowercase											=7
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=True
      _lowercase											=9_9
      _lowercase											=3_2
      _lowercase											=2
      _lowercase											=4
      _lowercase											=3_7
      _lowercase											='''gelu'''
      _lowercase											=0.1
      _lowercase											=0.1
      _lowercase											=5_1_2
      _lowercase											=1_6
      _lowercase											=2
      _lowercase											=0.02
      _lowercase											=3
      _lowercase											=4
      _lowercase											=None
      _lowercase											=self.vocab_size - 1





     def 					__A (self	)					->       str:
      _lowercase											=ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size	)

      _lowercase											=None
      if self.use_input_mask:
       _lowercase											=random_attention_mask([self.batch_size, self.seq_length]	)

      _lowercase											=None
      if self.use_token_type_ids:
       _lowercase											=ids_tensor([self.batch_size, self.seq_length]		,					self.type_vocab_size	)

      _lowercase											=None
      if self.use_mc_token_ids:
       _lowercase											=ids_tensor([self.batch_size, self.num_choices]		,					self.seq_length	)

      _lowercase											=None
      _lowercase											=None
      _lowercase											=None
      if self.use_labels:
       _lowercase											=ids_tensor([self.batch_size]		,					self.type_sequence_label_size	)
       _lowercase											=ids_tensor([self.batch_size, self.seq_length]		,					self.num_labels	)
       _lowercase											=ids_tensor([self.batch_size]		,					self.num_choices	)

      _lowercase											=OpenAIGPTConfig(
          vocab_size=self.vocab_size		,					n_embd=self.hidden_size		,					n_layer=self.num_hidden_layers		,					n_head=self.num_attention_heads		,					n_positions=self.max_position_embeddings		,					pad_token_id=self.pad_token_id		,					)

      _lowercase											=ids_tensor([self.num_hidden_layers, self.num_attention_heads]		,					2	)

      return (
          config,
          input_ids,
          input_mask,
          head_mask,
          token_type_ids,
          mc_token_ids,
          sequence_labels,
          token_labels,
          choice_labels,
      )





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       List[Any]:
      _lowercase											=TFOpenAIGPTModel(config=UpperCAmelCase	)
      _lowercase											={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
      _lowercase											=model(UpperCAmelCase	)

      _lowercase											=[input_ids, input_mask]
      _lowercase											=model(UpperCAmelCase	)

      _lowercase											=model(UpperCAmelCase	)

      self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size)	)





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       Optional[Any]:
      _lowercase											=TFOpenAIGPTLMHeadModel(config=UpperCAmelCase	)
      _lowercase											={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
      _lowercase											=model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.seq_length, self.vocab_size)	)





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       str:
      _lowercase											=TFOpenAIGPTDoubleHeadsModel(config=UpperCAmelCase	)

      _lowercase											=tf.tile(tf.expand_dims(UpperCAmelCase		,					1	)		,					(1, self.num_choices, 1)	)
      _lowercase											=tf.tile(tf.expand_dims(UpperCAmelCase		,					1	)		,					(1, self.num_choices, 1)	)
      _lowercase											=tf.tile(tf.expand_dims(UpperCAmelCase		,					1	)		,					(1, self.num_choices, 1)	)

      _lowercase											={
          '''input_ids''': multiple_choice_inputs_ids,
          '''mc_token_ids''': mc_token_ids,
          '''attention_mask''': multiple_choice_input_mask,
          '''token_type_ids''': multiple_choice_token_type_ids,
      }
      _lowercase											=model(UpperCAmelCase	)
      self.parent.assertEqual(
          result.logits.shape		,					(self.batch_size, self.num_choices, self.seq_length, self.vocab_size)	)
      self.parent.assertEqual(result.mc_logits.shape		,					(self.batch_size, self.num_choices)	)





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					*UpperCAmelCase	)					->       Any:
      _lowercase											=self.num_labels
      _lowercase											=ids_tensor([self.batch_size]		,					self.type_sequence_label_size	)
      _lowercase											={
          '''input_ids''': input_ids,
          '''attention_mask''': input_mask,
          '''token_type_ids''': token_type_ids,
          '''labels''': sequence_labels,
      }
      _lowercase											=TFOpenAIGPTForSequenceClassification(UpperCAmelCase	)
      _lowercase											=model(UpperCAmelCase	)
      self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)	)





     def 					__A (self	)					->       List[str]:
      _lowercase											=self.prepare_config_and_inputs()

      (
          (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   (
          _lowercase
      )				,   
      )											=config_and_inputs

      _lowercase											={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
      return config, inputs_dict







@require_tf
class 				lowerCamelCase__       (	lowerCAmelCase       ,							lowerCAmelCase       ,							unittest.TestCase):
     SCREAMING_SNAKE_CASE__				      =      (
         (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
         if is_tf_available()
         else ()
     )
     SCREAMING_SNAKE_CASE__				      =      (
         (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
     SCREAMING_SNAKE_CASE__				      =      (
         {
             '''feature-extraction''': TFOpenAIGPTModel,
             '''text-classification''': TFOpenAIGPTForSequenceClassification,
             '''text-generation''': TFOpenAIGPTLMHeadModel,
             '''zero-shot''': TFOpenAIGPTForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     SCREAMING_SNAKE_CASE__				      =      False
     SCREAMING_SNAKE_CASE__				      =      False





     def 					__A (self		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase		,					UpperCAmelCase	)					->       Optional[Any]:
      if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
       # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
       # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
       # tiny config could not be created.
       return True

      return False





     def 					__A (self	)					->       List[str]:
      _lowercase											=TFOpenAIGPTModelTester(self	)
      _lowercase											=ConfigTester(self		,					config_class=UpperCAmelCase		,					n_embd=3_7	)





     def 					__A (self	)					->       Any:
      self.config_tester.run_common_tests()





     def 					__A (self	)					->       Optional[int]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*UpperCAmelCase	)





     def 					__A (self	)					->       Union[str, Any]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCAmelCase	)





     def 					__A (self	)					->       Tuple:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_double_head(*UpperCAmelCase	)





     def 					__A (self	)					->       Union[str, Any]:
      _lowercase											=self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCAmelCase	)





     @slow
     def 					__A (self	)					->       Union[str, Any]:
      for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _lowercase											=TFOpenAIGPTModel.from_pretrained(UpperCAmelCase	)
       self.assertIsNotNone(UpperCAmelCase	)







@require_tf
class 				lowerCamelCase__       (	unittest.TestCase):





     @slow
     def 					__A (self	)					->       str:
      _lowercase											=TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''	)
      _lowercase											=tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]		,					dtype=tf.intaa	)  # the president is
      _lowercase											=[
          4_8_1,
          4_7_3_5,
          5_4_4,
          2_4_6,
          9_6_3,
          8_7_0,
          7_6_2,
          2_3_9,
          2_4_4,
          4_0_4_7_7,
          2_4_4,
          2_4_9,
          7_1_9,
          8_8_1,
          4_8_7,
          5_4_4,
          2_4_0,
          2_4_4,
          6_0_3,
          4_8_1,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

      _lowercase											=model.generate(UpperCAmelCase		,					do_sample=UpperCAmelCase	)
      self.assertListEqual(output_ids[0].numpy().tolist()		,					UpperCAmelCase	)

