import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402



class   lowerCamelCase				(unittest.TestCase      ):







		'''simple docstring'''



		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:
							UpperCAmelCase_   :				List[str]						=						0



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				Optional[Any]						=						AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'       )
							self.assertIsInstance(lowercase_    ,		lowercase_       )



		def  __UpperCAmelCase      (							self       )     ->							str:







							with tempfile.TemporaryDirectory() as tmpdirname:
												UpperCAmelCase_   :				Optional[int]						=						Path(lowercase_       ) / 'preprocessor_config.json'
												UpperCAmelCase_   :				str						=						Path(lowercase_       ) / 'config.json'
												json.dump(
												    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}    ,		open(lowercase_    ,		'w'       )    ,		)
												json.dump({'model_type': 'clip'}    ,		open(lowercase_    ,		'w'       )       )

												UpperCAmelCase_   :				Dict						=						AutoImageProcessor.from_pretrained(lowercase_       )
												self.assertIsInstance(lowercase_    ,		lowercase_       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:







							with tempfile.TemporaryDirectory() as tmpdirname:
												UpperCAmelCase_   :				Union[str, Any]						=						Path(lowercase_       ) / 'preprocessor_config.json'
												UpperCAmelCase_   :				Dict						=						Path(lowercase_       ) / 'config.json'
												json.dump(
												    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}    ,		open(lowercase_    ,		'w'       )    ,		)
												json.dump({'model_type': 'clip'}    ,		open(lowercase_    ,		'w'       )       )

												UpperCAmelCase_   :				Optional[int]						=						AutoImageProcessor.from_pretrained(lowercase_       )
												self.assertIsInstance(lowercase_    ,		lowercase_       )



		def  __UpperCAmelCase      (							self       )     ->							List[Any]:







							with tempfile.TemporaryDirectory() as tmpdirname:
												UpperCAmelCase_   :				Dict						=						CLIPConfig()

												# Create a dummy config file with image_proceesor_type
												UpperCAmelCase_   :				str						=						Path(lowercase_       ) / 'preprocessor_config.json'
												UpperCAmelCase_   :				List[str]						=						Path(lowercase_       ) / 'config.json'
												json.dump(
												    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}    ,		open(lowercase_    ,		'w'       )    ,		)
												json.dump({'model_type': 'clip'}    ,		open(lowercase_    ,		'w'       )       )

												# remove image_processor_type to make sure config.json alone is enough to load image processor locally
												UpperCAmelCase_   :				Tuple						=						AutoImageProcessor.from_pretrained(lowercase_       ).to_dict()

												config_dict.pop('image_processor_type'       )
												UpperCAmelCase_   :				Union[str, Any]						=						CLIPImageProcessor(**lowercase_       )

												# save in new folder
												model_config.save_pretrained(lowercase_       )
												config.save_pretrained(lowercase_       )

												UpperCAmelCase_   :				Any						=						AutoImageProcessor.from_pretrained(lowercase_       )

												# make sure private variable is not incorrectly saved
												UpperCAmelCase_   :				Any						=						json.loads(config.to_json_string()       )
												self.assertTrue('_processor_class' not in dict_as_saved       )

							self.assertIsInstance(lowercase_    ,		lowercase_       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[Any]:







							with tempfile.TemporaryDirectory() as tmpdirname:
												UpperCAmelCase_   :				int						=						Path(lowercase_       ) / 'preprocessor_config.json'
												json.dump(
												    {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}    ,		open(lowercase_    ,		'w'       )    ,		)

												UpperCAmelCase_   :				Any						=						AutoImageProcessor.from_pretrained(lowercase_       )
												self.assertIsInstance(lowercase_    ,		lowercase_       )



		def  __UpperCAmelCase      (							self       )     ->							int:







							with self.assertRaisesRegex(
							    lowercase_    ,		'clip-base is not a local folder and is not a valid model identifier'       ):
												UpperCAmelCase_   :				Dict						=						AutoImageProcessor.from_pretrained('clip-base'       )



		def  __UpperCAmelCase      (							self       )     ->							Union[str, Any]:







							with self.assertRaisesRegex(
							    lowercase_    ,		r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'       ):
												UpperCAmelCase_   :				Dict						=						AutoImageProcessor.from_pretrained(lowercase_    ,		revision='aaaaaa'       )



		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:







							with self.assertRaisesRegex(
							    lowercase_    ,		'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'    ,		):
												UpperCAmelCase_   :				int						=						AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model'       )



		def  __UpperCAmelCase      (							self       )     ->							int:







							with self.assertRaises(lowercase_       ):
												UpperCAmelCase_   :				List[str]						=						AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'       )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(lowercase_       ):
												UpperCAmelCase_   :				Tuple						=						AutoImageProcessor.from_pretrained(
												    'hf-internal-testing/test_dynamic_image_processor'    ,		trust_remote_code=lowercase_       )

							UpperCAmelCase_   :				int						=						AutoImageProcessor.from_pretrained(
							    'hf-internal-testing/test_dynamic_image_processor'    ,		trust_remote_code=lowercase_       )
							self.assertEqual(image_processor.__class__.__name__    ,		'NewImageProcessor'       )

							# Test image processor can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
												image_processor.save_pretrained(lowercase_       )
												UpperCAmelCase_   :				Dict						=						AutoImageProcessor.from_pretrained(lowercase_    ,		trust_remote_code=lowercase_       )
							self.assertEqual(reloaded_image_processor.__class__.__name__    ,		'NewImageProcessor'       )



		def  __UpperCAmelCase      (							self       )     ->							str:







							try:
												AutoConfig.register('custom'    ,		lowercase_       )
												AutoImageProcessor.register(lowercase_    ,		lowercase_       )
												# Trying to register something existing in the Transformers library will raise an error
												with self.assertRaises(lowercase_       ):
																	AutoImageProcessor.register(lowercase_    ,		lowercase_       )

												with tempfile.TemporaryDirectory() as tmpdirname:
																	UpperCAmelCase_   :				int						=						Path(lowercase_       ) / 'preprocessor_config.json'
																	UpperCAmelCase_   :				Union[str, Any]						=						Path(lowercase_       ) / 'config.json'
																	json.dump(
																	    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}    ,		open(lowercase_    ,		'w'       )    ,		)
																	json.dump({'model_type': 'clip'}    ,		open(lowercase_    ,		'w'       )       )

																	UpperCAmelCase_   :				str						=						CustomImageProcessor.from_pretrained(lowercase_       )

												# Now that the config is registered, it can be used as any other config with the auto-API
												with tempfile.TemporaryDirectory() as tmp_dir:
																	image_processor.save_pretrained(lowercase_       )
																	UpperCAmelCase_   :				Union[str, Any]						=						AutoImageProcessor.from_pretrained(lowercase_       )
																	self.assertIsInstance(lowercase_    ,		lowercase_       )

							finally:
												if "custom" in CONFIG_MAPPING._extra_content:
																	del CONFIG_MAPPING._extra_content["custom"]
												if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																	del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]



		def  __UpperCAmelCase      (							self       )     ->							Dict:



							class   lowerCamelCase				(_snake_case      ):







									'''simple docstring'''



									_snake_case   :   Optional[Any]							=  True

							try:
												AutoConfig.register('custom'    ,		lowercase_       )
												AutoImageProcessor.register(lowercase_    ,		lowercase_       )
												# If remote code is not set, the default is to use local
												UpperCAmelCase_   :				Optional[Any]						=						AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'       )
												self.assertEqual(image_processor.__class__.__name__    ,		'NewImageProcessor'       )
												self.assertTrue(image_processor.is_local       )

												# If remote code is disabled, we load the local one.
												UpperCAmelCase_   :				Optional[Any]						=						AutoImageProcessor.from_pretrained(
												    'hf-internal-testing/test_dynamic_image_processor'    ,		trust_remote_code=lowercase_       )
												self.assertEqual(image_processor.__class__.__name__    ,		'NewImageProcessor'       )
												self.assertTrue(image_processor.is_local       )

												# If remote is enabled, we load from the Hub
												UpperCAmelCase_   :				List[Any]						=						AutoImageProcessor.from_pretrained(
												    'hf-internal-testing/test_dynamic_image_processor'    ,		trust_remote_code=lowercase_       )
												self.assertEqual(image_processor.__class__.__name__    ,		'NewImageProcessor'       )
												self.assertTrue(not hasattr(lowercase_    ,		'is_local'       )       )

							finally:
												if "custom" in CONFIG_MAPPING._extra_content:
																	del CONFIG_MAPPING._extra_content["custom"]
												if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
																	del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]














