



"""simple docstring"""






import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()

class 	__SCREAMING_SNAKE_CASE     (				unittest.TestCase       ):


					'''simple docstring'''





					def 		snake_case	(	self							:  List[Any]					)-> Any:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()



					@property
					def 		snake_case	(	self							:  Optional[int]					)-> Optional[Any]:
								lowerCamelCase__		:		Optional[Any]		     =1
								lowerCamelCase__		:		int		     =3
								lowerCamelCase__		:		str		     =(32, 32)

								lowerCamelCase__		:		Optional[Any]		     =floats_tensor((batch_size, num_channels) + sizes,  rng=random.Random(0					)					).to(lowerCamelCase					)
								return image



					@property
					def 		snake_case	(	self							:  int					)-> Tuple:
								torch.manual_seed(0					)
								lowerCamelCase__		:		Union[str, Any]		     =UNetaDConditionModel(
								    block_out_channels=(32, 64),  layers_per_block=2,  sample_size=32,  in_channels=4,  out_channels=4,  down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D'''),  up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D'''),  cross_attention_dim=32,  )
								return model



					@property
					def 		snake_case	(	self							:  int					)-> Tuple:
								torch.manual_seed(0					)
								lowerCamelCase__		:		List[Any]		     =AutoencoderKL(
								    block_out_channels=[32, 64],  in_channels=3,  out_channels=3,  down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],  up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],  latent_channels=4,  )
								return model



					@property
					def 		snake_case	(	self							:  Optional[Any]					)-> int:
								torch.manual_seed(0					)
								lowerCamelCase__		:		str		     =RobertaSeriesConfig(
								    hidden_size=32,  project_dim=32,  intermediate_size=37,  layer_norm_eps=1E-05,  num_attention_heads=4,  num_hidden_layers=5,  pad_token_id=1,  vocab_size=5006,  )
								return RobertaSeriesModelWithTransformation(lowerCamelCase					)



					@property
					def 		snake_case	(	self							:  Tuple					)-> Any:
								def extract(*lowerCamelCase							:  List[str],  **lowerCamelCase							:  Optional[int]					):

											class 	__SCREAMING_SNAKE_CASE     :


																'''simple docstring'''





																def __init__(	self							:  List[str]					)-> Dict:
																			lowerCamelCase__		:		int		     =torch.ones([0]					)




																def 		snake_case	(	self							:  Union[str, Any],  lowerCamelCase							:  List[Any]					)-> Any:
																			self.pixel_values.to(lowerCamelCase					)
																			return self





											return Out()

								return extract



					def 		snake_case	(	self							:  List[Any]					)-> Tuple:
								lowerCamelCase__		:		Optional[Any]		     ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
								lowerCamelCase__		:		Any		     =self.dummy_cond_unet
								lowerCamelCase__		:		Dict		     =PNDMScheduler(skip_prk_steps=lowerCamelCase					)
								lowerCamelCase__		:		Optional[Any]		     =self.dummy_vae
								lowerCamelCase__		:		int		     =self.dummy_text_encoder
								lowerCamelCase__		:		str		     =XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''					)
								lowerCamelCase__		:		Union[str, Any]		     =77

								lowerCamelCase__		:		Union[str, Any]		     =self.dummy_image.to(lowerCamelCase					)
								lowerCamelCase__		:		Optional[Any]		     =init_image / 2 + 0.5

								# make sure here that pndm scheduler skips prk
								lowerCamelCase__		:		Optional[Any]		     =AltDiffusionImgaImgPipeline(
								    unet=lowerCamelCase,  scheduler=lowerCamelCase,  vae=lowerCamelCase,  text_encoder=lowerCamelCase,  tokenizer=lowerCamelCase,  safety_checker=lowerCamelCase,  feature_extractor=self.dummy_extractor,  )
								lowerCamelCase__		:		Optional[Any]		     =VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor,  do_normalize=lowerCamelCase					)
								lowerCamelCase__		:		Dict		     =alt_pipe.to(lowerCamelCase					)
								alt_pipe.set_progress_bar_config(disable=lowerCamelCase					)

								lowerCamelCase__		:		Dict		     ='''A painting of a squirrel eating a burger'''
								lowerCamelCase__		:		Any		     =torch.Generator(device=lowerCamelCase					).manual_seed(0					)
								lowerCamelCase__		:		Union[str, Any]		     =alt_pipe(
								    [prompt],  generator=lowerCamelCase,  guidance_scale=6.0,  num_inference_steps=2,  output_type='''np''',  image=lowerCamelCase,  )

								lowerCamelCase__		:		int		     =output.images

								lowerCamelCase__		:		Optional[int]		     =torch.Generator(device=lowerCamelCase					).manual_seed(0					)
								lowerCamelCase__		:		Optional[int]		     =alt_pipe(
								    [prompt],  generator=lowerCamelCase,  guidance_scale=6.0,  num_inference_steps=2,  output_type='''np''',  image=lowerCamelCase,  return_dict=lowerCamelCase,  )[0]

								lowerCamelCase__		:		Union[str, Any]		     =image[0, -3:, -3:, -1]
								lowerCamelCase__		:		List[str]		     =image_from_tuple[0, -3:, -3:, -1]

								assert image.shape == (1, 32, 32, 3)
								lowerCamelCase__		:		Union[str, Any]		     =np.array([0.4_427, 0.3_731, 0.4_249, 0.4_941, 0.4_546, 0.4_148, 0.4_193, 0.4_666, 0.4_499]					)

								assert np.abs(image_slice.flatten() - expected_slice					).max() < 5E-3
								assert np.abs(image_from_tuple_slice.flatten() - expected_slice					).max() < 5E-3



					@unittest.skipIf(torch_device != '''cuda''',  '''This test requires a GPU'''					)
					def 		snake_case	(	self							:  Tuple					)-> Dict:
								lowerCamelCase__		:		int		     =self.dummy_cond_unet
								lowerCamelCase__		:		Dict		     =PNDMScheduler(skip_prk_steps=lowerCamelCase					)
								lowerCamelCase__		:		Dict		     =self.dummy_vae
								lowerCamelCase__		:		Dict		     =self.dummy_text_encoder
								lowerCamelCase__		:		List[str]		     =XLMRobertaTokenizer.from_pretrained('''hf-internal-testing/tiny-xlm-roberta'''					)
								lowerCamelCase__		:		Tuple		     =77

								lowerCamelCase__		:		str		     =self.dummy_image.to(lowerCamelCase					)

								# put models in fp16
								lowerCamelCase__		:		List[Any]		     =unet.half()
								lowerCamelCase__		:		List[Any]		     =vae.half()
								lowerCamelCase__		:		List[Any]		     =bert.half()

								# make sure here that pndm scheduler skips prk
								lowerCamelCase__		:		Any		     =AltDiffusionImgaImgPipeline(
								    unet=lowerCamelCase,  scheduler=lowerCamelCase,  vae=lowerCamelCase,  text_encoder=lowerCamelCase,  tokenizer=lowerCamelCase,  safety_checker=lowerCamelCase,  feature_extractor=self.dummy_extractor,  )
								lowerCamelCase__		:		Any		     =VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor,  do_normalize=lowerCamelCase					)
								lowerCamelCase__		:		List[Any]		     =alt_pipe.to(lowerCamelCase					)
								alt_pipe.set_progress_bar_config(disable=lowerCamelCase					)

								lowerCamelCase__		:		Tuple		     ='''A painting of a squirrel eating a burger'''
								lowerCamelCase__		:		str		     =torch.manual_seed(0					)
								lowerCamelCase__		:		Union[str, Any]		     =alt_pipe(
								    [prompt],  generator=lowerCamelCase,  num_inference_steps=2,  output_type='''np''',  image=lowerCamelCase,  ).images

								assert image.shape == (1, 32, 32, 3)




					@unittest.skipIf(torch_device != '''cuda''',  '''This test requires a GPU'''					)
					def 		snake_case	(	self							:  List[Any]					)-> List[Any]:
								lowerCamelCase__		:		List[Any]		     =load_image(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
								    '''/img2img/sketch-mountains-input.jpg'''					)
								# resize to resolution that is divisible by 8 but not 16 or 32
								lowerCamelCase__		:		List[Any]		     =init_image.resize((760, 504)					)

								lowerCamelCase__		:		Dict		     ='''BAAI/AltDiffusion'''
								lowerCamelCase__		:		Tuple		     =AltDiffusionImgaImgPipeline.from_pretrained(
								    lowerCamelCase,  safety_checker=lowerCamelCase,  )
								pipe.to(lowerCamelCase					)
								pipe.set_progress_bar_config(disable=lowerCamelCase					)
								pipe.enable_attention_slicing()

								lowerCamelCase__		:		List[str]		     ='''A fantasy landscape, trending on artstation'''

								lowerCamelCase__		:		str		     =torch.manual_seed(0					)
								lowerCamelCase__		:		Tuple		     =pipe(
								    prompt=lowerCamelCase,  image=lowerCamelCase,  strength=0.75,  guidance_scale=7.5,  generator=lowerCamelCase,  output_type='''np''',  )
								lowerCamelCase__		:		str		     =output.images[0]

								lowerCamelCase__		:		List[Any]		     =image[255:258, 383:386, -1]

								assert image.shape == (504, 760, 3)
								lowerCamelCase__		:		Any		     =np.array([0.9_358, 0.9_397, 0.9_599, 0.9_901, 1.0_000, 1.0_000, 0.9_882, 1.0_000, 1.0_000]					)

								assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2





