






import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch
			from torch import nn

			from transformers import (
			    MODEL_MAPPING,
			    BeitForImageClassification,
			    BeitForMaskedImageModeling,
			    BeitForSemanticSegmentation,
			    BeitModel,
			)
			from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
			import PIL
			from PIL import Image

			from transformers import BeitImageProcessor







class a :
						"""simple docstring"""

						def __init__( self :     Any				,							lowerCamelCase__ :     Optional[Any]				,							lowerCamelCase__ :     int=100				,							lowerCamelCase__ :     List[str]=13				,							lowerCamelCase__ :     Union[str, Any]=30				,							lowerCamelCase__ :     List[str]=2				,							lowerCamelCase__ :     Dict=3				,							lowerCamelCase__ :     int=True				,							lowerCamelCase__ :     Optional[int]=True				,							lowerCamelCase__ :     Any=32				,							lowerCamelCase__ :     Dict=4				,							lowerCamelCase__ :     Optional[int]=4				,							lowerCamelCase__ :     List[str]=37				,							lowerCamelCase__ :     Optional[Any]="gelu"				,							lowerCamelCase__ :     Any=0.1				,							lowerCamelCase__ :     Optional[int]=0.1				,							lowerCamelCase__ :     List[str]=10				,							lowerCamelCase__ :     Any=0.0_2				,							lowerCamelCase__ :     List[str]=3				,							lowerCamelCase__ :     Any=None				,							lowerCamelCase__ :     Dict=[0, 1, 2, 3]				,							)    ->						Any:
													"""simple docstring"""


													__lowercase      =    parent
													__lowercase      =    100
													__lowercase      =    batch_size
													__lowercase      =    image_size
													__lowercase      =    patch_size
													__lowercase      =    num_channels
													__lowercase      =    is_training
													__lowercase      =    use_labels
													__lowercase      =    hidden_size
													__lowercase      =    num_hidden_layers
													__lowercase      =    num_attention_heads
													__lowercase      =    intermediate_size
													__lowercase      =    hidden_act
													__lowercase      =    hidden_dropout_prob
													__lowercase      =    attention_probs_dropout_prob
													__lowercase      =    type_sequence_label_size
													__lowercase      =    initializer_range
													__lowercase      =    scope
													__lowercase      =    out_indices
													__lowercase      =    num_labels

													# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
													__lowercase      =    (image_size // patch_size) ** 2
													__lowercase      =    num_patches + 1






						def     UpperCAmelCase_      ( self :     str				)    ->						str:
													"""simple docstring"""


													__lowercase      =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

													__lowercase      =    None
													__lowercase      =    None
													if self.use_labels:
																				__lowercase      =    ids_tensor([self.batch_size]				,							self.type_sequence_label_size				)
																				__lowercase      =    ids_tensor([self.batch_size, self.image_size, self.image_size]				,							self.num_labels				)

													__lowercase      =    self.get_config()

													return config, pixel_values, labels, pixel_labels






						def     UpperCAmelCase_      ( self :     Tuple				)    ->						int:
													"""simple docstring"""


													return BeitConfig(
													    vocab_size=self.vocab_size				,							image_size=self.image_size				,							patch_size=self.patch_size				,							num_channels=self.num_channels				,							hidden_size=self.hidden_size				,							num_hidden_layers=self.num_hidden_layers				,							num_attention_heads=self.num_attention_heads				,							intermediate_size=self.intermediate_size				,							hidden_act=self.hidden_act				,							hidden_dropout_prob=self.hidden_dropout_prob				,							attention_probs_dropout_prob=self.attention_probs_dropout_prob				,							is_decoder=_a				,							initializer_range=self.initializer_range				,							out_indices=self.out_indices				,							)






						def     UpperCAmelCase_      ( self :     List[str]				,							lowerCamelCase__ :     Any				,							lowerCamelCase__ :     Any				,							lowerCamelCase__ :     List[str]				,							lowerCamelCase__ :     List[Any]				)    ->						str:
													"""simple docstring"""


													__lowercase      =    BeitModel(config=_a				)
													model.to(_a				)
													model.eval()
													__lowercase      =    model(_a				)
													self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)				)






						def     UpperCAmelCase_      ( self :     Optional[Any]				,							lowerCamelCase__ :     List[str]				,							lowerCamelCase__ :     str				,							lowerCamelCase__ :     Dict				,							lowerCamelCase__ :     Optional[Any]				)    ->						List[Any]:
													"""simple docstring"""


													__lowercase      =    BeitForMaskedImageModeling(config=_a				)
													model.to(_a				)
													model.eval()
													__lowercase      =    model(_a				)
													self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length - 1, self.vocab_size)				)






						def     UpperCAmelCase_      ( self :     Optional[int]				,							lowerCamelCase__ :     List[str]				,							lowerCamelCase__ :     Union[str, Any]				,							lowerCamelCase__ :     List[Any]				,							lowerCamelCase__ :     str				)    ->						str:
													"""simple docstring"""


													__lowercase      =    self.type_sequence_label_size
													__lowercase      =    BeitForImageClassification(_a				)
													model.to(_a				)
													model.eval()
													__lowercase      =    model(_a				,							labels=_a				)
													self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)				)

													# test greyscale images
													__lowercase      =    1
													__lowercase      =    BeitForImageClassification(_a				)
													model.to(_a				)
													model.eval()

													__lowercase      =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
													__lowercase      =    model(_a				,							labels=_a				)
													self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.type_sequence_label_size)				)






						def     UpperCAmelCase_      ( self :     Tuple				,							lowerCamelCase__ :     List[Any]				,							lowerCamelCase__ :     List[Any]				,							lowerCamelCase__ :     Any				,							lowerCamelCase__ :     str				)    ->						Tuple:
													"""simple docstring"""


													__lowercase      =    self.num_labels
													__lowercase      =    BeitForSemanticSegmentation(_a				)
													model.to(_a				)
													model.eval()
													__lowercase      =    model(_a				)
													self.parent.assertEqual(
													    result.logits.shape				,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)
													__lowercase      =    model(_a				,							labels=_a				)
													self.parent.assertEqual(
													    result.logits.shape				,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)				)






						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						List[Any]:
													"""simple docstring"""


													__lowercase      =    self.prepare_config_and_inputs()
													__lowercase      =    config_and_inputs
													__lowercase      =    {"""pixel_values""": pixel_values}
													return config, inputs_dict







