






'''simple docstring'''




from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
		from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


__lowerCamelCase   :					List[str]   							=						logging.get_logger(__name__)

__lowerCamelCase   :					Tuple   							=						{
    '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''',
    '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''',
    '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''',
    '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''',
    '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''',
    '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''',
}







class    A_	(a_							):
							"""simple docstring"""




							a__   		=	'''bloom'''
							a__   		=	['''past_key_values''']
							a__   		=	{
							    '''num_hidden_layers''': '''n_layer''',
							    '''num_attention_heads''': '''n_head''',
							}

							def __init__(		self			:Union[str, Any]       ,							lowerCAmelCase__			:List[Any]=250_880       ,							lowerCAmelCase__			:List[str]=64       ,							lowerCAmelCase__			:Optional[Any]=2       ,							lowerCAmelCase__			:Union[str, Any]=8       ,							lowerCAmelCase__			:Optional[Any]=1E-5       ,							lowerCAmelCase__			:List[str]=0.0_2       ,							lowerCAmelCase__			:Union[str, Any]=True       ,							lowerCAmelCase__			:Optional[Any]=1       ,							lowerCAmelCase__			:str=2       ,							lowerCAmelCase__			:Optional[int]=False       ,							lowerCAmelCase__			:str=0.0       ,							lowerCAmelCase__			:Tuple=0.0       ,							lowerCAmelCase__			:Optional[Any]=1       ,							lowerCAmelCase__			:Optional[int]=False       ,							**lowerCAmelCase__			:str       ,							)				->  Dict:


										'''simple docstring'''




										snake_case_				:					str   =       vocab_size
										# Backward compatibility with n_embed kwarg
										snake_case_				:					Any   =       kwargs.pop("n_embed"       ,							lowerCAmelCase__				)
										snake_case_				:					List[Any]   =       hidden_size if n_embed is None else n_embed
										snake_case_				:					str   =       n_layer
										snake_case_				:					Optional[Any]   =       n_head
										snake_case_				:					int   =       layer_norm_epsilon
										snake_case_				:					Dict   =       initializer_range
										snake_case_				:					Tuple   =       use_cache
										snake_case_				:					Any   =       pretraining_tp
										snake_case_				:					Any   =       apply_residual_connection_post_layernorm
										snake_case_				:					List[Any]   =       hidden_dropout
										snake_case_				:					Dict   =       attention_dropout

										snake_case_				:					Union[str, Any]   =       bos_token_id
										snake_case_				:					List[str]   =       eos_token_id
										snake_case_				:					Any   =       slow_but_exact

										super().__init__(bos_token_id=lowerCAmelCase__       ,							eos_token_id=lowerCAmelCase__       ,							**lowerCAmelCase__				)







