



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


UpperCAmelCase_									=							logging.get_logger(__name__)

UpperCAmelCase_									=							{
    "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 		UpperCAmelCase						(						snake_case_				):
   SCREAMING_SNAKE_CASE__							=     '''bloom'''
   SCREAMING_SNAKE_CASE__							=     ['''past_key_values''']
   SCREAMING_SNAKE_CASE__							=     {
       '''num_hidden_layers''': '''n_layer''',
       '''num_attention_heads''': '''n_head''',
   }





   def __init__(	self       ,     _lowerCAmelCase=250_880       ,     _lowerCAmelCase=64       ,     _lowerCAmelCase=2       ,     _lowerCAmelCase=8       ,     _lowerCAmelCase=1E-5       ,     _lowerCAmelCase=0.02       ,     _lowerCAmelCase=True       ,     _lowerCAmelCase=1       ,     _lowerCAmelCase=2       ,     _lowerCAmelCase=False       ,     _lowerCAmelCase=0.0       ,     _lowerCAmelCase=0.0       ,     _lowerCAmelCase=1       ,     _lowerCAmelCase=False       ,     **_lowerCAmelCase       ,     ):
         _lowerCAmelCase 			=  vocab_size
         # Backward compatibility with n_embed kwarg
         _lowerCAmelCase 			=  kwargs.pop('''n_embed'''       ,     _lowerCAmelCase			)
         _lowerCAmelCase 			=  hidden_size if n_embed is None else n_embed
         _lowerCAmelCase 			=  n_layer
         _lowerCAmelCase 			=  n_head
         _lowerCAmelCase 			=  layer_norm_epsilon
         _lowerCAmelCase 			=  initializer_range
         _lowerCAmelCase 			=  use_cache
         _lowerCAmelCase 			=  pretraining_tp
         _lowerCAmelCase 			=  apply_residual_connection_post_layernorm
         _lowerCAmelCase 			=  hidden_dropout
         _lowerCAmelCase 			=  attention_dropout

         _lowerCAmelCase 			=  bos_token_id
         _lowerCAmelCase 			=  eos_token_id
         _lowerCAmelCase 			=  slow_but_exact

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






class 		UpperCAmelCase						(						snake_case_				):
   SCREAMING_SNAKE_CASE__							=     version.parse('''1.12'''				)





   def __init__(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase = "default"       ,     _lowerCAmelCase = None       ,     _lowerCAmelCase = False       ,     ):
         super().__init__(_lowerCAmelCase       ,     task=_lowerCAmelCase       ,     patching_specs=_lowerCAmelCase       ,     use_past=_lowerCAmelCase			)
         if not getattr(self._config       ,     '''pad_token_id'''       ,     _lowerCAmelCase			):
               # TODO: how to do that better?
               _lowerCAmelCase 			=  0





   @property
   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  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_(_lowerCAmelCase       ,     direction='''inputs'''       ,     inverted_values_shape=_lowerCAmelCase			)
               _lowerCAmelCase 			=  {0: '''batch''', 1: '''past_sequence + sequence'''}
         else:
               _lowerCAmelCase 			=  {0: '''batch''', 1: '''sequence'''}

         return common_inputs





   @property
   def 	__lowerCAmelCase							(	self			):
         return self._config.n_layer





   @property
   def 	__lowerCAmelCase							(	self			):
         return self._config.n_head





   @property
   def 	__lowerCAmelCase							(	self			):
         return 1E-3





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase       ,     _lowerCAmelCase = -1       ,     _lowerCAmelCase = -1       ,     _lowerCAmelCase = False       ,     _lowerCAmelCase = None       ,     ):
         _lowerCAmelCase 			=  super(_lowerCAmelCase       ,     self			).generate_dummy_inputs(
             _lowerCAmelCase       ,     batch_size=_lowerCAmelCase       ,     seq_length=_lowerCAmelCase       ,     is_pair=_lowerCAmelCase       ,     framework=_lowerCAmelCase			)

         # We need to order the input in the way they appears in the forward()
         _lowerCAmelCase 			=  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

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

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

         return ordered_inputs





   @property
   def 	__lowerCAmelCase							(	self			):
         return 13




from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch





@require_torch
@require_tf
class 		UpperCAmelCase						(						snake_case_				):





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  SMALL_MODEL_IDENTIFIER
         _lowerCAmelCase 			=  '''pt'''
         _lowerCAmelCase 			=  '''tf'''





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  AutoModel.from_pretrained(self.test_model			)
         model_pt.save_pretrained(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self       ,     _lowerCAmelCase			):
         _lowerCAmelCase 			=  TFAutoModel.from_pretrained(self.test_model       ,     from_pt=_lowerCAmelCase			)
         model_tf.save_pretrained(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  '''mock_framework'''

         # Framework provided - return whatever the user provides
         _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model       ,     _lowerCAmelCase			)
         self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)

         # Local checkpoint and framework provided - return provided framework
         # PyTorch checkpoint
         with TemporaryDirectory() as local_pt_ckpt:
               self._setup_pt_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)

         # TensorFlow checkpoint
         with TemporaryDirectory() as local_tf_ckpt:
               self._setup_tf_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase       ,     _lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     _lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         # PyTorch checkpoint
         with TemporaryDirectory() as local_pt_ckpt:
               self._setup_pt_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # TensorFlow checkpoint
         with TemporaryDirectory() as local_tf_ckpt:
               self._setup_tf_ckpt(_lowerCAmelCase			)
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_tf			)

         # Invalid local checkpoint
         with TemporaryDirectory() as local_invalid_ckpt:
               with self.assertRaises(_lowerCAmelCase			):
                     _lowerCAmelCase 			=  FeaturesManager.determine_framework(_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # PyTorch not in environment -> use TensorFlow
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_tf			)

         # Both in environment -> use PyTorch
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			), patch(
             '''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)
               self.assertEqual(_lowerCAmelCase       ,     self.framework_pt			)

         # Both not in environment -> raise error
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         _lowerCAmelCase 			=  MagicMock(return_value=_lowerCAmelCase			)
         with patch('''transformers.onnx.features.is_tf_available'''       ,     _lowerCAmelCase			), patch(
             '''transformers.onnx.features.is_torch_available'''       ,     _lowerCAmelCase			):
               with self.assertRaises(_lowerCAmelCase			):
                     _lowerCAmelCase 			=  FeaturesManager.determine_framework(self.test_model			)