

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


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

# General docstring
SCREAMING_SNAKE_CASE_					:	List[str]							  =						'UperNetConfig'



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Tuple    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: Union[int, Tuple[int, int]]    ,						UpperCamelCase: Union[int, Tuple[int, int], str] = 0    ,						UpperCamelCase: bool = False    ,						UpperCamelCase: Union[int, Tuple[int, int]] = 1    ,						):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: List[Any]    ,						UpperCamelCase: torch.Tensor       ):






         """simple docstring"""


         A__										=  self.conv(UpperCamelCase       )
         A__										=  self.batch_norm(UpperCamelCase       )
         A__										=  self.activation(UpperCamelCase       )

         return output



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Optional[Any]    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: int       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Dict    ,						UpperCamelCase: torch.Tensor       ):






         """simple docstring"""


         A__										=  input
         for layer in self.layers:
              A__										=  layer(UpperCamelCase       )
         return hidden_state



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Optional[int]    ,						UpperCamelCase: Tuple[int, ...]    ,						UpperCamelCase: int    ,						UpperCamelCase: int    ,						UpperCamelCase: bool       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: int    ,						UpperCamelCase: torch.Tensor       ):






         """simple docstring"""


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



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Dict    ,						UpperCamelCase: Optional[Any]    ,						UpperCamelCase: str       ):






         """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(UpperCamelCase    ,						self.channels    ,						kernel_size=1       )
              A__										=  UperNetConvModule(self.channels    ,						self.channels    ,						kernel_size=3    ,						padding=1       )
              self.lateral_convs.append(UpperCamelCase       )
              self.fpn_convs.append(UpperCamelCase       )

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

    def 				UpperCamelCase	(     self: Optional[Any]       ):






         """simple docstring"""


         self.apply(self._init_weights       )

    def 				UpperCamelCase	(     self: Optional[Any]    ,						UpperCamelCase: List[Any]       ):






         """simple docstring"""


         if isinstance(UpperCamelCase    ,						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 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: Tuple       ):






         """simple docstring"""


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

         return output





    def 				UpperCamelCase	(     self: List[Any]    ,						UpperCamelCase: 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(UpperCamelCase       )       )

         # build top-down path
         A__										=  len(UpperCamelCase       )
         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=UpperCamelCase    ,						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(UpperCamelCase    ,						dim=1       )
         A__										=  self.fpn_bottleneck(UpperCamelCase       )
         A__										=  self.classifier(UpperCamelCase       )

         return output



class 					a						(       nn.Module				):



    """simple docstring"""



    def __init__(     self: Optional[Any]    ,						UpperCamelCase: Optional[int]    ,						UpperCamelCase: int = 2    ,						UpperCamelCase: int = 3    ,						UpperCamelCase: 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=UpperCamelCase    ,						padding=UpperCamelCase    ,						dilation=UpperCamelCase       )       )
         for i in range(self.num_convs - 1       ):
              convs.append(
                  UperNetConvModule(
                      self.channels    ,						self.channels    ,						kernel_size=UpperCamelCase    ,						padding=UpperCamelCase    ,						dilation=UpperCamelCase       )       )
         if self.num_convs == 0:
              A__										=  nn.Identity()
         else:
              A__										=  nn.Sequential(*UpperCamelCase       )
         if self.concat_input:
              A__										=  UperNetConvModule(
                  self.in_channels + self.channels    ,						self.channels    ,						kernel_size=UpperCamelCase    ,						padding=kernel_size // 2       )

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

    def 				UpperCamelCase	(     self: Tuple       ):






         """simple docstring"""


         self.apply(self._init_weights       )

    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: Any       ):






         """simple docstring"""


         if isinstance(UpperCamelCase    ,						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 				UpperCamelCase	(     self: Optional[int]    ,						UpperCamelCase: torch.Tensor       ):






         """simple docstring"""


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



class 					a						(       _lowerCamelCase				):



    """simple docstring"""



    UpperCAmelCase						 =					UperNetConfig
    UpperCAmelCase						 =					"pixel_values"
    UpperCAmelCase						 =					True

    def 				UpperCamelCase	(     self: int    ,						UpperCamelCase: List[str]       ):






         """simple docstring"""


         if isinstance(UpperCamelCase    ,						UpperCamelCase       ):
              module.backbone.init_weights()
              module.decode_head.init_weights()
              module.auxiliary_head.init_weights()

    def 				UpperCamelCase	(     self: Optional[int]       ):






         """simple docstring"""


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





    def 				UpperCamelCase	(     self: Union[str, Any]    ,						UpperCamelCase: int    ,						UpperCamelCase: List[str]=False       ):






         """simple docstring"""


         if isinstance(UpperCamelCase    ,						UpperCamelCase       ):
              A__										=  value

SCREAMING_SNAKE_CASE_					:	Optional[int]							  =						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'

SCREAMING_SNAKE_CASE_					:	Union[str, 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.",	_lowerCamelCase,	)
class 					a						(       _lowerCamelCase				):



    """simple docstring"""



    def __init__(     self: List[Any]    ,						UpperCamelCase: Union[str, Any]       ):






         """simple docstring"""


         super().__init__(UpperCamelCase       )

         A__										=  AutoBackbone.from_config(config.backbone_config       )

         # Semantic segmentation head(s)
         A__										=  UperNetHead(UpperCamelCase    ,						in_channels=self.backbone.channels       )
         A__										=  UperNetFCNHead(UpperCamelCase       ) 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=UpperCamelCase    ,						config_class=_CONFIG_FOR_DOC       )
    def 				UpperCamelCase	(     self: Tuple    ,						UpperCamelCase: Optional[torch.Tensor] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[bool] = None    ,						UpperCamelCase: Optional[torch.Tensor] = None    ,						UpperCamelCase: 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(
             UpperCamelCase    ,						output_hidden_states=UpperCamelCase    ,						output_attentions=UpperCamelCase       )
         A__										=  outputs.feature_maps

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

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

         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(UpperCamelCase    ,						UpperCamelCase       )
                   A__										=  loss_fct(UpperCamelCase    ,						UpperCamelCase       )
                   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=UpperCamelCase    ,						logits=UpperCamelCase    ,						hidden_states=outputs.hidden_states    ,						attentions=outputs.attentions    ,						)









"""simple docstring"""



import argparse
from pathlib import Path
from typing import Dict, OrderedDict, Tuple

import torch
from audiocraft.models import MusicGen

from transformers import (
    AutoFeatureExtractor,
    AutoTokenizer,
    EncodecModel,
    MusicgenDecoderConfig,
    MusicgenForConditionalGeneration,
    MusicgenProcessor,
    TaEncoderModel,
)
from transformers.models.musicgen.modeling_musicgen import MusicgenForCausalLM
from transformers.utils import logging


logging.set_verbosity_info()
SCREAMING_SNAKE_CASE_					:	Union[str, Any]							  =						logging.get_logger(__name__)


SCREAMING_SNAKE_CASE_					:	Any							  =						['model.decoder.embed_positions.weights']





def  _snake_case     (    UpperCAmelCase_  :					Optional[int]			):
     if "emb" in name:
          A__										=  name.replace("""emb"""					,   """model.decoder.embed_tokens"""			)
     if "transformer" in name:
          A__										=  name.replace("""transformer"""					,   """model.decoder"""			)
     if "cross_attention" in name:
          A__										=  name.replace("""cross_attention"""					,   """encoder_attn"""			)
     if "linear1" in name:
          A__										=  name.replace("""linear1"""					,   """fc1"""			)
     if "linear2" in name:
          A__										=  name.replace("""linear2"""					,   """fc2"""			)
     if "norm1" in name:
          A__										=  name.replace("""norm1"""					,   """self_attn_layer_norm"""			)
     if "norm_cross" in name:
          A__										=  name.replace("""norm_cross"""					,   """encoder_attn_layer_norm"""			)
     if "norm2" in name:
          A__										=  name.replace("""norm2"""					,   """final_layer_norm"""			)
     if "out_norm" in name:
          A__										=  name.replace("""out_norm"""					,   """model.decoder.layer_norm"""			)
     if "linears" in name:
          A__										=  name.replace("""linears"""					,   """lm_heads"""			)
     if "condition_provider.conditioners.description.output_proj" in name:
          A__										=  name.replace("""condition_provider.conditioners.description.output_proj"""					,   """enc_to_dec_proj"""			)
     return name





def  _snake_case     (    UpperCAmelCase_  :					OrderedDict					,   UpperCAmelCase_  :					int			):
     A__										=  list(state_dict.keys()			)
     A__										=  {}
     for key in keys:
          A__										=  state_dict.pop(UpperCAmelCase_			)
          A__										=  rename_keys(UpperCAmelCase_			)
          if "in_proj_weight" in key:
               # split fused qkv proj
               A__										=  val[:hidden_size, :]
               A__										=  val[hidden_size : 2 * hidden_size, :]
               A__										=  val[-hidden_size:, :]
          elif "enc_to_dec_proj" in key:
               A__										=  val
          else:
               A__										=  val
     return state_dict, enc_dec_proj_state_dict





def  _snake_case     (    UpperCAmelCase_  :					str			):
     if checkpoint == "small":
          # default config values
          A__										=  1024
          A__										=  24
          A__										=  16
     elif checkpoint == "medium":
          A__										=  1536
          A__										=  48
          A__										=  24
     elif checkpoint == "large":
          A__										=  2048
          A__										=  48
          A__										=  32
     else:
          raise ValueError(F"""Checkpoint should be one of `['small', 'medium', 'large']`, got {checkpoint}."""			)
     A__										=  MusicgenDecoderConfig(
         hidden_size=UpperCAmelCase_					,   ffn_dim=hidden_size * 4					,   num_hidden_layers=UpperCAmelCase_					,   num_attention_heads=UpperCAmelCase_					,   )
     return config





@torch.no_grad()
def  _snake_case     (    UpperCAmelCase_  :					int					,   UpperCAmelCase_  :					List[Any]=None					,   UpperCAmelCase_  :					Tuple=None					,   UpperCAmelCase_  :					Any="cpu"			):
     A__										=  MusicGen.get_pretrained(UpperCAmelCase_					,   device=UpperCAmelCase_			)
     A__										=  decoder_config_from_checkpoint(UpperCAmelCase_			)

     A__										=  fairseq_model.lm.state_dict()
     A__			,				A__										=  rename_state_dict(
         UpperCAmelCase_					,   hidden_size=decoder_config.hidden_size			)

     A__										=  TaEncoderModel.from_pretrained("""t5-base"""			)
     A__										=  EncodecModel.from_pretrained("""facebook/encodec_32khz"""			)
     A__										=  MusicgenForCausalLM(UpperCAmelCase_			).eval()

     # load all decoder weights - expect that we'll be missing embeddings and enc-dec projection
     A__			,				A__										=  decoder.load_state_dict(UpperCAmelCase_					,   strict=UpperCAmelCase_			)

     for key in missing_keys.copy():
          if key.startswith(("""text_encoder""", """audio_encoder""")			) or key in EXPECTED_MISSING_KEYS:
               missing_keys.remove(UpperCAmelCase_			)

     if len(UpperCAmelCase_			) > 0:
          raise ValueError(F"""Missing key(s) in state_dict: {missing_keys}"""			)

     if len(UpperCAmelCase_			) > 0:
          raise ValueError(F"""Unexpected key(s) in state_dict: {unexpected_keys}"""			)

     # init the composite model
     A__										=  MusicgenForConditionalGeneration(text_encoder=UpperCAmelCase_					,   audio_encoder=UpperCAmelCase_					,   decoder=UpperCAmelCase_			)

     # load the pre-trained enc-dec projection (from the decoder state dict)
     model.enc_to_dec_proj.load_state_dict(UpperCAmelCase_			)

     # check we can do a forward pass
     A__										=  torch.arange(0					,   8					,   dtype=torch.long			).reshape(2					,   -1			)
     A__										=  input_ids.reshape(2 * 4					,   -1			)

     with torch.no_grad():
          A__										=  model(input_ids=UpperCAmelCase_					,   decoder_input_ids=UpperCAmelCase_			).logits

     if logits.shape != (8, 1, 2048):
          raise ValueError("""Incorrect shape for logits"""			)

     # now construct the processor
     A__										=  AutoTokenizer.from_pretrained("""t5-base"""			)
     A__										=  AutoFeatureExtractor.from_pretrained("""facebook/encodec_32khz"""					,   padding_side="""left"""			)

     A__										=  MusicgenProcessor(feature_extractor=UpperCAmelCase_					,   tokenizer=UpperCAmelCase_			)

     # set the appropriate bos/pad token ids
     A__										=  2048
     A__										=  2048

     # set other default generation config params
     A__										=  int(30 * audio_encoder.config.frame_rate			)
     A__										=  True
     A__										=  3.0

     if pytorch_dump_folder is not None:
          Path(UpperCAmelCase_			).mkdir(exist_ok=UpperCAmelCase_			)
          logger.info(F"""Saving model {checkpoint} to {pytorch_dump_folder}"""			)
          model.save_pretrained(UpperCAmelCase_			)
          processor.save_pretrained(UpperCAmelCase_			)

     if repo_id:
          logger.info(F"""Pushing model {checkpoint} to {repo_id}"""			)
          model.push_to_hub(UpperCAmelCase_			)
          processor.push_to_hub(UpperCAmelCase_			)


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE_					:	int							  =						argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--checkpoint',
     default='small',
     type=str,
     help='Checkpoint size of the MusicGen model you\'d like to convert. Can be one of: `[\'small\', \'medium\', \'large\']`.',
 )
 parser.add_argument(
     '--pytorch_dump_folder',
     required=True,
     default=None,
     type=str,
     help='Path to the output PyTorch model directory.',
 )
 parser.add_argument(
     '--push_to_hub', default=None, type=str, help='Where to upload the converted model on the 🤗 hub.'
 )
 parser.add_argument(
     '--device', default='cpu', type=str, help='Torch device to run the conversion, either cpu or cuda.'
 )

 SCREAMING_SNAKE_CASE_					:	Tuple							  =						parser.parse_args()
 convert_musicgen_checkpoint(args.checkpoint, args.pytorch_dump_folder, args.push_to_hub)






