


'''simple docstring'''
from typing import Optional

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

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


__snake_case				 =	logging.get_logger(__name__)

# General docstring
__snake_case				 =	'''ResNetConfig'''

# Base docstring
__snake_case				 =	'''microsoft/resnet-50'''
__snake_case				 =	[1, 2048, 7, 7]

# Image classification docstring
__snake_case				 =	'''microsoft/resnet-50'''
__snake_case				 =	'''tiger cat'''

__snake_case				 =	[
    '''microsoft/resnet-50''',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]
class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_ = 3       ,  UpperCamelCase_ = 1       ,  UpperCamelCase_ = "relu"				):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__  :Tuple				   =					nn.Convad(
							    UpperCamelCase_       ,  UpperCamelCase_       ,  kernel_size=UpperCamelCase_       ,  stride=UpperCamelCase_       ,  padding=kernel_size // 2       ,  bias=UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					nn.BatchNormad(UpperCamelCase_				)
							UpperCamelCase__  :Dict				   =					ACTaFN[activation] if activation is not None else nn.Identity()




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.convolution(UpperCamelCase_				)
							UpperCamelCase__  :Dict				   =					self.normalization(UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					self.activation(UpperCamelCase_				)
							return hidden_state



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__  :List[str]				   =					ResNetConvLayer(
							    config.num_channels       ,  config.embedding_size       ,  kernel_size=7       ,  stride=2       ,  activation=config.hidden_act				)
							UpperCamelCase__  :List[str]				   =					nn.MaxPoolad(kernel_size=3       ,  stride=2       ,  padding=1				)
							UpperCamelCase__  :Dict				   =					config.num_channels




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					pixel_values.shape[1]
							if num_channels != self.num_channels:
												raise ValueError(
												    '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.'''				)
							UpperCamelCase__  :List[Any]				   =					self.embedder(UpperCamelCase_				)
							UpperCamelCase__  :Union[str, Any]				   =					self.pooler(UpperCamelCase_				)
							return embedding



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_ = 2				):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__  :List[str]				   =					nn.Convad(UpperCamelCase_       ,  UpperCamelCase_       ,  kernel_size=1       ,  stride=UpperCamelCase_       ,  bias=UpperCamelCase_				)
							UpperCamelCase__  :Dict				   =					nn.BatchNormad(UpperCamelCase_				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					self.convolution(UpperCamelCase_				)
							UpperCamelCase__  :Optional[int]				   =					self.normalization(UpperCamelCase_				)
							return hidden_state



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_ = 1       ,  UpperCamelCase_ = "relu"				):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__  :Any				   =					in_channels != out_channels or stride != 1
							UpperCamelCase__  :List[Any]				   =					(
							    ResNetShortCut(UpperCamelCase_       ,  UpperCamelCase_       ,  stride=UpperCamelCase_				) if should_apply_shortcut else nn.Identity()
							)
							UpperCamelCase__  :Any				   =					nn.Sequential(
							    ResNetConvLayer(UpperCamelCase_       ,  UpperCamelCase_       ,  stride=UpperCamelCase_				)       ,  ResNetConvLayer(UpperCamelCase_       ,  UpperCamelCase_       ,  activation=UpperCamelCase_				)       ,  )
							UpperCamelCase__  :List[Any]				   =					ACTaFN[activation]




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :int				   =					hidden_state
							UpperCamelCase__  :List[Any]				   =					self.layer(UpperCamelCase_				)
							UpperCamelCase__  :List[Any]				   =					self.shortcut(UpperCamelCase_				)
							hidden_state += residual
							UpperCamelCase__  :Union[str, Any]				   =					self.activation(UpperCamelCase_				)
							return hidden_state



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_ = 1       ,  UpperCamelCase_ = "relu"       ,  UpperCamelCase_ = 4				):

							'''simple docstring'''


							super().__init__()
							UpperCamelCase__  :Optional[Any]				   =					in_channels != out_channels or stride != 1
							UpperCamelCase__  :Optional[int]				   =					out_channels // reduction
							UpperCamelCase__  :List[str]				   =					(
							    ResNetShortCut(UpperCamelCase_       ,  UpperCamelCase_       ,  stride=UpperCamelCase_				) if should_apply_shortcut else nn.Identity()
							)
							UpperCamelCase__  :Dict				   =					nn.Sequential(
							    ResNetConvLayer(UpperCamelCase_       ,  UpperCamelCase_       ,  kernel_size=1				)       ,  ResNetConvLayer(UpperCamelCase_       ,  UpperCamelCase_       ,  stride=UpperCamelCase_				)       ,  ResNetConvLayer(UpperCamelCase_       ,  UpperCamelCase_       ,  kernel_size=1       ,  activation=UpperCamelCase_				)       ,  )
							UpperCamelCase__  :List[Any]				   =					ACTaFN[activation]




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :List[str]				   =					hidden_state
							UpperCamelCase__  :Union[str, Any]				   =					self.layer(UpperCamelCase_				)
							UpperCamelCase__  :Tuple				   =					self.shortcut(UpperCamelCase_				)
							hidden_state += residual
							UpperCamelCase__  :Dict				   =					self.activation(UpperCamelCase_				)
							return hidden_state



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_ = 2       ,  UpperCamelCase_ = 2       ,  ):

							'''simple docstring'''


							super().__init__()

							UpperCamelCase__  :Optional[int]				   =					ResNetBottleNeckLayer if config.layer_type == '''bottleneck''' else ResNetBasicLayer

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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					input
							for layer in self.layers:
												UpperCamelCase__  :Tuple				   =					layer(UpperCamelCase_				)
							return hidden_state



