




'''simple docstring'''


import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

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 torch import nn

  from transformers import (
      MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
      MODEL_MAPPING,
      DeiTForImageClassification,
      DeiTForImageClassificationWithTeacher,
      DeiTForMaskedImageModeling,
      DeiTModel,
  )
  from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import DeiTImageProcessor





class    __lowercase						:







   def __init__(	self      ,							UpperCamelCase      ,							UpperCamelCase=13      ,							UpperCamelCase=30      ,							UpperCamelCase=2      ,							UpperCamelCase=3      ,							UpperCamelCase=True      ,							UpperCamelCase=True      ,							UpperCamelCase=32      ,							UpperCamelCase=5      ,							UpperCamelCase=4      ,							UpperCamelCase=37      ,							UpperCamelCase="gelu"      ,							UpperCamelCase=0.1      ,							UpperCamelCase=0.1      ,							UpperCamelCase=10      ,							UpperCamelCase=0.02      ,							UpperCamelCase=3      ,							UpperCamelCase=None      ,							UpperCamelCase=2      ,							)				->    Tuple:
     __a			=					parent
     __a			=					batch_size
     __a			=					image_size
     __a			=					patch_size
     __a			=					num_channels
     __a			=					is_training
     __a			=					use_labels
     __a			=					hidden_size
     __a			=					num_hidden_layers
     __a			=					num_attention_heads
     __a			=					intermediate_size
     __a			=					hidden_act
     __a			=					hidden_dropout_prob
     __a			=					attention_probs_dropout_prob
     __a			=					type_sequence_label_size
     __a			=					initializer_range
     __a			=					scope
     __a			=					encoder_stride

     # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
     __a			=					(image_size // patch_size) ** 2
     __a			=					num_patches + 2







   def 		UpperCamelCase__			(	self      )				->    str:
     __a			=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

     __a			=					None
     if self.use_labels:
       __a			=					ids_tensor([self.batch_size]      ,							self.type_sequence_label_size      )

     __a			=					self.get_config()

     return config, pixel_values, labels







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     return DeiTConfig(
         image_size=self.image_size      ,							patch_size=self.patch_size      ,							num_channels=self.num_channels      ,							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      ,							is_decoder=UpperCamelCase      ,							initializer_range=self.initializer_range      ,							encoder_stride=self.encoder_stride      ,							)







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[Any]:
     __a			=					DeiTModel(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.last_hidden_state.shape      ,							(self.batch_size, self.seq_length, self.hidden_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[Any]:
     __a			=					DeiTForMaskedImageModeling(config=UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(
         result.reconstruction.shape      ,							(self.batch_size, self.num_channels, self.image_size, self.image_size)      )

     # test greyscale images
     __a			=					1
     __a			=					DeiTForMaskedImageModeling(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
     __a			=					model(UpperCamelCase      )
     self.parent.assertEqual(result.reconstruction.shape      ,							(self.batch_size, 1, self.image_size, self.image_size)      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase      )				->    Optional[int]:
     __a			=					self.type_sequence_label_size
     __a			=					DeiTForImageClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()
     __a			=					model(UpperCamelCase      ,							labels=UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.type_sequence_label_size)      )

     # test greyscale images
     __a			=					1
     __a			=					DeiTForImageClassification(UpperCamelCase      )
     model.to(UpperCamelCase      )
     model.eval()

     __a			=					floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
     __a			=					model(UpperCamelCase      ,							labels=UpperCamelCase      )
     self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.type_sequence_label_size)      )







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a			=					self.prepare_config_and_inputs()
     (
         (
         __a
     )	,       (
         __a
     )	,       (
         __a
     )	,       
     )			=					config_and_inputs
     __a			=					{'pixel_values': pixel_values}
     return config, inputs_dict





@require_torch
class    __lowercase						(  __magic_name__       ,							__magic_name__       ,							unittest.TestCase ):
   _a							=      (
       (
           DeiTModel,
           DeiTForImageClassification,
           DeiTForImageClassificationWithTeacher,
           DeiTForMaskedImageModeling,
       )
       if is_torch_available()
       else ()
   )
   _a							=      (
       {
           """feature-extraction""": DeiTModel,
           """image-classification""": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
       }
       if is_torch_available()
       else {}
   )

   _a							=      False
   _a							=      False
   _a							=      False







   def 		UpperCamelCase__			(	self      )				->    int:
     __a			=					DeiTModelTester(self      )
     __a			=					ConfigTester(self      ,							config_class=UpperCamelCase      ,							has_text_modality=UpperCamelCase      ,							hidden_size=37      )







   def 		UpperCamelCase__			(	self      )				->    Union[str, Any]:
     self.config_tester.run_common_tests()







   @unittest.skip(reason='DeiT does not use inputs_embeds'      )
   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     pass







   def 		UpperCamelCase__			(	self      )				->    Tuple:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       __a			=					model_class(UpperCamelCase      )
       self.assertIsInstance(model.get_input_embeddings()      ,							(nn.Module)      )
       __a			=					model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(UpperCamelCase      ,							nn.Linear      )      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

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

       __a			=					['pixel_values']
       self.assertListEqual(arg_names[:1]      ,							UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[int]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Any:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a			=					self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*UpperCamelCase      )







   def 		UpperCamelCase__			(	self      ,							UpperCamelCase      ,							UpperCamelCase      ,							UpperCamelCase=False      )				->    str:
     __a			=					super()._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      ,							return_labels=UpperCamelCase      )

     if return_labels:
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
         del inputs_dict["labels"]

     return inputs_dict







   def 		UpperCamelCase__			(	self      )				->    Dict:
     if not self.model_tester.is_training:
       return

     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()
     __a			=					True

     for model_class in self.all_model_classes:
       # DeiTForImageClassificationWithTeacher supports inference-only
       if (
           model_class in get_values(UpperCamelCase      )
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
         continue
       __a			=					model_class(UpperCamelCase      )
       model.to(UpperCamelCase      )
       model.train()
       __a			=					self._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      ,							return_labels=UpperCamelCase      )
       __a			=					model(**UpperCamelCase      ).loss
       loss.backward()







   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()
     if not self.model_tester.is_training:
       return

     __a			=					False
     __a			=					True

     for model_class in self.all_model_classes:
       if model_class in get_values(UpperCamelCase      ) or not model_class.supports_gradient_checkpointing:
         continue
       # DeiTForImageClassificationWithTeacher supports inference-only
       if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
         continue
       __a			=					model_class(UpperCamelCase      )
       model.gradient_checkpointing_enable()
       model.to(UpperCamelCase      )
       model.train()
       __a			=					self._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      ,							return_labels=UpperCamelCase      )
       __a			=					model(**UpperCamelCase      ).loss
       loss.backward()







   def 		UpperCamelCase__			(	self      )				->    List[Any]:
     __a	,       __a			=					self.model_tester.prepare_config_and_inputs_for_common()

     __a			=					[
         {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
         {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
         {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
     ]

     for model_class in self.all_model_classes:
       if (
           model_class
           not in [
               *get_values(UpperCamelCase      ),
               *get_values(UpperCamelCase      ),
           ]
           or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
       ):
         continue

       for problem_type in problem_types:
         with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}"      ):
           __a			=					problem_type['title']
           __a			=					problem_type['num_labels']

           __a			=					model_class(UpperCamelCase      )
           model.to(UpperCamelCase      )
           model.train()

           __a			=					self._prepare_for_class(UpperCamelCase      ,							UpperCamelCase      ,							return_labels=UpperCamelCase      )

           if problem_type["num_labels"] > 1:
             __a			=					inputs['labels'].unsqueeze(1      ).repeat(1      ,							problem_type['num_labels']      )

           __a			=					inputs['labels'].to(problem_type['dtype']      )

           # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
           # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
           # they have the same size." which is a symptom something in wrong for the regression problem.
           # See https://github.com/huggingface/transformers/issues/11780
           with warnings.catch_warnings(record=UpperCamelCase      ) as warning_list:
             __a			=					model(**UpperCamelCase      ).loss
           for w in warning_list:
             if "Using a target size that is different to the input size" in str(w.message      ):
               raise ValueError(
                   f"Something is going wrong in the regression problem: intercepted {w.message}"      )

           loss.backward()







   @slow
   def 		UpperCamelCase__			(	self      )				->    Any:
     for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __a			=					DeiTModel.from_pretrained(UpperCamelCase      )
       self.assertIsNotNone(UpperCamelCase      )


def 						SCREAMING_SNAKE_CASE       (      ):
  __a			=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
  return image





@require_torch
@require_vision
class    __lowercase						(  unittest.TestCase ):







   @cached_property
   def 		UpperCamelCase__			(	self      )				->    int:
     return (
         DeiTImageProcessor.from_pretrained('facebook/deit-base-distilled-patch16-224'      )
         if is_vision_available()
         else None
     )







   @slow
   def 		UpperCamelCase__			(	self      )				->    Optional[Any]:
     __a			=					DeiTForImageClassificationWithTeacher.from_pretrained('facebook/deit-base-distilled-patch16-224'      ).to(
         UpperCamelCase      )

     __a			=					self.default_image_processor
     __a			=					prepare_img()
     __a			=					image_processor(images=UpperCamelCase      ,							return_tensors='pt'      ).to(UpperCamelCase      )

     # forward pass
     with torch.no_grad():
       __a			=					model(**UpperCamelCase      )

     # verify the logits
     __a			=					torch.Size((1, 1000)      )
     self.assertEqual(outputs.logits.shape      ,							UpperCamelCase      )

     __a			=					torch.tensor([-1.0_266, 0.1_912, -1.2_861]      ).to(UpperCamelCase      )

     self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,							UpperCamelCase      ,							atol=1e-4      )      )







   @slow
   @require_accelerate
   @require_torch_gpu
   def 		UpperCamelCase__			(	self      )				->    List[str]:
     __a			=					DeiTModel.from_pretrained(
         'facebook/deit-base-distilled-patch16-224'      ,							torch_dtype=torch.floataa      ,							device_map='auto'      )
     __a			=					self.default_image_processor

     __a			=					prepare_img()
     __a			=					image_processor(images=UpperCamelCase      ,							return_tensors='pt'      )
     __a			=					inputs.pixel_values.to(UpperCamelCase      )

     # forward pass to make sure inference works in fp16
     with torch.no_grad():
       __a			=					model(UpperCamelCase      )









