


'''simple docstring'''





from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   bool  ):
       '''simple docstring'''





       if not inplace:
              _lowerCamelCase :	Any						=ma + ma
       else:
              ma += ma

       return ma




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   List[int]  ):
       '''simple docstring'''





       _lowerCamelCase :	List[Any]						=-1 * len(SCREAMING_SNAKE_CASE__  )
       _lowerCamelCase :	Optional[int]						=list(range(len(tensor.shape[:zero_index]  )  )  )
       return tensor.permute(first_inds + [zero_index + i for i in inds]  )




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   int  ):
       '''simple docstring'''





       return t.reshape(t.shape[:-no_dims] + (-1,)  )




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   int	,    SCREAMING_SNAKE_CASE__      :   float = 1e-4  ):
       '''simple docstring'''





       _lowerCamelCase :	Tuple						=mask.expand(*value.shape  )
       return torch.sum(mask * value	,    dim=SCREAMING_SNAKE_CASE__  ) / (eps + torch.sum(SCREAMING_SNAKE_CASE__	,    dim=SCREAMING_SNAKE_CASE__  ))




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   torch.types.Number = 2.31_25	,    SCREAMING_SNAKE_CASE__      :   torch.types.Number = 21.68_75	,    SCREAMING_SNAKE_CASE__      :   int = 64  ):
       '''simple docstring'''





       _lowerCamelCase :	Tuple						=torch.linspace(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    no_bins - 1	,    device=pts.device  )
       _lowerCamelCase :	Union[str, Any]						=torch.sqrt(torch.sum((pts.unsqueeze(-2  ) - pts.unsqueeze(-3  )) ** 2	,    dim=-1  )  )
       return torch.bucketize(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )




def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[list], Any]	,    SCREAMING_SNAKE_CASE__      :   List[dict]  ):
       '''simple docstring'''





       _lowerCamelCase :	Optional[Any]						=dicts[0]
       _lowerCamelCase :	List[str]						={}
       for k, v in first.items():
              _lowerCamelCase :	Dict						=[d[k] for d in dicts]
              if isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
                     _lowerCamelCase :	int						=dict_multimap(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )
              else:
                     _lowerCamelCase :	Dict						=fn(SCREAMING_SNAKE_CASE__  )

       return new_dict




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   torch.Tensor  ):
       '''simple docstring'''





       _lowerCamelCase :	Optional[Any]						=v_bins.view(((1,) * len(x.shape  )) + (len(SCREAMING_SNAKE_CASE__  ),)  )
       _lowerCamelCase :	Dict						=x[..., None] - reshaped_bins
       _lowerCamelCase :	Union[str, Any]						=torch.argmin(torch.abs(SCREAMING_SNAKE_CASE__  )	,    dim=-1  )
       return nn.functional.one_hot(SCREAMING_SNAKE_CASE__	,    num_classes=len(SCREAMING_SNAKE_CASE__  )  ).float()




def 			a_   (					SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   torch.Tensor	,    SCREAMING_SNAKE_CASE__      :   int = 0	,    SCREAMING_SNAKE_CASE__      :   int = 0  ):
       '''simple docstring'''





       _lowerCamelCase :	List[Union[slice, torch.Tensor]]						=[]
       for i, s in enumerate(data.shape[:no_batch_dims]  ):
              _lowerCamelCase :	List[str]						=torch.arange(SCREAMING_SNAKE_CASE__  )
              _lowerCamelCase :	str						=r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape  ) - i - 1)))  )
              ranges.append(SCREAMING_SNAKE_CASE__  )

       _lowerCamelCase :	List[Union[slice, torch.Tensor]]						=[slice(SCREAMING_SNAKE_CASE__  ) for _ in range(len(data.shape  ) - no_batch_dims  )]
       _lowerCamelCase :	List[Any]						=inds
       ranges.extend(SCREAMING_SNAKE_CASE__  )
       # Matt note: Editing this to get around the behaviour of using a list as an array index changing
       # in recent Numpy versions
       return data[tuple(SCREAMING_SNAKE_CASE__  )]


lowerCamelCase		      = TypeVar('T')




def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[T], Any]	,    SCREAMING_SNAKE_CASE__      :   Dict[Any, Union[dict, list, tuple, T]]	,    SCREAMING_SNAKE_CASE__      :   Type[T]  ):
       '''simple docstring'''





       _lowerCamelCase :	Dict[Any, Union[dict, list, tuple, Any]]						={}
       for k, v in dic.items():
              if isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
                     _lowerCamelCase :	Any						=dict_map(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )
              else:
                     _lowerCamelCase :	Any						=tree_map(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )

       return new_dict




@overload
def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[T], Any]	,    SCREAMING_SNAKE_CASE__      :   T	,    SCREAMING_SNAKE_CASE__      :   Type[T]  ):
       '''simple docstring'''





       ...




@overload
def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[T], Any]	,    SCREAMING_SNAKE_CASE__      :   dict	,    SCREAMING_SNAKE_CASE__      :   Type[T]  ):
       '''simple docstring'''





       ...




@overload
def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[T], Any]	,    SCREAMING_SNAKE_CASE__      :   list	,    SCREAMING_SNAKE_CASE__      :   Type[T]  ):
       '''simple docstring'''





       ...




