

'''simple docstring'''

import os
from pathlib import Path




def       UpperCamelCase(     UpperCAmelCase_  ,       UpperCAmelCase_  ,       UpperCAmelCase_	):
 UpperCAmelCase       :	Dict											= {
     """en""": """Machine learning is great, isn't it?""",
     """ru""": """Машинное обучение - это здорово, не так ли?""",
     """de""": """Maschinelles Lernen ist großartig, oder?""",
 }

 # BLUE scores as follows:
 # "pair": [fairseq, transformers]
 UpperCAmelCase       :	str											= {
     """ru-en""": ["""[41.3](http://matrix.statmt.org/matrix/output/1907?run_id=6937)""", """39.20"""],
     """en-ru""": ["""[36.4](http://matrix.statmt.org/matrix/output/1914?run_id=6724)""", """33.47"""],
     """en-de""": ["""[43.1](http://matrix.statmt.org/matrix/output/1909?run_id=6862)""", """42.83"""],
     """de-en""": ["""[42.3](http://matrix.statmt.org/matrix/output/1902?run_id=6750)""", """41.35"""],
 }
 UpperCAmelCase       :	int											= F"""{src_lang}-{tgt_lang}"""

 UpperCAmelCase       :	Optional[int]											= F"""
---
language: 
- {src_lang}
- {tgt_lang}
thumbnail:
tags:
- translation
- wmt19
- facebook
license: apache-2.0
datasets:
- wmt19
metrics:
- bleu
---

# FSMT

## Model description

This is a ported version of [fairseq wmt19 transformer](https://github.com/pytorch/fairseq/blob/master/examples/wmt19/README.md) for {src_lang}-{tgt_lang}.

For more details, please see, [Facebook FAIR\'s WMT19 News Translation Task Submission](https://arxiv.org/abs/1907.06616).

The abbreviation FSMT stands for FairSeqMachineTranslation

All four models are available:

* [wmt19-en-ru](https://huggingface.co/facebook/wmt19-en-ru)
* [wmt19-ru-en](https://huggingface.co/facebook/wmt19-ru-en)
* [wmt19-en-de](https://huggingface.co/facebook/wmt19-en-de)
* [wmt19-de-en](https://huggingface.co/facebook/wmt19-de-en)

## Intended uses & limitations

#### How to use

```python
from transformers import FSMTForConditionalGeneration, FSMTTokenizer
mname = \"facebook/wmt19-{src_lang}-{tgt_lang}\"
tokenizer = FSMTTokenizer.from_pretrained(mname)
model = FSMTForConditionalGeneration.from_pretrained(mname)

input = \"{texts[src_lang]}\"
input_ids = tokenizer.encode(input, return_tensors=\"pt\")
outputs = model.generate(input_ids)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(decoded) # {texts[tgt_lang]}

```

#### Limitations and bias

- The original (and this ported model) doesn\'t seem to handle well inputs with repeated sub-phrases, [content gets truncated](https://discuss.huggingface.co/t/issues-with-translating-inputs-containing-repeated-phrases/981)

## Training data

Pretrained weights were left identical to the original model released by fairseq. For more details, please, see the [paper](https://arxiv.org/abs/1907.06616).

## Eval results

pair   | fairseq | transformers
-------|---------|----------
{pair}  | {scores[pair][0]} | {scores[pair][1]}

The score is slightly below the score reported by `fairseq`, since `transformers`` currently doesn\'t support:
- model ensemble, therefore the best performing checkpoint was ported (``model4.pt``).
- re-ranking

The score was calculated using this code:

```bash
git clone https://github.com/huggingface/transformers
cd transformers
export PAIR={pair}
export DATA_DIR=data/$PAIR
export SAVE_DIR=data/$PAIR
export BS=8
export NUM_BEAMS=15
mkdir -p $DATA_DIR
sacrebleu -t wmt19 -l $PAIR --echo src > $DATA_DIR/val.source
sacrebleu -t wmt19 -l $PAIR --echo ref > $DATA_DIR/val.target
echo $PAIR
PYTHONPATH=\"src:examples/seq2seq\" python examples/seq2seq/run_eval.py facebook/wmt19-$PAIR $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
```
note: fairseq reports using a beam of 50, so you should get a slightly higher score if re-run with `--num_beams 50`.

## Data Sources

- [training, etc.](http://www.statmt.org/wmt19/)
- [test set](http://matrix.statmt.org/test_sets/newstest2019.tgz?1556572561)


### BibTeX entry and citation info

```bibtex
@inproceedings{{...,
  year={{2020}},
  title={{Facebook FAIR\'s WMT19 News Translation Task Submission}},
  author={{Ng, Nathan and Yee, Kyra and Baevski, Alexei and Ott, Myle and Auli, Michael and Edunov, Sergey}},
  booktitle={{Proc. of WMT}},
}}
```


## TODO

- port model ensemble (fairseq uses 4 model checkpoints)

"""
 os.makedirs(_UpperCamelCase  ,       exist_ok=_UpperCamelCase	)
 UpperCAmelCase       :	Optional[Any]											= os.path.join(_UpperCamelCase  ,       'README.md'	)
 print(F"""Generating {path}"""	)
 with open(_UpperCamelCase  ,       'w'  ,       encoding='utf-8'	) as f:
  f.write(_UpperCamelCase	)


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

