




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


__UpperCamelCase   :			Union[str, Any]          = [
    "openmmlab/upernet-convnext-tiny",
    # See all UperNet models at https://huggingface.co/models?filter=upernet
]

# General docstring
__UpperCamelCase   :			Optional[Any]          = "UperNetConfig"

class 						__lowerCAmelCase      (      nn.Module					):
							def __init__(		self		:Dict						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		:Union[int, Tuple[int, int]]						, __magic_name__		:Union[int, Tuple[int, int], str] = 0						, __magic_name__		:bool = False						, __magic_name__		:Union[int, Tuple[int, int]] = 1						, ):







								'''simple docstring'''







								super().__init__()
								a									=				nn.Convad(
								    in_channels=__magic_name__						, out_channels=__magic_name__						, kernel_size=__magic_name__						, padding=__magic_name__						, bias=__magic_name__						, dilation=__magic_name__						, )
								a									=				nn.BatchNormad(__magic_name__							)
								a									=				nn.ReLU()





							def  lowerCamelCase__				(		self		:str						, __magic_name__		:torch.Tensor							):







								'''simple docstring'''







								a									=				self.conv(__magic_name__							)
								a									=				self.batch_norm(__magic_name__							)
								a									=				self.activation(__magic_name__							)

								return output







class 						__lowerCAmelCase      (      nn.Module					):
							def __init__(		self		:Any						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		:int							):







								'''simple docstring'''







								super().__init__()
								a									=				[
								    nn.AdaptiveAvgPoolad(__magic_name__							),
								    UperNetConvModule(__magic_name__						, __magic_name__						, kernel_size=1							),
								]
								for i, layer in enumerate(self.layers							):
									self.add_module(str(__magic_name__							)						, __magic_name__							)





							def  lowerCamelCase__				(		self		:str						, __magic_name__		:torch.Tensor							):







								'''simple docstring'''







								a									=				input
								for layer in self.layers:
									a									=				layer(__magic_name__							)
								return hidden_state







class 						__lowerCAmelCase      (      nn.Module					):
							def __init__(		self		:List[Any]						, __magic_name__		:Tuple[int, ...]						, __magic_name__		:int						, __magic_name__		:int						, __magic_name__		:bool							):







								'''simple docstring'''







								super().__init__()
								a									=				pool_scales
								a									=				align_corners
								a									=				in_channels
								a									=				channels
								a									=				[]
								for i, pool_scale in enumerate(__magic_name__							):
									a									=				UperNetPyramidPoolingBlock(pool_scale=__magic_name__						, in_channels=__magic_name__						, channels=__magic_name__							)
									self.blocks.append(__magic_name__							)
									self.add_module(str(__magic_name__							)						, __magic_name__							)





							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:torch.Tensor							):







								'''simple docstring'''







								a									=				[]
								for ppm in self.blocks:
									a									=				ppm(__magic_name__							)
									a									=				nn.functional.interpolate(
									    __magic_name__						, size=x.size()[2:]						, mode="""bilinear"""						, align_corners=self.align_corners							)
									ppm_outs.append(__magic_name__							)
								return ppm_outs







class 						__lowerCAmelCase      (      nn.Module					):
							def __init__(		self		:Tuple						, __magic_name__		:str						, __magic_name__		:int							):







								'''simple docstring'''







								super().__init__()

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

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

								a									=				UperNetConvModule(
								    len(self.in_channels							) * self.channels						, self.channels						, kernel_size=3						, padding=1						, )
							def  lowerCamelCase__				(		self		:Optional[Any]							):







								'''simple docstring'''







								self.apply(self._init_weights							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:Dict							):







								'''simple docstring'''







								if isinstance(__magic_name__						, 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		:str						, __magic_name__		:Dict							):







								'''simple docstring'''







								a									=				inputs[-1]
								a									=				[x]
								psp_outs.extend(self.psp_modules(__magic_name__							)							)
								a									=				torch.cat(__magic_name__						, dim=1							)
								a									=				self.bottleneck(__magic_name__							)

								return output





							def  lowerCamelCase__				(		self		:int						, __magic_name__		:torch.Tensor							):







								'''simple docstring'''







								a									=				[lateral_conv(encoder_hidden_states[i]							) for i, lateral_conv in enumerate(self.lateral_convs							)]

								laterals.append(self.psp_forward(__magic_name__							)							)

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

								# build outputs
								a									=				[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							):
									a									=				nn.functional.interpolate(
									    fpn_outs[i]						, size=fpn_outs[0].shape[2:]						, mode="""bilinear"""						, align_corners=self.align_corners							)
								a									=				torch.cat(__magic_name__						, dim=1							)
								a									=				self.fpn_bottleneck(__magic_name__							)
								a									=				self.classifier(__magic_name__							)

								return output







class 						__lowerCAmelCase      (      nn.Module					):
							def __init__(		self		:Tuple						, __magic_name__		:Union[str, Any]						, __magic_name__		:int = 2						, __magic_name__		:int = 3						, __magic_name__		:Union[int, Tuple[int, int]] = 1							):







								'''simple docstring'''







								super().__init__()

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

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

								a									=				nn.Convad(self.channels						, config.num_labels						, kernel_size=1							)
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								self.apply(self._init_weights							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:List[str]							):







								'''simple docstring'''







								if isinstance(__magic_name__						, 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		:Union[str, Any]						, __magic_name__		:torch.Tensor							):







								'''simple docstring'''







								a									=				encoder_hidden_states[self.in_index]
								a									=				self.convs(__magic_name__							)
								if self.concat_input:
									a									=				self.conv_cat(torch.cat([hidden_states, output]						, dim=1							)							)
								a									=				self.classifier(__magic_name__							)
								return output







