



"""simple docstring"""






import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch

     from transformers.generation import DisjunctiveConstraint



@require_torch
class     UpperCAmelCase    (	unittest.TestCase  ):



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     # For consistency across different places the DisjunctiveConstraint is called,
     # dc.token_ids is a list of integers. It is also initialized only by integers.

     _snake_case   	=      [[1, 2, 4], [1, 2, 3, 4]]
     _snake_case   	=      DisjunctiveConstraint(__lowerCamelCase				)
     self.assertTrue(isinstance(dc.token_ids							,       __lowerCamelCase				)				)

     with self.assertRaises(__lowerCamelCase				):
       DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]				)				)

     with self.assertRaises(__lowerCamelCase				):
       DisjunctiveConstraint([torch.LongTensor([1, 2, 4]				), torch.LongTensor([1, 2, 3, 4, 5]				)]				)



   def    __UpperCAmelCase    (       self    :	Tuple				):



     """simple docstring"""
     # We can't have constraints that are complete subsets of another. This leads to a preverse
     # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
     # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
     # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
     # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
     _snake_case   	=      [[1, 2], [1, 2, 3, 4]]

     with self.assertRaises(__lowerCamelCase				):
       DisjunctiveConstraint(__lowerCamelCase				)  # fails here



   def    __UpperCAmelCase    (       self    :	Any				):



     """simple docstring"""
     _snake_case   	=      [[1, 2, 3], [1, 2, 4]]

     _snake_case   	=      DisjunctiveConstraint(__lowerCamelCase				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(1				)
     _snake_case   	=      stepped is True and completed is False and reset is False
     self.assertTrue(__lowerCamelCase				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.current_seq == [1]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(2				)
     _snake_case   	=      stepped is True and completed is False and reset is False
     self.assertTrue(__lowerCamelCase				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.current_seq == [1, 2]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(3				)
     _snake_case   	=      stepped is True and completed is True and reset is False
     self.assertTrue(__lowerCamelCase				)
     self.assertTrue(dc.completed				)  # Completed!
     self.assertTrue(dc.current_seq == [1, 2, 3]				)





   def    __UpperCAmelCase    (       self    :	List[str]				):



     """simple docstring"""
     _snake_case   	=      [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

     _snake_case   	=      DisjunctiveConstraint(__lowerCamelCase				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(1				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.current_seq == [1]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(2				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.current_seq == [1, 2]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(4				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.current_seq == [1, 2, 4]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(5				)
     self.assertTrue(dc.completed				)  # Completed!
     self.assertTrue(dc.current_seq == [1, 2, 4, 5]				)

     dc.reset()

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(1				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.remaining() == 3				)
     self.assertTrue(dc.current_seq == [1]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(2				)
     self.assertTrue(not dc.completed				)
     self.assertTrue(dc.remaining() == 2				)
     self.assertTrue(dc.current_seq == [1, 2]				)

     _snake_case	,     _snake_case	,     _snake_case   	=      dc.update(5				)
     self.assertTrue(dc.completed				)  # Completed!
     self.assertTrue(dc.remaining() == 0				)
     self.assertTrue(dc.current_seq == [1, 2, 5]				)







"""simple docstring"""


from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream







class 					lowerCAmelCase_					(   _lowercase     ):



						'''simple docstring'''


						def __init__(			self  :   Any	,A_  :   Callable	,A_  :   Optional[Features] = None	,A_  :   str = None	,A_  :   bool = False	,A_  :   bool = False	,A_  :   Optional[dict] = None	,A_  :   Optional[int] = None	,**A_  :   int	,)  -> str:
							super().__init__(
							    features=A_	,cache_dir=A_	,keep_in_memory=A_	,streaming=A_	,num_proc=A_	,**A_	,)
							A     = Generator(
							    cache_dir=A_	,features=A_	,generator=A_	,gen_kwargs=A_	,**A_	,)




						def     _SCREAMING_SNAKE_CASE   (			self  :   Optional[int]   )  -> Optional[int]:
							# Build iterable dataset
							if self.streaming:
								A     = self.builder.as_streaming_dataset(split='train'   )
							# Build regular (map-style) dataset
							else:
								A     = None
								A     = None
								A     = None
								A     = None

								self.builder.download_and_prepare(
								    download_config=A_	,download_mode=A_	,verification_mode=A_	,base_path=A_	,num_proc=self.num_proc	,)
								A     = self.builder.as_dataset(
								    split='train'	,verification_mode=A_	,in_memory=self.keep_in_memory   )
							return dataset