for model_name in ["wmt19-ru-en", "wmt19-en-ru", "wmt19-en-de", "wmt19-de-en"]:
 lowercase__			    =			model_name.split("-")
 lowercase__			    =			model_cards_dir / 'facebook' / model_name
 write_model_card(model_card_dir, src_lang=src_lang, tgt_lang=tgt_lang)








"""simple docstring"""






import argparse
import json
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    SwiftFormerConfig,
    SwiftFormerForImageClassification,
    ViTImageProcessor,
)
from transformers.utils import logging


logging.set_verbosity_info()
UpperCAmelCase    :     Dict   						=		logging.get_logger(__name__)

UpperCAmelCase    :     Union[str, Any]   						=		torch.device('cpu')
def      lowerCamelCase       (     )		->       str:

							'''simple docstring'''



							__UpperCAmelCase   :						Optional[int]      							=					"""http://images.cocodataset.org/val2017/000000039769.jpg"""
							__UpperCAmelCase   :						Optional[Any]      							=					Image.open(requests.get(_UpperCamelCase       ,			stream=_UpperCamelCase							).raw							)
							return im
def      lowerCamelCase       (     _UpperCamelCase		:      Union[str, Any]							)		->       Union[str, Any]:

							'''simple docstring'''



							if swiftformer_name == "swiftformer_xs":
														return torch.tensor([-2.1_703E00, 2.1_107E00, -2.0_811E00, 8.8_685E-01, 2.4_360E-01]							)

							elif swiftformer_name == "swiftformer_s":
														return torch.tensor([3.9_636E-01, 2.3_478E-01, -1.6_963E00, -1.7_381E00, -8.6_337E-01]							)

							elif swiftformer_name == "swiftformer_l1":
														return torch.tensor([-4.2_768E-01, -4.7_429E-01, -1.0_897E00, -1.0_248E00, 3.5_523E-02]							)

							elif swiftformer_name == "swiftformer_l3":
														return torch.tensor([-2.5_330E-01, 2.4_211E-01, -6.0_185E-01, -8.2_789E-01, -6.0_446E-02]							)
