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


SCREAMING_SNAKE_CASE__   =					logging.get_logger(__name__)

# General docstring
SCREAMING_SNAKE_CASE__   =					"PoolFormerConfig"

# Base docstring
SCREAMING_SNAKE_CASE__   =					"sail/poolformer_s12"
SCREAMING_SNAKE_CASE__   =					[1, 512, 7, 7]

# Image classification docstring
SCREAMING_SNAKE_CASE__   =					"sail/poolformer_s12"
SCREAMING_SNAKE_CASE__   =					"tabby, tabby cat"

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





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Tuple	,     _UpperCamelCase    :			float = 0.0	,     _UpperCamelCase    :			bool = False   )   ->			Optional[Any]:





						"""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(_UpperCamelCase	,     dtype=input.dtype	,     device=input.device   )
						random_tensor.floor_()  # binarize
						snake_case        =     input.div(_UpperCamelCase   ) * random_tensor
						return output



class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase = None      ):






										"""simple docstring"""

										super().__init__()
										snake_case        =     drop_prob

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

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





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase=None      ):






										"""simple docstring"""

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

										snake_case        =     nn.Convad(lowerCAmelCase						,      lowerCAmelCase						,      kernel_size=lowerCAmelCase						,      stride=lowerCAmelCase						,      padding=lowerCAmelCase      )
										snake_case        =     norm_layer(lowerCAmelCase      ) if norm_layer else nn.Identity()

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     self.projection(lowerCAmelCase      )
										snake_case        =     self.norm(lowerCAmelCase      )
										return embeddings





class 					lowerCAmelCase_      (     nn.GroupNorm   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      **lowerCAmelCase      ):






										"""simple docstring"""

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





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										return self.pool(lowerCAmelCase      ) - hidden_states





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     self.conva(lowerCAmelCase      )
										snake_case        =     self.act_fn(lowerCAmelCase      )
										snake_case        =     self.drop(lowerCAmelCase      )
										snake_case        =     self.conva(lowerCAmelCase      )
										snake_case        =     self.drop(lowerCAmelCase      )

										return hidden_states





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      ):






										"""simple docstring"""

										super().__init__()
										snake_case        =     PoolFormerPooling(lowerCAmelCase      )
										snake_case        =     PoolFormerOutput(lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase						,      lowerCAmelCase      )
										snake_case        =     PoolFormerGroupNorm(lowerCAmelCase      )
										snake_case        =     PoolFormerGroupNorm(lowerCAmelCase      )

										# Useful for training neural nets
										snake_case        =     PoolFormerDropPath(lowerCAmelCase      ) 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((lowerCAmelCase)      )						,      requires_grad=lowerCAmelCase      )
																snake_case        =     nn.Parameter(
																    config.layer_scale_init_value * torch.ones((lowerCAmelCase)      )						,      requires_grad=lowerCAmelCase      )

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

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

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

																snake_case        =     (output,) + outputs
																return outputs

										else:
																snake_case        =     self.drop_path(self.pooling(self.before_norm(lowerCAmelCase      )      )      )
																# 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(lowerCAmelCase      )      )      )
																snake_case        =     hidden_states + layer_output

																snake_case        =     (output,) + outputs
																return outputs





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase      ):






										"""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(lowerCAmelCase      )

										# 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(
																						        lowerCAmelCase						,      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(lowerCAmelCase      )      )

										snake_case        =     nn.ModuleList(lowerCAmelCase      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase=False						,      lowerCAmelCase=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(lowerCAmelCase      )
																# Send the embeddings through the blocks
																for _, blk in enumerate(lowerCAmelCase      ):
																						snake_case        =     blk(lowerCAmelCase      )
																						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=lowerCAmelCase						,      hidden_states=lowerCAmelCase      )





