





'''simple docstring'''







import collections.abc
from typing import Optional, Tuple, Union

import torch
import torch.utils.checkpoint
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_poolformer import PoolFormerConfig


snake_case_     :       Dict						=  logging.get_logger(__name__)

# General docstring
snake_case_     :       Optional[int]						=  "PoolFormerConfig"

# Base docstring
snake_case_     :       List[str]						=  "sail/poolformer_s12"
snake_case_     :       Dict						=  [1, 5_12, 7, 7]

# Image classification docstring
snake_case_     :       Tuple						=  "sail/poolformer_s12"
snake_case_     :       Optional[int]						=  "tabby, tabby cat"

snake_case_     :       Any						=  [
    "sail/poolformer_s12",
    # See all PoolFormer models at https://huggingface.co/models?filter=poolformer
]





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			Tuple,			SCREAMING_SNAKE_CASE__      :			float = 0.0,			SCREAMING_SNAKE_CASE__      :			bool = False   ) ->	Dict:
						if drop_prob == 0.0 or not training:
												return input
						UpperCAmelCase_			:      List[str]     						=  1 - drop_prob
						UpperCAmelCase_			:      List[str]     						=  (input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
						UpperCAmelCase_			:      List[str]     						=  keep_prob + torch.rand(SCREAMING_SNAKE_CASE__,			dtype=input.dtype,			device=input.device   )
						random_tensor.floor_()  # binarize
						UpperCAmelCase_			:      List[Any]     						=  input.div(SCREAMING_SNAKE_CASE__   ) * random_tensor
						return output


class 					__a      (nn.Module     ):

							def __init__(							self							:     Optional[Any]       ,     __magic_name__							:     Optional[float] = None   )					->       None:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      List[str]     						=  drop_prob

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     torch.Tensor   )					->       torch.Tensor:







													"""simple docstring"""
													return drop_path(__magic_name__       ,     self.drop_prob       ,     self.training   )



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







													"""simple docstring"""
													return "p={}".format(self.drop_prob   )





