






import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor


@require_tokenizers
@require_vision
class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



         __A         =	tempfile.mkdtemp()

         # fmt: off
         __A         =	["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
         # fmt: on
         __A         =	os.path.join(self.tmpdirname ,	VOCAB_FILES_NAMES["""vocab_file"""]				)
         with open(self.vocab_file ,	"""w""" ,	encoding="""utf-8"""				) as vocab_writer:
             vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]				)				)

         __A         =	{
             """do_resize""": True,
             """size""": {"""height""": 18, """width""": 18},
             """do_normalize""": True,
             """image_mean""": [0.5, 0.5, 0.5],
             """image_std""": [0.5, 0.5, 0.5],
         }
         __A         =	os.path.join(self.tmpdirname ,	UpperCamelCase_				)
         with open(self.image_processor_file ,	"""w""" ,	encoding="""utf-8"""				) as fp:
             json.dump(UpperCamelCase_ ,	UpperCamelCase_				)




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


         """simple docstring"""



         return BertTokenizer.from_pretrained(self.tmpdirname ,	**UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			int ,	**UpperCamelCase_       :			Any				):


         """simple docstring"""



         return ViTImageProcessor.from_pretrained(self.tmpdirname ,	**UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



         shutil.rmtree(self.tmpdirname				)




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



         __A         =	[np.random.randint(255 ,	size=(3, 30, 400) ,	dtype=np.uinta				)]

         __A         =	[Image.fromarray(np.moveaxis(UpperCamelCase_ ,	0 ,	-1				)				) for x in image_inputs]

         return image_inputs




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


         """simple docstring"""



         __A         =	self.get_tokenizer()
         __A         =	self.get_image_processor()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         processor.save_pretrained(self.tmpdirname				)
         __A         =	VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname				)

         self.assertEqual(processor.tokenizer.get_vocab() ,	tokenizer.get_vocab()				)
         self.assertIsInstance(processor.tokenizer ,	(BertTokenizer, BertTokenizerFast)				)

         self.assertEqual(processor.image_processor.to_json_string() ,	image_processor.to_json_string()				)
         self.assertIsInstance(processor.image_processor ,	UpperCamelCase_				)




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A         =	VisionTextDualEncoderProcessor(
             tokenizer=self.get_tokenizer() ,	image_processor=self.get_image_processor()				)
         processor.save_pretrained(self.tmpdirname				)

         __A         =	self.get_tokenizer(bos_token="""(BOS)""" ,	eos_token="""(EOS)"""				)
         __A         =	self.get_image_processor(do_normalize=UpperCamelCase_ ,	padding_value=1.0				)

         __A         =	VisionTextDualEncoderProcessor.from_pretrained(
             self.tmpdirname ,	bos_token="""(BOS)""" ,	eos_token="""(EOS)""" ,	do_normalize=UpperCamelCase_ ,	padding_value=1.0				)

         self.assertEqual(processor.tokenizer.get_vocab() ,	tokenizer_add_kwargs.get_vocab()				)
         self.assertIsInstance(processor.tokenizer ,	(BertTokenizer, BertTokenizerFast)				)

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




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



         __A         =	self.get_image_processor()
         __A         =	self.get_tokenizer()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         __A         =	self.prepare_image_inputs()

         __A         =	image_processor(UpperCamelCase_ ,	return_tensors="""np"""				)
         __A         =	processor(images=UpperCamelCase_ ,	return_tensors="""np"""				)

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




     def 		lowerCAmelCase_     (				self       :			Tuple				):


         """simple docstring"""



         __A         =	self.get_image_processor()
         __A         =	self.get_tokenizer()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         __A         =	"""lower newer"""

         __A         =	processor(text=UpperCamelCase_				)

         __A         =	tokenizer(UpperCamelCase_				)

         for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key] ,	encoded_processor[key]				)




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



         __A         =	self.get_image_processor()
         __A         =	self.get_tokenizer()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         __A         =	"""lower newer"""
         __A         =	self.prepare_image_inputs()

         __A         =	processor(text=UpperCamelCase_ ,	images=UpperCamelCase_				)

         self.assertListEqual(list(inputs.keys()				) ,	["""input_ids""", """token_type_ids""", """attention_mask""", """pixel_values"""]				)

         # test if it raises when no input is passed
         with self.assertRaises(UpperCamelCase_				):
             processor()




     def 		lowerCAmelCase_     (				self       :			str				):


         """simple docstring"""



         __A         =	self.get_image_processor()
         __A         =	self.get_tokenizer()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         __A         =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         __A         =	processor.batch_decode(UpperCamelCase_				)
         __A         =	tokenizer.batch_decode(UpperCamelCase_				)

         self.assertListEqual(UpperCamelCase_ ,	UpperCamelCase_				)





     def 		lowerCAmelCase_     (				self       :			int				):


         """simple docstring"""



         __A         =	self.get_image_processor()
         __A         =	self.get_tokenizer()

         __A         =	VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase_ ,	image_processor=UpperCamelCase_				)

         __A         =	"""lower newer"""
         __A         =	self.prepare_image_inputs()

         __A         =	processor(text=UpperCamelCase_ ,	images=UpperCamelCase_				)

         self.assertListEqual(list(inputs.keys()				) ,	processor.model_input_names				)












