





import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch

@require_torch
class 	A_       :
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				return self.get_dummy_input()
	@property
	def      _lowercase				(    self		):






				'''simple docstring'''







				if self.block_type == "down":
							return (4, 3_2, 1_6, 1_6)
				elif self.block_type == "mid":
							return (4, 3_2, 3_2, 3_2)
				elif self.block_type == "up":
							return (4, 3_2, 6_4, 6_4)

				raise ValueError(F"""'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'."""		)
	def      _lowercase				(    self							,					_A=True							,					_A=False							,					_A=False							,					_A=False							,					):






				'''simple docstring'''







				UpperCAmelCase   		=				4
				UpperCAmelCase   		=				3_2
				UpperCAmelCase   		=				(3_2, 3_2)

				UpperCAmelCase   		=				torch.manual_seed(0		)
				UpperCAmelCase   		=				torch.device(_A		)
				UpperCAmelCase   		=				(batch_size, num_channels) + sizes
				UpperCAmelCase   		=				randn_tensor(_A							,					generator=_A							,					device=_A		)
				UpperCAmelCase   		=				{'''hidden_states''': hidden_states}

				if include_temb:
							UpperCAmelCase   		=				1_2_8
							UpperCAmelCase   		=				randn_tensor((batch_size, temb_channels)							,					generator=_A							,					device=_A		)

				if include_res_hidden_states_tuple:
							UpperCAmelCase   		=				torch.manual_seed(1		)
							UpperCAmelCase   		=				(randn_tensor(_A							,					generator=_A							,					device=_A		),)

				if include_encoder_hidden_states:
							UpperCAmelCase   		=				floats_tensor((batch_size, 3_2, 3_2)		).to(_A		)

				if include_skip_sample:
							UpperCAmelCase   		=				randn_tensor(((batch_size, 3) + sizes)							,					generator=_A							,					device=_A		)

				return dummy_input
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase   		=				{
				    '''in_channels''': 3_2,
				    '''out_channels''': 3_2,
				    '''temb_channels''': 1_2_8,
				}
				if self.block_type == "up":
							UpperCAmelCase   		=				3_2

				if self.block_type == "mid":
							init_dict.pop('''out_channels'''		)

				UpperCAmelCase   		=				self.dummy_input
				return init_dict, inputs_dict
	def      _lowercase				(    self							,					_A		):






				'''simple docstring'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.prepare_init_args_and_inputs_for_common()
				UpperCAmelCase   		=				self.block_class(**_A		)
				unet_block.to(_A		)
				unet_block.eval()

				with torch.no_grad():
							UpperCAmelCase   		=				unet_block(**_A		)

				if isinstance(_A							,					_A		):
							UpperCAmelCase   		=				output[0]

				self.assertEqual(output.shape							,					self.output_shape		)

				UpperCAmelCase   		=				output[0, -1, -3:, -3:]
				UpperCAmelCase   		=				torch.tensor(_A		).to(_A		)
				assert torch_all_close(output_slice.flatten()							,					_A							,					atol=5E-3		)







	@unittest.skipIf(torch_device == '''mps'''							,					'''Training is not supported in mps'''		)
	def      _lowercase				(    self		):






				'''simple docstring'''







				UpperCAmelCase		,			UpperCAmelCase   		=				self.prepare_init_args_and_inputs_for_common()
				UpperCAmelCase   		=				self.block_class(**_A		)
				model.to(_A		)
				model.train()
				UpperCAmelCase   		=				model(**_A		)

				if isinstance(_A							,					_A		):
							UpperCAmelCase   		=				output[0]

				UpperCAmelCase   		=				torch.device(_A		)
				UpperCAmelCase   		=				randn_tensor(output.shape							,					device=_A		)
				UpperCAmelCase   		=				torch.nn.functional.mse_loss(_A							,					_A		)
				loss.backward()










from __future__ import annotations

from collections.abc import Callable





def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__			,			UpperCamelCase__ = 100			,			)  ->				float:







			'''simple docstring'''

			UpperCAmelCase   		=				x_start
			UpperCAmelCase   		=				fnc(UpperCamelCase__ )
			UpperCAmelCase   		=				0.0
			for _ in range(UpperCamelCase__ ):
						# Approximates small segments of curve as linear and solve
						# for trapezoidal area
						UpperCAmelCase   		=				(x_end - x_start) / steps + xa
						UpperCAmelCase   		=				fnc(UpperCamelCase__ )
						area += abs(fxa + fxa ) * (xa - xa) / 2
						# Increment step
						UpperCAmelCase   		=				xa
						UpperCAmelCase   		=				fxa
			return area


if __name__ == "__main__":





				def 						__SCREAMING_SNAKE_CASE  (				UpperCamelCase__ )  ->				str:







							'''simple docstring'''

							return x**3 + x**2

				print("f(x) = x^3 + x^2")
				print("The area between the curve, x = -5, x = 5 and the x axis is:")
				__A				:    List[Any]       =   10
				while i <= 100_000:
								print(F'with {i} steps: {trapezoidal_area(f, -5, 5, i)}')
								i *= 10



