




'''simple docstring'''

from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream



class 		SCREAMING_SNAKE_CASE__  (			_UpperCamelCase	):





			def __init__(      self						:    List[str]      ,		a_						:    Callable      ,		a_						:    Optional[Features] = None      ,		a_						:    str = None      ,		a_						:    bool = False      ,		a_						:    bool = False      ,		a_						:    Optional[dict] = None      ,		a_						:    Optional[int] = None      ,		**a_						:    str      ,		):



						"""simple docstring"""




						super().__init__(
						    features=a_      ,		cache_dir=a_      ,		keep_in_memory=a_      ,		streaming=a_      ,		num_proc=a_      ,		**a_      ,		)
						__snake_case			=		Generator(
						    cache_dir=a_      ,		features=a_      ,		generator=a_      ,		gen_kwargs=a_      ,		**a_      ,		)






			def 							A (      self						:    Dict							):



						"""simple docstring"""




						if self.streaming:
									__snake_case			=		self.builder.as_streaming_dataset(split="train"							)
						# Build regular (map-style) dataset
						else:
									__snake_case			=		None
									__snake_case			=		None
									__snake_case			=		None
									__snake_case			=		None

									self.builder.download_and_prepare(
									    download_config=a_      ,		download_mode=a_      ,		verification_mode=a_      ,		base_path=a_      ,		num_proc=self.num_proc      ,		)
									__snake_case			=		self.builder.as_dataset(
									    split="train"      ,		verification_mode=a_      ,		in_memory=self.keep_in_memory							)
						return dataset








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        A__(a_							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> List[str]:
									a_						:						List[Any] 							= SMALL_MODEL_IDENTIFIER
									a_						:						Optional[int] 							= """pt"""
									a_						:						Union[str, Any] 							= """tf"""





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> str:
									a_						:						Union[str, Any] 							= AutoModel.from_pretrained(self.test_model    )
									model_pt.save_pretrained(_lowercase    )





						def 			UpperCamelCase__					(    self  ,      _lowercase    )     -> List[str]:
									a_						:						int 							= TFAutoModel.from_pretrained(self.test_model  ,      from_pt=_lowercase    )
									model_tf.save_pretrained(_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> Optional[Any]:
									a_						:						str 							= """mock_framework"""

									# Framework provided - return whatever the user provides
									a_						:						Optional[int] 							= FeaturesManager.determine_framework(self.test_model  ,      _lowercase    )
									self.assertEqual(_lowercase  ,      _lowercase    )

									# Local checkpoint and framework provided - return provided framework
									# PyTorch checkpoint
									with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(_lowercase    )
												a_						:						Optional[int] 							= FeaturesManager.determine_framework(_lowercase  ,      _lowercase    )
												self.assertEqual(_lowercase  ,      _lowercase    )

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





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									# PyTorch checkpoint
									with TemporaryDirectory() as local_pt_ckpt:
												self._setup_pt_ckpt(_lowercase    )
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# TensorFlow checkpoint
									with TemporaryDirectory() as local_tf_ckpt:
												self._setup_tf_ckpt(_lowercase    )
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )
												self.assertEqual(_lowercase  ,      self.framework_tf    )

									# Invalid local checkpoint
									with TemporaryDirectory() as local_invalid_ckpt:
												with self.assertRaises(_lowercase    ):
															a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(_lowercase    )





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						int 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ):
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# PyTorch not in environment -> use TensorFlow
									a_						:						Dict 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												a_						:						Union[str, Any] 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_tf    )

									# Both in environment -> use PyTorch
									a_						:						Optional[Any] 							= MagicMock(return_value=_lowercase    )
									a_						:						List[Any] 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ), patch(
									    """transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												a_						:						Any 							= FeaturesManager.determine_framework(self.test_model    )
												self.assertEqual(_lowercase  ,      self.framework_pt    )

									# Both not in environment -> raise error
									a_						:						List[str] 							= MagicMock(return_value=_lowercase    )
									a_						:						Optional[Any] 							= MagicMock(return_value=_lowercase    )
									with patch("""transformers.onnx.features.is_tf_available"""  ,      _lowercase    ), patch(
									    """transformers.onnx.features.is_torch_available"""  ,      _lowercase    ):
												with self.assertRaises(_lowercase    ):
															a_						:						Dict 							= FeaturesManager.determine_framework(self.test_model    )