@overload
def 			a_   (					SCREAMING_SNAKE_CASE__      :   Callable[[T], Any]	,    SCREAMING_SNAKE_CASE__      :   tuple	,    SCREAMING_SNAKE_CASE__      :   Type[T]  ):
       '''simple docstring'''





       ...




def 			a_   (					SCREAMING_SNAKE_CASE__      :   Any	,    SCREAMING_SNAKE_CASE__      :   Dict	,    SCREAMING_SNAKE_CASE__      :   Union[str, Any]  ):
       '''simple docstring'''





       if isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
              return dict_map(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  )
       elif isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
              return [tree_map(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ) for x in tree]
       elif isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
              return tuple(tree_map(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ) for x in tree  )
       elif isinstance(SCREAMING_SNAKE_CASE__	,    SCREAMING_SNAKE_CASE__  ):
              return fn(SCREAMING_SNAKE_CASE__  )
       else:
              print(type(SCREAMING_SNAKE_CASE__  )  )
              raise ValueError('Not supported'  )


lowerCamelCase		      = partial(tree_map, leaf_type=torch.Tensor)






from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


lowerCamelCase		      = logging.get_logger(__name__)

lowerCamelCase		      = {'vocab_file': 'vocab.json', 'merges_file': 'merges.txt', 'tokenizer_file': 'tokenizer.json'}

lowerCamelCase		      = {
    'vocab_file': {
        'openai/clip-vit-base-patch32': 'https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json',
    },
    'merges_file': {
        'openai/clip-vit-base-patch32': 'https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt',
    },
    'tokenizer_file': {
        'openai/clip-vit-base-patch32': (
            'https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json'
        ),
    },
}

lowerCamelCase		      = {
    'openai/clip-vit-base-patch32': 77,
}




class A						(     UpperCamelCase_  ):
      UpperCamelCase__							:						Dict   =VOCAB_FILES_NAMES
      UpperCamelCase__							:						Optional[int]   =PRETRAINED_VOCAB_FILES_MAP
      UpperCamelCase__							:						Optional[int]   =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      UpperCamelCase__							:						Optional[Any]   =['input_ids', 'attention_mask']
      UpperCamelCase__							:						str   =CLIPTokenizer






      def __init__(      self		:  str    ,     lowercase_		:  Optional[Any]=None    ,     lowercase_		:  Optional[int]=None    ,     lowercase_		:  int=None    ,     lowercase_		:  Optional[int]="<|endoftext|>"    ,     lowercase_		:  Union[str, Any]="<|startoftext|>"    ,     lowercase_		:  Tuple="<|endoftext|>"    ,     lowercase_		:  int="<|endoftext|>"    ,     **lowercase_		:  Optional[Any]    ,     )      ->		Any:
             """simple docstring"""




             super().__init__(
                 lowercase_    ,     lowercase_    ,     tokenizer_file=lowercase_    ,     unk_token=lowercase_    ,     bos_token=lowercase_    ,     eos_token=lowercase_    ,     pad_token=lowercase_    ,     **lowercase_    ,     )

             if not isinstance(self.backend_tokenizer.pre_tokenizer    ,     pre_tokenizers.Sequence				):
                    raise ValueError(
                        'The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been'
                        ' heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using'
                        ' to be compatible with this version.The easiest way to do so is'
                        ' `CLIPTokenizerFast.from_pretrained("path_to_local_folder_or_hub_repo, from_slow=True)`. If you want'
                        ' to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of'
                        ' transformers.'				)

             self._wrap_decode_method_backend_tokenizer()






      def  lowerCamelCase (      self		:  Any				)      ->		Union[str, Any]:
             """simple docstring"""




             _lowerCamelCase :	int						=self.backend_tokenizer.decode

             def new_decode_method(*lowercase_		:  List[str]    ,     **lowercase_		:  int				):
                    _lowerCamelCase :	Tuple						=orig_decode_method(*lowercase_    ,     **lowercase_				)
                    _lowerCamelCase :	Dict						=text.replace(self.backend_tokenizer.model.end_of_word_suffix    ,     ' '				).strip()
                    return text

             _lowerCamelCase :	List[Any]						=new_decode_method






      def  lowerCamelCase (      self		:  Dict    ,     lowercase_		:  List[int]    ,     lowercase_		:  Optional[List[int]] = None				)      ->		List[int]:
             """simple docstring"""




             _lowerCamelCase :	Union[str, Any]						=[self.bos_token_id]
             _lowerCamelCase :	str						=[self.eos_token_id]

             if token_ids_a is None:
                    return bos_token + token_ids_a + eos_token
             return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token






      def  lowerCamelCase (      self		:  Any    ,     lowercase_		:  List[int]    ,     lowercase_		:  Optional[List[int]] = None				)      ->		List[int]:
             """simple docstring"""




             _lowerCamelCase :	int						=[self.bos_token_id]
             _lowerCamelCase :	Any						=[self.eos_token_id]

             if token_ids_a is None:
                    return len(bos_token + token_ids_a + eos_token				) * [0]
             return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token				) * [0]






      def  lowerCamelCase (      self		:  str    ,     lowercase_		:  str    ,     lowercase_		:  Optional[str] = None				)      ->		Tuple[str]:
             """simple docstring"""




             _lowerCamelCase :	str						=self._tokenizer.model.save(lowercase_    ,     name=lowercase_				)
             return tuple(lowercase_				)


