


import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()







class 							lowerCamelCase__						(				unittest.TestCase   ):






			"""simple docstring"""






			@property
			def 					snake_case__					(				self  ):

						'''simple docstring'''


						torch.manual_seed(0  )
						UpperCamelCase__       =							UNetaDModel(
						    block_out_channels=(32, 64)   ,    layers_per_block=2   ,    sample_size=32   ,    in_channels=3   ,    out_channels=3   ,    down_block_types=("DownBlock2D", "AttnDownBlock2D")   ,    up_block_types=("AttnUpBlock2D", "UpBlock2D")   ,    )
						return model







			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							self.dummy_uncond_unet
						UpperCamelCase__       =							KarrasVeScheduler()

						UpperCamelCase__       =							KarrasVePipeline(unet=snake_case   ,    scheduler=snake_case  )
						pipe.to(snake_case  )
						pipe.set_progress_bar_config(disable=snake_case  )

						UpperCamelCase__       =							torch.manual_seed(0  )
						UpperCamelCase__       =							pipe(num_inference_steps=2   ,    generator=snake_case   ,    output_type="numpy"  ).images

						UpperCamelCase__       =							torch.manual_seed(0  )
						UpperCamelCase__       =							pipe(num_inference_steps=2   ,    generator=snake_case   ,    output_type="numpy"   ,    return_dict=snake_case  )[0]

						UpperCamelCase__       =							image[0, -3:, -3:, -1]
						UpperCamelCase__       =							image_from_tuple[0, -3:, -3:, -1]

						assert image.shape == (1, 32, 32, 3)
						UpperCamelCase__       =							np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]  )

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







@slow
@require_torch
class 							lowerCamelCase__						(				unittest.TestCase   ):






			"""simple docstring"""






			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"google/ncsnpp-celebahq-256"
						UpperCamelCase__       =							UNetaDModel.from_pretrained(snake_case  )
						UpperCamelCase__       =							KarrasVeScheduler()

						UpperCamelCase__       =							KarrasVePipeline(unet=snake_case   ,    scheduler=snake_case  )
						pipe.to(snake_case  )
						pipe.set_progress_bar_config(disable=snake_case  )

						UpperCamelCase__       =							torch.manual_seed(0  )
						UpperCamelCase__       =							pipe(num_inference_steps=20   ,    generator=snake_case   ,    output_type="numpy"  ).images

						UpperCamelCase__       =							image[0, -3:, -3:, -1]
						assert image.shape == (1, 256, 256, 3)
						UpperCamelCase__       =							np.array([0.578, 0.5811, 0.5924, 0.5809, 0.587, 0.5886, 0.5861, 0.5802, 0.586]  )

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






from unittest.mock import Mock, patch

from file_transfer.send_file import send_file
@patch("socket.socket"	)
@patch("builtins.open"	)
def 							UpperCamelCase_(					_A :Tuple					,  _A :str	)->		int:
			# ===== initialization =====
			UpperCamelCase__       =							Mock()
			UpperCamelCase__       =							conn, Mock()
			UpperCamelCase__       =							iter([1, None]	)
			UpperCamelCase__       =							lambda _A	: next(_A	)

			# ===== invoke =====
			send_file(filename="mytext.txt"					,  testing=_A	)

			# ===== ensurance =====
			sock.assert_called_once()
			sock.return_value.bind.assert_called_once()
			sock.return_value.listen.assert_called_once()
			sock.return_value.accept.assert_called_once()
			conn.recv.assert_called_once()

			file.return_value.__enter__.assert_called_once()
			file.return_value.__enter__.return_value.read.assert_called()

			conn.send.assert_called_once()
			conn.close.assert_called_once()
			sock.return_value.shutdown.assert_called_once()
			sock.return_value.close.assert_called_once()


