
import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     YosoForMaskedLM,
     YosoForMultipleChoice,
     YosoForQuestionAnswering,
     YosoForSequenceClassification,
     YosoForTokenClassification,
     YosoModel,
 )
 from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST


class   __lowerCAmelCase		:



      """simple docstring"""







      def __init__( self						:    Union[str, Any]			,    _snake_case						:    List[str]			,    _snake_case						:    List[str]=13			,    _snake_case						:    Optional[int]=7			,    _snake_case						:    str=True			,    _snake_case						:    str=True			,    _snake_case						:    List[str]=True			,    _snake_case						:    Optional[Any]=True			,    _snake_case						:    int=99			,    _snake_case						:    Dict=32			,    _snake_case						:    Tuple=5			,    _snake_case						:    Tuple=4			,    _snake_case						:    Optional[Any]=37			,    _snake_case						:    Union[str, Any]="gelu"			,    _snake_case						:    Tuple=0.1			,    _snake_case						:    Dict=0.1			,    _snake_case						:    Union[str, Any]=512			,    _snake_case						:    Union[str, Any]=16			,    _snake_case						:    Optional[Any]=2			,    _snake_case						:    str=0.02			,    _snake_case						:    str=3			,    _snake_case						:    Any=4			,    _snake_case						:    Union[str, Any]=None			,    ):
          __lowercase						:     Any		      =    parent
          __lowercase						:     List[Any]		      =    batch_size
          __lowercase						:     List[str]		      =    seq_length
          __lowercase						:     Optional[int]		      =    is_training
          __lowercase						:     List[Any]		      =    use_input_mask
          __lowercase						:     str		      =    use_token_type_ids
          __lowercase						:     Optional[int]		      =    use_labels
          __lowercase						:     Dict		      =    vocab_size
          __lowercase						:     Dict		      =    hidden_size
          __lowercase						:     Optional[Any]		      =    num_hidden_layers
          __lowercase						:     int		      =    num_attention_heads
          __lowercase						:     Optional[int]		      =    intermediate_size
          __lowercase						:     str		      =    hidden_act
          __lowercase						:     Union[str, Any]		      =    hidden_dropout_prob
          __lowercase						:     Tuple		      =    attention_probs_dropout_prob
          __lowercase						:     str		      =    max_position_embeddings
          __lowercase						:     str		      =    type_vocab_size
          __lowercase						:     Any		      =    type_sequence_label_size
          __lowercase						:     str		      =    initializer_range
          __lowercase						:     Any		      =    num_labels
          __lowercase						:     Optional[Any]		      =    num_choices
          __lowercase						:     Optional[Any]		      =    scope




      def 						snake_case_     ( self						:    Union[str, Any]			):
          __lowercase						:     List[str]		      =    ids_tensor([self.batch_size, self.seq_length]			,    self.vocab_size			)

          __lowercase						:     Dict		      =    None
          if self.use_input_mask:
              __lowercase						:     Tuple		      =    random_attention_mask([self.batch_size, self.seq_length]			)

          __lowercase						:     Union[str, Any]		      =    None
          if self.use_token_type_ids:
              __lowercase						:     Union[str, Any]		      =    ids_tensor([self.batch_size, self.seq_length]			,    self.type_vocab_size			)

          __lowercase						:     Union[str, Any]		      =    None
          __lowercase						:     List[Any]		      =    None
          __lowercase						:     List[Any]		      =    None
          if self.use_labels:
              __lowercase						:     List[Any]		      =    ids_tensor([self.batch_size]			,    self.type_sequence_label_size			)
              __lowercase						:     int		      =    ids_tensor([self.batch_size, self.seq_length]			,    self.num_labels			)
              __lowercase						:     Dict		      =    ids_tensor([self.batch_size]			,    self.num_choices			)

          __lowercase						:     Tuple		      =    self.get_config()

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




      def 						snake_case_     ( self						:    Union[str, Any]			):
          return YosoConfig(
              vocab_size=self.vocab_size			,    hidden_size=self.hidden_size			,    num_hidden_layers=self.num_hidden_layers			,    num_attention_heads=self.num_attention_heads			,    intermediate_size=self.intermediate_size			,    hidden_act=self.hidden_act			,    hidden_dropout_prob=self.hidden_dropout_prob			,    attention_probs_dropout_prob=self.attention_probs_dropout_prob			,    max_position_embeddings=self.max_position_embeddings			,    type_vocab_size=self.type_vocab_size			,    is_decoder=_snake_case			,    initializer_range=self.initializer_range			,    )




      def 						snake_case_     ( self						:    List[str]			):
          __lowercase						:     Dict		      =    self.get_config()
          __lowercase						:     Any		      =    300
          return config




      def 						snake_case_     ( self						:    Optional[int]			):
          (
              (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , 
          )						:     Dict		      =    self.prepare_config_and_inputs()

          __lowercase						:     str		      =    True
          __lowercase						:     List[str]		      =    floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
          __lowercase						:     Dict		      =    ids_tensor([self.batch_size, self.seq_length]			,    vocab_size=2			)

          return (
              config,
              input_ids,
              token_type_ids,
              input_mask,
              sequence_labels,
              token_labels,
              choice_labels,
              encoder_hidden_states,
              encoder_attention_mask,
          )




      def 						snake_case_     ( self						:    Tuple			,    _snake_case						:    Optional[Any]			,    _snake_case						:    int			,    _snake_case						:    List[str]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Union[str, Any]			,    _snake_case						:    int			):
          __lowercase						:     Dict		      =    YosoModel(config=_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     Optional[Any]		      =    model(_snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			)
          __lowercase						:     Tuple		      =    model(_snake_case			,    token_type_ids=_snake_case			)
          __lowercase						:     List[str]		      =    model(_snake_case			)
          self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)			)




      def 						snake_case_     ( self						:    Any			,    _snake_case						:    Optional[Any]			,    _snake_case						:    int			,    _snake_case						:    List[str]			,    _snake_case						:    List[str]			,    _snake_case						:    List[str]			,    _snake_case						:    Optional[Any]			,    _snake_case						:    List[Any]			,    _snake_case						:    Tuple			,    _snake_case						:    Tuple			,    ):
          __lowercase						:     List[str]		      =    True
          __lowercase						:     Tuple		      =    YosoModel(_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     str		      =    model(
              _snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    encoder_hidden_states=_snake_case			,    encoder_attention_mask=_snake_case			,    )
          __lowercase						:     Optional[Any]		      =    model(
              _snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    encoder_hidden_states=_snake_case			,    )
          __lowercase						:     Any		      =    model(_snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			)
          self.parent.assertEqual(result.last_hidden_state.shape			,    (self.batch_size, self.seq_length, self.hidden_size)			)




      def 						snake_case_     ( self						:    str			,    _snake_case						:    Optional[Any]			,    _snake_case						:    List[Any]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Dict			,    _snake_case						:    List[str]			,    _snake_case						:    Any			):
          __lowercase						:     List[Any]		      =    YosoForMaskedLM(config=_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     int		      =    model(_snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    labels=_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.vocab_size)			)




      def 						snake_case_     ( self						:    Optional[Any]			,    _snake_case						:    str			,    _snake_case						:    List[str]			,    _snake_case						:    List[str]			,    _snake_case						:    int			,    _snake_case						:    Optional[int]			,    _snake_case						:    List[str]			,    _snake_case						:    Optional[Any]			):
          __lowercase						:     Any		      =    YosoForQuestionAnswering(config=_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     Dict		      =    model(
              _snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    start_positions=_snake_case			,    end_positions=_snake_case			,    )
          self.parent.assertEqual(result.start_logits.shape			,    (self.batch_size, self.seq_length)			)
          self.parent.assertEqual(result.end_logits.shape			,    (self.batch_size, self.seq_length)			)




      def 						snake_case_     ( self						:    Optional[int]			,    _snake_case						:    Dict			,    _snake_case						:    Dict			,    _snake_case						:    Union[str, Any]			,    _snake_case						:    List[Any]			,    _snake_case						:    List[str]			,    _snake_case						:    Optional[int]			,    _snake_case						:    Dict			):
          __lowercase						:     List[str]		      =    self.num_labels
          __lowercase						:     int		      =    YosoForSequenceClassification(_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     Optional[Any]		      =    model(_snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    labels=_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_labels)			)




      def 						snake_case_     ( self						:    Dict			,    _snake_case						:    Any			,    _snake_case						:    Optional[int]			,    _snake_case						:    Any			,    _snake_case						:    Dict			,    _snake_case						:    List[str]			,    _snake_case						:    Optional[Any]			,    _snake_case						:    Union[str, Any]			):
          __lowercase						:     Dict		      =    self.num_labels
          __lowercase						:     Any		      =    YosoForTokenClassification(config=_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     Any		      =    model(_snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    labels=_snake_case			)
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.seq_length, self.num_labels)			)




      def 						snake_case_     ( self						:    Dict			,    _snake_case						:    str			,    _snake_case						:    Optional[Any]			,    _snake_case						:    Any			,    _snake_case						:    List[Any]			,    _snake_case						:    Tuple			,    _snake_case						:    List[Any]			,    _snake_case						:    str			):
          __lowercase						:     Optional[Any]		      =    self.num_choices
          __lowercase						:     List[str]		      =    YosoForMultipleChoice(config=_snake_case			)
          model.to(_snake_case			)
          model.eval()
          __lowercase						:     List[str]		      =    input_ids.unsqueeze(1			).expand(-1			,    self.num_choices			,    -1			).contiguous()
          __lowercase						:     Optional[Any]		      =    token_type_ids.unsqueeze(1			).expand(-1			,    self.num_choices			,    -1			).contiguous()
          __lowercase						:     Union[str, Any]		      =    input_mask.unsqueeze(1			).expand(-1			,    self.num_choices			,    -1			).contiguous()
          __lowercase						:     int		      =    model(
              _snake_case			,    attention_mask=_snake_case			,    token_type_ids=_snake_case			,    labels=_snake_case			,    )
          self.parent.assertEqual(result.logits.shape			,    (self.batch_size, self.num_choices)			)




      def 						snake_case_     ( self						:    str			):
          __lowercase						:     List[Any]		      =    self.prepare_config_and_inputs()
          (
              (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , (
              __lowercase
          )      , 
          )						:     Tuple		      =    config_and_inputs
          __lowercase						:     List[str]		      =    {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
          return config, inputs_dict






@require_torch
class   __lowerCAmelCase		(		lowerCAmelCase_	,	lowerCAmelCase_	,	unittest.TestCase ):



      """simple docstring"""







      A__		:     Tuple       				=      (
          (
              YosoModel,
              YosoForMaskedLM,
              YosoForMultipleChoice,
              YosoForQuestionAnswering,
              YosoForSequenceClassification,
              YosoForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      A__		:     str       				=      False
      A__		:     Any       				=      False
      A__		:     Union[str, Any]       				=      False

      A__		:     Optional[int]       				=      ()
      A__		:     int       				=      (
          {
              '''feature-extraction''': YosoModel,
              '''fill-mask''': YosoForMaskedLM,
              '''question-answering''': YosoForQuestionAnswering,
              '''text-classification''': YosoForSequenceClassification,
              '''token-classification''': YosoForTokenClassification,
              '''zero-shot''': YosoForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )




      def 						snake_case_     ( self						:    Optional[Any]			):
          __lowercase						:     Union[str, Any]		      =    YosoModelTester(self			)
          __lowercase						:     Any		      =    ConfigTester(self			,    config_class=_snake_case			,    hidden_size=37			)




      def 						snake_case_     ( self						:    List[str]			):
          self.config_tester.run_common_tests()




      def 						snake_case_     ( self						:    Optional[Any]			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_snake_case			)




      def 						snake_case_     ( self						:    Optional[int]			):
          __lowercase						:     List[Any]		      =    self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
              __lowercase						:     int		      =    type
              self.model_tester.create_and_check_model(*_snake_case			)




      def 						snake_case_     ( self						:    List[Any]			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*_snake_case			)




      def 						snake_case_     ( self						:    int			):
          __lowercase						:     Union[str, Any]		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_multiple_choice(*_snake_case			)




      def 						snake_case_     ( self						:    str			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*_snake_case			)




      def 						snake_case_     ( self						:    Union[str, Any]			):
          __lowercase						:     Any		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*_snake_case			)




      def 						snake_case_     ( self						:    Optional[int]			):
          __lowercase						:     Union[str, Any]		      =    self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*_snake_case			)




      @slow
      def 						snake_case_     ( self						:    Tuple			):
          for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __lowercase						:     Union[str, Any]		      =    YosoModel.from_pretrained(_snake_case			)
              self.assertIsNotNone(_snake_case			)




      def 						snake_case_     ( self						:    Optional[int]			):
          return






@require_torch
class   __lowerCAmelCase		(		unittest.TestCase ):



      """simple docstring"""







      @slow
      def 						snake_case_     ( self						:    Any			):
          __lowercase						:     Optional[int]		      =    YosoModel.from_pretrained('''uw-madison/yoso-4096'''			)
          __lowercase						:     Optional[int]		      =    torch.tensor([[0, 1, 2, 3, 4, 5]]			)

          with torch.no_grad():
              __lowercase						:     List[str]		      =    model(_snake_case			)[0]

          __lowercase						:     List[Any]		      =    torch.Size((1, 6, 768)			)
          self.assertEqual(output.shape			,    _snake_case			)

          __lowercase						:     List[str]		      =    torch.tensor(
              [[[-0.06_11, 0.12_42, 0.08_40], [0.02_80, -0.00_48, 0.11_25], [0.01_06, 0.02_26, 0.07_51]]]			)

          self.assertTrue(torch.allclose(output[:, :3, :3]			,    _snake_case			,    atol=1E-4			)			)




      @slow
      def 						snake_case_     ( self						:    Optional[Any]			):
          __lowercase						:     List[Any]		      =    YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''			)
          __lowercase						:     List[Any]		      =    torch.tensor([[0, 1, 2, 3, 4, 5]]			)

          with torch.no_grad():
              __lowercase						:     int		      =    model(_snake_case			)[0]

          __lowercase						:     Optional[Any]		      =    5_0265

          __lowercase						:     Dict		      =    torch.Size((1, 6, vocab_size)			)
          self.assertEqual(output.shape			,    _snake_case			)

          __lowercase						:     List[Any]		      =    torch.tensor(
              [[[-2.13_13, -3.72_85, -2.24_07], [-2.70_47, -3.33_14, -2.64_08], [0.06_29, -2.51_66, -0.33_56]]]			)

          self.assertTrue(torch.allclose(output[:, :3, :3]			,    _snake_case			,    atol=1E-4			)			)




      @slow
      def 						snake_case_     ( self						:    Dict			):
          __lowercase						:     Optional[Any]		      =    YosoForMaskedLM.from_pretrained('''uw-madison/yoso-4096'''			)
          __lowercase						:     str		      =    torch.arange(4096			).unsqueeze(0			)

          with torch.no_grad():
              __lowercase						:     str		      =    model(_snake_case			)[0]

          __lowercase						:     Dict		      =    5_0265

          __lowercase						:     List[str]		      =    torch.Size((1, 4096, vocab_size)			)
          self.assertEqual(output.shape			,    _snake_case			)

          __lowercase						:     Optional[Any]		      =    torch.tensor(
              [[[-2.39_14, -4.37_42, -5.09_56], [-4.09_88, -4.23_84, -7.04_06], [-3.14_27, -3.71_92, -6.68_00]]]			)

          self.assertTrue(torch.allclose(output[:, :3, :3]			,    _snake_case			,    atol=1E-4			)			)




from __future__ import annotations

from collections import Counter


def       UpperCAmelCase_					(  __lowerCAmelCase		)	->  list[int]:
    __lowercase						:     Any		      =    [True] * n
    __lowercase						:     str		      =    False
    __lowercase						:     int		      =    False
    __lowercase						:     List[Any]		      =    True

    for i in range(3		,   int(n**0.5 + 1		)		,   2		):
        __lowercase						:     List[str]		      =    i * 2
        while index < n:
            __lowercase						:     List[Any]		      =    False
            __lowercase						:     Dict		      =    index + i

    __lowercase						:     Tuple		      =    [2]

    for i in range(3		,   __lowerCAmelCase		,   2		):
        if is_prime[i]:
            primes.append(__lowerCAmelCase		)

    return primes


def       UpperCAmelCase_					(  __lowerCAmelCase		)	->  list[list[int]]:
    __lowercase						:     Optional[int]		      =    str(__lowerCAmelCase		)
    __lowercase						:     List[str]		      =    []
    __lowercase						:     int		      =    ['''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''']

    for duplicate in Counter(__lowerCAmelCase		) - Counter(set(__lowerCAmelCase		)		):
        __lowercase						:     Union[str, Any]		      =    [int(number_str.replace(__lowerCAmelCase		,   __lowerCAmelCase		)		) for digit in digits]
        replacements.append(__lowerCAmelCase		)

    return replacements


def       UpperCAmelCase_					(  __lowerCAmelCase = 8		)	->  int:
    __lowercase						:     Tuple		      =    set()

    # Filter primes with less than 3 replaceable digits
    __lowercase						:     int		      =    {
        x for x in set(prime_sieve(1_000_000		)		) if len(str(__lowerCAmelCase		)		) - len(set(str(__lowerCAmelCase		)		)		) >= 3
    }

    for prime in primes:
        if prime in numbers_checked:
            continue

        __lowercase						:     str		      =    digit_replacements(__lowerCAmelCase		)

        for family in replacements:
            numbers_checked.update(__lowerCAmelCase		)
            __lowercase						:     List[Any]		      =    primes.intersection(__lowerCAmelCase		)

            if len(__lowerCAmelCase		) != family_length:
                continue

            return min(__lowerCAmelCase		)

    return -1


if __name__ == "__main__":
 print(solution())


