
import unittest

from transformers import AutoConfig, AutoTokenizer, BertConfig, TensorType, is_flax_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, slow


if is_flax_available():
    import jax

    from transformers.models.auto.modeling_flax_auto import FlaxAutoModel
    from transformers.models.bert.modeling_flax_bert import FlaxBertModel
    from transformers.models.roberta.modeling_flax_roberta import FlaxRobertaModel




@require_flax
class 					_lowerCamelCase			(   unittest.TestCase			):





     """simple docstring"""







     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[str]:




           """simple docstring"""







           for model_name in ["bert-base-cased", "bert-large-uncased"]:
                 with self.subTest(__SCREAMING_SNAKE_CASE	):
                       UpperCamelCase__     : Any											=					AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE	)
                       self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)
                       self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)

                       UpperCamelCase__     : str											=					FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                       self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)
                       self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[Any]:




           """simple docstring"""







           for model_name in ["roberta-base", "roberta-large"]:
                 with self.subTest(__SCREAMING_SNAKE_CASE	):
                       UpperCamelCase__     : Tuple											=					AutoConfig.from_pretrained(__SCREAMING_SNAKE_CASE	)
                       self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)
                       self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)

                       UpperCamelCase__     : Optional[int]											=					FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                       self.assertIsNotNone(__SCREAMING_SNAKE_CASE	)
                       self.assertIsInstance(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	)






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           for model_name in ["bert-base-cased", "bert-large-uncased"]:
                 UpperCamelCase__     : int											=					AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 UpperCamelCase__     : List[Any]											=					FlaxBertModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 UpperCamelCase__     : int											=					tokenizer('''Do you support jax jitted function?'''    ,    return_tensors=TensorType.JAX	)

                 @jax.jit
                 def eval(**__SCREAMING_SNAKE_CASE	):
                       return model(**__SCREAMING_SNAKE_CASE	)

                 eval(**__SCREAMING_SNAKE_CASE	).block_until_ready()






     @slow
     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







           for model_name in ["roberta-base", "roberta-large"]:
                 UpperCamelCase__     : Dict											=					AutoTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 UpperCamelCase__     : Union[str, Any]											=					FlaxRobertaModel.from_pretrained(__SCREAMING_SNAKE_CASE	)
                 UpperCamelCase__     : Dict											=					tokenizer('''Do you support jax jitted function?'''    ,    return_tensors=TensorType.JAX	)

                 @jax.jit
                 def eval(**__SCREAMING_SNAKE_CASE	):
                       return model(**__SCREAMING_SNAKE_CASE	)

                 eval(**__SCREAMING_SNAKE_CASE	).block_until_ready()






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Tuple:




           """simple docstring"""







           with self.assertRaisesRegex(
               __SCREAMING_SNAKE_CASE    ,    '''bert-base is not a local folder and is not a valid model identifier'''	):
                 UpperCamelCase__     : int											=					FlaxAutoModel.from_pretrained('''bert-base'''	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> Dict:




           """simple docstring"""







           with self.assertRaisesRegex(
               __SCREAMING_SNAKE_CASE    ,    r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''	):
                 UpperCamelCase__     : str											=					FlaxAutoModel.from_pretrained(__SCREAMING_SNAKE_CASE    ,    revision='''aaaaaa'''	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[str]:




           """simple docstring"""







           with self.assertRaisesRegex(
               __SCREAMING_SNAKE_CASE    ,    '''hf-internal-testing/config-no-model does not appear to have a file named flax_model.msgpack'''    ,    ):
                 UpperCamelCase__     : List[Any]											=					FlaxAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''	)






     def  __SCREAMING_SNAKE_CASE      (				self	)	-> List[str]:




           """simple docstring"""







           with self.assertRaisesRegex(__SCREAMING_SNAKE_CASE    ,    '''Use `from_pt=True` to load this model'''	):
                 UpperCamelCase__     : Any											=					FlaxAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''	)




from typing import List, Optional, Union

import numpy as np
import PIL.Image

from ...image_processing_utils import BaseImageProcessor, BatchFeature
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowerCamelCase  =logging.get_logger(__name__)




