


'''simple docstring'''

from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
 from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


lowerCamelCase							:   Optional[int]  						=	logging.get_logger(__name__)

lowerCamelCase							:   int  						=	{
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}
class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Any  					=							"""bloom"""
      lowerCAmelCase__					:     Optional[Any]  					=							["""past_key_values"""]
      lowerCAmelCase__					:     Optional[int]  					=							{
          """num_hidden_layers""": """n_layer""",
          """num_attention_heads""": """n_head""",
      }



      def __init__(self    :    Dict   ,						UpperCamelCase    :    int=250880   ,						UpperCamelCase    :    Any=64   ,						UpperCamelCase    :    List[Any]=2   ,						UpperCamelCase    :    Any=8   ,						UpperCamelCase    :    Any=1E-5   ,						UpperCamelCase    :    Any=0.02   ,						UpperCamelCase    :    str=True   ,						UpperCamelCase    :    Optional[int]=1   ,						UpperCamelCase    :    Optional[int]=2   ,						UpperCamelCase    :    Union[str, Any]=False   ,						UpperCamelCase    :    List[str]=0.0   ,						UpperCamelCase    :    Any=0.0   ,						UpperCamelCase    :    Optional[Any]=1   ,						UpperCamelCase    :    List[Any]=False   ,						**UpperCamelCase    :    Tuple   ,						):

             '''simple docstring'''







             lowercase__      			= vocab_size
             # Backward compatibility with n_embed kwarg
             lowercase__      			= kwargs.pop('''n_embed'''   ,						UpperCamelCase )
             lowercase__      			= hidden_size if n_embed is None else n_embed
             lowercase__      			= n_layer
             lowercase__      			= n_head
             lowercase__      			= layer_norm_epsilon
             lowercase__      			= initializer_range
             lowercase__      			= use_cache
             lowercase__      			= pretraining_tp
             lowercase__      			= apply_residual_connection_post_layernorm
             lowercase__      			= hidden_dropout
             lowercase__      			= attention_dropout

             lowercase__      			= bos_token_id
             lowercase__      			= eos_token_id
             lowercase__      			= slow_but_exact

             super().__init__(bos_token_id=UpperCamelCase   ,						eos_token_id=UpperCamelCase   ,						**UpperCamelCase )






class   __lowerCAmelCase						(lowercase_     ):





      '''simple docstring'''






      lowerCAmelCase__					:     Tuple  					=							version.parse("""1.12"""     )



      def __init__(self    :    Optional[Any]   ,						UpperCamelCase    :    PretrainedConfig   ,						UpperCamelCase    :    str = "default"   ,						UpperCamelCase    :    List[PatchingSpec] = None   ,						UpperCamelCase    :    bool = False   ,						):

             '''simple docstring'''







             super().__init__(UpperCamelCase   ,						task=UpperCamelCase   ,						patching_specs=UpperCamelCase   ,						use_past=UpperCamelCase )
             if not getattr(self._config   ,						'''pad_token_id'''   ,						UpperCamelCase ):
                    # TODO: how to do that better?
                    lowercase__      			= 0



      @property
      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







             lowercase__      			= OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}} )
             if self.use_past:
                    # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
                    self.fill_with_past_key_values_(UpperCamelCase   ,						direction='''inputs'''   ,						inverted_values_shape=UpperCamelCase )
                    lowercase__      			= {0: '''batch''', 1: '''past_sequence + sequence'''}
             else:
                    lowercase__      			= {0: '''batch''', 1: '''sequence'''}

             return common_inputs



      @property
      def       UpperCamelCase__		(self    :    Optional[Any] ):

             '''simple docstring'''







             return self._config.n_layer



      @property
      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             return self._config.n_head



      @property
      def       UpperCamelCase__		(self    :    List[Any] ):

             '''simple docstring'''







             return 1E-3



      def       UpperCamelCase__		(self    :    str   ,						UpperCamelCase    :    "PreTrainedTokenizer"   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    int = -1   ,						UpperCamelCase    :    bool = False   ,						UpperCamelCase    :    Optional["TensorType"] = None   ,						):

             '''simple docstring'''







             lowercase__      			= super(UpperCamelCase   ,						self ).generate_dummy_inputs(
                 UpperCamelCase   ,						batch_size=UpperCamelCase   ,						seq_length=UpperCamelCase   ,						is_pair=UpperCamelCase   ,						framework=UpperCamelCase )

             # We need to order the input in the way they appears in the forward()
             lowercase__      			= OrderedDict({'''input_ids''': common_inputs['''input_ids''']} )

             # Need to add the past_keys
             if self.use_past:
                    if not is_torch_available():
                           raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''' )
                    else:
                           import torch

                           lowercase__			,lowercase__      			= common_inputs['''input_ids'''].shape
                           # Not using the same length for past_key_values
                           lowercase__      			= seqlen + 2
                           lowercase__      			= self._config.hidden_size // self.num_attention_heads
                           lowercase__      			= (
                               batch * self.num_attention_heads,
                               head_dim,
                               past_key_values_length,
                           )
                           lowercase__      			= (
                               batch * self.num_attention_heads,
                               past_key_values_length,
                               head_dim,
                           )
                           lowercase__      			= [
                               (torch.zeros(UpperCamelCase ), torch.zeros(UpperCamelCase )) for _ in range(self.num_layers )
                           ]

             lowercase__      			= common_inputs['''attention_mask''']
             if self.use_past:
                    lowercase__      			= ordered_inputs['''attention_mask'''].dtype
                    lowercase__      			= torch.cat(
                        [ordered_inputs['''attention_mask'''], torch.ones(UpperCamelCase   ,						UpperCamelCase   ,						dtype=UpperCamelCase )]   ,						dim=1 )

             return ordered_inputs






      @property
      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             return 13








'''simple docstring'''

import unittest

from transformers import DonutProcessor


lowerCamelCase							:   Tuple  						=	'naver-clova-ix/donut-base'
class   __lowerCAmelCase						(unittest.TestCase     ):





      '''simple docstring'''






      def       UpperCamelCase__		(self    :    int ):

             '''simple docstring'''







             lowercase__      			= DonutProcessor.from_pretrained(UpperCamelCase )






      def       UpperCamelCase__		(self    :    Tuple ):

             '''simple docstring'''







             lowercase__      			= {
                 '''name''': '''John Doe''',
                 '''age''': '''99''',
                 '''city''': '''Atlanta''',
                 '''state''': '''GA''',
                 '''zip''': '''30301''',
                 '''phone''': '''123-4567''',
                 '''nicknames''': [{'''nickname''': '''Johnny'''}, {'''nickname''': '''JD'''}],
             }

             lowercase__      			= (
                 '''<s_name>John Doe</s_name><s_age>99</s_age><s_city>Atlanta</s_city>'''
                 '''<s_state>GA</s_state><s_zip>30301</s_zip><s_phone>123-4567</s_phone>'''
                 '''<s_nicknames><s_nickname>Johnny</s_nickname>'''
                 '''<sep/><s_nickname>JD</s_nickname></s_nicknames>'''
             )
             lowercase__      			= self.processor.tokenajson(UpperCamelCase )

             self.assertDictEqual(UpperCamelCase   ,						UpperCamelCase )




