






import time
import unittest

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

from ..test_modeling_common import ids_tensor


if is_torch_available():
							import torch

							from transformers.generation import (
							    MaxLengthCriteria,
							    MaxNewTokensCriteria,
							    MaxTimeCriteria,
							    StoppingCriteriaList,
							    validate_stopping_criteria,
							)






@require_torch
class 			_UpperCamelCase(       unittest.TestCase		):

		def    __lowerCAmelCase  (					self		:    Tuple    ,       SCREAMING_SNAKE_CASE__		:    Dict    ):



					'''simple docstring'''



					__a						:  Any									=     3
					__a						:  int									=     2_5_0

					__a						:  Union[str, Any]									=     ids_tensor((batch_size, length)    ,       SCREAMING_SNAKE_CASE__    )
					__a						:  Optional[int]									=     torch.ones((batch_size, length)    ,       device=SCREAMING_SNAKE_CASE__    ,       dtype=torch.float    ) / length
					return input_ids, scores

		def    __lowerCAmelCase  (					self		:    Optional[int]    ):



					'''simple docstring'''



					__a						,					__a						:  List[str]									=     self._get_tensors(5    )

					__a						:  List[str]									=     StoppingCriteriaList(
					    [
					        MaxLengthCriteria(max_length=1_0    ),
					        MaxTimeCriteria(max_time=0.1    ),
					    ]    )

					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  str									=     self._get_tensors(9    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  List[Any]									=     self._get_tensors(1_0    )
					self.assertTrue(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

		def    __lowerCAmelCase  (					self		:    List[Any]    ):



					'''simple docstring'''



					__a						:  Any									=     MaxLengthCriteria(max_length=1_0    )

					__a						,					__a						:  Optional[Any]									=     self._get_tensors(5    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  Any									=     self._get_tensors(9    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  List[str]									=     self._get_tensors(1_0    )
					self.assertTrue(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

		def    __lowerCAmelCase  (					self		:    int    ):



					'''simple docstring'''



					__a						:  Dict									=     MaxNewTokensCriteria(start_length=5    ,       max_new_tokens=5    )

					__a						,					__a						:  List[Any]									=     self._get_tensors(5    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  Union[str, Any]									=     self._get_tensors(9    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						,					__a						:  Any									=     self._get_tensors(1_0    )
					self.assertTrue(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						:  Union[str, Any]									=     StoppingCriteriaList([criteria]    )
					self.assertEqual(criteria_list.max_length    ,       1_0    )

		def    __lowerCAmelCase  (					self		:    Union[str, Any]    ):



					'''simple docstring'''



					__a						,					__a						:  Tuple									=     self._get_tensors(5    )

					__a						:  Dict									=     MaxTimeCriteria(max_time=0.1    )
					self.assertFalse(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )

					__a						:  List[Any]									=     MaxTimeCriteria(max_time=0.1    ,       initial_timestamp=time.time() - 0.2    )
					self.assertTrue(criteria(SCREAMING_SNAKE_CASE__    ,       SCREAMING_SNAKE_CASE__    )    )



		def    __lowerCAmelCase  (					self		:    Tuple    ):



					'''simple docstring'''



					validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(1_0    )]    )    ,       1_0    )

					with self.assertWarns(SCREAMING_SNAKE_CASE__    ):
								validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(1_0    )]    )    ,       1_1    )

					__a						:  List[str]									=     validate_stopping_criteria(StoppingCriteriaList()    ,       1_1    )

					self.assertEqual(len(SCREAMING_SNAKE_CASE__    )    ,       1    )






'''simple docstring'''

def __UpperCamelCase( _A       :					float	,      _A       :					list[float]     ):
   '''simple docstring'''
   if discount_rate < 0:
      raise ValueError('''Discount rate cannot be negative'''     )
   if not cash_flows:
      raise ValueError('''Cash flows list cannot be empty'''     )
   UpperCAmelCase__				:					str 					=	sum(
       cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(_A     )     )
   return round(_A	,      ndigits=2     )


if __name__ == "__main__":
  import doctest

  doctest.testmod()




