


'''simple docstring'''


import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer





class     lowercase__  (						lowercase_					):





							'''simple docstring'''


							A_							:      List[str]      	=  '''Wav2Vec2FeatureExtractor'''
							A_							:      List[Any]      	=  '''AutoTokenizer'''






							def __init__(    self    ,					__snake_case    ,					__snake_case  ):
											super().__init__(__snake_case    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.feature_extractor
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      False






							@classmethod
							def UpperCAmelCase_	(    cls    ,					__snake_case    ,					**__snake_case  ):
											try:
															return super().from_pretrained(__snake_case    ,					**__snake_case  )
											except OSError:
															warnings.warn(
															    f"""Loading a tokenizer inside {cls.__name__} from a config that does not"""
															    """ include a `tokenizer_class` attribute is deprecated and will be """
															    """removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`"""
															    """ attribute to either your `config.json` or `tokenizer_config.json` """
															    """file to suppress this warning: """    ,					__snake_case    ,					)

															_SCREAMING_SNAKE_CASE :       int     		=      WavaVecaFeatureExtractor.from_pretrained(__snake_case    ,					**__snake_case  )
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      WavaVecaCTCTokenizer.from_pretrained(__snake_case    ,					**__snake_case  )

															return cls(feature_extractor=__snake_case    ,					tokenizer=__snake_case  )






							def __call__(    self    ,					*__snake_case    ,					**__snake_case  ):
											# For backward compatibility
											if self._in_target_context_manager:
															return self.current_processor(*__snake_case    ,					**__snake_case  )

											if "raw_speech" in kwargs:
															warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."""  )
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      kwargs.pop("""raw_speech"""  )
											else:
															_SCREAMING_SNAKE_CASE :       Optional[int]     		=      kwargs.pop("""audio"""    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Any     		=      kwargs.pop("""sampling_rate"""    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Dict     		=      kwargs.pop("""text"""    ,					__snake_case  )
											if len(__snake_case  ) > 0:
															_SCREAMING_SNAKE_CASE :       str     		=      args[0]
															_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      args[1:]

											if audio is None and text is None:
															raise ValueError("""You need to specify either an `audio` or `text` input to process."""  )

											if audio is not None:
															_SCREAMING_SNAKE_CASE :       List[str]     		=      self.feature_extractor(__snake_case    ,					*__snake_case    ,					sampling_rate=__snake_case    ,					**__snake_case  )
											if text is not None:
															_SCREAMING_SNAKE_CASE :       int     		=      self.tokenizer(__snake_case    ,					**__snake_case  )

											if text is None:
															return inputs
											elif audio is None:
															return encodings
											else:
															_SCREAMING_SNAKE_CASE :       int     		=      encodings["""input_ids"""]
															return inputs






							def UpperCAmelCase_	(    self    ,					*__snake_case    ,					**__snake_case  ):
											# For backward compatibility
											if self._in_target_context_manager:
															return self.current_processor.pad(*__snake_case    ,					**__snake_case  )

											_SCREAMING_SNAKE_CASE :       Tuple     		=      kwargs.pop("""input_features"""    ,					__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      kwargs.pop("""labels"""    ,					__snake_case  )
											if len(__snake_case  ) > 0:
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      args[0]
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      args[1:]

											if input_features is not None:
															_SCREAMING_SNAKE_CASE :       Tuple     		=      self.feature_extractor.pad(__snake_case    ,					*__snake_case    ,					**__snake_case  )
											if labels is not None:
															_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.tokenizer.pad(__snake_case    ,					**__snake_case  )

											if labels is None:
															return input_features
											elif input_features is None:
															return labels
											else:
															_SCREAMING_SNAKE_CASE :       str     		=      labels["""input_ids"""]
															return input_features






							def UpperCAmelCase_	(    self    ,					*__snake_case    ,					**__snake_case  ):
											return self.tokenizer.batch_decode(*__snake_case    ,					**__snake_case  )






							def UpperCAmelCase_	(    self    ,					*__snake_case    ,					**__snake_case  ):
											return self.tokenizer.decode(*__snake_case    ,					**__snake_case  )






							@contextmanager
							def UpperCAmelCase_	(    self  ):
											warnings.warn(
											    """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
											    """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
											    """your audio inputs, or in a separate call."""  )
											_SCREAMING_SNAKE_CASE :       str     		=      True
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.tokenizer
											yield
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.feature_extractor
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      False







import os

import numpy
import onnx





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    a.name
							snake_case_							=    b.name

							snake_case_							=    ''
							snake_case_							=    ''

							snake_case_							=    a == b

							snake_case_							=    name_a
							snake_case_							=    name_b

							return res





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							for i, input_name in enumerate(node_proto.input      ):
														if input_name == name:
																					node_proto.input.insert(UpperCamelCase__   ,							UpperCamelCase__      )
																					node_proto.input.pop(i + 1      )

							if node_proto.op_type == "If":
														_graph_replace_input_with(node_proto.attribute[0].g   ,							UpperCamelCase__   ,							UpperCamelCase__      )
														_graph_replace_input_with(node_proto.attribute[1].g   ,							UpperCamelCase__   ,							UpperCamelCase__      )
							if node_proto.op_type == "Loop":
														_graph_replace_input_with(node_proto.attribute[0].g   ,							UpperCamelCase__   ,							UpperCamelCase__      )





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							for n in graph_proto.node:
														_node_replace_input_with(UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      )





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    list(model.graph.initializer      )
							snake_case_							=    list(model_without_ext.graph.initializer      )
							for i, ref_i in ind_to_replace:
														assert inits_with_data[i].name == inits[i].name
														assert inits_with_data[ref_i].name == inits[ref_i].name
														assert i > ref_i

														snake_case_							=    inits[i].name
														snake_case_							=    inits[ref_i].name

														model_without_ext.graph.initializer.remove(inits[i]      )

														# for n in model.graph.node:
														_graph_replace_input_with(model_without_ext.graph   ,							UpperCamelCase__   ,							UpperCamelCase__      )





def 					__lowerCamelCase	( UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    os.path.dirname(UpperCamelCase__      )
							snake_case_							=    os.path.basename(UpperCamelCase__      )

							snake_case_							=    onnx.load(os.path.join(UpperCamelCase__   ,							UpperCamelCase__      )      )

							snake_case_							=    list(model.graph.initializer      )

							snake_case_							=    set()
							snake_case_							=    {}
							snake_case_							=    []

							snake_case_							=    0

							for i in range(len(UpperCamelCase__      )      ):
														if i in dup_set:
																					continue

														for j in range(i + 1   ,							len(UpperCamelCase__      )      ):
																					if j in dup_set:
																												continue
																					if _is_equal_tensor_proto(inits[i]   ,							inits[j]      ):
																												dup_set.add(UpperCamelCase__      )
																												dup_set.add(UpperCamelCase__      )

																												snake_case_							=    inits[j].data_type
																												snake_case_							=    numpy.prod(inits[j].dims      )
																												if dtype == 1:
																																			mem_size *= 4
																												elif dtype == 6:
																																			mem_size *= 4
																												elif dtype == 7 or dtype == 11:
																																			mem_size *= 8
																												else:
																																			print('unexpected data type: '   ,							UpperCamelCase__      )
																												total_reduced_size += mem_size

																												snake_case_							=    inits[i].name
																												snake_case_							=    inits[j].name

																												if name_i in dup_map:
																																			dup_map[name_i].append(UpperCamelCase__      )
																												else:
																																			snake_case_							=    [name_j]
																												ind_to_replace.append((j, i)      )

							print('total reduced size: '   ,							total_reduced_size / 1024 / 1024 / 1024   ,							'GB'      )

							snake_case_							=    sorted(UpperCamelCase__      )
							_remove_dup_initializers_from_model(UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      )

							snake_case_							=    'optimized_' + model_file_name
							snake_case_							=    os.path.join(UpperCamelCase__   ,							UpperCamelCase__      )
							onnx.save(UpperCamelCase__   ,							UpperCamelCase__      )

							return new_model