class 					lowerCAmelCase_      (     lowerCAmelCase   ):



				"""simple docstring"""
				_lowerCAmelCase       :      str     					=  PoolFormerConfig
				_lowerCAmelCase       :      List[Any]     					=  """poolformer"""
				_lowerCAmelCase       :      Tuple     					=  """pixel_values"""
				_lowerCAmelCase       :      Any     					=  True

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										if isinstance(lowerCAmelCase						,      (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(lowerCAmelCase						,      nn.LayerNorm      ):
																module.bias.data.zero_()
																module.weight.data.fill_(1.0      )

				def 						snake_case	(			self						,      lowerCAmelCase						,      lowerCAmelCase=False      ):






										"""simple docstring"""

										if isinstance(lowerCAmelCase						,      lowerCAmelCase      ):
																snake_case        =     value





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

SCREAMING_SNAKE_CASE__   =					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."""       ,      lowerCAmelCase       ,      )
class 					lowerCAmelCase_      (     lowerCAmelCase   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										super().__init__(lowerCAmelCase      )
										snake_case        =     config

										snake_case        =     PoolFormerEncoder(lowerCAmelCase      )

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

				def 						snake_case	(			self      ):






										"""simple docstring"""

										return self.embeddings.patch_embeddings

				@add_start_docstrings_to_model_forward(lowerCAmelCase      )
				@add_code_sample_docstrings(
				    checkpoint=_CHECKPOINT_FOR_DOC						,      output_type=lowerCAmelCase						,      config_class=_CONFIG_FOR_DOC						,      modality='vision'						,      expected_output=_EXPECTED_OUTPUT_SHAPE						,      )
				def 						snake_case	(			self						,      lowerCAmelCase = None						,      lowerCAmelCase = None						,      lowerCAmelCase = 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(
										    lowerCAmelCase						,      output_hidden_states=lowerCAmelCase						,      return_dict=lowerCAmelCase						,      )
										snake_case        =     encoder_outputs[0]

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

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





class 					lowerCAmelCase_      (     nn.Module   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

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

				def 						snake_case	(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										snake_case        =     self.dense(lowerCAmelCase      )
										return output





@add_start_docstrings(
    """
    PoolFormer Model transformer with an image classification head on top
    """       ,      lowerCAmelCase       ,      )
class 					lowerCAmelCase_      (     lowerCAmelCase   ):



				"""simple docstring"""

				def __init__(			self						,      lowerCAmelCase      ):






										"""simple docstring"""

										super().__init__(lowerCAmelCase      )
										snake_case        =     config.num_labels
										snake_case        =     PoolFormerModel(lowerCAmelCase      )

										# 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(lowerCAmelCase      )
				@add_code_sample_docstrings(
				    checkpoint=_IMAGE_CLASS_CHECKPOINT						,      output_type=lowerCAmelCase						,      config_class=_CONFIG_FOR_DOC						,      expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT						,      )
				def 						snake_case	(			self						,      lowerCAmelCase = None						,      lowerCAmelCase = None						,      lowerCAmelCase = None						,      lowerCAmelCase = None						,      ):






										"""simple docstring"""

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

										snake_case        =     self.poolformer(
										    lowerCAmelCase						,      output_hidden_states=lowerCAmelCase						,      return_dict=lowerCAmelCase						,      )

										snake_case        =     outputs[0]

										snake_case        =     self.classifier(self.norm(lowerCAmelCase      ).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(lowerCAmelCase						,      lowerCAmelCase      )
																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(lowerCAmelCase						,      lowerCAmelCase      )

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

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



"""simple docstring"""
import argparse
import math
import traceback

import dateutil.parser as date_parser
import requests





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Any   )   ->			int:





						"""simple docstring"""






						snake_case        =     {}

						snake_case        =     job['started_at']
						snake_case        =     job['completed_at']

						snake_case        =     date_parser.parse(_UpperCamelCase   )
						snake_case        =     date_parser.parse(_UpperCamelCase   )

						snake_case        =     round((end_datetime - start_datetime).total_seconds() / 60.0   )

						snake_case        =     start
						snake_case        =     end
						snake_case        =     duration_in_min

						return job_info





def 				lowerCAmelCase__      (     _UpperCamelCase    :			Dict	,     _UpperCamelCase    :			Any=None   )   ->			Union[str, Any]:





						"""simple docstring"""






						snake_case        =     None
						if token is not None:
												snake_case        =     {'Accept': 'application/vnd.github+json', 'Authorization': f"""Bearer {token}"""}

						snake_case        =     f"""https://api.github.com/repos/huggingface/transformers/actions/runs/{workflow_run_id}/jobs?per_page=100"""
						snake_case        =     requests.get(_UpperCamelCase	,     headers=_UpperCamelCase   ).json()
						snake_case        =     {}

						try:
												job_time.update({job['name']: extract_time_from_single_job(_UpperCamelCase   ) for job in result['jobs']}   )
												snake_case        =     math.ceil((result['total_count'] - 1_0_0) / 1_0_0   )

												for i in range(_UpperCamelCase   ):
																		snake_case        =     requests.get(url + f"""&page={i + 2}"""	,     headers=_UpperCamelCase   ).json()
																		job_time.update({job['name']: extract_time_from_single_job(_UpperCamelCase   ) for job in result['jobs']}   )

												return job_time
						except Exception:
												print(f"""Unknown error, could not fetch links:\n{traceback.format_exc()}"""   )

						return {}


if __name__ == "__main__":
					SCREAMING_SNAKE_CASE__   =					argparse.ArgumentParser()
					# Required parameters
					parser.add_argument("--workflow_run_id", type=str, required=True, help="A GitHub Actions workflow run id.")
					SCREAMING_SNAKE_CASE__   =					parser.parse_args()

					SCREAMING_SNAKE_CASE__   =					get_job_time(args.workflow_run_id)
					SCREAMING_SNAKE_CASE__   =					dict(sorted(job_time.items(), key=lambda item: item[1]["duration"], reverse=True))

					for k, v in job_time.items():
										print(f"""{k}: {v['duration']}""")