def      lowerCamelCase       (     _UpperCamelCase		:      Union[str, Any]       ,			_UpperCamelCase		:      Optional[int]       ,			_UpperCamelCase		:      List[Any]							)		->       Union[str, Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Any      							=					dct.pop(_UpperCamelCase							)
							__UpperCAmelCase   :						List[str]      							=					val
def      lowerCamelCase       (     _UpperCamelCase		:      List[Any]							)		->       List[Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Tuple      							=					[]
							for k in state_dict.keys():
														__UpperCAmelCase   :						int      							=					k
														if ".pwconv" in k:
																					__UpperCAmelCase   :						Optional[int]      							=					k_new.replace(""".pwconv"""       ,			""".point_wise_conv"""							)
														if ".dwconv" in k:
																					__UpperCAmelCase   :						List[Any]      							=					k_new.replace(""".dwconv"""       ,			""".depth_wise_conv"""							)
														if ".Proj." in k:
																					__UpperCAmelCase   :						str      							=					k_new.replace(""".Proj."""       ,			""".proj."""							)
														if "patch_embed" in k_new:
																					__UpperCAmelCase   :						int      							=					k_new.replace("""patch_embed"""       ,			"""swiftformer.patch_embed.patch_embedding"""							)
														if "network" in k_new:
																					__UpperCAmelCase   :						str      							=					k_new.split("""."""							)
																					if ls[2].isdigit():
																												__UpperCAmelCase   :						Dict      							=					"""swiftformer.encoder.network.""" + ls[1] + """.blocks.""" + ls[2] + """.""" + """.""".join(ls[3:]							)
																					else:
																												__UpperCAmelCase   :						List[str]      							=					k_new.replace("""network"""       ,			"""swiftformer.encoder.network"""							)
														rename_keys.append((k, k_new)							)
							return rename_keys
@torch.no_grad()
def      lowerCamelCase       (     _UpperCamelCase		:      int       ,			_UpperCamelCase		:      List[Any]       ,			_UpperCamelCase		:      int							)		->       Union[str, Any]:

							'''simple docstring'''



							__UpperCAmelCase   :						Optional[int]      							=					SwiftFormerConfig()

							# dataset (ImageNet-21k only or also fine-tuned on ImageNet 2012), patch_size and image_size
							__UpperCAmelCase   :						Optional[int]      							=					1_0_0_0
							__UpperCAmelCase   :						Optional[int]      							=					"""huggingface/label-files"""
							__UpperCAmelCase   :						str      							=					"""imagenet-1k-id2label.json"""
							__UpperCAmelCase   :						List[Any]      							=					json.load(open(hf_hub_download(_UpperCamelCase       ,			_UpperCamelCase       ,			repo_type="""dataset"""							)       ,			"""r"""							)							)
							__UpperCAmelCase   :						Any      							=					{int(_UpperCamelCase							): v for k, v in idalabel.items()}
							__UpperCAmelCase   :						Optional[Any]      							=					idalabel
							__UpperCAmelCase   :						Optional[int]      							=					{v: k for k, v in idalabel.items()}

							# size of the architecture
							if swiftformer_name == "swiftformer_xs":
														__UpperCAmelCase   :						Tuple      							=					[3, 3, 6, 4]
														__UpperCAmelCase   :						List[Any]      							=					[4_8, 5_6, 1_1_2, 2_2_0]

							elif swiftformer_name == "swiftformer_s":
														__UpperCAmelCase   :						Optional[int]      							=					[3, 3, 9, 6]
														__UpperCAmelCase   :						Tuple      							=					[4_8, 6_4, 1_6_8, 2_2_4]

							elif swiftformer_name == "swiftformer_l1":
														__UpperCAmelCase   :						Dict      							=					[4, 3, 1_0, 5]
														__UpperCAmelCase   :						Optional[Any]      							=					[4_8, 9_6, 1_9_2, 3_8_4]

							elif swiftformer_name == "swiftformer_l3":
														__UpperCAmelCase   :						str      							=					[4, 4, 1_2, 6]
														__UpperCAmelCase   :						str      							=					[6_4, 1_2_8, 3_2_0, 5_1_2]

							# load state_dict of original model, remove and rename some keys
							if original_ckpt:
														if original_ckpt.startswith("""https"""							):
																					__UpperCAmelCase   :						str      							=					torch.hub.load_state_dict_from_url(_UpperCamelCase       ,			map_location="""cpu"""       ,			check_hash=_UpperCamelCase							)
														else:
																					__UpperCAmelCase   :						Dict      							=					torch.load(_UpperCamelCase       ,			map_location="""cpu"""							)
							__UpperCAmelCase   :						str      							=					checkpoint

							__UpperCAmelCase   :						Union[str, Any]      							=					create_rename_keys(_UpperCamelCase							)
							for rename_key_src, rename_key_dest in rename_keys:
														rename_key(_UpperCamelCase       ,			_UpperCamelCase       ,			_UpperCamelCase							)

							# load HuggingFace model
							__UpperCAmelCase   :						int      							=					SwiftFormerForImageClassification(_UpperCamelCase							).eval()
							hf_model.load_state_dict(_UpperCamelCase							)

							# prepare test inputs
							__UpperCAmelCase   :						List[Any]      							=					prepare_img()
							__UpperCAmelCase   :						int      							=					ViTImageProcessor.from_pretrained("""preprocessor_config"""							)
							__UpperCAmelCase   :						Optional[Any]      							=					processor(images=_UpperCamelCase       ,			return_tensors="""pt"""							)

							# compare outputs from both models
							__UpperCAmelCase   :						Tuple      							=					get_expected_output(_UpperCamelCase							)
							__UpperCAmelCase   :						Dict      							=					hf_model(inputs["""pixel_values"""]							).logits

							assert hf_logits.shape == torch.Size([1, 1_0_0_0]							)
							assert torch.allclose(hf_logits[0, 0:5]       ,			_UpperCamelCase       ,			atol=1E-3							)

							Path(_UpperCamelCase							).mkdir(exist_ok=_UpperCamelCase							)
							print(f'''Saving model {swiftformer_name} to {pytorch_dump_folder_path}'''							)
							hf_model.save_pretrained(_UpperCamelCase							)


if __name__ == "__main__":
							UpperCAmelCase    :     Optional[Any]   						=		argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '--swiftformer_name',
							    default='swiftformer_xs',
							    choices=['swiftformer_xs', 'swiftformer_s', 'swiftformer_l1', 'swiftformer_l3'],
							    type=str,
							    help='Name of the SwiftFormer model you\'d like to convert.',
							)
							parser.add_argument(
							    '--pytorch_dump_folder_path',
							    default='./converted_outputs/',
							    type=str,
							    help='Path to the output PyTorch model directory.',
							)
							parser.add_argument('--original_ckpt', default=None, type=str, help='Path to the original model checkpoint.')

							UpperCAmelCase    :     Tuple   						=		parser.parse_args()
							convert_swiftformer_checkpoint(args.swiftformer_name, args.pytorch_dump_folder_path, args.original_ckpt)
