


'''simple docstring'''
import gc
import threading
import time

import psutil
import torch

class 						_lowerCamelCase					:




 '''simple docstring'''

 def __init__(      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			psutil.Process()
  __A  :		Optional[Any]       =			False

 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[Any]       =			-1

  while True:
   __A  :		List[str]       =			max(self.process.memory_info().rss      ,      self.cpu_memory_peak						)

   # can't sleep or will not catch the peak right (this comment is here on purpose)
   if not self.peak_monitoring:
    break

 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			True
  __A  :		str       =			threading.Thread(target=self.peak_monitor						)
  __A  :		int       =			True
  self.thread.start()







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[str]       =			False
  self.thread.join()
  return self.cpu_memory_peak


UpperCAmelCase_ :     List[Any]								=      PeakCPUMemory()


def 			_lowercase       (				):
 # Time
 __A  :		Tuple       =			{'time': time.time()}

 gc.collect()
 torch.cuda.empty_cache()

 # CPU mem
 __A  :		Optional[int]       =			psutil.Process().memory_info().rss
 cpu_peak_tracker.start()

 # GPU mem
 for i in range(torch.cuda.device_count()			):
  __A  :		List[Any]       =			torch.cuda.memory_allocated(UpperCamelCase__			)
 torch.cuda.reset_peak_memory_stats()

 return measures


def 			_lowercase       (				UpperCamelCase__       :					Optional[int]			):
 # Time
 __A  :		Optional[Any]       =			{'time': time.time() - start_measures['time']}

 gc.collect()
 torch.cuda.empty_cache()

 # CPU mem
 __A  :		Tuple       =			(psutil.Process().memory_info().rss - start_measures['cpu']) / 2**20
 __A  :		Optional[int]       =			(cpu_peak_tracker.stop() - start_measures['cpu']) / 2**20

 # GPU mem
 for i in range(torch.cuda.device_count()			):
  __A  :		Dict       =			(torch.cuda.memory_allocated(UpperCamelCase__			) - start_measures[str(UpperCamelCase__			)]) / 2**20
  __A  :		Optional[Any]       =			(torch.cuda.max_memory_allocated(UpperCamelCase__			) - start_measures[str(UpperCamelCase__			)]) / 2**20

 return measures


def 			_lowercase       (				UpperCamelCase__       :					List[Any],				UpperCamelCase__       :					Optional[int]			):
 print(f"""{description}:"""			)
 print(f"""- Time: {measures['time']:.2f}s"""			)
 for i in range(torch.cuda.device_count()			):
  print(f"""- GPU {i} allocated: {measures[str(UpperCamelCase__			)]:.2f}MiB"""			)
  __A  :		Tuple       =			measures[f"""{i}-peak"""]
  print(f"""- GPU {i} peak: {peak:.2f}MiB"""			)
 print(f"""- CPU RAM allocated: {measures['cpu']:.2f}MiB"""			)
 print(f"""- CPU RAM peak: {measures['cpu-peak']:.2f}MiB"""			)










'''simple docstring'''
import unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow

@is_pipeline_test
@require_torch
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			pipeline(
      task='zero-shot-audio-classification'      ,      model='hf-internal-testing/tiny-clap-htsat-unfused'						)
  __A  :		str       =			load_dataset('ashraq/esc50'						)
  __A  :		Union[str, Any]       =			dataset['train']['audio'][-1]['array']
  __A  :		List[str]       =			audio_classifier(__lowercase      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [{'score': 0.5_0_1, 'label': 'Sound of a dog'}, {'score': 0.4_9_9, 'label': 'Sound of vaccum cleaner'}]      ,      )


 @unittest.skip('No models are available in TF'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @slow
 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			pipeline(
      task='zero-shot-audio-classification'      ,      model='laion/clap-htsat-unfused'      ,      )
  # This is an audio of a dog
  __A  :		Optional[Any]       =			load_dataset('ashraq/esc50'						)
  __A  :		List[str]       =			dataset['train']['audio'][-1]['array']
  __A  :		Union[str, Any]       =			audio_classifier(__lowercase      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)

  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          {'score': 0.9_9_9, 'label': 'Sound of a dog'},
          {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
      ]      ,      )

  __A  :		Any       =			audio_classifier([audio] * 5      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          [
              {'score': 0.9_9_9, 'label': 'Sound of a dog'},
              {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
          ],
      ]
      * 5      ,      )
  __A  :		str       =			audio_classifier(
      [audio] * 5      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']      ,      batch_size=5						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          [
              {'score': 0.9_9_9, 'label': 'Sound of a dog'},
              {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
          ],
      ]
      * 5      ,      )







 @unittest.skip('No models are available in TF'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass






