





from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


__A				:    Optional[Any]       =   logging.get_logger(__name__)

# General docstring
__A				:    Optional[int]       =   "RegNetConfig"

# Base docstring
__A				:    str       =   "facebook/regnet-y-040"
__A				:    str       =   [1, 1_088, 7, 7]

# Image classification docstring
__A				:    List[str]       =   "facebook/regnet-y-040"
__A				:    Optional[Any]       =   "tabby, tabby cat"

__A				:    Optional[Any]       =   [
    "facebook/regnet-y-040",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A = 3							,					_A = 1							,					_A = 1							,					_A = "relu"							,					**_A							,					):






				'''simple docstring'''







				super().__init__(**_A		)
				# The padding and conv has been verified in
				# https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
				UpperCAmelCase   		=				tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2		)
				UpperCAmelCase   		=				tf.keras.layers.ConvaD(
				    filters=_A							,					kernel_size=_A							,					strides=_A							,					padding='''VALID'''							,					groups=_A							,					use_bias=_A							,					name='''convolution'''							,					)
				UpperCAmelCase   		=				tf.keras.layers.BatchNormalization(epsilon=1E-5							,					momentum=0.9							,					name='''normalization'''		)
				UpperCAmelCase   		=				ACTaFN[activation] if activation is not None else tf.identity







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.convolution(self.padding(_A		)		)
				UpperCAmelCase   		=				self.normalization(_A		)
				UpperCAmelCase   		=				self.activation(_A		)
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				config.num_channels
				UpperCAmelCase   		=				TFRegNetConvLayer(
				    out_channels=config.embedding_size							,					kernel_size=3							,					stride=2							,					activation=config.hidden_act							,					name='''embedder'''							,					)







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				shape_list(_A		)[1]
				if tf.executing_eagerly() and 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.'''		)

				# When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
				# So change the input format from `NCHW` to `NHWC`.
				# shape = (batch_size, in_height, in_width, in_channels=num_channels)
				UpperCAmelCase   		=				tf.transpose(_A							,					perm=(0, 2, 3, 1)		)
				UpperCAmelCase   		=				self.embedder(_A		)
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A = 2							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				tf.keras.layers.ConvaD(
				    filters=_A							,					kernel_size=1							,					strides=_A							,					use_bias=_A							,					name='''convolution'''		)
				UpperCAmelCase   		=				tf.keras.layers.BatchNormalization(epsilon=1E-5							,					momentum=0.9							,					name='''normalization'''		)







	def      _lowercase				(    self							,					_A							,					_A = False		):






				'''simple docstring'''







				return self.normalization(self.convolution(_A		)							,					training=_A		)

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A							,					name='''pooler'''		)
				UpperCAmelCase   		=				[
				    tf.keras.layers.ConvaD(filters=_A							,					kernel_size=1							,					activation='''relu'''							,					name='''attention.0'''		),
				    tf.keras.layers.ConvaD(filters=_A							,					kernel_size=1							,					activation='''sigmoid'''							,					name='''attention.2'''		),
				]







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				self.pooler(_A		)
				for layer_module in self.attention:
							UpperCAmelCase   		=				layer_module(_A		)
				UpperCAmelCase   		=				hidden_state * pooled
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A							,					_A							,					_A = 1							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				in_channels != out_channels or stride != 1
				UpperCAmelCase   		=				max(1							,					out_channels // config.groups_width		)
				UpperCAmelCase   		=				(
				    TFRegNetShortCut(_A							,					stride=_A							,					name='''shortcut'''		)
				    if should_apply_shortcut
				    else tf.keras.layers.Activation('''linear'''							,					name='''shortcut'''		)
				)
				# `self.layers` instead of `self.layer` because that is a reserved argument.
				UpperCAmelCase   		=				[
				    TFRegNetConvLayer(_A							,					kernel_size=1							,					activation=config.hidden_act							,					name='''layer.0'''		),
				    TFRegNetConvLayer(
				        _A							,					stride=_A							,					groups=_A							,					activation=config.hidden_act							,					name='''layer.1'''		),
				    TFRegNetConvLayer(_A							,					kernel_size=1							,					activation=_A							,					name='''layer.2'''		),
				]
				UpperCAmelCase   		=				ACTaFN[config.hidden_act]







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				hidden_state
				for layer_module in self.layers:
							UpperCAmelCase   		=				layer_module(_A		)
				UpperCAmelCase   		=				self.shortcut(_A		)
				hidden_state += residual
				UpperCAmelCase   		=				self.activation(_A		)
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A							,					_A							,					_A = 1							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				in_channels != out_channels or stride != 1
				UpperCAmelCase   		=				max(1							,					out_channels // config.groups_width		)
				UpperCAmelCase   		=				(
				    TFRegNetShortCut(_A							,					stride=_A							,					name='''shortcut'''		)
				    if should_apply_shortcut
				    else tf.keras.layers.Activation('''linear'''							,					name='''shortcut'''		)
				)
				UpperCAmelCase   		=				[
				    TFRegNetConvLayer(_A							,					kernel_size=1							,					activation=config.hidden_act							,					name='''layer.0'''		),
				    TFRegNetConvLayer(
				        _A							,					stride=_A							,					groups=_A							,					activation=config.hidden_act							,					name='''layer.1'''		),
				    TFRegNetSELayer(_A							,					reduced_channels=int(round(in_channels / 4		)		)							,					name='''layer.2'''		),
				    TFRegNetConvLayer(_A							,					kernel_size=1							,					activation=_A							,					name='''layer.3'''		),
				]
				UpperCAmelCase   		=				ACTaFN[config.hidden_act]







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase   		=				hidden_state
				for layer_module in self.layers:
							UpperCAmelCase   		=				layer_module(_A		)
				UpperCAmelCase   		=				self.shortcut(_A		)
				hidden_state += residual
				UpperCAmelCase   		=				self.activation(_A		)
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					_A							,					_A							,					_A = 2							,					_A = 2							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)

				UpperCAmelCase   		=				TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer
				UpperCAmelCase   		=				[
				    # downsampling is done in the first layer with stride of 2
				    layer(_A							,					_A							,					_A							,					stride=_A							,					name='''layers.0'''		),
				    *[layer(_A							,					_A							,					_A							,					name=F"""layers.{i+1}"""		) for i in range(depth - 1		)],
				]







	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				for layer_module in self.layers:
							UpperCAmelCase   		=				layer_module(_A		)
				return hidden_state

class 	A_       (tf.keras.layers.Layer			):
	def __init__(    self							,					_A							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				[]
				# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
				self.stages.append(
				    TFRegNetStage(
				        _A							,					config.embedding_size							,					config.hidden_sizes[0]							,					stride=2 if config.downsample_in_first_stage else 1							,					depth=config.depths[0]							,					name='''stages.0'''							,					)		)
				UpperCAmelCase   		=				zip(config.hidden_sizes							,					config.hidden_sizes[1:]		)
				for i, ((in_channels, out_channels), depth) in enumerate(zip(_A							,					config.depths[1:]		)		):
							self.stages.append(TFRegNetStage(_A							,					_A							,					_A							,					depth=_A							,					name=F"""stages.{i+1}"""		)		)







	def      _lowercase				(    self							,					_A							,					_A = False							,					_A = True		):






				'''simple docstring'''







				UpperCAmelCase   		=				() if output_hidden_states else None

				for stage_module in self.stages:
							if output_hidden_states:
										UpperCAmelCase   		=				hidden_states + (hidden_state,)

							UpperCAmelCase   		=				stage_module(_A		)

				if output_hidden_states:
							UpperCAmelCase   		=				hidden_states + (hidden_state,)

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

				return TFBaseModelOutputWithNoAttention(last_hidden_state=_A							,					hidden_states=_A		)

@keras_serializable
class 	A_       (tf.keras.layers.Layer			):
	UpperCAmelCase__					=			RegNetConfig
	def __init__(    self							,					_A							,					**_A		):






				'''simple docstring'''







				super().__init__(**_A		)
				UpperCAmelCase   		=				config
				UpperCAmelCase   		=				TFRegNetEmbeddings(_A							,					name='''embedder'''		)
				UpperCAmelCase   		=				TFRegNetEncoder(_A							,					name='''encoder'''		)
				UpperCAmelCase   		=				tf.keras.layers.GlobalAveragePoolingaD(keepdims=_A							,					name='''pooler'''		)







	@unpack_inputs
	def      _lowercase				(    self							,					_A							,					_A = None							,					_A = None							,					_A = False							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase   		=				return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase   		=				self.embedder(_A							,					training=_A		)

				UpperCAmelCase   		=				self.encoder(
				    _A							,					output_hidden_states=_A							,					return_dict=_A							,					training=_A		)

				UpperCAmelCase   		=				encoder_outputs[0]
				UpperCAmelCase   		=				self.pooler(_A		)

				# Change to NCHW output format have uniformity in the modules
				UpperCAmelCase   		=				tf.transpose(_A							,					perm=(0, 3, 1, 2)		)
				UpperCAmelCase   		=				tf.transpose(_A							,					perm=(0, 3, 1, 2)		)

				# Change the other hidden state outputs to NCHW as well
				if output_hidden_states:
							UpperCAmelCase   		=				tuple([tf.transpose(_A							,					perm=(0, 3, 1, 2)		) for h in encoder_outputs[1]]		)

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

				return TFBaseModelOutputWithPoolingAndNoAttention(
				    last_hidden_state=_A							,					pooler_output=_A							,					hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states							,					)

class 	A_       (a_			):
	UpperCAmelCase__					=			RegNetConfig
	UpperCAmelCase__					=			'''regnet'''
	UpperCAmelCase__					=			'''pixel_values'''
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4)							,					dtype=tf.floataa		)}
__A				:    str       =   R"\n    Parameters:\n    This model is a Tensorflow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n    behavior.\n        config ([`RegNetConfig`]): 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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"

__A				:    List[Any]       =   R"\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\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 RegNet model outputting raw features without any specific head on top.'''     ,							a_     ,							)
class 	A_       (a_			):
	def __init__(    self							,					_A							,					*_A							,					**_A		):






				'''simple docstring'''







				super().__init__(_A							,					*_A							,					**_A		)
				UpperCAmelCase   		=				TFRegNetMainLayer(_A							,					name='''regnet'''		)







	@unpack_inputs
	@add_start_docstrings_to_model_forward(_A		)
	@add_code_sample_docstrings(
	    checkpoint=_CHECKPOINT_FOR_DOC							,					output_type=_A							,					config_class=_CONFIG_FOR_DOC							,					modality='''vision'''							,					expected_output=_EXPECTED_OUTPUT_SHAPE							,					)
	def      _lowercase				(    self							,					_A							,					_A = None							,					_A = None							,					_A=False							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase   		=				return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase   		=				self.regnet(
				    pixel_values=_A							,					output_hidden_states=_A							,					return_dict=_A							,					training=_A							,					)
				if not return_dict:
							return (outputs[0],) + outputs[1:]

				return TFBaseModelOutputWithPoolingAndNoAttention(
				    last_hidden_state=outputs.last_hidden_state							,					pooler_output=outputs.pooler_output							,					hidden_states=outputs.hidden_states							,					)

@add_start_docstrings(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''     ,							a_     ,							)
class 	A_       (a_     ,							a_			):
	def __init__(    self							,					_A							,					*_A							,					**_A		):






				'''simple docstring'''







				super().__init__(_A							,					*_A							,					**_A		)
				UpperCAmelCase   		=				config.num_labels
				UpperCAmelCase   		=				TFRegNetMainLayer(_A							,					name='''regnet'''		)
				# classification head
				UpperCAmelCase   		=				[
				    tf.keras.layers.Flatten(),
				    tf.keras.layers.Dense(config.num_labels							,					name='''classifier.1'''		) if config.num_labels > 0 else tf.identity,
				]







	@unpack_inputs
	@add_start_docstrings_to_model_forward(_A		)
	@add_code_sample_docstrings(
	    checkpoint=_IMAGE_CLASS_CHECKPOINT							,					output_type=_A							,					config_class=_CONFIG_FOR_DOC							,					expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,					)
	def      _lowercase				(    self							,					_A = None							,					_A = None							,					_A = None							,					_A = None							,					_A=False							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				(
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase   		=				return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase   		=				self.regnet(
				    _A							,					output_hidden_states=_A							,					return_dict=_A							,					training=_A		)

				UpperCAmelCase   		=				outputs.pooler_output if return_dict else outputs[1]

				UpperCAmelCase   		=				self.classifier[0](_A		)
				UpperCAmelCase   		=				self.classifier[1](_A		)

				UpperCAmelCase   		=				None if labels is None else self.hf_compute_loss(labels=_A							,					logits=_A		)

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

				return TFSequenceClassifierOutput(loss=_A							,					logits=_A							,					hidden_states=outputs.hidden_states		)










from __future__ import annotations

from collections.abc import Callable





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__ = 100			,			)  ->				float:







			'''simple docstring'''

			UpperCAmelCase   		=				x_start
			UpperCAmelCase   		=				fnc(UpperCamelCase__ )
			UpperCAmelCase   		=				0.0
			for _ in range(UpperCamelCase__ ):
						# Approximates small segments of curve as linear and solve
						# for trapezoidal area
						UpperCAmelCase   		=				(x_end - x_start) / steps + xa
						UpperCAmelCase   		=				fnc(UpperCamelCase__ )
						area += abs(fxa + fxa ) * (xa - xa) / 2
						# Increment step
						UpperCAmelCase   		=				xa
						UpperCAmelCase   		=				fxa
			return area


if __name__ == "__main__":





				def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				str:







							'''simple docstring'''

							return x**3 + x**2

				print("f(x) = x^3 + x^2")
				print("The area between the curve, x = -5, x = 5 and the x axis is:")
				__A				:    List[Any]       =   10
				while i <= 100_000:
								print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}')
								i *= 10