class 					_lowerCamelCase			(   UpperCamelCase_			):





     """simple docstring"""







     SCREAMING_SNAKE_CASE_		 =	['''pixel_values''']






     def __init__(				self    ,    __SCREAMING_SNAKE_CASE = True    ,    __SCREAMING_SNAKE_CASE = 3_2    ,    __SCREAMING_SNAKE_CASE=PILImageResampling.BILINEAR    ,    __SCREAMING_SNAKE_CASE = True    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> None:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					do_resize
           UpperCamelCase__     : Tuple											=					do_rescale
           UpperCamelCase__     : Dict											=					size_divisor
           UpperCamelCase__     : str											=					resample
           super().__init__(**__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE = None    ,    **__SCREAMING_SNAKE_CASE	)	-> np.ndarray:




           """simple docstring"""







           UpperCamelCase__					,UpperCamelCase__     : Optional[Any]											=					get_image_size(__SCREAMING_SNAKE_CASE	)
           # Rounds the height and width down to the closest multiple of size_divisor
           UpperCamelCase__     : List[Any]											=					height // size_divisor * size_divisor
           UpperCamelCase__     : Dict											=					width // size_divisor * size_divisor
           UpperCamelCase__     : str											=					resize(__SCREAMING_SNAKE_CASE    ,    (new_h, new_w)    ,    resample=__SCREAMING_SNAKE_CASE    ,    data_format=__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)
           return image






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE = None    ,    **__SCREAMING_SNAKE_CASE	)	-> np.ndarray:




           """simple docstring"""







           return rescale(image=__SCREAMING_SNAKE_CASE    ,    scale=__SCREAMING_SNAKE_CASE    ,    data_format=__SCREAMING_SNAKE_CASE    ,    **__SCREAMING_SNAKE_CASE	)






     def  __SCREAMING_SNAKE_CASE      (				self    ,    __SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE = None    ,    __SCREAMING_SNAKE_CASE = None    ,    __SCREAMING_SNAKE_CASE=None    ,    __SCREAMING_SNAKE_CASE = None    ,    __SCREAMING_SNAKE_CASE = None    ,    __SCREAMING_SNAKE_CASE = ChannelDimension.FIRST    ,    **__SCREAMING_SNAKE_CASE    ,    )	-> BatchFeature:




           """simple docstring"""







           UpperCamelCase__     : Dict											=					do_resize if do_resize is not None else self.do_resize
           UpperCamelCase__     : List[Any]											=					do_rescale if do_rescale is not None else self.do_rescale
           UpperCamelCase__     : Union[str, Any]											=					size_divisor if size_divisor is not None else self.size_divisor
           UpperCamelCase__     : List[str]											=					resample if resample is not None else self.resample

           if do_resize and size_divisor is None:
                 raise ValueError('''size_divisor is required for resizing'''	)

           UpperCamelCase__     : List[Any]											=					make_list_of_images(__SCREAMING_SNAKE_CASE	)

           if not valid_images(__SCREAMING_SNAKE_CASE	):
                 raise ValueError('''Invalid image(s)'''	)

           # All transformations expect numpy arrays.
           UpperCamelCase__     : Dict											=					[to_numpy_array(__SCREAMING_SNAKE_CASE	) for img in images]

           if do_resize:
                 UpperCamelCase__     : Optional[int]											=					[self.resize(__SCREAMING_SNAKE_CASE    ,    size_divisor=__SCREAMING_SNAKE_CASE    ,    resample=__SCREAMING_SNAKE_CASE	) for image in images]

           if do_rescale:
                 UpperCamelCase__     : Union[str, Any]											=					[self.rescale(__SCREAMING_SNAKE_CASE    ,    scale=1 / 2_5_5	) for image in images]

           UpperCamelCase__     : List[str]											=					[to_channel_dimension_format(__SCREAMING_SNAKE_CASE    ,    __SCREAMING_SNAKE_CASE	) for image in images]

           UpperCamelCase__     : Optional[Any]											=					{'''pixel_values''': images}
           return BatchFeature(data=__SCREAMING_SNAKE_CASE    ,    tensor_type=__SCREAMING_SNAKE_CASE	)


