

"""simple docstring"""




import gc
import importlib.metadata
import tempfile
import unittest

from packaging import version

from transformers import (
    AutoModel,
    AutoModelForCausalLM,
    AutoModelForSeqaSeqLM,
    AutoModelForSequenceClassification,
    AutoTokenizer,
    BitsAndBytesConfig,
    pipeline,
)
from transformers.testing_utils import (
    is_torch_available,
    require_accelerate,
    require_bitsandbytes,
    require_torch,
    require_torch_gpu,
    require_torch_multi_gpu,
    slow,
)

def       a__					(		UpperCAmelCase							:      Optional[Any]      )  ->						str:
  if model.config.model_type == "gpt2":
    return model.transformer.h[0].mlp.c_fc
  return model.transformer.h[0].mlp.dense_ah_to_h


if is_torch_available():
     import torch
     import torch.nn as nn





     class  __UpperCAmelCase					(	nn.Module						):



        def __init__(							self							:					str,		__A							:					nn.Module,		__A							:					int						):
          super().__init__()
          UpperCAmelCase	:							Union[str, Any]						=			module
          UpperCAmelCase	:							List[str]						=			nn.Sequential(
              nn.Linear(module.in_features,		__A,		bias=__A						),		nn.Linear(__A,		module.out_features,		bias=__A						),		)
          UpperCAmelCase	:							Optional[int]						=			(2.0 / (5 * min(module.in_features,		module.out_features						))) ** 0.5
          nn.init.normal_(self.adapter[0].weight,		std=__A						)
          nn.init.zeros_(self.adapter[1].weight						)
          self.adapter.to(module.weight.device						)





        def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					Optional[Any],		*__A							:					Optional[int],		**__A							:					int						):
          return self.module(__A,		*__A,		**__A						) + self.adapter(__A						)





@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class  __UpperCAmelCase					(	unittest.TestCase						):
   # We keep the constants inside the init function and model loading inside setUp function

   # We need to test on relatively large models (aka >1b parameters otherwise the quantiztion may not work as expected)
   # Therefore here we use only bloom-1b3 to test our module
   UpperCamelCase       					=					"""bigscience/bloom-1b7"""

   # Constant values
   UpperCamelCase       					=					2.1_0_9_6_5_9_5_5_2_6_9_2_5_7_4

   UpperCamelCase       					=					"""Hello my name is"""
   UpperCamelCase       					=					set()
   EXPECTED_OUTPUTS.add("""Hello my name is John and I am a professional photographer. I"""						)
   EXPECTED_OUTPUTS.add("""Hello my name is John.\nI am a friend of your father.\n"""						)
   EXPECTED_OUTPUTS.add("""Hello my name is John Doe, I am a student at the University"""						)
   UpperCamelCase       					=					1_0



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     # Models and tokenizer
     UpperCAmelCase	:							List[str]						=			AutoTokenizer.from_pretrained(self.model_name						)






