if use_icl:
if prompt_start_time > prompt_end_time:
raise gr.Error(f"'Start time' should be less than 'End Time'")
if (prompt_end_time - prompt_start_time) > 30 :
raise gr.Error(f"The duration for the audio prompt should not exceed 30s")
if vocal_track_prompt == None:
raise gr.Error(f"You must provide at least a Vocal audio prompt")
args.prompt_start_time = prompt_start_time
args.prompt_end_time = prompt_end_time
if instrumental_track_prompt == None:
args.use_audio_prompt = True
args.audio_prompt_path = vocal_track_prompt
else:
args.use_dual_tracks_prompt = True
args.vocal_track_prompt_path = vocal_track_prompt
args.instrumental_track_prompt_path = instrumental_track_prompt
# return "output/cot_inspiring-female-uplifting-pop-airy-vocal-electronic-bright-vocal-vocal_tp0@93_T1@0_rp1@2_maxtk3000_mixed_e0a99c45-7f63-41c9-826f-9bde7417db4c.mp3"
stage1_output_set = []
# Tips:
# genre tags support instrumental,genre,mood,vocal timbr and vocal gender
# all kinds of tags are needed
genres = genres_input.strip()
lyrics = split_lyrics(lyrics_input)
# intruction
full_lyrics = "\n".join(lyrics)
prompt_texts = [f"Generate music from the given lyrics segment by segment.\n[Genre] {genres}\n{full_lyrics}"]
prompt_texts += lyrics
import random
if seed <= 0:
seed = random.randint(0, 999999999)
torch.cuda.manual_seed(seed)
random.seed(seed)
random_id = uuid.uuid4()
output_seq = None
# Here is suggested decoding config
top_p = 0.93
temperature = 1.0
repetition_penalty = 1.2
# special tokens
start_of_segment = mmtokenizer.tokenize('[start_of_segment]')
end_of_segment = mmtokenizer.tokenize('[end_of_segment]')
# Format text prompt
run_n_segments = min(run_n_segments, len(lyrics)) +1
for i, p in enumerate(tqdm(prompt_texts[:run_n_segments])):
section_text = p.replace('[start_of_segment]', '').replace('[end_of_segment]', '')
guidance_scale = 1.5 if i <=1 else 1.2
if i==0:
continue
if i==1:
if args.use_dual_tracks_prompt or args.use_audio_prompt:
if args.use_dual_tracks_prompt:
vocals_ids = load_audio_mono(args.vocal_track_prompt_path)
instrumental_ids = load_audio_mono(args.instrumental_track_prompt_path)
vocals_ids = encode_audio(codec_model, vocals_ids, device, target_bw=0.5)
instrumental_ids = encode_audio(codec_model, instrumental_ids, device, target_bw=0.5)
vocals_ids = codectool.npy2ids(vocals_ids[0])
instrumental_ids = codectool.npy2ids(instrumental_ids[0])
min_size = min(len(vocals_ids), len(instrumental_ids) )
vocals_ids = vocals_ids[0 : min_size]
instrumental_ids = instrumental_ids[0 : min_size]
ids_segment_interleaved = rearrange([np.array(vocals_ids), np.array(instrumental_ids)], 'b n -> (n b)')
audio_prompt_codec = ids_segment_interleaved[int(args.prompt_start_time*50*2): int(args.prompt_end_time*50*2)]
audio_prompt_codec = audio_prompt_codec.tolist()
elif args.use_audio_prompt:
audio_prompt = load_audio_mono(args.audio_prompt_path)
raw_codes = encode_audio(codec_model, audio_prompt, device, target_bw=0.5)
# Format audio prompt
code_ids = codectool.npy2ids(raw_codes[0])
audio_prompt_codec = code_ids[int(args.prompt_start_time *50): int(args.prompt_end_time *50)] # 50 is tps of xcodec
audio_prompt_codec_ids = [mmtokenizer.soa] + codectool.sep_ids + audio_prompt_codec + [mmtokenizer.eoa]
sentence_ids = mmtokenizer.tokenize("[start_of_reference]") + audio_prompt_codec_ids + mmtokenizer.tokenize("[end_of_reference]")
head_id = mmtokenizer.tokenize(prompt_texts[0]) + sentence_ids
else:
head_id = mmtokenizer.tokenize(prompt_texts[0])
prompt_ids = head_id + start_of_segment + mmtokenizer.tokenize(section_text) + [mmtokenizer.soa] + codectool.sep_ids
else:
prompt_ids = end_of_segment + start_of_segment + mmtokenizer.tokenize(section_text) + [mmtokenizer.soa] + codectool.sep_ids
prompt_ids = torch.as_tensor(prompt_ids).unsqueeze(0).to(device)
input_ids = torch.cat([raw_output, prompt_ids], dim=1) if i > 1 else prompt_ids
# Use window slicing in case output sequence exceeds the context of model
max_context = 16384-max_new_tokens-1
if input_ids.shape[-1] > max_context:
print(f'Section {i}: output length {input_ids.shape[-1]} exceeding context length {max_context}, now using the last {max_context} tokens.')
input_ids = input_ids[:, -(max_context):]
with torch.no_grad():
output_seq = model.generate(
input_ids=input_ids,
max_new_tokens=max_new_tokens,
min_new_tokens=100,
do_sample=True,
top_p=top_p,
temperature=temperature,
repetition_penalty=repetition_penalty,
eos_token_id=mmtokenizer.eoa,
pad_token_id=mmtokenizer.eoa,
logits_processor=LogitsProcessorList([BlockTokenRangeProcessor(0, 32002), BlockTokenRangeProcessor(32016, 32016)]),
guidance_scale=guidance_scale,
)
if output_seq[0][-1].item() != mmtokenizer.eoa:
tensor_eoa = torch.as_tensor([[mmtokenizer.eoa]]).to(model.device)
output_seq = torch.cat((output_seq, tensor_eoa), dim=1)
if i > 1:
raw_output = torch.cat([raw_output, prompt_ids, output_seq[:, input_ids.shape[-1]:]], dim=1)
else:
raw_output = output_seq
# save raw output and check sanity
ids = raw_output[0].cpu().numpy()
soa_idx = np.where(ids == mmtokenizer.soa)[0].tolist()
eoa_idx = np.where(ids == mmtokenizer.eoa)[0].tolist()
if len(soa_idx)!=len(eoa_idx):
raise ValueError(f'invalid pairs of soa and eoa, Num of soa: {len(soa_idx)}, Num of eoa: {len(eoa_idx)}')
vocals = []
instrumentals = []
range_begin = 1 if args.use_audio_prompt or args.use_dual_tracks_prompt else 0
for i in range(range_begin, len(soa_idx)):
codec_ids = ids[soa_idx[i]+1:eoa_idx[i]]
if codec_ids[0] == 32016:
codec_ids = codec_ids[1:]
codec_ids = codec_ids[:2 * (codec_ids.shape[0] // 2)]
vocals_ids = codectool.ids2npy(rearrange(codec_ids,"(n b) -> b n", b=2)[0])
vocals.append(vocals_ids)
instrumentals_ids = codectool.ids2npy(rearrange(codec_ids,"(n b) -> b n", b=2)[1])
instrumentals.append(instrumentals_ids)
vocals = np.concatenate(vocals, axis=1)
instrumentals = np.concatenate(instrumentals, axis=1)
vocal_save_path = os.path.join(stage1_output_dir, f"{genres.replace(' ', '-')}_tp{top_p}_T{temperature}_rp{repetition_penalty}_maxtk{max_new_tokens}_{random_id}_vtrack".replace('.', '@')+'.npy')
inst_save_path = os.path.join(stage1_output_dir, f"{genres.replace(' ', '-')}_tp{top_p}_T{temperature}_rp{repetition_penalty}_maxtk{max_new_tokens}_{random_id}_itrack".replace('.', '@')+'.npy')
np.save(vocal_save_path, vocals)
np.save(inst_save_path, instrumentals)
stage1_output_set.append(vocal_save_path)
stage1_output_set.append(inst_save_path)
The code you've provided appears to be a function for generating music based on given genres and lyrics, with support for various audio prompt options. It employs a deep learning model, potentially related to music generation, and utilizes libraries such as PyTorch for tensor operations.
However, you've mentioned that the "raw output gives errors." Without specific error messages or contexts, it's challenging to diagnose the exact issues. Nevertheless, I can offer some general troubleshooting suggestions based on common pitfalls in code that involves audio generation and model inference.
Common Errors and Troubleshooting Steps
-
Undefined Variables: Ensure that all variables used in the function (e.g., args, use_icl, mmtokenizer, model, codectool, device, codec_model, stage1_output_dir, etc.) are defined before they are accessed in the function.
-
Import Statements: Check if all necessary modules and classes (such as torch, numpy, uuid, tqdm, gr, and any other specific utility functions or classes) are imported at the beginning of your script or notebook.
-
Dependency Check: Make sure that all required third-party libraries are installed. For example, if using PyTorch with GPU support, ensure that the correct version is installed.
-
Error Handling: Currently, certain error checks raise gr.Error, which is probably specific to a library like Gradio. Ensure that the context is properly established for using gr.Error, or replace it with standard exceptions like ValueError to provide clear error messages.
-
Tensor Operations: Ensure tensors are compatible when performing operations like concatenation. Pay attention to their shapes (dimensionalities) when you concatenate with torch.cat. Mismatched dimensions will lead to runtime errors.
-
Array Indexing: Check that all array and tensor indices are within valid ranges. This includes ensuring that you do not try to access an index that is out of bounds, especially after operations that might change the shape or size of an array.
-
Audio Loading and Processing: Errors might originate from loading audio files or processing them. Verify that audio file paths are correct and that the files exist.
-
Model Inference Logic: There may be logical errors in how you're running the model's generate function. Check that the output is handled correctly and that you're aware of shapes after calling the generation function.
-
Output Verification: After generating the outputs, carefully check the contents of raw_output and ensure that the expected output format matches the processing logic later in the code.
-
Numpy Save Issues: Ensure that the paths used in np.save are valid, and that the directory specified (i.e., stage1_output_dir) exists and is writable.
Debugging Steps
-
Add Logging: Include print statements or logs throughout your code to capture the flow and the values of key variables. This can help pinpoint where an error might occur.
-
Check Stack Trace: If you're encountering an error and have the stack trace, analyze which line is failing and why. This will provide insight into what needs to be fixed.
-
Unit Testing: If feasible, break down your function into smaller parts and test them individually to isolate problems.
-
Simulate with Dummy Data: When debugging sections of the code, you can replace input data with smaller or simpler data to see if the function performs as expected.
-
Exception Handling: Wrap code segments in try-except blocks to catch exceptions and print useful information for debugging.
If you have specific error messages or a context in which the errors occur, please share them for more tailored advice!