




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 				lowerCAmelCase_       (					lowerCamelCase_							):






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : List[str]					  =      SMALL_MODEL_IDENTIFIER
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      'pt'
							SCREAMING_SNAKE_CASE_   : int					  =      'tf'






				def 		snake_case	(			self      ,snake_case__ ):
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      AutoModel.from_pretrained(self.test_model )
							model_pt.save_pretrained(snake_case__ )






				def 		snake_case	(			self      ,snake_case__ ):
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      TFAutoModel.from_pretrained(self.test_model      ,from_pt=snake_case__ )
							model_tf.save_pretrained(snake_case__ )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      'mock_framework'

							# Framework provided - return whatever the user provides
							SCREAMING_SNAKE_CASE_   : List[str]					  =      FeaturesManager.determine_framework(self.test_model      ,snake_case__ )
							self.assertEqual(snake_case__      ,snake_case__ )

							# Local checkpoint and framework provided - return provided framework
							# PyTorch checkpoint
							with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(snake_case__ )
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      FeaturesManager.determine_framework(snake_case__      ,snake_case__ )
										self.assertEqual(snake_case__      ,snake_case__ )

							# TensorFlow checkpoint
							with TemporaryDirectory() as local_tf_ckpt:
										self._setup_tf_ckpt(snake_case__ )
										SCREAMING_SNAKE_CASE_   : List[str]					  =      FeaturesManager.determine_framework(snake_case__      ,snake_case__ )
										self.assertEqual(snake_case__      ,snake_case__ )






				def 		snake_case	(			self ):
							# PyTorch checkpoint
							with TemporaryDirectory() as local_pt_ckpt:
										self._setup_pt_ckpt(snake_case__ )
										SCREAMING_SNAKE_CASE_   : List[Any]					  =      FeaturesManager.determine_framework(snake_case__ )
										self.assertEqual(snake_case__      ,self.framework_pt )

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

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






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      MagicMock(return_value=snake_case__ )
							with patch('transformers.onnx.features.is_tf_available'      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Any					  =      FeaturesManager.determine_framework(self.test_model )
										self.assertEqual(snake_case__      ,self.framework_pt )

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

							# Both in environment -> use PyTorch
							SCREAMING_SNAKE_CASE_   : List[str]					  =      MagicMock(return_value=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      MagicMock(return_value=snake_case__ )
							with patch('transformers.onnx.features.is_tf_available'      ,snake_case__ ), patch(
							    'transformers.onnx.features.is_torch_available'      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Any					  =      FeaturesManager.determine_framework(self.test_model )
										self.assertEqual(snake_case__      ,self.framework_pt )

							# Both not in environment -> raise error
							SCREAMING_SNAKE_CASE_   : int					  =      MagicMock(return_value=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      MagicMock(return_value=snake_case__ )
							with patch('transformers.onnx.features.is_tf_available'      ,snake_case__ ), patch(
							    'transformers.onnx.features.is_torch_available'      ,snake_case__ ):
										with self.assertRaises(snake_case__ ):
													SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      FeaturesManager.determine_framework(self.test_model )









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


UpperCamelCase__ :   Optional[int]						=							{
    '''configuration_bloom''': ['''BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''BloomConfig''', '''BloomOnnxConfig'''],
}
try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						UpperCamelCase__ :   Tuple						=							['''BloomTokenizerFast''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						UpperCamelCase__ :   Any						=							[
						    '''BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''BloomForCausalLM''',
						    '''BloomModel''',
						    '''BloomPreTrainedModel''',
						    '''BloomForSequenceClassification''',
						    '''BloomForTokenClassification''',
						    '''BloomForQuestionAnswering''',
						]

if TYPE_CHECKING:
						from .configuration_bloom import BLOOM_PRETRAINED_CONFIG_ARCHIVE_MAP, BloomConfig, BloomOnnxConfig

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_bloom_fast import BloomTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_bloom import (
												    BLOOM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    BloomForCausalLM,
												    BloomForQuestionAnswering,
												    BloomForSequenceClassification,
												    BloomForTokenClassification,
												    BloomModel,
												    BloomPreTrainedModel,
												)

else:
						import sys

						UpperCamelCase__ :   Tuple						=							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