@slow
@require_torch_gpu
class 	__SCREAMING_SNAKE_CASE     (				unittest.TestCase       ):


					'''simple docstring'''





					def 		snake_case	(	self							:  Optional[Any]					)-> Union[str, Any]:
								# clean up the VRAM after each test
								super().tearDown()
								gc.collect()
								torch.cuda.empty_cache()




					def 		snake_case	(	self							:  Any					)-> List[str]:
								lowerCamelCase__		:		Optional[int]		     =load_image(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
								    '''/img2img/sketch-mountains-input.jpg'''					)
								lowerCamelCase__		:		List[str]		     =init_image.resize((768, 512)					)
								lowerCamelCase__		:		Any		     =load_numpy(
								    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy'''					)

								lowerCamelCase__		:		Optional[int]		     ='''BAAI/AltDiffusion'''
								lowerCamelCase__		:		int		     =AltDiffusionImgaImgPipeline.from_pretrained(
								    lowerCamelCase,  safety_checker=lowerCamelCase,  )
								pipe.to(lowerCamelCase					)
								pipe.set_progress_bar_config(disable=lowerCamelCase					)
								pipe.enable_attention_slicing()

								lowerCamelCase__		:		List[str]		     ='''A fantasy landscape, trending on artstation'''

								lowerCamelCase__		:		Union[str, Any]		     =torch.manual_seed(0					)
								lowerCamelCase__		:		Optional[Any]		     =pipe(
								    prompt=lowerCamelCase,  image=lowerCamelCase,  strength=0.75,  guidance_scale=7.5,  generator=lowerCamelCase,  output_type='''np''',  )
								lowerCamelCase__		:		Any		     =output.images[0]

								assert image.shape == (512, 768, 3)
								# img2img is flaky across GPUs even in fp32, so using MAE here
								assert np.abs(expected_image - image					).max() < 1E-2









"""simple docstring"""






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

import os
from pathlib import Path






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

			# BLUE scores as follows:
			# "pair": [fairseq, transformers]
			lowerCamelCase__		:		Optional[Any]		     ={
			    '''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],
			}
			lowerCamelCase__		:		Any		     =f'''{src_lang}-{tgt_lang}'''

			lowerCamelCase__		:		Any		     =f'''
---
language:
- {src_lang}
- {tgt_lang}
thumbnail:
tags:
- translation
- wmt16
- allenai
license: apache-2.0
datasets:
- wmt16
metrics:
- bleu
---

# FSMT

## Model description

This is a ported version of fairseq-based [wmt16 transformer](https://github.com/jungokasai/deep-shallow/) for {src_lang}-{tgt_lang}.

For more details, please, see [Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation](https://arxiv.org/abs/2006.10369).

All 3 models are available:

* [wmt16-en-de-dist-12-1](https://huggingface.co/allenai/wmt16-en-de-dist-12-1)
* [wmt16-en-de-dist-6-1](https://huggingface.co/allenai/wmt16-en-de-dist-6-1)
* [wmt16-en-de-12-1](https://huggingface.co/allenai/wmt16-en-de-12-1)


## Intended uses & limitations

#### How to use

```python
from transformers import FSMTForConditionalGeneration, FSMTTokenizer
mname = "allenai/{model_name}"
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


## Training data

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

## Eval results

Here are the BLEU scores:

model   | fairseq | transformers
-------|---------|----------
{model_name}  | {scores[model_name][0]} | {scores[model_name][1]}

The 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.

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=5
mkdir -p $DATA_DIR
sacrebleu -t wmt16 -l $PAIR --echo src > $DATA_DIR/val.source
sacrebleu -t wmt16 -l $PAIR --echo ref > $DATA_DIR/val.target
echo $PAIR
PYTHONPATH="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
```

## Data Sources

- [training, etc.](http://www.statmt.org/wmt16/)
- [test set](http://matrix.statmt.org/test_sets/newstest2016.tgz?1504722372)


### BibTeX entry and citation info

```
@misc{{kasai2020deep,
    title={{Deep Encoder, Shallow Decoder: Reevaluating the Speed-Quality Tradeoff in Machine Translation}},
    author={{Jungo Kasai and Nikolaos Pappas and Hao Peng and James Cross and Noah A. Smith}},
    year={{2020}},
    eprint={{2006.10369}},
    archivePrefix={{arXiv}},
    primaryClass={{cs.CL}}
}}
```

'''
			model_card_dir.mkdir(parents=__lowerCamelCase							,		exist_ok=__lowerCamelCase     )
			lowerCamelCase__		:		str		     =os.path.join(__lowerCamelCase							,		'''README.md'''     )
			print(f'''Generating {path}'''     )
			with open(__lowerCamelCase							,		'''w'''							,		encoding='''utf-8'''     ) as f:
						f.write(__lowerCamelCase     )


# make sure we are under the root of the project
_lowercase			:			List[str]										=						Path(__file__).resolve().parent.parent.parent
_lowercase			:			Dict										=						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"]:
		_lowercase			:			int										=						model_cards_dir / "allenai" / model_name
		write_model_card(model_card_dir, src_lang="en", tgt_lang="de", model_name=model_name)