class 					__a      (nn.Module     ):

							def __init__(							self							:     Tuple       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     int=None   )					->       Dict:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      List[str]     						=  patch_size if isinstance(__magic_name__       ,     collections.abc.Iterable   ) else (patch_size, patch_size)
													UpperCAmelCase_			:      Optional[Any]     						=  stride if isinstance(__magic_name__       ,     collections.abc.Iterable   ) else (stride, stride)
													UpperCAmelCase_			:      List[str]     						=  padding if isinstance(__magic_name__       ,     collections.abc.Iterable   ) else (padding, padding)

													UpperCAmelCase_			:      List[str]     						=  nn.Convad(__magic_name__       ,     __magic_name__       ,     kernel_size=__magic_name__       ,     stride=__magic_name__       ,     padding=__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  norm_layer(__magic_name__   ) if norm_layer else nn.Identity()



							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     Dict   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.projection(__magic_name__   )
													UpperCAmelCase_			:      str     						=  self.norm(__magic_name__   )
													return embeddings





class 					__a      (nn.GroupNorm     ):

							def __init__(							self							:     List[str]       ,     __magic_name__							:     Optional[Any]       ,     **__magic_name__							:     int   )					->       Dict:







													"""simple docstring"""
													super().__init__(1       ,     __magic_name__       ,     **__magic_name__   )





class 					__a      (nn.Module     ):

							def __init__(							self							:     Any       ,     __magic_name__							:     List[Any]   )					->       str:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      str     						=  nn.AvgPoolad(__magic_name__       ,     stride=1       ,     padding=pool_size // 2       ,     count_include_pad=__magic_name__   )



							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     List[str]   )					->       Union[str, Any]:







													"""simple docstring"""
													return self.pool(__magic_name__   ) - hidden_states





class 					__a      (nn.Module     ):

							def __init__(							self							:     Union[str, Any]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Any       ,     __magic_name__							:     Optional[int]   )					->       Dict:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      Union[str, Any]     						=  nn.Convad(__magic_name__       ,     __magic_name__       ,     1   )
													UpperCAmelCase_			:      int     						=  nn.Convad(__magic_name__       ,     __magic_name__       ,     1   )
													UpperCAmelCase_			:      Tuple     						=  PoolFormerDropPath(__magic_name__   )
													if isinstance(config.hidden_act       ,     __magic_name__   ):
																			UpperCAmelCase_			:      Tuple     						=  ACTaFN[config.hidden_act]
													else:
																			UpperCAmelCase_			:      Dict     						=  config.hidden_act



							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Any   )					->       Optional[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  self.conva(__magic_name__   )
													UpperCAmelCase_			:      int     						=  self.act_fn(__magic_name__   )
													UpperCAmelCase_			:      List[str]     						=  self.drop(__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  self.conva(__magic_name__   )
													UpperCAmelCase_			:      int     						=  self.drop(__magic_name__   )

													return hidden_states





class 					__a      (nn.Module     ):

							def __init__(							self							:     Union[str, Any]       ,     __magic_name__							:     Dict       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     int       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     Optional[int]   )					->       Optional[int]:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      List[Any]     						=  PoolFormerPooling(__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  PoolFormerOutput(__magic_name__       ,     __magic_name__       ,     __magic_name__       ,     __magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  PoolFormerGroupNorm(__magic_name__   )
													UpperCAmelCase_			:      Optional[Any]     						=  PoolFormerGroupNorm(__magic_name__   )

													# Useful for training neural nets
													UpperCAmelCase_			:      Any     						=  PoolFormerDropPath(__magic_name__   ) if drop_path > 0.0 else nn.Identity()
													UpperCAmelCase_			:      Optional[Any]     						=  config.use_layer_scale
													if config.use_layer_scale:
																			UpperCAmelCase_			:      str     						=  nn.Parameter(
																			    config.layer_scale_init_value * torch.ones((__magic_name__)   )       ,     requires_grad=__magic_name__   )
																			UpperCAmelCase_			:      Optional[int]     						=  nn.Parameter(
																			    config.layer_scale_init_value * torch.ones((__magic_name__)   )       ,     requires_grad=__magic_name__   )



							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Tuple   )					->       Any:







													"""simple docstring"""
													if self.use_layer_scale:
																			UpperCAmelCase_			:      Dict     						=  self.pooling(self.before_norm(__magic_name__   )   )
																			UpperCAmelCase_			:      Optional[int]     						=  self.layer_scale_a.unsqueeze(-1   ).unsqueeze(-1   ) * pooling_output
																			# First residual connection
																			UpperCAmelCase_			:      List[Any]     						=  hidden_states + self.drop_path(__magic_name__   )
																			UpperCAmelCase_			:      Optional[Any]     						=  ()

																			UpperCAmelCase_			:      Optional[int]     						=  self.output(self.after_norm(__magic_name__   )   )
																			UpperCAmelCase_			:      Union[str, Any]     						=  self.layer_scale_a.unsqueeze(-1   ).unsqueeze(-1   ) * layer_output
																			# Second residual connection
																			UpperCAmelCase_			:      Union[str, Any]     						=  hidden_states + self.drop_path(__magic_name__   )

																			UpperCAmelCase_			:      Any     						=  (output,) + outputs
																			return outputs

													else:
																			UpperCAmelCase_			:      List[Any]     						=  self.drop_path(self.pooling(self.before_norm(__magic_name__   )   )   )
																			# First residual connection
																			UpperCAmelCase_			:      List[str]     						=  pooling_output + hidden_states
																			UpperCAmelCase_			:      Optional[Any]     						=  ()

																			# Second residual connection inside the PoolFormerOutput block
																			UpperCAmelCase_			:      Union[str, Any]     						=  self.drop_path(self.output(self.after_norm(__magic_name__   )   )   )
																			UpperCAmelCase_			:      List[str]     						=  hidden_states + layer_output

																			UpperCAmelCase_			:      int     						=  (output,) + outputs
																			return outputs





class 					__a      (nn.Module     ):

							def __init__(							self							:     Tuple       ,     __magic_name__							:     Optional[Any]   )					->       List[Any]:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      int     						=  config
													# stochastic depth decay rule
													UpperCAmelCase_			:      Dict     						=  [x.item() for x in torch.linspace(0       ,     config.drop_path_rate       ,     sum(config.depths   )   )]

													# patch embeddings
													UpperCAmelCase_			:      str     						=  []
													for i in range(config.num_encoder_blocks   ):
																			embeddings.append(
																			    PoolFormerEmbeddings(
																			        patch_size=config.patch_sizes[i]       ,     stride=config.strides[i]       ,     padding=config.padding[i]       ,     num_channels=config.num_channels if i == 0 else config.hidden_sizes[i - 1]       ,     hidden_size=config.hidden_sizes[i]       ,     )   )
													UpperCAmelCase_			:      Tuple     						=  nn.ModuleList(__magic_name__   )

													# Transformer blocks
													UpperCAmelCase_			:      Optional[Any]     						=  []
													UpperCAmelCase_			:      Dict     						=  0
													for i in range(config.num_encoder_blocks   ):
																			# each block consists of layers
																			UpperCAmelCase_			:      Dict     						=  []
																			if i != 0:
																									cur += config.depths[i - 1]
																			for j in range(config.depths[i]   ):
																									layers.append(
																									    PoolFormerLayer(
																									        __magic_name__       ,     num_channels=config.hidden_sizes[i]       ,     pool_size=config.pool_size       ,     hidden_size=config.hidden_sizes[i]       ,     intermediate_size=int(config.hidden_sizes[i] * config.mlp_ratio   )       ,     drop_path=dpr[cur + j]       ,     )   )
																			blocks.append(nn.ModuleList(__magic_name__   )   )

													UpperCAmelCase_			:      Optional[int]     						=  nn.ModuleList(__magic_name__   )



							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     str       ,     __magic_name__							:     List[str]=False       ,     __magic_name__							:     Tuple=True   )					->       str:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  () if output_hidden_states else None

													UpperCAmelCase_			:      Any     						=  pixel_values
													for idx, layers in enumerate(zip(self.patch_embeddings       ,     self.block   )   ):
																			UpperCAmelCase_						,       UpperCAmelCase_			:      List[Any]     						=  layers
																			# Get patch embeddings from hidden_states
																			UpperCAmelCase_			:      Optional[int]     						=  embedding_layer(__magic_name__   )
																			# Send the embeddings through the blocks
																			for _, blk in enumerate(__magic_name__   ):
																									UpperCAmelCase_			:      Optional[int]     						=  blk(__magic_name__   )
																									UpperCAmelCase_			:      Union[str, Any]     						=  layer_outputs[0]

																			if output_hidden_states:
																									UpperCAmelCase_			:      List[Any]     						=  all_hidden_states + (hidden_states,)

													if not return_dict:
																			return tuple(v for v in [hidden_states, all_hidden_states] if v is not None   )

													return BaseModelOutputWithNoAttention(last_hidden_state=__magic_name__       ,     hidden_states=__magic_name__   )





class 					__a      (lowerCamelCase     ):
							__a					:   Optional[Any]									=       PoolFormerConfig
							__a					:   str									=       "poolformer"
							__a					:   Tuple									=       "pixel_values"
							__a					:   List[Any]									=       True

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     List[str]   )					->       Any:







													"""simple docstring"""
													if isinstance(__magic_name__       ,     (nn.Linear, nn.Convad)   ):
																			module.weight.data.normal_(mean=0.0       ,     std=self.config.initializer_range   )
																			if module.bias is not None:
																									module.bias.data.zero_()
													elif isinstance(__magic_name__       ,     nn.LayerNorm   ):
																			module.bias.data.zero_()
																			module.weight.data.fill_(1.0   )



							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     List[Any]=False   )					->       Any:







													"""simple docstring"""
													if isinstance(__magic_name__       ,     __magic_name__   ):
																			UpperCAmelCase_			:      Tuple     						=  value





