
"""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


UpperCamelCase_       :    Dict						      =			logging.get_logger(__name__)

# General docstring
UpperCamelCase_       :    Optional[Any]						      =			'''ResNetConfig'''

# Base docstring
UpperCamelCase_       :    Dict						      =			'''microsoft/resnet-50'''
UpperCamelCase_       :    Tuple						      =			[1, 2048, 7, 7]

# Image classification docstring
UpperCamelCase_       :    List[Any]						      =			'''microsoft/resnet-50'''
UpperCamelCase_       :    List[str]						      =			'''tiger cat'''

UpperCamelCase_       :    List[Any]						      =			[
    '''microsoft/resnet-50''',
    # See all resnet models at https://huggingface.co/models?filter=resnet
]



class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Dict							, _snake_case    :       int							, _snake_case    :       int							, _snake_case    :       int = 3							, _snake_case    :       int = 1							, _snake_case    :       str = "relu"						)   ->			Optional[Any]:







								"""simple docstring"""
								super().__init__()
								A_								=    nn.Convad(
								    _snake_case							, _snake_case							, kernel_size=_snake_case							, stride=_snake_case							, padding=kernel_size // 2							, bias=_snake_case						)
								A_								=    nn.BatchNormad(_snake_case						)
								A_								=    ACTaFN[activation] if activation is not None else nn.Identity()






			def   lowerCamelCase__  (					self    :       Any							, _snake_case    :       Tensor						)   ->			Tensor:







								"""simple docstring"""
								A_								=    self.convolution(_snake_case						)
								A_								=    self.normalization(_snake_case						)
								A_								=    self.activation(_snake_case						)
								return hidden_state





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Any							, _snake_case    :       ResNetConfig						)   ->			Optional[int]:







								"""simple docstring"""
								super().__init__()
								A_								=    ResNetConvLayer(
								    config.num_channels							, config.embedding_size							, kernel_size=7							, stride=2							, activation=config.hidden_act						)
								A_								=    nn.MaxPoolad(kernel_size=3							, stride=2							, padding=1						)
								A_								=    config.num_channels






			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Tensor						)   ->			Tensor:







								"""simple docstring"""
								A_								=    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."						)
								A_								=    self.embedder(_snake_case						)
								A_								=    self.pooler(_snake_case						)
								return embedding





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       str							, _snake_case    :       int							, _snake_case    :       int							, _snake_case    :       int = 2						)   ->			str:







								"""simple docstring"""
								super().__init__()
								A_								=    nn.Convad(_snake_case							, _snake_case							, kernel_size=1							, stride=_snake_case							, bias=_snake_case						)
								A_								=    nn.BatchNormad(_snake_case						)






			def   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Tensor						)   ->			Tensor:







								"""simple docstring"""
								A_								=    self.convolution(_snake_case						)
								A_								=    self.normalization(_snake_case						)
								return hidden_state





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Any							, _snake_case    :       int							, _snake_case    :       int							, _snake_case    :       int = 1							, _snake_case    :       str = "relu"						)   ->			Tuple:







								"""simple docstring"""
								super().__init__()
								A_								=    in_channels != out_channels or stride != 1
								A_								=    (
								    ResNetShortCut(_snake_case							, _snake_case							, stride=_snake_case						) if should_apply_shortcut else nn.Identity()
								)
								A_								=    nn.Sequential(
								    ResNetConvLayer(_snake_case							, _snake_case							, stride=_snake_case						)							, ResNetConvLayer(_snake_case							, _snake_case							, activation=_snake_case						)							, )
								A_								=    ACTaFN[activation]






			def   lowerCamelCase__  (					self    :       Optional[int]							, _snake_case    :       Union[str, Any]						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    hidden_state
								A_								=    self.layer(_snake_case						)
								A_								=    self.shortcut(_snake_case						)
								hidden_state += residual
								A_								=    self.activation(_snake_case						)
								return hidden_state





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Dict							, _snake_case    :       int							, _snake_case    :       int							, _snake_case    :       int = 1							, _snake_case    :       str = "relu"							, _snake_case    :       int = 4						)   ->			Dict:







								"""simple docstring"""
								super().__init__()
								A_								=    in_channels != out_channels or stride != 1
								A_								=    out_channels // reduction
								A_								=    (
								    ResNetShortCut(_snake_case							, _snake_case							, stride=_snake_case						) if should_apply_shortcut else nn.Identity()
								)
								A_								=    nn.Sequential(
								    ResNetConvLayer(_snake_case							, _snake_case							, kernel_size=1						)							, ResNetConvLayer(_snake_case							, _snake_case							, stride=_snake_case						)							, ResNetConvLayer(_snake_case							, _snake_case							, kernel_size=1							, activation=_snake_case						)							, )
								A_								=    ACTaFN[activation]






			def   lowerCamelCase__  (					self    :       str							, _snake_case    :       Union[str, Any]						)   ->			List[str]:







								"""simple docstring"""
								A_								=    hidden_state
								A_								=    self.layer(_snake_case						)
								A_								=    self.shortcut(_snake_case						)
								hidden_state += residual
								A_								=    self.activation(_snake_case						)
								return hidden_state





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Optional[int]							, _snake_case    :       ResNetConfig							, _snake_case    :       int							, _snake_case    :       int							, _snake_case    :       int = 2							, _snake_case    :       int = 2							, )   ->			Union[str, Any]:







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

								A_								=    ResNetBottleNeckLayer if config.layer_type == "bottleneck" else ResNetBasicLayer

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






			def   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Tensor						)   ->			Tensor:







								"""simple docstring"""
								A_								=    input
								for layer in self.layers:
													A_								=    layer(_snake_case						)
								return hidden_state





class __lowerCAmelCase					(       nn.Module   ):




			"""simple docstring"""







			def __init__(					self    :       Union[str, Any]							, _snake_case    :       ResNetConfig						)   ->			List[str]:







								"""simple docstring"""
								super().__init__()
								A_								=    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(
								        _snake_case							, config.embedding_size							, config.hidden_sizes[0]							, stride=2 if config.downsample_in_first_stage else 1							, depth=config.depths[0]							, )						)
								A_								=    zip(config.hidden_sizes							, config.hidden_sizes[1:]						)
								for (in_channels, out_channels), depth in zip(_snake_case							, config.depths[1:]						):
													self.stages.append(ResNetStage(_snake_case							, _snake_case							, _snake_case							, depth=_snake_case						)						)






			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       Tensor							, _snake_case    :       bool = False							, _snake_case    :       bool = True						)   ->			BaseModelOutputWithNoAttention:







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

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

													A_								=    stage_module(_snake_case						)

								if output_hidden_states:
													A_								=    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=_snake_case							, hidden_states=_snake_case							, )





class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			snake_case						   =					ResNetConfig
			snake_case						   =					"resnet"
			snake_case						   =					"pixel_values"
			snake_case						   =					True




			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Any						)   ->			Union[str, Any]:







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






			def   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       Optional[Any]=False						)   ->			str:







								"""simple docstring"""
								if isinstance(_snake_case							, _snake_case						):
													A_								=    value





UpperCamelCase_       :    List[Any]						      =			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.
'''

UpperCamelCase_       :    str						      =			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."  ,					_lowercase  ,					)
class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       int							, _snake_case    :       Any						)   ->			Optional[Any]:







								"""simple docstring"""
								super().__init__(_snake_case						)
								A_								=    config
								A_								=    ResNetEmbeddings(_snake_case						)
								A_								=    ResNetEncoder(_snake_case						)
								A_								=    nn.AdaptiveAvgPoolad((1, 1)						)
								# Initialize weights and apply final processing
								self.post_init()






			@add_start_docstrings_to_model_forward(_snake_case						)
			@add_code_sample_docstrings(
			    checkpoint=_CHECKPOINT_FOR_DOC							, output_type=_snake_case							, config_class=_CONFIG_FOR_DOC							, modality="vision"							, expected_output=_EXPECTED_OUTPUT_SHAPE							, )
			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       Tensor							, _snake_case    :       Optional[bool] = None							, _snake_case    :       Optional[bool] = None						)   ->			BaseModelOutputWithPoolingAndNoAttention:







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

								A_								=    self.embedder(_snake_case						)

								A_								=    self.encoder(
								    _snake_case							, output_hidden_states=_snake_case							, return_dict=_snake_case						)

								A_								=    encoder_outputs[0]

								A_								=    self.pooler(_snake_case						)

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

								return BaseModelOutputWithPoolingAndNoAttention(
								    last_hidden_state=_snake_case							, pooler_output=_snake_case							, 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    "  ,					_lowercase  ,					)
class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       str							, _snake_case    :       List[Any]						)   ->			Any:







								"""simple docstring"""
								super().__init__(_snake_case						)
								A_								=    config.num_labels
								A_								=    ResNetModel(_snake_case						)
								# classification head
								A_								=    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(_snake_case						)
			@add_code_sample_docstrings(
			    checkpoint=_IMAGE_CLASS_CHECKPOINT							, output_type=_snake_case							, config_class=_CONFIG_FOR_DOC							, expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							, )
			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Optional[torch.FloatTensor] = None							, _snake_case    :       Optional[torch.LongTensor] = None							, _snake_case    :       Optional[bool] = None							, _snake_case    :       Optional[bool] = None							, )   ->			ImageClassifierOutputWithNoAttention:







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

								A_								=    self.resnet(_snake_case							, output_hidden_states=_snake_case							, return_dict=_snake_case						)

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

								A_								=    self.classifier(_snake_case						)

								A_								=    None

								if labels is not None:
													if self.config.problem_type is None:
																		if self.num_labels == 1:
																							A_								=    "regression"
																		elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																							A_								=    "single_label_classification"
																		else:
																							A_								=    "multi_label_classification"
													if self.config.problem_type == "regression":
																		A_								=    MSELoss()
																		if self.num_labels == 1:
																							A_								=    loss_fct(logits.squeeze()							, labels.squeeze()						)
																		else:
																							A_								=    loss_fct(_snake_case							, _snake_case						)
													elif self.config.problem_type == "single_label_classification":
																		A_								=    CrossEntropyLoss()
																		A_								=    loss_fct(logits.view(-1							, self.num_labels						)							, labels.view(-1						)						)
													elif self.config.problem_type == "multi_label_classification":
																		A_								=    BCEWithLogitsLoss()
																		A_								=    loss_fct(_snake_case							, _snake_case						)

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

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





@add_start_docstrings(
    "\n    ResNet backbone, to be used with frameworks like DETR and MaskFormer.\n    "  ,					_lowercase  ,					)
class __lowerCAmelCase					(       _lowercase  ,					_lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       Optional[int]							, _snake_case    :       List[Any]						)   ->			int:







								"""simple docstring"""
								super().__init__(_snake_case						)
								super()._init_backbone(_snake_case						)

								A_								=    [config.embedding_size] + config.hidden_sizes
								A_								=    ResNetEmbeddings(_snake_case						)
								A_								=    ResNetEncoder(_snake_case						)

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






			@add_start_docstrings_to_model_forward(_snake_case						)
			@replace_return_docstrings(output_type=_snake_case							, config_class=_CONFIG_FOR_DOC						)
			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Tensor							, _snake_case    :       Optional[bool] = None							, _snake_case    :       Optional[bool] = None						)   ->			BackboneOutput:







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

								A_								=    self.embedder(_snake_case						)

								A_								=    self.encoder(_snake_case							, output_hidden_states=_snake_case							, return_dict=_snake_case						)

								A_								=    outputs.hidden_states

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

								if not return_dict:
													A_								=    (feature_maps,)
													if output_hidden_states:
																		output += (outputs.hidden_states,)
													return output

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






"""simple docstring"""





import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
		from transformers.trainer import DEFAULT_CALLBACKS

		from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel



class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       Optional[int]						)   ->			str:







								"""simple docstring"""
								A_								=    []




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       List[str]							, _snake_case    :       Optional[int]							, _snake_case    :       Optional[int]							, **_snake_case    :       str						)   ->			int:







								"""simple docstring"""
								self.events.append("on_init_end"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[Any]							, _snake_case    :       Tuple							, **_snake_case    :       Tuple						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_train_begin"						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Any							, _snake_case    :       Optional[Any]							, _snake_case    :       Tuple							, **_snake_case    :       Optional[int]						)   ->			str:







								"""simple docstring"""
								self.events.append("on_train_end"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       str							, _snake_case    :       List[Any]							, _snake_case    :       str							, **_snake_case    :       Optional[int]						)   ->			Dict:







								"""simple docstring"""
								self.events.append("on_epoch_begin"						)




			def   lowerCamelCase__  (					self    :       Optional[int]							, _snake_case    :       Optional[int]							, _snake_case    :       Any							, _snake_case    :       Optional[Any]							, **_snake_case    :       Optional[Any]						)   ->			str:







								"""simple docstring"""
								self.events.append("on_epoch_end"						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       List[str]							, _snake_case    :       str							, _snake_case    :       Optional[Any]							, **_snake_case    :       Optional[int]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_step_begin"						)




			def   lowerCamelCase__  (					self    :       Optional[Any]							, _snake_case    :       Union[str, Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       Any							, **_snake_case    :       Union[str, Any]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_step_end"						)




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Dict							, _snake_case    :       Dict							, _snake_case    :       Union[str, Any]							, **_snake_case    :       List[str]						)   ->			Optional[int]:







								"""simple docstring"""
								self.events.append("on_evaluate"						)




			def   lowerCamelCase__  (					self    :       Optional[int]							, _snake_case    :       Optional[Any]							, _snake_case    :       int							, _snake_case    :       int							, **_snake_case    :       List[Any]						)   ->			List[str]:







								"""simple docstring"""
								self.events.append("on_predict"						)




			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Union[str, Any]							, _snake_case    :       List[str]							, _snake_case    :       Optional[Any]							, **_snake_case    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								self.events.append("on_save"						)




			def   lowerCamelCase__  (					self    :       Dict							, _snake_case    :       Dict							, _snake_case    :       List[Any]							, _snake_case    :       Optional[int]							, **_snake_case    :       Dict						)   ->			str:







								"""simple docstring"""
								self.events.append("on_log"						)






			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Optional[Any]							, _snake_case    :       str							, _snake_case    :       int							, **_snake_case    :       List[Any]						)   ->			int:







								"""simple docstring"""
								self.events.append("on_prediction_step"						)





@require_torch
class __lowerCAmelCase					(       unittest.TestCase   ):




			"""simple docstring"""







			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Dict:







								"""simple docstring"""
								A_								=    tempfile.mkdtemp()




			def   lowerCamelCase__  (					self    :       str						)   ->			Tuple:







								"""simple docstring"""
								shutil.rmtree(self.output_dir						)




			def   lowerCamelCase__  (					self    :       List[str]							, _snake_case    :       Any=0							, _snake_case    :       Optional[int]=0							, _snake_case    :       int=64							, _snake_case    :       Optional[int]=64							, _snake_case    :       List[Any]=None							, _snake_case    :       List[Any]=False							, **_snake_case    :       Optional[int]						)   ->			List[Any]:







								"""simple docstring"""
								# disable_tqdm in TrainingArguments has a flaky default since it depends on the level of logging. We make sure
								# its set to False since the tests later on depend on its value.
								A_								=    RegressionDataset(length=_snake_case						)
								A_								=    RegressionDataset(length=_snake_case						)
								A_								=    RegressionModelConfig(a=_snake_case							, b=_snake_case						)
								A_								=    RegressionPreTrainedModel(_snake_case						)

								A_								=    TrainingArguments(self.output_dir							, disable_tqdm=_snake_case							, report_to=[]							, **_snake_case						)
								return Trainer(
								    _snake_case							, _snake_case							, train_dataset=_snake_case							, eval_dataset=_snake_case							, callbacks=_snake_case							, )




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Optional[Any]							, _snake_case    :       List[Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								self.assertEqual(len(_snake_case						)							, len(_snake_case						)						)

								# Order doesn't matter
								A_								=    sorted(_snake_case							, key=lambda _snake_case						: cb.__name__ if isinstance(_snake_case							, _snake_case						) else cb.__class__.__name__						)
								A_								=    sorted(_snake_case							, key=lambda _snake_case						: cb.__name__ if isinstance(_snake_case							, _snake_case						) else cb.__class__.__name__						)

								for cba, cba in zip(_snake_case							, _snake_case						):
													if isinstance(_snake_case							, _snake_case						) and isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(_snake_case							, _snake_case						)
													elif isinstance(_snake_case							, _snake_case						) and not isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(_snake_case							, cba.__class__						)
													elif not isinstance(_snake_case							, _snake_case						) and isinstance(_snake_case							, _snake_case						):
																		self.assertEqual(cba.__class__							, _snake_case						)
													else:
																		self.assertEqual(_snake_case							, _snake_case						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Union[str, Any]						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    ["on_init_end", "on_train_begin"]
								A_								=    0
								A_								=    len(trainer.get_eval_dataloader()						)
								A_								=    ["on_prediction_step"] * len(trainer.get_eval_dataloader()						) + ["on_log", "on_evaluate"]
								for _ in range(trainer.state.num_train_epochs						):
													expected_events.append("on_epoch_begin"						)
													for _ in range(_snake_case						):
																		step += 1
																		expected_events += ["on_step_begin", "on_step_end"]
																		if step % trainer.args.logging_steps == 0:
																							expected_events.append("on_log"						)
																		if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
																							expected_events += evaluation_events.copy()
																		if step % trainer.args.save_steps == 0:
																							expected_events.append("on_save"						)
													expected_events.append("on_epoch_end"						)
													if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
																		expected_events += evaluation_events.copy()
								expected_events += ["on_log", "on_train_end"]
								return expected_events




			def   lowerCamelCase__  (					self    :       List[Any]						)   ->			int:







								"""simple docstring"""
								A_								=    self.get_trainer()
								A_								=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# Callbacks passed at init are added to the default callbacks
								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]						)
								expected_callbacks.append(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
								A_								=    self.get_trainer(disable_tqdm=_snake_case						)
								A_								=    DEFAULT_CALLBACKS.copy() + [PrinterCallback]
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)




			def   lowerCamelCase__  (					self    :       Optional[Any]						)   ->			List[Any]:







								"""simple docstring"""
								A_								=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
								A_								=    self.get_trainer()

								# We can add, pop, or remove by class name
								trainer.remove_callback(_snake_case						)
								expected_callbacks.remove(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								A_								=    self.get_trainer()
								A_								=    trainer.pop_callback(_snake_case						)
								self.assertEqual(cb.__class__							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								trainer.add_callback(_snake_case						)
								expected_callbacks.insert(0							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								# We can also add, pop, or remove by instance
								A_								=    self.get_trainer()
								A_								=    trainer.callback_handler.callbacks[0]
								trainer.remove_callback(_snake_case						)
								expected_callbacks.remove(_snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								A_								=    self.get_trainer()
								A_								=    trainer.callback_handler.callbacks[0]
								A_								=    trainer.pop_callback(_snake_case						)
								self.assertEqual(_snake_case							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)

								trainer.add_callback(_snake_case						)
								expected_callbacks.insert(0							, _snake_case						)
								self.check_callbacks_equality(trainer.callback_handler.callbacks							, _snake_case						)






			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			Union[str, Any]:







								"""simple docstring"""
								import warnings

								# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
								warnings.simplefilter(action="ignore"							, category=_snake_case						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# Independent log/save/eval
								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, logging_steps=5						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, save_steps=5						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, eval_steps=5							, evaluation_strategy="steps"						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								A_								=    self.get_trainer(callbacks=[MyTestTrainerCallback]							, evaluation_strategy="epoch"						)
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# A bit of everything
								A_								=    self.get_trainer(
								    callbacks=[MyTestTrainerCallback]							, logging_steps=3							, save_steps=10							, eval_steps=5							, evaluation_strategy="steps"							, )
								trainer.train()
								A_								=    trainer.callback_handler.callbacks[-2].events
								self.assertEqual(_snake_case							, self.get_expected_events(_snake_case						)						)

								# warning should be emitted for duplicated callbacks
								with patch("transformers.trainer_callback.logger.warning"						) as warn_mock:
													A_								=    self.get_trainer(
													    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]							, )
													assert str(_snake_case						) in warn_mock.call_args[0][0]




