
'''simple docstring'''






import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import SegformerImageProcessor, SwinConfig, UperNetConfig, UperNetForSemanticSegmentation







def 		A_							(						_lowerCAmelCase :     Tuple				):



      """simple docstring"""




      _lowerCamelCase					: Dict		    = 384
      _lowerCamelCase					: Optional[Any]		    = 7
      if "tiny" in model_name:
            _lowerCamelCase					: Tuple		    = 96
            _lowerCamelCase					: Union[str, Any]		    = (2, 2, 6, 2)
            _lowerCamelCase					: int		    = (3, 6, 12, 24)
      elif "small" in model_name:
            _lowerCamelCase					: int		    = 96
            _lowerCamelCase					: List[str]		    = (2, 2, 18, 2)
            _lowerCamelCase					: List[Any]		    = (3, 6, 12, 24)
      elif "base" in model_name:
            _lowerCamelCase					: Dict		    = 128
            _lowerCamelCase					: Union[str, Any]		    = (2, 2, 18, 2)
            _lowerCamelCase					: Optional[Any]		    = (4, 8, 16, 32)
            _lowerCamelCase					: List[str]		    = 12
            _lowerCamelCase					: List[str]		    = 512
      elif "large" in model_name:
            _lowerCamelCase					: List[Any]		    = 192
            _lowerCamelCase					: Optional[int]		    = (2, 2, 18, 2)
            _lowerCamelCase					: str		    = (6, 12, 24, 48)
            _lowerCamelCase					: Optional[int]		    = 12
            _lowerCamelCase					: int		    = 768

      # set label information
      _lowerCamelCase					: List[Any]		    = 150
      _lowerCamelCase					: Dict		    = "huggingface/label-files"
      _lowerCamelCase					: Tuple		    = "ade20k-id2label.json"
      _lowerCamelCase					: Union[str, Any]		    = json.load(open(hf_hub_download(lowerCAmelCase__					,						lowerCAmelCase__					,						repo_type="dataset"				)					,						"r"				)				)
      _lowerCamelCase					: int		    = {int(lowerCAmelCase__				): v for k, v in idalabel.items()}
      _lowerCamelCase					: Tuple		    = {v: k for k, v in idalabel.items()}

      _lowerCamelCase					: Tuple		    = SwinConfig(
          embed_dim=lowerCAmelCase__					,						depths=lowerCAmelCase__					,						num_heads=lowerCAmelCase__					,						window_size=lowerCAmelCase__					,						out_features=["stage1", "stage2", "stage3", "stage4"]					,						)
      _lowerCamelCase					: Optional[int]		    = UperNetConfig(
          backbone_config=lowerCAmelCase__					,						auxiliary_in_channels=lowerCAmelCase__					,						num_labels=lowerCAmelCase__					,						idalabel=lowerCAmelCase__					,						labelaid=lowerCAmelCase__					,						)

      return config







