




import random
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      import PIL

      from transformers import FlavaImageProcessor
      from transformers.image_utils import PILImageResampling
      from transformers.models.flava.image_processing_flava import (
          FLAVA_CODEBOOK_MEAN,
          FLAVA_CODEBOOK_STD,
          FLAVA_IMAGE_MEAN,
          FLAVA_IMAGE_STD,
      )
else:
      __SCREAMING_SNAKE_CASE     : Tuple      =None






class       A      (		unittest.TestCase		):
     """simple docstring"""

     def __init__(     self	: Any						,	A__	: Tuple						,	A__	: List[str]=7						,	A__	: Optional[Any]=3						,	A__	: Tuple=3_0						,	A__	: List[str]=4_0_0						,	A__	: Optional[Any]=True						,	A__	: int=None						,	A__	: Any=True						,	A__	: Union[str, Any]=None						,	A__	: List[str]=None						,	A__	: Union[str, Any]=True						,	A__	: List[Any]=1 / 2_5_5						,	A__	: Any=True						,	A__	: List[Any]=FLAVA_IMAGE_MEAN						,	A__	: List[str]=FLAVA_IMAGE_STD						,	A__	: Dict=1_4						,	A__	: str=7_5						,	A__	: Tuple=None						,	A__	: List[str]=1_6						,	A__	: Union[str, Any]=0.3						,	A__	: List[Any]=None						,	A__	: Optional[Any]=True						,	A__	: Optional[Any]=None						,	A__	: int=None						,	A__	: str=True						,	A__	: Dict=None						,	A__	: Optional[int]=True						,	A__	: Any=True						,	A__	: Any=FLAVA_CODEBOOK_MEAN						,	A__	: int=FLAVA_CODEBOOK_STD						,	)					->							List[str]:
           lowerCamelCase__   	=					size if size is not None else {'height': 2_2_4, 'width': 2_2_4}
           lowerCamelCase__   	=					crop_size if crop_size is not None else {'height': 2_2_4, 'width': 2_2_4}
           lowerCamelCase__   	=					codebook_size if codebook_size is not None else {'height': 1_1_2, 'width': 1_1_2}
           lowerCamelCase__   	=					codebook_crop_size if codebook_crop_size is not None else {'height': 1_1_2, 'width': 1_1_2}

           lowerCamelCase__   	=					parent
           lowerCamelCase__   	=					batch_size
           lowerCamelCase__   	=					num_channels
           lowerCamelCase__   	=					do_resize
           lowerCamelCase__   	=					do_rescale
           lowerCamelCase__   	=					rescale_factor
           lowerCamelCase__   	=					min_resolution
           lowerCamelCase__   	=					max_resolution
           lowerCamelCase__   	=					size
           lowerCamelCase__   	=					resample if resample is not None else PILImageResampling.BICUBIC
           lowerCamelCase__   	=					do_normalize
           lowerCamelCase__   	=					image_mean
           lowerCamelCase__   	=					image_std
           lowerCamelCase__   	=					do_center_crop
           lowerCamelCase__   	=					crop_size

           lowerCamelCase__   	=					input_size_patches
           lowerCamelCase__   	=					total_mask_patches
           lowerCamelCase__   	=					mask_group_max_patches
           lowerCamelCase__   	=					mask_group_min_patches
           lowerCamelCase__   	=					mask_group_min_aspect_ratio
           lowerCamelCase__   	=					mask_group_max_aspect_ratio

           lowerCamelCase__   	=					codebook_do_resize
           lowerCamelCase__   	=					codebook_size
           lowerCamelCase__   	=					codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
           lowerCamelCase__   	=					codebook_do_center_crop
           lowerCamelCase__   	=					codebook_crop_size
           lowerCamelCase__   	=					codebook_do_map_pixels
           lowerCamelCase__   	=					codebook_do_normalize
           lowerCamelCase__   	=					codebook_image_mean
           lowerCamelCase__   	=					codebook_image_std




     def 			__snake_case (     self	: Union[str, Any]      )					->							List[Any]:
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "resample": self.resample,
               "do_rescale": self.do_rescale,
               "rescale_factor": self.rescale_factor,
               "do_center_crop": self.do_center_crop,
               "crop_size": self.crop_size,
               "input_size_patches": self.input_size_patches,
               "total_mask_patches": self.total_mask_patches,
               "mask_group_max_patches": self.mask_group_max_patches,
               "mask_group_min_patches": self.mask_group_min_patches,
               "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
               "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
               "codebook_do_resize": self.codebook_do_resize,
               "codebook_size": self.codebook_size,
               "codebook_resample": self.codebook_resample,
               "codebook_do_center_crop": self.codebook_do_center_crop,
               "codebook_crop_size": self.codebook_crop_size,
               "codebook_do_map_pixels": self.codebook_do_map_pixels,
               "codebook_do_normalize": self.codebook_do_normalize,
               "codebook_image_mean": self.codebook_image_mean,
               "codebook_image_std": self.codebook_image_std,
           }




     def 			__snake_case (     self	: str      )					->							List[str]:
           return (self.size["height"], self.size["width"])




     def 			__snake_case (     self	: List[str]      )					->							Any:
           return (
               (self.input_size_patches, self.input_size_patches)
               if not isinstance(self.input_size_patches						,	A__      )
               else self.input_size_patches
           )




     def 			__snake_case (     self	: Optional[int]      )					->							Optional[int]:
           return (self.codebook_size["height"], self.codebook_size["width"])






