



"""simple docstring"""





from typing import List, Optional, Tuple, Union

import torch
from torch import nn
from torch.nn import CrossEntropyLoss

from ... import AutoBackbone
from ...modeling_outputs import SemanticSegmenterOutput
from ...modeling_utils import PreTrainedModel
from ...utils import add_start_docstrings, add_start_docstrings_to_model_forward, replace_return_docstrings
from ...utils.backbone_utils import BackboneMixin
from .configuration_upernet import UperNetConfig


lowercase  							=					[
    '''openmmlab/upernet-convnext-tiny''',
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
lowercase  							=					'''UperNetConfig'''




class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case = 0	,       snake_case = False	,       snake_case = 1	,       )			->							None:
									super().__init__()
									_UpperCAmelCase							 =		nn.Convad(
									    in_channels=snake_case	,       out_channels=snake_case	,       kernel_size=snake_case	,       padding=snake_case	,       bias=snake_case	,       dilation=snake_case	,       )
									_UpperCAmelCase							 =		nn.BatchNormad(snake_case						)
									_UpperCAmelCase							 =		nn.ReLU()






							def        lowerCamelCase_   (  self	,       snake_case						)			->							torch.Tensor:
									_UpperCAmelCase							 =		self.conv(snake_case						)
									_UpperCAmelCase							 =		self.batch_norm(snake_case						)
									_UpperCAmelCase							 =		self.activation(snake_case						)

									return output







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case	,       snake_case						)			->							None:
									super().__init__()
									_UpperCAmelCase							 =		[
									    nn.AdaptiveAvgPoolad(snake_case						),
									    UperNetConvModule(snake_case	,       snake_case	,       kernel_size=1						),
									]
									for i, layer in enumerate(self.layers						):
											self.add_module(str(snake_case						)	,       snake_case						)






							def        lowerCamelCase_   (  self	,       snake_case						)			->							torch.Tensor:
									_UpperCAmelCase							 =		input
									for layer in self.layers:
											_UpperCAmelCase							 =		layer(snake_case						)
									return hidden_state







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							None:
									super().__init__()
									_UpperCAmelCase							 =		pool_scales
									_UpperCAmelCase							 =		align_corners
									_UpperCAmelCase							 =		in_channels
									_UpperCAmelCase							 =		channels
									_UpperCAmelCase							 =		[]
									for i, pool_scale in enumerate(snake_case						):
											_UpperCAmelCase							 =		UperNetPyramidPoolingBlock(pool_scale=snake_case	,       in_channels=snake_case	,       channels=snake_case						)
											self.blocks.append(snake_case						)
											self.add_module(str(snake_case						)	,       snake_case						)






							def        lowerCamelCase_   (  self	,       snake_case						)			->							List[torch.Tensor]:
									_UpperCAmelCase							 =		[]
									for ppm in self.blocks:
											_UpperCAmelCase							 =		ppm(snake_case						)
											_UpperCAmelCase							 =		nn.functional.interpolate(
											    snake_case	,       size=x.size()[2:]	,       mode='bilinear'	,       align_corners=self.align_corners						)
											ppm_outs.append(snake_case						)
									return ppm_outs







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case						)			->							List[str]:
									super().__init__()

									_UpperCAmelCase							 =		config
									_UpperCAmelCase							 =		config.pool_scales  # e.g. (1, 2, 3, 6)
									_UpperCAmelCase							 =		in_channels
									_UpperCAmelCase							 =		config.hidden_size
									_UpperCAmelCase							 =		False
									_UpperCAmelCase							 =		nn.Convad(self.channels	,       config.num_labels	,       kernel_size=1						)

									# PSP Module
									_UpperCAmelCase							 =		UperNetPyramidPoolingModule(
									    self.pool_scales	,       self.in_channels[-1]	,       self.channels	,       align_corners=self.align_corners	,       )
									_UpperCAmelCase							 =		UperNetConvModule(
									    self.in_channels[-1] + len(self.pool_scales						) * self.channels	,       self.channels	,       kernel_size=3	,       padding=1	,       )
									# FPN Module
									_UpperCAmelCase							 =		nn.ModuleList()
									_UpperCAmelCase							 =		nn.ModuleList()
									for in_channels in self.in_channels[:-1]:  # skip the top layer
											_UpperCAmelCase							 =		UperNetConvModule(snake_case	,       self.channels	,       kernel_size=1						)
											_UpperCAmelCase							 =		UperNetConvModule(self.channels	,       self.channels	,       kernel_size=3	,       padding=1						)
											self.lateral_convs.append(snake_case						)
											self.fpn_convs.append(snake_case						)

									_UpperCAmelCase							 =		UperNetConvModule(
									    len(self.in_channels						) * self.channels	,       self.channels	,       kernel_size=3	,       padding=1	,       )






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									self.apply(self._init_weights						)






							def        lowerCamelCase_   (  self	,       snake_case						)			->							Tuple:
									if isinstance(snake_case	,       nn.Convad						):
											module.weight.data.normal_(mean=0.0	,       std=self.config.initializer_range						)
											if module.bias is not None:
													module.bias.data.zero_()






							def        lowerCamelCase_   (  self	,       snake_case						)			->							Optional[int]:
									_UpperCAmelCase							 =		inputs[-1]
									_UpperCAmelCase							 =		[x]
									psp_outs.extend(self.psp_modules(snake_case						)						)
									_UpperCAmelCase							 =		torch.cat(snake_case	,       dim=1						)
									_UpperCAmelCase							 =		self.bottleneck(snake_case						)

									return output






							def        lowerCamelCase_   (  self	,       snake_case						)			->							torch.Tensor:
									# build laterals
									_UpperCAmelCase							 =		[lateral_conv(encoder_hidden_states[i]						) for i, lateral_conv in enumerate(self.lateral_convs						)]

									laterals.append(self.psp_forward(snake_case						)						)

									# build top-down path
									_UpperCAmelCase							 =		len(snake_case						)
									for i in range(used_backbone_levels - 1	,       0	,       -1						):
											_UpperCAmelCase							 =		laterals[i - 1].shape[2:]
											_UpperCAmelCase							 =		laterals[i - 1] + nn.functional.interpolate(
											    laterals[i]	,       size=snake_case	,       mode='bilinear'	,       align_corners=self.align_corners						)

									# build outputs
									_UpperCAmelCase							 =		[self.fpn_convs[i](laterals[i]						) for i in range(used_backbone_levels - 1						)]
									# append psp feature
									fpn_outs.append(laterals[-1]						)

									for i in range(used_backbone_levels - 1	,       0	,       -1						):
											_UpperCAmelCase							 =		nn.functional.interpolate(
											    fpn_outs[i]	,       size=fpn_outs[0].shape[2:]	,       mode='bilinear'	,       align_corners=self.align_corners						)
									_UpperCAmelCase							 =		torch.cat(snake_case	,       dim=1						)
									_UpperCAmelCase							 =		self.fpn_bottleneck(snake_case						)
									_UpperCAmelCase							 =		self.classifier(snake_case						)

									return output