class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					Any						):
     super().setUp()

     # Models and tokenizer
     UpperCAmelCase	:							str						=			AutoModelForCausalLM.from_pretrained(
         self.model_name,		torch_dtype=torch.floataa,		device_map='''auto'''						)
     UpperCAmelCase	:							Union[str, Any]						=			AutoModelForCausalLM.from_pretrained(self.model_name,		load_in_abit=__A,		device_map='''auto'''						)



   def 	__magic_name__   (							self							:					Optional[int]						):
     del self.model_fpaa
     del self.model_abit

     gc.collect()
     torch.cuda.empty_cache()



   def 	__magic_name__   (							self							:					Tuple						):
     UpperCAmelCase	:							List[Any]						=			self.model_abit.config

     self.assertTrue(hasattr(__A,		'''quantization_config'''						)						)

     UpperCAmelCase	:							Optional[Any]						=			config.to_dict()
     UpperCAmelCase	:							Optional[int]						=			config.to_diff_dict()

     UpperCAmelCase	:							Optional[Any]						=			config.to_json_string()



   def 	__magic_name__   (							self							:					List[Any]						):
     from bitsandbytes.nn import Paramsabit

     UpperCAmelCase	:							int						=			self.model_fpaa.get_memory_footprint()
     UpperCAmelCase	:							List[str]						=			self.model_abit.get_memory_footprint()

     self.assertAlmostEqual(mem_fpaa / mem_abit,		self.EXPECTED_RELATIVE_DIFFERENCE						)
     UpperCAmelCase	:							str						=			get_some_linear_layer(self.model_abit						)
     self.assertTrue(linear.weight.__class__ == Paramsabit						)



   def 	__magic_name__   (							self							:					Dict						):
     from transformers import TaPreTrainedModel

     self.model_fpaa.get_memory_footprint()
     self.model_abit.get_memory_footprint()

     for name, module in self.model_abit.named_modules():
       if isinstance(__A,		torch.nn.Linear						):
         if name not in ["lm_head"] + TaPreTrainedModel._keep_in_fpaa_modules:
           # 4-bit parameters are packed in uint8 variables
           self.assertTrue(module.weight.dtype == torch.uinta						)



   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase	:							Tuple						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						)
     UpperCAmelCase	:							str						=			self.model_abit.generate(input_ids=encoded_input['''input_ids'''].to(0						),		max_new_tokens=1_0						)

     self.assertIn(self.tokenizer.decode(output_sequences[0],		skip_special_tokens=__A						),		self.EXPECTED_OUTPUTS						)



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Union[str, Any]						=			BitsAndBytesConfig()
     UpperCAmelCase	:							Union[str, Any]						=			True

     UpperCAmelCase	:							Optional[Any]						=			AutoModelForCausalLM.from_pretrained(
         self.model_name,		quantization_config=__A,		device_map='''auto'''						)

     UpperCAmelCase	:							Optional[Any]						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						)
     UpperCAmelCase	:							str						=			model_abit_from_config.generate(
         input_ids=encoded_input['''input_ids'''].to(0						),		max_new_tokens=1_0						)

     self.assertIn(self.tokenizer.decode(output_sequences[0],		skip_special_tokens=__A						),		self.EXPECTED_OUTPUTS						)



   def 	__magic_name__   (							self							:					List[str]						):
     with self.assertRaises(__A						), tempfile.TemporaryDirectory() as tmpdirname:
       self.model_abit.save_pretrained(__A						)



   def 	__magic_name__   (							self							:					Tuple						):
     UpperCAmelCase	:							Union[str, Any]						=			BitsAndBytesConfig()

     with self.assertRaises(__A						):
       UpperCAmelCase	:							int						=			AutoModelForCausalLM.from_pretrained(
           self.model_name,		quantization_config=__A,		load_in_abit=__A,		device_map='''auto''',		bnb_abit_quant_type='''nf4''',		)



   def 	__magic_name__   (							self							:					List[str]						):
     with self.assertRaises(__A						):
       # Tries with `str`
       self.model_abit.to('''cpu'''						)

     with self.assertRaises(__A						):
       # Tries with a `dtype``
       self.model_abit.to(torch.floataa						)

     with self.assertRaises(__A						):
       # Tries with a `device`
       self.model_abit.to(torch.device('''cuda:0'''						)						)

     with self.assertRaises(__A						):
       # Tries with a `device`
       self.model_abit.float()

     with self.assertRaises(__A						):
       # Tries with a `device`
       self.model_abit.half()

     # Test if we did not break anything
     UpperCAmelCase	:							Tuple						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						)

     UpperCAmelCase	:							Any						=			self.model_fpaa.to(torch.floataa						)
     UpperCAmelCase	:							Dict						=			self.model_fpaa.generate(input_ids=encoded_input['''input_ids'''].to(0						),		max_new_tokens=1_0						)

     # Check this does not throw an error
     UpperCAmelCase	:							Optional[Any]						=			self.model_fpaa.to('''cpu'''						)

     # Check this does not throw an error
     UpperCAmelCase	:							Optional[int]						=			self.model_fpaa.half()

     # Check this does not throw an error
     UpperCAmelCase	:							List[str]						=			self.model_fpaa.float()





   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							str						=			AutoModelForSeqaSeqLM.from_pretrained('''t5-small''',		load_in_abit=__A,		device_map='''auto'''						)
     self.assertTrue(model.decoder.block[0].layer[2].DenseReluDense.wo.weight.dtype == torch.floataa						)






