





import torch

from transformers import CamembertForMaskedLM, CamembertTokenizer





def      _UpperCamelCase  (a__ :Dict ,      a__ :str ,      a__ :List[str] ,      a__ :Any=5							):






							"""simple docstring"""





							assert masked_input.count("""<mask>"""							) == 1
							UpperCamelCase__					  =      torch.tensor(tokenizer.encode(a__ ,      add_special_tokens=a__							)							).unsqueeze(0							)  # Batch size 1
							UpperCamelCase__					  =      model(a__							)[0]  # The last hidden-state is the first element of the output tuple
							UpperCamelCase__					  =      (input_ids.squeeze() == tokenizer.mask_token_id).nonzero().item()
							UpperCamelCase__					  =      logits[0, masked_index, :]
							UpperCamelCase__					  =      logits.softmax(dim=0							)
							UpperCamelCase__  ,  UpperCamelCase__					  =      prob.topk(k=a__ ,      dim=0							)
							UpperCamelCase__					  =      """ """.join(
							    [tokenizer.convert_ids_to_tokens(indices[i].item()							) for i in range(len(a__							)							)]							)
							UpperCamelCase__					  =      tokenizer.mask_token
							UpperCamelCase__					  =      []
							for index, predicted_token_bpe in enumerate(topk_predicted_token_bpe.split(""" """							)							):
														UpperCamelCase__					  =      predicted_token_bpe.replace("""\u2581""" ,      """ """							)
														if " {0}".format(a__							) in masked_input:
																					topk_filled_outputs.append(
																					    (
																					        masked_input.replace(""" {0}""".format(a__							) ,      a__							),
																					        values[index].item(),
																					        predicted_token,
																					    )							)
														else:
																					topk_filled_outputs.append(
																					    (
																					        masked_input.replace(a__ ,      a__							),
																					        values[index].item(),
																					        predicted_token,
																					    )							)
							return topk_filled_outputs


UpperCamelCase__           =			CamembertTokenizer.from_pretrained("camembert-base")
UpperCamelCase__           =			CamembertForMaskedLM.from_pretrained("camembert-base")
model.eval()

UpperCamelCase__           =			"Le camembert est <mask> :)"
print(fill_mask(masked_input, model, tokenizer, topk=3))











import unittest

import torch

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

class 							__SCREAMING_SNAKE_CASE       (  _a ,		_a ,		unittest.TestCase							):
							snake_case  :   List[str]      =							VQModel
							snake_case  :   Dict      =							"""sample"""






							@property
							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase=(32, 32)	):
														UpperCamelCase__					  =      4
														UpperCamelCase__					  =      3

														UpperCamelCase__					  =      floats_tensor((batch_size, num_channels) + sizes	).to(__lowerCAmelCase	)

														return {"sample": image}






							@property
							def 					_lowerCamelCase		(    self	):
														return (3, 32, 32)






							@property
							def 					_lowerCamelCase		(    self	):
														return (3, 32, 32)






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      {
														    """block_out_channels""": [32, 64],
														    """in_channels""": 3,
														    """out_channels""": 3,
														    """down_block_types""": ["""DownEncoderBlock2D""", """DownEncoderBlock2D"""],
														    """up_block_types""": ["""UpDecoderBlock2D""", """UpDecoderBlock2D"""],
														    """latent_channels""": 3,
														}
														UpperCamelCase__					  =      self.dummy_input
														return init_dict, inputs_dict






							def 					_lowerCamelCase		(    self	):
														pass






							def 					_lowerCamelCase		(    self	):
														pass






							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__  ,  UpperCamelCase__					  =      VQModel.from_pretrained("""fusing/vqgan-dummy"""	,			output_loading_info=__lowerCAmelCase	)
														self.assertIsNotNone(__lowerCAmelCase	)
														self.assertEqual(len(loading_info["""missing_keys"""]	)	,			0	)

														model.to(__lowerCAmelCase	)
														UpperCamelCase__					  =      model(**self.dummy_input	)

														assert image is not None, "Make sure output is not None"







							def 					_lowerCamelCase		(    self	):
														UpperCamelCase__					  =      VQModel.from_pretrained("""fusing/vqgan-dummy"""	)
														model.to(__lowerCAmelCase	).eval()

														torch.manual_seed(0	)
														if torch.cuda.is_available():
																					torch.cuda.manual_seed_all(0	)

														UpperCamelCase__					  =      torch.randn(1	,			model.config.in_channels	,			model.config.sample_size	,			model.config.sample_size	)
														UpperCamelCase__					  =      image.to(__lowerCAmelCase	)
														with torch.no_grad():
																					UpperCamelCase__					  =      model(__lowerCAmelCase	).sample

														UpperCamelCase__					  =      output[0, -1, -3:, -3:].flatten().cpu()
														# fmt: off
														UpperCamelCase__					  =      torch.tensor([-0.0153, -0.4044, -0.1880, -0.5161, -0.2418, -0.4072, -0.1612, -0.0633, -0.0143]	)
														# fmt: on
														self.assertTrue(torch.allclose(__lowerCAmelCase	,			__lowerCAmelCase	,			atol=1E-3	)	)




