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   						=    _SCREAMING_SNAKE_CASE   						=    _SCREAMING_SNAKE_CASE   						=    _SCREAMING_SNAKE_CASE   						=    None







class 					UpperCAmelCase_				(							unittest.TestCase    ):




    def __init__(					self  :   Union[str, Any]      ,      lowerCAmelCase  :   List[Any]      ,      lowerCAmelCase  :   List[Any]=7      ,      lowerCAmelCase  :   List[str]=3      ,      lowerCAmelCase  :   int=30      ,      lowerCAmelCase  :   Optional[int]=400      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   Optional[int]=None      ,      lowerCAmelCase  :   Union[str, Any]=True      ,      lowerCAmelCase  :   Union[str, Any]=None      ,      lowerCAmelCase  :   str=None      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   Union[str, Any]=1 / 255      ,      lowerCAmelCase  :   Dict=True      ,      lowerCAmelCase  :   Dict=FLAVA_IMAGE_MEAN      ,      lowerCAmelCase  :   Optional[int]=FLAVA_IMAGE_STD      ,      lowerCAmelCase  :   Tuple=14      ,      lowerCAmelCase  :   int=75      ,      lowerCAmelCase  :   str=None      ,      lowerCAmelCase  :   str=16      ,      lowerCAmelCase  :   str=0.3      ,      lowerCAmelCase  :   List[str]=None      ,      lowerCAmelCase  :   Tuple=True      ,      lowerCAmelCase  :   Optional[Any]=None      ,      lowerCAmelCase  :   Union[str, Any]=None      ,      lowerCAmelCase  :   Optional[Any]=True      ,      lowerCAmelCase  :   List[str]=None      ,      lowerCAmelCase  :   Any=True      ,      lowerCAmelCase  :   Any=True      ,      lowerCAmelCase  :   Optional[Any]=FLAVA_CODEBOOK_MEAN      ,      lowerCAmelCase  :   Any=FLAVA_CODEBOOK_STD      ,      )	-> Optional[Any]:
      a__     		=     size if size is not None else {"height": 224, "width": 224}
      a__     		=     crop_size if crop_size is not None else {"height": 224, "width": 224}
      a__     		=     codebook_size if codebook_size is not None else {"height": 112, "width": 112}
      a__     		=     codebook_crop_size if codebook_crop_size is not None else {"height": 112, "width": 112}

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

      a__     		=     input_size_patches
      a__     		=     total_mask_patches
      a__     		=     mask_group_max_patches
      a__     		=     mask_group_min_patches
      a__     		=     mask_group_min_aspect_ratio
      a__     		=     mask_group_max_aspect_ratio

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




    def 						__lowerCamelCase (					self  :   Tuple   )	-> int:
      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 						__lowerCamelCase (					self  :   List[str]   )	-> Any:
      return (self.size["height"], self.size["width"])




    def 						__lowerCamelCase (					self  :   str   )	-> List[str]:
      return (
          (self.input_size_patches, self.input_size_patches)
          if not isinstance(self.input_size_patches      ,      lowerCAmelCase   )
          else self.input_size_patches
      )




    def 						__lowerCamelCase (					self  :   List[Any]   )	-> List[Any]:
      return (self.codebook_size["height"], self.codebook_size["width"])