from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer


@dataclass
class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_			,      lowercase_  ):







     '''simple docstring'''




     @register_to_config
     def __init__(				self       :			List[Any] ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			Tuple[str] = ("DownEncoderBlock2D",) ,	UpperCamelCase_       :			Tuple[str] = ("UpDecoderBlock2D",) ,	UpperCamelCase_       :			Tuple[int] = (64,) ,	UpperCamelCase_       :			int = 1 ,	UpperCamelCase_       :			str = "silu" ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			int = 32 ,	UpperCamelCase_       :			int = 256 ,	UpperCamelCase_       :			int = 32 ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			float = 0.18215 ,	UpperCamelCase_       :			str = "group" ,	):


         """simple docstring"""



         super().__init__()

         # pass init params to Encoder
         __A         =	Encoder(
             in_channels=UpperCamelCase_ ,	out_channels=UpperCamelCase_ ,	down_block_types=UpperCamelCase_ ,	block_out_channels=UpperCamelCase_ ,	layers_per_block=UpperCamelCase_ ,	act_fn=UpperCamelCase_ ,	norm_num_groups=UpperCamelCase_ ,	double_z=UpperCamelCase_ ,	)

         __A         =	vq_embed_dim if vq_embed_dim is not None else latent_channels

         __A         =	nn.Convad(UpperCamelCase_ ,	UpperCamelCase_ ,	1				)
         __A         =	VectorQuantizer(UpperCamelCase_ ,	UpperCamelCase_ ,	beta=0.25 ,	remap=UpperCamelCase_ ,	sane_index_shape=UpperCamelCase_				)
         __A         =	nn.Convad(UpperCamelCase_ ,	UpperCamelCase_ ,	1				)

         # pass init params to Decoder
         __A         =	Decoder(
             in_channels=UpperCamelCase_ ,	out_channels=UpperCamelCase_ ,	up_block_types=UpperCamelCase_ ,	block_out_channels=UpperCamelCase_ ,	layers_per_block=UpperCamelCase_ ,	act_fn=UpperCamelCase_ ,	norm_num_groups=UpperCamelCase_ ,	norm_type=UpperCamelCase_ ,	)




     @apply_forward_hook
     def 		lowerCAmelCase_     (				self       :			List[str] ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         __A         =	self.encoder(UpperCamelCase_				)
         __A         =	self.quant_conv(UpperCamelCase_				)

         if not return_dict:
             return (h,)

         return VQEncoderOutput(latents=UpperCamelCase_				)




     @apply_forward_hook
     def 		lowerCAmelCase_     (				self       :			List[str] ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         if not force_not_quantize:
             __A    ,	__A    ,	__A         =	self.quantize(UpperCamelCase_				)
         else:
             __A         =	h
         __A         =	self.post_quant_conv(UpperCamelCase_				)
         __A         =	self.decoder(UpperCamelCase_ ,	quant if self.config.norm_type == """spatial""" else None				)

         if not return_dict:
             return (dec,)

         return DecoderOutput(sample=UpperCamelCase_				)





     def 		lowerCAmelCase_     (				self       :			Any ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         __A         =	sample
         __A         =	self.encode(UpperCamelCase_				).latents
         __A         =	self.decode(UpperCamelCase_				).sample

         if not return_dict:
             return (dec,)

         return DecoderOutput(sample=UpperCamelCase_				)




