
"""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 		a_    (			unittest.TestCase						):







    def   _snake_case       (      self      :							Union[str, Any]  )		->Any:






        '''simple docstring'''






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

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

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







    def   _snake_case       (      self      :							Optional[int]  )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2], [1, 2, 3, 4]]

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







    def   _snake_case       (      self      :							Optional[int]  )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2, 3], [1, 2, 4]]

        _UpperCAmelCase        = DisjunctiveConstraint(__UpperCamelCase  )

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

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

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







    def   _snake_case       (      self      :							List[Any]  )		->Any:






        '''simple docstring'''






        _UpperCAmelCase        = [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

        _UpperCAmelCase        = DisjunctiveConstraint(__UpperCamelCase  )

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

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

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

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

        dc.reset()

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

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

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

"""simple docstring"""



import json
import os
import tempfile

import transformers

import datasets
from utils import generate_example_dataset, get_duration


a       :						int       		=		5_0_0_0_0_0

a		,			a       :						Union[str, Any]       		=		os.path.split(__file__)
a       :						Dict       		=		os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))


@get_duration
def 			_UpperCamelCase						(    _A       ,   **_A   )     ->	Any:





    """simple docstring"""

    _UpperCAmelCase        = dataset.map(**_A   )


@get_duration
def 			_UpperCamelCase						(    _A       ,   **_A   )     ->	Dict:





    """simple docstring"""

    _UpperCAmelCase        = dataset.filter(**_A   )


def 			_UpperCamelCase						(    )     ->	Tuple:





    """simple docstring"""

    _UpperCAmelCase        = {"""num examples""": SPEED_TEST_N_EXAMPLES}
    with tempfile.TemporaryDirectory() as tmp_dir:
        _UpperCAmelCase        = datasets.Features({"""text""": datasets.Value("""string"""   ), """numbers""": datasets.Value("""float32"""   )}   )
        _UpperCAmelCase        = generate_example_dataset(
            os.path.join(_A       ,   """dataset.arrow"""   )       ,   _A       ,   num_examples=_A   )

        _UpperCAmelCase        = transformers.AutoTokenizer.from_pretrained("""bert-base-cased"""       ,   use_fast=_A   )

        def tokenize(_A   ):
            return tokenizer(examples["""text"""]   )

        _UpperCAmelCase        = map(_A   )

        _UpperCAmelCase        = map(_A       ,   batched=_A   )

        _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""numpy"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""pandas"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""torch"""       ,   columns="""numbers"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        with dataset.formatted_as(type="""tensorflow"""       ,   columns="""numbers"""   ):
            _UpperCAmelCase        = map(_A       ,   function=lambda _A   : None       ,   batched=_A   )

        _UpperCAmelCase        = map(_A       ,   function=_A       ,   batched=_A   )

        _UpperCAmelCase        = filter(_A   )

        # Activate later when tokenizer support batched inputs
        # with dataset.formatted_as(type='numpy'):
        #     times[func.__name__ + " fast-tokenizer batched numpy"] = func(dataset, function=tokenize, batched=True)

    with open(_A       ,   """wb"""   ) as f:
        f.write(json.dumps(_A   ).encode("""utf-8"""   )   )


if __name__ == "__main__":  # useful to run the profiler
 benchmark_map_filter()