





"""simple docstring"""






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_			=							logging.get_logger(__name__)

def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       Tuple[int, int]:






       """simple docstring"""




       def constraint_to_multiple_of(SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE=0,SCREAMING_SNAKE_CASE=None							):
              _UpperCAmelCase     =     round(val / multiple							) * multiple

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

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

              return x

       _UpperCAmelCase     =     (output_size, output_size) if isinstance(SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							) else output_size

       _UpperCAmelCase    ,							_UpperCAmelCase     =     get_image_size(SCREAMING_SNAKE_CASE							)
       _UpperCAmelCase    ,							_UpperCAmelCase     =     output_size

       # determine new height and width
       _UpperCAmelCase     =     output_height / input_height
       _UpperCAmelCase     =     output_width / input_width

       if keep_aspect_ratio:
              # scale as little as possible
              if abs(1 - scale_width							) < abs(1 - scale_height							):
                     # fit width
                     _UpperCAmelCase     =     scale_width
              else:
                     # fit height
                     _UpperCAmelCase     =     scale_height

       _UpperCAmelCase     =     constraint_to_multiple_of(scale_height * input_height,multiple=SCREAMING_SNAKE_CASE							)
       _UpperCAmelCase     =     constraint_to_multiple_of(scale_width * input_width,multiple=SCREAMING_SNAKE_CASE							)

       return (new_height, new_width)



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  ["""pixel_values"""]






       def __init__(   self    ,  a__ = True    ,  a__ = None    ,  a__ = PILImageResampling.BILINEAR    ,  a__ = False    ,  a__ = 1    ,  a__ = True    ,  a__ = 1 / 2_55    ,  a__ = True    ,  a__ = None    ,  a__ = None    ,  **a__    ,  ):
              super().__init__(**a__     )
              _UpperCAmelCase     =     size if size is not None else {'height': 3_84, 'width': 3_84}
              _UpperCAmelCase     =     get_size_dict(a__     )
              _UpperCAmelCase     =     do_resize
              _UpperCAmelCase     =     size
              _UpperCAmelCase     =     keep_aspect_ratio
              _UpperCAmelCase     =     ensure_multiple_of
              _UpperCAmelCase     =     resample
              _UpperCAmelCase     =     do_rescale
              _UpperCAmelCase     =     rescale_factor
              _UpperCAmelCase     =     do_normalize
              _UpperCAmelCase     =     image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
              _UpperCAmelCase     =     image_std if image_std is not None else IMAGENET_STANDARD_STD






       def 							__A (   self    ,  a__    ,  a__    ,  a__ = False    ,  a__ = 1    ,  a__ = PILImageResampling.BICUBIC    ,  a__ = None    ,  **a__    ,  ):
              _UpperCAmelCase     =     get_size_dict(a__     )
              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()}"""     )
              _UpperCAmelCase     =     get_resize_output_image_size(
                  a__    ,  output_size=(size['height'], size['width'])    ,  keep_aspect_ratio=a__    ,  multiple=a__    ,  )
              return resize(a__    ,  size=a__    ,  resample=a__    ,  data_format=a__    ,  **a__     )






       def 							__A (   self    ,  a__    ,  a__    ,  a__ = None    ,  **a__    ,  ):
              return rescale(a__    ,  scale=a__    ,  data_format=a__    ,  **a__     )






       def 							__A (   self    ,  a__    ,  a__    ,  a__    ,  a__ = None    ,  **a__    ,  ):
              return normalize(a__    ,  mean=a__    ,  std=a__    ,  data_format=a__    ,  **a__     )






       def 							__A (   self    ,  a__    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = None    ,  a__ = ChannelDimension.FIRST    ,  **a__    ,  ):
              _UpperCAmelCase     =     do_resize if do_resize is not None else self.do_resize
              _UpperCAmelCase     =     size if size is not None else self.size
              _UpperCAmelCase     =     get_size_dict(a__     )
              _UpperCAmelCase     =     keep_aspect_ratio if keep_aspect_ratio is not None else self.keep_aspect_ratio
              _UpperCAmelCase     =     ensure_multiple_of if ensure_multiple_of is not None else self.ensure_multiple_of
              _UpperCAmelCase     =     resample if resample is not None else self.resample
              _UpperCAmelCase     =     do_rescale if do_rescale is not None else self.do_rescale
              _UpperCAmelCase     =     rescale_factor if rescale_factor is not None else self.rescale_factor
              _UpperCAmelCase     =     do_normalize if do_normalize is not None else self.do_normalize
              _UpperCAmelCase     =     image_mean if image_mean is not None else self.image_mean
              _UpperCAmelCase     =     image_std if image_std is not None else self.image_std

              _UpperCAmelCase     =     make_list_of_images(a__     )

              if not valid_images(a__     ):
                     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.
              _UpperCAmelCase     =     [to_numpy_array(a__     ) for image in images]

              if do_resize:
                     _UpperCAmelCase     =     [self.resize(image=a__    ,  size=a__    ,  resample=a__     ) for image in images]

              if do_rescale:
                     _UpperCAmelCase     =     [self.rescale(image=a__    ,  scale=a__     ) for image in images]

              if do_normalize:
                     _UpperCAmelCase     =     [self.normalize(image=a__    ,  mean=a__    ,  std=a__     ) for image in images]

              _UpperCAmelCase     =     [to_channel_dimension_format(a__    ,  a__     ) for image in images]

              _UpperCAmelCase     =     {'pixel_values': images}
              return BatchFeature(data=a__    ,  tensor_type=a__     )






       def 							__A (   self    ,  a__    ,  a__ = None     ):
              _UpperCAmelCase     =     outputs.logits

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

                     if is_torch_tensor(a__     ):
                            _UpperCAmelCase     =     target_sizes.numpy()

                     _UpperCAmelCase     =     []

                     for idx in range(len(a__     )     ):
                            _UpperCAmelCase     =     torch.nn.functional.interpolate(
                                logits[idx].unsqueeze(dim=0     )    ,  size=target_sizes[idx]    ,  mode='bilinear'    ,  align_corners=a__     )
                            _UpperCAmelCase     =     resized_logits[0].argmax(dim=0     )
                            semantic_segmentation.append(a__     )
              else:
                     _UpperCAmelCase     =     logits.argmax(dim=1     )
                     _UpperCAmelCase     =     [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]     )]

              return semantic_segmentation













"""simple docstring"""






import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


lowerCAmelCase_			=							logging.get_logger(__name__)

lowerCAmelCase_			=							'''▁'''

lowerCAmelCase_			=							{'''vocab_file''': '''sentencepiece.bpe.model''', '''monolingual_vocab_file''': '''dict.txt'''}

lowerCAmelCase_			=							{
    '''vocab_file''': {
        '''vinai/bartpho-syllable''': '''https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model''',
    },
    '''monolingual_vocab_file''': {
        '''vinai/bartpho-syllable''': '''https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt''',
    },
}

lowerCAmelCase_			=							{'''vinai/bartpho-syllable''': 1_024}



class 	lowerCAmelCase					(      snake_case							):
       lowerCAmelCase__						 =  VOCAB_FILES_NAMES
       lowerCAmelCase__						 =  PRETRAINED_VOCAB_FILES_MAP
       lowerCAmelCase__						 =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       lowerCAmelCase__						 =  ["""input_ids""", """attention_mask"""]






       def __init__(   self    ,  a__    ,  a__    ,  a__="<s>"    ,  a__="</s>"    ,  a__="</s>"    ,  a__="<s>"    ,  a__="<unk>"    ,  a__="<pad>"    ,  a__="<mask>"    ,  a__ = None    ,  **a__    ,  ):
              # Mask token behave like a normal word, i.e. include the space before it
              _UpperCAmelCase     =     AddedToken(a__    ,  lstrip=a__    ,  rstrip=a__     ) if isinstance(a__    ,  a__     ) else mask_token

              _UpperCAmelCase     =     {} if sp_model_kwargs is None else sp_model_kwargs

              super().__init__(
                  bos_token=a__    ,  eos_token=a__    ,  unk_token=a__    ,  sep_token=a__    ,  cls_token=a__    ,  pad_token=a__    ,  mask_token=a__    ,  sp_model_kwargs=self.sp_model_kwargs    ,  **a__    ,  )

              _UpperCAmelCase     =     vocab_file
              _UpperCAmelCase     =     monolingual_vocab_file
              _UpperCAmelCase     =     spm.SentencePieceProcessor(**self.sp_model_kwargs     )
              self.sp_model.Load(str(a__     )     )

              # Load the reduced vocab

              # Keep order of special tokens for backward compatibility
              _UpperCAmelCase     =     {}
              _UpperCAmelCase     =     0
              for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]:
                     if str(a__     ) not in self.fairseq_tokens_to_ids:
                            _UpperCAmelCase     =     cnt
                            cnt += 1
              with open(a__    ,  'r'    ,  encoding='utf-8'     ) as f:
                     for line in f.readlines():
                            _UpperCAmelCase     =     line.strip().split()[0]
                            _UpperCAmelCase     =     len(self.fairseq_tokens_to_ids     )
              if str(a__     ) not in self.fairseq_tokens_to_ids:
                     _UpperCAmelCase     =     len(self.fairseq_tokens_to_ids     )

              _UpperCAmelCase     =     {v: k for k, v in self.fairseq_tokens_to_ids.items()}






       def __getstate__(   self     ):
              _UpperCAmelCase     =     self.__dict__.copy()
              _UpperCAmelCase     =     None
              _UpperCAmelCase     =     self.sp_model.serialized_model_proto()
              return state






       def __setstate__(   self    ,  a__     ):
              _UpperCAmelCase     =     d

              # for backward compatibility
              if not hasattr(self    ,  'sp_model_kwargs'     ):
                     _UpperCAmelCase     =     {}

              _UpperCAmelCase     =     spm.SentencePieceProcessor(**self.sp_model_kwargs     )
              self.sp_model.LoadFromSerializedProto(self.sp_model_proto     )






       def 							__A (   self    ,  a__    ,  a__ = None     ):

              if token_ids_a is None:
                     return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
              _UpperCAmelCase     =     [self.cls_token_id]
              _UpperCAmelCase     =     [self.sep_token_id]
              return cls + token_ids_a + sep + sep + token_ids_a + sep






       def 							__A (   self    ,  a__    ,  a__ = None    ,  a__ = False     ):

              if already_has_special_tokens:
                     return super().get_special_tokens_mask(
                         token_ids_a=a__    ,  token_ids_a=a__    ,  already_has_special_tokens=a__     )

              if token_ids_a is None:
                     return [1] + ([0] * len(a__     )) + [1]
              return [1] + ([0] * len(a__     )) + [1, 1] + ([0] * len(a__     )) + [1]






       def 							__A (   self    ,  a__    ,  a__ = None     ):
              _UpperCAmelCase     =     [self.sep_token_id]
              _UpperCAmelCase     =     [self.cls_token_id]

              if token_ids_a is None:
                     return len(cls + token_ids_a + sep     ) * [0]
              return len(cls + token_ids_a + sep + sep + token_ids_a + sep     ) * [0]






       @property
       def 							__A (   self     ):
              return len(self.fairseq_ids_to_tokens     )






       def 							__A (   self     ):
              _UpperCAmelCase     =     {self.convert_ids_to_tokens(a__     ): i for i in range(self.vocab_size     )}
              vocab.update(self.added_tokens_encoder     )
              return vocab






       def 							__A (   self    ,  a__     ):
              return self.sp_model.encode(a__    ,  out_type=a__     )






       def 							__A (   self    ,  a__     ):
              if token in self.fairseq_tokens_to_ids:
                     return self.fairseq_tokens_to_ids[token]
              else:
                     return self.unk_token_id






       def 							__A (   self    ,  a__     ):
              return self.fairseq_ids_to_tokens[index]






       def 							__A (   self    ,  a__     ):
              _UpperCAmelCase     =     ''.join(a__     ).replace(a__    ,  ' '     ).strip()
              return out_string






       def 							__A (   self    ,  a__    ,  a__ = None     ):
              if not os.path.isdir(a__     ):
                     logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""     )
                     return
              _UpperCAmelCase     =     os.path.join(
                  a__    ,  (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']     )
              _UpperCAmelCase     =     os.path.join(
                  a__    ,  (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['monolingual_vocab_file']    ,  )

              if os.path.abspath(self.vocab_file     ) != os.path.abspath(a__     ) and os.path.isfile(self.vocab_file     ):
                     copyfile(self.vocab_file    ,  a__     )
              elif not os.path.isfile(self.vocab_file     ):
                     with open(a__    ,  'wb'     ) as fi:
                            _UpperCAmelCase     =     self.sp_model.serialized_model_proto()
                            fi.write(a__     )

              if os.path.abspath(self.monolingual_vocab_file     ) != os.path.abspath(
                  a__     ) and os.path.isfile(self.monolingual_vocab_file     ):
                     copyfile(self.monolingual_vocab_file    ,  a__     )
              elif not os.path.isfile(self.monolingual_vocab_file     ):
                     with open(a__    ,  'w'    ,  encoding='utf-8'     ) as fp:
                            for token in self.fairseq_tokens_to_ids:
                                   if token not in self.all_special_tokens:
                                          fp.write(f"""{str(a__     )} \n"""     )

              return out_vocab_file, out_monolingual_vocab_file






