'''simple docstring'''




import torch
from torch import nn

class 	lowerCamelCase_	(nn.Module    ):
      '''simple docstring'''

      def __init__(       self						:				Optional[Any]						,					A						:				Dict						,					A						:				Tuple						,					A						:				Optional[Any]						,					A						:				Tuple						,					A						:				Union[str, Any]=1						,					A						:				str=False       ):
             super().__init__()

             _UpperCAmelCase     :  Union[str, Any]							      =    n_token
             _UpperCAmelCase     :  List[Any]							      =    d_embed
             _UpperCAmelCase     :  List[str]							      =    d_proj

             _UpperCAmelCase     :  Union[str, Any]							      =    cutoffs + [n_token]
             _UpperCAmelCase     :  str							      =    [0] + self.cutoffs
             _UpperCAmelCase     :  Dict							      =    div_val

             _UpperCAmelCase     :  Tuple							      =    self.cutoffs[0]
             _UpperCAmelCase     :  Tuple							      =    len(self.cutoffs       ) - 1
             _UpperCAmelCase     :  Tuple							      =    self.shortlist_size + self.n_clusters

             if self.n_clusters > 0:
                    _UpperCAmelCase     :  Any							      =    nn.Parameter(torch.zeros(self.n_clusters						,					self.d_embed       )       )
                    _UpperCAmelCase     :  Optional[int]							      =    nn.Parameter(torch.zeros(self.n_clusters       )       )

             _UpperCAmelCase     :  str							      =    nn.ModuleList()
             _UpperCAmelCase     :  Dict							      =    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(A						,					A       )       )       )
                           else:
                                  self.out_projs.append(A       )

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

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

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

             _UpperCAmelCase     :  Dict							      =    keep_order




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

             return logit




      def      _A	(       self						:				Optional[int]						,					A						:				Tuple						,					A						:				Union[str, Any]=None						,					A						:				Any=False       ):

             if labels is not None:
                    # Shift so that tokens < n predict n
                    _UpperCAmelCase     :  Union[str, Any]							      =    hidden[..., :-1, :].contiguous()
                    _UpperCAmelCase     :  str							      =    labels[..., 1:].contiguous()
                    _UpperCAmelCase     :  Any							      =    hidden.view(-1						,					hidden.size(-1       )       )
                    _UpperCAmelCase     :  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:
                    _UpperCAmelCase     :  Union[str, Any]							      =    hidden.view(-1						,					hidden.size(-1       )       )

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

                           if i == 0:
                                  _UpperCAmelCase     :  int							      =    torch.cat([weight_i, self.cluster_weight]						,					dim=0       )
                                  _UpperCAmelCase     :  Union[str, Any]							      =    torch.cat([bias_i, self.cluster_bias]						,					dim=0       )

                           weights.append(A       )
                           biases.append(A       )

                    _UpperCAmelCase      ,  _UpperCAmelCase      ,  _UpperCAmelCase     :  List[str]							      =    weights[0], biases[0], self.out_projs[0]

                    _UpperCAmelCase     :  Dict							      =    self._compute_logit(A						,					A						,					A						,					A       )
                    _UpperCAmelCase     :  List[str]							      =    nn.functional.log_softmax(A						,					dim=1       )

                    if labels is None:
                           _UpperCAmelCase     :  int							      =    hidden.new_empty((head_logit.size(0       ), self.n_token)       )
                    else:
                           _UpperCAmelCase     :  int							      =    torch.zeros_like(A						,					dtype=hidden.dtype						,					device=hidden.device       )

                    _UpperCAmelCase     :  Dict							      =    0
                    _UpperCAmelCase     :  Any							      =    [0] + self.cutoffs
                    for i in range(len(A       ) - 1       ):
                           _UpperCAmelCase      ,  _UpperCAmelCase     :  str							      =    cutoff_values[i], cutoff_values[i + 1]

                           if labels is not None:
                                  _UpperCAmelCase     :  List[Any]							      =    (labels >= l_idx) & (labels < r_idx)
                                  _UpperCAmelCase     :  Any							      =    mask_i.nonzero().squeeze()

                                  if indices_i.numel() == 0:
                                         continue

                                  _UpperCAmelCase     :  Union[str, Any]							      =    labels.index_select(0						,					A       ) - l_idx
                                  _UpperCAmelCase     :  Any							      =    head_logprob.index_select(0						,					A       )
                                  _UpperCAmelCase     :  Dict							      =    hidden.index_select(0						,					A       )
                           else:
                                  _UpperCAmelCase     :  List[Any]							      =    hidden

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

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

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

             return out





      def      _A	(       self						:				Optional[int]						,					A						:				str       ):
             if self.n_clusters == 0:
                    _UpperCAmelCase     :  List[str]							      =    self._compute_logit(A						,					self.out_layers[0].weight						,					self.out_layers[0].bias						,					self.out_projs[0]       )
                    return nn.functional.log_softmax(A						,					dim=-1       )
             else:
                    # construct weights and biases
                    _UpperCAmelCase      ,  _UpperCAmelCase     :  List[Any]							      =    [], []
                    for i in range(len(self.cutoffs       )       ):
                           if self.div_val == 1:
                                  _UpperCAmelCase      ,  _UpperCAmelCase     :  Optional[Any]							      =    self.cutoff_ends[i], self.cutoff_ends[i + 1]
                                  _UpperCAmelCase     :  Union[str, Any]							      =    self.out_layers[0].weight[l_idx:r_idx]
                                  _UpperCAmelCase     :  List[Any]							      =    self.out_layers[0].bias[l_idx:r_idx]
                           else:
                                  _UpperCAmelCase     :  int							      =    self.out_layers[i].weight
                                  _UpperCAmelCase     :  List[str]							      =    self.out_layers[i].bias

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

                           weights.append(A       )
                           biases.append(A       )

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

                    _UpperCAmelCase     :  Union[str, Any]							      =    hidden.new_empty((head_logit.size(0       ), self.n_token)       )
                    _UpperCAmelCase     :  Any							      =    nn.functional.log_softmax(A						,					dim=1       )

                    _UpperCAmelCase     :  Optional[Any]							      =    [0] + self.cutoffs
                    for i in range(len(A       ) - 1       ):
                           _UpperCAmelCase      ,  _UpperCAmelCase     :  List[str]							      =    cutoff_values[i], cutoff_values[i + 1]

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

                                  _UpperCAmelCase     :  int							      =    self._compute_logit(A						,					A						,					A						,					A       )
                                  _UpperCAmelCase     :  List[str]							      =    nn.functional.log_softmax(A						,					dim=1       )

                                  _UpperCAmelCase     :  Optional[Any]							      =    head_logprob[:, -i] + tail_logprob_i
                                  _UpperCAmelCase     :  Any							      =    logprob_i

                    return out