@require_bitsandbytes
@require_accelerate
@require_torch
@require_torch_gpu
@slow
class  __UpperCAmelCase					(	unittest.TestCase						):



   @classmethod
   def 	__magic_name__   (							cls							:					Tuple						):
     UpperCAmelCase	:							Union[str, Any]						=			'''t5-small'''
     UpperCAmelCase	:							Optional[int]						=			'''google/flan-t5-small'''  # flan-t5 uses dense-act instead of dense-relu-dense
     UpperCAmelCase	:							List[Any]						=			AutoTokenizer.from_pretrained(cls.model_name						)
     UpperCAmelCase	:							Optional[int]						=			'''Translate in German: Hello, my dog is cute'''



   def 	__magic_name__   (							self							:					List[str]						):
     gc.collect()
     torch.cuda.empty_cache()



   def 	__magic_name__   (							self							:					int						):
     from transformers import TaForConditionalGeneration

     UpperCAmelCase	:							str						=			TaForConditionalGeneration._keep_in_fpaa_modules
     UpperCAmelCase	:							Dict						=			None

     # test with `t5-small`
     UpperCAmelCase	:							Union[str, Any]						=			TaForConditionalGeneration.from_pretrained(self.model_name,		load_in_abit=__A,		device_map='''auto'''						)
     UpperCAmelCase	:							Any						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						).to(0						)
     UpperCAmelCase	:							Dict						=			model.generate(**__A						)

     # test with `flan-t5-small`
     UpperCAmelCase	:							List[str]						=			TaForConditionalGeneration.from_pretrained(
         self.dense_act_model_name,		load_in_abit=__A,		device_map='''auto'''						)
     UpperCAmelCase	:							Optional[int]						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						).to(0						)
     UpperCAmelCase	:							Tuple						=			model.generate(**__A						)
     UpperCAmelCase	:							Dict						=			modules





   def 	__magic_name__   (							self							:					List[str]						):
     import bitsandbytes as bnb

     from transformers import TaForConditionalGeneration

     # test with `t5-small`
     UpperCAmelCase	:							Tuple						=			TaForConditionalGeneration.from_pretrained(self.model_name,		load_in_abit=__A,		device_map='''auto'''						)

     # there was a bug with decoders - this test checks that it is fixed
     self.assertTrue(isinstance(model.decoder.block[0].layer[0].SelfAttention.q,		bnb.nn.Linearabit						)						)

     UpperCAmelCase	:							int						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						).to(0						)
     UpperCAmelCase	:							Optional[int]						=			model.generate(**__A						)

     # test with `flan-t5-small`
     UpperCAmelCase	:							Optional[int]						=			TaForConditionalGeneration.from_pretrained(
         self.dense_act_model_name,		load_in_abit=__A,		device_map='''auto'''						)
     UpperCAmelCase	:							int						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						).to(0						)
     UpperCAmelCase	:							List[str]						=			model.generate(**__A						)






class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     super().setUp()
     # model_name
     UpperCAmelCase	:							Optional[int]						=			'''bigscience/bloom-560m'''
     UpperCAmelCase	:							int						=			'''t5-small'''

     # Different types of model

     UpperCAmelCase	:							Tuple						=			AutoModel.from_pretrained(self.model_name,		load_in_abit=__A,		device_map='''auto'''						)
     # Sequence classification model
     UpperCAmelCase	:							Tuple						=			AutoModelForSequenceClassification.from_pretrained(
         self.model_name,		load_in_abit=__A,		device_map='''auto'''						)
     # CausalLM model
     UpperCAmelCase	:							Tuple						=			AutoModelForCausalLM.from_pretrained(self.model_name,		load_in_abit=__A,		device_map='''auto'''						)
     # Seq2seq model
     UpperCAmelCase	:							Optional[int]						=			AutoModelForSeqaSeqLM.from_pretrained(
         self.seq_to_seq_name,		load_in_abit=__A,		device_map='''auto'''						)



   def 	__magic_name__   (							self							:					Any						):
     del self.base_model
     del self.sequence_model
     del self.model_abit
     del self.seq_to_seq_model

     gc.collect()
     torch.cuda.empty_cache()





   def 	__magic_name__   (							self							:					Dict						):
     from bitsandbytes.nn import Paramsabit

     self.assertTrue(self.base_model.h[-1].mlp.dense_ah_to_h.weight.__class__ == Paramsabit						)

     # Other heads should be nn.Parameter
     self.assertTrue(self.model_abit.lm_head.weight.__class__ == torch.nn.Parameter						)
     self.assertTrue(self.sequence_model.score.weight.__class__ == torch.nn.Parameter						)
     self.assertTrue(self.seq_to_seq_model.lm_head.weight.__class__ == torch.nn.Parameter						)






