
'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNetaDModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class  __A				( _SCREAMING_SNAKE_CASE    ,						unittest.TestCase			):
 lowerCamelCase				       =DanceDiffusionPipeline
 lowerCamelCase				       =UNCONDITIONAL_AUDIO_GENERATION_PARAMS
 lowerCamelCase				       =PipelineTesterMixin.required_optional_params - {
     '''callback''',
     '''latents''',
     '''callback_steps''',
     '''output_type''',
     '''num_images_per_prompt''',
 }
 lowerCamelCase				       =UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS
 lowerCamelCase				       =False
 lowerCamelCase				       =False
 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





   torch.manual_seed(0       )
   __A		:     str												=		UNetaDModel(
       block_out_channels=(32, 32, 64)				,   extra_in_channels=16				,   sample_size=5_12				,   sample_rate=1_60_00				,   in_channels=2				,   out_channels=2				,   flip_sin_to_cos=lowerCamelCase				,   use_timestep_embedding=lowerCamelCase				,   time_embedding_type="""fourier"""				,   mid_block_type="""UNetMidBlock1D"""				,   down_block_types=("""DownBlock1DNoSkip""", """DownBlock1D""", """AttnDownBlock1D""")				,   up_block_types=("""AttnUpBlock1D""", """UpBlock1D""", """UpBlock1DNoSkip""")				,   )
   __A		:     Dict												=		IPNDMScheduler()

   __A		:     Tuple												=		{
       """unet""": unet,
       """scheduler""": scheduler,
   }
   return components
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Dict=0       ):



   """simple docstring"""





   if str(lowerCamelCase       ).startswith("""mps"""       ):
     __A		:     str												=		torch.manual_seed(lowerCamelCase       )
   else:
     __A		:     str												=		torch.Generator(device=lowerCamelCase       ).manual_seed(lowerCamelCase       )
   __A		:     Tuple												=		{
       """batch_size""": 1,
       """generator""": generator,
       """num_inference_steps""": 4,
   }
   return inputs
 def 		lowercase_(   self			:    int       ):



   """simple docstring"""





   __A		:     Any												=		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
   __A		:     List[str]												=		self.get_dummy_components()
   __A		:     Tuple												=		DanceDiffusionPipeline(**lowerCamelCase       )
   __A		:     List[Any]												=		pipe.to(lowerCamelCase       )
   pipe.set_progress_bar_config(disable=lowerCamelCase       )

   __A		:     Union[str, Any]												=		self.get_dummy_inputs(lowerCamelCase       )
   __A		:     Union[str, Any]												=		pipe(**lowerCamelCase       )
   __A		:     Dict												=		output.audios

   __A		:     Dict												=		audio[0, -3:, -3:]

   assert audio.shape == (1, 2, components["unet"].sample_size)
   __A		:     int												=		np.array([-0.7_265, 1.0_000, -0.8_388, 0.1_175, 0.9_498, -1.0_000]       )
   assert np.abs(audio_slice.flatten() - expected_slice       ).max() < 1e-2
 @skip_mps
 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   return super().test_save_load_local()
 @skip_mps
 def 		lowercase_(   self			:    int       ):



   """simple docstring"""





   return super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3       )
 @skip_mps
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   return super().test_save_load_optional_components()
 @skip_mps
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   return super().test_attention_slicing_forward_pass()






 def 		lowercase_(   self			:    Dict       ):



   """simple docstring"""





   super().test_inference_batch_single_identical(expected_max_diff=3e-3       )







@slow
@require_torch_gpu
class  __A				( unittest.TestCase			):
 def 		lowercase_(   self			:    str       ):



   """simple docstring"""





   super().tearDown()
   gc.collect()
   torch.cuda.empty_cache()
 def 		lowercase_(   self			:    int       ):



   """simple docstring"""





   __A		:     Union[str, Any]												=		torch_device

   __A		:     int												=		DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""       )
   __A		:     Optional[Any]												=		pipe.to(lowerCamelCase       )
   pipe.set_progress_bar_config(disable=lowerCamelCase       )

   __A		:     Union[str, Any]												=		torch.manual_seed(0       )
   __A		:     int												=		pipe(generator=lowerCamelCase				,   num_inference_steps=1_00				,   audio_length_in_s=4.096       )
   __A		:     Optional[int]												=		output.audios

   __A		:     int												=		audio[0, -3:, -3:]

   assert audio.shape == (1, 2, pipe.unet.sample_size)
   __A		:     Optional[int]												=		np.array([-0.0_192, -0.0_231, -0.0_318, -0.0_059, 0.0_002, -0.0_020]       )

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






 def 		lowercase_(   self			:    Union[str, Any]       ):



   """simple docstring"""





   __A		:     Dict												=		torch_device

   __A		:     List[str]												=		DanceDiffusionPipeline.from_pretrained("""harmonai/maestro-150k"""				,   torch_dtype=torch.floataa       )
   __A		:     Any												=		pipe.to(lowerCamelCase       )
   pipe.set_progress_bar_config(disable=lowerCamelCase       )

   __A		:     Optional[int]												=		torch.manual_seed(0       )
   __A		:     Optional[Any]												=		pipe(generator=lowerCamelCase				,   num_inference_steps=1_00				,   audio_length_in_s=4.096       )
   __A		:     Optional[int]												=		output.audios

   __A		:     Union[str, Any]												=		audio[0, -3:, -3:]

   assert audio.shape == (1, 2, pipe.unet.sample_size)
   __A		:     Optional[int]												=		np.array([-0.0_367, -0.0_488, -0.0_771, -0.0_525, -0.0_444, -0.0_341]       )

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





'''simple docstring'''
from collections.abc import Sequence







def        A_     (						__SCREAMING_SNAKE_CASE						:  Sequence[float]			,     __SCREAMING_SNAKE_CASE						:  bool = False )				->       float:





  """simple docstring"""

  if not arr:
    return 0

  __A		:     Any												=		0 if allow_empty_subarrays else float("""-inf""" )
  __A		:     List[Any]												=		0.0
  for num in arr:
    __A		:     Tuple												=		max(0 if allow_empty_subarrays else num			,     curr_sum + num )
    __A		:     Optional[int]												=		max(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE )

  return max_sum


if __name__ == "__main__":
     from doctest import testmod

     testmod()

     A__   :		Any 		=[-2, 1, -3, 4, -1, 2, 1, -5, 4]
     print(F"{max_subarray_sum(nums) = }")