'''simple docstring'''


# Usage:
# ./gen-card-allenai-wmt16.py

import os
from pathlib import Path


def 						SCREAMING_SNAKE_CASE       (      a_	:							Optional[Any]				,					a_	:							int				,					a_	:							Tuple				,					a_	:							Tuple			):
  __a			=					{
      'en': 'Machine learning is great, isn\'t it?',
      'ru': 'Машинное обучение - это здорово, не так ли?',
      'de': 'Maschinelles Lernen ist großartig, nicht wahr?',
  }

  # BLUE scores as follows:
  # "pair": [fairseq, transformers]
  __a			=					{
      'wmt16-en-de-dist-12-1': [28.3, 27.52],
      'wmt16-en-de-dist-6-1': [27.4, 27.11],
      'wmt16-en-de-12-1': [26.9, 25.75],
  }
  __a			=					f"{src_lang}-{tgt_lang}"

  __a			=					f"\n---\nlanguage:\n- {src_lang}\n- {tgt_lang}\nthumbnail:\ntags:\n- translation\n- wmt16\n- allenai\nlicense: apache-2.0\ndatasets:\n- wmt16\nmetrics:\n- bleu\n---\n\n# FSMT\n\n## Model description\n\nThis is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.\n\nFor more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).\n\nAll 3 models are available:\n\n* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)\n* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)\n* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)\n\n\n## Intended uses & limitations\n\n#### How to use\n\n```python\nfrom transformers import FSMTForConditionalGeneration, FSMTTokenizer\nmname = \"allenai/{model_name}\"\ntokenizer = FSMTTokenizer.from_pretrained(mname)\nmodel = FSMTForConditionalGeneration.from_pretrained(mname)\n\ninput = \"{texts[src_lang]}\"\ninput_ids = tokenizer.encode(input, return_tensors=\"pt\")\noutputs = model.generate(input_ids)\ndecoded = tokenizer.decode(outputs[0], skip_special_tokens=True)\nprint(decoded) # {texts[tgt_lang]}\n\n```\n\n#### Limitations and bias\n\n\n## Training data\n\nPretrained weights were left identical to the original model released by allenai. For more details, please, see the [paper](https://arxiv.org/abs/2006.10369).\n\n## Eval results\n\nHere are the BLEU scores:\n\nmodel   | fairseq | transformers\n-------|---------|----------\n{model_name}  | {scores[model_name][0]} | {scores[model_name][1]}\n\nThe score is slightly below the score reported in the paper, as the researchers don't use `sacrebleu` and measure the score on tokenized outputs. `transformers` score was measured using `sacrebleu` on detokenized outputs.\n\nThe score was calculated using this code:\n\n```bash\ngit clone https://github.com/huggingface/transformers\ncd transformers\nexport PAIR={pair}\nexport DATA_DIR=data/$PAIR\nexport SAVE_DIR=data/$PAIR\nexport BS=8\nexport NUM_BEAMS=5\nmkdir -p $DATA_DIR\nsacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source\nsacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target\necho $PAIR\nPYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py allenai/{model_name} $DATA_DIR/val.source $SAVE_DIR/test_translations.txt --reference_path $DATA_DIR/val.target --score_path $SAVE_DIR/test_bleu.json --bs $BS --task translation --num_beams $NUM_BEAMS\n```\n\n## Data Sources\n\n- [training, etc.](http://www.statmt.org/wmt16/)\n- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)\n\n\n### BibTeX entry and citation info\n\n```\n@misc{{kasai2020deep,\n    title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},\n    author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},\n    year={{2020}},\n    eprint={{2006.10369}},\n    archivePrefix={{arXiv}},\n    primaryClass={{cs.CL}}\n}}\n```\n\n"
  model_card_dir.mkdir(parents=a_				,					exist_ok=a_			)
  __a			=					os.path.join(a_				,					'README.md'			)
  print(f"Generating {path}"			)
  with open(a_				,					'w'				,					encoding='utf-8'			) as f:
    f.write(a_			)


# make sure we are under the root of the project
UpperCAmelCase_ 		=      Path(__file__).resolve().parent.parent.parent
UpperCAmelCase_ 		=      repo_dir / "model_cards"

for model_name in ["wmt16-en-de-dist-12-1", "wmt16-en-de-dist-6-1", "wmt16-en-de-12-1"]:
  UpperCAmelCase_ 		=      model_cards_dir / "allenai" / model_name
  write_model_card(model_card_dir, src_lang="en", tgt_lang="de", model_name=model_name)