class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					Optional[int]						):
     super().setUp()



   def 	__magic_name__   (							self							:					Any						):
     del self.pipe

     gc.collect()
     torch.cuda.empty_cache()





   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Dict						=			pipeline(
         '''text-generation''',		model=self.model_name,		model_kwargs={'''device_map''': '''auto''', '''load_in_4bit''': True, '''torch_dtype''': torch.floataa},		max_new_tokens=self.MAX_NEW_TOKENS,		)

     # Real second forward pass
     UpperCAmelCase	:							Dict						=			self.pipe(self.input_text						)
     self.assertIn(pipeline_output[0]['''generated_text'''],		self.EXPECTED_OUTPUTS						)






@require_torch_multi_gpu
class  __UpperCAmelCase					(	lowerCamelCase__						):



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





   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase	:							str						=			AutoModelForCausalLM.from_pretrained(
         self.model_name,		load_in_abit=__A,		device_map='''balanced'''						)

     # Check correct device map
     self.assertEqual(set(model_parallel.hf_device_map.values()						),		{0, 1}						)

     # Check that inference pass works on the model
     UpperCAmelCase	:							Optional[int]						=			self.tokenizer(self.input_text,		return_tensors='''pt'''						)

     # Second real batch
     UpperCAmelCase	:							Union[str, Any]						=			model_parallel.generate(input_ids=encoded_input['''input_ids'''].to(0						),		max_new_tokens=1_0						)
     self.assertIn(self.tokenizer.decode(output_parallel[0],		skip_special_tokens=__A						),		self.EXPECTED_OUTPUTS						)






class  __UpperCAmelCase					(	lowerCamelCase__						):



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							Union[str, Any]						=			'''facebook/opt-350m'''
     super().setUp()





   def 	__magic_name__   (							self							:					List[str]						):
     if version.parse(importlib.metadata.version('''bitsandbytes'''						)						) < version.parse('''0.37.0'''						):
       return

     # Step 1: freeze all parameters
     UpperCAmelCase	:							Optional[Any]						=			AutoModelForCausalLM.from_pretrained(self.model_name,		load_in_abit=__A						)

     self.assertEqual(set(model.hf_device_map.values()						),		{torch.cuda.current_device()}						)

     for param in model.parameters():
       UpperCAmelCase	:							Optional[int]						=			False  # freeze the model - train adapters later
       if param.ndim == 1:
         # cast the small parameters (e.g. layernorm) to fp32 for stability
         UpperCAmelCase	:							Union[str, Any]						=			param.data.to(torch.floataa						)

        # Step 2: add adapters
     for _, module in model.named_modules():
       if "OPTAttention" in repr(type(__A						)						):
         UpperCAmelCase	:							Dict						=			LoRALayer(module.q_proj,		rank=1_6						)
         UpperCAmelCase	:							Dict						=			LoRALayer(module.k_proj,		rank=1_6						)
         UpperCAmelCase	:							List[str]						=			LoRALayer(module.v_proj,		rank=1_6						)

        # Step 3: dummy batch
     UpperCAmelCase	:							List[str]						=			self.tokenizer('''Test batch ''',		return_tensors='''pt'''						).to(0						)

     # Step 4: Check if the gradient is not None
     with torch.cuda.amp.autocast():
       UpperCAmelCase	:							Optional[int]						=			model.forward(**__A						)
       out.logits.norm().backward()

     for module in model.modules():
       if isinstance(__A,		__A						):
         self.assertTrue(module.adapter[1].weight.grad is not None						)
         self.assertTrue(module.adapter[1].weight.grad.norm().item() > 0						)
       elif isinstance(__A,		nn.Embedding						):
         self.assertTrue(module.weight.grad is None						)






class  __UpperCAmelCase					(	lowerCamelCase__						):
   UpperCamelCase       					=					"""gpt2-xl"""
   UpperCamelCase       					=					3.3_1_9_1_8_5_4_8_5_4_1_5_2_1_8_7








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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import ChineseCLIPImageProcessor