def 		A_							(						_lowerCAmelCase :     List[str]				):



      """simple docstring"""




      _lowerCamelCase					: int		    = []

      # fmt: off
      # stem
      rename_keys.append(("backbone.patch_embed.projection.weight", "backbone.embeddings.patch_embeddings.projection.weight")				)
      rename_keys.append(("backbone.patch_embed.projection.bias", "backbone.embeddings.patch_embeddings.projection.bias")				)
      rename_keys.append(("backbone.patch_embed.norm.weight", "backbone.embeddings.norm.weight")				)
      rename_keys.append(("backbone.patch_embed.norm.bias", "backbone.embeddings.norm.bias")				)
      # stages
      for i in range(len(config.backbone_config.depths				)				):
            for j in range(config.backbone_config.depths[i]				):
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.norm1.weight', F'backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.norm1.bias', F'backbone.encoder.layers.{i}.blocks.{j}.layernorm_before.bias')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_bias_table', F'backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_bias_table')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.attn.w_msa.relative_position_index', F'backbone.encoder.layers.{i}.blocks.{j}.attention.self.relative_position_index')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.weight', F'backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.attn.w_msa.proj.bias', F'backbone.encoder.layers.{i}.blocks.{j}.attention.output.dense.bias')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.norm2.weight', F'backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.norm2.bias', F'backbone.encoder.layers.{i}.blocks.{j}.layernorm_after.bias')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.weight', F'backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.ffn.layers.0.0.bias', F'backbone.encoder.layers.{i}.blocks.{j}.intermediate.dense.bias')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.ffn.layers.1.weight', F'backbone.encoder.layers.{i}.blocks.{j}.output.dense.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.blocks.{j}.ffn.layers.1.bias', F'backbone.encoder.layers.{i}.blocks.{j}.output.dense.bias')				)

            if i < 3:
                  rename_keys.append((F'backbone.stages.{i}.downsample.reduction.weight', F'backbone.encoder.layers.{i}.downsample.reduction.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.downsample.norm.weight', F'backbone.encoder.layers.{i}.downsample.norm.weight')				)
                  rename_keys.append((F'backbone.stages.{i}.downsample.norm.bias', F'backbone.encoder.layers.{i}.downsample.norm.bias')				)
            rename_keys.append((F'backbone.norm{i}.weight', F'backbone.hidden_states_norms.stage{i+1}.weight')				)
            rename_keys.append((F'backbone.norm{i}.bias', F'backbone.hidden_states_norms.stage{i+1}.bias')				)

      # decode head
      rename_keys.extend(
          [
              ("decode_head.conv_seg.weight", "decode_head.classifier.weight"),
              ("decode_head.conv_seg.bias", "decode_head.classifier.bias"),
              ("auxiliary_head.conv_seg.weight", "auxiliary_head.classifier.weight"),
              ("auxiliary_head.conv_seg.bias", "auxiliary_head.classifier.bias"),
          ]				)
      # fmt: on

      return rename_keys







def 		A_							(						_lowerCAmelCase :     Dict					,						_lowerCAmelCase :     Dict					,						_lowerCAmelCase :     Dict				):



      """simple docstring"""




      _lowerCamelCase					: int		    = dct.pop(lowerCAmelCase__				)
      _lowerCamelCase					: Any		    = val







def 		A_							(						_lowerCAmelCase :     Dict					,						_lowerCAmelCase :     Any				):



      """simple docstring"""




      _lowerCamelCase					: str		    = [int(backbone_config.embed_dim * 2**i				) for i in range(len(backbone_config.depths				)				)]
      for i in range(len(backbone_config.depths				)				):
            _lowerCamelCase					: int		    = num_features[i]
            for j in range(backbone_config.depths[i]				):
                  # fmt: off
                  # read in weights + bias of input projection layer (in original implementation, this is a single matrix + bias)
                  _lowerCamelCase					: Any		    = state_dict.pop(F'backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.weight'				)
                  _lowerCamelCase					: Dict		    = state_dict.pop(F'backbone.stages.{i}.blocks.{j}.attn.w_msa.qkv.bias'				)
                  # next, add query, keys and values (in that order) to the state dict
                  _lowerCamelCase					: Dict		    = in_proj_weight[:dim, :]
                  _lowerCamelCase					: List[str]		    = in_proj_bias[: dim]
                  _lowerCamelCase					: Optional[int]		    = in_proj_weight[
                      dim : dim * 2, :
                  ]
                  _lowerCamelCase					: List[str]		    = in_proj_bias[
                      dim : dim * 2
                  ]
                  _lowerCamelCase					: List[Any]		    = in_proj_weight[
                      -dim :, :
                  ]
                  _lowerCamelCase					: List[str]		    = in_proj_bias[-dim :]
                  # fmt: on







