


"""simple docstring"""





from typing import Optional

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

from ...activations import ACTaFN
from ...modeling_outputs import (
    BackboneOutput,
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import (
    add_code_sample_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    logging,
    replace_return_docstrings,
)
from ...utils.backbone_utils import BackboneMixin
from .configuration_resnet import ResNetConfig


__UpperCAmelCase     					=   logging.get_logger(__name__)

# General docstring
__UpperCAmelCase     					=   "ResNetConfig"

# Base docstring
__UpperCAmelCase     					=   "microsoft/resnet-50"
__UpperCAmelCase     					=   [1, 2_048, 7, 7]

# Image classification docstring
__UpperCAmelCase     					=   "microsoft/resnet-50"
__UpperCAmelCase     					=   "tiger cat"

__UpperCAmelCase     					=   [
    "microsoft/resnet-50",
    # See all resnet models at https://huggingface.co/models?filter=resnet
]

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				List[str]		,	_lowercase			:				str		,	_lowercase			:				int		,	_lowercase			:				Dict = 3		,	_lowercase			:				List[Any] = 1		,	_lowercase			:				str = "relu"     ):
        super().__init__()
        lowerCAmelCase__     :     Optional[int]										=       nn.Convad(
            _lowercase		,	_lowercase		,	kernel_size=_lowercase		,	stride=_lowercase		,	padding=kernel_size // 2		,	bias=_lowercase     )
        lowerCAmelCase__     :     Any										=       nn.BatchNormad(_lowercase     )
        lowerCAmelCase__     :     Union[str, Any]										=       ACTaFN[activation] if activation is not None else nn.Identity()







      def 					_lowerCAmelCase							(   self			:				Optional[Any]		,	_lowercase			:				Any     ):
        lowerCAmelCase__     :     Optional[int]										=       self.convolution(_lowercase     )
        lowerCAmelCase__     :     Dict										=       self.normalization(_lowercase     )
        lowerCAmelCase__     :     int										=       self.activation(_lowercase     )
        return hidden_state

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				Dict		,	_lowercase			:				Optional[Any]     ):
        super().__init__()
        lowerCAmelCase__     :     Tuple										=       ResNetConvLayer(
            config.num_channels		,	config.embedding_size		,	kernel_size=7		,	stride=2		,	activation=config.hidden_act     )
        lowerCAmelCase__     :     Optional[Any]										=       nn.MaxPoolad(kernel_size=3		,	stride=2		,	padding=1     )
        lowerCAmelCase__     :     Union[str, Any]										=       config.num_channels







      def 					_lowerCAmelCase							(   self			:				Dict		,	_lowercase			:				Any     ):
        lowerCAmelCase__     :     Optional[Any]										=       pixel_values.shape[1]
        if num_channels != self.num_channels:
          raise ValueError(
              "Make sure that the channel dimension of the pixel values match with the one set in the configuration."     )
        lowerCAmelCase__     :     Any										=       self.embedder(_lowercase     )
        lowerCAmelCase__     :     List[Any]										=       self.pooler(_lowercase     )
        return embedding

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				Any		,	_lowercase			:				Optional[int]		,	_lowercase			:				int		,	_lowercase			:				List[str] = 2     ):
        super().__init__()
        lowerCAmelCase__     :     Optional[int]										=       nn.Convad(_lowercase		,	_lowercase		,	kernel_size=1		,	stride=_lowercase		,	bias=_lowercase     )
        lowerCAmelCase__     :     Optional[Any]										=       nn.BatchNormad(_lowercase     )







      def 					_lowerCAmelCase							(   self			:				Dict		,	_lowercase			:				Tuple     ):
        lowerCAmelCase__     :     List[str]										=       self.convolution(_lowercase     )
        lowerCAmelCase__     :     List[str]										=       self.normalization(_lowercase     )
        return hidden_state

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				Any		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				str		,	_lowercase			:				Dict = 1		,	_lowercase			:				int = "relu"     ):
        super().__init__()
        lowerCAmelCase__     :     Optional[Any]										=       in_channels != out_channels or stride != 1
        lowerCAmelCase__     :     Tuple										=       (
            ResNetShortCut(_lowercase		,	_lowercase		,	stride=_lowercase     ) if should_apply_shortcut else nn.Identity()
        )
        lowerCAmelCase__     :     Tuple										=       nn.Sequential(
            ResNetConvLayer(_lowercase		,	_lowercase		,	stride=_lowercase     )		,	ResNetConvLayer(_lowercase		,	_lowercase		,	activation=_lowercase     )		,	)
        lowerCAmelCase__     :     Optional[int]										=       ACTaFN[activation]







      def 					_lowerCAmelCase							(   self			:				Tuple		,	_lowercase			:				List[str]     ):
        lowerCAmelCase__     :     int										=       hidden_state
        lowerCAmelCase__     :     Union[str, Any]										=       self.layer(_lowercase     )
        lowerCAmelCase__     :     Optional[Any]										=       self.shortcut(_lowercase     )
        hidden_state += residual
        lowerCAmelCase__     :     Tuple										=       self.activation(_lowercase     )
        return hidden_state

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				List[str]		,	_lowercase			:				Optional[Any]		,	_lowercase			:				str		,	_lowercase			:				List[Any] = 1		,	_lowercase			:				Any = "relu"		,	_lowercase			:				List[Any] = 4     ):
        super().__init__()
        lowerCAmelCase__     :     int										=       in_channels != out_channels or stride != 1
        lowerCAmelCase__     :     List[str]										=       out_channels // reduction
        lowerCAmelCase__     :     Dict										=       (
            ResNetShortCut(_lowercase		,	_lowercase		,	stride=_lowercase     ) if should_apply_shortcut else nn.Identity()
        )
        lowerCAmelCase__     :     List[Any]										=       nn.Sequential(
            ResNetConvLayer(_lowercase		,	_lowercase		,	kernel_size=1     )		,	ResNetConvLayer(_lowercase		,	_lowercase		,	stride=_lowercase     )		,	ResNetConvLayer(_lowercase		,	_lowercase		,	kernel_size=1		,	activation=_lowercase     )		,	)
        lowerCAmelCase__     :     Any										=       ACTaFN[activation]







      def 					_lowerCAmelCase							(   self			:				int		,	_lowercase			:				Optional[int]     ):
        lowerCAmelCase__     :     Dict										=       hidden_state
        lowerCAmelCase__     :     str										=       self.layer(_lowercase     )
        lowerCAmelCase__     :     str										=       self.shortcut(_lowercase     )
        hidden_state += residual
        lowerCAmelCase__     :     Union[str, Any]										=       self.activation(_lowercase     )
        return hidden_state

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				str		,	_lowercase			:				str		,	_lowercase			:				int		,	_lowercase			:				int		,	_lowercase			:				List[str] = 2		,	_lowercase			:				List[Any] = 2		,	):
        super().__init__()

        lowerCAmelCase__     :     Tuple										=       ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

        lowerCAmelCase__     :     List[Any]										=       nn.Sequential(
            # downsampling is done in the first layer with stride of 2
            layer(_lowercase		,	_lowercase		,	stride=_lowercase		,	activation=config.hidden_act     )		,	*[layer(_lowercase		,	_lowercase		,	activation=config.hidden_act     ) for _ in range(depth - 1     )]		,	)







      def 					_lowerCAmelCase							(   self			:				Union[str, Any]		,	_lowercase			:				Tuple     ):
        lowerCAmelCase__     :     int										=       input
        for layer in self.layers:
          lowerCAmelCase__     :     str										=       layer(_lowercase     )
        return hidden_state

