
import inspect
from typing import Callable, List, Optional, Set, Tuple, Union

import torch
from packaging import version
from safetensors.torch import storage_ptr, storage_size
from torch import nn

from .utils import logging


lowercase_											=	[nn.LayerNorm]

lowercase_											=	logging.get_logger(__name__)

lowercase_											=	version.parse(version.parse(torch.__version__).base_version)

lowercase_											=	parsed_torch_version_base >= version.parse("""2.0""")
lowercase_											=	parsed_torch_version_base >= version.parse("""1.12""")
lowercase_											=	parsed_torch_version_base >= version.parse("""1.11""")
lowercase_											=	parsed_torch_version_base < version.parse("""1.11""")


def     a__			(    snake_case   ,	snake_case   ,	snake_case   ,	snake_case   ,	self    ):





  """simple docstring"""
  from torch import _softmax_backward_data

  if is_torch_less_than_1_11:
    return _softmax_backward_data(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_   ,	parent.dim   ,	self    )
  else:
    return _softmax_backward_data(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_   ,	parent.dim   ,	self.dtype    )


def     a__			(    snake_case   ,	snake_case   ,	snake_case = 0    ):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE     :					str			=						index.to(layer.weight.device    )
  __SCREAMING_SNAKE_CASE     :					List[str]			=						layer.weight.index_select(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_    ).clone().detach()
  if layer.bias is not None:
    if dim == 1:
      __SCREAMING_SNAKE_CASE     :					Any			=						layer.bias.clone().detach()
    else:
      __SCREAMING_SNAKE_CASE     :					Tuple			=						layer.bias[index].clone().detach()
  __SCREAMING_SNAKE_CASE     :					str			=						list(layer.weight.size()    )
  __SCREAMING_SNAKE_CASE     :					List[str]			=						len(SCREAMING_SNAKE_CASE_    )
  __SCREAMING_SNAKE_CASE     :					Dict			=						nn.Linear(new_size[1]   ,	new_size[0]   ,	bias=layer.bias is not None    ).to(layer.weight.device    )
  __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						False
  new_layer.weight.copy_(W.contiguous()    )
  __SCREAMING_SNAKE_CASE     :					List[Any]			=						True
  if layer.bias is not None:
    __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						False
    new_layer.bias.copy_(b.contiguous()    )
    __SCREAMING_SNAKE_CASE     :					Optional[int]			=						True
  return new_layer





