
# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowercase_ 				=			abspath(join(dirname(__file__), """src"""))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="""ignore""", category=FutureWarning)


def 							a__					(							snake_case							):





  """simple docstring"""
  config.addinivalue_line(
      '''markers'''       ,			'''is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested'''							)
  config.addinivalue_line(
      '''markers'''       ,			'''is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested'''							)
  config.addinivalue_line('''markers'''       ,			'''is_pipeline_test: mark test to run only when pipelines are tested'''							)
  config.addinivalue_line('''markers'''       ,			'''is_staging_test: mark test to run only in the staging environment'''							)
  config.addinivalue_line('''markers'''       ,			'''accelerate_tests: mark test that require accelerate'''							)
  config.addinivalue_line('''markers'''       ,			'''tool_tests: mark the tool tests that are run on their specific schedule'''							)


def 							a__					(							snake_case							):





  """simple docstring"""
  from transformers.testing_utils import pytest_addoption_shared

  pytest_addoption_shared(snake_case							)


def 							a__					(							snake_case							):





  """simple docstring"""
  from transformers.testing_utils import pytest_terminal_summary_main

  __SCREAMING_SNAKE_CASE      :	List[Any]					=		terminalreporter.config.getoption('''--make-reports'''							)
  if make_reports:
    pytest_terminal_summary_main(snake_case       ,			id=snake_case							)


def 							a__					(							snake_case       ,			snake_case							):





  """simple docstring"""
  # If no tests are collected, pytest exists with code 5, which makes the CI fail.
  if exitstatus == 5:
    __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		0


# Doctest custom flag to ignore output.
lowercase_ 				=			doctest.register_optionflag("""IGNORE_RESULT""")

lowercase_ 				=			doctest.OutputChecker





class     __UpperCamelCase (							lowerCAmelCase__       ):






     """simple docstring"""







     def  UpperCAmelCase__							(				self							:							int     ,						_A							:							Any     ,						_A							:							Tuple     ,						_A							:							Optional[int] ):







       """simple docstring"""




       if IGNORE_RESULT & optionflags:
         return True
       return OutputChecker.check_output(self     ,						_A     ,						_A     ,						_A )






lowercase_ 				=			CustomOutputChecker
lowercase_ 				=			HfDoctestModule


lowercase_ 				=			HfDocTestParser



'''simple docstring'''







import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class        A__   (   unittest.TestCase      ):



    @property
    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           torch.manual_seed(0							)
           A_						       =   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							)    ->   str:





           '''simple docstring'''
           A_						       =   self.dummy_uncond_unet
           A_						       =   PNDMScheduler()

           A_						       =   PNDMPipeline(unet=UpperCamelCase__		,					scheduler=UpperCamelCase__							)
           pndm.to(UpperCamelCase__							)
           pndm.set_progress_bar_config(disable=UpperCamelCase__							)

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

           A_						       =   torch.manual_seed(0							)
           A_						       =   pndm(generator=UpperCamelCase__		,					num_inference_steps=20		,					output_type="""numpy"""		,					return_dict=UpperCamelCase__							)[0]

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

           assert image.shape == (1, 32, 32, 3)
           A_						       =   np.array([1.0, 1.0, 0.0, 1.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        A__   (   unittest.TestCase      ):



    def        snake_case_      (       self							)    ->   Tuple:





           '''simple docstring'''
           A_						       =   """google/ddpm-cifar10-32"""

           A_						       =   UNetaDModel.from_pretrained(UpperCamelCase__							)
           A_						       =   PNDMScheduler()

           A_						       =   PNDMPipeline(unet=UpperCamelCase__		,					scheduler=UpperCamelCase__							)
           pndm.to(UpperCamelCase__							)
           pndm.set_progress_bar_config(disable=UpperCamelCase__							)
           A_						       =   torch.manual_seed(0							)
           A_						       =   pndm(generator=UpperCamelCase__		,					output_type="""numpy"""							).images

           A_						       =   image[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           A_						       =   np.array([0.1564, 0.14645, 0.1406, 0.14715, 0.12425, 0.14045, 0.13115, 0.12175, 0.125]							)

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