@require_torch
@require_vision
class 					UpperCAmelCase_				(							_a     ,     unittest.TestCase    ):
    UpperCAmelCase_			:       Any						 =							FlavaImageProcessor if is_vision_available() else None
    UpperCAmelCase_			:       str						 =							None




    def 						__lowerCamelCase (					self  :   Optional[int]   )	-> int:
      a__     		=     FlavaImageProcessingTester(self   )




    @property
    def 						__lowerCamelCase (					self  :   int   )	-> Optional[Any]:
      return self.image_processor_tester.prepare_image_processor_dict()




    def 						__lowerCamelCase (					self  :   List[str]   )	-> Dict:
      a__     		=     self.image_processing_class(**self.image_processor_dict   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "image_mean"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "image_std"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "do_normalize"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "do_resize"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "resample"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "crop_size"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "do_center_crop"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "do_rescale"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "rescale_factor"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "masking_generator"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_do_resize"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_size"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_resample"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_do_center_crop"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_crop_size"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_do_map_pixels"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_do_normalize"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_image_mean"   )   )
      self.assertTrue(hasattr(lowerCAmelCase      ,      "codebook_image_std"   )   )




    def 						__lowerCamelCase (					self  :   Any   )	-> Union[str, Any]:
      a__     		=     self.image_processing_class.from_dict(self.image_processor_dict   )
      self.assertEqual(image_processor.size      ,      {"height": 224, "width": 224}   )
      self.assertEqual(image_processor.crop_size      ,      {"height": 224, "width": 224}   )
      self.assertEqual(image_processor.codebook_size      ,      {"height": 112, "width": 112}   )
      self.assertEqual(image_processor.codebook_crop_size      ,      {"height": 112, "width": 112}   )

      a__     		=     self.image_processing_class.from_dict(
          self.image_processor_dict      ,      size=42      ,      crop_size=84      ,      codebook_size=33      ,      codebook_crop_size=66   )
      self.assertEqual(image_processor.size      ,      {"height": 42, "width": 42}   )
      self.assertEqual(image_processor.crop_size      ,      {"height": 84, "width": 84}   )
      self.assertEqual(image_processor.codebook_size      ,      {"height": 33, "width": 33}   )
      self.assertEqual(image_processor.codebook_crop_size      ,      {"height": 66, "width": 66}   )




    def 						__lowerCamelCase (					self  :   Tuple   )	-> List[str]:
      pass




    def 						__lowerCamelCase (					self  :   List[str]   )	-> int:
      # Initialize image_processing
      a__     		=     self.image_processing_class(**self.image_processor_dict   )
      # create random PIL images
      a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase   )
      for image in image_inputs:
        self.assertIsInstance(lowerCAmelCase      ,      PIL.Image.Image   )

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

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

      a__		,a__     		=     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
      a__     		=     image_processing(lowerCAmelCase      ,      return_tensors="pt"   )
      a__		,a__     		=     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 						__lowerCamelCase (					self  :   Dict      ,      lowerCAmelCase  :   Tuple      ,      lowerCAmelCase  :   int   )	-> List[Any]:
      # Initialize image_processing
      a__     		=     self.image_processing_class(**self.image_processor_dict   )
      # create random tensors
      a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase      ,      **lowerCAmelCase   )
      for image in image_inputs:
        self.assertIsInstance(lowerCAmelCase      ,      lowerCAmelCase   )

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

      a__		,a__     		=     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)      ,      )

      a__     		=     image_processing(lowerCAmelCase      ,      return_image_mask=lowerCAmelCase      ,      return_tensors="pt"   )

      a__		,a__     		=     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,
          )      ,      )

      a__		,a__     		=     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
      a__     		=     image_processing(lowerCAmelCase      ,      return_tensors="pt"   ).pixel_values

      a__		,a__     		=     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
      a__     		=     image_processing(lowerCAmelCase      ,      return_image_mask=lowerCAmelCase      ,      return_tensors="pt"   )

      a__		,a__     		=     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,
          )      ,      )

      a__		,a__     		=     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 						__lowerCamelCase (					self  :   Union[str, Any]   )	-> Tuple:
      self._test_call_framework(np.ndarray      ,      prepare_kwargs={"numpify": True}   )




    def 						__lowerCamelCase (					self  :   Dict   )	-> Optional[Any]:
      self._test_call_framework(torch.Tensor      ,      prepare_kwargs={"torchify": True}   )




    def 						__lowerCamelCase (					self  :   Dict   )	-> Tuple:
      # Initialize image_processing
      random.seed(1234   )
      a__     		=     self.image_processing_class(**self.image_processor_dict   )
      a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase      ,      torchify=lowerCAmelCase   )

      # Test not batched input
      a__     		=     image_processing(image_inputs[0]      ,      return_image_mask=lowerCAmelCase      ,      return_tensors="pt"   )
      self.assertEqual(encoded_images.bool_masked_pos.sum().item()      ,      75   )




    def 						__lowerCamelCase (					self  :   Tuple   )	-> str:
      # Initialize image_processing
      a__     		=     self.image_processing_class(**self.image_processor_dict   )
      # create random PIL images
      a__     		=     prepare_image_inputs(self.image_processor_tester      ,      equal_resolution=lowerCAmelCase   )
      for image in image_inputs:
        self.assertIsInstance(lowerCAmelCase      ,      PIL.Image.Image   )

      # Test not batched input
      a__     		=     image_processing(image_inputs[0]      ,      return_codebook_pixels=lowerCAmelCase      ,      return_tensors="pt"   )
      a__		,a__     		=     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
      a__     		=     image_processing(lowerCAmelCase      ,      return_codebook_pixels=lowerCAmelCase      ,      return_tensors="pt"   )
      a__		,a__     		=     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   						=    {
    """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   						=    ["""LlamaTokenizer"""]

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

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _SCREAMING_SNAKE_CASE   						=    [
         """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   						=    _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

