



"""simple docstring"""
# Copyright 2022 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 os
from typing import List



def 				lowerCAmelCase_(      lowercase_   :							List[str]  ,      lowercase_   :							str							)		->				Any:
				if name != "training_function" and name != "main":
								raise ValueError(F"""Incorrect function name passed: {name}, choose either 'main' or 'training_function'"""							)
				_lowerCamelCase     ,   _lowerCamelCase					     = [], False
				for line in lines:
								if not found_start and F"""def {name}""" in line:
												_lowerCamelCase					     = True
												good_lines.append(lowercase_							)
												continue
								if found_start:
												if name == "training_function" and "def main" in line:
																return good_lines
												if name == "main" and "if __name__" in line:
																return good_lines
												good_lines.append(lowercase_							)



def 				lowerCAmelCase_(      lowercase_   :							List[str]							)		->				str:
				return [line for line in lines if not line.lstrip().startswith('''#'''							) and line != "\n"]




def 				lowerCAmelCase_(      lowercase_   :							str  ,      lowercase_   :							str  ,      lowercase_   :							bool  ,      lowercase_   :							str = None							)		->				List[str]:
				with open(lowercase_  ,      '''r'''							) as f:
								_lowerCamelCase					     = f.readlines()
				with open(os.path.abspath(os.path.join('''examples'''  ,      '''nlp_example.py'''							)							)  ,      '''r'''							) as f:
								_lowerCamelCase					     = f.readlines()
				with open(lowercase_  ,      '''r'''							) as f:
								_lowerCamelCase					     = f.readlines()
				if secondary_filename is not None:
								with open(lowercase_  ,      '''r'''							) as f:
												_lowerCamelCase					     = f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
				if parser_only:
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''main'''							)							)
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''main'''							)							)
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''main'''							)							)
								if secondary_filename is not None:
												_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''main'''							)							)
				else:
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''training_function'''							)							)
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''training_function'''							)							)
								_lowerCamelCase					     = clean_lines(get_function_contents_by_name(lowercase_  ,      '''training_function'''							)							)
								if secondary_filename is not None:
												_lowerCamelCase					     = clean_lines(
												    get_function_contents_by_name(lowercase_  ,      '''training_function'''							)							)

				_lowerCamelCase					     = '''train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n'''

				# Specific code in our script that differs from the full version, aka what is new
				_lowerCamelCase					     = []
				_lowerCamelCase					     = []  # We keep track of the idxs just in case it's a repeated statement
				_lowerCamelCase					     = iter(lowercase_							)
				for i in range(len(lowercase_							) - 1							):
								if i not in passed_idxs:
												_lowerCamelCase					     = next(lowercase_							)
												if (line not in full_file_func) and (line.lstrip() != _dl_line):
																if "TESTING_MOCKED_DATALOADERS" not in line:
																				new_feature_code.append(lowercase_							)
																				passed_idxs.append(lowercase_							)
																else:
																				# Skip over the `config['num_epochs'] = 2` statement
																				_lowerCamelCase					     = next(lowercase_							)

    # Extract out just the new parts from the full_file_training_func
				_lowerCamelCase					     = []
				_lowerCamelCase					     = []  # We keep track of the idxs just in case it's a repeated statement
				for i, line in enumerate(lowercase_							):
								if i not in passed_idxs:
												if (line not in full_file_func) and (line.lstrip() != _dl_line):
																if "TESTING_MOCKED_DATALOADERS" not in line:
																				new_full_example_parts.append(lowercase_							)
																				passed_idxs.append(lowercase_							)

    # Finally, get the overall diff
				_lowerCamelCase					     = [line for line in new_feature_code if line not in new_full_example_parts]
				if secondary_filename is not None:
								_lowerCamelCase					     = [line for line in full_file_contents if line not in secondary_file_func]
								_lowerCamelCase					     = [line for line in diff_from_example if line not in diff_from_two]

				return diff_from_example