class    A_	(a_							):
							"""simple docstring"""




							a__   		=	version.parse('''1.12'''							)

							def __init__(		self			:Optional[int]       ,							lowerCAmelCase__			:PretrainedConfig       ,							lowerCAmelCase__			:str = "default"       ,							lowerCAmelCase__			:List[PatchingSpec] = None       ,							lowerCAmelCase__			:bool = False       ,							)				->  Dict:


										'''simple docstring'''




										super().__init__(lowerCAmelCase__       ,							task=lowerCAmelCase__       ,							patching_specs=lowerCAmelCase__       ,							use_past=lowerCAmelCase__				)
										if not getattr(self._config       ,							"pad_token_id"       ,							lowerCAmelCase__				):
													# TODO: how to do that better?
													snake_case_				:					List[Any]   =       0

							@property
							def 							_A	(		self			:Optional[int]				)				->  Mapping[str, Mapping[int, str]]:


										'''simple docstring'''




										snake_case_				:					List[str]   =       OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}				)
										if self.use_past:
													# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
													self.fill_with_past_key_values_(lowerCAmelCase__       ,							direction="inputs"       ,							inverted_values_shape=lowerCAmelCase__				)
													snake_case_				:					int   =       {0: "batch", 1: "past_sequence + sequence"}
										else:
													snake_case_				:					int   =       {0: "batch", 1: "sequence"}

										return common_inputs

							@property
							def 							_A	(		self			:str				)				->  int:


										'''simple docstring'''




										return self._config.n_layer

							@property
							def 							_A	(		self			:int				)				->  int:


										'''simple docstring'''




										return self._config.n_head

							@property
							def 							_A	(		self			:Tuple				)				->  float:


										'''simple docstring'''




										return 1E-3

							def 							_A	(		self			:Optional[int]       ,							lowerCAmelCase__			:"PreTrainedTokenizer"       ,							lowerCAmelCase__			:int = -1       ,							lowerCAmelCase__			:int = -1       ,							lowerCAmelCase__			:bool = False       ,							lowerCAmelCase__			:Optional["TensorType"] = None       ,							)				->  Mapping[str, Any]:


										'''simple docstring'''




										snake_case_				:					int   =       super(lowerCAmelCase__       ,							self				).generate_dummy_inputs(
										    lowerCAmelCase__       ,							batch_size=lowerCAmelCase__       ,							seq_length=lowerCAmelCase__       ,							is_pair=lowerCAmelCase__       ,							framework=lowerCAmelCase__				)

										# We need to order the input in the way they appears in the forward()
										snake_case_				:					Any   =       OrderedDict({"input_ids": common_inputs["input_ids"]}				)

										# Need to add the past_keys
										if self.use_past:
													if not is_torch_available():
																raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."				)
													else:
																import torch

																snake_case_				:					Optional[Any]   =       common_inputs["input_ids"].shape
																# Not using the same length for past_key_values
																snake_case_				:					Any   =       seqlen + 2
																snake_case_				:					Union[str, Any]   =       self._config.hidden_size // self.num_attention_heads
																snake_case_				:					str   =       (
																    batch * self.num_attention_heads,
																    head_dim,
																    past_key_values_length,
																)
																snake_case_				:					str   =       (
																    batch * self.num_attention_heads,
																    past_key_values_length,
																    head_dim,
																)
																snake_case_				:					Dict   =       [
																    (torch.zeros(lowerCAmelCase__				), torch.zeros(lowerCAmelCase__				)) for _ in range(self.num_layers				)
																]

										snake_case_				:					str   =       common_inputs["attention_mask"]
										if self.use_past:
													snake_case_				:					Any   =       ordered_inputs["attention_mask"].dtype
													snake_case_				:					Union[str, Any]   =       torch.cat(
													    [ordered_inputs["attention_mask"], torch.ones(lowerCAmelCase__       ,							lowerCAmelCase__       ,							dtype=lowerCAmelCase__				)]       ,							dim=1				)

										return ordered_inputs






							@property
							def 							_A	(		self			:Optional[int]				)				->  int:


										'''simple docstring'''




										return 13












'''simple docstring'''




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
__lowerCamelCase   :					str   							=					'''.'''

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

def 	__UpperCAmelCase       ( __magic_name__     ,__magic_name__     ,__magic_name__						)->    List[str]:



			"""simple docstring"""



			snake_case_				:					Tuple   =       SavedModel()
			snake_case_				:					Dict   =       []

			with open(os.path.join(__magic_name__     ,"utils"     ,"tf_ops"     ,"onnx.json"						)						) as f:
						snake_case_				:					Dict   =       json.load(__magic_name__						)["opsets"]

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

			with open(__magic_name__     ,"rb"						) as f:
						saved_model.ParseFromString(f.read()						)

			snake_case_				:					Tuple   =       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
			snake_case_				:					str   =       sorted(__magic_name__						)
			snake_case_				:					Optional[int]   =       []

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

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


if __name__ == "__main__":
		__lowerCamelCase   :					Any   							=					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)'''
		)
		__lowerCamelCase   :					Dict   							=					parser.parse_args()

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