class   lowercase      (				nn.Module   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


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




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_ = False       ,  UpperCamelCase_ = True				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					() if output_hidden_states else None

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

												UpperCamelCase__  :Tuple				   =					stage_module(UpperCamelCase_				)

							if output_hidden_states:
												UpperCamelCase__  :str				   =					hidden_states + (hidden_state,)

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

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



class   lowercase      (				A__   ):

		"""simple docstring"""

		_a       					=							ResNetConfig
		_a       					=							'resnet'
		_a       					=							'pixel_values'
		_a       					=							True




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							if isinstance(UpperCamelCase_       ,  nn.Convad				):
												nn.init.kaiming_normal_(module.weight       ,  mode='''fan_out'''       ,  nonlinearity='''relu'''				)
							elif isinstance(UpperCamelCase_       ,  (nn.BatchNormad, nn.GroupNorm)				):
												nn.init.constant_(module.weight       ,  1				)
												nn.init.constant_(module.bias       ,  0				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_=False				):

							'''simple docstring'''


							if isinstance(UpperCamelCase_       ,  UpperCamelCase_				):
												UpperCamelCase__  :Dict				   =					value



__snake_case				 =	R'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ResNetConfig`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

__snake_case				 =	R'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__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 ResNet model outputting raw features without any specific head on top.'      ,						A__      ,						)
class   lowercase      (				A__   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							super().__init__(UpperCamelCase_				)
							UpperCamelCase__  :List[Any]				   =					config
							UpperCamelCase__  :List[str]				   =					ResNetEmbeddings(UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					ResNetEncoder(UpperCamelCase_				)
							UpperCamelCase__  :List[str]				   =					nn.AdaptiveAvgPoolad((1, 1)				)
							# Initialize weights and apply final processing
							self.post_init()




		@add_start_docstrings_to_model_forward(UpperCamelCase_				)
		@add_code_sample_docstrings(
		    checkpoint=_CHECKPOINT_FOR_DOC       ,  output_type=UpperCamelCase_       ,  config_class=_CONFIG_FOR_DOC       ,  modality='''vision'''       ,  expected_output=_EXPECTED_OUTPUT_SHAPE       ,  )
		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_ = None       ,  UpperCamelCase_ = None				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)
							UpperCamelCase__  :str				   =					return_dict if return_dict is not None else self.config.use_return_dict

							UpperCamelCase__  :Optional[Any]				   =					self.embedder(UpperCamelCase_				)

							UpperCamelCase__  :List[str]				   =					self.encoder(
							    UpperCamelCase_       ,  output_hidden_states=UpperCamelCase_       ,  return_dict=UpperCamelCase_				)

							UpperCamelCase__  :Tuple				   =					encoder_outputs[0]

							UpperCamelCase__  :List[str]				   =					self.pooler(UpperCamelCase_				)

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

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



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

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							super().__init__(UpperCamelCase_				)
							UpperCamelCase__  :List[Any]				   =					config.num_labels
							UpperCamelCase__  :Tuple				   =					ResNetModel(UpperCamelCase_				)
							# classification head
							UpperCamelCase__  :Any				   =					nn.Sequential(
							    nn.Flatten()       ,  nn.Linear(config.hidden_sizes[-1]       ,  config.num_labels				) if config.num_labels > 0 else nn.Identity()       ,  )
							# initialize weights and apply final processing
							self.post_init()




		@add_start_docstrings_to_model_forward(UpperCamelCase_				)
		@add_code_sample_docstrings(
		    checkpoint=_IMAGE_CLASS_CHECKPOINT       ,  output_type=UpperCamelCase_       ,  config_class=_CONFIG_FOR_DOC       ,  expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT       ,  )
		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_ = None       ,  UpperCamelCase_ = None       ,  UpperCamelCase_ = None       ,  UpperCamelCase_ = None       ,  ):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					return_dict if return_dict is not None else self.config.use_return_dict

							UpperCamelCase__  :List[Any]				   =					self.resnet(UpperCamelCase_       ,  output_hidden_states=UpperCamelCase_       ,  return_dict=UpperCamelCase_				)

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

							UpperCamelCase__  :List[Any]				   =					self.classifier(UpperCamelCase_				)

							UpperCamelCase__  :str				   =					None

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

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

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



