

'''simple docstring'''
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


_lowerCamelCase      					=     logging.get_logger(__name__)

# General docstring
_lowerCamelCase      					=     """RegNetConfig"""

# Base docstring
_lowerCamelCase      					=     """facebook/regnet-y-040"""
_lowerCamelCase      					=     [1, 1088, 7, 7]

# Image classification docstring
_lowerCamelCase      					=     """facebook/regnet-y-040"""
_lowerCamelCase      					=     """tabby, tabby cat"""

_lowerCamelCase      					=     [
    """facebook/regnet-y-040""",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]
class 					_snake_case		(tf.keras.layers.Layer):






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






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				UpperCAmelCase_     :      Union[str, Any]						=  self.convolution(self.padding(_snake_case     )     )
				UpperCAmelCase_     :      Union[str, Any]						=  self.normalization(_snake_case     )
				UpperCAmelCase_     :      str						=  self.activation(_snake_case     )
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      Tuple						=  config.num_channels
				UpperCAmelCase_     :      Dict						=  TFRegNetConvLayer(
				    out_channels=config.embedding_size			,kernel_size=3			,stride=2			,activation=config.hidden_act			,name="embedder"			,)






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				UpperCAmelCase_     :      Optional[Any]						=  shape_list(_snake_case     )[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_     :      Tuple						=  tf.transpose(_snake_case			,perm=(0, 2, 3, 1)     )
				UpperCAmelCase_     :      Optional[Any]						=  self.embedder(_snake_case     )
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,_snake_case = 2			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      Optional[Any]						=  tf.keras.layers.ConvaD(
				    filters=_snake_case			,kernel_size=1			,strides=_snake_case			,use_bias=_snake_case			,name="convolution"     )
				UpperCAmelCase_     :      Tuple						=  tf.keras.layers.BatchNormalization(epsilon=1E-5			,momentum=0.9			,name="normalization"     )






			def 		UpperCamelCase__							(  self			,_snake_case			,_snake_case = False     ):
				return self.normalization(self.convolution(_snake_case     )			,training=_snake_case     )




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,_snake_case			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      Optional[int]						=  tf.keras.layers.GlobalAveragePoolingaD(keepdims=_snake_case			,name="pooler"     )
				UpperCAmelCase_     :      Tuple						=  [
				    tf.keras.layers.ConvaD(filters=_snake_case			,kernel_size=1			,activation="relu"			,name="attention.0"     ),
				    tf.keras.layers.ConvaD(filters=_snake_case			,kernel_size=1			,activation="sigmoid"			,name="attention.2"     ),
				]






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				# [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
				UpperCAmelCase_     :      Optional[Any]						=  self.pooler(_snake_case     )
				for layer_module in self.attention:
					UpperCAmelCase_     :      Dict						=  layer_module(_snake_case     )
				UpperCAmelCase_     :      Any						=  hidden_state * pooled
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,_snake_case			,_snake_case			,_snake_case = 1			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      str						=  in_channels != out_channels or stride != 1
				UpperCAmelCase_     :      Optional[int]						=  max(1			,out_channels // config.groups_width     )
				UpperCAmelCase_     :      Tuple						=  (
				    TFRegNetShortCut(_snake_case			,stride=_snake_case			,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_     :      List[str]						=  [
				    TFRegNetConvLayer(_snake_case			,kernel_size=1			,activation=config.hidden_act			,name="layer.0"     ),
				    TFRegNetConvLayer(
				        _snake_case			,stride=_snake_case			,groups=_snake_case			,activation=config.hidden_act			,name="layer.1"     ),
				    TFRegNetConvLayer(_snake_case			,kernel_size=1			,activation=_snake_case			,name="layer.2"     ),
				]
				UpperCAmelCase_     :      List[Any]						=  ACTaFN[config.hidden_act]






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				UpperCAmelCase_     :      Optional[Any]						=  hidden_state
				for layer_module in self.layers:
					UpperCAmelCase_     :      str						=  layer_module(_snake_case     )
				UpperCAmelCase_     :      List[str]						=  self.shortcut(_snake_case     )
				hidden_state += residual
				UpperCAmelCase_     :      Any						=  self.activation(_snake_case     )
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,_snake_case			,_snake_case			,_snake_case = 1			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      int						=  in_channels != out_channels or stride != 1
				UpperCAmelCase_     :      Optional[Any]						=  max(1			,out_channels // config.groups_width     )
				UpperCAmelCase_     :      Optional[Any]						=  (
				    TFRegNetShortCut(_snake_case			,stride=_snake_case			,name="shortcut"     )
				    if should_apply_shortcut
				    else tf.keras.layers.Activation("linear"			,name="shortcut"     )
				)
				UpperCAmelCase_     :      Optional[Any]						=  [
				    TFRegNetConvLayer(_snake_case			,kernel_size=1			,activation=config.hidden_act			,name="layer.0"     ),
				    TFRegNetConvLayer(
				        _snake_case			,stride=_snake_case			,groups=_snake_case			,activation=config.hidden_act			,name="layer.1"     ),
				    TFRegNetSELayer(_snake_case			,reduced_channels=int(round(in_channels / 4     )     )			,name="layer.2"     ),
				    TFRegNetConvLayer(_snake_case			,kernel_size=1			,activation=_snake_case			,name="layer.3"     ),
				]
				UpperCAmelCase_     :      List[str]						=  ACTaFN[config.hidden_act]






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				UpperCAmelCase_     :      List[Any]						=  hidden_state
				for layer_module in self.layers:
					UpperCAmelCase_     :      Union[str, Any]						=  layer_module(_snake_case     )
				UpperCAmelCase_     :      Tuple						=  self.shortcut(_snake_case     )
				hidden_state += residual
				UpperCAmelCase_     :      Dict						=  self.activation(_snake_case     )
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,_snake_case			,_snake_case			,_snake_case = 2			,_snake_case = 2			,**_snake_case     ):
				super().__init__(**_snake_case     )

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






			def 		UpperCamelCase__							(  self			,_snake_case     ):
				for layer_module in self.layers:
					UpperCAmelCase_     :      int						=  layer_module(_snake_case     )
				return hidden_state




class 					_snake_case		(tf.keras.layers.Layer):






			def __init__(  self			,_snake_case			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      Optional[int]						=  []
				# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
				self.stages.append(
				    TFRegNetStage(
				        _snake_case			,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_     :      Union[str, Any]						=  zip(config.hidden_sizes			,config.hidden_sizes[1:]     )
				for i, ((in_channels, out_channels), depth) in enumerate(zip(_snake_case			,config.depths[1:]     )     ):
					self.stages.append(TFRegNetStage(_snake_case			,_snake_case			,_snake_case			,depth=_snake_case			,name=f'''stages.{i+1}'''     )     )






			def 		UpperCamelCase__							(  self			,_snake_case			,_snake_case = False			,_snake_case = True     ):
				UpperCAmelCase_     :      Union[str, Any]						=  () if output_hidden_states else None

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

					UpperCAmelCase_     :      int						=  stage_module(_snake_case     )

				if output_hidden_states:
					UpperCAmelCase_     :      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 TFBaseModelOutputWithNoAttention(last_hidden_state=_snake_case			,hidden_states=_snake_case     )




@keras_serializable
class 					_snake_case		(tf.keras.layers.Layer):
			__A  :	List[Any]        =RegNetConfig






			def __init__(  self			,_snake_case			,**_snake_case     ):
				super().__init__(**_snake_case     )
				UpperCAmelCase_     :      Any						=  config
				UpperCAmelCase_     :      int						=  TFRegNetEmbeddings(_snake_case			,name="embedder"     )
				UpperCAmelCase_     :      List[str]						=  TFRegNetEncoder(_snake_case			,name="encoder"     )
				UpperCAmelCase_     :      Optional[Any]						=  tf.keras.layers.GlobalAveragePoolingaD(keepdims=_snake_case			,name="pooler"     )






			@unpack_inputs
			def 		UpperCamelCase__							(  self			,_snake_case			,_snake_case = None			,_snake_case = None			,_snake_case = False			,):
				UpperCAmelCase_     :      List[str]						=  (
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase_     :      Dict						=  return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase_     :      Optional[int]						=  self.embedder(_snake_case			,training=_snake_case     )

				UpperCAmelCase_     :      List[Any]						=  self.encoder(
				    _snake_case			,output_hidden_states=_snake_case			,return_dict=_snake_case			,training=_snake_case     )

				UpperCAmelCase_     :      List[str]						=  encoder_outputs[0]
				UpperCAmelCase_     :      Optional[int]						=  self.pooler(_snake_case     )

				# Change to NCHW output format have uniformity in the modules
				UpperCAmelCase_     :      Dict						=  tf.transpose(_snake_case			,perm=(0, 3, 1, 2)     )
				UpperCAmelCase_     :      List[str]						=  tf.transpose(_snake_case			,perm=(0, 3, 1, 2)     )

				# Change the other hidden state outputs to NCHW as well
				if output_hidden_states:
					UpperCAmelCase_     :      int						=  tuple([tf.transpose(_snake_case			,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=_snake_case			,pooler_output=_snake_case			,hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states			,)




class 					_snake_case		(__SCREAMING_SNAKE_CASE):
			__A  :	Union[str, Any]        =RegNetConfig
			__A  :	Optional[Any]        ="regnet"
			__A  :	List[Any]        ="pixel_values"






			@property
			def 		UpperCamelCase__							(  self     ):
				return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_24, 2_24)			,dtype=tf.floataa     )}




_lowerCamelCase      					=     R"""
    Parameters:
    This model is a Tensorflow
    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and
    behavior.
        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.
"""

_lowerCamelCase      					=     R"""
    Args:
        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConveNextImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
"""
@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."			,			__SCREAMING_SNAKE_CASE			,			)
class 					_snake_case		(__SCREAMING_SNAKE_CASE):






			def __init__(  self			,_snake_case			,*_snake_case			,**_snake_case     ):
				super().__init__(_snake_case			,*_snake_case			,**_snake_case     )
				UpperCAmelCase_     :      Optional[int]						=  TFRegNetMainLayer(_snake_case			,name="regnet"     )






			@unpack_inputs
			@add_start_docstrings_to_model_forward(_snake_case     )
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC			,output_type=_snake_case			,config_class=_CONFIG_FOR_DOC			,modality="vision"			,expected_output=_EXPECTED_OUTPUT_SHAPE			,)
			def 		UpperCamelCase__							(  self			,_snake_case			,_snake_case = None			,_snake_case = None			,_snake_case=False			,):
				UpperCAmelCase_     :      Union[str, Any]						=  (
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase_     :      Union[str, Any]						=  return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase_     :      Optional[Any]						=  self.regnet(
				    pixel_values=_snake_case			,output_hidden_states=_snake_case			,return_dict=_snake_case			,training=_snake_case			,)
				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(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "			,			__SCREAMING_SNAKE_CASE			,			)
class 					_snake_case		(__SCREAMING_SNAKE_CASE			,			__SCREAMING_SNAKE_CASE):






			def __init__(  self			,_snake_case			,*_snake_case			,**_snake_case     ):
				super().__init__(_snake_case			,*_snake_case			,**_snake_case     )
				UpperCAmelCase_     :      Dict						=  config.num_labels
				UpperCAmelCase_     :      List[str]						=  TFRegNetMainLayer(_snake_case			,name="regnet"     )
				# classification head
				UpperCAmelCase_     :      Dict						=  [
				    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(_snake_case     )
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT			,output_type=_snake_case			,config_class=_CONFIG_FOR_DOC			,expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,)
			def 		UpperCamelCase__							(  self			,_snake_case = None			,_snake_case = None			,_snake_case = None			,_snake_case = None			,_snake_case=False			,):
				UpperCAmelCase_     :      Optional[Any]						=  (
				    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
				)
				UpperCAmelCase_     :      Optional[int]						=  return_dict if return_dict is not None else self.config.use_return_dict

				UpperCAmelCase_     :      Any						=  self.regnet(
				    _snake_case			,output_hidden_states=_snake_case			,return_dict=_snake_case			,training=_snake_case     )

				UpperCAmelCase_     :      List[str]						=  outputs.pooler_output if return_dict else outputs[1]

				UpperCAmelCase_     :      Union[str, Any]						=  self.classifier[0](_snake_case     )
				UpperCAmelCase_     :      List[str]						=  self.classifier[1](_snake_case     )

				UpperCAmelCase_     :      str						=  None if labels is None else self.hf_compute_loss(labels=_snake_case			,logits=_snake_case     )

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

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









'''simple docstring'''





from __future__ import annotations

_UpperCamelCase											=		10

def 			a_	(				_lowerCAmelCase			) -> list[int]:
  __lowerCamelCase			:			str       			=					1
  __lowerCamelCase			:			Union[str, Any]       			=					max(_lowerCAmelCase			)
  while placement <= max_digit:
    # declare and initialize empty buckets
    __lowerCamelCase			:			list[list]       			=					[[] for _ in range(_lowerCAmelCase			)]
    # split list_of_ints between the buckets
    for i in list_of_ints:
      __lowerCamelCase			:			List[str]       			=					int((i / placement) % RADIX			)
      buckets[tmp].append(_lowerCAmelCase			)
    # put each buckets' contents into list_of_ints
    __lowerCamelCase			:			Dict       			=					0
    for b in range(_lowerCAmelCase			):
      for i in buckets[b]:
        __lowerCamelCase			:			List[str]       			=					i
        a += 1
        # move to next
    placement *= RADIX
  return list_of_ints


if __name__ == "__main__":
   import doctest

   doctest.testmod()






