


import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

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


if is_torch_available():
 import torch

 from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
 from transformers.models.esm.modeling_esm import (
     ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
     EsmEmbeddings,
     create_position_ids_from_input_ids,
 )





class   __a			:
 def __init__(   self				, lowerCAmelCase__				, lowerCAmelCase__=13				, lowerCAmelCase__=7				, lowerCAmelCase__=False				, lowerCAmelCase__=True				, lowerCAmelCase__=False				, lowerCAmelCase__=True				, lowerCAmelCase__=33				, lowerCAmelCase__=32				, lowerCAmelCase__=5				, lowerCAmelCase__=4				, lowerCAmelCase__=37				, lowerCAmelCase__="gelu"				, lowerCAmelCase__=0.1				, lowerCAmelCase__=0.1				, lowerCAmelCase__=512				, lowerCAmelCase__=16				, lowerCAmelCase__=2				, lowerCAmelCase__=0.0_2				, lowerCAmelCase__=3				, lowerCAmelCase__=4				, lowerCAmelCase__=None				, )					->				Optional[int]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   parent
        lowercase__:   Optional[Any]  			=   batch_size
        lowercase__:   Optional[int]  			=   seq_length
        lowercase__:   Dict  			=   is_training
        lowercase__:   List[str]  			=   use_input_mask
        lowercase__:   Optional[Any]  			=   use_token_type_ids
        lowercase__:   Optional[int]  			=   use_labels
        lowercase__:   Dict  			=   vocab_size
        lowercase__:   Tuple  			=   hidden_size
        lowercase__:   Optional[int]  			=   num_hidden_layers
        lowercase__:   str  			=   num_attention_heads
        lowercase__:   Tuple  			=   intermediate_size
        lowercase__:   Union[str, Any]  			=   hidden_act
        lowercase__:   Optional[Any]  			=   hidden_dropout_prob
        lowercase__:   Dict  			=   attention_probs_dropout_prob
        lowercase__:   List[str]  			=   max_position_embeddings
        lowercase__:   int  			=   type_vocab_size
        lowercase__:   Dict  			=   type_sequence_label_size
        lowercase__:   int  			=   initializer_range
        lowercase__:   List[str]  			=   num_labels
        lowercase__:   Dict  			=   num_choices
        lowercase__:   Dict  			=   scope
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   int  			=   ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size )

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

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

        lowercase__:   str  			=   self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        return EsmConfig(
            vocab_size=self.vocab_size				, hidden_size=self.hidden_size				, pad_token_id=1				, 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				, initializer_range=self.initializer_range				, )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				int:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   EsmModel(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   List[str]  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__ )
        lowercase__:   Union[str, Any]  			=   model(lowerCAmelCase__ )
        lowercase__:   Dict  			=   model(lowerCAmelCase__ )

        self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size) )
        self.parent.assertEqual(result.pooler_output.shape				, (self.batch_size, self.hidden_size) )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				List[str]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   EsmForMaskedLM(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Any  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__				, labels=lowerCAmelCase__ )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.vocab_size) )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   int  			=   self.num_labels
        lowercase__:   Union[str, Any]  			=   EsmForTokenClassification(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Tuple  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__				, labels=lowerCAmelCase__ )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.num_labels) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   int  			=   self.prepare_config_and_inputs()
        (
            (
            lowercase__
        )			,						(
            lowercase__
        )			,						(
            lowercase__
        )			,						(
            lowercase__
        )			,						(
            lowercase__
        )			,						(
            lowercase__
        )			,						
        ):   str  			=   config_and_inputs
        lowercase__:   List[str]  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict





@require_torch
class   __a			(    __UpperCamelCase       ,       __UpperCamelCase       ,       unittest.TestCase      ):
 __lowercase :							Optional[Any]        = False

 __lowercase :							str        = (
     (
         EsmForMaskedLM,
         EsmModel,
         EsmForSequenceClassification,
         EsmForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowercase :							Dict        = ()
 __lowercase :							Optional[Any]        = (
     {
         'feature-extraction': EsmModel,
         'fill-mask': EsmForMaskedLM,
         'text-classification': EsmForSequenceClassification,
         'token-classification': EsmForTokenClassification,
         'zero-shot': EsmForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowercase :							int        = True
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				str:




        '''simple docstring'''
        lowercase__:   Dict  			=   EsmModelTester(self )
        lowercase__:   List[Any]  			=   ConfigTester(self				, config_class=lowerCAmelCase__				, hidden_size=37 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				str:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   int  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   List[str]  			=   self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               lowercase__:   Dict  			=   type
               self.model_tester.create_and_check_model(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__ )
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:   int  			=   EsmModel.from_pretrained(lowerCAmelCase__ )
               self.assertIsNotNone(lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   str  			=   self.model_tester.prepare_config_and_inputs()[0]
        lowercase__:   str  			=   EsmEmbeddings(config=lowerCAmelCase__ )

        lowercase__:   List[str]  			=   torch.as_tensor([[12, 31, 13, model.padding_idx]] )
        lowercase__:   Any  			=   torch.as_tensor(
            [
                [
                    0 + model.padding_idx + 1,
                    1 + model.padding_idx + 1,
                    2 + model.padding_idx + 1,
                    model.padding_idx,
                ]
            ] )
        lowercase__:   int  			=   create_position_ids_from_input_ids(lowerCAmelCase__				, model.padding_idx )
        self.assertEqual(position_ids.shape				, expected_positions.shape )
        self.assertTrue(torch.all(torch.eq(lowerCAmelCase__				, lowerCAmelCase__ ) ) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   str  			=   self.model_tester.prepare_config_and_inputs()[0]
        lowercase__:   str  			=   EsmEmbeddings(config=lowerCAmelCase__ )

        lowercase__:   str  			=   torch.empty(2				, 4				, 30 )
        lowercase__:   List[str]  			=   [
            0 + embeddings.padding_idx + 1,
            1 + embeddings.padding_idx + 1,
            2 + embeddings.padding_idx + 1,
            3 + embeddings.padding_idx + 1,
        ]
        lowercase__:   Dict  			=   torch.as_tensor([expected_single_positions, expected_single_positions] )
        lowercase__:   List[str]  			=   embeddings.create_position_ids_from_inputs_embeds(lowerCAmelCase__ )
        self.assertEqual(position_ids.shape				, expected_positions.shape )
        self.assertTrue(torch.all(torch.eq(lowerCAmelCase__				, lowerCAmelCase__ ) ) )
 @unittest.skip('Esm does not support embedding resizing' )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        pass
 @unittest.skip('Esm does not support embedding resizing' )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        pass
 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.' )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        pass





@require_torch
class   __a			(    __UpperCamelCase      ):
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        with torch.no_grad():
               lowercase__:   List[str]  			=   EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D' )
               model.eval()
               lowercase__:   Optional[int]  			=   torch.tensor([[0, 1, 2, 3, 4, 5]] )
               lowercase__:   Dict  			=   model(lowerCAmelCase__ )[0]

               lowercase__:   Optional[int]  			=   33

               lowercase__:   Any  			=   torch.Size((1, 6, vocab_size) )
               self.assertEqual(output.shape				, lowerCAmelCase__ )

               lowercase__:   Optional[int]  			=   torch.tensor(
                   [[[8.9_2_1_5, -1_0.5_8_9_8, -6.4_6_7_1], [-6.3_9_6_7, -1_3.9_1_1_4, -1.1_2_1_2], [-7.7_8_1_2, -1_3.9_5_1_6, -3.7_4_0_6]]] )
               self.assertTrue(torch.allclose(output[:, :3, :3]				, lowerCAmelCase__				, atol=1E-4 ) )
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        with torch.no_grad():
               lowercase__:   str  			=   EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D' )
               model.eval()

               lowercase__:   Any  			=   torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]] )
               lowercase__:   str  			=   model(lowerCAmelCase__ )[0]
               # compare the actual values for a slice.
               lowercase__:   Optional[int]  			=   torch.tensor(
                   [[[0.1_4_4_4, 0.5_4_1_3, 0.3_2_4_8], [0.3_0_3_4, 0.0_0_5_3, 0.3_1_0_8], [0.3_2_2_8, -0.2_4_9_9, 0.3_4_1_5]]] )
               self.assertTrue(torch.allclose(output[:, :3, :3]				, lowerCAmelCase__				, atol=1E-4 ) )






import unittest

from knapsack import knapsack as k





class   __a			(    unittest.TestCase      ):
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Tuple:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   0
        lowercase__:   List[Any]  			=   [0]
        lowercase__:   str  			=   [0]
        lowercase__:   Tuple  			=   len(lowerCAmelCase__ )
        self.assertEqual(k.knapsack(lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )				, 0 )

        lowercase__:   Optional[Any]  			=   [60]
        lowercase__:   Dict  			=   [10]
        lowercase__:   str  			=   len(lowerCAmelCase__ )
        self.assertEqual(k.knapsack(lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )				, 0 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Union[str, Any]  			=   3
        lowercase__:   List[str]  			=   [1, 2, 3]
        lowercase__:   Union[str, Any]  			=   [3, 2, 1]
        lowercase__:   Union[str, Any]  			=   len(lowerCAmelCase__ )
        self.assertEqual(k.knapsack(lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )				, 5 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   50
        lowercase__:   str  			=   [60, 100, 120]
        lowercase__:   Any  			=   [10, 20, 30]
        lowercase__:   List[Any]  			=   len(lowerCAmelCase__ )
        self.assertEqual(k.knapsack(lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )				, 220 )


if __name__ == "__main__":
 unittest.main()