class  __UpperCAmelCase					(	unittest.TestCase						):



   def __init__(							self							:					List[Any],		__A							:					str,		__A							:					List[str]=7,		__A							:					List[str]=3,		__A							:					Optional[int]=1_8,		__A							:					List[Any]=3_0,		__A							:					Tuple=4_0_0,		__A							:					Tuple=True,		__A							:					List[Any]=None,		__A							:					str=True,		__A							:					int=None,		__A							:					Optional[Any]=True,		__A							:					List[Any]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],		__A							:					List[str]=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],		__A							:					Tuple=True,		):
     UpperCAmelCase	:							int						=			size if size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
     UpperCAmelCase	:							Optional[Any]						=			crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
     UpperCAmelCase	:							int						=			parent
     UpperCAmelCase	:							Union[str, Any]						=			batch_size
     UpperCAmelCase	:							List[Any]						=			num_channels
     UpperCAmelCase	:							Dict						=			image_size
     UpperCAmelCase	:							List[str]						=			min_resolution
     UpperCAmelCase	:							Optional[Any]						=			max_resolution
     UpperCAmelCase	:							Union[str, Any]						=			do_resize
     UpperCAmelCase	:							Dict						=			size
     UpperCAmelCase	:							Any						=			do_center_crop
     UpperCAmelCase	:							Union[str, Any]						=			crop_size
     UpperCAmelCase	:							List[str]						=			do_normalize
     UpperCAmelCase	:							Optional[Any]						=			image_mean
     UpperCAmelCase	:							Optional[Any]						=			image_std
     UpperCAmelCase	:							List[Any]						=			do_convert_rgb



   def 	__magic_name__   (							self							:					int						):
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_convert_rgb": self.do_convert_rgb,
     }





   def 	__magic_name__   (							self							:					Optional[Any],		__A							:					Any=False,		__A							:					str=False,		__A							:					List[Any]=False						):
     assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

     if equal_resolution:
       UpperCAmelCase	:							Dict						=			[]
       for i in range(self.batch_size						):
         image_inputs.append(
             np.random.randint(
                 2_5_5,		size=(self.num_channels, self.max_resolution, self.max_resolution),		dtype=np.uinta						)						)
     else:
       UpperCAmelCase	:							Tuple						=			[]
       for i in range(self.batch_size						):
         UpperCAmelCase			,						UpperCAmelCase	:							Tuple						=			np.random.choice(np.arange(self.min_resolution,		self.max_resolution						),		2						)
         image_inputs.append(np.random.randint(2_5_5,		size=(self.num_channels, width, height),		dtype=np.uinta						)						)

     if not numpify and not torchify:
       # PIL expects the channel dimension as last dimension
       UpperCAmelCase	:							Optional[int]						=			[Image.fromarray(np.moveaxis(__A,		0,		-1						)						) for x in image_inputs]

     if torchify:
       UpperCAmelCase	:							str						=			[torch.from_numpy(__A						) for x in image_inputs]

     return image_inputs






