




'''simple docstring'''





import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor




class 				_snake_case	(			unittest.TestCase							):




			def      lowerCAmelCase__       (      self       ,							a__ = False							)       ->       Any:

										'''simple docstring'''




										snake_case_			      =   10

										if only_cross_attention:
																	snake_case_			      =   12
										else:
																	# when only cross attention is not set, the cross attention dim must be the same as the query dim
																	snake_case_			      =   query_dim

										return {
										    "query_dim": query_dim,
										    "cross_attention_dim": cross_attention_dim,
										    "heads": 2,
										    "dim_head": 4,
										    "added_kv_proj_dim": 6,
										    "norm_num_groups": 1,
										    "only_cross_attention": only_cross_attention,
										    "processor": AttnAddedKVProcessor(),
										}




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   2

										snake_case_			      =   torch.rand(a__       ,							a__       ,							3       ,							2							)
										snake_case_			      =   torch.rand(a__       ,							4       ,							a__							)
										snake_case_			      =   None

										return {
										    "hidden_states": hidden_states,
										    "encoder_hidden_states": encoder_hidden_states,
										    "attention_mask": attention_mask,
										}




			def      lowerCAmelCase__       (      self							)       ->       List[Any]:

										'''simple docstring'''




										torch.manual_seed(0							)

										snake_case_			      =   self.get_constructor_arguments(only_cross_attention=a__							)
										snake_case_			      =   Attention(**a__							)

										self.assertTrue(attn.to_k is not None							)
										self.assertTrue(attn.to_v is not None							)

										snake_case_			      =   self.get_forward_arguments(
										    query_dim=constructor_args["query_dim"]       ,							added_kv_proj_dim=constructor_args["added_kv_proj_dim"]							)

										snake_case_			      =   attn(**a__							)

										# only self attention

										torch.manual_seed(0							)

										snake_case_			      =   self.get_constructor_arguments(only_cross_attention=a__							)
										snake_case_			      =   Attention(**a__							)

										self.assertTrue(attn.to_k is None							)
										self.assertTrue(attn.to_v is None							)

										snake_case_			      =   self.get_forward_arguments(
										    query_dim=constructor_args["query_dim"]       ,							added_kv_proj_dim=constructor_args["added_kv_proj_dim"]							)

										snake_case_			      =   attn(**a__							)

										self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()							)




class 				_snake_case	(			unittest.TestCase							):




			def      lowerCAmelCase__       (      self							)       ->       Tuple:

										'''simple docstring'''




										snake_case_			      =   DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"       ,							safety_checker=a__							)

										snake_case_			      =   pipe(
										    "foo"       ,							num_inference_steps=2       ,							generator=torch.Generator("cpu"							).manual_seed(0							)       ,							output_type="np"       ,							).images

										# the initial conversion succeeds
										snake_case_			      =   DiffusionPipeline.from_pretrained(
										    "hf-internal-testing/tiny-stable-diffusion-pipe"       ,							device_map="sequential"       ,							safety_checker=a__							)

										snake_case_			      =   pipe(
										    "foo"       ,							num_inference_steps=2       ,							generator=torch.Generator("cpu"							).manual_seed(0							)       ,							output_type="np"       ,							).images

										with tempfile.TemporaryDirectory() as tmpdir:
																	# save the converted model
																	pipe.save_pretrained(a__							)

																	# can also load the converted weights
																	snake_case_			      =   DiffusionPipeline.from_pretrained(a__       ,							device_map="sequential"       ,							safety_checker=a__							)

										snake_case_			      =   pipe(
										    "foo"       ,							num_inference_steps=2       ,							generator=torch.Generator("cpu"							).manual_seed(0							)       ,							output_type="np"       ,							).images

										self.assertTrue(np.allclose(a__       ,							a__       ,							atol=1e-5							)							)
										self.assertTrue(np.allclose(a__       ,							a__       ,							atol=1e-5							)							)










'''simple docstring'''





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
		import torch

		from transformers import CamembertModel




@require_torch
@require_sentencepiece
@require_tokenizers
class 				_snake_case	(			unittest.TestCase							):




			@slow
			def      lowerCAmelCase__       (      self							)       ->       List[Any]:

										'''simple docstring'''




										snake_case_			      =   CamembertModel.from_pretrained("camembert-base"							)
										model.to(a__							)

										snake_case_			      =   torch.tensor(
										    [[5, 121, 11, 660, 16, 730, 25_543, 110, 83, 6]]       ,							device=a__       ,							dtype=torch.long       ,							)  # J'aime le camembert !
										with torch.no_grad():
																	snake_case_			      =   model(a__							)["last_hidden_state"]
										snake_case_			      =   torch.Size((1, 10, 768)							)
										self.assertEqual(output.shape       ,							a__							)
										# compare the actual values for a slice.
										snake_case_			      =   torch.tensor(
										    [[[-0.0_2_5_4, 0.0_2_3_5, 0.1_0_2_7], [0.0_6_0_6, -0.1_8_1_1, -0.0_4_1_8], [-0.1_5_6_1, -0.1_1_2_7, 0.2_6_8_7]]]       ,							device=a__       ,							dtype=torch.float       ,							)
										# camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
										# camembert.eval()
										# expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

										self.assertTrue(torch.allclose(output[:, :3, :3]       ,							a__       ,							atol=1e-4							)							)