"""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	):







     def      lowercase	(							self			:			Any			):
       _snake_case					     =       tempfile.mkdtemp()

       # fmt: off
       _snake_case					     =       ['''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
       _snake_case					     =       dict(zip(_lowerCamelCase    ,      range(len(_lowerCamelCase			)			)			)			)
       _snake_case					     =       ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
       _snake_case					     =       {'''unk_token''': '''<unk>'''}

       _snake_case					     =       os.path.join(self.tmpdirname    ,      VOCAB_FILES_NAMES['''vocab_file''']			)
       _snake_case					     =       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			)			)

       _snake_case					     =       {
           '''do_resize''': True,
           '''size''': 20,
           '''do_center_crop''': True,
           '''crop_size''': 18,
           '''do_normalize''': True,
           '''image_mean''': [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],
           '''image_std''': [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],
       }
       _snake_case					     =       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			:			Any			):
       return CLIPTokenizer.from_pretrained(self.tmpdirname    ,      **_lowerCamelCase			)







     def      lowercase	(							self			:			str    ,      **_lowerCamelCase			:			Any			):
       return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,      **_lowerCamelCase			)







     def      lowercase	(							self			:			int    ,      **_lowerCamelCase			:			Optional[int]			):
       return CLIPImageProcessor.from_pretrained(self.tmpdirname    ,      **_lowerCamelCase			)







     def      lowercase	(							self			:			Union[str, Any]			):
       shutil.rmtree(self.tmpdirname			)







     def      lowercase	(							self			:			Any			):
       _snake_case					     =       [np.random.randint(255    ,      size=(3, 30, 400)    ,      dtype=np.uinta			)]

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

       return image_inputs







     def      lowercase	(							self			:			Optional[Any]			):
       _snake_case					     =       self.get_tokenizer()
       _snake_case					     =       self.get_rust_tokenizer()
       _snake_case					     =       self.get_image_processor()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)
       processor_slow.save_pretrained(self.tmpdirname			)
       _snake_case					     =       CLIPProcessor.from_pretrained(self.tmpdirname    ,      use_fast=_lowerCamelCase			)

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)
       processor_fast.save_pretrained(self.tmpdirname			)
       _snake_case					     =       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			:			List[Any]			):
       _snake_case					     =       CLIPProcessor(tokenizer=self.get_tokenizer()    ,      image_processor=self.get_image_processor()			)
       processor.save_pretrained(self.tmpdirname			)

       _snake_case					     =       self.get_tokenizer(bos_token='''(BOS)'''    ,      eos_token='''(EOS)'''			)
       _snake_case					     =       self.get_image_processor(do_normalize=_lowerCamelCase    ,      padding_value=1.0			)

       _snake_case					     =       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			:			int			):
       _snake_case					     =       self.get_image_processor()
       _snake_case					     =       self.get_tokenizer()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)

       _snake_case					     =       self.prepare_image_inputs()

       _snake_case					     =       image_processor(_lowerCamelCase    ,      return_tensors='''np'''			)
       _snake_case					     =       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			:			Any			):
       _snake_case					     =       self.get_image_processor()
       _snake_case					     =       self.get_tokenizer()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)

       _snake_case					     =       '''lower newer'''

       _snake_case					     =       processor(text=_lowerCamelCase			)

       _snake_case					     =       tokenizer(_lowerCamelCase			)

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







     def      lowercase	(							self			:			Any			):
       _snake_case					     =       self.get_image_processor()
       _snake_case					     =       self.get_tokenizer()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)

       _snake_case					     =       '''lower newer'''
       _snake_case					     =       self.prepare_image_inputs()

       _snake_case					     =       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			:			List[str]			):
       _snake_case					     =       self.get_image_processor()
       _snake_case					     =       self.get_tokenizer()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)

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

       _snake_case					     =       processor.batch_decode(_lowerCamelCase			)
       _snake_case					     =       tokenizer.batch_decode(_lowerCamelCase			)

       self.assertListEqual(_lowerCamelCase    ,      _lowerCamelCase			)







     def      lowercase	(							self			:			List[Any]			):
       _snake_case					     =       self.get_image_processor()
       _snake_case					     =       self.get_tokenizer()

       _snake_case					     =       CLIPProcessor(tokenizer=_lowerCamelCase    ,      image_processor=_lowerCamelCase			)

       _snake_case					     =       '''lower newer'''
       _snake_case					     =       self.prepare_image_inputs()

       _snake_case					     =       processor(text=_lowerCamelCase    ,      images=_lowerCamelCase			)

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