"""simple docstring"""
import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import (
			    ErnieMForInformationExtraction,
			    ErnieMForMultipleChoice,
			    ErnieMForQuestionAnswering,
			    ErnieMForSequenceClassification,
			    ErnieMForTokenClassification,
			    ErnieMModel,
			)
			from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST


class 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(			self ,				lowerCamelCase__ ,				lowerCamelCase__=1_3 ,				lowerCamelCase__=7 ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=True ,				lowerCamelCase__=9_9 ,				lowerCamelCase__=3_2 ,				lowerCamelCase__=5 ,				lowerCamelCase__=4 ,				lowerCamelCase__=3_7 ,				lowerCamelCase__="gelu" ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=0.1 ,				lowerCamelCase__=5_1_2 ,				lowerCamelCase__=1_6 ,				lowerCamelCase__=2 ,				lowerCamelCase__=0.0_2 ,				lowerCamelCase__=3 ,				lowerCamelCase__=4 ,				lowerCamelCase__=None ,				):
									_lowerCamelCase					     = parent
									_lowerCamelCase					     = batch_size
									_lowerCamelCase					     = seq_length
									_lowerCamelCase					     = is_training
									_lowerCamelCase					     = use_input_mask
									_lowerCamelCase					     = use_labels
									_lowerCamelCase					     = vocab_size
									_lowerCamelCase					     = hidden_size
									_lowerCamelCase					     = num_hidden_layers
									_lowerCamelCase					     = num_attention_heads
									_lowerCamelCase					     = intermediate_size
									_lowerCamelCase					     = hidden_act
									_lowerCamelCase					     = hidden_dropout_prob
									_lowerCamelCase					     = attention_probs_dropout_prob
									_lowerCamelCase					     = max_position_embeddings
									_lowerCamelCase					     = type_vocab_size
									_lowerCamelCase					     = type_sequence_label_size
									_lowerCamelCase					     = initializer_range
									_lowerCamelCase					     = num_labels
									_lowerCamelCase					     = num_choices
									_lowerCamelCase					     = scope

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

									_lowerCamelCase					     = None
									if self.use_input_mask:
													_lowerCamelCase					     = random_attention_mask([self.batch_size, self.seq_length]      )

									_lowerCamelCase					     = None
									_lowerCamelCase					     = None
									_lowerCamelCase					     = None
									if self.use_labels:
													_lowerCamelCase					     = ids_tensor([self.batch_size] ,				self.type_sequence_label_size      )
													_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.num_labels      )
													_lowerCamelCase					     = ids_tensor([self.batch_size] ,				self.num_choices      )

									_lowerCamelCase					     = self.get_config()

									return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size      )

									_lowerCamelCase					     = None
									if self.use_input_mask:
													_lowerCamelCase					     = random_attention_mask([self.batch_size, self.seq_length]      )
									_lowerCamelCase					     = self.get_config()

									return config, input_ids, input_mask

					def 	snake_case__					(			self      ):
									return ErnieMConfig(
									    vocab_size=self.vocab_size ,				hidden_size=self.hidden_size ,				num_hidden_layers=self.num_hidden_layers ,				num_attention_heads=self.num_attention_heads ,				intermediate_size=self.intermediate_size ,				hidden_act=self.hidden_act ,				hidden_dropout_prob=self.hidden_dropout_prob ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob ,				max_position_embeddings=self.max_position_embeddings ,				type_vocab_size=self.type_vocab_size ,				initializer_range=self.initializer_range ,				)

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMModel(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				return_dict=lowerCamelCase__      )
									self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMForQuestionAnswering(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				start_positions=lowerCamelCase__ ,				end_positions=lowerCamelCase__ ,				)
									self.parent.assertEqual(result.start_logits.shape ,				(self.batch_size, self.seq_length)      )
									self.parent.assertEqual(result.end_logits.shape ,				(self.batch_size, self.seq_length)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = ErnieMForInformationExtraction(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = torch.ones_like(lowerCamelCase__ ,				dtype=torch.floataa      )
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				start_positions=lowerCamelCase__ ,				end_positions=lowerCamelCase__ ,				)
									self.parent.assertEqual(result.start_logits.shape ,				(self.batch_size, self.seq_length)      )
									self.parent.assertEqual(result.end_logits.shape ,				(self.batch_size, self.seq_length)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = ErnieMForSequenceClassification(lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__      )
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_labels
									_lowerCamelCase					     = ErnieMForTokenClassification(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									input_ids.to(lowerCamelCase__      )
									input_mask.to(lowerCamelCase__      )
									token_labels.to(lowerCamelCase__      )

									_lowerCamelCase					     = model(lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__      )

									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)      )

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									_lowerCamelCase					     = self.num_choices
									_lowerCamelCase					     = ErnieMForMultipleChoice(config=lowerCamelCase__      )
									model.to(lowerCamelCase__      )
									model.eval()
									_lowerCamelCase					     = input_ids.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = input_mask.unsqueeze(1      ).expand(-1 ,				self.num_choices ,				-1      ).contiguous()
									_lowerCamelCase					     = model(
									    lowerCamelCase__ ,				attention_mask=lowerCamelCase__ ,				labels=lowerCamelCase__ ,				)
									self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices)      )





					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.prepare_config_and_inputs()
									(
									    (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   (
									    _lowerCamelCase
									)     ,   
									)					     = config_and_inputs
									_lowerCamelCase					     = {'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict


@require_torch
class 							lowerCamelCase_(      A__, A__, unittest.TestCase			):





					'''simple docstring'''



					lowercase__      :				Optional[Any]        =       (
					    (
					        ErnieMModel,
					        ErnieMForMultipleChoice,
					        ErnieMForQuestionAnswering,
					        ErnieMForSequenceClassification,
					        ErnieMForTokenClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowercase__      :				List[Any]        =       ()
					lowercase__      :				Union[str, Any]        =       (
					    {
					        'feature-extraction': ErnieMModel,
					        'question-answering': ErnieMForQuestionAnswering,
					        'text-classification': ErnieMForSequenceClassification,
					        'token-classification': ErnieMForTokenClassification,
					        'zero-shot': ErnieMForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					lowercase__      :				Optional[int]        =       False

					def 	snake_case__					(			self ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__ ,				lowerCamelCase__      ):
									if pipeline_test_casse_name == "QAPipelineTests":
													return True

									return False

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ErnieMModelTester(self      )
									_lowerCamelCase					     = ConfigTester(self ,				config_class=lowerCamelCase__ ,				hidden_size=3_7      )

					def 	snake_case__					(			self      ):
									self.config_tester.run_common_tests()

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
													_lowerCamelCase					     = type
													self.model_tester.create_and_check_model(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_information_extraction(*lowerCamelCase__      )

					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__      )





					@slow
					def 	snake_case__					(			self      ):
									for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_lowerCamelCase					     = ErnieMModel.from_pretrained(lowerCamelCase__      )
													self.assertIsNotNone(lowerCamelCase__      )


@require_torch
class 							lowerCamelCase_(      unittest.TestCase			):





					'''simple docstring'''



					@slow
					def 	snake_case__					(			self      ):
									_lowerCamelCase					     = ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''      )
									model.eval()
									_lowerCamelCase					     = torch.tensor([[0, 1, 2, 3, 4, 5]]      )
									_lowerCamelCase					     = model(lowerCamelCase__      )[0]

									# TODO Replace vocab size
									_lowerCamelCase					     = 7_6_8

									_lowerCamelCase					     = torch.Size((1, 6, hidden_size)      )
									self.assertEqual(output.shape ,				lowerCamelCase__      )

									_lowerCamelCase					     = torch.tensor(
									    [[[-0.0_0_1_2, 0.1_2_4_5, -0.0_2_1_4], [-0.0_7_4_2, 0.0_2_4_4, -0.0_7_7_1], [-0.0_3_3_3, 0.1_1_6_4, -0.1_5_5_4]]]      )

									self.assertTrue(torch.allclose(output[:, :3, :3] ,				lowerCamelCase__ ,				atol=1e-3      )      )