@require_torch
class a (     UpperCamelCase__       ,						UpperCamelCase__       ,						unittest.TestCase  ):
						"""simple docstring"""

						UpperCamelCase_	:       Tuple       	=		(
						    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
						    if is_torch_available()
						    else ()
						)
						UpperCamelCase_	:       List[str]       	=		(
						    {
						        'feature-extraction': BeitModel,
						        'image-classification': BeitForImageClassification,
						        'image-segmentation': BeitForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase_	:       List[str]       	=		False
						UpperCamelCase_	:       Tuple       	=		False
						UpperCamelCase_	:       str       	=		False






						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						Dict:
													"""simple docstring"""


													__lowercase      =    BeitModelTester(self				)
													__lowercase      =    ConfigTester(self				,							config_class=_a				,							has_text_modality=_a				,							hidden_size=37				)






						def     UpperCAmelCase_      ( self :     List[str]				)    ->						Dict:
													"""simple docstring"""


													self.config_tester.run_common_tests()






						@unittest.skip(reason='''BEiT does not use inputs_embeds'''				)
						def     UpperCAmelCase_      ( self :     Optional[int]				)    ->						Any:
													"""simple docstring"""


													pass






						@require_torch_multi_gpu
						@unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''				)
						def     UpperCAmelCase_      ( self :     Tuple				)    ->						Union[str, Any]:
													"""simple docstring"""


													pass






						def     UpperCAmelCase_      ( self :     Any				)    ->						Dict:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__lowercase      =    model_class(_a				)
																				self.assertIsInstance(model.get_input_embeddings()				,							(nn.Module)				)
																				__lowercase      =    model.get_output_embeddings()
																				self.assertTrue(x is None or isinstance(_a				,							nn.Linear				)				)






						def     UpperCAmelCase_      ( self :     str				)    ->						Any:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__lowercase      =    model_class(_a				)
																				__lowercase      =    inspect.signature(model.forward				)
																				# signature.parameters is an OrderedDict => so arg_names order is deterministic
																				__lowercase      =    [*signature.parameters.keys()]

																				__lowercase      =    ["""pixel_values"""]
																				self.assertListEqual(arg_names[:1]				,							_a				)






						def     UpperCAmelCase_      ( self :     Dict				)    ->						Optional[int]:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_a				)






						def     UpperCAmelCase_      ( self :     str				)    ->						Tuple:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*_a				)






						def     UpperCAmelCase_      ( self :     Optional[Any]				)    ->						List[Any]:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_image_classification(*_a				)






						def     UpperCAmelCase_      ( self :     List[str]				)    ->						Dict:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_semantic_segmentation(*_a				)






						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						Tuple:
													"""simple docstring"""


													if not self.model_tester.is_training:
																				return

													__lowercase      =    self.model_tester.prepare_config_and_inputs_for_common()
													__lowercase      =    True

													for model_class in self.all_model_classes:
																				# we don't test BeitForMaskedImageModeling
																				if model_class in [*get_values(_a				), BeitForMaskedImageModeling]:
																											continue

																				__lowercase      =    model_class(_a				)
																				model.to(_a				)
																				model.train()
																				__lowercase      =    self._prepare_for_class(_a				,							_a				,							return_labels=_a				)
																				__lowercase      =    model(**_a				).loss
																				loss.backward()






						def     UpperCAmelCase_      ( self :     Any				)    ->						Dict:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs_for_common()
													if not self.model_tester.is_training:
																				return

													__lowercase      =    False
													__lowercase      =    True

													for model_class in self.all_model_classes:
																				# we don't test BeitForMaskedImageModeling
																				if (
																				    model_class in [*get_values(_a				), BeitForMaskedImageModeling]
																				    or not model_class.supports_gradient_checkpointing
																				):
																											continue

																				__lowercase      =    model_class(_a				)
																				model.gradient_checkpointing_enable()
																				model.to(_a				)
																				model.train()
																				__lowercase      =    self._prepare_for_class(_a				,							_a				,							return_labels=_a				)
																				__lowercase      =    model(**_a				).loss
																				loss.backward()






						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						List[str]:
													"""simple docstring"""


													__lowercase      =    self.model_tester.prepare_config_and_inputs_for_common()

													__lowercase      =    _config_zero_init(_a				)
													for model_class in self.all_model_classes:
																				__lowercase      =    model_class(config=_a				)
																				for name, param in model.named_parameters():
																											# we skip lambda parameters as these require special initial values
																											# determined by config.layer_scale_init_value
																											if "lambda" in name:
																																		continue
																											if param.requires_grad:
																																		self.assertIn(
																																		    ((param.data.mean() * 1e9).round() / 1e9).item()				,							[0.0, 1.0]				,							msg=f'Parameter {name} of model {model_class} seems not properly initialized'				,							)






						@slow
						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						Union[str, Any]:
													"""simple docstring"""


													for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__lowercase      =    BeitModel.from_pretrained(_a				)
																				self.assertIsNotNone(_a				)







