
import math
from typing import Dict, Iterable, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    is_torch_available,
    is_torch_tensor,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_torch_available():
    import torch

if is_vision_available():
    import PIL


lowerCAmelCase			:							Any     =logging.get_logger(__name__)







def 						A__  (					__A   , __A   , __A   , __A	):







    '''simple docstring'''







    def constraint_to_multiple_of(__A   , __A   , __A=0   , __A=None	):
        _lowerCamelCase     :		List[str] 						=						round(val / multiple	) * multiple

        if max_val is not None and x > max_val:
            _lowerCamelCase     :		Tuple 						=						math.floor(val / multiple	) * multiple

        if x < min_val:
            _lowerCamelCase     :		Tuple 						=						math.ceil(val / multiple	) * multiple

        return x

    _lowerCamelCase     :		List[str] 						=						(output_size, output_size) if isinstance(__snake_case   , __snake_case	) else output_size

    _lowerCamelCase						,							_lowerCamelCase     :		Any 						=						get_image_size(__snake_case	)
    _lowerCamelCase						,							_lowerCamelCase     :		Tuple 						=						output_size

    # determine new height and width
    _lowerCamelCase     :		List[Any] 						=						output_height / input_height
    _lowerCamelCase     :		Optional[int] 						=						output_width / input_width

    if keep_aspect_ratio:
        # scale as little as possible
        if abs(1 - scale_width	) < abs(1 - scale_height	):
            # fit width
            _lowerCamelCase     :		List[str] 						=						scale_width
        else:
            # fit height
            _lowerCamelCase     :		str 						=						scale_height

    _lowerCamelCase     :		str 						=						constraint_to_multiple_of(scale_height * input_height   , multiple=__snake_case	)
    _lowerCamelCase     :		Union[str, Any] 						=						constraint_to_multiple_of(scale_width * input_width   , multiple=__snake_case	)

    return (new_height, new_width)