class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    UperNetConfig
							UpperCamelCase__   	=    '''pixel_values'''
							UpperCamelCase__   	=    True
							def  lowerCamelCase__				(		self		:Tuple						, __magic_name__		:Optional[int]							):







								'''simple docstring'''







								if isinstance(__magic_name__						, __magic_name__							):
									module.backbone.init_weights()
									module.decode_head.init_weights()
									module.auxiliary_head.init_weights()
							def  lowerCamelCase__				(		self		:int							):







								'''simple docstring'''







								self.backbone.init_weights()
								self.decode_head.init_weights()
								self.auxiliary_head.init_weights()





							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:Any						, __magic_name__		:int=False							):







								'''simple docstring'''







								if isinstance(__magic_name__						, __magic_name__							):
									a									=				value







__UpperCamelCase   :			List[Any]          = R"\n    Parameters:\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        config ([`UperNetConfig`]): 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"

__UpperCamelCase   :			Optional[Any]          = R"\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using\n            [`AutoImageProcessor`]. See [`SegformerImageProcessor.__call__`] for details.\n        output_attentions (`bool`, *optional*):\n            Whether or not to return the attentions tensors of all attention layers in case the backbone has them. See\n            `attentions` under returned tensors for more detail.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers of the backbone. See `hidden_states` under\n            returned tensors for more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"

@add_start_docstrings(
    '''UperNet framework leveraging any vision backbone e.g. for ADE20k, CityScapes.''' ,			__magic_name__ ,			)
class 						__lowerCAmelCase      (      __magic_name__					):
							def __init__(		self		:str						, __magic_name__		:List[Any]							):







								'''simple docstring'''







								super().__init__(__magic_name__							)

								a									=				AutoBackbone.from_config(config.backbone_config							)

								# Semantic segmentation head(s)
								a									=				UperNetHead(__magic_name__						, in_channels=self.backbone.channels							)
								a									=				UperNetFCNHead(__magic_name__							) 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=__magic_name__						, config_class=_CONFIG_FOR_DOC							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:Optional[torch.Tensor] = None						, __magic_name__		:Optional[bool] = None						, __magic_name__		:Optional[bool] = None						, __magic_name__		:Optional[torch.Tensor] = None						, __magic_name__		:Optional[bool] = None						, ):







								'''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									=				output_attentions if output_attentions is not None else self.config.output_attentions

								a									=				self.backbone.forward_with_filtered_kwargs(
								    __magic_name__						, output_hidden_states=__magic_name__						, output_attentions=__magic_name__							)
								a									=				outputs.feature_maps

								a									=				self.decode_head(__magic_name__							)
								a									=				nn.functional.interpolate(__magic_name__						, size=pixel_values.shape[2:]						, mode="""bilinear"""						, align_corners=__magic_name__							)

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

								a									=				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
										a									=				CrossEntropyLoss(ignore_index=self.config.loss_ignore_index							)
										a									=				loss_fct(__magic_name__						, __magic_name__							)
										a									=				loss_fct(__magic_name__						, __magic_name__							)
										a									=				main_loss + self.config.auxiliary_loss_weight * auxiliary_loss

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

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







def 						__A						(      ) ->			list[list[int]]:
	return [list(range(1000 - i				,     -1000 - i				,     -1       )       ) for i in range(1000       )]


__UpperCamelCase   :			str          = generate_large_matrix()
__UpperCamelCase   :			Tuple          = (
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)





def 						__A						(      __lowerCamelCase       ) ->			None:
	assert all(row == sorted(__lowerCamelCase				,     reverse=__lowerCamelCase       ) for row in grid       )
	assert all(list(__lowerCamelCase       ) == sorted(__lowerCamelCase				,     reverse=__lowerCamelCase       ) for col in zip(*__lowerCamelCase       )       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	a									=				len(__lowerCamelCase       ) - 1

	# Edge cases such as no values or all numbers are negative.
	if not array or array[0] < 0:
		return 0

	while right + 1 > left:
		a									=				(left + right) // 2
		a									=				array[mid]

		# Num must be negative and the index must be greater than or equal to 0.
		if num < 0 and array[mid - 1] >= 0:
			return mid

		if num >= 0:
			a									=				mid + 1
		else:
			a									=				mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
	return len(__lowerCamelCase       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	a									=				len(grid[0]       )

	for i in range(len(__lowerCamelCase       )       ):
		a									=				find_negative_index(grid[i][:bound]       )
		total += bound
	return (len(__lowerCamelCase       ) * len(grid[0]       )) - total





def 						__A						(      __lowerCamelCase       ) ->			int:
	return len([number for row in grid for number in row if number < 0]       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	for row in grid:
		for i, number in enumerate(__lowerCamelCase       ):
			if number < 0:
				total += len(__lowerCamelCase       ) - i
				break
	return total





def 						__A						(      ) ->			None:
	from timeit import timeit

	print("""Running benchmarks"""       )
	a									=				(
	    """from __main__ import count_negatives_binary_search, """
	    """count_negatives_brute_force, count_negatives_brute_force_with_break, grid"""
	)
	for func in (
	    "count_negatives_binary_search",  # took 0.7727 seconds
	    "count_negatives_brute_force_with_break",  # took 4.6505 seconds
	    "count_negatives_brute_force",  # took 12.8160 seconds
	):
		a									=				timeit(f'{func}(grid=grid)'				,     setup=__lowerCamelCase				,     number=500       )
		print(f'{func}() took {time:0.4f} seconds'       )


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()

