
import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
   import torch

if is_tf_available():
   import tensorflow as tf






@require_vision
@require_torchvision
class UpperCAmelCase   (						unittest.TestCase   ):





   '''simple docstring'''


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  int					=       tempfile.mkdtemp()
      _A:  str					=       SamImageProcessor()
      _A:  List[str]					=       SamProcessor(lowerCAmelCase_     )
      processor.save_pretrained(self.tmpdirname     )


   def  __magic_name__				(					self     :							str			,			**lowerCAmelCase_     :							List[Any]     ):

      """simple docstring"""
      return AutoProcessor.from_pretrained(self.tmpdirname			,			**lowerCAmelCase_     ).image_processor


   def  __magic_name__				(					self     :							Union[str, Any]     ):

      """simple docstring"""
      shutil.rmtree(self.tmpdirname     )


   def  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      _A:  List[Any]					=       [np.random.randint(2_5_5			,			size=(3, 3_0, 4_0_0)			,			dtype=np.uinta     )]

      _A:  List[Any]					=       [Image.fromarray(np.moveaxis(lowerCAmelCase_			,			0			,			-1     )     ) for x in image_inputs]

      return image_inputs


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  List[Any]					=       SamProcessor(image_processor=self.get_image_processor()     )
      processor.save_pretrained(self.tmpdirname     )

      _A:  Dict					=       self.get_image_processor(do_normalize=lowerCAmelCase_			,			padding_value=1.0     )

      _A:  str					=       SamProcessor.from_pretrained(self.tmpdirname			,			do_normalize=lowerCAmelCase_			,			padding_value=1.0     )

      self.assertEqual(processor.image_processor.to_json_string()			,			image_processor_add_kwargs.to_json_string()     )
      self.assertIsInstance(processor.image_processor			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  Union[str, Any]					=       self.get_image_processor()

      _A:  Any					=       SamProcessor(image_processor=lowerCAmelCase_     )

      _A:  int					=       self.prepare_image_inputs()

      _A:  str					=       image_processor(lowerCAmelCase_			,			return_tensors='''np'''     )
      _A:  Optional[Any]					=       processor(images=lowerCAmelCase_			,			return_tensors='''np'''     )

      input_feat_extract.pop('''original_sizes'''     )  # pop original_sizes as it is popped in the processor
      input_feat_extract.pop('''reshaped_input_sizes'''     )  # pop original_sizes as it is popped in the processor

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


   @require_torch
   def  __magic_name__				(					self     :							str     ):

      """simple docstring"""
      _A:  int					=       self.get_image_processor()

      _A:  int					=       SamProcessor(image_processor=lowerCAmelCase_     )
      _A:  Any					=       [torch.ones((1, 3, 5, 5)     )]

      _A:  List[str]					=       [[1_7_6_4, 2_6_4_6]]

      _A:  Optional[Any]					=       [[6_8_3, 1_0_2_4]]
      _A:  List[str]					=       processor.post_process_masks(lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_     )
      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      _A:  int					=       processor.post_process_masks(
          lowerCAmelCase_			,			torch.tensor(lowerCAmelCase_     )			,			torch.tensor(lowerCAmelCase_     )     )
      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      # should also work with np
      _A:  List[str]					=       [np.ones((1, 3, 5, 5)     )]
      _A:  str					=       processor.post_process_masks(lowerCAmelCase_			,			np.array(lowerCAmelCase_     )			,			np.array(lowerCAmelCase_     )     )

      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      _A:  Tuple					=       [[1, 0], [0, 1]]
      with self.assertRaises(lowerCAmelCase_     ):
         _A:  List[str]					=       processor.post_process_masks(lowerCAmelCase_			,			np.array(lowerCAmelCase_     )			,			np.array(lowerCAmelCase_     )     )






@require_vision
@require_tf
class UpperCAmelCase   (						unittest.TestCase   ):





   '''simple docstring'''


   def  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      _A:  Any					=       tempfile.mkdtemp()
      _A:  Any					=       SamImageProcessor()
      _A:  int					=       SamProcessor(lowerCAmelCase_     )
      processor.save_pretrained(self.tmpdirname     )


   def  __magic_name__				(					self     :							Optional[Any]			,			**lowerCAmelCase_     :							int     ):

      """simple docstring"""
      return AutoProcessor.from_pretrained(self.tmpdirname			,			**lowerCAmelCase_     ).image_processor


   def  __magic_name__				(					self     :							Union[str, Any]     ):

      """simple docstring"""
      shutil.rmtree(self.tmpdirname     )


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  Tuple					=       [np.random.randint(2_5_5			,			size=(3, 3_0, 4_0_0)			,			dtype=np.uinta     )]

      _A:  List[str]					=       [Image.fromarray(np.moveaxis(lowerCAmelCase_			,			0			,			-1     )     ) for x in image_inputs]

      return image_inputs


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  int					=       SamProcessor(image_processor=self.get_image_processor()     )
      processor.save_pretrained(self.tmpdirname     )

      _A:  List[Any]					=       self.get_image_processor(do_normalize=lowerCAmelCase_			,			padding_value=1.0     )

      _A:  Optional[Any]					=       SamProcessor.from_pretrained(self.tmpdirname			,			do_normalize=lowerCAmelCase_			,			padding_value=1.0     )

      self.assertEqual(processor.image_processor.to_json_string()			,			image_processor_add_kwargs.to_json_string()     )
      self.assertIsInstance(processor.image_processor			,			lowerCAmelCase_     )


   def  __magic_name__				(					self     :							int     ):

      """simple docstring"""
      _A:  Optional[Any]					=       self.get_image_processor()

      _A:  Any					=       SamProcessor(image_processor=lowerCAmelCase_     )

      _A:  List[str]					=       self.prepare_image_inputs()

      _A:  Union[str, Any]					=       image_processor(lowerCAmelCase_			,			return_tensors='''np'''     )
      _A:  Union[str, Any]					=       processor(images=lowerCAmelCase_			,			return_tensors='''np'''     )

      input_feat_extract.pop('''original_sizes'''     )  # pop original_sizes as it is popped in the processor
      input_feat_extract.pop('''reshaped_input_sizes'''     )  # pop reshaped_input_sizes as it is popped in the processor

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


   @require_tf
   def  __magic_name__				(					self     :							str     ):

      """simple docstring"""
      _A:  List[Any]					=       self.get_image_processor()

      _A:  Dict					=       SamProcessor(image_processor=lowerCAmelCase_     )
      _A:  Dict					=       [tf.ones((1, 3, 5, 5)     )]

      _A:  List[Any]					=       [[1_7_6_4, 2_6_4_6]]

      _A:  Any					=       [[6_8_3, 1_0_2_4]]
      _A:  Optional[Any]					=       processor.post_process_masks(lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			return_tensors='''tf'''     )
      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      _A:  str					=       processor.post_process_masks(
          lowerCAmelCase_			,			tf.convert_to_tensor(lowerCAmelCase_     )			,			tf.convert_to_tensor(lowerCAmelCase_     )			,			return_tensors='''tf'''			,			)
      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      # should also work with np
      _A:  Dict					=       [np.ones((1, 3, 5, 5)     )]
      _A:  Optional[Any]					=       processor.post_process_masks(
          lowerCAmelCase_			,			np.array(lowerCAmelCase_     )			,			np.array(lowerCAmelCase_     )			,			return_tensors='''tf'''     )

      self.assertEqual(masks[0].shape			,			(1, 3, 1_7_6_4, 2_6_4_6)     )

      _A:  int					=       [[1, 0], [0, 1]]
      with self.assertRaises(tf.errors.InvalidArgumentError     ):
         _A:  int					=       processor.post_process_masks(
             lowerCAmelCase_			,			np.array(lowerCAmelCase_     )			,			np.array(lowerCAmelCase_     )			,			return_tensors='''tf'''     )






@require_vision
@require_torchvision
class UpperCAmelCase   (						unittest.TestCase   ):





   '''simple docstring'''


   def  __magic_name__				(					self     :							Any     ):

      """simple docstring"""
      _A:  Optional[Any]					=       tempfile.mkdtemp()
      _A:  List[str]					=       SamImageProcessor()
      _A:  Union[str, Any]					=       SamProcessor(lowerCAmelCase_     )
      processor.save_pretrained(self.tmpdirname     )


   def  __magic_name__				(					self     :							Union[str, Any]			,			**lowerCAmelCase_     :							Union[str, Any]     ):

      """simple docstring"""
      return AutoProcessor.from_pretrained(self.tmpdirname			,			**lowerCAmelCase_     ).image_processor


   def  __magic_name__				(					self     :							Optional[int]     ):

      """simple docstring"""
      shutil.rmtree(self.tmpdirname     )


   def  __magic_name__				(					self     :							List[Any]     ):

      """simple docstring"""
      _A:  Dict					=       [np.random.randint(2_5_5			,			size=(3, 3_0, 4_0_0)			,			dtype=np.uinta     )]

      _A:  Dict					=       [Image.fromarray(np.moveaxis(lowerCAmelCase_			,			0			,			-1     )     ) for x in image_inputs]

      return image_inputs


   @is_pt_tf_cross_test
   def  __magic_name__				(					self     :							Optional[Any]     ):

      """simple docstring"""
      _A:  str					=       self.get_image_processor()

      _A:  Optional[Any]					=       SamProcessor(image_processor=lowerCAmelCase_     )
      _A:  Optional[Any]					=       np.random.randint(0			,			2			,			size=(1, 3, 5, 5)     ).astype(np.floataa     )
      _A:  List[Any]					=       [tf.convert_to_tensor(lowerCAmelCase_     )]
      _A:  Optional[Any]					=       [torch.tensor(lowerCAmelCase_     )]

      _A:  Dict					=       [[1_7_6_4, 2_6_4_6]]

      _A:  Tuple					=       [[6_8_3, 1_0_2_4]]
      _A:  Union[str, Any]					=       processor.post_process_masks(
          lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			return_tensors='''tf'''     )
      _A:  Optional[Any]					=       processor.post_process_masks(
          lowerCAmelCase_			,			lowerCAmelCase_			,			lowerCAmelCase_			,			return_tensors='''pt'''     )

      self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()     )     )


   @is_pt_tf_cross_test
   def  __magic_name__				(					self     :							Optional[Any]     ):

      """simple docstring"""
      _A:  Optional[Any]					=       self.get_image_processor()

      _A:  Tuple					=       SamProcessor(image_processor=lowerCAmelCase_     )

      _A:  Any					=       self.prepare_image_inputs()

      _A:  List[Any]					=       image_processor(lowerCAmelCase_			,			return_tensors='''pt'''     )['''pixel_values'''].numpy()
      _A:  List[Any]					=       processor(images=lowerCAmelCase_			,			return_tensors='''pt'''     )['''pixel_values'''].numpy()

      _A:  Optional[int]					=       image_processor(lowerCAmelCase_			,			return_tensors='''tf'''     )['''pixel_values'''].numpy()
      _A:  int					=       processor(images=lowerCAmelCase_			,			return_tensors='''tf'''     )['''pixel_values'''].numpy()

      self.assertTrue(np.allclose(lowerCAmelCase_			,			lowerCAmelCase_     )     )
      self.assertTrue(np.allclose(lowerCAmelCase_			,			lowerCAmelCase_     )     )
      self.assertTrue(np.allclose(lowerCAmelCase_			,			lowerCAmelCase_     )     )





import random

from .binary_exp_mod import bin_exp_mod






def   lowerCamelCase__							(					a		,		a=10_00      )      ->   Optional[int]:
   if n < 2:
      return False

   if n % 2 == 0:
      return n == 2

   # this means n is odd
   _A:  List[Any]					=       n - 1
   _A:  Dict					=       0
   while d % 2 == 0:
      d /= 2
      exp += 1

   # n - 1=d*(2**exp)
   _A:  List[str]					=       0
   while count < prec:
      _A:  Optional[int]					=       random.randint(2		,		n - 1      )
      _A:  Union[str, Any]					=       bin_exp_mod(a		,		a		,		a      )
      if b != 1:
         _A:  Optional[Any]					=       True
         for _ in range(a      ):
            if b == n - 1:
               _A:  int					=       False
               break
            _A:  Optional[Any]					=       b * b
            b %= n
         if flag:
            return False
         count += 1
   return True


if __name__ == "__main__":
   UpperCAmelCase__		:     Dict     			=      abs(int(input('Enter bound : ').strip()))
   print('Here\'s the list of primes:')
   print(', '.join(str(i) for i in range(n + 1) if is_prime_big(i)))



