

"""simple docstring"""





import random
import unittest

import torch

from diffusers import IFImgaImgSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin




@skip_mps
class  _lowerCAmelCase  (     lowercase					,lowercase					,unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : Any       		=     IFImgaImgSuperResolutionPipeline
 __UpperCAmelCase     : str       		=     TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
 __UpperCAmelCase     : int       		=     TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"original_image"}       )
 __UpperCAmelCase     : Optional[Any]       		=     PipelineTesterMixin.required_optional_params - {"latents"}


 def  _lowercase			(      self							:      Tuple     ):
    return self._get_superresolution_dummy_components()


 def  _lowercase			(      self							:      Optional[Any], UpperCAmelCase__							:      Union[str, Any], UpperCAmelCase__							:      List[Any]=0     ):
    if str(UpperCAmelCase__     ).startswith("mps"     ):
       __lowercase								=						torch.manual_seed(UpperCAmelCase__     )
    else:
       __lowercase								=						torch.Generator(device=UpperCAmelCase__     ).manual_seed(UpperCAmelCase__     )

    __lowercase								=						floats_tensor((1, 3, 3_2, 3_2), rng=random.Random(UpperCAmelCase__     )     ).to(UpperCAmelCase__     )
    __lowercase								=						floats_tensor((1, 3, 1_6, 1_6), rng=random.Random(UpperCAmelCase__     )     ).to(UpperCAmelCase__     )

    __lowercase								=						{
        "prompt": "A painting of a squirrel eating a burger",
        "image": image,
        "original_image": original_image,
        "generator": generator,
        "num_inference_steps": 2,
        "output_type": "numpy",
    }

    return inputs


 @unittest.skipIf(
     torch_device != "cuda" or not is_xformers_available(), reason="XFormers attention is only available with CUDA and `xformers` installed", )
 def  _lowercase			(      self							:      Optional[Any]     ):
    self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3     )


 def  _lowercase			(      self							:      Union[str, Any]     ):
    self._test_save_load_optional_components()


 @unittest.skipIf(torch_device != "cuda", reason="float16 requires CUDA"     )
 def  _lowercase			(      self							:      List[str]     ):
    # Due to non-determinism in save load of the hf-internal-testing/tiny-random-t5 text encoder
    super().test_save_load_floataa(expected_max_diff=1E-1     )


 def  _lowercase			(      self							:      List[str]     ):
    self._test_attention_slicing_forward_pass(expected_max_diff=1E-2     )


 def  _lowercase			(      self							:      Optional[int]     ):
    self._test_save_load_local()



 def  _lowercase			(      self							:      Union[str, Any]     ):
    self._test_inference_batch_single_identical(
        expected_max_diff=1E-2, )







"""simple docstring"""





import numpy

# List of input, output pairs
_a							=							(
    ((5, 2, 3), 15),
    ((6, 5, 9), 25),
    ((11, 12, 13), 41),
    ((1, 1, 1), 8),
    ((11, 12, 13), 41),
)
_a							=							(((5_15, 22, 13), 5_55), ((61, 35, 49), 1_50))
_a							=							[2, 4, 1, 5]
_a							=							len(train_data)
_a							=							0.009




def   _A  (  UpperCamelCase_					:	str,      UpperCamelCase_					:	List[Any]="train")     ->						Optional[Any]:
   '''simple docstring'''


   return calculate_hypothesis_value(UpperCamelCase_,      UpperCamelCase_) - output(
       UpperCamelCase_,      UpperCamelCase_)




def   _A  (  UpperCamelCase_					:	List[Any])     ->						Union[str, Any]:
   '''simple docstring'''


   __lowercase								=						0
   for i in range(len(UpperCamelCase_) - 1):
      hyp_val += data_input_tuple[i] * parameter_vector[i + 1]
   hyp_val += parameter_vector[0]
   return hyp_val




def   _A  (  UpperCamelCase_					:	Dict,      UpperCamelCase_					:	Optional[int])     ->						Dict:
   '''simple docstring'''


   if data_set == "train":
      return train_data[example_no][1]
   elif data_set == "test":
      return test_data[example_no][1]
   return None




def   _A  (  UpperCamelCase_					:	Dict,      UpperCamelCase_					:	List[str])     ->						int:
   '''simple docstring'''


   if data_set == "train":
      return _hypothesis_value(train_data[example_no][0])
   elif data_set == "test":
      return _hypothesis_value(test_data[example_no][0])
   return None




def   _A  (  UpperCamelCase_					:	Any,      UpperCamelCase_					:	Tuple=m)     ->						int:
   '''simple docstring'''


   __lowercase								=						0
   for i in range(UpperCamelCase_):
      if index == -1:
         summation_value += _error(UpperCamelCase_)
      else:
         summation_value += _error(UpperCamelCase_) * train_data[i][0][index]
   return summation_value




def   _A  (  UpperCamelCase_					:	str)     ->						str:
   '''simple docstring'''


   __lowercase								=						summation_of_cost_derivative(UpperCamelCase_,      UpperCamelCase_) / m
   return cost_derivative_value




def   _A  (  )     ->						List[str]:
   '''simple docstring'''


   global parameter_vector
   # Tune these values to set a tolerance value for predicted output
   __lowercase								=						0.000_002
   __lowercase								=						0
   __lowercase								=						0
   while True:
      j += 1
      __lowercase								=						[0, 0, 0, 0]
      for i in range(0,      len(UpperCamelCase_)):
         __lowercase								=						get_cost_derivative(i - 1)
         __lowercase								=						(
             parameter_vector[i] - LEARNING_RATE * cost_derivative
         )
      if numpy.allclose(
          UpperCamelCase_,      UpperCamelCase_,      atol=UpperCamelCase_,      rtol=UpperCamelCase_,      ):
         break
      __lowercase								=						temp_parameter_vector
   print(("Number of iterations:", j))




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


   for i in range(len(UpperCamelCase_)):
      print(("Actual output value:", output(UpperCamelCase_,      "test")))
      print(("Hypothesis output:", calculate_hypothesis_value(UpperCamelCase_,      "test")))


if __name__ == "__main__":
   run_gradient_descent()
   print('\nTesting gradient descent for a linear hypothesis function.\n')
   test_gradient_descent()