snake_case_     :       int						=  r"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use\n    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`PoolFormerConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n"

snake_case_     :       Optional[Any]						=  r"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n"


@add_start_docstrings(
    "The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top."       ,		lowerCamelCase       ,		)
class 					__a      (lowerCamelCase     ):

							def __init__(							self							:     int       ,     __magic_name__							:     Optional[int]   )					->       Tuple:







													"""simple docstring"""
													super().__init__(__magic_name__   )
													UpperCAmelCase_			:      str     						=  config

													UpperCAmelCase_			:      Optional[Any]     						=  PoolFormerEncoder(__magic_name__   )

													# Initialize weights and apply final processing
													self.post_init()

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Tuple:







													"""simple docstring"""
													return self.embeddings.patch_embeddings



							@add_start_docstrings_to_model_forward(__magic_name__   )
							@add_code_sample_docstrings(
							    checkpoint=_CHECKPOINT_FOR_DOC       ,     output_type=__magic_name__       ,     config_class=_CONFIG_FOR_DOC       ,     modality='''vision'''       ,     expected_output=_EXPECTED_OUTPUT_SHAPE       ,     )
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[bool] = None       ,     __magic_name__							:     Optional[bool] = None       ,     )					->       Union[Tuple, BaseModelOutputWithNoAttention]:







													"""simple docstring"""
													UpperCAmelCase_			:      List[Any]     						=  (
													    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
													)
													UpperCAmelCase_			:      Tuple     						=  return_dict if return_dict is not None else self.config.use_return_dict

													if pixel_values is None:
																			raise ValueError('''You have to specify pixel_values'''   )

													UpperCAmelCase_			:      Dict     						=  self.encoder(
													    __magic_name__       ,     output_hidden_states=__magic_name__       ,     return_dict=__magic_name__       ,     )
													UpperCAmelCase_			:      Optional[Any]     						=  encoder_outputs[0]

													if not return_dict:
																			return (sequence_output, None) + encoder_outputs[1:]

													return BaseModelOutputWithNoAttention(
													    last_hidden_state=__magic_name__       ,     hidden_states=encoder_outputs.hidden_states       ,     )





