




"""simple docstring"""





import shutil
import tempfile
import unittest
from unittest.mock import patch

from transformers import (
    DefaultFlowCallback,
    IntervalStrategy,
    PrinterCallback,
    ProgressCallback,
    Trainer,
    TrainerCallback,
    TrainingArguments,
    is_torch_available,
)
from transformers.testing_utils import require_torch


if is_torch_available():
					from transformers.trainer import DEFAULT_CALLBACKS

					from .test_trainer import RegressionDataset, RegressionModelConfig, RegressionPreTrainedModel



class      a__				(	__magic_name__   ):


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


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					[]


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Optional[Any]      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




						self.events.append("on_init_end")


			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Dict      ,  **UpperCamelCase_      :    int):


						"""simple docstring"""




						self.events.append("on_train_begin")


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Any      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




						self.events.append("on_train_end")


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    List[Any]      ,  **UpperCamelCase_      :    int):


						"""simple docstring"""




						self.events.append("on_epoch_begin")


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict      ,  **UpperCamelCase_      :    Tuple):


						"""simple docstring"""




						self.events.append("on_epoch_end")


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    List[Any]      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict      ,  **UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						self.events.append("on_step_begin")


			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    List[str]      ,  **UpperCamelCase_      :    Dict):


						"""simple docstring"""




						self.events.append("on_step_end")


			def       a_     (      self      :    Tuple      ,  UpperCamelCase_      :    List[str]      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    Dict      ,  **UpperCamelCase_      :    List[str]):


						"""simple docstring"""




						self.events.append("on_evaluate")


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    Optional[Any]      ,  **UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						self.events.append("on_predict")


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    str      ,  UpperCamelCase_      :    Union[str, Any]      ,  **UpperCamelCase_      :    Any):


						"""simple docstring"""




						self.events.append("on_save")


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    Union[str, Any]      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    Tuple      ,  **UpperCamelCase_      :    Dict):


						"""simple docstring"""




						self.events.append("on_log")


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    Tuple      ,  UpperCamelCase_      :    int      ,  UpperCamelCase_      :    Dict      ,  **UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						self.events.append("on_prediction_step")






