


"""simple docstring"""


from typing import Union

import fire
import torch
from tqdm import tqdm


def _lowerCamelCase			(	_UpperCamelCase					,				_UpperCamelCase = "cpu"					,				_UpperCamelCase = None   ):

			'''simple docstring'''




			__lowerCAmelCase       					=    torch.load(_UpperCamelCase					,				map_location=_UpperCamelCase   )
			for k, v in tqdm(state_dict.items()   ):
						if not isinstance(_UpperCamelCase					,				torch.Tensor   ):
									raise TypeError("FP16 conversion only works on paths that are saved state dicts, like pytorch_model.bin"   )
						__lowerCAmelCase       					=    v.half()
			if save_path is None:  # overwrite src_path
						__lowerCAmelCase       					=    src_path
			torch.save(_UpperCamelCase					,				_UpperCamelCase   )


if __name__ == "__main__":
	fire.Fire(convert)








"""simple docstring"""


import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
	import tensorflow as tf

	from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments
@require_tf
class 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self					,     __a			):
										for model_result in results.values():
													for batch_size, sequence_length in zip(model_result["bs"]					,     model_result["ss"]			):
																__lowerCAmelCase       					=    model_result["result"][batch_size][sequence_length]
																self.assertIsNotNone(__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sgugger/tiny-distilbert-classification"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     only_pretrain_model=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "patrickvonplaten/t5-tiny-random"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     configs=[config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("GPU"			)			) == 0					,     "Cannot do xla on CPU."			)
							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     use_xla=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]					,     inference=__a					,     save_to_csv=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     inference_time_csv_file=os.path.join(__a					,     "inf_time.csv"			)					,     inference_memory_csv_file=os.path.join(__a					,     "inf_mem.csv"			)					,     env_info_csv_file=os.path.join(__a					,     "env.csv"			)					,     multi_process=__a					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													benchmark.run()
													self.assertTrue(Path(os.path.join(__a					,     "inf_time.csv"			)			).exists()			)
													self.assertTrue(Path(os.path.join(__a					,     "inf_mem.csv"			)			).exists()			)
													self.assertTrue(Path(os.path.join(__a					,     "env.csv"			)			).exists()			)



							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"

										def _check_summary_is_not_empty(__a			):
													self.assertTrue(hasattr(__a					,     "sequential"			)			)
													self.assertTrue(hasattr(__a					,     "cumulative"			)			)
													self.assertTrue(hasattr(__a					,     "current"			)			)
													self.assertTrue(hasattr(__a					,     "total"			)			)

										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
													    models=[MODEL_ID]					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     log_filename=os.path.join(__a					,     "log.txt"			)					,     log_print=__a					,     trace_memory_line_by_line=__a					,     eager_mode=__a					,     multi_process=__a					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													__lowerCAmelCase       					=    benchmark.run()
													_check_summary_is_not_empty(result.inference_summary			)
													self.assertTrue(Path(os.path.join(__a					,     "log.txt"			)			).exists()			)