@require_torch
@require_vision
class       A      (		_lowerCAmelCase      ,    unittest.TestCase		):
     """simple docstring"""

     _snake_case:						Optional[Any]  =FlavaImageProcessor if is_vision_available() else None
     _snake_case:						Dict  =None




     def 			__snake_case (     self	: str      )					->							Dict:
           lowerCamelCase__   	=					FlavaImageProcessingTester(self      )




     @property
     def 			__snake_case (     self	: str      )					->							List[str]:
           return self.image_processor_tester.prepare_image_processor_dict()




     def 			__snake_case (     self	: List[Any]      )					->							int:
           lowerCamelCase__   	=					self.image_processing_class(**self.image_processor_dict      )
           self.assertTrue(hasattr(A__						,	'image_mean'      )      )
           self.assertTrue(hasattr(A__						,	'image_std'      )      )
           self.assertTrue(hasattr(A__						,	'do_normalize'      )      )
           self.assertTrue(hasattr(A__						,	'do_resize'      )      )
           self.assertTrue(hasattr(A__						,	'resample'      )      )
           self.assertTrue(hasattr(A__						,	'crop_size'      )      )
           self.assertTrue(hasattr(A__						,	'do_center_crop'      )      )
           self.assertTrue(hasattr(A__						,	'do_rescale'      )      )
           self.assertTrue(hasattr(A__						,	'rescale_factor'      )      )
           self.assertTrue(hasattr(A__						,	'masking_generator'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_do_resize'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_size'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_resample'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_do_center_crop'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_crop_size'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_do_map_pixels'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_do_normalize'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_image_mean'      )      )
           self.assertTrue(hasattr(A__						,	'codebook_image_std'      )      )




     def 			__snake_case (     self	: Tuple      )					->							Optional[int]:
           lowerCamelCase__   	=					self.image_processing_class.from_dict(self.image_processor_dict      )
           self.assertEqual(image_processor.size						,	{'height': 2_2_4, 'width': 2_2_4}      )
           self.assertEqual(image_processor.crop_size						,	{'height': 2_2_4, 'width': 2_2_4}      )
           self.assertEqual(image_processor.codebook_size						,	{'height': 1_1_2, 'width': 1_1_2}      )
           self.assertEqual(image_processor.codebook_crop_size						,	{'height': 1_1_2, 'width': 1_1_2}      )

           lowerCamelCase__   	=					self.image_processing_class.from_dict(
               self.image_processor_dict						,	size=4_2						,	crop_size=8_4						,	codebook_size=3_3						,	codebook_crop_size=6_6      )
           self.assertEqual(image_processor.size						,	{'height': 4_2, 'width': 4_2}      )
           self.assertEqual(image_processor.crop_size						,	{'height': 8_4, 'width': 8_4}      )
           self.assertEqual(image_processor.codebook_size						,	{'height': 3_3, 'width': 3_3}      )
           self.assertEqual(image_processor.codebook_crop_size						,	{'height': 6_6, 'width': 6_6}      )




     def 			__snake_case (     self	: Any      )					->							Any:
           pass




     def 			__snake_case (     self	: Any      )					->							Tuple:
           # Initialize image_processing
           lowerCamelCase__   	=					self.image_processing_class(**self.image_processor_dict      )
           # create random PIL images
           lowerCamelCase__   	=					prepare_image_inputs(self.image_processor_tester						,	equal_resolution=A__      )
           for image in image_inputs:
                 self.assertIsInstance(A__						,	PIL.Image.Image      )

           # Test not batched input
           lowerCamelCase__   	=					image_processing(image_inputs[0]						,	return_tensors='pt'      )

           # Test no bool masked pos
           self.assertFalse('bool_masked_pos' in encoded_images      )

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()

           self.assertEqual(
               encoded_images.pixel_values.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

           # Test batched
           lowerCamelCase__   	=					image_processing(A__						,	return_tensors='pt'      )
           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()

           # Test no bool masked pos
           self.assertFalse('bool_masked_pos' in encoded_images      )

           self.assertEqual(
               encoded_images.pixel_values.shape						,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )						,	)




     def 			__snake_case (     self	: int						,	A__	: int						,	A__	: Union[str, Any]      )					->							List[str]:
           # Initialize image_processing
           lowerCamelCase__   	=					self.image_processing_class(**self.image_processor_dict      )
           # create random tensors
           lowerCamelCase__   	=					prepare_image_inputs(self.image_processor_tester						,	equal_resolution=A__						,	**A__      )
           for image in image_inputs:
                 self.assertIsInstance(A__						,	A__      )

           # Test not batched input
           lowerCamelCase__   	=					image_processing(image_inputs[0]						,	return_tensors='pt'      )

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

           lowerCamelCase__   	=					image_processing(A__						,	return_image_mask=A__						,	return_tensors='pt'      )

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape						,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )						,	)

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_mask_size()
           self.assertEqual(
               encoded_images.bool_masked_pos.shape						,	(
                   self.image_processor_tester.batch_size,
                   expected_height,
                   expected_width,
               )						,	)

           # Test batched
           lowerCamelCase__   	=					image_processing(A__						,	return_tensors='pt'      ).pixel_values

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.shape						,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )						,	)

           # Test masking
           lowerCamelCase__   	=					image_processing(A__						,	return_image_mask=A__						,	return_tensors='pt'      )

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_image_size()
           self.assertEqual(
               encoded_images.pixel_values.shape						,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )						,	)

           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_mask_size()
           self.assertEqual(
               encoded_images.bool_masked_pos.shape						,	(
                   self.image_processor_tester.batch_size,
                   expected_height,
                   expected_width,
               )						,	)




     def 			__snake_case (     self	: Union[str, Any]      )					->							Optional[Any]:
           self._test_call_framework(np.ndarray						,	prepare_kwargs={'numpify': True}      )




     def 			__snake_case (     self	: Optional[int]      )					->							int:
           self._test_call_framework(torch.Tensor						,	prepare_kwargs={'torchify': True}      )




     def 			__snake_case (     self	: Any      )					->							Dict:
           # Initialize image_processing
           random.seed(1_2_3_4      )
           lowerCamelCase__   	=					self.image_processing_class(**self.image_processor_dict      )
           lowerCamelCase__   	=					prepare_image_inputs(self.image_processor_tester						,	equal_resolution=A__						,	torchify=A__      )

           # Test not batched input
           lowerCamelCase__   	=					image_processing(image_inputs[0]						,	return_image_mask=A__						,	return_tensors='pt'      )
           self.assertEqual(encoded_images.bool_masked_pos.sum().item()						,	7_5      )




     def 			__snake_case (     self	: Union[str, Any]      )					->							Tuple:
           # Initialize image_processing
           lowerCamelCase__   	=					self.image_processing_class(**self.image_processor_dict      )
           # create random PIL images
           lowerCamelCase__   	=					prepare_image_inputs(self.image_processor_tester						,	equal_resolution=A__      )
           for image in image_inputs:
                 self.assertIsInstance(A__						,	PIL.Image.Image      )

           # Test not batched input
           lowerCamelCase__   	=					image_processing(image_inputs[0]						,	return_codebook_pixels=A__						,	return_tensors='pt'      )
           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_codebook_image_size()
           self.assertEqual(
               encoded_images.codebook_pixel_values.shape						,	(1, self.image_processor_tester.num_channels, expected_height, expected_width)						,	)

           # Test batched
           lowerCamelCase__   	=					image_processing(A__						,	return_codebook_pixels=A__						,	return_tensors='pt'      )
           lowerCamelCase__	,		lowerCamelCase__   	=					self.image_processor_tester.get_expected_codebook_image_size()
           self.assertEqual(
               encoded_images.codebook_pixel_values.shape						,	(
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )						,	)





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE     : Tuple      ={
    """configuration_open_llama""": ["""OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP""", """OpenLlamaConfig"""],
}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __SCREAMING_SNAKE_CASE     : Dict      =["""LlamaTokenizer"""]

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __SCREAMING_SNAKE_CASE     : int      =["""LlamaTokenizerFast"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __SCREAMING_SNAKE_CASE     : Optional[Any]      =[
          """OpenLlamaForCausalLM""",
          """OpenLlamaModel""",
          """OpenLlamaPreTrainedModel""",
          """OpenLlamaForSequenceClassification""",
      ]


if TYPE_CHECKING:
      from .configuration_open_llama import OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenLlamaConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from transformers import LlamaTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from transformers import LlamaTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_open_llama import (
                OpenLlamaForCausalLM,
                OpenLlamaForSequenceClassification,
                OpenLlamaModel,
                OpenLlamaPreTrainedModel,
            )


else:
      import sys

      __SCREAMING_SNAKE_CASE     : Union[str, Any]      =_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)