






"""simple docstring"""



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        lowercase__      (							snake_case__       ):


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							str									=SMALL_MODEL_IDENTIFIER
										lowerCamelCase_	:							List[str]									="pt"
										lowerCamelCase_	:							List[str]									="tf"


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  Union[str, Any] ):
										lowerCamelCase_	:							int									=AutoModel.from_pretrained(self.test_model )
										model_pt.save_pretrained(snake_case__ )


			def      UpperCAmelCase__						(				self					:  Optional[int]       ,	snake_case__					:  Optional[Any] ):
										lowerCamelCase_	:							Dict									=TFAutoModel.from_pretrained(self.test_model       ,	from_pt=snake_case__ )
										model_tf.save_pretrained(snake_case__ )


			def      UpperCAmelCase__						(				self					:  int ):
										lowerCamelCase_	:							List[Any]									="mock_framework"

										# Framework provided - return whatever the user provides
										lowerCamelCase_	:							int									=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__ )
																	lowerCamelCase_	:							str									=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__ )
																	lowerCamelCase_	:							str									=FeaturesManager.determine_framework(snake_case__       ,	snake_case__ )
																	self.assertEqual(snake_case__       ,	snake_case__ )


			def      UpperCAmelCase__						(				self					:  Any ):
										# PyTorch checkpoint
										with TemporaryDirectory() as local_pt_ckpt:
																	self._setup_pt_ckpt(snake_case__ )
																	lowerCamelCase_	:							Union[str, 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__ )
																	lowerCamelCase_	:							Optional[int]									=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__ ):
																								lowerCamelCase_	:							List[Any]									=FeaturesManager.determine_framework(snake_case__ )


			def      UpperCAmelCase__						(				self					:  Any ):
										lowerCamelCase_	:							Tuple									=MagicMock(return_value=snake_case__ )
										with patch("transformers.onnx.features.is_tf_available"       ,	snake_case__ ):
																	lowerCamelCase_	:							Union[str, Any]									=FeaturesManager.determine_framework(self.test_model )
																	self.assertEqual(snake_case__       ,	self.framework_pt )

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

										# Both in environment -> use PyTorch
										lowerCamelCase_	:							Union[str, Any]									=MagicMock(return_value=snake_case__ )
										lowerCamelCase_	:							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__ ):
																	lowerCamelCase_	:							Any									=FeaturesManager.determine_framework(self.test_model )
																	self.assertEqual(snake_case__       ,	self.framework_pt )

										# Both not in environment -> raise error
										lowerCamelCase_	:							Tuple									=MagicMock(return_value=snake_case__ )
										lowerCamelCase_	:							Optional[int]									=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__ ):
																								lowerCamelCase_	:							Dict									=FeaturesManager.determine_framework(self.test_model )











"""simple docstring"""



# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.test_utils import execute_subprocess_async

def 			_snake_case    (					lowerCamelCase__     :   int=None  )    ->     Union[str, Any]:
							if subparsers is not None:
														lowerCamelCase_	:							List[Any]									=subparsers.add_parser("test"  )
							else:
														lowerCamelCase_	:							List[str]									=argparse.ArgumentParser("Accelerate test command"  )

							parser.add_argument(
							    "--config_file"    ,						default=lowerCamelCase__    ,						help=(
							        "The path to use to store the config file. Will default to a file named default_config.yaml in the cache "
							        "location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have "
							        "such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed "
							        "with 'huggingface'."
							    )    ,						)

							if subparsers is not None:
														parser.set_defaults(func=lowerCamelCase__  )
							return parser

def 			_snake_case    (					lowerCamelCase__     :   List[Any]  )    ->     Any:
							lowerCamelCase_	:							Optional[Any]									=os.path.sep.join(__file__.split(os.path.sep  )[:-2] + ["test_utils", "scripts", "test_script.py"]  )

							if args.config_file is None:
														lowerCamelCase_	:							List[Any]									=script_name
							else:
														lowerCamelCase_	:							Union[str, Any]									=F"""--config_file={args.config_file} {script_name}"""

							lowerCamelCase_	:							List[str]									=["accelerate-launch"] + test_args.split()
							lowerCamelCase_	:							Tuple									=execute_subprocess_async(lowerCamelCase__    ,						env=os.environ.copy()  )
							if result.returncode == 0:
														print("Test is a success! You are ready for your distributed training!"  )

def 			_snake_case    (					)    ->     Tuple:
							lowerCamelCase_	:							Any									=test_command_parser()
							lowerCamelCase_	:							List[Any]									=parser.parse_args()
							test_command(lowerCamelCase__  )


if __name__ == "__main__":
						main()