import argparse
import json
import os

from tensorflow.core.protobuf.saved_model_pba import SavedModel


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_copies.py
__UpperCAmelCase     							=						'.'

# Internal TensorFlow ops that can be safely ignored (mostly specific to a saved model)
__UpperCAmelCase     							=						[
    'Assert',
    'AssignVariableOp',
    'EmptyTensorList',
    'MergeV2Checkpoints',
    'ReadVariableOp',
    'ResourceGather',
    'RestoreV2',
    'SaveV2',
    'ShardedFilename',
    'StatefulPartitionedCall',
    'StaticRegexFullMatch',
    'VarHandleOp',
]




def 					lowercase__				(					__snake_case   :							Union[str, Any]					,	__snake_case   :							Optional[int]					,	__snake_case   :							Union[str, Any]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Optional[Any]						=						SavedModel()
					UpperCAmelCase_   :				Optional[Any]						=						[]

					with open(os.path.join(__snake_case					,	'utils'					,	'tf_ops'					,	'onnx.json'  )  ) as f:
										UpperCAmelCase_   :				Optional[Any]						=						json.load(__snake_case  )['opsets']

					for i in range(1					,	opset + 1  ):
										onnx_ops.extend(onnx_opsets[str(__snake_case  )]  )

					with open(__snake_case					,	'rb'  ) as f:
										saved_model.ParseFromString(f.read()  )

					UpperCAmelCase_   :				List[Any]						=						set()

					# Iterate over every metagraph in case there is more than one (a saved model can contain multiple graphs)
					for meta_graph in saved_model.meta_graphs:
										# Add operations in the graph definition
										model_op_names.update(node.op for node in meta_graph.graph_def.node  )

										# Go through the functions in the graph definition
										for func in meta_graph.graph_def.library.function:
															# Add operations in each function
															model_op_names.update(node.op for node in func.node_def  )

    # Convert to list, sorted if you want
					UpperCAmelCase_   :				Optional[int]						=						sorted(__snake_case  )
					UpperCAmelCase_   :				int						=						[]

					for op in model_op_names:
										if op not in onnx_ops and op not in INTERNAL_OPS:
															incompatible_ops.append(__snake_case  )

					if strict and len(__snake_case  ) > 0:
										raise Exception(F"Found the following incompatible ops for the opset {opset}:\n" + incompatible_ops  )
					elif len(__snake_case  ) > 0:
										print(F"Found the following incompatible ops for the opset {opset}:"  )
										print(*__snake_case					,	sep='\n'  )
					else:
										print(F"The saved model {saved_model_path} can properly be converted with ONNX."  )


if __name__ == "__main__":
		__UpperCAmelCase     							=						argparse.ArgumentParser()
		parser.add_argument('--saved_model_path', help='Path of the saved model to check (the .pb file).')
		parser.add_argument(
		    '--opset', default=12, type=int, help='The ONNX opset against which the model has to be tested.'
		)
		parser.add_argument(
		    '--framework', choices=['onnx'], default='onnx', help='Frameworks against which to test the saved model.'
		)
		parser.add_argument(
		    '--strict', action='store_true', help='Whether make the checking strict (raise errors) or not (raise warnings)'
		)
		__UpperCAmelCase     							=						parser.parse_args()

		if args.framework == "onnx":
				onnx_compliancy(args.saved_model_path, args.strict, args.opset)