class 					__a      (nn.Module     ):

							def __init__(							self							:     Dict       ,     __magic_name__							:     List[Any]   )					->       List[Any]:







													"""simple docstring"""
													super().__init__()
													UpperCAmelCase_			:      str     						=  nn.Linear(config.hidden_size       ,     config.hidden_size   )



							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     List[str]   )					->       Dict:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  self.dense(__magic_name__   )
													return output





@add_start_docstrings(
    "\n    PoolFormer Model transformer with an image classification head on top\n    "       ,		lowerCamelCase       ,		)
class 					__a      (lowerCamelCase     ):

							def __init__(							self							:     Union[str, Any]       ,     __magic_name__							:     Tuple   )					->       Any:







													"""simple docstring"""
													super().__init__(__magic_name__   )
													UpperCAmelCase_			:      Optional[int]     						=  config.num_labels
													UpperCAmelCase_			:      Optional[Any]     						=  PoolFormerModel(__magic_name__   )

													# Final norm
													UpperCAmelCase_			:      List[Any]     						=  PoolFormerGroupNorm(config.hidden_sizes[-1]   )
													# Classifier head
													UpperCAmelCase_			:      Tuple     						=  (
													    nn.Linear(config.hidden_sizes[-1]       ,     config.num_labels   ) if config.num_labels > 0 else nn.Identity()
													)

													# Initialize weights and apply final processing
													self.post_init()



							@add_start_docstrings_to_model_forward(__magic_name__   )
							@add_code_sample_docstrings(
							    checkpoint=_IMAGE_CLASS_CHECKPOINT       ,     output_type=__magic_name__       ,     config_class=_CONFIG_FOR_DOC       ,     expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,     )
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[torch.LongTensor] = None       ,     __magic_name__							:     Optional[bool] = None       ,     __magic_name__							:     Optional[bool] = None       ,     )					->       Union[Tuple, ImageClassifierOutputWithNoAttention]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  return_dict if return_dict is not None else self.config.use_return_dict

													UpperCAmelCase_			:      Tuple     						=  self.poolformer(
													    __magic_name__       ,     output_hidden_states=__magic_name__       ,     return_dict=__magic_name__       ,     )

													UpperCAmelCase_			:      Dict     						=  outputs[0]

													UpperCAmelCase_			:      List[Any]     						=  self.classifier(self.norm(__magic_name__   ).mean([-2, -1]   )   )

													UpperCAmelCase_			:      Tuple     						=  None
													if labels is not None:
																			if self.config.problem_type is None:
																									if self.num_labels == 1:
																															UpperCAmelCase_			:      Dict     						=  '''regression'''
																									elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																															UpperCAmelCase_			:      Any     						=  '''single_label_classification'''
																									else:
																															UpperCAmelCase_			:      Tuple     						=  '''multi_label_classification'''

																			if self.config.problem_type == "regression":
																									UpperCAmelCase_			:      Optional[Any]     						=  MSELoss()
																									if self.num_labels == 1:
																															UpperCAmelCase_			:      Union[str, Any]     						=  loss_fct(logits.squeeze()       ,     labels.squeeze()   )
																									else:
																															UpperCAmelCase_			:      List[Any]     						=  loss_fct(__magic_name__       ,     __magic_name__   )
																			elif self.config.problem_type == "single_label_classification":
																									UpperCAmelCase_			:      str     						=  CrossEntropyLoss()
																									UpperCAmelCase_			:      Dict     						=  loss_fct(logits.view(-1       ,     self.num_labels   )       ,     labels.view(-1   )   )
																			elif self.config.problem_type == "multi_label_classification":
																									UpperCAmelCase_			:      int     						=  BCEWithLogitsLoss()
																									UpperCAmelCase_			:      Tuple     						=  loss_fct(__magic_name__       ,     __magic_name__   )

													if not return_dict:
																			UpperCAmelCase_			:      str     						=  (logits,) + outputs[2:]
																			return ((loss,) + output) if loss is not None else output

													return ImageClassifierOutputWithNoAttention(loss=__magic_name__       ,     logits=__magic_name__       ,     hidden_states=outputs.hidden_states   )












'''simple docstring'''







# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ..models.whisper import WhisperForConditionalGeneration, WhisperProcessor
from .base import PipelineTool


class 					__a      (lowerCamelCase     ):
							__a					:   List[Any]									=       "openai/whisper-base"
							__a					:   Optional[Any]									=       (
							    "This is a tool that transcribes an audio into text. It takes an input named `audio` and returns the "
							    "transcribed text."
							)
							__a					:   Any									=       "transcriber"
							__a					:   str									=       WhisperProcessor
							__a					:   List[Any]									=       WhisperForConditionalGeneration

							__a					:   int									=       ["audio"]
							__a					:   Optional[Any]									=       ["text"]

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     List[str]   )					->       Optional[int]:







													"""simple docstring"""
													return self.pre_processor(__magic_name__       ,     return_tensors='''pt'''   ).input_features

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Dict   )					->       Tuple:







													"""simple docstring"""
													return self.model.generate(inputs=__magic_name__   )



							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Dict   )					->       str:







													"""simple docstring"""
													return self.pre_processor.batch_decode(__magic_name__       ,     skip_special_tokens=__magic_name__   )[0]





