class_docstr: Pipeline for text-to-audio generation using AudioLDM2.
  
  This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
  implemented for all pipelines (downloading, saving, running on a particular device, etc.).
  
  Args:
      vae ([`AutoencoderKL`]):
          Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
      text_encoder ([`~transformers.ClapModel`]):
          First frozen text-encoder. AudioLDM2 uses the joint audio-text embedding model
          [CLAP](https://huggingface.co/docs/transformers/model_doc/clap#transformers.CLAPTextModelWithProjection),
          specifically the [laion/clap-htsat-unfused](https://huggingface.co/laion/clap-htsat-unfused) variant. The
          text branch is used to encode the text prompt to a prompt embedding. The full audio-text model is used to
          rank generated waveforms against the text prompt by computing similarity scores.
      text_encoder_2 ([`~transformers.T5EncoderModel`]):
          Second frozen text-encoder. AudioLDM2 uses the encoder of
          [T5](https://huggingface.co/docs/transformers/model_doc/t5#transformers.T5EncoderModel), specifically the
          [google/flan-t5-large](https://huggingface.co/google/flan-t5-large) variant.
      projection_model ([`AudioLDM2ProjectionModel`]):
          A trained model used to linearly project the hidden-states from the first and second text encoder models
          and insert learned SOS and EOS token embeddings. The projected hidden-states from the two text encoders are
          concatenated to give the input to the language model.
      language_model ([`~transformers.GPT2Model`]):
          An auto-regressive language model used to generate a sequence of hidden-states conditioned on the projected
          outputs from the two text encoders.
      tokenizer ([`~transformers.RobertaTokenizer`]):
          Tokenizer to tokenize text for the first frozen text-encoder.
      tokenizer_2 ([`~transformers.T5Tokenizer`]):
          Tokenizer to tokenize text for the second frozen text-encoder.
      feature_extractor ([`~transformers.ClapFeatureExtractor`]):
          Feature extractor to pre-process generated audio waveforms to log-mel spectrograms for automatic scoring.
      unet ([`UNet2DConditionModel`]):
          A `UNet2DConditionModel` to denoise the encoded audio latents.
      scheduler ([`SchedulerMixin`]):
          A scheduler to be used in combination with `unet` to denoise the encoded audio latents. Can be one of
          [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
      vocoder ([`~transformers.SpeechT5HifiGan`]):
          Vocoder of class `SpeechT5HifiGan` to convert the mel-spectrogram latents to the final audio waveform.
class_docstr_tok: ["Pipeline","for","text-to-audio","generation","using","AudioLDM2",".","This","model","inherits","from","[","`","DiffusionPipeline","`","]",".","Check","the","superclass","documentation","for","the","generic","methods","implemented","for","all","pipelines","(","downloading",",","saving",",","running","on","a","particular","device",",","etc.",")",".","Args",":","vae","(","[","`","AutoencoderKL","`","]",")",":","Variational","Auto-Encoder","(","VAE",")","model","to","encode","and","decode","images","to","and","from","latent","representations",".","text_encoder","(","[","`","~transformers.ClapModel","`","]",")",":","First","frozen","text-encoder",".","AudioLDM2","uses","the","joint","audio-text","embedding","model","[","CLAP","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/clap","#","transformers.CLAPTextModelWithProjection",")",",","specifically","the","[","laion\/clap-htsat-unfused","]","(","https",":","\/\/huggingface.co\/laion\/clap-htsat-unfused",")","variant",".","The","text","branch","is","used","to","encode","the","text","prompt","to","a","prompt","embedding",".","The","full","audio-text","model","is","used","to","rank","generated","waveforms","against","the","text","prompt","by","computing","similarity","scores",".","text_encoder_2","(","[","`","~transformers.T5EncoderModel","`","]",")",":","Second","frozen","text-encoder",".","AudioLDM2","uses","the","encoder","of","[","T5","]","(","https",":","\/\/huggingface.co\/docs\/transformers\/model_doc\/t5","#","transformers.T5EncoderModel",")",",","specifically","the","[","google\/flan-t5-large","]","(","https",":","\/\/huggingface.co\/google\/flan-t5-large",")","variant",".","projection_model","(","[","`","AudioLDM2ProjectionModel","`","]",")",":","A","trained","model","used","to","linearly","project","the","hidden-states","from","the","first","and","second","text","encoder","models","and","insert","learned","SOS","and","EOS","token","embeddings",".","The","projected","hidden-states","from","the","two","text","encoders","are","concatenated","to","give","the","input","to","the","language","model",".","language_model","(","[","`","~transformers.GPT2Model","`","]",")",":","An","auto-regressive","language","model","used","to","generate","a","sequence","of","hidden-states","conditioned","on","the","projected","outputs","from","the","two","text","encoders",".","tokenizer","(","[","`","~transformers.RobertaTokenizer","`","]",")",":","Tokenizer","to","tokenize","text","for","the","first","frozen","text-encoder",".","tokenizer_2","(","[","`","~transformers.T5Tokenizer","`","]",")",":","Tokenizer","to","tokenize","text","for","the","second","frozen","text-encoder",".","feature_extractor","(","[","`","~transformers.ClapFeatureExtractor","`","]",")",":","Feature","extractor","to","pre-process","generated","audio","waveforms","to","log-mel","spectrograms","for","automatic","scoring",".","unet","(","[","`","UNet2DConditionModel","`","]",")",":","A","`","UNet2DConditionModel","`","to","denoise","the","encoded","audio","latents",".","scheduler","(","[","`","SchedulerMixin","`","]",")",":","A","scheduler","to","be","used","in","combination","with","`","unet","`","to","denoise","the","encoded","audio","latents",".","Can","be","one","of","[","`","DDIMScheduler","`","]",",","[","`","LMSDiscreteScheduler","`","]",",","or","[","`","PNDMScheduler","`","]",".","vocoder","(","[","`","~transformers.SpeechT5HifiGan","`","]",")",":","Vocoder","of","class","`","SpeechT5HifiGan","`","to","convert","the","mel-spectrogram","latents","to","the","final","audio","waveform","."]
docstr: Encodes the prompt into text encoder hidden states.
  
  Args:
      prompt (`str` or `List[str]`, *optional*):
          prompt to be encoded
      device (`torch.device`):
          torch device
      num_waveforms_per_prompt (`int`):
          number of waveforms that should be generated per prompt
      do_classifier_free_guidance (`bool`):
          whether to use classifier free guidance or not
      negative_prompt (`str` or `List[str]`, *optional*):
          The prompt or prompts not to guide the audio generation. If not defined, one has to pass
          `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
          less than `1`).
      prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-computed text embeddings from the Flan T5 model. Can be used to easily tweak text inputs, *e.g.*
          prompt weighting. If not provided, text embeddings will be computed from `prompt` input argument.
      negative_prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-computed negative text embeddings from the Flan T5 model. Can be used to easily tweak text inputs,
          *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be computed from
          `negative_prompt` input argument.
      generated_prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-generated text embeddings from the GPT2 langauge model. Can be used to easily tweak text inputs,
           *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input
           argument.
      negative_generated_prompt_embeds (`torch.FloatTensor`, *optional*):
          Pre-generated negative text embeddings from the GPT2 language model. Can be used to easily tweak text
          inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be computed from
          `negative_prompt` input argument.
      attention_mask (`torch.LongTensor`, *optional*):
          Pre-computed attention mask to be applied to the `prompt_embeds`. If not provided, attention mask will
          be computed from `prompt` input argument.
      negative_attention_mask (`torch.LongTensor`, *optional*):
          Pre-computed attention mask to be applied to the `negative_prompt_embeds`. If not provided, attention
          mask will be computed from `negative_prompt` input argument.
      max_new_tokens (`int`, *optional*, defaults to None):
          The number of new tokens to generate with the GPT2 language model.
  Returns:
      prompt_embeds (`torch.FloatTensor`):
          Text embeddings from the Flan T5 model.
      attention_mask (`torch.LongTensor`):
          Attention mask to be applied to the `prompt_embeds`.
      generated_prompt_embeds (`torch.FloatTensor`):
          Text embeddings generated from the GPT2 langauge model.
  
  Example:
  
  ```python
  >>> import scipy
  >>> import torch
  >>> from diffusers import AudioLDM2Pipeline
  
  >>> repo_id = "cvssp/audioldm2"
  >>> pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
  >>> pipe = pipe.to("cuda")
  
  >>> # Get text embedding vectors
  >>> prompt_embeds, attention_mask, generated_prompt_embeds = pipe.encode_prompt(
  ...     prompt="Techno music with a strong, upbeat tempo and high melodic riffs",
  ...     device="cuda",
  ...     do_classifier_free_guidance=True,
  ... )
  
  >>> # Pass text embeddings to pipeline for text-conditional audio generation
  >>> audio = pipe(
  ...     prompt_embeds=prompt_embeds,
  ...     attention_mask=attention_mask,
  ...     generated_prompt_embeds=generated_prompt_embeds,
  ...     num_inference_steps=200,
  ...     audio_length_in_s=10.0,
  ... ).audios[0]
  
  >>> # save generated audio sample
  >>> scipy.io.wavfile.write("techno.wav", rate=16000, data=audio)
  ```
docstr_tok: ["Encodes","the","prompt","into","text","encoder","hidden","states",".","Args",":","prompt","(","`","str","`","or","`","List","[","str","]","`",",","*","optional","*",")",":","prompt","to","be","encoded","device","(","`","torch.device","`",")",":","torch","device","num_waveforms_per_prompt","(","`","int","`",")",":","number","of","waveforms","that","should","be","generated","per","prompt","do_classifier_free_guidance","(","`","bool","`",")",":","whether","to","use","classifier","free","guidance","or","not","negative_prompt","(","`","str","`","or","`","List","[","str","]","`",",","*","optional","*",")",":","The","prompt","or","prompts","not","to","guide","the","audio","generation",".","If","not","defined",",","one","has","to","pass","`","negative_prompt_embeds","`","instead",".","Ignored","when","not","using","guidance","(","i.e.",",","ignored","if","`","guidance_scale","`","is","less","than","`","1","`",")",".","prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-computed","text","embeddings","from","the","Flan","T5","model",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","text","embeddings","will","be","computed","from","`","prompt","`","input","argument",".","negative_prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-computed","negative","text","embeddings","from","the","Flan","T5","model",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","negative_prompt_embeds","will","be","computed","from","`","negative_prompt","`","input","argument",".","generated_prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-generated","text","embeddings","from","the","GPT2","langauge","model",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","text","embeddings","will","be","generated","from","`","prompt","`","input","argument",".","negative_generated_prompt_embeds","(","`","torch.FloatTensor","`",",","*","optional","*",")",":","Pre-generated","negative","text","embeddings","from","the","GPT2","language","model",".","Can","be","used","to","easily","tweak","text","inputs",",","*","e.g",".","*","prompt","weighting",".","If","not","provided",",","negative_prompt_embeds","will","be","computed","from","`","negative_prompt","`","input","argument",".","attention_mask","(","`","torch.LongTensor","`",",","*","optional","*",")",":","Pre-computed","attention","mask","to","be","applied","to","the","`","prompt_embeds","`",".","If","not","provided",",","attention","mask","will","be","computed","from","`","prompt","`","input","argument",".","negative_attention_mask","(","`","torch.LongTensor","`",",","*","optional","*",")",":","Pre-computed","attention","mask","to","be","applied","to","the","`","negative_prompt_embeds","`",".","If","not","provided",",","attention","mask","will","be","computed","from","`","negative_prompt","`","input","argument",".","max_new_tokens","(","`","int","`",",","*","optional","*",",","defaults","to","None",")",":","The","number","of","new","tokens","to","generate","with","the","GPT2","language","model",".","Returns",":","prompt_embeds","(","`","torch.FloatTensor","`",")",":","Text","embeddings","from","the","Flan","T5","model",".","attention_mask","(","`","torch.LongTensor","`",")",":","Attention","mask","to","be","applied","to","the","`","prompt_embeds","`",".","generated_prompt_embeds","(","`","torch.FloatTensor","`",")",":","Text","embeddings","generated","from","the","GPT2","langauge","model",".","Example",":","``","`","python",">",">",">","import","scipy",">",">",">","import","torch",">",">",">","from","diffusers","import","AudioLDM2Pipeline",">",">",">","repo_id","=","``","cvssp\/audioldm2","''",">",">",">","pipe","=","AudioLDM2Pipeline.from_pretrained","(","repo_id",",","torch_dtype=torch.float16",")",">",">",">","pipe","=","pipe.to","(","``","cuda","''",")",">",">",">","#","Get","text","embedding","vectors",">",">",">","prompt_embeds",",","attention_mask",",","generated_prompt_embeds","=","pipe.encode_prompt","(","...","prompt=","''","Techno","music","with","a","strong",",","upbeat","tempo","and","high","melodic","riffs","''",",","...","device=","''","cuda","''",",","...","do_classifier_free_guidance=True",",","...",")",">",">",">","#","Pass","text","embeddings","to","pipeline","for","text-conditional","audio","generation",">",">",">","audio","=","pipe","(","...","prompt_embeds=prompt_embeds",",","...","attention_mask=attention_mask",",","...","generated_prompt_embeds=generated_prompt_embeds",",","...","num_inference_steps=200",",","...","audio_length_in_s=10.0",",","...",")",".audios","[","0","]",">",">",">","#","save","generated","audio","sample",">",">",">","scipy.io.wavfile.write","(","``","techno.wav","''",",","rate=16000",",","data=audio",")","``","`"]
code: def encode_prompt(
      self,
      prompt,
      device,
      num_waveforms_per_prompt,
      do_classifier_free_guidance,
      negative_prompt=None,
      prompt_embeds: Optional[torch.FloatTensor] = None,
      negative_prompt_embeds: Optional[torch.FloatTensor] = None,
      generated_prompt_embeds: Optional[torch.FloatTensor] = None,
      negative_generated_prompt_embeds: Optional[
          torch.FloatTensor
      ] = None,
      attention_mask: Optional[torch.LongTensor] = None,
      negative_attention_mask: Optional[torch.LongTensor] = None,
      max_new_tokens: Optional[int] = None,
  ):
      r"""
      Encodes the prompt into text encoder hidden states.
  
      Args:
          prompt (`str` or `List[str]`, *optional*):
              prompt to be encoded
          device (`torch.device`):
              torch device
          num_waveforms_per_prompt (`int`):
              number of waveforms that should be generated per prompt
          do_classifier_free_guidance (`bool`):
              whether to use classifier free guidance or not
          negative_prompt (`str` or `List[str]`, *optional*):
              The prompt or prompts not to guide the audio generation. If not defined, one has to pass
              `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
              less than `1`).
          prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-computed text embeddings from the Flan T5 model. Can be used to easily tweak text inputs, *e.g.*
              prompt weighting. If not provided, text embeddings will be computed from `prompt` input argument.
          negative_prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-computed negative text embeddings from the Flan T5 model. Can be used to easily tweak text inputs,
              *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be computed from
              `negative_prompt` input argument.
          generated_prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-generated text embeddings from the GPT2 langauge model. Can be used to easily tweak text inputs,
               *e.g.* prompt weighting. If not provided, text embeddings will be generated from `prompt` input
               argument.
          negative_generated_prompt_embeds (`torch.FloatTensor`, *optional*):
              Pre-generated negative text embeddings from the GPT2 language model. Can be used to easily tweak text
              inputs, *e.g.* prompt weighting. If not provided, negative_prompt_embeds will be computed from
              `negative_prompt` input argument.
          attention_mask (`torch.LongTensor`, *optional*):
              Pre-computed attention mask to be applied to the `prompt_embeds`. If not provided, attention mask will
              be computed from `prompt` input argument.
          negative_attention_mask (`torch.LongTensor`, *optional*):
              Pre-computed attention mask to be applied to the `negative_prompt_embeds`. If not provided, attention
              mask will be computed from `negative_prompt` input argument.
          max_new_tokens (`int`, *optional*, defaults to None):
              The number of new tokens to generate with the GPT2 language model.
      Returns:
          prompt_embeds (`torch.FloatTensor`):
              Text embeddings from the Flan T5 model.
          attention_mask (`torch.LongTensor`):
              Attention mask to be applied to the `prompt_embeds`.
          generated_prompt_embeds (`torch.FloatTensor`):
              Text embeddings generated from the GPT2 langauge model.
  
      Example:
  
      ```python
      >>> import scipy
      >>> import torch
      >>> from diffusers import AudioLDM2Pipeline
  
      >>> repo_id = "cvssp/audioldm2"
      >>> pipe = AudioLDM2Pipeline.from_pretrained(repo_id, torch_dtype=torch.float16)
      >>> pipe = pipe.to("cuda")
  
      >>> # Get text embedding vectors
      >>> prompt_embeds, attention_mask, generated_prompt_embeds = pipe.encode_prompt(
      ...     prompt="Techno music with a strong, upbeat tempo and high melodic riffs",
      ...     device="cuda",
      ...     do_classifier_free_guidance=True,
      ... )
  
      >>> # Pass text embeddings to pipeline for text-conditional audio generation
      >>> audio = pipe(
      ...     prompt_embeds=prompt_embeds,
      ...     attention_mask=attention_mask,
      ...     generated_prompt_embeds=generated_prompt_embeds,
      ...     num_inference_steps=200,
      ...     audio_length_in_s=10.0,
      ... ).audios[0]
  
      >>> # save generated audio sample
      >>> scipy.io.wavfile.write("techno.wav", rate=16000, data=audio)
      ```"""
      if prompt is not None and isinstance(prompt, str):
          batch_size = 1
      elif prompt is not None and isinstance(prompt, list):
          batch_size = len(prompt)
      else:
          batch_size = prompt_embeds.shape[0]
  
      # Define tokenizers and text encoders
      tokenizers = [self.tokenizer, self.tokenizer_2]
      text_encoders = [self.text_encoder, self.text_encoder_2]
  
      if prompt_embeds is None:
          prompt_embeds_list = []
          attention_mask_list = []
  
          for tokenizer, text_encoder in zip(tokenizers, text_encoders):
              text_inputs = tokenizer(
                  prompt,
                  padding="max_length"
                  if isinstance(
                      tokenizer,
                      (RobertaTokenizer, RobertaTokenizerFast),
                  )
                  else True,
                  max_length=tokenizer.model_max_length,
                  truncation=True,
                  return_tensors="pt",
              )
              text_input_ids = text_inputs.input_ids
              attention_mask = text_inputs.attention_mask
              untruncated_ids = tokenizer(
                  prompt, padding="longest", return_tensors="pt"
              ).input_ids
  
              if untruncated_ids.shape[-1] >= text_input_ids.shape[
                  -1
              ] and not torch.equal(text_input_ids, untruncated_ids):
                  removed_text = tokenizer.batch_decode(
                      untruncated_ids[
                          :, tokenizer.model_max_length - 1 : -1
                      ]
                  )
                  logger.warning(
                      f"The following part of your input was truncated because {text_encoder.config.model_type} can "
                      f"only handle sequences up to {tokenizer.model_max_length} tokens: {removed_text}"
                  )
  
              text_input_ids = text_input_ids.to(device)
              attention_mask = attention_mask.to(device)
  
              if text_encoder.config.model_type == "clap":
                  prompt_embeds = text_encoder.get_text_features(
                      text_input_ids,
                      attention_mask=attention_mask,
                  )
                  # append the seq-len dim: (bs, hidden_size) -> (bs, seq_len, hidden_size)
                  prompt_embeds = prompt_embeds[:, None, :]
                  # make sure that we attend to this single hidden-state
                  attention_mask = attention_mask.new_ones(
                      (batch_size, 1)
                  )
              else:
                  prompt_embeds = text_encoder(
                      text_input_ids,
                      attention_mask=attention_mask,
                  )
                  prompt_embeds = prompt_embeds[0]
  
              prompt_embeds_list.append(prompt_embeds)
              attention_mask_list.append(attention_mask)
  
          projection_output = self.projection_model(
              hidden_states=prompt_embeds_list[0],
              hidden_states_1=prompt_embeds_list[1],
              attention_mask=attention_mask_list[0],
              attention_mask_1=attention_mask_list[1],
          )
          projected_prompt_embeds = projection_output.hidden_states
          projected_attention_mask = projection_output.attention_mask
  
          generated_prompt_embeds = self.generate_language_model(
              projected_prompt_embeds,
              attention_mask=projected_attention_mask,
              max_new_tokens=max_new_tokens,
          )
  
      prompt_embeds = prompt_embeds.to(
          dtype=self.text_encoder_2.dtype, device=device
      )
      attention_mask = (
          attention_mask.to(device=device)
          if attention_mask is not None
          else torch.ones(
              prompt_embeds.shape[:2], dtype=torch.long, device=device
          )
      )
      generated_prompt_embeds = generated_prompt_embeds.to(
          dtype=self.language_model.dtype, device=device
      )
  
      bs_embed, seq_len, hidden_size = prompt_embeds.shape
      # duplicate text embeddings for each generation per prompt, using mps friendly method
      prompt_embeds = prompt_embeds.repeat(
          1, num_waveforms_per_prompt, 1
      )
      prompt_embeds = prompt_embeds.view(
          bs_embed * num_waveforms_per_prompt, seq_len, hidden_size
      )
  
      # duplicate attention mask for each generation per prompt
      attention_mask = attention_mask.repeat(
          1, num_waveforms_per_prompt
      )
      attention_mask = attention_mask.view(
          bs_embed * num_waveforms_per_prompt, seq_len
      )
  
      bs_embed, seq_len, hidden_size = generated_prompt_embeds.shape
      # duplicate generated embeddings for each generation per prompt, using mps friendly method
      generated_prompt_embeds = generated_prompt_embeds.repeat(
          1, num_waveforms_per_prompt, 1
      )
      generated_prompt_embeds = generated_prompt_embeds.view(
          bs_embed * num_waveforms_per_prompt, seq_len, hidden_size
      )
  
      # get unconditional embeddings for classifier free guidance
      if do_classifier_free_guidance and negative_prompt_embeds is None:
          uncond_tokens: List[str]
          if negative_prompt is None:
              uncond_tokens = [""] * batch_size
          elif type(prompt) is not type(negative_prompt):
              raise TypeError(
                  f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
                  f" {type(prompt)}."
              )
          elif isinstance(negative_prompt, str):
              uncond_tokens = [negative_prompt]
          elif batch_size != len(negative_prompt):
              raise ValueError(
                  f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
                  f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
                  " the batch size of `prompt`."
              )
          else:
              uncond_tokens = negative_prompt
  
          negative_prompt_embeds_list = []
          negative_attention_mask_list = []
          max_length = prompt_embeds.shape[1]
          for tokenizer, text_encoder in zip(tokenizers, text_encoders):
              uncond_input = tokenizer(
                  uncond_tokens,
                  padding="max_length",
                  max_length=tokenizer.model_max_length
                  if isinstance(
                      tokenizer,
                      (RobertaTokenizer, RobertaTokenizerFast),
                  )
                  else max_length,
                  truncation=True,
                  return_tensors="pt",
              )
  
              uncond_input_ids = uncond_input.input_ids.to(device)
              negative_attention_mask = uncond_input.attention_mask.to(
                  device
              )
  
              if text_encoder.config.model_type == "clap":
                  negative_prompt_embeds = (
                      text_encoder.get_text_features(
                          uncond_input_ids,
                          attention_mask=negative_attention_mask,
                      )
                  )
                  # append the seq-len dim: (bs, hidden_size) -> (bs, seq_len, hidden_size)
                  negative_prompt_embeds = negative_prompt_embeds[
                      :, None, :
                  ]
                  # make sure that we attend to this single hidden-state
                  negative_attention_mask = (
                      negative_attention_mask.new_ones((batch_size, 1))
                  )
              else:
                  negative_prompt_embeds = text_encoder(
                      uncond_input_ids,
                      attention_mask=negative_attention_mask,
                  )
                  negative_prompt_embeds = negative_prompt_embeds[0]
  
              negative_prompt_embeds_list.append(negative_prompt_embeds)
              negative_attention_mask_list.append(
                  negative_attention_mask
              )
  
          projection_output = self.projection_model(
              hidden_states=negative_prompt_embeds_list[0],
              hidden_states_1=negative_prompt_embeds_list[1],
              attention_mask=negative_attention_mask_list[0],
              attention_mask_1=negative_attention_mask_list[1],
          )
          negative_projected_prompt_embeds = (
              projection_output.hidden_states
          )
          negative_projected_attention_mask = (
              projection_output.attention_mask
          )
  
          negative_generated_prompt_embeds = (
              self.generate_language_model(
                  negative_projected_prompt_embeds,
                  attention_mask=negative_projected_attention_mask,
                  max_new_tokens=max_new_tokens,
              )
          )
  
      if do_classifier_free_guidance:
          seq_len = negative_prompt_embeds.shape[1]
  
          negative_prompt_embeds = negative_prompt_embeds.to(
              dtype=self.text_encoder_2.dtype, device=device
          )
          negative_attention_mask = (
              negative_attention_mask.to(device=device)
              if negative_attention_mask is not None
              else torch.ones(
                  negative_prompt_embeds.shape[:2],
                  dtype=torch.long,
                  device=device,
              )
          )
          negative_generated_prompt_embeds = (
              negative_generated_prompt_embeds.to(
                  dtype=self.language_model.dtype, device=device
              )
          )
  
          # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
          negative_prompt_embeds = negative_prompt_embeds.repeat(
              1, num_waveforms_per_prompt, 1
          )
          negative_prompt_embeds = negative_prompt_embeds.view(
              batch_size * num_waveforms_per_prompt, seq_len, -1
          )
  
          # duplicate unconditional attention mask for each generation per prompt
          negative_attention_mask = negative_attention_mask.repeat(
              1, num_waveforms_per_prompt
          )
          negative_attention_mask = negative_attention_mask.view(
              batch_size * num_waveforms_per_prompt, seq_len
          )
  
          # duplicate unconditional generated embeddings for each generation per prompt
          seq_len = negative_generated_prompt_embeds.shape[1]
          negative_generated_prompt_embeds = (
              negative_generated_prompt_embeds.repeat(
                  1, num_waveforms_per_prompt, 1
              )
          )
          negative_generated_prompt_embeds = (
              negative_generated_prompt_embeds.view(
                  batch_size * num_waveforms_per_prompt, seq_len, -1
              )
          )
  
          # For classifier free guidance, we need to do two forward passes.
          # Here we concatenate the unconditional and text embeddings into a single batch
          # to avoid doing two forward passes
          prompt_embeds = torch.cat(
              [negative_prompt_embeds, prompt_embeds]
          )
          attention_mask = torch.cat(
              [negative_attention_mask, attention_mask]
          )
          generated_prompt_embeds = torch.cat(
              [
                  negative_generated_prompt_embeds,
                  generated_prompt_embeds,
              ]
          )
  
      return prompt_embeds, attention_mask, generated_prompt_embeds
  
imports: import inspect
  from typing import Any, Callable, Dict, List, Optional, Union
  import numpy
  import torch
  from transformers import ClapFeatureExtractor, ClapModel, GPT2Model, RobertaTokenizer, RobertaTokenizerFast, SpeechT5HifiGan, T5EncoderModel, T5Tokenizer, T5TokenizerFast
  from ...models import AutoencoderKL
  from ...schedulers import KarrasDiffusionSchedulers
  from ...utils import is_accelerate_available, is_accelerate_version, is_librosa_available, logging, replace_example_docstring
  from ...utils.torch_utils import randn_tensor
  from ..pipeline_utils import AudioPipelineOutput, DiffusionPipeline
  from .modeling_audioldm2 import AudioLDM2ProjectionModel, AudioLDM2UNet2DConditionModel
label_desc: Use image node_id 6 for calling the AudioLDM2Pipeline obj's underlying member method code with example usage: obj.encode_prompt(prompt, device, num_waveforms_per_prompt, do_classifier_free_guidance, negative_prompt, prompt_embeds, negative_prompt_embeds, generated_prompt_embeds, negative_generated_prompt_embeds, attention_mask, negative_attention_mask, max_new_tokens) and returns: prompt_embeds,  attention_mask,  generated_prompt_embeds