



"""simple docstring"""







import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					["""image_processor""", """tokenizer"""]
      _SCREAMING_SNAKE_CASE      =					"""LayoutLMv2ImageProcessor"""
      _SCREAMING_SNAKE_CASE      =					("""LayoutXLMTokenizer""", """LayoutXLMTokenizerFast""")



      def __init__(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				str=None   ,      SCREAMING_SNAKE_CASE_    :				Tuple=None   ,      **SCREAMING_SNAKE_CASE_    :				List[Any]	):
        if "feature_extractor" in kwargs:
          warnings.warn(
              'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
              ' instead.'   ,      SCREAMING_SNAKE_CASE_   ,      )
          lowerCAmelCase_       :    Dict								=			kwargs.pop('feature_extractor'	)

        lowerCAmelCase_       :    Any								=			image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
          raise ValueError('You need to specify an `image_processor`.'	)
        if tokenizer is None:
          raise ValueError('You need to specify a `tokenizer`.'	)

        super().__init__(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	)



      def __call__(				self    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				Any   ,      SCREAMING_SNAKE_CASE_    :				Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Union[List[List[int]], List[List[List[int]]]] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[List[int], List[List[int]]]] = None   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Union[bool, str, PaddingStrategy] = False   ,      SCREAMING_SNAKE_CASE_    :				Union[bool, str, TruncationStrategy] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = None   ,      SCREAMING_SNAKE_CASE_    :				int = 0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[bool] = None   ,      SCREAMING_SNAKE_CASE_    :				Optional[bool] = None   ,      SCREAMING_SNAKE_CASE_    :				bool = False   ,      SCREAMING_SNAKE_CASE_    :				bool = False   ,      SCREAMING_SNAKE_CASE_    :				bool = False   ,      SCREAMING_SNAKE_CASE_    :				bool = False   ,      SCREAMING_SNAKE_CASE_    :				bool = True   ,      SCREAMING_SNAKE_CASE_    :				Optional[Union[str, TensorType]] = None   ,      **SCREAMING_SNAKE_CASE_    :				Tuple   ,      ):
        # verify input
        if self.image_processor.apply_ocr and (boxes is not None):
          raise ValueError(
              'You cannot provide bounding boxes '
              'if you initialized the image processor with apply_ocr set to True.'	)

        if self.image_processor.apply_ocr and (word_labels is not None):
          raise ValueError(
              'You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'	)

        if return_overflowing_tokens is True and return_offsets_mapping is False:
          raise ValueError('You cannot return overflowing tokens without returning the offsets mapping.'	)

        # first, apply the image processor
        lowerCAmelCase_       :    Optional[Any]								=			self.image_processor(images=SCREAMING_SNAKE_CASE_   ,      return_tensors=SCREAMING_SNAKE_CASE_	)

        # second, apply the tokenizer
        if text is not None and self.image_processor.apply_ocr and text_pair is None:
          if isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	):
            lowerCAmelCase_       :    Dict								=			[text]  # add batch dimension (as the image processor always adds a batch dimension)
          lowerCAmelCase_       :    str								=			features['words']

        lowerCAmelCase_       :    str								=			self.tokenizer(
            text=text if text is not None else features['words']   ,      text_pair=text_pair if text_pair is not None else None   ,      boxes=boxes if boxes is not None else features['boxes']   ,      word_labels=SCREAMING_SNAKE_CASE_   ,      add_special_tokens=SCREAMING_SNAKE_CASE_   ,      padding=SCREAMING_SNAKE_CASE_   ,      truncation=SCREAMING_SNAKE_CASE_   ,      max_length=SCREAMING_SNAKE_CASE_   ,      stride=SCREAMING_SNAKE_CASE_   ,      pad_to_multiple_of=SCREAMING_SNAKE_CASE_   ,      return_token_type_ids=SCREAMING_SNAKE_CASE_   ,      return_attention_mask=SCREAMING_SNAKE_CASE_   ,      return_overflowing_tokens=SCREAMING_SNAKE_CASE_   ,      return_special_tokens_mask=SCREAMING_SNAKE_CASE_   ,      return_offsets_mapping=SCREAMING_SNAKE_CASE_   ,      return_length=SCREAMING_SNAKE_CASE_   ,      verbose=SCREAMING_SNAKE_CASE_   ,      return_tensors=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )

        # add pixel values
        lowerCAmelCase_       :    int								=			features.pop('pixel_values'	)
        if return_overflowing_tokens is True:
          lowerCAmelCase_       :    Tuple								=			self.get_overflowing_images(SCREAMING_SNAKE_CASE_   ,      encoded_inputs['overflow_to_sample_mapping']	)
        lowerCAmelCase_       :    Tuple								=			images

        return encoded_inputs



      def 					SCREAMING_SNAKE_CASE__			(				self    :				str   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]	):
        # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
        lowerCAmelCase_       :    List[Any]								=			[]
        for sample_idx in overflow_to_sample_mapping:
          images_with_overflow.append(images[sample_idx]	)

        if len(SCREAMING_SNAKE_CASE_	) != len(SCREAMING_SNAKE_CASE_	):
          raise ValueError(
              'Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'
              F" {len(SCREAMING_SNAKE_CASE_	)} and {len(SCREAMING_SNAKE_CASE_	)}"	)

        return images_with_overflow



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      *SCREAMING_SNAKE_CASE_    :				Dict   ,      **SCREAMING_SNAKE_CASE_    :				Tuple	):
        return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      *SCREAMING_SNAKE_CASE_    :				Tuple   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]	):
        return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_	)



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]	):
        return ["input_ids", "bbox", "attention_mask", "image"]



      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        warnings.warn(
            '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'   ,      SCREAMING_SNAKE_CASE_   ,      )
        return self.image_processor_class





      @property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        warnings.warn(
            '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'   ,      SCREAMING_SNAKE_CASE_   ,      )
        return self.image_processor