def 		A_							(						_lowerCAmelCase :     Dict				):



      """simple docstring"""




      _lowerCamelCase					: List[str]		    = x.shape
      _lowerCamelCase					: Tuple		    = x.reshape(lowerCAmelCase__					,						4					,						in_channel // 4				)
      _lowerCamelCase					: List[str]		    = x[:, [0, 2, 1, 3], :].transpose(1					,						2				).reshape(lowerCAmelCase__					,						lowerCAmelCase__				)
      return x







def 		A_							(						_lowerCAmelCase :     Any				):



      """simple docstring"""




      _lowerCamelCase					: int		    = x.shape
      _lowerCamelCase					: str		    = x.reshape(lowerCAmelCase__					,						in_channel // 4					,						4				)
      _lowerCamelCase					: Union[str, Any]		    = x[:, :, [0, 2, 1, 3]].transpose(1					,						2				).reshape(lowerCAmelCase__					,						lowerCAmelCase__				)

      return x







def 		A_							(						_lowerCAmelCase :     int				):



      """simple docstring"""




      _lowerCamelCase					: Tuple		    = x.shape[0]
      _lowerCamelCase					: int		    = x.reshape(4					,						in_channel // 4				)
      _lowerCamelCase					: List[Any]		    = x[[0, 2, 1, 3], :].transpose(0					,						1				).reshape(lowerCAmelCase__				)
      return x







def 		A_							(						_lowerCAmelCase :     Union[str, Any]				):



      """simple docstring"""




      _lowerCamelCase					: List[Any]		    = x.shape[0]
      _lowerCamelCase					: List[str]		    = x.reshape(in_channel // 4					,						4				)
      _lowerCamelCase					: Dict		    = x[:, [0, 2, 1, 3]].transpose(0					,						1				).reshape(lowerCAmelCase__				)
      return x







def 		A_							(						_lowerCAmelCase :     Optional[Any]					,						_lowerCAmelCase :     List[Any]					,						_lowerCAmelCase :     List[Any]				):



      """simple docstring"""




      _lowerCamelCase					: Dict		    = {
          "upernet-swin-tiny": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_tiny_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210531_112542-e380ad3e.pth",
          "upernet-swin-small": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K/upernet_swin_small_patch4_window7_512x512_160k_ade20k_pretrain_224x224_1K_20210526_192015-ee2fff1c.pth",
          "upernet-swin-base": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K/upernet_swin_base_patch4_window12_512x512_160k_ade20k_pretrain_384x384_22K_20210531_125459-429057bf.pth",
          "upernet-swin-large": "https://download.openmmlab.com/mmsegmentation/v0.5/swin/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k/upernet_swin_large_patch4_window12_512x512_pretrain_384x384_22K_160k_ade20k_20220318_091743-9ba68901.pth",
      }
      _lowerCamelCase					: Any		    = model_name_to_url[model_name]
      _lowerCamelCase					: Optional[Any]		    = torch.hub.load_state_dict_from_url(lowerCAmelCase__					,						map_location="cpu"					,						file_name=lowerCAmelCase__				)[
          "state_dict"
      ]

      for name, param in state_dict.items():
            print(lowerCAmelCase__					,						param.shape				)

      _lowerCamelCase					: List[str]		    = get_upernet_config(lowerCAmelCase__				)
      _lowerCamelCase					: str		    = UperNetForSemanticSegmentation(lowerCAmelCase__				)
      model.eval()

      # replace "bn" => "batch_norm"
      for key in state_dict.copy().keys():
            _lowerCamelCase					: Optional[Any]		    = state_dict.pop(lowerCAmelCase__				)
            if "bn" in key:
                  _lowerCamelCase					: Optional[Any]		    = key.replace("bn"					,						"batch_norm"				)
            _lowerCamelCase					: Optional[int]		    = val

      # rename keys
      _lowerCamelCase					: List[Any]		    = create_rename_keys(lowerCAmelCase__				)
      for src, dest in rename_keys:
            rename_key(lowerCAmelCase__					,						lowerCAmelCase__					,						lowerCAmelCase__				)
      read_in_q_k_v(lowerCAmelCase__					,						config.backbone_config				)

      # fix downsample parameters
      for key, value in state_dict.items():
            if "downsample" in key:
                  if "reduction" in key:
                        _lowerCamelCase					: int		    = reverse_correct_unfold_reduction_order(lowerCAmelCase__				)
                  if "norm" in key:
                        _lowerCamelCase					: List[str]		    = reverse_correct_unfold_norm_order(lowerCAmelCase__				)

      model.load_state_dict(lowerCAmelCase__				)

      # verify on image
      _lowerCamelCase					: Optional[Any]		    = "https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg"
      _lowerCamelCase					: Tuple		    = Image.open(requests.get(lowerCAmelCase__					,						stream=lowerCAmelCase__				).raw				).convert("RGB"				)

      _lowerCamelCase					: Dict		    = SegformerImageProcessor()
      _lowerCamelCase					: int		    = processor(lowerCAmelCase__					,						return_tensors="pt"				).pixel_values

      with torch.no_grad():
            _lowerCamelCase					: Optional[int]		    = model(lowerCAmelCase__				)
            _lowerCamelCase					: Any		    = outputs.logits

      print(logits.shape				)
      print("First values of logits:"					,						logits[0, 0, :3, :3]				)
      # assert values
      if model_name == "upernet-swin-tiny":
            _lowerCamelCase					: int		    = torch.tensor(
                [[-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.5_9_5_8, -7.5_9_5_8, -7.4_3_0_2], [-7.4_7_9_7, -7.4_7_9_7, -7.3_0_6_8]]				)
      elif model_name == "upernet-swin-small":
            _lowerCamelCase					: str		    = torch.tensor(
                [[-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.1_9_2_1, -7.1_9_2_1, -6.9_5_3_2], [-7.0_9_0_8, -7.0_9_0_8, -6.8_5_3_4]]				)
      elif model_name == "upernet-swin-base":
            _lowerCamelCase					: Optional[Any]		    = torch.tensor(
                [[-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.5_8_5_1, -6.5_8_5_1, -6.4_3_3_0], [-6.4_7_6_3, -6.4_7_6_3, -6.3_2_5_4]]				)
      elif model_name == "upernet-swin-large":
            _lowerCamelCase					: str		    = torch.tensor(
                [[-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.5_2_9_7, -7.5_2_9_7, -7.3_8_0_2], [-7.4_0_4_4, -7.4_0_4_4, -7.2_5_8_6]]				)
      print("Logits:"					,						outputs.logits[0, 0, :3, :3]				)
      assert torch.allclose(outputs.logits[0, 0, :3, :3]					,						lowerCAmelCase__					,						atol=1E-4				)
      print("Looks ok!"				)

      if pytorch_dump_folder_path is not None:
            print(F'Saving model {model_name} to {pytorch_dump_folder_path}'				)
            model.save_pretrained(lowerCAmelCase__				)
            print(F'Saving processor to {pytorch_dump_folder_path}'				)
            processor.save_pretrained(lowerCAmelCase__				)

      if push_to_hub:
            print(F'Pushing model and processor for {model_name} to hub'				)
            model.push_to_hub(F'openmmlab/{model_name}'				)
            processor.push_to_hub(F'openmmlab/{model_name}'				)


if __name__ == "__main__":
 UpperCAmelCase_							: int =							argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     '--model_name',
     default='upernet-swin-tiny',
     type=str,
     choices=[f'''upernet-swin-{size}''' for size in ['tiny', 'small', 'base', 'large']],
     help='Name of the Swin + UperNet model you\'d like to convert.',
 )
 parser.add_argument(
     '--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model directory.'
 )
 parser.add_argument(
     '--push_to_hub', action='store_true', help='Whether or not to push the converted model to the 🤗 hub.'
 )

 UpperCAmelCase_							: List[str] =							parser.parse_args()
 convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)






"""simple docstring"""






from typing import List, Union

import numpy as np

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, logging
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, ChunkPipeline


__UpperCAmelCase						=			logging.get_logger(__name__)



class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def       UpperCAmelCase      (		self    :			int	, a_    :			List[Any]    )	->	Optional[Any]:



							'''simple docstring'''



							if isinstance(a_	, a_    ):
										a__  :	Any		 =			[label.strip() for label in labels.split(","    ) if label.strip()]
							return labels






				def __call__(		self    :			Union[str, Any]	, a_    :			Tuple	, a_    :			Optional[Any]	, a_    :			List[str]    )	->	Optional[Any]:



							'''simple docstring'''



							if len(a_    ) == 0 or len(a_    ) == 0:
										raise ValueError("You must include at least one label and at least one sequence."    )
							if hypothesis_template.format(labels[0]    ) == hypothesis_template:
										raise ValueError(
										    (
										        "The provided hypothesis_template \"{}\" was not able to be formatted with the target labels. "
										        "Make sure the passed template includes formatting syntax such as {{}} where the label should go."
										    ).format(a_    )    )

							if isinstance(a_	, a_    ):
										a__  :	str		 =			[sequences]

							a__  :	Optional[int]		 =			[]
							for sequence in sequences:
										sequence_pairs.extend([[sequence, hypothesis_template.format(a_    )] for label in labels]    )

							return sequence_pairs, sequences



@add_end_docstrings(_UpperCamelCase  )
class 					__UpperCAmelCase							(						_UpperCamelCase  ):
				def __init__(		self    :			str	, a_    :			Optional[Any]=ZeroShotClassificationArgumentHandler()	, *a_    :			Tuple	, **a_    :			str    )	->	Optional[int]:



							'''simple docstring'''



							a__  :	List[Any]		 =			args_parser
							super().__init__(*a_	, **a_    )
							if self.entailment_id == -1:
										logger.warning(
										    "Failed to determine 'entailment' label id from the label2id mapping in the model config. Setting to "
										    "-1. Define a descriptive label2id mapping in the model config to ensure correct outputs."    )
				@property
				def       UpperCAmelCase      (		self    :			Tuple    )	->	str:



							'''simple docstring'''



							for label, ind in self.model.config.labelaid.items():
										if label.lower().startswith("entail"    ):
													return ind
							return -1
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			List[Any]	, a_    :			int=True	, a_    :			Tuple=True	, a_    :			Tuple=TruncationStrategy.ONLY_FIRST	, **a_    :			Dict    )	->	List[Any]:



							'''simple docstring'''



							a__  :	str		 =			self.framework
							if self.tokenizer.pad_token is None:
										# Override for tokenizers not supporting padding
										logger.error(
										    "Tokenizer was not supporting padding necessary for zero-shot, attempting to use "
										    " `pad_token=eos_token`"    )
										a__  :	List[str]		 =			self.tokenizer.eos_token
							try:
										a__  :	List[str]		 =			self.tokenizer(
										    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=a_	, )
							except Exception as e:
										if "too short" in str(a_    ):
													# tokenizers might yell that we want to truncate
													# to a value that is not even reached by the input.
													# In that case we don't want to truncate.
													# It seems there's not a really better way to catch that
													# exception.

													a__  :	List[str]		 =			self.tokenizer(
													    a_	, add_special_tokens=a_	, return_tensors=a_	, padding=a_	, truncation=TruncationStrategy.DO_NOT_TRUNCATE	, )
										else:
													raise e

							return inputs
				def       UpperCAmelCase      (		self    :			Tuple	, **a_    :			Tuple    )	->	Optional[int]:



							'''simple docstring'''



							if kwargs.get("multi_class"	, a_    ) is not None:
										a__  :	str		 =			kwargs["multi_class"]
										logger.warning(
										    "The `multi_class` argument has been deprecated and renamed to `multi_label`. "
										    "`multi_class` will be removed in a future version of Transformers."    )
							a__  :	Tuple		 =			{}
							if "candidate_labels" in kwargs:
										a__  :	Any		 =			self._args_parser._parse_labels(kwargs["candidate_labels"]    )
							if "hypothesis_template" in kwargs:
										a__  :	str		 =			kwargs["hypothesis_template"]

							a__  :	Tuple		 =			{}
							if "multi_label" in kwargs:
										a__  :	Dict		 =			kwargs["multi_label"]
							return preprocess_params, {}, postprocess_params
				def __call__(		self    :			str	, a_    :			Union[str, List[str]]	, *a_    :			List[str]	, **a_    :			List[Any]	, )	->	Tuple:



							'''simple docstring'''



							if len(a_    ) == 0:
										pass
							elif len(a_    ) == 1 and "candidate_labels" not in kwargs:
										a__  :	Any		 =			args[0]
							else:
										raise ValueError(F"Unable to understand extra arguments {args}"    )

							return super().__call__(a_	, **a_    )
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Tuple	, a_    :			Any=None	, a_    :			Dict="This example is {}."    )	->	Optional[int]:



							'''simple docstring'''



							a__ ,	a__  :	Optional[Any]		 =			self._args_parser(a_	, a_	, a_    )

							for i, (candidate_label, sequence_pair) in enumerate(zip(a_	, a_    )    ):
										a__  :	Union[str, Any]		 =			self._parse_and_tokenize([sequence_pair]    )

										yield {
										    "candidate_label": candidate_label,
										    "sequence": sequences[0],
										    "is_last": i == len(a_    ) - 1,
										    **model_input,
										}
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			Optional[Any]    )	->	List[Any]:



							'''simple docstring'''



							a__  :	Dict		 =			inputs["candidate_label"]
							a__  :	Optional[int]		 =			inputs["sequence"]
							a__  :	Optional[int]		 =			{k: inputs[k] for k in self.tokenizer.model_input_names}
							a__  :	int		 =			self.model(**a_    )

							a__  :	Optional[int]		 =			{
							    "candidate_label": candidate_label,
							    "sequence": sequence,
							    "is_last": inputs["is_last"],
							    **outputs,
							}
							return model_outputs






				def       UpperCAmelCase      (		self    :			Dict	, a_    :			Any	, a_    :			List[str]=False    )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	int		 =			[outputs["candidate_label"] for outputs in model_outputs]
							a__  :	Optional[int]		 =			[outputs["sequence"] for outputs in model_outputs]
							a__  :	Union[str, Any]		 =			np.concatenate([output["logits"].numpy() for output in model_outputs]    )
							a__  :	List[str]		 =			logits.shape[0]
							a__  :	Optional[int]		 =			len(a_    )
							a__  :	List[str]		 =			N // n
							a__  :	int		 =			logits.reshape((num_sequences, n, -1)    )

							if multi_label or len(a_    ) == 1:
										# softmax over the entailment vs. contradiction dim for each label independently
										a__  :	str		 =			self.entailment_id
										a__  :	str		 =			-1 if entailment_id == 0 else 0
										a__  :	str		 =			reshaped_outputs[..., [contradiction_id, entailment_id]]
										a__  :	List[Any]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )
										a__  :	str		 =			scores[..., 1]
							else:
										# softmax the "entailment" logits over all candidate labels
										a__  :	str		 =			reshaped_outputs[..., self.entailment_id]
										a__  :	Optional[int]		 =			np.exp(a_    ) / np.exp(a_    ).sum(-1	, keepdims=a_    )

							a__  :	List[str]		 =			list(reversed(scores[0].argsort()    )    )
							return {
							    "sequence": sequences[0],
							    "labels": [candidate_labels[i] for i in top_inds],
							    "scores": scores[0, top_inds].tolist(),
							}