






'''simple docstring'''





import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, 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 (
     SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
     SqueezeBertForMaskedLM,
     SqueezeBertForMultipleChoice,
     SqueezeBertForQuestionAnswering,
     SqueezeBertForSequenceClassification,
     SqueezeBertForTokenClassification,
     SqueezeBertModel,
 )







class 	_UpperCAmelCase  (  __lowerCAmelCase		):




      """simple docstring"""



      def __init__(   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase=13    ,	_lowerCAmelCase=7    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=False    ,	_lowerCAmelCase=True    ,	_lowerCAmelCase=99    ,	_lowerCAmelCase=32    ,	_lowerCAmelCase=5    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=64    ,	_lowerCAmelCase="gelu"    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=0.1    ,	_lowerCAmelCase=512    ,	_lowerCAmelCase=16    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=0.02    ,	_lowerCAmelCase=3    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=None    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=2    ,	_lowerCAmelCase=4    ,	_lowerCAmelCase=1    ,	):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				parent
          lowerCAmelCase__   :Tuple      							=				batch_size
          lowerCAmelCase__   :Optional[int]      							=				seq_length
          lowerCAmelCase__   :List[str]      							=				is_training
          lowerCAmelCase__   :Union[str, Any]      							=				use_input_mask
          lowerCAmelCase__   :Tuple      							=				use_token_type_ids
          lowerCAmelCase__   :Optional[Any]      							=				use_labels
          lowerCAmelCase__   :str      							=				vocab_size
          lowerCAmelCase__   :Optional[Any]      							=				hidden_size
          lowerCAmelCase__   :Union[str, Any]      							=				num_hidden_layers
          lowerCAmelCase__   :List[str]      							=				num_attention_heads
          lowerCAmelCase__   :Dict      							=				intermediate_size
          lowerCAmelCase__   :Optional[int]      							=				hidden_act
          lowerCAmelCase__   :Dict      							=				hidden_dropout_prob
          lowerCAmelCase__   :List[Any]      							=				attention_probs_dropout_prob
          lowerCAmelCase__   :int      							=				max_position_embeddings
          lowerCAmelCase__   :Union[str, Any]      							=				type_vocab_size
          lowerCAmelCase__   :Optional[Any]      							=				type_sequence_label_size
          lowerCAmelCase__   :Dict      							=				initializer_range
          lowerCAmelCase__   :Optional[Any]      							=				num_labels
          lowerCAmelCase__   :List[str]      							=				num_choices
          lowerCAmelCase__   :Optional[Any]      							=				scope
          lowerCAmelCase__   :int      							=				q_groups
          lowerCAmelCase__   :Union[str, Any]      							=				k_groups
          lowerCAmelCase__   :List[str]      							=				v_groups
          lowerCAmelCase__   :Union[str, Any]      							=				post_attention_groups
          lowerCAmelCase__   :List[Any]      							=				intermediate_groups
          lowerCAmelCase__   :int      							=				output_groups
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size		)

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

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

          lowerCAmelCase__   :Tuple      							=				self.get_config()

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 			snake_case_      (   self		):



          '''simple docstring'''
          return SqueezeBertConfig(
              embedding_size=self.hidden_size    ,	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    ,	attention_probs_dropout_prob=self.hidden_dropout_prob    ,	attention_dropout=self.attention_probs_dropout_prob    ,	max_position_embeddings=self.max_position_embeddings    ,	initializer_range=self.initializer_range    ,	q_groups=self.q_groups    ,	k_groups=self.k_groups    ,	v_groups=self.v_groups    ,	post_attention_groups=self.post_attention_groups    ,	intermediate_groups=self.intermediate_groups    ,	output_groups=self.output_groups    ,	)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				SqueezeBertModel(config=_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()
          lowerCAmelCase__   :Tuple      							=				model(_UpperCamelCase    ,	_UpperCamelCase		)
          lowerCAmelCase__   :Dict      							=				model(_UpperCamelCase		)
          self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				SqueezeBertForMaskedLM(config=_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()
          lowerCAmelCase__   :List[str]      							=				model(_UpperCamelCase    ,	attention_mask=_UpperCamelCase    ,	labels=_UpperCamelCase		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				SqueezeBertForQuestionAnswering(config=_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[int]      							=				model(
              _UpperCamelCase    ,	attention_mask=_UpperCamelCase    ,	start_positions=_UpperCamelCase    ,	end_positions=_UpperCamelCase		)
          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    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				self.num_labels
          lowerCAmelCase__   :Optional[int]      							=				SqueezeBertForSequenceClassification(_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[int]      							=				model(_UpperCamelCase    ,	attention_mask=_UpperCamelCase    ,	labels=_UpperCamelCase		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				self.num_labels
          lowerCAmelCase__   :str      							=				SqueezeBertForTokenClassification(config=_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()

          lowerCAmelCase__   :str      							=				model(_UpperCamelCase    ,	attention_mask=_UpperCamelCase    ,	labels=_UpperCamelCase		)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.num_labels)		)
      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.num_choices
          lowerCAmelCase__   :Tuple      							=				SqueezeBertForMultipleChoice(config=_UpperCamelCase		)
          model.to(_UpperCamelCase		)
          model.eval()
          lowerCAmelCase__   :Optional[int]      							=				input_ids.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Dict      							=				input_mask.unsqueeze(1		).expand(-1    ,	self.num_choices    ,	-1		).contiguous()
          lowerCAmelCase__   :Optional[int]      							=				model(
              _UpperCamelCase    ,	attention_mask=_UpperCamelCase    ,	labels=_UpperCamelCase    ,	)
          self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_choices)		)



      def 			snake_case_      (   self		):



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







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




      """simple docstring"""



      A          =    (
          (
              SqueezeBertModel,
              SqueezeBertForMaskedLM,
              SqueezeBertForMultipleChoice,
              SqueezeBertForQuestionAnswering,
              SqueezeBertForSequenceClassification,
              SqueezeBertForTokenClassification,
          )
          if is_torch_available()
          else None
      )
      A          =    (
          {
              '''feature-extraction''': SqueezeBertModel,
              '''fill-mask''': SqueezeBertForMaskedLM,
              '''question-answering''': SqueezeBertForQuestionAnswering,
              '''text-classification''': SqueezeBertForSequenceClassification,
              '''token-classification''': SqueezeBertForTokenClassification,
              '''zero-shot''': SqueezeBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      A          =    False
      A          =    True
      A          =    False
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				SqueezeBertModelTester(self		)
          lowerCAmelCase__   :List[str]      							=				ConfigTester(self    ,	config_class=_UpperCamelCase    ,	dim=37		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          self.config_tester.run_common_tests()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_model(*_UpperCamelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCamelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCamelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCamelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCamelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCamelCase		)



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''


          for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              lowerCAmelCase__   :Tuple      							=				SqueezeBertModel.from_pretrained(_UpperCamelCase		)
              self.assertIsNotNone(_UpperCamelCase		)







@require_sentencepiece
@require_tokenizers
@require_torch
class 	_UpperCAmelCase  (  unittest.TestCase		):




      """simple docstring"""



      @slow
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :int      							=				SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"		)

          lowerCAmelCase__   :Any      							=				torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]		)
          lowerCAmelCase__   :int      							=				model(_UpperCamelCase		)[0]
          lowerCAmelCase__   :List[str]      							=				torch.Size((1, 3)		)
          self.assertEqual(output.shape    ,	_UpperCamelCase		)
          lowerCAmelCase__   :Tuple      							=				torch.tensor([[0.6401, -0.0349, -0.6041]]		)
          self.assertTrue(torch.allclose(_UpperCamelCase    ,	_UpperCamelCase    ,	atol=1e-4		)		)











def 				snake_case__							(							UpperCAmelCase							:  float    ,			UpperCAmelCase							:  list[float]       ):
    if discount_rate < 0:
        raise ValueError("Discount rate cannot be negative"       )
    if not cash_flows:
        raise ValueError("Cash flows list cannot be empty"       )
    lowerCAmelCase__   :List[Any]      							=				sum(
        cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(UpperCAmelCase       )       )
    return round(UpperCAmelCase    ,			ndigits=2       )


if __name__ == "__main__":
 import doctest

 doctest.testmod()