class 	__snake_case	( __lowerCAmelCase		):





      '''simple docstring'''






      _snake_case    		=       ['pixel_values']



      def __init__(					self     :				int    ,  _UpperCamelCase     :				Dict = True    ,  _UpperCamelCase     :				Dict = None    ,  _UpperCamelCase     :				int = PILImageResampling.BILINEAR    ,  _UpperCamelCase     :				Optional[int] = False    ,  _UpperCamelCase     :				Dict = 1    ,  _UpperCamelCase     :				Optional[Any] = True    ,  _UpperCamelCase     :				Union[str, Any] = 1 / 255    ,  _UpperCamelCase     :				List[Any] = True    ,  _UpperCamelCase     :				Optional[int] = None    ,  _UpperCamelCase     :				List[Any] = None    ,  **_UpperCamelCase     :				Union[str, Any]    ,  )				->Optional[Any]:


          """simple docstring"""


          super().__init__(**__lowerCAmelCase)
          _lowerCamelCase     :		Dict 						=						size if size is not None else {"""height""": 384, """width""": 384}
          _lowerCamelCase     :		int 						=						get_size_dict(__lowerCAmelCase)
          _lowerCamelCase     :		Union[str, Any] 						=						do_resize
          _lowerCamelCase     :		Tuple 						=						size
          _lowerCamelCase     :		Optional[Any] 						=						keep_aspect_ratio
          _lowerCamelCase     :		List[Any] 						=						ensure_multiple_of
          _lowerCamelCase     :		Tuple 						=						resample
          _lowerCamelCase     :		List[str] 						=						do_rescale
          _lowerCamelCase     :		str 						=						rescale_factor
          _lowerCamelCase     :		Dict 						=						do_normalize
          _lowerCamelCase     :		Dict 						=						image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
          _lowerCamelCase     :		Union[str, Any] 						=						image_std if image_std is not None else IMAGENET_STANDARD_STD



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				Tuple    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				List[Any] = False    ,  _UpperCamelCase     :				List[str] = 1    ,  _UpperCamelCase     :				str = PILImageResampling.BICUBIC    ,  _UpperCamelCase     :				Union[str, Any] = None    ,  **_UpperCamelCase     :				Optional[Any]    ,  )				->Dict:


          """simple docstring"""


          _lowerCamelCase     :		Dict 						=						get_size_dict(__lowerCAmelCase)
          if "height" not in size or "width" not in size:
              raise ValueError(F"""The size dictionary must contain the keys \'height\' and \'width\'. Got {size.keys()}""")
          _lowerCamelCase     :		Optional[Any] 						=						get_resize_output_image_size(
              __lowerCAmelCase    ,  output_size=(size["""height"""], size["""width"""])    ,  keep_aspect_ratio=__lowerCAmelCase    ,  multiple=__lowerCAmelCase    ,  )
          return resize(__lowerCAmelCase    ,  size=__lowerCAmelCase    ,  resample=__lowerCAmelCase    ,  data_format=__lowerCAmelCase    ,  **__lowerCAmelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple    ,  _UpperCamelCase     :				Union[str, Any]    ,  _UpperCamelCase     :				str    ,  _UpperCamelCase     :				Union[str, Any] = None    ,  **_UpperCamelCase     :				Dict    ,  )				->Any:


          """simple docstring"""


          return rescale(__lowerCAmelCase    ,  scale=__lowerCAmelCase    ,  data_format=__lowerCAmelCase    ,  **__lowerCAmelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				Union[str, Any]    ,  _UpperCamelCase     :				Any    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				int    ,  _UpperCamelCase     :				Dict = None    ,  **_UpperCamelCase     :				Dict    ,  )				->Tuple:


          """simple docstring"""


          return normalize(__lowerCAmelCase    ,  mean=__lowerCAmelCase    ,  std=__lowerCAmelCase    ,  data_format=__lowerCAmelCase    ,  **__lowerCAmelCase)



      def 	_SCREAMING_SNAKE_CASE    (					self     :				str    ,  _UpperCamelCase     :				Optional[int]    ,  _UpperCamelCase     :				str = None    ,  _UpperCamelCase     :				Optional[int] = None    ,  _UpperCamelCase     :				Optional[Any] = None    ,  _UpperCamelCase     :				List[str] = None    ,  _UpperCamelCase     :				Optional[Any] = None    ,  _UpperCamelCase     :				List[str] = None    ,  _UpperCamelCase     :				str = None    ,  _UpperCamelCase     :				str = None    ,  _UpperCamelCase     :				Optional[Any] = None    ,  _UpperCamelCase     :				Optional[Any] = None    ,  _UpperCamelCase     :				str = None    ,  _UpperCamelCase     :				str = ChannelDimension.FIRST    ,  **_UpperCamelCase     :				int    ,  )				->List[Any]:


          """simple docstring"""


          _lowerCamelCase     :		Union[str, Any] 						=						do_resize if do_resize is not None else self.do_resize
          _lowerCamelCase     :		int 						=						size if size is not None else self.size
          _lowerCamelCase     :		Tuple 						=						get_size_dict(__lowerCAmelCase)
          _lowerCamelCase     :		List[Any] 						=						keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
          _lowerCamelCase     :		int 						=						ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
          _lowerCamelCase     :		List[str] 						=						resample if resample is not None else self.resample
          _lowerCamelCase     :		Dict 						=						do_rescale if do_rescale is not None else self.do_rescale
          _lowerCamelCase     :		Union[str, Any] 						=						rescale_factor if rescale_factor is not None else self.rescale_factor
          _lowerCamelCase     :		Dict 						=						do_normalize if do_normalize is not None else self.do_normalize
          _lowerCamelCase     :		Tuple 						=						image_mean if image_mean is not None else self.image_mean
          _lowerCamelCase     :		Union[str, Any] 						=						image_std if image_std is not None else self.image_std

          _lowerCamelCase     :		Optional[Any] 						=						make_list_of_images(__lowerCAmelCase)

          if not valid_images(__lowerCAmelCase):
              raise ValueError(
                  """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
                  """torch.Tensor, tf.Tensor or jax.ndarray.""")

          if do_resize and size is None or resample is None:
              raise ValueError("""Size and resample must be specified if do_resize is True.""")

          if do_rescale and rescale_factor is None:
              raise ValueError("""Rescale factor must be specified if do_rescale is True.""")

          if do_normalize and (image_mean is None or image_std is None):
              raise ValueError("""Image mean and std must be specified if do_normalize is True.""")

          # All transformations expect numpy arrays.
          _lowerCamelCase     :		List[str] 						=						[to_numpy_array(__lowerCAmelCase) for image in images]

          if do_resize:
              _lowerCamelCase     :		Any 						=						[self.resize(image=__lowerCAmelCase    ,  size=__lowerCAmelCase    ,  resample=__lowerCAmelCase) for image in images]

          if do_rescale:
              _lowerCamelCase     :		str 						=						[self.rescale(image=__lowerCAmelCase    ,  scale=__lowerCAmelCase) for image in images]

          if do_normalize:
              _lowerCamelCase     :		Optional[Any] 						=						[self.normalize(image=__lowerCAmelCase    ,  mean=__lowerCAmelCase    ,  std=__lowerCAmelCase) for image in images]

          _lowerCamelCase     :		Dict 						=						[to_channel_dimension_format(__lowerCAmelCase    ,  __lowerCAmelCase) for image in images]

          _lowerCamelCase     :		Union[str, Any] 						=						{"""pixel_values""": images}
          return BatchFeature(data=__lowerCAmelCase    ,  tensor_type=__lowerCAmelCase)




      def 	_SCREAMING_SNAKE_CASE    (					self     :				Tuple    ,  _UpperCamelCase     :				Optional[Any]    ,  _UpperCamelCase     :				List[Any] = None)				->Optional[int]:


          """simple docstring"""


          _lowerCamelCase     :		Optional[Any] 						=						outputs.logits

          # Resize logits and compute semantic segmentation maps
          if target_sizes is not None:
              if len(__lowerCAmelCase) != len(__lowerCAmelCase):
                  raise ValueError(
                      """Make sure that you pass in as many target sizes as the batch dimension of the logits""")

              if is_torch_tensor(__lowerCAmelCase):
                  _lowerCamelCase     :		int 						=						target_sizes.numpy()

              _lowerCamelCase     :		Union[str, Any] 						=						[]

              for idx in range(len(__lowerCAmelCase)):
                  _lowerCamelCase     :		List[str] 						=						torch.nn.functional.interpolate(
                      logits[idx].unsqueeze(dim=0)    ,  size=target_sizes[idx]    ,  mode="""bilinear"""    ,  align_corners=__lowerCAmelCase)
                  _lowerCamelCase     :		List[str] 						=						resized_logits[0].argmax(dim=0)
                  semantic_segmentation.append(__lowerCAmelCase)
          else:
              _lowerCamelCase     :		Dict 						=						logits.argmax(dim=1)
              _lowerCamelCase     :		Tuple 						=						[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])]

          return semantic_segmentation





import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


lowerCAmelCase			:							int     ={
    "cola": 2,
    "mnli": 3,
    "mrpc": 2,
    "sst-2": 2,
    "sts-b": 1,
    "qqp": 2,
    "qnli": 2,
    "rte": 2,
    "wnli": 2,
}


logging.set_verbosity_info()







def 						A__  (					__A   , __A   , __A   , __A=None	):







    '''simple docstring'''
    # Initialise PyTorch model
    _lowerCamelCase     :		Tuple 						=						XLNetConfig.from_json_file(__A	)

    _lowerCamelCase     :		List[Any] 						=						finetuning_task.lower() if finetuning_task is not None else """"""
    if finetuning_task in GLUE_TASKS_NUM_LABELS:
        print(F"""Building PyTorch XLNetForSequenceClassification model from configuration: {config}"""	)
        _lowerCamelCase     :		int 						=						finetuning_task
        _lowerCamelCase     :		Union[str, Any] 						=						GLUE_TASKS_NUM_LABELS[finetuning_task]
        _lowerCamelCase     :		int 						=						XLNetForSequenceClassification(__A	)
    elif "squad" in finetuning_task:
        _lowerCamelCase     :		Dict 						=						finetuning_task
        _lowerCamelCase     :		Optional[Any] 						=						XLNetForQuestionAnswering(__A	)
    else:
        _lowerCamelCase     :		Any 						=						XLNetLMHeadModel(__A	)

    # Load weights from tf checkpoint
    load_tf_weights_in_xlnet(__A   , __A   , __A	)

    # Save pytorch-model
    _lowerCamelCase     :		Optional[Any] 						=						os.path.join(__A   , __A	)
    _lowerCamelCase     :		Any 						=						os.path.join(__A   , __A	)
    print(F"""Save PyTorch model to {os.path.abspath(__A	)}"""	)
    torch.save(model.state_dict()   , __A	)
    print(F"""Save configuration file to {os.path.abspath(__A	)}"""	)
    with open(__A   , """w"""   , encoding="""utf-8"""	) as f:
        f.write(config.to_json_string()	)


if __name__ == "__main__":
    lowerCAmelCase			:							Dict     =argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
    )
    parser.add_argument(
        "--xlnet_config_file",
        default=None,
        type=str,
        required=True,
        help=(
            "The config json file corresponding to the pre-trained XLNet model. \n"
            "This specifies the model architecture."
        ),
    )
    parser.add_argument(
        "--pytorch_dump_folder_path",
        default=None,
        type=str,
        required=True,
        help="Path to the folder to store the PyTorch model or dataset/vocab.",
    )
    parser.add_argument(
        "--finetuning_task",
        default=None,
        type=str,
        help="Name of a task on which the XLNet TensorFlow model was fine-tuned",
    )
    lowerCAmelCase			:							Union[str, Any]     =parser.parse_args()
    print(args)

    convert_xlnet_checkpoint_to_pytorch(
        args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
    )




