




from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFImageClassifierOutputWithNoAttention,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_resnet import ResNetConfig


__UpperCAmelCase      				=							logging.get_logger(__name__)

# General docstring
__UpperCAmelCase      				=							"ResNetConfig"

# Base docstring
__UpperCAmelCase      				=							"microsoft/resnet-50"
__UpperCAmelCase      				=							[1, 2_0_4_8, 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 					a_(						tf.keras.layers.Layer					):



     """simple docstring"""



     def __init__(						self			:					int       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int = 3       ,       lowerCAmelCase__			:					int = 1       ,       lowerCAmelCase__			:					str = "relu"       ,       **lowerCAmelCase__			:					Optional[int])    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						kernel_size // 2
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.ConvaD(
          lowerCAmelCase__       ,       kernel_size=lowerCAmelCase__       ,       strides=lowerCAmelCase__       ,       padding='valid'       ,       use_bias=lowerCAmelCase__       ,       name='convolution')
      # Use same default momentum and epsilon as PyTorch equivalent
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.BatchNormalization(epsilon=1e-5       ,       momentum=0.9       ,       name='normalization')
      SCREAMING_SNAKE_CASE							  =						ACTaFN[activation] if activation is not None else tf.keras.layers.Activation('linear')





     def   __UpperCamelCase		(						self			:					List[Any]       ,       lowerCAmelCase__			:					tf.Tensor)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						SCREAMING_SNAKE_CASE							  =						(self.pad_value, self.pad_value)
      SCREAMING_SNAKE_CASE							  =						tf.pad(lowerCAmelCase__       ,       [(0, 0), height_pad, width_pad, (0, 0)])
      SCREAMING_SNAKE_CASE							  =						self.conv(lowerCAmelCase__)
      return hidden_state





     def   __UpperCamelCase		(						self			:					Optional[int]       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						self.convolution(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.normalization(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.activation(lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					Union[str, Any]       ,       lowerCAmelCase__			:					ResNetConfig       ,       **lowerCAmelCase__			:					Optional[int])    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(
          config.embedding_size       ,       kernel_size=7       ,       stride=2       ,       activation=config.hidden_act       ,       name='embedder'       ,       )
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.MaxPoolaD(pool_size=3       ,       strides=2       ,       padding='valid'       ,       name='pooler')
      SCREAMING_SNAKE_CASE							  =						config.num_channels





     def   __UpperCamelCase		(						self			:					Union[str, Any]       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE  , SCREAMING_SNAKE_CASE							  =						shape_list(lowerCAmelCase__)
      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.')
      SCREAMING_SNAKE_CASE							  =						pixel_values
      SCREAMING_SNAKE_CASE							  =						self.embedder(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						tf.pad(lowerCAmelCase__       ,       [[0, 0], [1, 1], [1, 1], [0, 0]])
      SCREAMING_SNAKE_CASE							  =						self.pooler(lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					Optional[int]       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int = 2       ,       **lowerCAmelCase__			:					Dict)    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.ConvaD(
          lowerCAmelCase__       ,       kernel_size=1       ,       strides=lowerCAmelCase__       ,       use_bias=lowerCAmelCase__       ,       name='convolution')
      # Use same default momentum and epsilon as PyTorch equivalent
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.BatchNormalization(epsilon=1e-5       ,       momentum=0.9       ,       name='normalization')





     def   __UpperCamelCase		(						self			:					str       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						x
      SCREAMING_SNAKE_CASE							  =						self.convolution(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.normalization(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					List[Any]       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int = 1       ,       lowerCAmelCase__			:					str = "relu"       ,       **lowerCAmelCase__			:					List[Any])    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						in_channels != out_channels or stride != 1
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(lowerCAmelCase__       ,       stride=lowerCAmelCase__       ,       name='layer.0')
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(lowerCAmelCase__       ,       activation=lowerCAmelCase__       ,       name='layer.1')
      SCREAMING_SNAKE_CASE							  =						(
          TFResNetShortCut(lowerCAmelCase__       ,       stride=lowerCAmelCase__       ,       name='shortcut')
          if should_apply_shortcut
          else tf.keras.layers.Activation('linear'       ,       name='shortcut')
      )
      SCREAMING_SNAKE_CASE							  =						ACTaFN[activation]





     def   __UpperCamelCase		(						self			:					Optional[int]       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						hidden_state
      SCREAMING_SNAKE_CASE							  =						self.conva(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.conva(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.shortcut(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      hidden_state += residual
      SCREAMING_SNAKE_CASE							  =						self.activation(lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					List[str]       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int = 1       ,       lowerCAmelCase__			:					str = "relu"       ,       lowerCAmelCase__			:					int = 4       ,       **lowerCAmelCase__			:					Tuple       ,       )    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						in_channels != out_channels or stride != 1
      SCREAMING_SNAKE_CASE							  =						out_channels // reduction
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(lowerCAmelCase__       ,       kernel_size=1       ,       name='layer.0')
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(lowerCAmelCase__       ,       stride=lowerCAmelCase__       ,       name='layer.1')
      SCREAMING_SNAKE_CASE							  =						TFResNetConvLayer(lowerCAmelCase__       ,       kernel_size=1       ,       activation=lowerCAmelCase__       ,       name='layer.2')
      SCREAMING_SNAKE_CASE							  =						(
          TFResNetShortCut(lowerCAmelCase__       ,       stride=lowerCAmelCase__       ,       name='shortcut')
          if should_apply_shortcut
          else tf.keras.layers.Activation('linear'       ,       name='shortcut')
      )
      SCREAMING_SNAKE_CASE							  =						ACTaFN[activation]





     def   __UpperCamelCase		(						self			:					Any       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						hidden_state
      SCREAMING_SNAKE_CASE							  =						self.conva(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.conva(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.conva(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.shortcut(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      hidden_state += residual
      SCREAMING_SNAKE_CASE							  =						self.activation(lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					Optional[Any]       ,       lowerCAmelCase__			:					ResNetConfig       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int       ,       lowerCAmelCase__			:					int = 2       ,       lowerCAmelCase__			:					int = 2       ,       **lowerCAmelCase__			:					List[str])    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)

      SCREAMING_SNAKE_CASE							  =						TFResNetBottleNeckLayer if config.layer_type == 'bottleneck' else TFResNetBasicLayer

      SCREAMING_SNAKE_CASE							  =						[layer(lowerCAmelCase__       ,       lowerCAmelCase__       ,       stride=lowerCAmelCase__       ,       activation=config.hidden_act       ,       name='layers.0')]
      layers += [
          layer(lowerCAmelCase__       ,       lowerCAmelCase__       ,       activation=config.hidden_act       ,       name=f'''layers.{i + 1}''')
          for i in range(depth - 1)
      ]
      SCREAMING_SNAKE_CASE							  =						layers





     def   __UpperCamelCase		(						self			:					Tuple       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False)    ->					tf.Tensor:

      """simple docstring"""
      for layer in self.stage_layers:
       SCREAMING_SNAKE_CASE							  =						layer(lowerCAmelCase__       ,       training=lowerCAmelCase__)
      return hidden_state

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



     """simple docstring"""



     def __init__(						self			:					Any       ,       lowerCAmelCase__			:					ResNetConfig       ,       **lowerCAmelCase__			:					int)    ->					None:

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





     def   __UpperCamelCase		(						self			:					Tuple       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					bool = False       ,       lowerCAmelCase__			:					bool = True       ,       lowerCAmelCase__			:					bool = False       ,       )    ->					TFBaseModelOutputWithNoAttention:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						() if output_hidden_states else None

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

       SCREAMING_SNAKE_CASE							  =						stage_module(lowerCAmelCase__       ,       training=lowerCAmelCase__)

      if output_hidden_states:
       SCREAMING_SNAKE_CASE							  =						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=lowerCAmelCase__       ,       hidden_states=lowerCAmelCase__)

class 					a_(						lowercase__					):



     """simple docstring"""



     __snake_case						:							int       			=ResNetConfig
     __snake_case						:							List[Any]       			='''resnet'''
     __snake_case						:							Any       			='''pixel_values'''





     @property
     def   __UpperCamelCase		(						self			:					Dict)    ->					int:

      """simple docstring"""
      return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 2_2_4, 2_2_4)       ,       dtype=tf.floataa)}

__UpperCAmelCase      				=							R"\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\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 [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"


__UpperCAmelCase      				=							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            [`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"
@keras_serializable
class 					a_(						tf.keras.layers.Layer					):



     """simple docstring"""



     __snake_case						:							Tuple       			=ResNetConfig





     def __init__(						self			:					Any       ,       lowerCAmelCase__			:					ResNetConfig       ,       **lowerCAmelCase__			:					List[str])    ->					None:

      """simple docstring"""
      super().__init__(**lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						config
      SCREAMING_SNAKE_CASE							  =						TFResNetEmbeddings(lowerCAmelCase__       ,       name='embedder')
      SCREAMING_SNAKE_CASE							  =						TFResNetEncoder(lowerCAmelCase__       ,       name='encoder')
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.GlobalAveragePoolingaD(keepdims=lowerCAmelCase__)





     @unpack_inputs
     def   __UpperCamelCase		(						self			:					Dict       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					Optional[bool] = None       ,       lowerCAmelCase__			:					Optional[bool] = None       ,       lowerCAmelCase__			:					bool = False       ,       )    ->					Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      SCREAMING_SNAKE_CASE							  =						return_dict if return_dict is not None else self.config.use_return_dict

      # TF 2.0 image layers can't use NCHW format when running on CPU.
      # We transpose to NHWC format and then transpose back after the full forward pass.
      # (batch_size, num_channels, height, width) -> (batch_size, height, width, num_channels)
      SCREAMING_SNAKE_CASE							  =						tf.transpose(lowerCAmelCase__       ,       perm=[0, 2, 3, 1])
      SCREAMING_SNAKE_CASE							  =						self.embedder(lowerCAmelCase__       ,       training=lowerCAmelCase__)

      SCREAMING_SNAKE_CASE							  =						self.encoder(
          lowerCAmelCase__       ,       output_hidden_states=lowerCAmelCase__       ,       return_dict=lowerCAmelCase__       ,       training=lowerCAmelCase__)

      SCREAMING_SNAKE_CASE							  =						encoder_outputs[0]

      SCREAMING_SNAKE_CASE							  =						self.pooler(lowerCAmelCase__)

      # Transpose all the outputs to the NCHW format
      # (batch_size, height, width, num_channels) -> (batch_size, num_channels, height, width)
      SCREAMING_SNAKE_CASE							  =						tf.transpose(lowerCAmelCase__       ,       (0, 3, 1, 2))
      SCREAMING_SNAKE_CASE							  =						tf.transpose(lowerCAmelCase__       ,       (0, 3, 1, 2))
      SCREAMING_SNAKE_CASE							  =						()
      for hidden_state in encoder_outputs[1:]:
       SCREAMING_SNAKE_CASE							  =						hidden_states + tuple(tf.transpose(lowerCAmelCase__       ,       (0, 3, 1, 2)) for h in hidden_state)

      if not return_dict:
       return (last_hidden_state, pooled_output) + hidden_states

      SCREAMING_SNAKE_CASE							  =						hidden_states if output_hidden_states else None

      return TFBaseModelOutputWithPoolingAndNoAttention(
          last_hidden_state=lowerCAmelCase__       ,       pooler_output=lowerCAmelCase__       ,       hidden_states=lowerCAmelCase__       ,       )

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



     """simple docstring"""



     def __init__(						self			:					Optional[Any]       ,       lowerCAmelCase__			:					ResNetConfig       ,       **lowerCAmelCase__			:					str)    ->					None:

      """simple docstring"""
      super().__init__(lowerCAmelCase__       ,       **lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						TFResNetMainLayer(config=lowerCAmelCase__       ,       name='resnet')





     @add_start_docstrings_to_model_forward(lowerCAmelCase__)
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC       ,       output_type=lowerCAmelCase__       ,       config_class=_CONFIG_FOR_DOC       ,       modality='vision'       ,       expected_output=_EXPECTED_OUTPUT_SHAPE       ,       )
     @unpack_inputs
     def   __UpperCamelCase		(						self			:					Tuple       ,       lowerCAmelCase__			:					tf.Tensor       ,       lowerCAmelCase__			:					Optional[bool] = None       ,       lowerCAmelCase__			:					Optional[bool] = None       ,       lowerCAmelCase__			:					bool = False       ,       )    ->					Union[Tuple[tf.Tensor], TFBaseModelOutputWithPoolingAndNoAttention]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						(
          output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
      )
      SCREAMING_SNAKE_CASE							  =						return_dict if return_dict is not None else self.config.use_return_dict

      SCREAMING_SNAKE_CASE							  =						self.resnet(
          pixel_values=lowerCAmelCase__       ,       output_hidden_states=lowerCAmelCase__       ,       return_dict=lowerCAmelCase__       ,       training=lowerCAmelCase__       ,       )
      return resnet_outputs

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



     """simple docstring"""



     def __init__(						self			:					List[Any]       ,       lowerCAmelCase__			:					ResNetConfig       ,       **lowerCAmelCase__			:					str)    ->					None:

      """simple docstring"""
      super().__init__(lowerCAmelCase__       ,       **lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						config.num_labels
      SCREAMING_SNAKE_CASE							  =						TFResNetMainLayer(lowerCAmelCase__       ,       name='resnet')
      # classification head
      SCREAMING_SNAKE_CASE							  =						(
          tf.keras.layers.Dense(config.num_labels       ,       name='classifier.1')
          if config.num_labels > 0
          else tf.keras.layers.Activation('linear'       ,       name='classifier.1')
      )





     def   __UpperCamelCase		(						self			:					List[Any]       ,       lowerCAmelCase__			:					tf.Tensor)    ->					tf.Tensor:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						tf.keras.layers.Flatten()(lowerCAmelCase__)
      SCREAMING_SNAKE_CASE							  =						self.classifier_layer(lowerCAmelCase__)
      return logits





     @add_start_docstrings_to_model_forward(lowerCAmelCase__)
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT       ,       output_type=lowerCAmelCase__       ,       config_class=_CONFIG_FOR_DOC       ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,       )
     @unpack_inputs
     def   __UpperCamelCase		(						self			:					str       ,       lowerCAmelCase__			:					tf.Tensor = None       ,       lowerCAmelCase__			:					tf.Tensor = None       ,       lowerCAmelCase__			:					bool = None       ,       lowerCAmelCase__			:					bool = None       ,       lowerCAmelCase__			:					bool = False       ,       )    ->					Union[Tuple[tf.Tensor], TFImageClassifierOutputWithNoAttention]:

      """simple docstring"""
      SCREAMING_SNAKE_CASE							  =						return_dict if return_dict is not None else self.config.use_return_dict

      SCREAMING_SNAKE_CASE							  =						self.resnet(
          lowerCAmelCase__       ,       output_hidden_states=lowerCAmelCase__       ,       return_dict=lowerCAmelCase__       ,       training=lowerCAmelCase__)

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

      SCREAMING_SNAKE_CASE							  =						self.classifier(lowerCAmelCase__)

      SCREAMING_SNAKE_CASE							  =						None if labels is None else self.hf_compute_loss(lowerCAmelCase__       ,       lowerCAmelCase__)

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

      return TFImageClassifierOutputWithNoAttention(loss=lowerCAmelCase__       ,       logits=lowerCAmelCase__       ,       hidden_states=outputs.hidden_states)












import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
      import torch_xla.core.xla_model as xm





def 		A_			(		lowercase_					,       lowercase_ = False	)							->Union[str, Any]:







 """simple docstring"""

 if device_specific:
  seed += AcceleratorState().process_index
 random.seed(lowercase_	)
 np.random.seed(lowercase_	)
 torch.manual_seed(lowercase_	)
 if is_xpu_available():
  torch.xpu.manual_seed_all(lowercase_	)
 elif is_npu_available():
  torch.npu.manual_seed_all(lowercase_	)
 else:
  torch.cuda.manual_seed_all(lowercase_	)
 # ^^ safe to call this function even if cuda is not available
 if is_tpu_available():
  xm.set_rng_state(lowercase_	)





def 		A_			(		lowercase_ = None					,       lowercase_ = None	)							->Optional[Any]:







 """simple docstring"""

 if rng_type == RNGType.TORCH:
  SCREAMING_SNAKE_CASE							  =						torch.get_rng_state()
 elif rng_type == RNGType.CUDA:
  SCREAMING_SNAKE_CASE							  =						torch.cuda.get_rng_state()
 elif rng_type == RNGType.XLA:
  assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
  SCREAMING_SNAKE_CASE							  =						torch.tensor(xm.get_rng_state()	)
 elif rng_type == RNGType.NPU:
  assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
  SCREAMING_SNAKE_CASE							  =						torch.npu.get_rng_state()
 elif rng_type == RNGType.XPU:
  assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
  SCREAMING_SNAKE_CASE							  =						torch.xpu.get_rng_state()
 elif rng_type == RNGType.GENERATOR:
  assert generator is not None, "Need a generator to synchronize its seed."
  SCREAMING_SNAKE_CASE							  =						generator.get_state()

 # Broadcast the rng state from device 0 to other devices
 SCREAMING_SNAKE_CASE							  =						AcceleratorState()
 if state.distributed_type == DistributedType.TPU:
  SCREAMING_SNAKE_CASE							  =						rng_state.to(xm.xla_device()	)
  xm.collective_broadcast([rng_state]	)
  xm.mark_step()
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif (
     state.distributed_type in CUDA_DISTRIBUTED_TYPES
     or state.distributed_type == DistributedType.MULTI_NPU
     or state.distributed_type == DistributedType.MULTI_XPU
 ):
  SCREAMING_SNAKE_CASE							  =						rng_state.to(state.device	)
  torch.distributed.broadcast(lowercase_					,       0	)
  SCREAMING_SNAKE_CASE							  =						rng_state.cpu()
 elif state.distributed_type == DistributedType.MULTI_CPU:
  torch.distributed.broadcast(lowercase_					,       0	)

 # Set the broadcast rng state
 if rng_type == RNGType.TORCH:
  torch.set_rng_state(lowercase_	)
 elif rng_type == RNGType.CUDA:
  torch.cuda.set_rng_state(lowercase_	)
 elif rng_type == RNGType.NPU:
  torch.npu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XPU:
  torch.xpu.set_rng_state(lowercase_	)
 elif rng_type == RNGType.XLA:
  xm.set_rng_state(rng_state.item()	)
 elif rng_type == RNGType.GENERATOR:
  generator.set_state(lowercase_	)





def 		A_			(		lowercase_					,       lowercase_ = None	)							->str:







 """simple docstring"""

 for rng_type in rng_types:
  synchronize_rng_state(RNGType(lowercase_	)					,       generator=lowercase_	)