"""simple docstring"""







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__		:    List[Any]				   =     logging.get_logger(__name__)

lowercase__		:    List[str]				   =     {
    """s-JoL/Open-Llama-V1""": """https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json""",
}







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""open-llama"""



      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Tuple=1_0_0_0_0_0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=4_0_9_6   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=1_1_0_0_8   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=3_2   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=3_2   ,      SCREAMING_SNAKE_CASE_    :				str="silu"   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2_0_4_8   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=0.02   ,      SCREAMING_SNAKE_CASE_    :				int=1E-6   ,      SCREAMING_SNAKE_CASE_    :				List[str]=True   ,      SCREAMING_SNAKE_CASE_    :				Dict=0   ,      SCREAMING_SNAKE_CASE_    :				Dict=1   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2   ,      SCREAMING_SNAKE_CASE_    :				str=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=0.1   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=0.1   ,      SCREAMING_SNAKE_CASE_    :				Dict=True   ,      SCREAMING_SNAKE_CASE_    :				Any=True   ,      SCREAMING_SNAKE_CASE_    :				Tuple=None   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      ):
        lowerCAmelCase_       :    Union[str, Any]								=			vocab_size
        lowerCAmelCase_       :    int								=			max_position_embeddings
        lowerCAmelCase_       :    Union[str, Any]								=			hidden_size
        lowerCAmelCase_       :    Optional[Any]								=			intermediate_size
        lowerCAmelCase_       :    List[Any]								=			num_hidden_layers
        lowerCAmelCase_       :    Optional[int]								=			num_attention_heads
        lowerCAmelCase_       :    List[Any]								=			hidden_act
        lowerCAmelCase_       :    List[Any]								=			initializer_range
        lowerCAmelCase_       :    List[str]								=			rms_norm_eps
        lowerCAmelCase_       :    List[Any]								=			use_cache
        lowerCAmelCase_       :    Optional[int]								=			kwargs.pop(
            'use_memorry_efficient_attention'   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    List[Any]								=			hidden_dropout_prob
        lowerCAmelCase_       :    List[str]								=			attention_dropout_prob
        lowerCAmelCase_       :    Tuple								=			use_stable_embedding
        lowerCAmelCase_       :    Optional[Any]								=			shared_input_output_embedding
        lowerCAmelCase_       :    Optional[Any]								=			rope_scaling
        self._rope_scaling_validation()

        super().__init__(
            pad_token_id=SCREAMING_SNAKE_CASE_   ,      bos_token_id=SCREAMING_SNAKE_CASE_   ,      eos_token_id=SCREAMING_SNAKE_CASE_   ,      tie_word_embeddings=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )





      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        if self.rope_scaling is None:
          return

        if not isinstance(self.rope_scaling   ,      SCREAMING_SNAKE_CASE_	) or len(self.rope_scaling	) != 2:
          raise ValueError(
              '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '
              F"got {self.rope_scaling}"	)
        lowerCAmelCase_       :    int								=			self.rope_scaling.get('type'   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Union[str, Any]								=			self.rope_scaling.get('factor'   ,      SCREAMING_SNAKE_CASE_	)
        if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
          raise ValueError(
              F"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"	)
        if rope_scaling_factor is None or not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) or rope_scaling_factor <= 1.0:
          raise ValueError(F"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"	)