class   __UpperCamelCase				(    nn.Module   ):






     """simple docstring"""







     def __init__(							self    :    List[str]      ,		_A    :    Optional[int]      ,		_A    :    List[Any]				):







       """simple docstring"""




       super().__init__()
       __SCREAMING_SNAKE_CASE     :					Tuple			=						nf
       __SCREAMING_SNAKE_CASE     :					str			=						nn.Parameter(torch.empty(_A      ,		_A				)				)
       __SCREAMING_SNAKE_CASE     :					Tuple			=						nn.Parameter(torch.zeros(_A				)				)
       nn.init.normal_(self.weight      ,		std=0.02				)




     def 				UpperCAmelCase__    (							self    :    Dict      ,		_A    :    Any				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					str			=						x.size()[:-1] + (self.nf,)
       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						torch.addmm(self.bias      ,		x.view(-1      ,		x.size(-1				)				)      ,		self.weight				)
       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						x.view(_A				)
       return x






def     a__			(    snake_case   ,	snake_case   ,	snake_case = 1    ):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE     :					Dict			=						index.to(layer.weight.device    )
  __SCREAMING_SNAKE_CASE     :					List[str]			=						layer.weight.index_select(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_    ).clone().detach()
  if dim == 0:
    __SCREAMING_SNAKE_CASE     :					Tuple			=						layer.bias.clone().detach()
  else:
    __SCREAMING_SNAKE_CASE     :					str			=						layer.bias[index].clone().detach()
  __SCREAMING_SNAKE_CASE     :					Dict			=						list(layer.weight.size()    )
  __SCREAMING_SNAKE_CASE     :					Optional[int]			=						len(SCREAMING_SNAKE_CASE_    )
  __SCREAMING_SNAKE_CASE     :					Any			=						ConvaD(new_size[1]   ,	new_size[0]    ).to(layer.weight.device    )
  __SCREAMING_SNAKE_CASE     :					Dict			=						False
  new_layer.weight.copy_(W.contiguous()    )
  __SCREAMING_SNAKE_CASE     :					List[str]			=						True
  __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						False
  new_layer.bias.copy_(b.contiguous()    )
  __SCREAMING_SNAKE_CASE     :					List[Any]			=						True
  return new_layer


def     a__			(    snake_case   ,	snake_case   ,	snake_case = None    ):





  """simple docstring"""





  if isinstance(SCREAMING_SNAKE_CASE_   ,	nn.Linear    ):
    return prune_linear_layer(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_   ,	dim=0 if dim is None else dim    )
  elif isinstance(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_    ):
    return prune_convad_layer(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_   ,	dim=1 if dim is None else dim    )
  else:
    raise ValueError(F'''Can\'t prune layer of class {layer.__class__}'''    )


def     a__			(    snake_case   ,	snake_case   ,	snake_case   ,	*snake_case    ):





  """simple docstring"""
  assert len(SCREAMING_SNAKE_CASE_    ) > 0, F'''{input_tensors} has to be a tuple/list of tensors'''

  # inspect.signature exist since python 3.5 and is a python method -> no problem with backward compatibility
  __SCREAMING_SNAKE_CASE     :					int			=						len(inspect.signature(SCREAMING_SNAKE_CASE_    ).parameters    )
  if num_args_in_forward_chunk_fn != len(SCREAMING_SNAKE_CASE_    ):
    raise ValueError(
        F'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(SCREAMING_SNAKE_CASE_    )} input '''
        '''tensors are given'''    )

  if chunk_size > 0:
    __SCREAMING_SNAKE_CASE     :					Any			=						input_tensors[0].shape[chunk_dim]
    for input_tensor in input_tensors:
      if input_tensor.shape[chunk_dim] != tensor_shape:
        raise ValueError(
            F'''All input tenors have to be of the same shape: {tensor_shape}, '''
            F'''found shape {input_tensor.shape[chunk_dim]}'''    )

    if input_tensors[0].shape[chunk_dim] % chunk_size != 0:
      raise ValueError(
          F'''The dimension to be chunked {input_tensors[0].shape[chunk_dim]} has to be a multiple of the chunk '''
          F'''size {chunk_size}'''    )

    __SCREAMING_SNAKE_CASE     :					Optional[int]			=						input_tensors[0].shape[chunk_dim] // chunk_size

    # chunk input tensor into tuples
    __SCREAMING_SNAKE_CASE     :					List[Any]			=						tuple(input_tensor.chunk(SCREAMING_SNAKE_CASE_   ,	dim=SCREAMING_SNAKE_CASE_    ) for input_tensor in input_tensors    )
    # apply forward fn to every tuple
    __SCREAMING_SNAKE_CASE     :					Tuple			=						tuple(forward_fn(*SCREAMING_SNAKE_CASE_    ) for input_tensors_chunk in zip(*SCREAMING_SNAKE_CASE_    )    )
    # concatenate output at same dimension
    return torch.cat(SCREAMING_SNAKE_CASE_   ,	dim=SCREAMING_SNAKE_CASE_    )

  return forward_fn(*SCREAMING_SNAKE_CASE_    )


def     a__			(    snake_case   ,	snake_case   ,	snake_case   ,	snake_case    ):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE     :					int			=						torch.ones(SCREAMING_SNAKE_CASE_   ,	SCREAMING_SNAKE_CASE_    )
  __SCREAMING_SNAKE_CASE     :					Any			=						set(SCREAMING_SNAKE_CASE_    ) - already_pruned_heads  # Convert to set and remove already pruned heads
  for head in heads:
    # Compute how many pruned heads are before the head and move the index accordingly
    __SCREAMING_SNAKE_CASE     :					int			=						head - sum(1 if h < head else 0 for h in already_pruned_heads    )
    __SCREAMING_SNAKE_CASE     :					List[Any]			=						0
  __SCREAMING_SNAKE_CASE     :					List[str]			=						mask.view(-1    ).contiguous().eq(1    )
  __SCREAMING_SNAKE_CASE     :					Dict			=						torch.arange(len(SCREAMING_SNAKE_CASE_    )    )[mask].long()
  return heads, index


def     a__			(    *snake_case   ,	snake_case = None    ):





  """simple docstring"""
  return torch.meshgrid(*SCREAMING_SNAKE_CASE_   ,	indexing=SCREAMING_SNAKE_CASE_    )


def     a__			(    snake_case    ):





  """simple docstring"""
  return tensor.device, storage_ptr(SCREAMING_SNAKE_CASE_    ), storage_size(SCREAMING_SNAKE_CASE_    )




import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class   __UpperCamelCase				(    lowerCAmelCase__   ):






     """simple docstring"""







     lowerCAmelCase_     =	['''image_processor''', '''tokenizer''']
     lowerCAmelCase_     =	'''LayoutLMv2ImageProcessor'''
     lowerCAmelCase_     =	('''LayoutLMv2Tokenizer''', '''LayoutLMv2TokenizerFast''')



     def __init__(							self    :    Optional[Any]      ,		_A    :    List[Any]=None      ,		_A    :    List[str]=None      ,		**_A    :    int				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Optional[int]			=						None
       if "feature_extractor" in kwargs:
         warnings.warn(
             '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
             ''' instead.'''      ,		_A      ,		)
         __SCREAMING_SNAKE_CASE     :					Tuple			=						kwargs.pop('''feature_extractor'''				)

       __SCREAMING_SNAKE_CASE     :					List[str]			=						image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
         raise ValueError('''You need to specify an `image_processor`.'''				)
       if tokenizer is None:
         raise ValueError('''You need to specify a `tokenizer`.'''				)

       super().__init__(_A      ,		_A				)



     def __call__(							self    :    Optional[int]      ,		_A    :    Tuple      ,		_A    :    Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None      ,		_A    :    Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None      ,		_A    :    Union[List[List[int]], List[List[List[int]]]] = None      ,		_A    :    Optional[Union[List[int], List[List[int]]]] = None      ,		_A    :    bool = True      ,		_A    :    Union[bool, str, PaddingStrategy] = False      ,		_A    :    Union[bool, str, TruncationStrategy] = False      ,		_A    :    Optional[int] = None      ,		_A    :    int = 0      ,		_A    :    Optional[int] = None      ,		_A    :    Optional[bool] = None      ,		_A    :    Optional[bool] = None      ,		_A    :    bool = False      ,		_A    :    bool = False      ,		_A    :    bool = False      ,		_A    :    bool = False      ,		_A    :    bool = True      ,		_A    :    Optional[Union[str, TensorType]] = None      ,		**_A    :    Tuple      ,		):







       """simple docstring"""




       if self.image_processor.apply_ocr and (boxes is not None):
         raise ValueError(
             '''You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.'''				)

       if self.image_processor.apply_ocr and (word_labels is not None):
         raise ValueError(
             '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''				)

       if return_overflowing_tokens is True and return_offsets_mapping is False:
         raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''				)

       # first, apply the image processor
       __SCREAMING_SNAKE_CASE     :					Optional[Any]			=						self.image_processor(images=_A      ,		return_tensors=_A				)

       # second, apply the tokenizer
       if text is not None and self.image_processor.apply_ocr and text_pair is None:
         if isinstance(_A      ,		_A				):
           __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						[text]  # add batch dimension (as the image processor always adds a batch dimension)
         __SCREAMING_SNAKE_CASE     :					Tuple			=						features['''words''']

       __SCREAMING_SNAKE_CASE     :					str			=						self.tokenizer(
           text=text if text is not None else features['''words''']      ,		text_pair=text_pair if text_pair is not None else None      ,		boxes=boxes if boxes is not None else features['''boxes''']      ,		word_labels=_A      ,		add_special_tokens=_A      ,		padding=_A      ,		truncation=_A      ,		max_length=_A      ,		stride=_A      ,		pad_to_multiple_of=_A      ,		return_token_type_ids=_A      ,		return_attention_mask=_A      ,		return_overflowing_tokens=_A      ,		return_special_tokens_mask=_A      ,		return_offsets_mapping=_A      ,		return_length=_A      ,		verbose=_A      ,		return_tensors=_A      ,		**_A      ,		)

       # add pixel values
       __SCREAMING_SNAKE_CASE     :					str			=						features.pop('''pixel_values'''				)
       if return_overflowing_tokens is True:
         __SCREAMING_SNAKE_CASE     :					str			=						self.get_overflowing_images(_A      ,		encoded_inputs['''overflow_to_sample_mapping''']				)
       __SCREAMING_SNAKE_CASE     :					List[str]			=						images

       return encoded_inputs



     def 				UpperCAmelCase__    (							self    :    List[str]      ,		_A    :    List[Any]      ,		_A    :    Dict				):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE     :					Union[str, Any]			=						[]
       for sample_idx in overflow_to_sample_mapping:
         images_with_overflow.append(images[sample_idx]				)

       if len(_A				) != len(_A				):
         raise ValueError(
             '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
             F''' {len(_A				)} and {len(_A				)}'''				)

       return images_with_overflow



     def 				UpperCAmelCase__    (							self    :    int      ,		*_A    :    Dict      ,		**_A    :    Optional[Any]				):







       """simple docstring"""




       return self.tokenizer.batch_decode(*_A      ,		**_A				)



     def 				UpperCAmelCase__    (							self    :    Union[str, Any]      ,		*_A    :    Union[str, Any]      ,		**_A    :    List[str]				):







       """simple docstring"""




       return self.tokenizer.decode(*_A      ,		**_A				)



     @property
     def 				UpperCAmelCase__    (							self    :    Optional[int]				):







       """simple docstring"""




       return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]



     @property
     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''      ,		_A      ,		)
       return self.image_processor_class




     @property
     def 				UpperCAmelCase__    (							self    :    List[str]				):







       """simple docstring"""




       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''      ,		_A      ,		)
       return self.image_processor
