


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
   from transformers import TvltImageProcessor

if is_speech_available():
   from transformers import TvltFeatureExtractor

from transformers import TvltProcessor
@require_torch
class 		__magic_name__						(unittest.TestCase				):





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       snake_case__						= '''ZinengTang/tvlt-base'''
       snake_case__						= tempfile.mkdtemp()

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict     ,		**_a:List[Any]   ):
       return TvltImageProcessor.from_pretrained(self.checkpoint     ,		**_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]     ,		**_a:Tuple   ):
       return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,		**_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       shutil.rmtree(self.tmpdirname   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       snake_case__						= self.get_image_processor()
       snake_case__						= self.get_feature_extractor()

       snake_case__						= TvltProcessor(image_processor=_a     ,		feature_extractor=_a   )
       processor.save_pretrained(self.tmpdirname   )
       snake_case__						= TvltProcessor.from_pretrained(self.tmpdirname   )

       self.assertIsInstance(processor.feature_extractor     ,		_a   )
       self.assertIsInstance(processor.image_processor     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       snake_case__						= self.get_image_processor()
       snake_case__						= self.get_feature_extractor()

       snake_case__						= TvltProcessor(image_processor=_a     ,		feature_extractor=_a   )

       snake_case__						= np.ones([1_20_00]   )

       snake_case__						= feature_extractor(_a     ,		return_tensors='''np'''   )
       snake_case__						= processor(audio=_a     ,		return_tensors='''np'''   )

       for key in audio_dict.keys():
         self.assertAlmostEqual(audio_dict[key].sum()     ,		input_processor[key].sum()     ,		delta=1e-2   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= self.get_image_processor()
       snake_case__						= self.get_feature_extractor()

       snake_case__						= TvltProcessor(image_processor=_a     ,		feature_extractor=_a   )

       snake_case__						= np.ones([3, 2_24, 2_24]   )

       snake_case__						= image_processor(_a     ,		return_tensors='''np'''   )
       snake_case__						= processor(images=_a     ,		return_tensors='''np'''   )

       for key in image_dict.keys():
         self.assertAlmostEqual(image_dict[key].sum()     ,		input_processor[key].sum()     ,		delta=1e-2   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Any   ):
       snake_case__						= self.get_image_processor()
       snake_case__						= self.get_feature_extractor()

       snake_case__						= TvltProcessor(image_processor=_a     ,		feature_extractor=_a   )

       snake_case__						= np.ones([1_20_00]   )
       snake_case__						= np.ones([3, 2_24, 2_24]   )

       snake_case__						= processor(audio=_a     ,		images=_a   )

       self.assertListEqual(list(inputs.keys()   )     ,		['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']   )

       # test if it raises when no input is passed
       with pytest.raises(_a   ):
         processor()







     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= self.get_image_processor()
       snake_case__						= self.get_feature_extractor()

       snake_case__						= TvltProcessor(image_processor=_a     ,		feature_extractor=_a   )

       self.assertListEqual(
           processor.model_input_names     ,		image_processor.model_input_names + feature_extractor.model_input_names     ,		msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''     ,		)





import pytest

from datasets.utils.sharding import _distribute_shards, _number_of_shards_in_gen_kwargs, _split_gen_kwargs







@pytest.mark.parametrize(
    '''kwargs, expected'''							,   [
        ({'''num_shards''': 0, '''max_num_jobs''': 1}, []),
        ({'''num_shards''': 10, '''max_num_jobs''': 1}, [range(10				)]),
        ({'''num_shards''': 10, '''max_num_jobs''': 10}, [range(__lowerCAmelCase							,   i + 1				) for i in range(10				)]),
        ({'''num_shards''': 1, '''max_num_jobs''': 10}, [range(1				)]),
        ({'''num_shards''': 10, '''max_num_jobs''': 3}, [range(0							,   4				), range(4							,   7				), range(7							,   10				)]),
        ({'''num_shards''': 3, '''max_num_jobs''': 10}, [range(0							,   1				), range(1							,   2				), range(2							,   3				)]),
    ]							,   )
def SCREAMING_SNAKE_CASE	(			__lowerCAmelCase							,   __lowerCAmelCase				)    ->				Optional[int]:
  snake_case__						= _distribute_shards(**__lowerCAmelCase				)
  assert out == expected







@pytest.mark.parametrize(
    '''gen_kwargs, max_num_jobs, expected'''							,   [
        ({'''foo''': 0}, 10, [{'''foo''': 0}]),
        ({'''shards''': [0, 1, 2, 3]}, 1, [{'''shards''': [0, 1, 2, 3]}]),
        ({'''shards''': [0, 1, 2, 3]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}, {'''shards''': [2]}, {'''shards''': [3]}]),
        ({'''shards''': [0, 1]}, 4, [{'''shards''': [0]}, {'''shards''': [1]}]),
        ({'''shards''': [0, 1, 2, 3]}, 2, [{'''shards''': [0, 1]}, {'''shards''': [2, 3]}]),
    ]							,   )
def SCREAMING_SNAKE_CASE	(			__lowerCAmelCase							,   __lowerCAmelCase							,   __lowerCAmelCase				)    ->				Dict:
  snake_case__						= _split_gen_kwargs(__lowerCAmelCase							,   __lowerCAmelCase				)
  assert out == expected







@pytest.mark.parametrize(
    '''gen_kwargs, expected'''							,   [
        ({'''foo''': 0}, 1),
        ({'''shards''': [0]}, 1),
        ({'''shards''': [0, 1, 2, 3]}, 4),
        ({'''shards''': [0, 1, 2, 3], '''foo''': 0}, 4),
        ({'''shards''': [0, 1, 2, 3], '''other''': (0, 1)}, 4),
        ({'''shards''': [0, 1, 2, 3], '''shards2''': [0, 1]}, RuntimeError),
    ]							,   )
def SCREAMING_SNAKE_CASE	(			__lowerCAmelCase							,   __lowerCAmelCase				)    ->				List[Any]:
  if expected is RuntimeError:
    with pytest.raises(__lowerCAmelCase				):
      _number_of_shards_in_gen_kwargs(__lowerCAmelCase				)
  else:
    snake_case__						= _number_of_shards_in_gen_kwargs(__lowerCAmelCase				)
    assert out == expected