class 			lowercase_	(			nn.Module    ):







      def __init__(   self			:				str		,	_lowercase			:				Union[str, Any]     ):
        super().__init__()
        lowerCAmelCase__     :     Union[str, Any]										=       nn.ModuleList([]     )
        # based on `downsample_in_first_stage` the first layer of the first stage may or may not downsample the input
        self.stages.append(
            ResNetStage(
                _lowercase		,	config.embedding_size		,	config.hidden_sizes[0]		,	stride=2 if config.downsample_in_first_stage else 1		,	depth=config.depths[0]		,	)     )
        lowerCAmelCase__     :     List[str]										=       zip(config.hidden_sizes		,	config.hidden_sizes[1:]     )
        for (in_channels, out_channels), depth in zip(_lowercase		,	config.depths[1:]     ):
          self.stages.append(ResNetStage(_lowercase		,	_lowercase		,	_lowercase		,	depth=_lowercase     )     )







      def 					_lowerCAmelCase							(   self			:				int		,	_lowercase			:				Tuple		,	_lowercase			:				Union[str, Any] = False		,	_lowercase			:				int = True     ):
        lowerCAmelCase__     :     Tuple										=       () if output_hidden_states else None

        for stage_module in self.stages:
          if output_hidden_states:
            lowerCAmelCase__     :     List[Any]										=       hidden_states + (hidden_state,)

          lowerCAmelCase__     :     List[Any]										=       stage_module(_lowercase     )

        if output_hidden_states:
          lowerCAmelCase__     :     int										=       hidden_states + (hidden_state,)

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

        return BaseModelOutputWithNoAttention(
            last_hidden_state=_lowercase		,	hidden_states=_lowercase		,	)

