





'''simple docstring'''






import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_MAPPING,
					    SegformerForImageClassification,
					    SegformerForSemanticSegmentation,
					    SegformerModel,
					)
					from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
					from PIL import Image

					from transformers import SegformerImageProcessor
class 		lowerCamelCase	(   _A							):



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= self.config_class(**self.inputs_dict						)
			self.parent.assertTrue(hasattr(a_		,   "hidden_sizes"						)						)
			self.parent.assertTrue(hasattr(a_		,   "num_attention_heads"						)						)
			self.parent.assertTrue(hasattr(a_		,   "num_encoder_blocks"						)						)






class 		lowerCamelCase	:



	def __init__( self		,   a_		,   a_=13		,   a_=64		,   a_=3		,   a_=4		,   a_=[2, 2, 2, 2]		,   a_=[8, 4, 2, 1]		,   a_=[16, 32, 64, 128]		,   a_=[1, 4, 8, 16]		,   a_=[1, 2, 4, 8]		,   a_=True		,   a_=True		,   a_="gelu"		,   a_=0.1		,   a_=0.1		,   a_=0.02		,   a_=3		,   a_=None		,   ):
			lowerCAmelCase  :     Optional[Any]       						= parent
			lowerCAmelCase  :     str       						= batch_size
			lowerCAmelCase  :     int       						= image_size
			lowerCAmelCase  :     Tuple       						= num_channels
			lowerCAmelCase  :     Union[str, Any]       						= num_encoder_blocks
			lowerCAmelCase  :     Tuple       						= sr_ratios
			lowerCAmelCase  :     Any       						= depths
			lowerCAmelCase  :     List[Any]       						= hidden_sizes
			lowerCAmelCase  :     List[str]       						= downsampling_rates
			lowerCAmelCase  :     Dict       						= num_attention_heads
			lowerCAmelCase  :     Any       						= is_training
			lowerCAmelCase  :     Dict       						= use_labels
			lowerCAmelCase  :     List[Any]       						= hidden_act
			lowerCAmelCase  :     List[str]       						= hidden_dropout_prob
			lowerCAmelCase  :     Dict       						= attention_probs_dropout_prob
			lowerCAmelCase  :     Union[str, Any]       						= initializer_range
			lowerCAmelCase  :     List[str]       						= num_labels
			lowerCAmelCase  :     int       						= scope



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Union[str, Any]       						= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

			lowerCAmelCase  :     str       						= None
			if self.use_labels:
					lowerCAmelCase  :     Any       						= ids_tensor([self.batch_size, self.image_size, self.image_size]		,   self.num_labels						)

			lowerCAmelCase  :     int       						= self.get_config()
			return config, pixel_values, labels



	def 	_lowerCamelCase     ( self						):
			return SegformerConfig(
			    image_size=self.image_size		,   num_channels=self.num_channels		,   num_encoder_blocks=self.num_encoder_blocks		,   depths=self.depths		,   hidden_sizes=self.hidden_sizes		,   num_attention_heads=self.num_attention_heads		,   hidden_act=self.hidden_act		,   hidden_dropout_prob=self.hidden_dropout_prob		,   attention_probs_dropout_prob=self.attention_probs_dropout_prob		,   initializer_range=self.initializer_range		,   )



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     Optional[int]       						= SegformerModel(config=a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Tuple       						= model(a_						)
			lowerCAmelCase  :     str       						= self.image_size // (self.downsampling_rates[-1] * 2)
			self.parent.assertEqual(
			    result.last_hidden_state.shape		,   (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)						)



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     Dict       						= self.num_labels
			lowerCAmelCase  :     Any       						= SegformerForSemanticSegmentation(a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Any       						= model(a_						)
			self.parent.assertEqual(
			    result.logits.shape		,   (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
			lowerCAmelCase  :     Any       						= model(a_		,   labels=a_						)
			self.parent.assertEqual(
			    result.logits.shape		,   (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)						)
			self.parent.assertGreater(result.loss		,   0.0						)



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_						):
			lowerCAmelCase  :     Optional[int]       						= 1
			lowerCAmelCase  :     Union[str, Any]       						= SegformerForSemanticSegmentation(config=a_						)
			model.to(a_						)
			model.eval()
			lowerCAmelCase  :     Dict       						= torch.randint(0		,   1		,   (self.batch_size, self.image_size, self.image_size)						).to(a_						)
			lowerCAmelCase  :     Optional[int]       						= model(a_		,   labels=a_						)
			self.parent.assertGreater(result.loss		,   0.0						)





	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     List[str]       						= self.prepare_config_and_inputs()
			lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  :     Optional[Any]       						= config_and_inputs
			lowerCAmelCase  :     List[str]       						= {"pixel_values": pixel_values}
			return config, inputs_dict






@require_torch
class 		lowerCamelCase	(   _A   , _A   , unittest.TestCase							):
	snake_case_   = (
	    (
	        SegformerModel,
	        SegformerForSemanticSegmentation,
	        SegformerForImageClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case_   = (
	    {
	        "feature-extraction": SegformerModel,
	        "image-classification": SegformerForImageClassification,
	        "image-segmentation": SegformerForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	snake_case_   = True
	snake_case_   = False
	snake_case_   = False
	snake_case_   = False



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[Any]       						= SegformerModelTester(self						)
			lowerCAmelCase  :     Optional[int]       						= SegformerConfigTester(self		,   config_class=a_						)



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



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Dict       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Optional[int]       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_binary_image_segmentation(*a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase  :     Dict       						= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_image_segmentation(*a_						)



	@unittest.skip("SegFormer does not use inputs_embeds"						)
	def 	_lowerCamelCase     ( self						):
			pass



	@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"						)
	def 	_lowerCamelCase     ( self						):
			pass



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase						,		lowerCAmelCase  :     Optional[Any]       						= self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCAmelCase  :     Optional[int]       						= model_class(a_						)
					lowerCAmelCase  :     Optional[int]       						= inspect.signature(model.forward						)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					lowerCAmelCase  :     Optional[int]       						= [*signature.parameters.keys()]

					lowerCAmelCase  :     List[Any]       						= ["pixel_values"]
					self.assertListEqual(arg_names[:1]		,   a_						)



	def 	_lowerCamelCase     ( self						):
			lowerCAmelCase						,		lowerCAmelCase  :     List[str]       						= self.model_tester.prepare_config_and_inputs_for_common()
			lowerCAmelCase  :     List[str]       						= True

			for model_class in self.all_model_classes:
					lowerCAmelCase  :     Any       						= True
					lowerCAmelCase  :     str       						= False
					lowerCAmelCase  :     Dict       						= True
					lowerCAmelCase  :     Dict       						= model_class(a_						)
					model.to(a_						)
					model.eval()
					with torch.no_grad():
							lowerCAmelCase  :     Optional[int]       						= model(**self._prepare_for_class(a_		,   a_						)						)
					lowerCAmelCase  :     Optional[Any]       						= outputs.attentions

					lowerCAmelCase  :     List[Any]       						= sum(self.model_tester.depths						)
					self.assertEqual(len(a_						)		,   a_						)

					# check that output_attentions also work using config
					del inputs_dict["output_attentions"]
					lowerCAmelCase  :     Union[str, Any]       						= True
					lowerCAmelCase  :     Optional[int]       						= model_class(a_						)
					model.to(a_						)
					model.eval()
					with torch.no_grad():
							lowerCAmelCase  :     int       						= model(**self._prepare_for_class(a_		,   a_						)						)
					lowerCAmelCase  :     Optional[int]       						= outputs.attentions

					self.assertEqual(len(a_						)		,   a_						)

					# verify the first attentions (first block, first layer)
					lowerCAmelCase  :     List[Any]       						= (self.model_tester.image_size // 4) ** 2
					lowerCAmelCase  :     List[Any]       						= (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
					self.assertListEqual(
					    list(attentions[0].shape[-3:]						)		,   [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,   )

					# verify the last attentions (last block, last layer)
					lowerCAmelCase  :     Dict       						= (self.model_tester.image_size // 32) ** 2
					lowerCAmelCase  :     Dict       						= (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
					self.assertListEqual(
					    list(attentions[-1].shape[-3:]						)		,   [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]		,   )
					lowerCAmelCase  :     str       						= len(a_						)

					# Check attention is always last and order is fine
					lowerCAmelCase  :     Any       						= True
					lowerCAmelCase  :     List[Any]       						= True
					lowerCAmelCase  :     List[Any]       						= model_class(a_						)
					model.to(a_						)
					model.eval()
					with torch.no_grad():
							lowerCAmelCase  :     Any       						= model(**self._prepare_for_class(a_		,   a_						)						)

					self.assertEqual(out_len + 1		,   len(a_						)						)

					lowerCAmelCase  :     int       						= outputs.attentions

					self.assertEqual(len(a_						)		,   a_						)
					# verify the first attentions (first block, first layer)
					lowerCAmelCase  :     Union[str, Any]       						= (self.model_tester.image_size // 4) ** 2
					lowerCAmelCase  :     Optional[int]       						= (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
					self.assertListEqual(
					    list(self_attentions[0].shape[-3:]						)		,   [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]		,   )



	def 	_lowerCamelCase     ( self						):
			def check_hidden_states_output(a_		,   a_		,   a_						):
					lowerCAmelCase  :     int       						= model_class(a_						)
					model.to(a_						)
					model.eval()

					with torch.no_grad():
							lowerCAmelCase  :     Optional[Any]       						= model(**self._prepare_for_class(a_		,   a_						)						)

					lowerCAmelCase  :     Optional[int]       						= outputs.hidden_states

					lowerCAmelCase  :     Tuple       						= self.model_tester.num_encoder_blocks
					self.assertEqual(len(a_						)		,   a_						)

					# verify the first hidden states (first block)
					self.assertListEqual(
					    list(hidden_states[0].shape[-3:]						)		,   [
					        self.model_tester.hidden_sizes[0],
					        self.model_tester.image_size // 4,
					        self.model_tester.image_size // 4,
					    ]		,   )

			lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= self.model_tester.prepare_config_and_inputs_for_common()

			for model_class in self.all_model_classes:
					lowerCAmelCase  :     Tuple       						= True
					check_hidden_states_output(a_		,   a_		,   a_						)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					lowerCAmelCase  :     List[Any]       						= True

					check_hidden_states_output(a_		,   a_		,   a_						)



	def 	_lowerCamelCase     ( self						):
			if not self.model_tester.is_training:
					return

			lowerCAmelCase						,		lowerCAmelCase  :     Optional[int]       						= self.model_tester.prepare_config_and_inputs_for_common()
			lowerCAmelCase  :     List[Any]       						= True

			for model_class in self.all_model_classes:
					if model_class in get_values(a_						):
							continue

					lowerCAmelCase  :     Optional[Any]       						= model_class(a_						)
					model.to(a_						)
					model.train()
					lowerCAmelCase  :     Union[str, Any]       						= self._prepare_for_class(a_		,   a_		,   return_labels=a_						)
					lowerCAmelCase  :     Optional[Any]       						= model(**a_						).loss
					loss.backward()



	@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
	def 	_lowerCamelCase     ( self						):
			pass





	@slow
	def 	_lowerCamelCase     ( self						):
			for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase  :     Any       						= SegformerModel.from_pretrained(a_						)
					self.assertIsNotNone(a_						)






def   __A    (				):
		lowerCAmelCase  :     Optional[Any]       						= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
		return image

@require_torch
class 		lowerCamelCase	(   unittest.TestCase							):



	@slow
	def 	_lowerCamelCase     ( self						):
			# only resize + normalize
			lowerCAmelCase  :     int       						= SegformerImageProcessor(
			    image_scale=(512, 512)		,   keep_ratio=a_		,   align=a_		,   do_random_crop=a_						)
			lowerCAmelCase  :     str       						= SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
			    a_						)

			lowerCAmelCase  :     List[Any]       						= prepare_img()
			lowerCAmelCase  :     Any       						= image_processor(images=a_		,   return_tensors="pt"						)
			lowerCAmelCase  :     Optional[int]       						= encoded_inputs.pixel_values.to(a_						)

			with torch.no_grad():
					lowerCAmelCase  :     Union[str, Any]       						= model(a_						)

			lowerCAmelCase  :     Union[str, Any]       						= torch.Size((1, model.config.num_labels, 128, 128)						)
			self.assertEqual(outputs.logits.shape		,   a_						)

			lowerCAmelCase  :     Optional[Any]       						= torch.tensor(
			    [
			        [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
			        [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
			        [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
			    ]						).to(a_						)
			self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,   a_		,   atol=1e-4						)						)



	@slow
	def 	_lowerCamelCase     ( self						):
			# only resize + normalize
			lowerCAmelCase  :     Tuple       						= SegformerImageProcessor(
			    image_scale=(512, 512)		,   keep_ratio=a_		,   align=a_		,   do_random_crop=a_						)
			lowerCAmelCase  :     Dict       						= SegformerForSemanticSegmentation.from_pretrained(
			    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"						).to(a_						)

			lowerCAmelCase  :     Optional[Any]       						= prepare_img()
			lowerCAmelCase  :     Any       						= image_processor(images=a_		,   return_tensors="pt"						)
			lowerCAmelCase  :     Tuple       						= encoded_inputs.pixel_values.to(a_						)

			with torch.no_grad():
					lowerCAmelCase  :     Any       						= model(a_						)

			lowerCAmelCase  :     Optional[Any]       						= torch.Size((1, model.config.num_labels, 128, 128)						)
			self.assertEqual(outputs.logits.shape		,   a_						)

			lowerCAmelCase  :     str       						= torch.tensor(
			    [
			        [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
			        [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
			        [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
			    ]						).to(a_						)
			self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]		,   a_		,   atol=1e-1						)						)





	@slow
	def 	_lowerCamelCase     ( self						):
			# only resize + normalize
			lowerCAmelCase  :     List[str]       						= SegformerImageProcessor(
			    image_scale=(512, 512)		,   keep_ratio=a_		,   align=a_		,   do_random_crop=a_						)
			lowerCAmelCase  :     int       						= SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"						).to(
			    a_						)

			lowerCAmelCase  :     str       						= prepare_img()
			lowerCAmelCase  :     Optional[int]       						= image_processor(images=a_		,   return_tensors="pt"						)
			lowerCAmelCase  :     Optional[Any]       						= encoded_inputs.pixel_values.to(a_						)

			with torch.no_grad():
					lowerCAmelCase  :     Tuple       						= model(a_						)

			lowerCAmelCase  :     Dict       						= outputs.logits.detach().cpu()

			lowerCAmelCase  :     List[Any]       						= image_processor.post_process_semantic_segmentation(outputs=a_		,   target_sizes=[(500, 300)]						)
			lowerCAmelCase  :     Dict       						= torch.Size((500, 300)						)
			self.assertEqual(segmentation[0].shape		,   a_						)

			lowerCAmelCase  :     Optional[int]       						= image_processor.post_process_semantic_segmentation(outputs=a_						)
			lowerCAmelCase  :     Union[str, Any]       						= torch.Size((128, 128)						)
			self.assertEqual(segmentation[0].shape		,   a_						)










'''simple docstring'''






import json
import os
import shutil
import warnings
from argparse import ArgumentParser, Namespace
from pathlib import Path
from typing import List

from ..utils import logging
from . import BaseTransformersCLICommand


try:
					from cookiecutter.main import cookiecutter

					lowerCAmelCase									=   True
except ImportError:
					lowerCAmelCase									=   False

lowerCAmelCase									=   logging.get_logger(__name__)  # pylint: disable=invalid-name






def   __A    (				a_    :	Namespace						):
		return AddNewModelCommand(args.testing   ,args.testing_file   ,path=args.path						)

class 		lowerCamelCase	(   _A							):



	@staticmethod
	def 	_lowerCamelCase     ( a_						):
			lowerCAmelCase  :     Optional[int]       						= parser.add_parser("add-new-model"						)
			add_new_model_parser.add_argument("--testing"		,   action="store_true"		,   help="If in testing mode."						)
			add_new_model_parser.add_argument("--testing_file"		,   type=a_		,   help="Configuration file on which to run."						)
			add_new_model_parser.add_argument(
			    "--path"		,   type=a_		,   help="Path to cookiecutter. Should only be used for testing purposes."						)
			add_new_model_parser.set_defaults(func=a_						)



	def __init__( self		,   a_		,   a_		,   a_=None		,   *a_						):
			lowerCAmelCase  :     Any       						= testing
			lowerCAmelCase  :     str       						= testing_file
			lowerCAmelCase  :     Optional[int]       						= path





	def 	_lowerCamelCase     ( self						):
			warnings.warn(
			    "The command `transformers-cli add-new-model` is deprecated and will be removed in v5 of Transformers. "
			    "It is not actively maintained anymore, so might give a result that won't pass all tests and quality "
			    "checks, you should use `transformers-cli add-new-model-like` instead."						)
			if not _has_cookiecutter:
					raise ImportError(
					    "Model creation dependencies are required to use the `add_new_model` command. Install them by running "
					    "the following at the root of your `transformers` clone:\n\n\t$ pip install -e .[modelcreation]\n"						)
			# Ensure that there is no other `cookiecutter-template-xxx` directory in the current working directory
			lowerCAmelCase  :     Optional[Any]       						= [directory for directory in os.listdir() if "cookiecutter-template-" == directory[:22]]
			if len(a_						) > 0:
					raise ValueError(
					    "Several directories starting with `cookiecutter-template-` in current working directory. "
					    "Please clean your directory by removing all folders starting with `cookiecutter-template-` or "
					    "change your working directory."						)

			lowerCAmelCase  :     List[str]       						= (
			    Path(a_						).parent.parent.parent.parent if self._path is None else Path(self._path						).parent.parent
			)
			lowerCAmelCase  :     str       						= path_to_transformer_root / "templates" / "adding_a_new_model"

			# Execute cookiecutter
			if not self._testing:
					cookiecutter(str(a_						)						)
			else:
					with open(self._testing_file		,   "r"						) as configuration_file:
							lowerCAmelCase  :     Dict       						= json.load(a_						)

					cookiecutter(
					    str(path_to_cookiecutter if self._path is None else self._path						)		,   no_input=a_		,   extra_context=a_		,   )

			lowerCAmelCase  :     Optional[Any]       						= [directory for directory in os.listdir() if "cookiecutter-template-" in directory[:22]][0]

			# Retrieve configuration
			with open(directory + "/configuration.json"		,   "r"						) as configuration_file:
					lowerCAmelCase  :     str       						= json.load(a_						)

			lowerCAmelCase  :     List[str]       						= configuration["lowercase_modelname"]
			lowerCAmelCase  :     List[Any]       						= configuration["generate_tensorflow_pytorch_and_flax"]
			os.remove(F'''{directory}/configuration.json'''						)

			lowerCAmelCase  :     Optional[int]       						= "PyTorch" in generate_tensorflow_pytorch_and_flax
			lowerCAmelCase  :     Optional[int]       						= "TensorFlow" in generate_tensorflow_pytorch_and_flax
			lowerCAmelCase  :     Optional[Any]       						= "Flax" in generate_tensorflow_pytorch_and_flax

			lowerCAmelCase  :     List[str]       						= F'''{path_to_transformer_root}/src/transformers/models/{lowercase_model_name}'''
			os.makedirs(a_		,   exist_ok=a_						)
			os.makedirs(F'''{path_to_transformer_root}/tests/models/{lowercase_model_name}'''		,   exist_ok=a_						)

			# Tests require submodules as they have parent imports
			with open(F'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/__init__.py'''		,   "w"						):
					pass

			shutil.move(
			    F'''{directory}/__init__.py'''		,   F'''{model_dir}/__init__.py'''		,   )
			shutil.move(
			    F'''{directory}/configuration_{lowercase_model_name}.py'''		,   F'''{model_dir}/configuration_{lowercase_model_name}.py'''		,   )

			def remove_copy_lines(a_						):
					with open(a_		,   "r"						) as f:
							lowerCAmelCase  :     Dict       						= f.readlines()
					with open(a_		,   "w"						) as f:
							for line in lines:
									if "# Copied from transformers." not in line:
											f.write(a_						)

			if output_pytorch:
					if not self._testing:
							remove_copy_lines(F'''{directory}/modeling_{lowercase_model_name}.py'''						)

					shutil.move(
					    F'''{directory}/modeling_{lowercase_model_name}.py'''		,   F'''{model_dir}/modeling_{lowercase_model_name}.py'''		,   )

					shutil.move(
					    F'''{directory}/test_modeling_{lowercase_model_name}.py'''		,   F'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_{lowercase_model_name}.py'''		,   )
			else:
					os.remove(F'''{directory}/modeling_{lowercase_model_name}.py'''						)
					os.remove(F'''{directory}/test_modeling_{lowercase_model_name}.py'''						)

			if output_tensorflow:
					if not self._testing:
							remove_copy_lines(F'''{directory}/modeling_tf_{lowercase_model_name}.py'''						)

					shutil.move(
					    F'''{directory}/modeling_tf_{lowercase_model_name}.py'''		,   F'''{model_dir}/modeling_tf_{lowercase_model_name}.py'''		,   )

					shutil.move(
					    F'''{directory}/test_modeling_tf_{lowercase_model_name}.py'''		,   F'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_tf_{lowercase_model_name}.py'''		,   )
			else:
					os.remove(F'''{directory}/modeling_tf_{lowercase_model_name}.py'''						)
					os.remove(F'''{directory}/test_modeling_tf_{lowercase_model_name}.py'''						)

			if output_flax:
					if not self._testing:
							remove_copy_lines(F'''{directory}/modeling_flax_{lowercase_model_name}.py'''						)

					shutil.move(
					    F'''{directory}/modeling_flax_{lowercase_model_name}.py'''		,   F'''{model_dir}/modeling_flax_{lowercase_model_name}.py'''		,   )

					shutil.move(
					    F'''{directory}/test_modeling_flax_{lowercase_model_name}.py'''		,   F'''{path_to_transformer_root}/tests/models/{lowercase_model_name}/test_modeling_flax_{lowercase_model_name}.py'''		,   )
			else:
					os.remove(F'''{directory}/modeling_flax_{lowercase_model_name}.py'''						)
					os.remove(F'''{directory}/test_modeling_flax_{lowercase_model_name}.py'''						)

			shutil.move(
			    F'''{directory}/{lowercase_model_name}.md'''		,   F'''{path_to_transformer_root}/docs/source/en/model_doc/{lowercase_model_name}.md'''		,   )

			shutil.move(
			    F'''{directory}/tokenization_{lowercase_model_name}.py'''		,   F'''{model_dir}/tokenization_{lowercase_model_name}.py'''		,   )

			shutil.move(
			    F'''{directory}/tokenization_fast_{lowercase_model_name}.py'''		,   F'''{model_dir}/tokenization_{lowercase_model_name}_fast.py'''		,   )

			from os import fdopen, remove
			from shutil import copymode, move
			from tempfile import mkstemp

			def replace(a_		,   a_		,   a_						):
					# Create temp file
					lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= mkstemp()
					lowerCAmelCase  :     int       						= False
					with fdopen(a_		,   "w"						) as new_file:
							with open(a_						) as old_file:
									for line in old_file:
											new_file.write(a_						)
											if line_to_copy_below in line:
													lowerCAmelCase  :     Optional[int]       						= True
													for line_to_copy in lines_to_copy:
															new_file.write(a_						)

					if not line_found:
							raise ValueError(F'''Line {line_to_copy_below} was not found in file.'''						)

					# Copy the file permissions from the old file to the new file
					copymode(a_		,   a_						)
					# Remove original file
					remove(a_						)
					# Move new file
					move(a_		,   a_						)

			def skip_units(a_						):
					return (
					    ("generating PyTorch" in line and not output_pytorch)
					    or ("generating TensorFlow" in line and not output_tensorflow)
					    or ("generating Flax" in line and not output_flax)
					)

			def replace_in_files(a_						):
					with open(a_						) as datafile:
							lowerCAmelCase  :     Dict       						= []
							lowerCAmelCase  :     Optional[int]       						= False
							lowerCAmelCase  :     List[Any]       						= False
							for line in datafile:
									if "# To replace in: " in line and "##" not in line:
											lowerCAmelCase  :     Optional[int]       						= line.split("\""						)[1]
											lowerCAmelCase  :     Tuple       						= skip_units(a_						)
									elif "# Below: " in line and "##" not in line:
											lowerCAmelCase  :     Any       						= line.split("\""						)[1]
											lowerCAmelCase  :     List[str]       						= skip_units(a_						)
									elif "# End." in line and "##" not in line:
											if not skip_file and not skip_snippet:
													replace(a_		,   a_		,   a_						)

											lowerCAmelCase  :     List[str]       						= []
									elif "# Replace with" in line and "##" not in line:
											lowerCAmelCase  :     Any       						= []
									elif "##" not in line:
											lines_to_copy.append(a_						)

					remove(a_						)

			replace_in_files(F'''{directory}/to_replace_{lowercase_model_name}.py'''						)
			os.rmdir(a_						)



