




import io
import itertools
import json
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa
import pyarrow.json as paj

import datasets
from datasets.table import table_cast
from datasets.utils.file_utils import readline


lowercase__							:	int											=		datasets.utils.logging.get_logger(__name__)
@dataclass
class 						UpperCAmelCase  (							datasets.BuilderConfig     ):







						'''simple docstring'''





						lowerCAmelCase_							=  None
						lowerCAmelCase_							=  "utf-8"
						lowerCAmelCase_							=  None
						lowerCAmelCase_							=  None
						lowerCAmelCase_							=  True  # deprecated
						lowerCAmelCase_							=  None  # deprecated
						lowerCAmelCase_							=  10 << 20  # 10MB
						lowerCAmelCase_							=  None







class 						UpperCAmelCase  (							datasets.ArrowBasedBuilder     ):







						'''simple docstring'''





						lowerCAmelCase_							=  JsonConfig






						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							if self.config.block_size is not None:
								logger.warning("The JSON loader parameter `block_size` is deprecated. Please use `chunksize` instead"		)
								snake_case_						       =	self.config.block_size
							if self.config.use_threads is not True:
								logger.warning(
								    "The JSON loader parameter `use_threads` is deprecated and doesn't have any effect anymore."		)
							if self.config.newlines_in_values is not None:
								raise ValueError("The JSON loader parameter `newlines_in_values` is no longer supported"		)
							return datasets.DatasetInfo(features=self.config.features		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	Any		):






							"""simple docstring"""






							if not self.config.data_files:
								raise ValueError(f"At least one data file must be specified, but got data_files={self.config.data_files}"		)
							snake_case_						       =	dl_manager.download_and_extract(self.config.data_files		)
							if isinstance(__lowercase			,      (str, list, tuple)		):
								snake_case_						       =	data_files
								if isinstance(__lowercase			,      __lowercase		):
									snake_case_						       =	[files]
								snake_case_						       =	[dl_manager.iter_files(__lowercase		) for file in files]
								return [datasets.SplitGenerator(name=datasets.Split.TRAIN			,      gen_kwargs={"files": files}		)]
							snake_case_						       =	[]
							for split_name, files in data_files.items():
								if isinstance(__lowercase			,      __lowercase		):
									snake_case_						       =	[files]
								snake_case_						       =	[dl_manager.iter_files(__lowercase		) for file in files]
								splits.append(datasets.SplitGenerator(name=__lowercase			,      gen_kwargs={"files": files}		)		)
							return splits






						def 			snake_case__		(		self				:	Tuple			,      __lowercase				:	pa.Table		):






							"""simple docstring"""






							if self.config.features is not None:
								# adding missing columns
								for column_name in set(self.config.features		) - set(pa_table.column_names		):
									snake_case_						       =	self.config.features.arrow_schema.field(__lowercase		).type
									snake_case_						       =	pa_table.append_column(__lowercase			,      pa.array([None] * len(__lowercase		)			,      type=__lowercase		)		)
								# more expensive cast to support nested structures with keys in a different order
								# allows str <-> int/float or str to Audio for example
								snake_case_						       =	table_cast(__lowercase			,      self.config.features.arrow_schema		)
							return pa_table






						def 			snake_case__		(		self				:	Optional[Any]			,      __lowercase				:	Dict		):






							"""simple docstring"""






							for file_idx, file in enumerate(itertools.chain.from_iterable(__lowercase		)		):
								# If the file is one json object and if we need to look at the list of items in one specific field
								if self.config.field is not None:
									with open(__lowercase			,      encoding=self.config.encoding			,      errors=self.config.encoding_errors		) as f:
										snake_case_						       =	json.load(__lowercase		)

									# We keep only the field we are interested in
									snake_case_						       =	dataset[self.config.field]

									# We accept two format: a list of dicts or a dict of lists
									if isinstance(__lowercase			,      (list, tuple)		):
										snake_case_						       =	set().union(*[row.keys() for row in dataset]		)
										snake_case_						       =	{col: [row.get(__lowercase		) for row in dataset] for col in keys}
									else:
										snake_case_						       =	dataset
									snake_case_						       =	pa.Table.from_pydict(__lowercase		)
									yield file_idx, self._cast_table(__lowercase		)

								# If the file has one json object per line
								else:
									with open(__lowercase			,      "rb"		) as f:
										snake_case_						       =	0
										# Use block_size equal to the chunk size divided by 32 to leverage multithreading
										# Set a default minimum value of 16kB if the chunk size is really small
										snake_case_						       =	max(self.config.chunksize // 32			,      16 << 10		)
										snake_case_						       =	(
										    self.config.encoding_errors if self.config.encoding_errors is not None else "strict"
										)
										while True:
											snake_case_						       =	f.read(self.config.chunksize		)
											if not batch:
												break
											# Finish current line
											try:
												batch += f.readline()
											except (AttributeError, io.UnsupportedOperation):
												batch += readline(__lowercase		)
											# PyArrow only accepts utf-8 encoded bytes
											if self.config.encoding != "utf-8":
												snake_case_						       =	batch.decode(self.config.encoding			,      errors=__lowercase		).encode("utf-8"		)
											try:
												while True:
													try:
														snake_case_						       =	paj.read_json(
														    io.BytesIO(__lowercase		)			,      read_options=paj.ReadOptions(block_size=__lowercase		)		)
														break
													except (pa.ArrowInvalid, pa.ArrowNotImplementedError) as e:
														if (
														    isinstance(__lowercase			,      pa.ArrowInvalid		)
														    and "straddling" not in str(__lowercase		)
														    or block_size > len(__lowercase		)
														):
															raise
														else:
															# Increase the block size in case it was too small.
															# The block size will be reset for the next file.
															logger.debug(
															    f"Batch of {len(__lowercase		)} bytes couldn't be parsed with block_size={block_size}. Retrying with block_size={block_size * 2}."		)
															block_size *= 2
											except pa.ArrowInvalid as e:
												try:
													with open(
													    __lowercase			,      encoding=self.config.encoding			,      errors=self.config.encoding_errors		) as f:
														snake_case_						       =	json.load(__lowercase		)
												except json.JSONDecodeError:
													logger.error(f"Failed to read file '{file}' with error {type(__lowercase		)}: {e}"		)
													raise e
												# If possible, parse the file as a list of json objects and exit the loop
												if isinstance(__lowercase			,      __lowercase		):  # list is the only sequence type supported in JSON
													try:
														snake_case_						       =	set().union(*[row.keys() for row in dataset]		)
														snake_case_						       =	{col: [row.get(__lowercase		) for row in dataset] for col in keys}
														snake_case_						       =	pa.Table.from_pydict(__lowercase		)
													except (pa.ArrowInvalid, AttributeError) as e:
														logger.error(f"Failed to read file '{file}' with error {type(__lowercase		)}: {e}"		)
														raise ValueError(f"Not able to read records in the JSON file at {file}."		) from None
													yield file_idx, self._cast_table(__lowercase		)
													break
												else:
													logger.error(f"Failed to read file '{file}' with error {type(__lowercase		)}: {e}"		)
													raise ValueError(
													    f"Not able to read records in the JSON file at {file}. "
													    f"You should probably indicate the field of the JSON file containing your records. "
													    f"This JSON file contain the following fields: {str(list(dataset.keys()		)		)}. "
													    f"Select the correct one and provide it as `field='XXX'` to the dataset loading method. "		) from None
                        # Uncomment for debugging (will print the Arrow table size and elements)
                        # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                        # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
											yield (file_idx, batch_idx), self._cast_table(__lowercase		)
											batch_idx += 1






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

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

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


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

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

# Base docstring
lowercase__							:	Optional[Any]											=		"sail/poolformer_s12"
lowercase__							:	Union[str, Any]											=		[1, 512, 7, 7]

# Image classification docstring
lowercase__							:	List[str]											=		"sail/poolformer_s12"
lowercase__							:	Dict											=		"tabby, tabby cat"

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


def 		lowerCamelCase__       (						_A	,      _A = 0.0	,      _A = False				):







	'''simple docstring'''





	if drop_prob == 0.0 or not training:
		return input
	snake_case_						       =	1 - drop_prob
	snake_case_						       =	(input.shape[0],) + (1,) * (input.ndim - 1)  # work with diff dim tensors, not just 2D ConvNets
	snake_case_						       =	keep_prob + torch.rand(_A	,      dtype=input.dtype	,      device=input.device				)
	random_tensor.floor_()  # binarize
	snake_case_						       =	input.div(_A				) * random_tensor
	return output
class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[Any]			,      __lowercase				:	Optional[float] = None		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	drop_prob






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	torch.Tensor		):






							"""simple docstring"""






							return drop_path(__lowercase			,      self.drop_prob			,      self.training		)






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






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







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





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






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	patch_size if isinstance(__lowercase			,      collections.abc.Iterable		) else (patch_size, patch_size)
							snake_case_						       =	stride if isinstance(__lowercase			,      collections.abc.Iterable		) else (stride, stride)
							snake_case_						       =	padding if isinstance(__lowercase			,      collections.abc.Iterable		) else (padding, padding)

							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      kernel_size=__lowercase			,      stride=__lowercase			,      padding=__lowercase		)
							snake_case_						       =	norm_layer(__lowercase		) if norm_layer else nn.Identity()






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.projection(__lowercase		)
							snake_case_						       =	self.norm(__lowercase		)
							return embeddings







