





import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import ImageGPTImageProcessor
class 							SCREAMING_SNAKE_CASE  (unittest.TestCase ):


   def __init__(       self     :							Optional[Any]     ,	a     :							Optional[int]     ,	a     :							Tuple=7     ,	a     :							Tuple=3     ,	a     :							Dict=18     ,	a     :							int=30     ,	a     :							Dict=400     ,	a     :							List[str]=True     ,	a     :							str=None     ,	a     :							str=True     ,	)->  Tuple:





     """simple docstring"""







     lowercase__           =    size if size is not None else {"height": 18, "width": 18}
     lowercase__           =    parent
     lowercase__           =    batch_size
     lowercase__           =    num_channels
     lowercase__           =    image_size
     lowercase__           =    min_resolution
     lowercase__           =    max_resolution
     lowercase__           =    do_resize
     lowercase__           =    size
     lowercase__           =    do_normalize


   def 			SCREAMING_SNAKE_CASE_				(       self     :							Any			)->  List[str]:





     """simple docstring"""







     return {
         # here we create 2 clusters for the sake of simplicity
         "clusters": np.asarray(
             [
                 [0.8866443634033203, 0.6618829369544983, 0.3891746401786804],
                 [-0.6042559146881104, -0.02295008860528469, 0.5423797369003296],
             ]			),
         "do_resize": self.do_resize,
         "size": self.size,
         "do_normalize": self.do_normalize,
     }






@require_torch
@require_vision
class 							SCREAMING_SNAKE_CASE  (__SCREAMING_SNAKE_CASE     ,      unittest.TestCase ):
   _UpperCamelCase       :						Any					   =			ImageGPTImageProcessor if is_vision_available() else None


   def 			SCREAMING_SNAKE_CASE_				(       self     :							Optional[int]			)->  List[str]:





     """simple docstring"""







     lowercase__           =    ImageGPTImageProcessingTester(self			)


   @property
   def 			SCREAMING_SNAKE_CASE_				(       self     :							Dict			)->  List[Any]:





     """simple docstring"""







     return self.image_processor_tester.prepare_image_processor_dict()


   def 			SCREAMING_SNAKE_CASE_				(       self     :							Dict			)->  List[Any]:





     """simple docstring"""







     lowercase__           =    self.image_processing_class(**self.image_processor_dict			)
     self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,	'clusters'			)			)
     self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,	'do_resize'			)			)
     self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,	'size'			)			)
     self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,	'do_normalize'			)			)


   def 			SCREAMING_SNAKE_CASE_				(       self     :							List[Any]			)->  int:





     """simple docstring"""







     lowercase__           =    self.image_processing_class.from_dict(self.image_processor_dict			)
     self.assertEqual(image_processor.size     ,	{'height': 18, 'width': 18}			)

     lowercase__           =    self.image_processing_class.from_dict(self.image_processor_dict     ,	size=42			)
     self.assertEqual(image_processor.size     ,	{'height': 42, 'width': 42}			)


   def 			SCREAMING_SNAKE_CASE_				(       self     :							Dict			)->  List[Any]:





     """simple docstring"""







     lowercase__           =    self.image_processing_class(**self.image_processor_dict			)
     lowercase__           =    json.loads(image_processor.to_json_string()			)
     for key, value in self.image_processor_dict.items():
       if key == "clusters":
         self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE     ,	obj[key]			)			)
       else:
         self.assertEqual(obj[key]     ,	_SCREAMING_SNAKE_CASE			)


   def 			SCREAMING_SNAKE_CASE_				(       self     :							List[str]			)->  Any:





     """simple docstring"""







     lowercase__           =    self.image_processing_class(**self.image_processor_dict			)

     with tempfile.TemporaryDirectory() as tmpdirname:
       lowercase__           =    os.path.join(_SCREAMING_SNAKE_CASE     ,	'image_processor.json'			)
       image_processor_first.to_json_file(_SCREAMING_SNAKE_CASE			)
       lowercase__           =    self.image_processing_class.from_json_file(_SCREAMING_SNAKE_CASE			).to_dict()

     lowercase__           =    image_processor_first.to_dict()
     for key, value in image_processor_first.items():
       if key == "clusters":
         self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE     ,	image_processor_second[key]			)			)
       else:
         self.assertEqual(image_processor_first[key]     ,	_SCREAMING_SNAKE_CASE			)


   def 			SCREAMING_SNAKE_CASE_				(       self     :							int			)->  List[Any]:





     """simple docstring"""







     lowercase__           =    self.image_processing_class(**self.image_processor_dict			)

     with tempfile.TemporaryDirectory() as tmpdirname:
       image_processor_first.save_pretrained(_SCREAMING_SNAKE_CASE			)
       lowercase__           =    self.image_processing_class.from_pretrained(_SCREAMING_SNAKE_CASE			).to_dict()

     lowercase__           =    image_processor_first.to_dict()
     for key, value in image_processor_first.items():
       if key == "clusters":
         self.assertTrue(np.array_equal(_SCREAMING_SNAKE_CASE     ,	image_processor_second[key]			)			)
       else:
         self.assertEqual(image_processor_first[key]     ,	_SCREAMING_SNAKE_CASE			)


   @unittest.skip('ImageGPT requires clusters at initialization'			)
   def 			SCREAMING_SNAKE_CASE_				(       self     :							str			)->  List[str]:





     """simple docstring"""







     pass






def      __UpperCamelCase    ()							->  Union[str, Any]:
  lowercase__           =    load_dataset('hf-internal-testing/fixtures_image_utils'						,		split='test'						)

  lowercase__           =    Image.open(dataset[4]['file']						)
  lowercase__           =    Image.open(dataset[5]['file']						)

  lowercase__           =    [imagea, imagea]

  return images


@require_vision
@require_torch
class 							SCREAMING_SNAKE_CASE  (unittest.TestCase ):


   @slow
   def 			SCREAMING_SNAKE_CASE_				(       self     :							Union[str, Any]			)->  Dict:





     """simple docstring"""







     lowercase__           =    ImageGPTImageProcessor.from_pretrained('openai/imagegpt-small'			)

     lowercase__           =    prepare_images()

     # test non-batched
     lowercase__           =    image_processing(images[0]     ,	return_tensors='pt'			)

     self.assertIsInstance(encoding.input_ids     ,	torch.LongTensor			)
     self.assertEqual(encoding.input_ids.shape     ,	(1, 1_024)			)

     lowercase__           =    [306, 191, 191]
     self.assertEqual(encoding.input_ids[0, :3].tolist()     ,	_SCREAMING_SNAKE_CASE			)

     # test batched
     lowercase__           =    image_processing(_SCREAMING_SNAKE_CASE     ,	return_tensors='pt'			)

     self.assertIsInstance(encoding.input_ids     ,	torch.LongTensor			)
     self.assertEqual(encoding.input_ids.shape     ,	(2, 1_024)			)

     lowercase__           =    [303, 13, 13]
     self.assertEqual(encoding.input_ids[1, -3:].tolist()     ,	_SCREAMING_SNAKE_CASE			)







"""simple docstring"""






def      _lowercase							(    __snake_case   ,__snake_case      )		->  int:
			return int((input_a, input_a).count(1      ) != 0      )


def      _lowercase							(    )		->  None:
			assert or_gate(0   ,0      ) == 0
			assert or_gate(0   ,1      ) == 1
			assert or_gate(1   ,0      ) == 1
			assert or_gate(1   ,1      ) == 1


if __name__ == "__main__":
	print(or_gate(0, 1))
	print(or_gate(1, 0))
	print(or_gate(0, 0))
	print(or_gate(1, 1))