@require_torch
class      a__				(	unittest.TestCase   ):


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					tempfile.mkdtemp()


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						shutil.rmtree(self.output_dir)


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    Optional[int]=0      ,  UpperCamelCase_      :    List[Any]=0      ,  UpperCamelCase_      :    Dict=64      ,  UpperCamelCase_      :    Tuple=64      ,  UpperCamelCase_      :    Any=None      ,  UpperCamelCase_      :    Optional[Any]=False      ,  **UpperCamelCase_      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					RegressionDataset(length=UpperCamelCase_)
						__UpperCAmelCase	: Tuple		=					RegressionDataset(length=UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					RegressionModelConfig(a=UpperCamelCase_      ,  b=UpperCamelCase_)
						__UpperCAmelCase	: Optional[Any]		=					RegressionPreTrainedModel(UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					TrainingArguments(self.output_dir      ,  disable_tqdm=UpperCamelCase_      ,  report_to=[]      ,  **UpperCamelCase_)
						return Trainer(
						    UpperCamelCase_      ,  UpperCamelCase_      ,  train_dataset=UpperCamelCase_      ,  eval_dataset=UpperCamelCase_      ,  callbacks=UpperCamelCase_      ,  )


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    Optional[Any]      ,  UpperCamelCase_      :    List[str]):


						"""simple docstring"""




						self.assertEqual(len(UpperCamelCase_)      ,  len(UpperCamelCase_))

						# Order doesn't matter
						__UpperCAmelCase	: Union[str, Any]		=					sorted(UpperCamelCase_      ,  key=lambda UpperCamelCase_: cb.__name__ if isinstance(UpperCamelCase_      ,  UpperCamelCase_) else cb.__class__.__name__)
						__UpperCAmelCase	: List[Any]		=					sorted(UpperCamelCase_      ,  key=lambda UpperCamelCase_: cb.__name__ if isinstance(UpperCamelCase_      ,  UpperCamelCase_) else cb.__class__.__name__)

						for cba, cba in zip(UpperCamelCase_      ,  UpperCamelCase_):
									if isinstance(UpperCamelCase_      ,  UpperCamelCase_) and isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												self.assertEqual(UpperCamelCase_      ,  UpperCamelCase_)
									elif isinstance(UpperCamelCase_      ,  UpperCamelCase_) and not isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												self.assertEqual(UpperCamelCase_      ,  cba.__class__)
									elif not isinstance(UpperCamelCase_      ,  UpperCamelCase_) and isinstance(UpperCamelCase_      ,  UpperCamelCase_):
												self.assertEqual(cba.__class__      ,  UpperCamelCase_)
									else:
												self.assertEqual(UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					["on_init_end", "on_train_begin"]
						__UpperCAmelCase	: List[str]		=					0
						__UpperCAmelCase	: Optional[Any]		=					len(trainer.get_eval_dataloader())
						__UpperCAmelCase	: Dict		=					["on_prediction_step"] * len(trainer.get_eval_dataloader()) + ["on_log", "on_evaluate"]
						for _ in range(trainer.state.num_train_epochs):
									expected_events.append("on_epoch_begin")
									for _ in range(UpperCamelCase_):
												step += 1
												expected_events += ["on_step_begin", "on_step_end"]
												if step % trainer.args.logging_steps == 0:
															expected_events.append("on_log")
												if trainer.args.evaluation_strategy == IntervalStrategy.STEPS and step % trainer.args.eval_steps == 0:
															expected_events += evaluation_events.copy()
												if step % trainer.args.save_steps == 0:
															expected_events.append("on_save")
									expected_events.append("on_epoch_end")
									if trainer.args.evaluation_strategy == IntervalStrategy.EPOCH:
												expected_events += evaluation_events.copy()
						expected_events += ["on_log", "on_train_end"]
						return expected_events


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Dict		=					self.get_trainer()
						__UpperCAmelCase	: List[Any]		=					DEFAULT_CALLBACKS.copy() + [ProgressCallback]
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						# Callbacks passed at init are added to the default callbacks
						__UpperCAmelCase	: Optional[Any]		=					self.get_trainer(callbacks=[MyTestTrainerCallback])
						expected_callbacks.append(UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
						__UpperCAmelCase	: List[Any]		=					self.get_trainer(disable_tqdm=UpperCamelCase_)
						__UpperCAmelCase	: Tuple		=					DEFAULT_CALLBACKS.copy() + [PrinterCallback]
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					DEFAULT_CALLBACKS.copy() + [ProgressCallback]
						__UpperCAmelCase	: List[Any]		=					self.get_trainer()

						# We can add, pop, or remove by class name
						trainer.remove_callback(UpperCamelCase_)
						expected_callbacks.remove(UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					self.get_trainer()
						__UpperCAmelCase	: Any		=					trainer.pop_callback(UpperCamelCase_)
						self.assertEqual(cb.__class__      ,  UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						trainer.add_callback(UpperCamelCase_)
						expected_callbacks.insert(0      ,  UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						# We can also add, pop, or remove by instance
						__UpperCAmelCase	: Tuple		=					self.get_trainer()
						__UpperCAmelCase	: Optional[Any]		=					trainer.callback_handler.callbacks[0]
						trainer.remove_callback(UpperCamelCase_)
						expected_callbacks.remove(UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					self.get_trainer()
						__UpperCAmelCase	: Optional[int]		=					trainer.callback_handler.callbacks[0]
						__UpperCAmelCase	: Optional[int]		=					trainer.pop_callback(UpperCamelCase_)
						self.assertEqual(UpperCamelCase_      ,  UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)

						trainer.add_callback(UpperCamelCase_)
						expected_callbacks.insert(0      ,  UpperCamelCase_)
						self.check_callbacks_equality(trainer.callback_handler.callbacks      ,  UpperCamelCase_)


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						import warnings

						# XXX: for now ignore scatter_gather warnings in this test since it's not relevant to what's being tested
						warnings.simplefilter(action="ignore"      ,  category=UpperCamelCase_)

						__UpperCAmelCase	: Union[str, Any]		=					self.get_trainer(callbacks=[MyTestTrainerCallback])
						trainer.train()
						__UpperCAmelCase	: Dict		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						# Independent log/save/eval
						__UpperCAmelCase	: Optional[int]		=					self.get_trainer(callbacks=[MyTestTrainerCallback]      ,  logging_steps=5)
						trainer.train()
						__UpperCAmelCase	: Dict		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						__UpperCAmelCase	: Dict		=					self.get_trainer(callbacks=[MyTestTrainerCallback]      ,  save_steps=5)
						trainer.train()
						__UpperCAmelCase	: Any		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						__UpperCAmelCase	: int		=					self.get_trainer(callbacks=[MyTestTrainerCallback]      ,  eval_steps=5      ,  evaluation_strategy="steps")
						trainer.train()
						__UpperCAmelCase	: int		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						__UpperCAmelCase	: str		=					self.get_trainer(callbacks=[MyTestTrainerCallback]      ,  evaluation_strategy="epoch")
						trainer.train()
						__UpperCAmelCase	: Union[str, Any]		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						# A bit of everything
						__UpperCAmelCase	: Any		=					self.get_trainer(
						    callbacks=[MyTestTrainerCallback]      ,  logging_steps=3      ,  save_steps=10      ,  eval_steps=5      ,  evaluation_strategy="steps"      ,  )
						trainer.train()
						__UpperCAmelCase	: Optional[Any]		=					trainer.callback_handler.callbacks[-2].events
						self.assertEqual(UpperCamelCase_      ,  self.get_expected_events(UpperCamelCase_))

						# warning should be emitted for duplicated callbacks
						with patch("transformers.trainer_callback.logger.warning") as warn_mock:
									__UpperCAmelCase	: Dict		=					self.get_trainer(
									    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]      ,  )
									assert str(UpperCamelCase_) in warn_mock.call_args[0][0]








"""simple docstring"""





import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list



@require_torchaudio
@require_sentencepiece
class      a__				(	unittest.TestCase   ):


			def       a_     (      self      :    Any):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					"laion/clap-htsat-unfused"
						__UpperCAmelCase	: List[str]		=					tempfile.mkdtemp()


			def       a_     (      self      :    Optional[Any]      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




						return RobertaTokenizer.from_pretrained(self.checkpoint      ,  **UpperCamelCase_)


			def       a_     (      self      :    Union[str, Any]      ,  **UpperCamelCase_      :    Optional[int]):


						"""simple docstring"""




						return ClapFeatureExtractor.from_pretrained(self.checkpoint      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						shutil.rmtree(self.tmpdirname)


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					self.get_tokenizer()
						__UpperCAmelCase	: str		=					self.get_feature_extractor()

						__UpperCAmelCase	: List[Any]		=					ClapProcessor(tokenizer=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						processor.save_pretrained(self.tmpdirname)
						__UpperCAmelCase	: Union[str, Any]		=					ClapProcessor.from_pretrained(self.tmpdirname)

						self.assertEqual(processor.tokenizer.get_vocab()      ,  tokenizer.get_vocab())
						self.assertIsInstance(processor.tokenizer      ,  UpperCamelCase_)

						self.assertEqual(processor.feature_extractor.to_json_string()      ,  feature_extractor.to_json_string())
						self.assertIsInstance(processor.feature_extractor      ,  UpperCamelCase_)


			def       a_     (      self      :    Any):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					ClapProcessor(tokenizer=self.get_tokenizer()      ,  feature_extractor=self.get_feature_extractor())
						processor.save_pretrained(self.tmpdirname)

						__UpperCAmelCase	: int		=					self.get_tokenizer(bos_token="(BOS)"      ,  eos_token="(EOS)")
						__UpperCAmelCase	: Any		=					self.get_feature_extractor(do_normalize=UpperCamelCase_      ,  padding_value=1.0)

						__UpperCAmelCase	: Tuple		=					ClapProcessor.from_pretrained(
						    self.tmpdirname      ,  bos_token="(BOS)"      ,  eos_token="(EOS)"      ,  do_normalize=UpperCamelCase_      ,  padding_value=1.0)

						self.assertEqual(processor.tokenizer.get_vocab()      ,  tokenizer_add_kwargs.get_vocab())
						self.assertIsInstance(processor.tokenizer      ,  UpperCamelCase_)

						self.assertEqual(processor.feature_extractor.to_json_string()      ,  feature_extractor_add_kwargs.to_json_string())
						self.assertIsInstance(processor.feature_extractor      ,  UpperCamelCase_)


			def       a_     (      self      :    Any):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					self.get_feature_extractor()
						__UpperCAmelCase	: int		=					self.get_tokenizer()

						__UpperCAmelCase	: Any		=					ClapProcessor(tokenizer=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					floats_list((3, 1000))

						__UpperCAmelCase	: Tuple		=					feature_extractor(UpperCamelCase_      ,  return_tensors="np")
						__UpperCAmelCase	: Any		=					processor(audios=UpperCamelCase_      ,  return_tensors="np")

						for key in input_feat_extract.keys():
									self.assertAlmostEqual(input_feat_extract[key].sum()      ,  input_processor[key].sum()      ,  delta=1e-2)


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					self.get_feature_extractor()
						__UpperCAmelCase	: Optional[int]		=					self.get_tokenizer()

						__UpperCAmelCase	: str		=					ClapProcessor(tokenizer=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Any		=					"This is a test string"

						__UpperCAmelCase	: List[Any]		=					processor(text=UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					tokenizer(UpperCamelCase_)

						for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key]      ,  encoded_processor[key])


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					self.get_feature_extractor()
						__UpperCAmelCase	: List[str]		=					self.get_tokenizer()

						__UpperCAmelCase	: List[str]		=					ClapProcessor(tokenizer=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						__UpperCAmelCase	: int		=					processor.batch_decode(UpperCamelCase_)
						__UpperCAmelCase	: Tuple		=					tokenizer.batch_decode(UpperCamelCase_)

						self.assertListEqual(UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					self.get_feature_extractor()
						__UpperCAmelCase	: int		=					self.get_tokenizer()

						__UpperCAmelCase	: int		=					ClapProcessor(tokenizer=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						self.assertListEqual(
						    processor.model_input_names[2:]      ,  feature_extractor.model_input_names      ,  msg="`processor` and `feature_extractor` model input names do not match"      ,  )