class 			lowercase_	(			a_    ):
      __magic_name__  :				Tuple 							=					ResNetConfig
      __magic_name__  :				Tuple 							=					'resnet'
      __magic_name__  :				List[Any] 							=					'pixel_values'
      __magic_name__  :				Union[str, Any] 							=					True







      def 					_lowerCAmelCase							(   self			:				Union[str, Any]		,	_lowercase			:				Optional[Any]     ):
        if isinstance(_lowercase		,	nn.Convad     ):
          nn.init.kaiming_normal_(module.weight		,	mode="fan_out"		,	nonlinearity="relu"     )
        elif isinstance(_lowercase		,	(nn.BatchNormad, nn.GroupNorm)     ):
          nn.init.constant_(module.weight		,	1     )
          nn.init.constant_(module.bias		,	0     )







      def 					_lowerCAmelCase							(   self			:				Optional[Any]		,	_lowercase			:				Union[str, Any]		,	_lowercase			:				Tuple=False     ):
        if isinstance(_lowercase		,	_lowercase     ):
          lowerCAmelCase__     :     Optional[Any]										=       value
__UpperCAmelCase     					=   R"\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    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 ([`ResNetConfig`]): 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"

__UpperCAmelCase     					=   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            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"

@add_start_docstrings(
    """The bare ResNet model outputting raw features without any specific head on top."""				,				a_				,				)
class 			lowercase_	(			a_    ):







      def __init__(   self			:				List[str]		,	_lowercase			:				List[str]     ):
        super().__init__(_lowercase     )
        lowerCAmelCase__     :     Optional[int]										=       config
        lowerCAmelCase__     :     int										=       ResNetEmbeddings(_lowercase     )
        lowerCAmelCase__     :     str										=       ResNetEncoder(_lowercase     )
        lowerCAmelCase__     :     Dict										=       nn.AdaptiveAvgPoolad((1, 1)     )
        # Initialize weights and apply final processing
        self.post_init()







      @add_start_docstrings_to_model_forward(_lowercase     )
      @add_code_sample_docstrings(
          checkpoint=_CHECKPOINT_FOR_DOC		,	output_type=_lowercase		,	config_class=_CONFIG_FOR_DOC		,	modality="vision"		,	expected_output=_EXPECTED_OUTPUT_SHAPE		,	)
      def 					_lowerCAmelCase							(   self			:				Tuple		,	_lowercase			:				int		,	_lowercase			:				Optional[int] = None		,	_lowercase			:				Dict = None     ):
        lowerCAmelCase__     :     Any										=       (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        lowerCAmelCase__     :     Optional[Any]										=       return_dict if return_dict is not None else self.config.use_return_dict

        lowerCAmelCase__     :     int										=       self.embedder(_lowercase     )

        lowerCAmelCase__     :     Any										=       self.encoder(
            _lowercase		,	output_hidden_states=_lowercase		,	return_dict=_lowercase     )

        lowerCAmelCase__     :     Dict										=       encoder_outputs[0]

        lowerCAmelCase__     :     Optional[Any]										=       self.pooler(_lowercase     )

        if not return_dict:
          return (last_hidden_state, pooled_output) + encoder_outputs[1:]

        return BaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=_lowercase		,	pooler_output=_lowercase		,	hidden_states=encoder_outputs.hidden_states		,	)