class 			lowercase__			(      nn.Module    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case = 2	,       snake_case = 3	,       snake_case = 1						)			->							None:
									super().__init__()

									_UpperCAmelCase							 =		config
									_UpperCAmelCase							 =		config.auxiliary_in_channels
									_UpperCAmelCase							 =		config.auxiliary_channels
									_UpperCAmelCase							 =		config.auxiliary_num_convs
									_UpperCAmelCase							 =		config.auxiliary_concat_input
									_UpperCAmelCase							 =		in_index

									_UpperCAmelCase							 =		(kernel_size // 2) * dilation
									_UpperCAmelCase							 =		[]
									convs.append(
									    UperNetConvModule(
									        self.in_channels	,       self.channels	,       kernel_size=snake_case	,       padding=snake_case	,       dilation=snake_case						)						)
									for i in range(self.num_convs - 1						):
											convs.append(
											    UperNetConvModule(
											        self.channels	,       self.channels	,       kernel_size=snake_case	,       padding=snake_case	,       dilation=snake_case						)						)
									if self.num_convs == 0:
											_UpperCAmelCase							 =		nn.Identity()
									else:
											_UpperCAmelCase							 =		nn.Sequential(*snake_case						)
									if self.concat_input:
											_UpperCAmelCase							 =		UperNetConvModule(
											    self.in_channels + self.channels	,       self.channels	,       kernel_size=snake_case	,       padding=kernel_size // 2						)

									_UpperCAmelCase							 =		nn.Convad(self.channels	,       config.num_labels	,       kernel_size=1						)






							def        lowerCamelCase_   (  self						)			->							Dict:
									self.apply(self._init_weights						)






							def        lowerCamelCase_   (  self	,       snake_case						)			->							Union[str, Any]:
									if isinstance(snake_case	,       nn.Convad						):
											module.weight.data.normal_(mean=0.0	,       std=self.config.initializer_range						)
											if module.bias is not None:
													module.bias.data.zero_()






							def        lowerCamelCase_   (  self	,       snake_case						)			->							torch.Tensor:
									# just take the relevant feature maps
									_UpperCAmelCase							 =		encoder_hidden_states[self.in_index]
									_UpperCAmelCase							 =		self.convs(snake_case						)
									if self.concat_input:
											_UpperCAmelCase							 =		self.conv_cat(torch.cat([hidden_states, output]	,       dim=1						)						)
									_UpperCAmelCase							 =		self.classifier(snake_case						)
									return output







class 			lowercase__			(      A    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					UperNetConfig
							_UpperCAmelCase      				=					'''pixel_values'''
							_UpperCAmelCase      				=					True






							def        lowerCamelCase_   (  self	,       snake_case						)			->							List[str]:
									if isinstance(snake_case	,       snake_case						):
											module.backbone.init_weights()
											module.decode_head.init_weights()
											module.auxiliary_head.init_weights()






							def        lowerCamelCase_   (  self						)			->							Optional[int]:
									self.backbone.init_weights()
									self.decode_head.init_weights()
									self.auxiliary_head.init_weights()






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case=False						)			->							Any:
									if isinstance(snake_case	,       snake_case						):
											_UpperCAmelCase							 =		value







lowercase  							=					r'''
    Parameters:
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.
        config ([`UperNetConfig`]): 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.
'''

lowercase  							=					r'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See
            `attentions` under returned tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers of the backbone. 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(
    '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''',					A,					)
class 			lowercase__			(      A    ):
							'''simple docstring'''







							def __init__(  self	,       snake_case						)			->							Dict:
									super().__init__(snake_case						)

									_UpperCAmelCase							 =		AutoBackbone.from_config(config.backbone_config						)

									# Semantic segmentation head(s)
									_UpperCAmelCase							 =		UperNetHead(snake_case	,       in_channels=self.backbone.channels						)
									_UpperCAmelCase							 =		UperNetFCNHead(snake_case						) if config.use_auxiliary_head else None

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






							@add_start_docstrings_to_model_forward(UPERNET_INPUTS_DOCSTRING.format('batch_size, sequence_length'						)						)
							@replace_return_docstrings(output_type=snake_case	,       config_class=_CONFIG_FOR_DOC						)
							def        lowerCamelCase_   (  self	,       snake_case = None	,       snake_case = None	,       snake_case = None	,       snake_case = None	,       snake_case = None	,       )			->							Union[tuple, SemanticSegmenterOutput]:
									_UpperCAmelCase							 =		return_dict if return_dict is not None else self.config.use_return_dict
									_UpperCAmelCase							 =		(
									    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
									)
									_UpperCAmelCase							 =		output_attentions if output_attentions is not None else self.config.output_attentions

									_UpperCAmelCase							 =		self.backbone.forward_with_filtered_kwargs(
									    snake_case	,       output_hidden_states=snake_case	,       output_attentions=snake_case						)
									_UpperCAmelCase							 =		outputs.feature_maps

									_UpperCAmelCase							 =		self.decode_head(snake_case						)
									_UpperCAmelCase							 =		nn.functional.interpolate(snake_case	,       size=pixel_values.shape[2:]	,       mode='bilinear'	,       align_corners=snake_case						)

									_UpperCAmelCase							 =		None
									if self.auxiliary_head is not None:
											_UpperCAmelCase							 =		self.auxiliary_head(snake_case						)
											_UpperCAmelCase							 =		nn.functional.interpolate(
											    snake_case	,       size=pixel_values.shape[2:]	,       mode='bilinear'	,       align_corners=snake_case						)

									_UpperCAmelCase							 =		None
									if labels is not None:
											if self.config.num_labels == 1:
													raise ValueError('The number of labels should be greater than one'						)
											else:
													# compute weighted loss
													_UpperCAmelCase							 =		CrossEntropyLoss(ignore_index=self.config.loss_ignore_index						)
													_UpperCAmelCase							 =		loss_fct(snake_case	,       snake_case						)
													_UpperCAmelCase							 =		loss_fct(snake_case	,       snake_case						)
													_UpperCAmelCase							 =		main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

									return SemanticSegmenterOutput(
									    loss=snake_case	,       logits=snake_case	,       hidden_states=outputs.hidden_states	,       attentions=outputs.attentions	,       )







"""simple docstring"""





import argparse
import logging
import pickle
from collections import Counter


logging.basicConfig(
    format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s''', datefmt='''%m/%d/%Y %H:%M:%S''', level=logging.INFO
)
lowercase  							=					logging.getLogger(__name__)

if __name__ == "__main__":
					lowercase  							=					argparse.ArgumentParser(
					    description='''Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)'''
					)
					parser.add_argument(
					    '''--data_file''', type=str, default='''data/dump.bert-base-uncased.pickle''', help='''The binarized dataset.'''
					)
					parser.add_argument(
					    '''--token_counts_dump''', type=str, default='''data/token_counts.bert-base-uncased.pickle''', help='''The dump file.'''
					)
					parser.add_argument('''--vocab_size''', default=3_05_22, type=int)
					lowercase  							=					parser.parse_args()

					logger.info(F'''Loading data from {args.data_file}''')
					with open(args.data_file, '''rb''') as fp:
										lowercase  							=					pickle.load(fp)

					logger.info('''Counting occurrences for MLM.''')
					lowercase  							=					Counter()
					for tk_ids in data:
										counter.update(tk_ids)
					lowercase  							=					[0] * args.vocab_size
					for k, v in counter.items():
										lowercase  							=					v

					logger.info(F'''Dump to {args.token_counts_dump}''')
					with open(args.token_counts_dump, '''wb''') as handle:
										pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)


