
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       :



  """simple docstring"""





  def __init__(					self						,    __UpperCamelCase						,    ):
      A_								=					parent
      A_								=					13
      A_								=					7
      A_								=					True
      A_								=					True
      A_								=					True
      A_								=					True
      A_								=					True
      A_								=					99
      A_								=					32
      A_								=					2
      A_								=					4
      A_								=					37
      A_								=					"gelu"
      A_								=					0.1
      A_								=					0.1
      A_								=					512
      A_								=					16
      A_								=					2
      A_								=					0.02
      A_								=					3
      A_								=					4
      A_								=					None
      A_								=					self.vocab_size - 1

  def 					lowercase_						(					self					):
      A_								=					ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size					)

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

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

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

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

      A_								=					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						,    )

      A_								=					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 					lowercase_						(					self						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    *__UpperCamelCase					):
      A_								=					TFOpenAIGPTModel(config=__UpperCamelCase					)
      A_								=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      A_								=					model(__UpperCamelCase					)

      A_								=					[input_ids, input_mask]
      A_								=					model(__UpperCamelCase					)

      A_								=					model(__UpperCamelCase					)

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

  def 					lowercase_						(					self						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    *__UpperCamelCase					):
      A_								=					TFOpenAIGPTLMHeadModel(config=__UpperCamelCase					)
      A_								=					{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
      A_								=					model(__UpperCamelCase					)
      self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)					)

  def 					lowercase_						(					self						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    *__UpperCamelCase					):
      A_								=					TFOpenAIGPTDoubleHeadsModel(config=__UpperCamelCase					)

      A_								=					tf.tile(tf.expand_dims(__UpperCamelCase						,    1					)						,    (1, self.num_choices, 1)					)
      A_								=					tf.tile(tf.expand_dims(__UpperCamelCase						,    1					)						,    (1, self.num_choices, 1)					)
      A_								=					tf.tile(tf.expand_dims(__UpperCamelCase						,    1					)						,    (1, self.num_choices, 1)					)

      A_								=					{
          "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,
      }
      A_								=					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 					lowercase_						(					self						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    *__UpperCamelCase					):
      A_								=					self.num_labels
      A_								=					ids_tensor([self.batch_size]						,    self.type_sequence_label_size					)
      A_								=					{
          "input_ids": input_ids,
          "attention_mask": input_mask,
          "token_type_ids": token_type_ids,
          "labels": sequence_labels,
      }
      A_								=					TFOpenAIGPTForSequenceClassification(__UpperCamelCase					)
      A_								=					model(__UpperCamelCase					)
      self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)					)

  def 					lowercase_						(					self					):
      A_								=					self.prepare_config_and_inputs()

      (
          (
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	(
          A_
      )	,	
      )								=					config_and_inputs

      A_								=					{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
      return config, inputs_dict



@require_tf
class 							lowerCamelCase       (		__snake_case			,  __snake_case			,  unittest.TestCase					):



  """simple docstring"""





  lowerCAmelCase_								=		(
      (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
      if is_tf_available()
      else ()
  )
  lowerCAmelCase_								=		(
      (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
  )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
  lowerCAmelCase_								=		(
      {
          """feature-extraction""": TFOpenAIGPTModel,
          """text-classification""": TFOpenAIGPTForSequenceClassification,
          """text-generation""": TFOpenAIGPTLMHeadModel,
          """zero-shot""": TFOpenAIGPTForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  lowerCAmelCase_								=		False
  lowerCAmelCase_								=		False

  def 					lowercase_						(					self						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase						,    __UpperCamelCase					):
      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 					lowercase_						(					self					):
      A_								=					TFOpenAIGPTModelTester(self					)
      A_								=					ConfigTester(self						,    config_class=__UpperCamelCase						,    n_embd=37					)

  def 					lowercase_						(					self					):
      self.config_tester.run_common_tests()

  def 					lowercase_						(					self					):
      A_								=					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*__UpperCamelCase					)

  def 					lowercase_						(					self					):
      A_								=					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_lm_head(*__UpperCamelCase					)

  def 					lowercase_						(					self					):
      A_								=					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_double_head(*__UpperCamelCase					)

  def 					lowercase_						(					self					):
      A_								=					self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__UpperCamelCase					)

  @slow
  def 					lowercase_						(					self					):
      for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          A_								=					TFOpenAIGPTModel.from_pretrained(__UpperCamelCase					)
          self.assertIsNotNone(__UpperCamelCase					)



@require_tf
class 							lowerCamelCase       (		unittest.TestCase					):



  """simple docstring"""





  @slow
  def 					lowercase_						(					self					):
      A_								=					TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"					)
      A_								=					tf.convert_to_tensor([[481, 4735, 544]]						,    dtype=tf.intaa					)  # the president is
      A_								=					[
          481,
          4735,
          544,
          246,
          963,
          870,
          762,
          239,
          244,
          40477,
          244,
          249,
          719,
          881,
          487,
          544,
          240,
          244,
          603,
          481,
      ]  # the president is a very good man. " \n " i\'m sure he is, " said the

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








import sys

sys.set_int_max_str_digits(0)  # type: ignore





def        lowerCAmelCase       (				snake_case__   :      int )->			bool:
    A_								=					[0] * 11
    A_								=					[0] * 11

    # mark last 9 numbers
    for _ in range(9 ):
        A_								=					1
        A_								=					number // 10
    # flag
    A_								=					True

    # check last 9 numbers for pandigitality

    for x in range(9 ):
        if not check_last[x + 1]:
            A_								=					False
    if not f:
        return f

    # mark first 9 numbers
    A_								=					int(str(snake_case__ )[:9] )

    for _ in range(9 ):
        A_								=					1
        A_								=					number // 10

    # check first 9 numbers for pandigitality

    for x in range(9 ):
        if not check_front[x + 1]:
            A_								=					False
    return f





def        lowerCAmelCase       (				snake_case__   :      int )->			bool:
    A_								=					[0] * 11

    # mark last 9 numbers
    for _ in range(9 ):
        A_								=					1
        A_								=					number // 10
    # flag
    A_								=					True

    # check last 9 numbers for pandigitality

    for x in range(9 ):
        if not check_last[x + 1]:
            A_								=					False
    return f





def        lowerCAmelCase       (				)->			int:
    A_								=					1
    A_								=					1
    A_								=					2
    # temporary Fibonacci numbers

    A_								=					1
    A_								=					1
    A_								=					2
    # temporary Fibonacci numbers mod 1e9

    # mod m=1e9, done for fast optimisation
    A_								=					[0] * 1000000
    A_								=					1000000000

    for x in range(1000000 ):
        A_								=					(aa + ba) % m
        A_								=					ba % m
        A_								=					ca % m
        if checka(snake_case__ ):
            A_								=					1

    for x in range(1000000 ):
        A_								=					a + b
        A_								=					b
        A_								=					c
        # perform check only if in tocheck
        if tocheck[x + 3] and check(snake_case__ ):
            return x + 3  # first 2 already done
    return -1


if __name__ == "__main__":
     print(f"""{solution() = }""")