@add_start_docstrings(
    """\n    ResNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    """				,				a_				,				)
class 			lowercase_	(			a_    ):







      def __init__(   self			:				int		,	_lowercase			:				List[Any]     ):
        super().__init__(_lowercase     )
        lowerCAmelCase__     :     List[str]										=       config.num_labels
        lowerCAmelCase__     :     Any										=       ResNetModel(_lowercase     )
        # classification head
        lowerCAmelCase__     :     List[str]										=       nn.Sequential(
            nn.Flatten()		,	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(_lowercase     )
      @add_code_sample_docstrings(
          checkpoint=_IMAGE_CLASS_CHECKPOINT		,	output_type=_lowercase		,	config_class=_CONFIG_FOR_DOC		,	expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT		,	)
      def 					_lowerCAmelCase							(   self			:				str		,	_lowercase			:				Optional[Any] = None		,	_lowercase			:				Union[str, Any] = None		,	_lowercase			:				Dict = None		,	_lowercase			:				Optional[Any] = None		,	):
        lowerCAmelCase__     :     List[str]										=       return_dict if return_dict is not None else self.config.use_return_dict

        lowerCAmelCase__     :     Union[str, Any]										=       self.resnet(_lowercase		,	output_hidden_states=_lowercase		,	return_dict=_lowercase     )

        lowerCAmelCase__     :     Optional[int]										=       outputs.pooler_output if return_dict else outputs[1]

        lowerCAmelCase__     :     Tuple										=       self.classifier(_lowercase     )

        lowerCAmelCase__     :     Optional[int]										=       None

        if labels is not None:
          if self.config.problem_type is None:
            if self.num_labels == 1:
              lowerCAmelCase__     :     Tuple										=       "regression"
            elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
              lowerCAmelCase__     :     List[str]										=       "single_label_classification"
            else:
              lowerCAmelCase__     :     List[Any]										=       "multi_label_classification"
          if self.config.problem_type == "regression":
            lowerCAmelCase__     :     Union[str, Any]										=       MSELoss()
            if self.num_labels == 1:
              lowerCAmelCase__     :     Tuple										=       loss_fct(logits.squeeze()		,	labels.squeeze()     )
            else:
              lowerCAmelCase__     :     str										=       loss_fct(_lowercase		,	_lowercase     )
          elif self.config.problem_type == "single_label_classification":
            lowerCAmelCase__     :     List[str]										=       CrossEntropyLoss()
            lowerCAmelCase__     :     int										=       loss_fct(logits.view(-1		,	self.num_labels     )		,	labels.view(-1     )     )
          elif self.config.problem_type == "multi_label_classification":
            lowerCAmelCase__     :     Any										=       BCEWithLogitsLoss()
            lowerCAmelCase__     :     Dict										=       loss_fct(_lowercase		,	_lowercase     )

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

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

@add_start_docstrings(
    """\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    """				,				a_				,				)
class 			lowercase_	(			a_				,				a_    ):







      def __init__(   self			:				str		,	_lowercase			:				int     ):
        super().__init__(_lowercase     )
        super()._init_backbone(_lowercase     )

        lowerCAmelCase__     :     str										=       [config.embedding_size] + config.hidden_sizes
        lowerCAmelCase__     :     Any										=       ResNetEmbeddings(_lowercase     )
        lowerCAmelCase__     :     Optional[Any]										=       ResNetEncoder(_lowercase     )

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







      @add_start_docstrings_to_model_forward(_lowercase     )
      @replace_return_docstrings(output_type=_lowercase		,	config_class=_CONFIG_FOR_DOC     )
      def 					_lowerCAmelCase							(   self			:				Union[str, Any]		,	_lowercase			:				Dict		,	_lowercase			:				Optional[int] = None		,	_lowercase			:				int = None     ):
        lowerCAmelCase__     :     str										=       return_dict if return_dict is not None else self.config.use_return_dict
        lowerCAmelCase__     :     Dict										=       (
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )

        lowerCAmelCase__     :     Union[str, Any]										=       self.embedder(_lowercase     )

        lowerCAmelCase__     :     List[Any]										=       self.encoder(_lowercase		,	output_hidden_states=_lowercase		,	return_dict=_lowercase     )

        lowerCAmelCase__     :     List[Any]										=       outputs.hidden_states

        lowerCAmelCase__     :     Optional[int]										=       ()
        for idx, stage in enumerate(self.stage_names     ):
          if stage in self.out_features:
            feature_maps += (hidden_states[idx],)

        if not return_dict:
          lowerCAmelCase__     :     Optional[Any]										=       (feature_maps,)
          if output_hidden_states:
            output += (outputs.hidden_states,)
          return output

        return BackboneOutput(
            feature_maps=_lowercase		,	hidden_states=outputs.hidden_states if output_hidden_states else None		,	attentions=_lowercase		,	)


"""simple docstring"""


def  lowercase						(	lowerCAmelCase__						):
			lowerCamelCase_					=  generate_pascal_triangle(lowerCAmelCase__						)
			for row_idx in range(lowerCAmelCase__						):
						# Print left spaces
						for _ in range(num_rows - row_idx - 1						):
									print(end=''' '''						)
						# Print row values
						for col_idx in range(row_idx + 1						):
									if col_idx != row_idx:
												print(triangle[row_idx][col_idx]	,end=''' '''						)
									else:
												print(triangle[row_idx][col_idx]	,end=''''''						)
						print()






def  lowercase						(	lowerCAmelCase__						):

			if not isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						raise TypeError('''The input value of \'num_rows\' should be \'int\''''						)

			if num_rows == 0:
						return []
			elif num_rows < 0:
						raise ValueError(
						    '''The input value of \'num_rows\' should be greater than or equal to 0'''						)

			lowerCamelCase_					=  []
			for current_row_idx in range(lowerCAmelCase__						):
						lowerCamelCase_					=  populate_current_row(lowerCAmelCase__	,lowerCAmelCase__						)
						triangle.append(lowerCAmelCase__						)
			return triangle






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__						):
			lowerCamelCase_					=  [-1] * (current_row_idx + 1)
			# first and last elements of current row are equal to 1
			lowerCamelCase_		, lowerCamelCase_					=  1, 1
			for current_col_idx in range(1	,lowerCAmelCase__						):
						calculate_current_element(
						    lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__						)
			return current_row