class 						UpperCAmelCase  (							nn.GroupNorm     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      **__lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							super().__init__(1			,      __lowercase			,      **__lowercase		)







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.AvgPoolad(__lowercase			,      stride=1			,      padding=pool_size // 2			,      count_include_pad=__lowercase		)






						def 			snake_case__		(		self				:	int			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							return self.pool(__lowercase		) - hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	str			,      __lowercase				:	List[Any]			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	nn.Convad(__lowercase			,      __lowercase			,      1		)
							snake_case_						       =	PoolFormerDropPath(__lowercase		)
							if isinstance(config.hidden_act			,      __lowercase		):
								snake_case_						       =	ACTaFN[config.hidden_act]
							else:
								snake_case_						       =	config.hidden_act






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.act_fn(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)
							snake_case_						       =	self.conva(__lowercase		)
							snake_case_						       =	self.drop(__lowercase		)

							return hidden_states







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	int			,      __lowercase				:	List[str]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	List[str]			,      __lowercase				:	int		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	PoolFormerPooling(__lowercase		)
							snake_case_						       =	PoolFormerOutput(__lowercase			,      __lowercase			,      __lowercase			,      __lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)
							snake_case_						       =	PoolFormerGroupNorm(__lowercase		)

							# Useful for training neural nets
							snake_case_						       =	PoolFormerDropPath(__lowercase		) if drop_path > 0.0 else nn.Identity()
							snake_case_						       =	config.use_layer_scale
							if config.use_layer_scale:
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)
								snake_case_						       =	nn.Parameter(
								    config.layer_scale_init_value * torch.ones((__lowercase)		)			,      requires_grad=__lowercase		)






						def 			snake_case__		(		self				:	Union[str, Any]			,      __lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							if self.use_layer_scale:
								snake_case_						       =	self.pooling(self.before_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * pooling_output
								# First residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)
								snake_case_						       =	()

								snake_case_						       =	self.output(self.after_norm(__lowercase		)		)
								snake_case_						       =	self.layer_scale_a.unsqueeze(-1		).unsqueeze(-1		) * layer_output
								# Second residual connection
								snake_case_						       =	hidden_states + self.drop_path(__lowercase		)

								snake_case_						       =	(output,) + outputs
								return outputs

							else:
								snake_case_						       =	self.drop_path(self.pooling(self.before_norm(__lowercase		)		)		)
								# First residual connection
								snake_case_						       =	pooling_output + hidden_states
								snake_case_						       =	()

								# Second residual connection inside the PoolFormerOutput block
								snake_case_						       =	self.drop_path(self.output(self.after_norm(__lowercase		)		)		)
								snake_case_						       =	hidden_states + layer_output

								snake_case_						       =	(output,) + outputs
								return outputs







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	List[Any]			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	config
							# stochastic depth decay rule
							snake_case_						       =	[x.item() for x in torch.linspace(0			,      config.drop_path_rate			,      sum(config.depths		)		)]

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

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

							snake_case_						       =	nn.ModuleList(__lowercase		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	List[Any]			,      __lowercase				:	int=False			,      __lowercase				:	Tuple=True		):






							"""simple docstring"""






							snake_case_						       =	() if output_hidden_states else None

							snake_case_						       =	pixel_values
							for idx, layers in enumerate(zip(self.patch_embeddings			,      self.block		)		):
								snake_case_       , snake_case_						       =	layers
								# Get patch embeddings from hidden_states
								snake_case_						       =	embedding_layer(__lowercase		)
								# Send the embeddings through the blocks
								for _, blk in enumerate(__lowercase		):
									snake_case_						       =	blk(__lowercase		)
									snake_case_						       =	layer_outputs[0]

								if output_hidden_states:
									snake_case_						       =	all_hidden_states + (hidden_states,)

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

							return BaseModelOutputWithNoAttention(last_hidden_state=__lowercase			,      hidden_states=__lowercase		)







class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						lowerCAmelCase_							=  PoolFormerConfig
						lowerCAmelCase_							=  '''poolformer'''
						lowerCAmelCase_							=  '''pixel_values'''
						lowerCAmelCase_							=  True






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	Optional[Any]		):






							"""simple docstring"""






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






						def 			snake_case__		(		self				:	str			,      __lowercase				:	Any			,      __lowercase				:	Tuple=False		):






							"""simple docstring"""






							if isinstance(__lowercase			,      __lowercase		):
								snake_case_						       =	value







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

lowercase__							:	List[str]											=		R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`PoolFormerImageProcessor.__call__`] for details.\n"
@add_start_docstrings(
    '''The bare PoolFormer Model transformer outputting raw hidden-states without any specific head on top.'''			,  UpperCAmelCase__			,  )
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Any			,      __lowercase				:	Any		):






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config

							snake_case_						       =	PoolFormerEncoder(__lowercase		)

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






						def 			snake_case__		(		self				:	List[Any]		):






							"""simple docstring"""






							return self.embeddings.patch_embeddings






						@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 			snake_case__		(		self				:	Optional[Any]			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






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

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

							snake_case_						       =	self.encoder(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )
							snake_case_						       =	encoder_outputs[0]

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

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







class 						UpperCAmelCase  (							nn.Module     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	Tuple		):






							"""simple docstring"""






							super().__init__()
							snake_case_						       =	nn.Linear(config.hidden_size			,      config.hidden_size		)






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	self.dense(__lowercase		)
							return output







@add_start_docstrings(
    '''
    PoolFormer Model transformer with an image classification head on top
    '''			,  UpperCAmelCase__			,  )
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Optional[int]			,      __lowercase				:	int		):






							"""simple docstring"""






							super().__init__(__lowercase		)
							snake_case_						       =	config.num_labels
							snake_case_						       =	PoolFormerModel(__lowercase		)

							# Final norm
							snake_case_						       =	PoolFormerGroupNorm(config.hidden_sizes[-1]		)
							# Classifier head
							snake_case_						       =	(
							    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 			snake_case__		(		self				:	str			,      __lowercase				:	Optional[torch.FloatTensor] = None			,      __lowercase				:	Optional[torch.LongTensor] = None			,      __lowercase				:	Optional[bool] = None			,      __lowercase				:	Optional[bool] = None			,      ):






							"""simple docstring"""






							snake_case_						       =	return_dict if return_dict is not None else self.config.use_return_dict

							snake_case_						       =	self.poolformer(
							    __lowercase			,      output_hidden_states=__lowercase			,      return_dict=__lowercase			,      )

							snake_case_						       =	outputs[0]

							snake_case_						       =	self.classifier(self.norm(__lowercase		).mean([-2, -1]		)		)

							snake_case_						       =	None
							if labels is not None:
								if self.config.problem_type is None:
									if self.num_labels == 1:
										snake_case_						       =	"regression"
									elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
										snake_case_						       =	"single_label_classification"
									else:
										snake_case_						       =	"multi_label_classification"

								if self.config.problem_type == "regression":
									snake_case_						       =	MSELoss()
									if self.num_labels == 1:
										snake_case_						       =	loss_fct(logits.squeeze()			,      labels.squeeze()		)
									else:
										snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)
								elif self.config.problem_type == "single_label_classification":
									snake_case_						       =	CrossEntropyLoss()
									snake_case_						       =	loss_fct(logits.view(-1			,      self.num_labels		)			,      labels.view(-1		)		)
								elif self.config.problem_type == "multi_label_classification":
									snake_case_						       =	BCEWithLogitsLoss()
									snake_case_						       =	loss_fct(__lowercase			,      __lowercase		)

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

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