@add_start_docstrings(
    '\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    '      ,						A__      ,						)
class   lowercase      (				A__      ,						A__   ):

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							super().__init__(UpperCamelCase_				)
							super()._init_backbone(UpperCamelCase_				)

							UpperCamelCase__  :Any				   =					[config.embedding_size] + config.hidden_sizes
							UpperCamelCase__  :List[str]				   =					ResNetEmbeddings(UpperCamelCase_				)
							UpperCamelCase__  :Dict				   =					ResNetEncoder(UpperCamelCase_				)

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




		@add_start_docstrings_to_model_forward(UpperCamelCase_				)
		@replace_return_docstrings(output_type=UpperCamelCase_       ,  config_class=_CONFIG_FOR_DOC				)
		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_ = None       ,  UpperCamelCase_ = None				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					return_dict if return_dict is not None else self.config.use_return_dict
							UpperCamelCase__  :Tuple				   =					(
							    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
							)

							UpperCamelCase__  :Tuple				   =					self.embedder(UpperCamelCase_				)

							UpperCamelCase__  :Union[str, Any]				   =					self.encoder(UpperCamelCase_       ,  output_hidden_states=UpperCamelCase_       ,  return_dict=UpperCamelCase_				)

							UpperCamelCase__  :List[Any]				   =					outputs.hidden_states

							UpperCamelCase__  :Union[str, Any]				   =					()
							for idx, stage in enumerate(self.stage_names				):
												if stage in self.out_features:
																	feature_maps += (hidden_states[idx],)

							if not return_dict:
												UpperCamelCase__  :Union[str, Any]				   =					(feature_maps,)
												if output_hidden_states:
																	output += (outputs.hidden_states,)
												return output

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



'''simple docstring'''
import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    FalconForCausalLM,
		    FalconForQuestionAnswering,
		    FalconForSequenceClassification,
		    FalconForTokenClassification,
		    FalconModel,
		)