def  lowercase						(	lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__	,lowerCAmelCase__	,):
			lowerCamelCase_					=  triangle[current_row_idx - 1][current_col_idx - 1]
			lowerCamelCase_					=  triangle[current_row_idx - 1][current_col_idx]
			lowerCamelCase_					=  above_to_left_elt + above_to_right_elt






def  lowercase						(	lowerCAmelCase__						):

			if not isinstance(lowerCAmelCase__	,lowerCAmelCase__						):
						raise TypeError('''The input value of \'num_rows\' should be \'int\''''						)

			if num_rows == 0:
						return []
			elif num_rows < 0:
						raise ValueError(
						    '''The input value of \'num_rows\' should be greater than or equal to 0'''						)

			lowerCamelCase_					=  [[1]]

			for row_index in range(1	,lowerCAmelCase__						):
						lowerCamelCase_					=  [0] + result[-1] + [0]
						lowerCamelCase_					=  row_index + 1
						# Calculate the number of distinct elements in a row
						lowerCamelCase_					=  sum(divmod(lowerCAmelCase__	,2						)						)
						lowerCamelCase_					=  [
						    temp_row[i - 1] + temp_row[i] for i in range(1	,distinct_elements + 1						)
						]
						lowerCamelCase_					=  row_first_half[: (row_index + 1) // 2]
						row_second_half.reverse()
						lowerCamelCase_					=  row_first_half + row_second_half
						result.append(lowerCAmelCase__						)

			return result






def  lowercase						(	):
			from collections.abc import Callable
			from timeit import timeit

			def benchmark_a_function(lowerCAmelCase__	,lowerCAmelCase__						) -> None:
						lowerCamelCase_					=  f"{func.__name__}({value})"
						lowerCamelCase_					=  timeit(f"__main__.{call}"	,setup='''import __main__'''						)
						# print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
						print(f"{call:38} -- {timing:.4f} seconds"						)

			for value in range(15						):  # (1, 7, 14):
						for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
									benchmark_a_function(lowerCAmelCase__	,lowerCAmelCase__						)
						print()


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()