def 		_A(       )    -> Dict:
							'''simple docstring'''


							__lowercase      =    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
							return image







@require_torch
@require_vision
class a (     unittest.TestCase  ):
						"""simple docstring"""

						@cached_property
						def     UpperCAmelCase_      ( self :     Tuple				)    ->						Any:
													"""simple docstring"""


													return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''				) if is_vision_available() else None






						@slow
						def     UpperCAmelCase_      ( self :     List[Any]				)    ->						str:
													"""simple docstring"""


													__lowercase      =    BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''				).to(_a				)

													__lowercase      =    self.default_image_processor
													__lowercase      =    prepare_img()
													__lowercase      =    image_processor(images=_a				,							return_tensors='''pt'''				).pixel_values.to(_a				)

													# prepare bool_masked_pos
													__lowercase      =    torch.ones((1, 196)				,							dtype=torch.bool				).to(_a				)

													# forward pass
													with torch.no_grad():
																				__lowercase      =    model(pixel_values=_a				,							bool_masked_pos=_a				)
													__lowercase      =    outputs.logits

													# verify the logits
													__lowercase      =    torch.Size((1, 196, 8_192)				)
													self.assertEqual(logits.shape				,							_a				)

													__lowercase      =    torch.tensor(
													    [[-3.2_4_3_7, 0.5_0_7_2, -1_3.9_1_7_4], [-3.2_4_5_6, 0.4_9_4_8, -1_3.9_4_0_1], [-3.2_0_3_3, 0.5_1_2_1, -1_3.8_5_5_0]]				).to(_a				)

													self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]				,							_a				,							atol=1e-2				)				)






						@slow
						def     UpperCAmelCase_      ( self :     int				)    ->						List[str]:
													"""simple docstring"""


													__lowercase      =    BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''				).to(_a				)

													__lowercase      =    self.default_image_processor
													__lowercase      =    prepare_img()
													__lowercase      =    image_processor(images=_a				,							return_tensors='''pt'''				).to(_a				)

													# forward pass
													with torch.no_grad():
																				__lowercase      =    model(**_a				)
													__lowercase      =    outputs.logits

													# verify the logits
													__lowercase      =    torch.Size((1, 1_000)				)
													self.assertEqual(logits.shape				,							_a				)

													__lowercase      =    torch.tensor([-1.2_3_8_5, -1.0_9_8_7, -1.0_1_0_8]				).to(_a				)

													self.assertTrue(torch.allclose(logits[0, :3]				,							_a				,							atol=1e-4				)				)

													__lowercase      =    281
													self.assertEqual(logits.argmax(-1				).item()				,							_a				)






						@slow
						def     UpperCAmelCase_      ( self :     Any				)    ->						Dict:
													"""simple docstring"""


													__lowercase      =    BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''				).to(
													    _a				)

													__lowercase      =    self.default_image_processor
													__lowercase      =    prepare_img()
													__lowercase      =    image_processor(images=_a				,							return_tensors='''pt'''				).to(_a				)

													# forward pass
													with torch.no_grad():
																				__lowercase      =    model(**_a				)
													__lowercase      =    outputs.logits

													# verify the logits
													__lowercase      =    torch.Size((1, 21_841)				)
													self.assertEqual(logits.shape				,							_a				)

													__lowercase      =    torch.tensor([1.6_8_8_1, -0.2_7_8_7, 0.5_9_0_1]				).to(_a				)

													self.assertTrue(torch.allclose(logits[0, :3]				,							_a				,							atol=1e-4				)				)

													__lowercase      =    2_396
													self.assertEqual(logits.argmax(-1				).item()				,							_a				)






						@slow
						def     UpperCAmelCase_      ( self :     Dict				)    ->						str:
													"""simple docstring"""


													__lowercase      =    BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''				)
													__lowercase      =    model.to(_a				)

													__lowercase      =    BeitImageProcessor(do_resize=_a				,							size=640				,							do_center_crop=_a				)

													__lowercase      =    load_dataset('''hf-internal-testing/fixtures_ade20k'''				,							split='''test'''				)
													__lowercase      =    Image.open(ds[0]['''file''']				)
													__lowercase      =    image_processor(images=_a				,							return_tensors='''pt'''				).to(_a				)

													# forward pass
													with torch.no_grad():
																				__lowercase      =    model(**_a				)
													__lowercase      =    outputs.logits

													# verify the logits
													__lowercase      =    torch.Size((1, 150, 160, 160)				)
													self.assertEqual(logits.shape				,							_a				)

													__lowercase      =    version.parse(PIL.__version__				) < version.parse('''9.0.0'''				)

													if is_pillow_less_than_a:
																				__lowercase      =    torch.tensor(
																				    [
																				        [[-4.9_2_2_5, -2.3_9_5_4, -3.0_5_2_2], [-2.8_8_2_2, -1.0_0_4_6, -1.7_5_6_1], [-2.9_5_4_9, -1.3_2_2_8, -2.1_3_4_7]],
																				        [[-5.8_1_6_8, -3.4_1_2_9, -4.0_7_7_8], [-3.8_6_5_1, -2.2_2_1_4, -3.0_2_7_7], [-3.8_3_5_6, -2.4_6_4_3, -3.3_5_3_5]],
																				        [[-0.0_0_7_8, 3.9_9_5_2, 4.0_7_5_4], [2.9_8_5_6, 4.6_9_4_4, 5.0_0_3_5], [3.2_4_1_3, 4.7_8_1_3, 4.9_9_6_9]],
																				    ]				,							device=_a				,							)
													else:
																				__lowercase      =    torch.tensor(
																				    [
																				        [[-4.8_9_6_0, -2.3_6_8_8, -3.0_3_5_5], [-2.8_4_7_8, -0.9_8_3_6, -1.7_4_1_8], [-2.9_4_4_9, -1.3_3_3_2, -2.1_4_5_6]],
																				        [[-5.8_0_8_1, -3.4_1_2_4, -4.1_0_0_6], [-3.8_5_6_1, -2.2_0_8_1, -3.0_3_2_3], [-3.8_3_6_5, -2.4_6_0_1, -3.3_6_6_9]],
																				        [[-0.0_3_0_9, 3.9_8_6_8, 4.0_5_4_0], [2.9_6_4_0, 4.6_8_7_7, 4.9_9_7_6], [3.2_0_8_1, 4.7_6_9_0, 4.9_9_4_2]],
																				    ]				,							device=_a				,							)

													self.assertTrue(torch.allclose(logits[0, :3, :3, :3]				,							_a				,							atol=1e-4				)				)






						@slow
						def     UpperCAmelCase_      ( self :     str				)    ->						int:
													"""simple docstring"""


													__lowercase      =    BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''				)
													__lowercase      =    model.to(_a				)

													__lowercase      =    BeitImageProcessor(do_resize=_a				,							size=640				,							do_center_crop=_a				)

													__lowercase      =    load_dataset('''hf-internal-testing/fixtures_ade20k'''				,							split='''test'''				)
													__lowercase      =    Image.open(ds[0]['''file''']				)
													__lowercase      =    image_processor(images=_a				,							return_tensors='''pt'''				).to(_a				)

													# forward pass
													with torch.no_grad():
																				__lowercase      =    model(**_a				)

													__lowercase      =    outputs.logits.detach().cpu()

													__lowercase      =    image_processor.post_process_semantic_segmentation(outputs=_a				,							target_sizes=[(500, 300)]				)
													__lowercase      =    torch.Size((500, 300)				)
													self.assertEqual(segmentation[0].shape				,							_a				)

													__lowercase      =    image_processor.post_process_semantic_segmentation(outputs=_a				)
													__lowercase      =    torch.Size((160, 160)				)
													self.assertEqual(segmentation[0].shape				,							_a				)












