
"""simple docstring"""



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 					lowerCAmelCase			(				unittest.TestCase					):
     """simple docstring"""







     def    _lowerCAmelCase		(      self ,		UpperCamelCase__							)					->  Optional[int]:







            '''simple docstring'''





            lowerCamelCase_       							=		3
            lowerCamelCase_       							=		250

            lowerCamelCase_       							=		ids_tensor((batch_size, length) ,		lowercase_							)
            lowerCamelCase_       							=		torch.ones((batch_size, length) ,		device=lowercase_ ,		dtype=torch.float							) / length
            return input_ids, scores




     def    _lowerCAmelCase		(      self							)					->  Optional[Any]:







            '''simple docstring'''





            lowerCamelCase_       							=		self._get_tensors(5							)

            lowerCamelCase_       							=		StoppingCriteriaList(
                [
                    MaxLengthCriteria(max_length=10							),
                    MaxTimeCriteria(max_time=0.1							),
                ]							)

            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(9							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(10							)
            self.assertTrue(criteria(lowercase_ ,		lowercase_							)							)




     def    _lowerCAmelCase		(      self							)					->  Tuple:







            '''simple docstring'''





            lowerCamelCase_       							=		MaxLengthCriteria(max_length=10							)

            lowerCamelCase_       							=		self._get_tensors(5							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(9							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(10							)
            self.assertTrue(criteria(lowercase_ ,		lowercase_							)							)




     def    _lowerCAmelCase		(      self							)					->  Tuple:







            '''simple docstring'''





            lowerCamelCase_       							=		MaxNewTokensCriteria(start_length=5 ,		max_new_tokens=5							)

            lowerCamelCase_       							=		self._get_tensors(5							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(9							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		self._get_tensors(10							)
            self.assertTrue(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		StoppingCriteriaList([criteria]							)
            self.assertEqual(criteria_list.max_length ,		10							)




     def    _lowerCAmelCase		(      self							)					->  str:







            '''simple docstring'''





            lowerCamelCase_       							=		self._get_tensors(5							)

            lowerCamelCase_       							=		MaxTimeCriteria(max_time=0.1							)
            self.assertFalse(criteria(lowercase_ ,		lowercase_							)							)

            lowerCamelCase_       							=		MaxTimeCriteria(max_time=0.1 ,		initial_timestamp=time.time() - 0.2							)
            self.assertTrue(criteria(lowercase_ ,		lowercase_							)							)




     def    _lowerCAmelCase		(      self							)					->  Optional[int]:







            '''simple docstring'''





            validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10							)]							) ,		10							)

            with self.assertWarns(lowercase_							):
                   validate_stopping_criteria(StoppingCriteriaList([MaxLengthCriteria(10							)]							) ,		11							)

            lowerCamelCase_       							=		validate_stopping_criteria(StoppingCriteriaList() ,		11							)

            self.assertEqual(len(lowercase_							) ,		1							)



def       a_ (       SCREAMING_SNAKE_CASE__   : bytes	):
       '''simple docstring'''





       return "".join([hex(SCREAMING_SNAKE_CASE__	)[2:].zfill(2	).upper() for byte in list(SCREAMING_SNAKE_CASE__	)]	)




def       a_ (       SCREAMING_SNAKE_CASE__   : str	):
       '''simple docstring'''





       if (len(SCREAMING_SNAKE_CASE__	) % 2) != 0:
              raise ValueError(
                  'Base16 encoded data is invalid:\nData does not have an even number of hex digits.'	)
       # Check the character set - the standard base16 alphabet
       # is uppercase according to RFC3548 section 6
       if not set(SCREAMING_SNAKE_CASE__	) <= set('0123456789ABCDEF'	):
              raise ValueError(
                  'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.'	)
       # For every two hexadecimal digits (= a byte), turn it into an integer.
       # Then, string the result together into bytes, and return it.
       return bytes(int(data[i] + data[i + 1]						,     16	) for i in range(0						,     len(SCREAMING_SNAKE_CASE__	)						,     2	)	)


if __name__ == "__main__":
 import doctest

 doctest.testmod()


