
"""simple docstring"""



import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class        _SCREAMING_SNAKE_CASE  (  unittest.TestCase							):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE_:							Dict							=					MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				SCREAMING_SNAKE_CASE_:							int							=					TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


				def 				__lowerCamelCase	(					self	:			Any				,     UpperCAmelCase_	:			Union[str, Any]				,     UpperCAmelCase_	:			List[str]				,     UpperCAmelCase_	:			Optional[int]  )		->  Dict:







											"""simple docstring"""




											_lowerCAmelCase  	=			AudioClassificationPipeline(model=UpperCAmelCase_				,     feature_extractor=UpperCAmelCase_  )

											# test with a raw waveform
											_lowerCAmelCase  	=			np.zeros((34_000,)  )
											_lowerCAmelCase  	=			np.zeros((14_000,)  )
											return audio_classifier, [audioa, audio]


				def 				__lowerCamelCase	(					self	:			Optional[int]				,     UpperCAmelCase_	:			int				,     UpperCAmelCase_	:			Any  )		->  List[str]:







											"""simple docstring"""




											_lowerCAmelCase	,		_lowerCAmelCase  	=			examples
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_  )
											# by default a model is initialized with num_labels=2
											self.assertEqual(
											    UpperCAmelCase_				,     [
											        {'score': ANY(UpperCAmelCase_  ), 'label': ANY(UpperCAmelCase_  )},
											        {'score': ANY(UpperCAmelCase_  ), 'label': ANY(UpperCAmelCase_  )},
											    ]				,     )
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_				,     top_k=1  )
											self.assertEqual(
											    UpperCAmelCase_				,     [
											        {'score': ANY(UpperCAmelCase_  ), 'label': ANY(UpperCAmelCase_  )},
											    ]				,     )

											self.run_torchaudio(UpperCAmelCase_  )


				@require_torchaudio
				def 				__lowerCamelCase	(					self	:			Optional[Any]				,     UpperCAmelCase_	:			List[str]  )		->  Optional[int]:







											"""simple docstring"""




											import datasets

											# test with a local file
											_lowerCAmelCase  	=			datasets.load_dataset('hf-internal-testing/librispeech_asr_dummy'				,     'clean'				,     split='validation'  )
											_lowerCAmelCase  	=			dataset[0]['audio']['array']
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_  )
											self.assertEqual(
											    UpperCAmelCase_				,     [
											        {'score': ANY(UpperCAmelCase_  ), 'label': ANY(UpperCAmelCase_  )},
											        {'score': ANY(UpperCAmelCase_  ), 'label': ANY(UpperCAmelCase_  )},
											    ]				,     )


				@require_torch
				def 				__lowerCamelCase	(					self	:			Dict  )		->  int:







											"""simple docstring"""




											_lowerCAmelCase  	=			'anton-l/wav2vec2-random-tiny-classifier'

											_lowerCAmelCase  	=			pipeline('audio-classification'				,     model=UpperCAmelCase_  )

											_lowerCAmelCase  	=			np.ones((8_000,)  )
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_				,     top_k=4  )

											_lowerCAmelCase  	=			[
											    {'score': 0.0842, 'label': 'no'},
											    {'score': 0.0838, 'label': 'up'},
											    {'score': 0.0837, 'label': 'go'},
											    {'score': 0.0834, 'label': 'right'},
											]
											_lowerCAmelCase  	=			[
											    {'score': 0.0845, 'label': 'stop'},
											    {'score': 0.0844, 'label': 'on'},
											    {'score': 0.0841, 'label': 'right'},
											    {'score': 0.0834, 'label': 'left'},
											]
											self.assertIn(nested_simplify(UpperCAmelCase_				,     decimals=4  )				,     [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

											_lowerCAmelCase  	=			{'array': np.ones((8_000,)  ), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_				,     top_k=4  )
											self.assertIn(nested_simplify(UpperCAmelCase_				,     decimals=4  )				,     [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )


				@require_torch
				@slow
				def 				__lowerCamelCase	(					self	:			List[Any]  )		->  Union[str, Any]:







											"""simple docstring"""




											import datasets

											_lowerCAmelCase  	=			'superb/wav2vec2-base-superb-ks'

											_lowerCAmelCase  	=			pipeline('audio-classification'				,     model=UpperCAmelCase_  )
											_lowerCAmelCase  	=			datasets.load_dataset('anton-l/superb_dummy'				,     'ks'				,     split='test'  )

											_lowerCAmelCase  	=			np.array(dataset[3]['speech']				,     dtype=np.floataa  )
											_lowerCAmelCase  	=			audio_classifier(UpperCAmelCase_				,     top_k=4  )
											self.assertEqual(
											    nested_simplify(UpperCAmelCase_				,     decimals=3  )				,     [
											        {'score': 0.981, 'label': 'go'},
											        {'score': 0.007, 'label': 'up'},
											        {'score': 0.006, 'label': '_unknown_'},
											        {'score': 0.001, 'label': 'down'},
											    ]				,     )







				@require_tf
				@unittest.skip('Audio classification is not implemented for TF'  )
				def 				__lowerCamelCase	(					self	:			Optional[Any]  )		->  Optional[Any]:







											"""simple docstring"""




											pass








"""simple docstring"""



import argparse

import intel_extension_for_pytorch as ipex
import torch

from diffusers import DPMSolverMultistepScheduler, StableDiffusionPipeline


_snake_case          =						argparse.ArgumentParser('''Stable Diffusion script with intel optimization''', add_help=False)
parser.add_argument('''--dpm''', action='''store_true''', help='''Enable DPMSolver or not''')
parser.add_argument('''--steps''', default=None, type=int, help='''Num inference steps''')
_snake_case          =						parser.parse_args()


_snake_case          =						'''cpu'''
_snake_case          =						'''a lovely <dicoo> in red dress and hat, in the snowly and brightly night, with many brighly buildings'''

_snake_case          =						'''path-to-your-trained-model'''
_snake_case          =						StableDiffusionPipeline.from_pretrained(model_id)
if args.dpm:
	_snake_case          =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
_snake_case          =						pipe.to(device)

# to channels last
_snake_case          =						pipe.unet.to(memory_format=torch.channels_last)
_snake_case          =						pipe.vae.to(memory_format=torch.channels_last)
_snake_case          =						pipe.text_encoder.to(memory_format=torch.channels_last)
if pipe.requires_safety_checker:
	_snake_case          =						pipe.safety_checker.to(memory_format=torch.channels_last)

# optimize with ipex
_snake_case          =						torch.randn(2, 4, 6_4, 6_4)
_snake_case          =						torch.rand(1) * 9_9_9
_snake_case          =						torch.randn(2, 7_7, 7_6_8)
_snake_case          =						(sample, timestep, encoder_hidden_status)
try:
	_snake_case          =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True, sample_input=input_example)
except Exception:
	_snake_case          =						ipex.optimize(pipe.unet.eval(), dtype=torch.bfloataa, inplace=True)
_snake_case          =						ipex.optimize(pipe.vae.eval(), dtype=torch.bfloataa, inplace=True)
_snake_case          =						ipex.optimize(pipe.text_encoder.eval(), dtype=torch.bfloataa, inplace=True)
if pipe.requires_safety_checker:
	_snake_case          =						ipex.optimize(pipe.safety_checker.eval(), dtype=torch.bfloataa, inplace=True)

# compute
_snake_case          =						6_6_6
_snake_case          =						torch.Generator(device).manual_seed(seed)
_snake_case          =						{'''generator''': generator}
if args.steps is not None:
	_snake_case          =						args.steps

with torch.cpu.amp.autocast(enabled=True, dtype=torch.bfloataa):
	_snake_case          =						pipe(prompt, **generate_kwargs).images[0]

# save image
image.save('''generated.png''')






