




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


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

a_				:    Dict										=     {
    '''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 __lowercase(      lowercase__				):




					'''simple docstring'''


					__a						:						int     		= 'bloom'
					__a						:						Optional[Any]     		= ['past_key_values']
					__a						:						Union[str, Any]     		= {
					    'num_hidden_layers': 'n_layer',
					    'num_attention_heads': 'n_head',
					}
					def __init__(    self						,	__a=250880						,	__a=64						,	__a=2						,	__a=8						,	__a=1E-5						,	__a=0.02						,	__a=True						,	__a=1						,	__a=2						,	__a=False						,	__a=0.0						,	__a=0.0						,	__a=1						,	__a=False						,	**__a						,	):
								__lowerCamelCase					:	Union[str, Any]	       =				vocab_size
								# Backward compatibility with n_embed kwarg
								__lowerCamelCase					:	List[str]	       =				kwargs.pop('n_embed'						,	__a     )
								__lowerCamelCase					:	Optional[Any]	       =				hidden_size if n_embed is None else n_embed
								__lowerCamelCase					:	Any	       =				n_layer
								__lowerCamelCase					:	str	       =				n_head
								__lowerCamelCase					:	Tuple	       =				layer_norm_epsilon
								__lowerCamelCase					:	Optional[Any]	       =				initializer_range
								__lowerCamelCase					:	Any	       =				use_cache
								__lowerCamelCase					:	str	       =				pretraining_tp
								__lowerCamelCase					:	Optional[Any]	       =				apply_residual_connection_post_layernorm
								__lowerCamelCase					:	Optional[int]	       =				hidden_dropout
								__lowerCamelCase					:	Any	       =				attention_dropout

								__lowerCamelCase					:	str	       =				bos_token_id
								__lowerCamelCase					:	Tuple	       =				eos_token_id
								__lowerCamelCase					:	Optional[Any]	       =				slow_but_exact

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



class __lowercase(      lowercase__				):




					'''simple docstring'''


					__a						:						Optional[int]     		= version.parse('1.12'				)
					def __init__(    self						,	__a						,	__a = "default"						,	__a = None						,	__a = False						,	):
								super().__init__(__a						,	task=__a						,	patching_specs=__a						,	use_past=__a     )
								if not getattr(self._config						,	'pad_token_id'						,	__a     ):
											# TODO: how to do that better?
											__lowerCamelCase					:	List[str]	       =				0
					@property
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[str]	       =				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_(__a						,	direction='inputs'						,	inverted_values_shape=__a     )
											__lowerCamelCase					:	int	       =				{0: 'batch', 1: 'past_sequence + sequence'}
								else:
											__lowerCamelCase					:	int	       =				{0: 'batch', 1: 'sequence'}

								return common_inputs
					@property
					def     snake_case_				(    self     ):
								return self._config.n_layer
					@property
					def     snake_case_				(    self     ):
								return self._config.n_head
					@property
					def     snake_case_				(    self     ):
								return 1E-3
					def     snake_case_				(    self						,	__a						,	__a = -1						,	__a = -1						,	__a = False						,	__a = None						,	):
								__lowerCamelCase					:	Optional[Any]	       =				super(__a						,	self     ).generate_dummy_inputs(
								    __a						,	batch_size=__a						,	seq_length=__a						,	is_pair=__a						,	framework=__a     )

								# We need to order the input in the way they appears in the forward()
								__lowerCamelCase					:	int	       =				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					:	Tuple	       =				common_inputs['input_ids'].shape
														# Not using the same length for past_key_values
														__lowerCamelCase					:	Tuple	       =				seqlen + 2
														__lowerCamelCase					:	str	       =				self._config.hidden_size // self.num_attention_heads
														__lowerCamelCase					:	List[Any]	       =				(
														    batch * self.num_attention_heads,
														    head_dim,
														    past_key_values_length,
														)
														__lowerCamelCase					:	List[str]	       =				(
														    batch * self.num_attention_heads,
														    past_key_values_length,
														    head_dim,
														)
														__lowerCamelCase					:	int	       =				[
														    (torch.zeros(__a     ), torch.zeros(__a     )) for _ in range(self.num_layers     )
														]

								__lowerCamelCase					:	List[Any]	       =				common_inputs['attention_mask']
								if self.use_past:
											__lowerCamelCase					:	Optional[Any]	       =				ordered_inputs['attention_mask'].dtype
											__lowerCamelCase					:	List[str]	       =				torch.cat(
											    [ordered_inputs['attention_mask'], torch.ones(__a						,	__a						,	dtype=__a     )]						,	dim=1     )

								return ordered_inputs





					@property
					def     snake_case_				(    self     ):
								return 13









"""simple docstring"""
import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


a_				:    Tuple										=     '''hf-internal-testing/tiny-random-bert'''
a_				:    List[Any]										=     os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
a_				:    Dict										=     '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''

class __lowercase(      unittest.TestCase				):




					'''simple docstring'''


					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	__a     )
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(__a     )     )
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
											self.assertTrue(os.path.isdir(os.path.join(__a						,	__a     )     )     )
								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Optional[int]	       =				f.read()
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
								self.assertTrue(os.path.isfile(__a     )     )

								# File is cached at the same place the second time.
								__lowerCamelCase					:	Optional[Any]	       =				cached_file(__a						,	__a     )
								self.assertEqual(__a						,	__a     )

								# Using a specific revision to test the full commit hash.
								__lowerCamelCase					:	Optional[int]	       =				cached_file(__a						,	__a						,	revision='9b8c223'     )
								self.assertEqual(__a						,	os.path.join(__a						,	'snapshots'						,	__a						,	__a     )     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											__lowerCamelCase					:	List[str]	       =				cached_file('tiny-random-bert'						,	__a     )

								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											__lowerCamelCase					:	int	       =				cached_file(__a						,	__a						,	revision='aaaa'     )

								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	List[Any]	       =				cached_file(__a						,	'conf'     )
					def     snake_case_				(    self     ):
								with self.assertRaisesRegex(__a						,	'does not appear to have a file named'     ):
											__lowerCamelCase					:	Dict	       =				cached_file(__a						,	'conf'     )

								with open(os.path.join(__a						,	'refs'						,	'main'     )     ) as f:
											__lowerCamelCase					:	Any	       =				f.read()
								self.assertTrue(os.path.isfile(os.path.join(__a						,	'.no_exist'						,	__a						,	'conf'     )     )     )

								__lowerCamelCase					:	str	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	int	       =				cached_file(__a						,	'conf'						,	local_files_only=__a						,	_raise_exceptions_for_missing_entries=__a     )
								self.assertIsNone(__a     )

								__lowerCamelCase					:	List[str]	       =				mock.Mock()
								__lowerCamelCase					:	str	       =				500
								__lowerCamelCase					:	Union[str, Any]	       =				{}
								__lowerCamelCase					:	Dict	       =				HTTPError
								__lowerCamelCase					:	Any	       =				{}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch('requests.Session.request'						,	return_value=__a     ) as mock_head:
											__lowerCamelCase					:	Any	       =				cached_file(__a						,	'conf'						,	_raise_exceptions_for_connection_errors=__a     )
											self.assertIsNone(__a     )
											# This check we did call the fake head request
											mock_head.assert_called()
					def     snake_case_				(    self     ):
								self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
								self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'						,	__a     )     )
					def     snake_case_				(    self     ):
								# `get_file_from_repo` returns None if the file does not exist
								self.assertIsNone(get_file_from_repo('bert-base-cased'						,	'ahah.txt'     )     )

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid model identifier'     ):
											get_file_from_repo('bert-base-case'						,	__a     )

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(__a						,	'is not a valid git identifier'     ):
											get_file_from_repo('bert-base-cased'						,	__a						,	revision='ahaha'     )

								__lowerCamelCase					:	Optional[int]	       =				get_file_from_repo('bert-base-cased'						,	__a     )
								# The name is the cached name which is not very easy to test, so instead we load the content.
								__lowerCamelCase					:	int	       =				json.loads(open(__a						,	'r'     ).read()     )
								self.assertEqual(config['hidden_size']						,	768     )





					def     snake_case_				(    self     ):
								with tempfile.TemporaryDirectory() as tmp_dir:
											__lowerCamelCase					:	int	       =				Path(__a     ) / 'a.txt'
											filename.touch()
											self.assertEqual(get_file_from_repo(__a						,	'a.txt'     )						,	str(__a     )     )

											self.assertIsNone(get_file_from_repo(__a						,	'b.txt'     )     )



