

'''simple docstring'''



import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import CLIPImageProcessor, CLIPProcessor



@require_vision
class lowerCamelCase			(  unittest.TestCase						):



			'''simple docstring'''






			def lowercase__						(				self	:						Union[str, Any]			)      ->		int:





									'''simple docstring'''
									A__					:       int     =tempfile.mkdtemp()

									# fmt: off
									A__					:       Optional[Any]     =["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
									# fmt: on
									A__					:       Tuple     =dict(zip(lowerCAmelCase_		,      range(len(lowerCAmelCase_			)			)			)			)
									A__					:       List[str]     =["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
									A__					:       Tuple     ={"""unk_token""": """<unk>"""}

									A__					:       List[Any]     =os.path.join(self.tmpdirname		,      VOCAB_FILES_NAMES["""vocab_file"""]			)
									A__					:       List[str]     =os.path.join(self.tmpdirname		,      VOCAB_FILES_NAMES["""merges_file"""]			)
									with open(self.vocab_file		,      """w"""		,      encoding="""utf-8"""			) as fp:
															fp.write(json.dumps(lowerCAmelCase_			) + """\n"""			)
									with open(self.merges_file		,      """w"""		,      encoding="""utf-8"""			) as fp:
															fp.write("""\n""".join(lowerCAmelCase_			)			)

									A__					:       List[Any]     ={
									    """do_resize""": True,
									    """size""": 20,
									    """do_center_crop""": True,
									    """crop_size""": 18,
									    """do_normalize""": True,
									    """image_mean""": [0.48145466, 0.4578275, 0.40821073],
									    """image_std""": [0.26862954, 0.26130258, 0.27577711],
									}
									A__					:       List[str]     =os.path.join(self.tmpdirname		,      lowerCAmelCase_			)
									with open(self.image_processor_file		,      """w"""		,      encoding="""utf-8"""			) as fp:
															json.dump(lowerCAmelCase_		,      lowerCAmelCase_			)


			def lowercase__						(				self	:						Tuple		,      **lowerCAmelCase_	:						Tuple			)      ->		Tuple:





									'''simple docstring'''
									return CLIPTokenizer.from_pretrained(self.tmpdirname		,      **lowerCAmelCase_			)


			def lowercase__						(				self	:						Tuple		,      **lowerCAmelCase_	:						Tuple			)      ->		int:





									'''simple docstring'''
									return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,      **lowerCAmelCase_			)


			def lowercase__						(				self	:						Dict		,      **lowerCAmelCase_	:						int			)      ->		Optional[int]:





									'''simple docstring'''
									return CLIPImageProcessor.from_pretrained(self.tmpdirname		,      **lowerCAmelCase_			)


			def lowercase__						(				self	:						str			)      ->		int:





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


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





									'''simple docstring'''
									A__					:       Dict     =[np.random.randint(2_55		,      size=(3, 30, 4_00)		,      dtype=np.uinta			)]

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

									return image_inputs


			def lowercase__						(				self	:						Any			)      ->		str:





									'''simple docstring'''
									A__					:       Optional[int]     =self.get_tokenizer()
									A__					:       List[Any]     =self.get_rust_tokenizer()
									A__					:       Optional[int]     =self.get_image_processor()

									A__					:       Dict     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)
									processor_slow.save_pretrained(self.tmpdirname			)
									A__					:       List[str]     =CLIPProcessor.from_pretrained(self.tmpdirname		,      use_fast=lowerCAmelCase_			)

									A__					:       Tuple     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)
									processor_fast.save_pretrained(self.tmpdirname			)
									A__					:       Dict     =CLIPProcessor.from_pretrained(self.tmpdirname			)

									self.assertEqual(processor_slow.tokenizer.get_vocab()		,      tokenizer_slow.get_vocab()			)
									self.assertEqual(processor_fast.tokenizer.get_vocab()		,      tokenizer_fast.get_vocab()			)
									self.assertEqual(tokenizer_slow.get_vocab()		,      tokenizer_fast.get_vocab()			)
									self.assertIsInstance(processor_slow.tokenizer		,      lowerCAmelCase_			)
									self.assertIsInstance(processor_fast.tokenizer		,      lowerCAmelCase_			)

									self.assertEqual(processor_slow.image_processor.to_json_string()		,      image_processor.to_json_string()			)
									self.assertEqual(processor_fast.image_processor.to_json_string()		,      image_processor.to_json_string()			)
									self.assertIsInstance(processor_slow.image_processor		,      lowerCAmelCase_			)
									self.assertIsInstance(processor_fast.image_processor		,      lowerCAmelCase_			)


			def lowercase__						(				self	:						Any			)      ->		Any:





									'''simple docstring'''
									A__					:       List[str]     =CLIPProcessor(tokenizer=self.get_tokenizer()		,      image_processor=self.get_image_processor()			)
									processor.save_pretrained(self.tmpdirname			)

									A__					:       str     =self.get_tokenizer(bos_token="""(BOS)"""		,      eos_token="""(EOS)"""			)
									A__					:       Tuple     =self.get_image_processor(do_normalize=lowerCAmelCase_		,      padding_value=1.0			)

									A__					:       str     =CLIPProcessor.from_pretrained(
									    self.tmpdirname		,      bos_token="""(BOS)"""		,      eos_token="""(EOS)"""		,      do_normalize=lowerCAmelCase_		,      padding_value=1.0			)

									self.assertEqual(processor.tokenizer.get_vocab()		,      tokenizer_add_kwargs.get_vocab()			)
									self.assertIsInstance(processor.tokenizer		,      lowerCAmelCase_			)

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


			def lowercase__						(				self	:						Any			)      ->		Any:





									'''simple docstring'''
									A__					:       Optional[Any]     =self.get_image_processor()
									A__					:       str     =self.get_tokenizer()

									A__					:       Any     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)

									A__					:       Dict     =self.prepare_image_inputs()

									A__					:       List[Any]     =image_processor(lowerCAmelCase_		,      return_tensors="""np"""			)
									A__					:       Dict     =processor(images=lowerCAmelCase_		,      return_tensors="""np"""			)

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


			def lowercase__						(				self	:						Union[str, Any]			)      ->		List[Any]:





									'''simple docstring'''
									A__					:       int     =self.get_image_processor()
									A__					:       List[str]     =self.get_tokenizer()

									A__					:       Optional[Any]     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)

									A__					:       Union[str, Any]     ="""lower newer"""

									A__					:       Dict     =processor(text=lowerCAmelCase_			)

									A__					:       Any     =tokenizer(lowerCAmelCase_			)

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


			def lowercase__						(				self	:						Tuple			)      ->		Optional[int]:





									'''simple docstring'''
									A__					:       Optional[int]     =self.get_image_processor()
									A__					:       Tuple     =self.get_tokenizer()

									A__					:       List[str]     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)

									A__					:       Dict     ="""lower newer"""
									A__					:       Optional[Any]     =self.prepare_image_inputs()

									A__					:       Union[str, Any]     =processor(text=lowerCAmelCase_		,      images=lowerCAmelCase_			)

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

									# test if it raises when no input is passed
									with pytest.raises(lowerCAmelCase_			):
															processor()


			def lowercase__						(				self	:						Dict			)      ->		Dict:





									'''simple docstring'''
									A__					:       List[str]     =self.get_image_processor()
									A__					:       Optional[Any]     =self.get_tokenizer()

									A__					:       Optional[Any]     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)

									A__					:       Optional[Any]     =[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									A__					:       List[str]     =processor.batch_decode(lowerCAmelCase_			)
									A__					:       Any     =tokenizer.batch_decode(lowerCAmelCase_			)

									self.assertListEqual(lowerCAmelCase_		,      lowerCAmelCase_			)



			def lowercase__						(				self	:						Any			)      ->		str:





									'''simple docstring'''
									A__					:       str     =self.get_image_processor()
									A__					:       List[str]     =self.get_tokenizer()

									A__					:       List[str]     =CLIPProcessor(tokenizer=lowerCAmelCase_		,      image_processor=lowerCAmelCase_			)

									A__					:       str     ="""lower newer"""
									A__					:       int     =self.prepare_image_inputs()

									A__					:       Dict     =processor(text=lowerCAmelCase_		,      images=lowerCAmelCase_			)

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




'''simple docstring'''



import torch
from torch import nn



class lowerCamelCase			(  nn.Module						):



			'''simple docstring'''






			def __init__(				self	:						Any		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						List[Any]		,      lowerCAmelCase_	:						Dict		,      lowerCAmelCase_	:						str		,      lowerCAmelCase_	:						Optional[int]=1		,      lowerCAmelCase_	:						str=False			)      ->		List[str]:





									'''simple docstring'''
									super().__init__()

									A__					:       Any     =n_token
									A__					:       int     =d_embed
									A__					:       Any     =d_proj

									A__					:       Tuple     =cutoffs + [n_token]
									A__					:       Optional[Any]     =[0] + self.cutoffs
									A__					:       Dict     =div_val

									A__					:       str     =self.cutoffs[0]
									A__					:       Optional[Any]     =len(self.cutoffs			) - 1
									A__					:       List[Any]     =self.shortlist_size + self.n_clusters

									if self.n_clusters > 0:
															A__					:       Any     =nn.Parameter(torch.zeros(self.n_clusters		,      self.d_embed			)			)
															A__					:       str     =nn.Parameter(torch.zeros(self.n_clusters			)			)

									A__					:       Union[str, Any]     =nn.ModuleList()
									A__					:       Optional[int]     =nn.ParameterList()

									if div_val == 1:
															for i in range(len(self.cutoffs			)			):
																					if d_proj != d_embed:
																											self.out_projs.append(nn.Parameter(torch.FloatTensor(lowerCAmelCase_		,      lowerCAmelCase_			)			)			)
																					else:
																											self.out_projs.append(lowerCAmelCase_			)

															self.out_layers.append(nn.Linear(lowerCAmelCase_		,      lowerCAmelCase_			)			)
									else:
															for i in range(len(self.cutoffs			)			):
																					A__   ,						A__					:       Optional[int]     =self.cutoff_ends[i], self.cutoff_ends[i + 1]
																					A__					:       Tuple     =d_embed // (div_val**i)

																					self.out_projs.append(nn.Parameter(torch.FloatTensor(lowerCAmelCase_		,      lowerCAmelCase_			)			)			)

																					self.out_layers.append(nn.Linear(lowerCAmelCase_		,      r_idx - l_idx			)			)

									A__					:       Optional[int]     =keep_order


			def lowercase__						(				self	:						Dict		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						Tuple		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Any			)      ->		Union[str, Any]:





									'''simple docstring'''
									if proj is None:
															A__					:       Optional[int]     =nn.functional.linear(lowerCAmelCase_		,      lowerCAmelCase_		,      bias=lowerCAmelCase_			)
									else:
															# if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
															A__					:       Optional[int]     =nn.functional.linear(lowerCAmelCase_		,      proj.t().contiguous()			)
															A__					:       Union[str, Any]     =nn.functional.linear(lowerCAmelCase_		,      lowerCAmelCase_		,      bias=lowerCAmelCase_			)
															# else:
															#     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
															#     if bias is not None:
															#         logit = logit + bias

									return logit


			def lowercase__						(				self	:						Tuple		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						List[Any]=None		,      lowerCAmelCase_	:						Dict=False			)      ->		Optional[int]:





									'''simple docstring'''

									if labels is not None:
															# Shift so that tokens < n predict n
															A__					:       Optional[Any]     =hidden[..., :-1, :].contiguous()
															A__					:       List[Any]     =labels[..., 1:].contiguous()
															A__					:       Optional[int]     =hidden.view(-1		,      hidden.size(-1			)			)
															A__					:       str     =labels.view(-1			)
															if hidden.size(0			) != labels.size(0			):
																					raise RuntimeError("""Input and labels should have the same size in the batch dimension."""			)
									else:
															A__					:       Optional[int]     =hidden.view(-1		,      hidden.size(-1			)			)

									if self.n_clusters == 0:
															A__					:       Optional[Any]     =self._compute_logit(lowerCAmelCase_		,      self.out_layers[0].weight		,      self.out_layers[0].bias		,      self.out_projs[0]			)
															if labels is not None:
																					A__					:       Tuple     =labels != -1_00
																					A__					:       int     =torch.zeros_like(lowerCAmelCase_		,      dtype=hidden.dtype		,      device=hidden.device			)
																					A__					:       Union[str, Any]     =(
																					    -nn.functional.log_softmax(lowerCAmelCase_		,      dim=-1			)[mask].gather(1		,      labels[mask].unsqueeze(1			)			).squeeze(1			)
																					)
															else:
																					A__					:       List[Any]     =nn.functional.log_softmax(lowerCAmelCase_		,      dim=-1			)
									else:
															# construct weights and biases
															A__   ,						A__					:       Any     =[], []
															for i in range(len(self.cutoffs			)			):
																					if self.div_val == 1:
																											A__   ,						A__					:       Optional[int]     =self.cutoff_ends[i], self.cutoff_ends[i + 1]
																											A__					:       int     =self.out_layers[0].weight[l_idx:r_idx]
																											A__					:       List[str]     =self.out_layers[0].bias[l_idx:r_idx]
																					else:
																											A__					:       List[str]     =self.out_layers[i].weight
																											A__					:       Union[str, Any]     =self.out_layers[i].bias

																					if i == 0:
																											A__					:       Tuple     =torch.cat([weight_i, self.cluster_weight]		,      dim=0			)
																											A__					:       List[str]     =torch.cat([bias_i, self.cluster_bias]		,      dim=0			)

																					weights.append(lowerCAmelCase_			)
																					biases.append(lowerCAmelCase_			)

															A__   ,						A__   ,						A__					:       Tuple     =weights[0], biases[0], self.out_projs[0]

															A__					:       List[Any]     =self._compute_logit(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)
															A__					:       int     =nn.functional.log_softmax(lowerCAmelCase_		,      dim=1			)

															if labels is None:
																					A__					:       Union[str, Any]     =hidden.new_empty((head_logit.size(0			), self.n_token)			)
															else:
																					A__					:       Union[str, Any]     =torch.zeros_like(lowerCAmelCase_		,      dtype=hidden.dtype		,      device=hidden.device			)

															A__					:       Any     =0
															A__					:       Tuple     =[0] + self.cutoffs
															for i in range(len(lowerCAmelCase_			) - 1			):
																					A__   ,						A__					:       Tuple     =cutoff_values[i], cutoff_values[i + 1]

																					if labels is not None:
																											A__					:       Tuple     =(labels >= l_idx) & (labels < r_idx)
																											A__					:       Any     =mask_i.nonzero().squeeze()

																											if indices_i.numel() == 0:
																																	continue

																											A__					:       int     =labels.index_select(0		,      lowerCAmelCase_			) - l_idx
																											A__					:       List[str]     =head_logprob.index_select(0		,      lowerCAmelCase_			)
																											A__					:       str     =hidden.index_select(0		,      lowerCAmelCase_			)
																					else:
																											A__					:       Optional[Any]     =hidden

																					if i == 0:
																											if labels is not None:
																																	A__					:       Optional[Any]     =head_logprob_i.gather(1		,      target_i[:, None]			).squeeze(1			)
																											else:
																																	A__					:       Union[str, Any]     =head_logprob[:, : self.cutoffs[0]]
																					else:
																											A__   ,						A__   ,						A__					:       Dict     =weights[i], biases[i], self.out_projs[i]

																											A__					:       List[Any]     =self._compute_logit(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)
																											A__					:       List[Any]     =nn.functional.log_softmax(lowerCAmelCase_		,      dim=1			)
																											A__					:       Optional[Any]     =self.cutoffs[0] + i - 1  # No probability for the head cluster
																											if labels is not None:
																																	A__					:       Union[str, Any]     =head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
																																	    1		,      target_i[:, None]			).squeeze(1			)
																											else:
																																	A__					:       List[str]     =head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
																																	A__					:       Tuple     =logprob_i

																					if labels is not None:
																											if (hasattr(self		,      """keep_order"""			) and self.keep_order) or keep_order:
																																	out.index_copy_(0		,      lowerCAmelCase_		,      -logprob_i			)
																											else:
																																	out[offset : offset + logprob_i.size(0			)].copy_(-logprob_i			)
																											offset += logprob_i.size(0			)

									return out



			def lowercase__						(				self	:						List[str]		,      lowerCAmelCase_	:						Optional[Any]			)      ->		Any:





									'''simple docstring'''
									if self.n_clusters == 0:
															A__					:       List[str]     =self._compute_logit(lowerCAmelCase_		,      self.out_layers[0].weight		,      self.out_layers[0].bias		,      self.out_projs[0]			)
															return nn.functional.log_softmax(lowerCAmelCase_		,      dim=-1			)
									else:
															# construct weights and biases
															A__   ,						A__					:       List[str]     =[], []
															for i in range(len(self.cutoffs			)			):
																					if self.div_val == 1:
																											A__   ,						A__					:       int     =self.cutoff_ends[i], self.cutoff_ends[i + 1]
																											A__					:       List[str]     =self.out_layers[0].weight[l_idx:r_idx]
																											A__					:       List[Any]     =self.out_layers[0].bias[l_idx:r_idx]
																					else:
																											A__					:       Dict     =self.out_layers[i].weight
																											A__					:       Any     =self.out_layers[i].bias

																					if i == 0:
																											A__					:       List[str]     =torch.cat([weight_i, self.cluster_weight]		,      dim=0			)
																											A__					:       Tuple     =torch.cat([bias_i, self.cluster_bias]		,      dim=0			)

																					weights.append(lowerCAmelCase_			)
																					biases.append(lowerCAmelCase_			)

															A__   ,						A__   ,						A__					:       Optional[int]     =weights[0], biases[0], self.out_projs[0]
															A__					:       Any     =self._compute_logit(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)

															A__					:       Dict     =hidden.new_empty((head_logit.size(0			), self.n_token)			)
															A__					:       Dict     =nn.functional.log_softmax(lowerCAmelCase_		,      dim=1			)

															A__					:       Tuple     =[0] + self.cutoffs
															for i in range(len(lowerCAmelCase_			) - 1			):
																					A__   ,						A__					:       List[Any]     =cutoff_values[i], cutoff_values[i + 1]

																					if i == 0:
																											A__					:       Tuple     =head_logprob[:, : self.cutoffs[0]]
																					else:
																											A__   ,						A__   ,						A__					:       Any     =weights[i], biases[i], self.out_projs[i]

																											A__					:       Dict     =self._compute_logit(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)
																											A__					:       str     =nn.functional.log_softmax(lowerCAmelCase_		,      dim=1			)

																											A__					:       str     =head_logprob[:, -i] + tail_logprob_i
																											A__					:       List[Any]     =logprob_i

															return out