class   lowercase      :

		"""simple docstring"""

		def __init__(						self       ,  UpperCamelCase_       ,  UpperCamelCase_=3       ,  UpperCamelCase_=7       ,  UpperCamelCase_=True       ,  UpperCamelCase_=True       ,  UpperCamelCase_=False       ,  UpperCamelCase_=True       ,  UpperCamelCase_=99       ,  UpperCamelCase_=32       ,  UpperCamelCase_=5       ,  UpperCamelCase_=4       ,  UpperCamelCase_=37       ,  UpperCamelCase_="gelu"       ,  UpperCamelCase_=0.1       ,  UpperCamelCase_=0.1       ,  UpperCamelCase_=512       ,  UpperCamelCase_=16       ,  UpperCamelCase_=2       ,  UpperCamelCase_=0.02       ,  UpperCamelCase_=3       ,  UpperCamelCase_=4       ,  UpperCamelCase_=None       ,  ):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					parent
							UpperCamelCase__  :Any				   =					batch_size
							UpperCamelCase__  :Tuple				   =					seq_length
							UpperCamelCase__  :str				   =					is_training
							UpperCamelCase__  :str				   =					use_input_mask
							UpperCamelCase__  :Union[str, Any]				   =					use_token_type_ids
							UpperCamelCase__  :int				   =					use_labels
							UpperCamelCase__  :Union[str, Any]				   =					vocab_size
							UpperCamelCase__  :int				   =					hidden_size
							UpperCamelCase__  :Union[str, Any]				   =					num_hidden_layers
							UpperCamelCase__  :List[Any]				   =					num_attention_heads
							UpperCamelCase__  :int				   =					intermediate_size
							UpperCamelCase__  :str				   =					hidden_act
							UpperCamelCase__  :List[str]				   =					hidden_dropout_prob
							UpperCamelCase__  :Tuple				   =					attention_probs_dropout_prob
							UpperCamelCase__  :str				   =					max_position_embeddings
							UpperCamelCase__  :List[Any]				   =					type_vocab_size
							UpperCamelCase__  :Any				   =					type_sequence_label_size
							UpperCamelCase__  :Dict				   =					initializer_range
							UpperCamelCase__  :List[str]				   =					num_labels
							UpperCamelCase__  :Tuple				   =					num_choices
							UpperCamelCase__  :List[Any]				   =					scope




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size				)

							UpperCamelCase__  :str				   =					None
							if self.use_input_mask:
												UpperCamelCase__  :List[str]				   =					random_attention_mask([self.batch_size, self.seq_length]				)

							UpperCamelCase__  :Optional[Any]				   =					None

							UpperCamelCase__  :Optional[Any]				   =					None
							UpperCamelCase__  :Optional[int]				   =					None
							UpperCamelCase__  :Optional[int]				   =					None
							if self.use_labels:
												UpperCamelCase__  :Optional[Any]				   =					ids_tensor([self.batch_size]       ,  self.type_sequence_label_size				)
												UpperCamelCase__  :Any				   =					ids_tensor([self.batch_size, self.seq_length]       ,  self.num_labels				)
												UpperCamelCase__  :Any				   =					ids_tensor([self.batch_size]       ,  self.num_choices				)

							UpperCamelCase__  :Any				   =					self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							return FalconConfig(
							    vocab_size=self.vocab_size       ,  hidden_size=self.hidden_size       ,  num_hidden_layers=self.num_hidden_layers       ,  num_attention_heads=self.num_attention_heads       ,  intermediate_size=self.intermediate_size       ,  hidden_act=self.hidden_act       ,  hidden_dropout_prob=self.hidden_dropout_prob       ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,  max_position_embeddings=self.max_position_embeddings       ,  type_vocab_size=self.type_vocab_size       ,  is_decoder=UpperCamelCase_       ,  initializer_range=self.initializer_range       ,  pad_token_id=1       ,  new_decoder_architecture=UpperCamelCase_       ,  )




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :Any				   =					FalconModel(config=UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :Dict				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)
							UpperCamelCase__  :Tuple				   =					model(UpperCamelCase_				)
							self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  ):

							'''simple docstring'''


							UpperCamelCase__  :Union[str, Any]				   =					True
							UpperCamelCase__  :Dict				   =					FalconModel(UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :Optional[Any]				   =					model(
							    UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  encoder_hidden_states=UpperCamelCase_       ,  encoder_attention_mask=UpperCamelCase_       ,  )
							UpperCamelCase__  :List[str]				   =					model(
							    UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  encoder_hidden_states=UpperCamelCase_       ,  )
							UpperCamelCase__  :str				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_				)
							self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  ):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					FalconForCausalLM(config=UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :Any				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  labels=UpperCamelCase_				)
							self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)				)




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  ):

							'''simple docstring'''


							UpperCamelCase__  :str				   =					True
							UpperCamelCase__  :Tuple				   =					True
							UpperCamelCase__  :Any				   =					FalconForCausalLM(config=UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()

							# first forward pass
							UpperCamelCase__  :Dict				   =					model(
							    UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  encoder_hidden_states=UpperCamelCase_       ,  encoder_attention_mask=UpperCamelCase_       ,  use_cache=UpperCamelCase_       ,  )
							UpperCamelCase__  :List[Any]				   =					outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							UpperCamelCase__  :Any				   =					ids_tensor((self.batch_size, 3)       ,  config.vocab_size				)
							UpperCamelCase__  :Optional[Any]				   =					ids_tensor((self.batch_size, 3)       ,  vocab_size=2				)

							# append to next input_ids and
							UpperCamelCase__  :Dict				   =					torch.cat([input_ids, next_tokens]       ,  dim=-1				)
							UpperCamelCase__  :Any				   =					torch.cat([input_mask, next_mask]       ,  dim=-1				)

							UpperCamelCase__  :Any				   =					model(
							    UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  encoder_hidden_states=UpperCamelCase_       ,  encoder_attention_mask=UpperCamelCase_       ,  output_hidden_states=UpperCamelCase_       ,  )['''hidden_states'''][0]
							UpperCamelCase__  :Optional[Any]				   =					model(
							    UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  encoder_hidden_states=UpperCamelCase_       ,  encoder_attention_mask=UpperCamelCase_       ,  past_key_values=UpperCamelCase_       ,  output_hidden_states=UpperCamelCase_       ,  )['''hidden_states'''][0]

							# select random slice
							UpperCamelCase__  :List[Any]				   =					ids_tensor((1,)       ,  output_from_past.shape[-1]				).item()
							UpperCamelCase__  :Optional[int]				   =					output_from_no_past[:, -3:, random_slice_idx].detach()
							UpperCamelCase__  :Tuple				   =					output_from_past[:, :, random_slice_idx].detach()

							self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]				)

							# test that outputs are equal for slice
							self.parent.assertTrue(torch.allclose(UpperCamelCase_       ,  UpperCamelCase_       ,  atol=1e-3				)				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Dict				   =					self.prepare_config_and_inputs()
							(
							    (
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							(
							    UpperCamelCase__
							)   ,							
							)  :Optional[int]				   =					config_and_inputs
							UpperCamelCase__  :Optional[int]				   =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class   lowercase      (				A__      ,						A__      ,						A__      ,						unittest.TestCase   ):

		"""simple docstring"""

		_a       					=							(
		    (
		        FalconModel,
		        FalconForCausalLM,
		        FalconForSequenceClassification,
		        FalconForTokenClassification,
		        FalconForQuestionAnswering,
		    )
		    if is_torch_available()
		    else ()
		)
		_a       					=							(FalconForCausalLM,) if is_torch_available() else ()
		_a       					=							(
		    {
		        'feature-extraction': FalconModel,
		        'text-classification': FalconForSequenceClassification,
		        'text-generation': FalconForCausalLM,
		        'question-answering': FalconForQuestionAnswering,
		        'token-classification': FalconForTokenClassification,
		        'zero-shot': FalconForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		_a       					=							False
		_a       					=							False




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Union[str, Any]				   =					FalconModelTester(self				)
							UpperCamelCase__  :List[Any]				   =					ConfigTester(self       ,  config_class=UpperCamelCase_       ,  hidden_size=37				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :Tuple				   =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							*UpperCamelCase__  :Union[str, Any]				   =					self.model_tester.prepare_config_and_inputs()
							for alibi in [True, False]:
												UpperCamelCase__  :Any				   =					alibi
												self.model_tester.create_and_check_model(UpperCamelCase_       ,  *UpperCamelCase_				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[str]				   =					self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase__  :str				   =					3
							UpperCamelCase__  :Any				   =					input_dict['''input_ids''']
							UpperCamelCase__  :Tuple				   =					input_ids.ne(1				).to(UpperCamelCase_				)
							UpperCamelCase__  :Optional[Any]				   =					ids_tensor([self.model_tester.batch_size]       ,  self.model_tester.type_sequence_label_size				)
							UpperCamelCase__  :List[Any]				   =					FalconForSequenceClassification(UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :Dict				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  labels=UpperCamelCase_				)
							self.assertEqual(result.logits.shape       ,  (self.model_tester.batch_size, self.model_tester.num_labels)				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[Any]				   =					self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase__  :List[str]				   =					3
							UpperCamelCase__  :str				   =					'''single_label_classification'''
							UpperCamelCase__  :Union[str, Any]				   =					input_dict['''input_ids''']
							UpperCamelCase__  :List[str]				   =					input_ids.ne(1				).to(UpperCamelCase_				)
							UpperCamelCase__  :Dict				   =					ids_tensor([self.model_tester.batch_size]       ,  self.model_tester.type_sequence_label_size				)
							UpperCamelCase__  :Dict				   =					FalconForSequenceClassification(UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :str				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  labels=UpperCamelCase_				)
							self.assertEqual(result.logits.shape       ,  (self.model_tester.batch_size, self.model_tester.num_labels)				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :Tuple				   =					self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase__  :int				   =					input_dict['''input_ids''']
							UpperCamelCase__  :List[str]				   =					FalconForCausalLM(UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :Optional[int]				   =					model(UpperCamelCase_       ,  use_cache=UpperCamelCase_				)
							UpperCamelCase__  :Any				   =					input_ids.shape[0]
							UpperCamelCase__  :Optional[int]				   =					model._convert_to_rw_cache(result.past_key_values				)
							UpperCamelCase__  :List[Any]				   =					model._convert_cache_to_standard_format(UpperCamelCase_       ,  UpperCamelCase_				)
							for layer in range(len(UpperCamelCase_				)				):
												for tensor_idx in range(2				):
																	self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3				)
																	self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4				)
																	self.assertTrue(
																	    torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]				)				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :List[Any]				   =					self.model_tester.prepare_config_and_inputs_for_common()
							UpperCamelCase__  :Optional[int]				   =					3
							UpperCamelCase__  :Union[str, Any]				   =					'''multi_label_classification'''
							UpperCamelCase__  :Dict				   =					input_dict['''input_ids''']
							UpperCamelCase__  :Tuple				   =					input_ids.ne(1				).to(UpperCamelCase_				)
							UpperCamelCase__  :Union[str, Any]				   =					ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]       ,  self.model_tester.type_sequence_label_size				).to(torch.float				)
							UpperCamelCase__  :Optional[Any]				   =					FalconForSequenceClassification(UpperCamelCase_				)
							model.to(UpperCamelCase_				)
							model.eval()
							UpperCamelCase__  :str				   =					model(UpperCamelCase_       ,  attention_mask=UpperCamelCase_       ,  labels=UpperCamelCase_				)
							self.assertEqual(result.logits.shape       ,  (self.model_tester.batch_size, self.model_tester.num_labels)				)




		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							for model_class in self.all_generative_model_classes:
												UpperCamelCase__   ,							UpperCamelCase__  :List[Any]				   =					self.model_tester.prepare_config_and_inputs_for_common()

												# If it doesn't support cache, pass the test
												if not hasattr(UpperCamelCase_       ,  '''use_cache'''				):
																	return

												UpperCamelCase__  :Tuple				   =					model_class(UpperCamelCase_				).to(UpperCamelCase_				)
												if "use_cache" not in inputs:
																	UpperCamelCase__  :Tuple				   =					True
												UpperCamelCase__  :List[str]				   =					model(**UpperCamelCase_				)

												# If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
												if "past_key_values" not in outputs:
																	return

												UpperCamelCase__  :str				   =					(
												    getattr(UpperCamelCase_       ,  '''decoder_layers'''       ,  UpperCamelCase_				)
												    or getattr(UpperCamelCase_       ,  '''num_decoder_layers'''       ,  UpperCamelCase_				)
												    or config.num_hidden_layers
												)
												UpperCamelCase__  :List[Any]				   =					getattr(UpperCamelCase_       ,  '''num_kv_heads'''       ,  config.num_attention_heads				)
												UpperCamelCase__  :str				   =					getattr(UpperCamelCase_       ,  '''d_model'''       ,  config.hidden_size				)
												UpperCamelCase__  :Optional[Any]				   =					embed_dim // num_attention_heads

												UpperCamelCase__  :Optional[Any]				   =					outputs['''past_key_values''']
												self.assertEqual(len(UpperCamelCase_				)       ,  UpperCamelCase_				)

												UpperCamelCase__   ,							UpperCamelCase__  :Tuple				   =					inputs['''input_ids'''].shape
												for i in range(UpperCamelCase_				):
																	if config.new_decoder_architecture:
																						UpperCamelCase__  :List[Any]				   =					config.num_attention_heads
																	elif config.multi_query:
																						UpperCamelCase__  :str				   =					1
																	self.assertEqual(len(past_kv[0]				)       ,  2				)  # K V for the decoder = 2
																	self.assertEqual(
																	    past_kv[i][0].shape       ,  (batch_size, num_attention_heads, seq_length, per_head_embed_dim)				)
																	self.assertEqual(
																	    past_kv[i][1].shape       ,  (batch_size, num_attention_heads, seq_length, per_head_embed_dim)				)



@require_torch
class   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :List[str]				   =					AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''				)
							UpperCamelCase__  :List[Any]				   =					FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''				)
							model.eval()
							model.to(UpperCamelCase_				)
							UpperCamelCase__  :List[str]				   =					tokenizer('''My favorite food is'''       ,  return_tensors='''pt'''				).to(UpperCamelCase_				)

							UpperCamelCase__  :Optional[Any]				   =					(
							    '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
							)

							UpperCamelCase__  :int				   =					model.generate(**UpperCamelCase_       ,  do_sample=UpperCamelCase_       ,  max_new_tokens=19				)
							UpperCamelCase__  :str				   =					tokenizer.batch_decode(UpperCamelCase_				)[0]

							self.assertEqual(UpperCamelCase_       ,  UpperCamelCase_				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
												UpperCamelCase__  :Any				   =					AutoTokenizer.from_pretrained(UpperCamelCase_				)
												UpperCamelCase__  :Dict				   =					FalconForCausalLM.from_pretrained(UpperCamelCase_				)
												model.eval()
												model.to(UpperCamelCase_				)
												UpperCamelCase__  :str				   =					tokenizer('''My favorite food is'''       ,  return_tensors='''pt'''				).to(UpperCamelCase_				)

												# We just test that these run without errors - the models are randomly initialized
												# and so the actual text outputs will be garbage
												model.generate(**UpperCamelCase_       ,  do_sample=UpperCamelCase_       ,  max_new_tokens=4				)
												model.generate(**UpperCamelCase_       ,  do_sample=UpperCamelCase_       ,  max_new_tokens=4				)
												model.generate(**UpperCamelCase_       ,  num_beams=2       ,  max_new_tokens=4				)




		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							with torch.no_grad():
												for repo in [
												    "Rocketknight1/falcon-rw-1b",
												    "Rocketknight1/tiny-random-falcon-7b",
												    "Rocketknight1/tiny-random-falcon-40b",
												]:
																	UpperCamelCase__  :List[Any]				   =					AutoTokenizer.from_pretrained(UpperCamelCase_				)
																	UpperCamelCase__  :Union[str, Any]				   =					FalconForCausalLM.from_pretrained(UpperCamelCase_				)
																	model.eval()
																	model.to(device=UpperCamelCase_				)
																	UpperCamelCase__  :Tuple				   =					tokenizer('''My favorite food is'''       ,  return_tensors='''pt'''				).to(UpperCamelCase_				)

																	# Test results are the same with and without cache
																	UpperCamelCase__  :Union[str, Any]				   =					model.generate(**UpperCamelCase_       ,  do_sample=UpperCamelCase_       ,  max_new_tokens=20       ,  use_cache=UpperCamelCase_				)
																	UpperCamelCase__  :Any				   =					model.generate(**UpperCamelCase_       ,  do_sample=UpperCamelCase_       ,  max_new_tokens=20       ,  use_cache=UpperCamelCase_				)
																	self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0				)