





'''simple docstring'''




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


__UpperCAmelCase         =   [nn.LayerNorm]

__UpperCAmelCase         =   logging.get_logger(__name__)

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

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



def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_	,	self					):


		"""simple docstring"""



		from torch import _softmax_backward_data

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



def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_ = 0					):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE				:							Optional[Any]						=	index.to(layer.weight.device					)
		SCREAMING_SNAKE_CASE				:							Dict						=	layer.weight.index_select(lowercase_	,	lowercase_					).clone().detach()
		if layer.bias is not None:
				if dim == 1:
						SCREAMING_SNAKE_CASE				:							int						=	layer.bias.clone().detach()
				else:
						SCREAMING_SNAKE_CASE				:							Optional[int]						=	layer.bias[index].clone().detach()
		SCREAMING_SNAKE_CASE				:							int						=	list(layer.weight.size()					)
		SCREAMING_SNAKE_CASE				:							Any						=	len(lowercase_					)
		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				:							Dict						=	False
		new_layer.weight.copy_(W.contiguous()					)
		SCREAMING_SNAKE_CASE				:							str						=	True
		if layer.bias is not None:
				SCREAMING_SNAKE_CASE				:							Any						=	False
				new_layer.bias.copy_(b.contiguous()					)
				SCREAMING_SNAKE_CASE				:							int						=	True
		return new_layer




class       UpperCamelCase__			(			nn.Module	):




					"""simple docstring"""


					def __init__(		self							:	Dict     ,		lowerCamelCase_							:	int     ,		lowerCamelCase_							:	Dict		):

							'''simple docstring'''



							super().__init__()
							SCREAMING_SNAKE_CASE				:							List[Any]						=	nf
							SCREAMING_SNAKE_CASE				:							str						=	nn.Parameter(torch.empty(__UpperCamelCase     ,		__UpperCamelCase		)		)
							SCREAMING_SNAKE_CASE				:							Tuple						=	nn.Parameter(torch.zeros(__UpperCamelCase		)		)
							nn.init.normal_(self.weight     ,		std=0.02		)



					def lowerCamelCase_	(		self							:	str     ,		lowerCamelCase_							:	Optional[int]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							int						=	x.size()[:-1] + (self.nf,)
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	torch.addmm(self.bias     ,		x.view(-1     ,		x.size(-1		)		)     ,		self.weight		)
							SCREAMING_SNAKE_CASE				:							str						=	x.view(__UpperCamelCase		)
							return x







def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_ = 1					):


		"""simple docstring"""



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



def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_ = None					):


		"""simple docstring"""



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



def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_	,	*lowerCamelCase_					):


		"""simple docstring"""



		assert len(lowercase_					) > 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				:							List[str]						=	len(inspect.signature(lowercase_					).parameters					)
		if num_args_in_forward_chunk_fn != len(lowercase_					):
				raise ValueError(
				    f'''forward_chunk_fn expects {num_args_in_forward_chunk_fn} arguments, but only {len(lowercase_					)} input '''
				    """tensors are given"""					)

		if chunk_size > 0:
				SCREAMING_SNAKE_CASE				:							str						=	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(lowercase_	,	dim=lowercase_					) for input_tensor in input_tensors					)
				# apply forward fn to every tuple
				SCREAMING_SNAKE_CASE				:							str						=	tuple(forward_fn(*lowercase_					) for input_tensors_chunk in zip(*lowercase_					)					)
				# concatenate output at same dimension
				return torch.cat(lowercase_	,	dim=lowercase_					)

		return forward_fn(*lowercase_					)



def 						__A						(       lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_	,	lowerCamelCase_					):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE				:							Optional[int]						=	torch.ones(lowercase_	,	lowercase_					)
		SCREAMING_SNAKE_CASE				:							Any						=	set(lowercase_					) - 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				:							List[Any]						=	head - sum(1 if h < head else 0 for h in already_pruned_heads					)
				SCREAMING_SNAKE_CASE				:							int						=	0
		SCREAMING_SNAKE_CASE				:							Any						=	mask.view(-1					).contiguous().eq(1					)
		SCREAMING_SNAKE_CASE				:							Optional[Any]						=	torch.arange(len(lowercase_					)					)[mask].long()
		return heads, index



def 						__A						(       *lowerCamelCase_	,	lowerCamelCase_ = None					):


		"""simple docstring"""



		return torch.meshgrid(*lowercase_	,	indexing=lowercase_					)



def 						__A						(       lowerCamelCase_					):


		"""simple docstring"""



		return tensor.device, storage_ptr(lowercase_					), storage_size(lowercase_					)










'''simple docstring'''




import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
							from PIL import Image




class       UpperCamelCase__			(			unittest.TestCase		,			lowercase_	):




					"""simple docstring"""


					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							int						=	load_tool("""image-captioning"""		)
							self.tool.setup()
							SCREAMING_SNAKE_CASE				:							str						=	load_tool("""image-captioning"""     ,		remote=lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	self.tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	str		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	self.remote_tool(lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							str						=	self.tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	Image.open(Path(get_tests_dir("""fixtures/tests_samples/COCO"""		)		) / """000000039769.png"""		)
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	self.remote_tool(image=lowerCamelCase_		)
							self.assertEqual(lowerCamelCase_     ,		"""two cats sleeping on a couch"""		)