@require_torch
@require_vision
class  __UpperCAmelCase					(	lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					ChineseCLIPImageProcessor if is_vision_available() else None



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							Optional[Any]						=			ChineseCLIPImageProcessingTester(self,		do_center_crop=__A						)



   @property
   def 	__magic_name__   (							self							:					Tuple						):
     return self.image_processor_tester.prepare_image_processor_dict()



   def 	__magic_name__   (							self							:					Any						):
     UpperCAmelCase	:							List[Any]						=			self.image_processing_class(**self.image_processor_dict						)
     self.assertTrue(hasattr(__A,		'''do_resize'''						)						)
     self.assertTrue(hasattr(__A,		'''size'''						)						)
     self.assertTrue(hasattr(__A,		'''do_center_crop'''						)						)
     self.assertTrue(hasattr(__A,		'''center_crop'''						)						)
     self.assertTrue(hasattr(__A,		'''do_normalize'''						)						)
     self.assertTrue(hasattr(__A,		'''image_mean'''						)						)
     self.assertTrue(hasattr(__A,		'''image_std'''						)						)
     self.assertTrue(hasattr(__A,		'''do_convert_rgb'''						)						)



   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase	:							Union[str, Any]						=			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''': 1_8, '''width''': 1_8}						)

     UpperCAmelCase	:							int						=			self.image_processing_class.from_dict(self.image_processor_dict,		size=4_2,		crop_size=8_4						)
     self.assertEqual(image_processor.size,		{'''shortest_edge''': 4_2}						)
     self.assertEqual(image_processor.crop_size,		{'''height''': 8_4, '''width''': 8_4}						)



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



   def 	__magic_name__   (							self							:					Tuple						):
     # Initialize image_processing
     UpperCAmelCase	:							Dict						=			self.image_processing_class(**self.image_processor_dict						)
     # create random PIL images
     UpperCAmelCase	:							Optional[int]						=			self.image_processor_tester.prepare_inputs(equal_resolution=__A						)
     for image in image_inputs:
       self.assertIsInstance(__A,		Image.Image						)

     # Test not batched input
     UpperCAmelCase	:							str						=			image_processing(image_inputs[0],		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)

     # Test batched
     UpperCAmelCase	:							Any						=			image_processing(__A,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)



   def 	__magic_name__   (							self							:					Optional[Any]						):
     # Initialize image_processing
     UpperCAmelCase	:							List[str]						=			self.image_processing_class(**self.image_processor_dict						)
     # create random numpy tensors
     UpperCAmelCase	:							List[Any]						=			self.image_processor_tester.prepare_inputs(equal_resolution=__A,		numpify=__A						)
     for image in image_inputs:
       self.assertIsInstance(__A,		np.ndarray						)

     # Test not batched input
     UpperCAmelCase	:							List[str]						=			image_processing(image_inputs[0],		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)

     # Test batched
     UpperCAmelCase	:							Dict						=			image_processing(__A,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)





   def 	__magic_name__   (							self							:					Any						):
     # Initialize image_processing
     UpperCAmelCase	:							str						=			self.image_processing_class(**self.image_processor_dict						)
     # create random PyTorch tensors
     UpperCAmelCase	:							Optional[Any]						=			self.image_processor_tester.prepare_inputs(equal_resolution=__A,		torchify=__A						)
     for image in image_inputs:
       self.assertIsInstance(__A,		torch.Tensor						)

     # Test not batched input
     UpperCAmelCase	:							str						=			image_processing(image_inputs[0],		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)

     # Test batched
     UpperCAmelCase	:							List[str]						=			image_processing(__A,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)






@require_torch
@require_vision
class  __UpperCAmelCase					(	lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					ChineseCLIPImageProcessor if is_vision_available() else None



   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase	:							str						=			ChineseCLIPImageProcessingTester(self,		num_channels=4,		do_center_crop=__A						)
     UpperCAmelCase	:							Dict						=			3



   @property
   def 	__magic_name__   (							self							:					List[Any]						):
     return self.image_processor_tester.prepare_image_processor_dict()



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase	:							List[Any]						=			self.image_processing_class(**self.image_processor_dict						)
     self.assertTrue(hasattr(__A,		'''do_resize'''						)						)
     self.assertTrue(hasattr(__A,		'''size'''						)						)
     self.assertTrue(hasattr(__A,		'''do_center_crop'''						)						)
     self.assertTrue(hasattr(__A,		'''center_crop'''						)						)
     self.assertTrue(hasattr(__A,		'''do_normalize'''						)						)
     self.assertTrue(hasattr(__A,		'''image_mean'''						)						)
     self.assertTrue(hasattr(__A,		'''image_std'''						)						)
     self.assertTrue(hasattr(__A,		'''do_convert_rgb'''						)						)



   def 	__magic_name__   (							self							:					List[Any]						):
     pass





   def 	__magic_name__   (							self							:					Tuple						):
     # Initialize image_processing
     UpperCAmelCase	:							Optional[Any]						=			self.image_processing_class(**self.image_processor_dict						)
     # create random PIL images
     UpperCAmelCase	:							Dict						=			self.image_processor_tester.prepare_inputs(equal_resolution=__A						)
     for image in image_inputs:
       self.assertIsInstance(__A,		Image.Image						)

     # Test not batched input
     UpperCAmelCase	:							Tuple						=			image_processing(image_inputs[0],		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             1,
             self.expected_encoded_image_num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)

     # Test batched
     UpperCAmelCase	:							Tuple						=			image_processing(__A,		return_tensors='''pt'''						).pixel_values
     self.assertEqual(
         encoded_images.shape,		(
             self.image_processor_tester.batch_size,
             self.expected_encoded_image_num_channels,
             self.image_processor_tester.crop_size['''height'''],
             self.image_processor_tester.crop_size['''width'''],
         ),		)