from typing import List, Optional, Tuple, Union

import torch

from ...models import UNetaDModel
from ...schedulers import KarrasVeScheduler
from ...utils import randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput







class a (     __SCREAMING_SNAKE_CASE  ):
						"""simple docstring"""

						UpperCamelCase_	:       UNetaDModel
						UpperCamelCase_	:       KarrasVeScheduler






						def __init__( self :     Tuple				,							lowerCamelCase__ :     UNetaDModel				,							lowerCamelCase__ :     KarrasVeScheduler				)    ->						Optional[Any]:
													"""simple docstring"""


													super().__init__()
													self.register_modules(unet=lowerCamelCase__				,							scheduler=lowerCamelCase__				)






						@torch.no_grad()
						def __call__( self :     str				,							lowerCamelCase__ :     int = 1				,							lowerCamelCase__ :     int = 50				,							lowerCamelCase__ :     Optional[Union[torch.Generator, List[torch.Generator]]] = None				,							lowerCamelCase__ :     Optional[str] = "pil"				,							lowerCamelCase__ :     bool = True				,							**lowerCamelCase__ :     int				,							)    ->						Union[Tuple, ImagePipelineOutput]:
													"""simple docstring"""


													__lowercase      =    self.unet.config.sample_size
													__lowercase      =    (batch_size, 3, img_size, img_size)

													__lowercase      =    self.unet

													# sample x_0 ~ N(0, sigma_0^2 * I)
													__lowercase      =    randn_tensor(lowerCamelCase__				,							generator=lowerCamelCase__				,							device=self.device				) * self.scheduler.init_noise_sigma

													self.scheduler.set_timesteps(lowerCamelCase__				)

													for t in self.progress_bar(self.scheduler.timesteps				):
																				# here sigma_t == t_i from the paper
																				__lowercase      =    self.scheduler.schedule[t]
																				__lowercase      =    self.scheduler.schedule[t - 1] if t > 0 else 0

																				# 1. Select temporarily increased noise level sigma_hat
																				# 2. Add new noise to move from sample_i to sample_hat
																				__lowercase     ,     __lowercase      =    self.scheduler.add_noise_to_input(lowerCamelCase__				,							lowerCamelCase__				,							generator=lowerCamelCase__				)

																				# 3. Predict the noise residual given the noise magnitude `sigma_hat`
																				# The model inputs and output are adjusted by following eq. (213) in [1].
																				__lowercase      =    (sigma_hat / 2) * model((sample_hat + 1) / 2				,							sigma_hat / 2				).sample

																				# 4. Evaluate dx/dt at sigma_hat
																				# 5. Take Euler step from sigma to sigma_prev
																				__lowercase      =    self.scheduler.step(lowerCamelCase__				,							lowerCamelCase__				,							lowerCamelCase__				,							lowerCamelCase__				)

																				if sigma_prev != 0:
																											# 6. Apply 2nd order correction
																											# The model inputs and output are adjusted by following eq. (213) in [1].
																											__lowercase      =    (sigma_prev / 2) * model((step_output.prev_sample + 1) / 2				,							sigma_prev / 2				).sample
																											__lowercase      =    self.scheduler.step_correct(
																											    lowerCamelCase__				,							lowerCamelCase__				,							lowerCamelCase__				,							lowerCamelCase__				,							step_output.prev_sample				,							step_output['''derivative''']				,							)
																				__lowercase      =    step_output.prev_sample

													__lowercase      =    (sample / 2 + 0.5).clamp(0				,							1				)
													__lowercase      =    sample.cpu().permute(0				,							2				,							3				,							1				).numpy()
													if output_type == "pil":
																				__lowercase      =    self.numpy_to_pil(lowerCamelCase__				)

													if not return_dict:
																				return (image,)

													return ImagePipelineOutput(images=lowerCamelCase__				)






