Spaces:
Running on Zero
Running on Zero
update examles
Browse filesFiles changed (4)
12345678910111213141516171819202122232425{ "prompt": "anime, cute female vocals, kawaii pop, j-pop, childish, piano, guitar, synthesizer, fast, happy, cheerful, lighthearted", "lyrics": "[Chorus]\nねぇ、顔が赤いよ?\nどうしたの? 熱があるの?\nそれとも怒ってるの?\nねぇ、言ってよ!\n\nどうしてそんな目で見るの?\n私、悪いことした?\n何か間違えたの?\nお願い、やめて… 怖いから…\nだから、やめてよ…\n\n[Bridge]\n目を閉じて、くるっと背を向けて、\n何も見なかったフリするから、\n怒らないで… 許してよ…\n\n[Chorus]\nねぇ、顔が赤いよ?\nどうしたの? 熱があるの?\nそれとも怒ってるの?\nねぇ、言ってよ!\n\nどうしてそんな目で見るの?\n私、悪いことした?\n何か間違えたの?\nお願い、やめて… 怖いから…\nだから、やめてよ…\n\n[Bridge 2]\n待って、もし私が悪いなら、\nごめんなさいって言うから、\nアイスクリームあげるから、\nもう怒らないで?\n\nOoooh… 言ってよ!", "audio_duration": 160, "infer_step": 60, "guidance_scale": 15, "scheduler_type": "euler", "cfg_type": "apg", "omega_scale": 10, "guidance_interval": 0.5, "guidance_interval_decay": 0, "min_guidance_scale": 3, "use_erg_tag": true, "use_erg_lyric": true, "use_erg_diffusion": true, "oss_steps": [], "timecosts": { "preprocess": 0.0282442569732666, "diffusion": 12.104875326156616, "latent2audio": 1.587641954421997 }, "actual_seeds": [ 4028738662 ]}21 unmodified lines2223242526272821 unmodified lines"latent2audio": 0.5694489479064941},"actual_seeds": [721655639],"retake_seeds": [160320161721 unmodified lines2223242526272821 unmodified lines"latent2audio": 0.5694489479064941},"actual_seeds": [226581098],"retake_seeds": [1603201617
123456789101112131415161718192021222324252627282930313233343536373839404142434445{ "lora_name_or_path": "/root/sag_train/data/ace_step_v1_chinese_rap_lora_80k", "task": "text2music", "prompt": "articulate, spoken word, young adult, rap music, male, clear, energetic, warm, relaxed, breathy, night club", "lyrics": "[verse]\n这 这 谁 又 在 派 对 喝 多\n我 的 脑 袋\n像 被 驴 踢 过\n不 对 劲\n舌 头 打 结 不 会 说\n你 来 挑 战 我 就 跪\n开 局 直 接 崩 溃\n\n[chorus]\n就 咪 乱 咪 念 咪 错 咪\n嘴 咪 瓢 咪 成 咪 狗 咪\n脑 咪 袋 咪 像 咪 浆 咪 糊 咪\n跟 咪 着 咪 节 咪 奏 咪\n把 咪 歌 咪 词 咪 全 咪 忘 咪\n一 咪 张 咪 嘴 咪 就 咪 废 咪\n只 咪 剩 咪 下 咪 尴 咪 尬 咪 回 咪 忆\n草!\n\n[verse]\n错 错 错 错 了\n一 口 气 全 念 错\n错 错 错 错 了\n舌 头 打 结 甩 锅\n甩 甩 甩 甩 锅\n甩 锅 甩 锅\n拍 子 全 部 乱 套\n观 众 笑 到 吐 血\n\n[verse]\n你 的 歌 词 我 的 噩 梦\n唱 完 直 接 社 死\n调 跑 到 外 太 空\n观 众 表 情 裂 开\n你 笑 我 菜\n我 笑 你 不 懂\n这 叫 艺 术 表 演\n不 服 你 来!\n\n[verse]\n这 这 谁 又 在 派 对 丢 人\n我 的 世 界\n已 经 彻 底 崩 溃\n没 有 完 美\n只 有 翻 车 现 场\n以 及 观 众 的 嘲 讽\n\n[chorus]\n就 咪 乱 咪 念 咪 错 咪\n嘴 咪 瓢 咪 成 咪 狗 咪\n脑 咪 袋 咪 像 咪 浆 咪 糊 咪\n跟 咪 着 咪 节 咪 奏 咪\n把 咪 歌 咪 词 咪 全 咪 忘 咪\n一 咪 张 咪 嘴 咪 就 咪 废 咪\n只 咪 剩 咪 下 咪 尴 咪 尬 咪 回 咪 忆\n草!\n\n[verse]\n错 错 错 错 了\n一 口 气 全 念 错\n错 错 错 错 了\n舌 头 打 结 甩 锅\n甩 甩 甩 甩 锅\n甩 锅 甩 锅\n拍 子 全 部 乱 套\n观 众 笑 到 吐 血\n\n[verse]\n你 的 歌 词 我 的 噩 梦\n唱 完 直 接 社 死\n调 跑 到 外 太 空\n观 众 表 情 裂 开\n你 笑 我 菜\n我 笑 你 不 懂\n这 叫 艺 术 表 演\n不 服 你 来!", "audio_duration": 169.12, "infer_step": 60, "guidance_scale": 15, "scheduler_type": "euler", "cfg_type": "apg", "omega_scale": 10, "guidance_interval": 0.5, "guidance_interval_decay": 0, "min_guidance_scale": 3, "use_erg_tag": true, "use_erg_lyric": false, "use_erg_diffusion": true, "oss_steps": [], "timecosts": { "preprocess": 0.041605472564697266, "diffusion": 14.009192705154419, "latent2audio": 1.55946946144104 }, "actual_seeds": [ 547563805 ], "retake_seeds": [ 2702917060 ], "retake_variance": 0.5, "guidance_scale_text": 0, "guidance_scale_lyric": 0, "repaint_start": 0, "repaint_end": 0, "edit_n_min": 0.0, "edit_n_max": 1.0, "edit_n_avg": 1, "src_audio_path": null, "edit_target_prompt": null, "edit_target_lyrics": null, "audio2audio_enable": false, "ref_audio_strength": 0.5, "ref_audio_input": null, "audio_path": "./outputs/output_20250512160830_0.wav"}11 unmodified lines1213141516171819201 unmodified line222324252627282930313233343536373839404142434411 unmodified lines565758596061621 unmodified line646566676869703 unmodified lines74757677787980818283848586878889908 unmodified lines991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521532 unmodified lines1561571581591601611621631641651661671681691701711721731741751761771781791801811821831843 unmodified lines1881891901911921931942 unmodified lines19719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725827 unmodified lines28628728828929029129222 unmodified lines3153163173183193203213223233243258 unmodified lines3343353363373383393408 unmodified lines34935035135235335435563 unmodified lines41942042142242342442542642742842943043143243343443543643743843944044144244344410 unmodified lines4554564574584594604614624634641 unmodified line46646746846947047147217 unmodified lines49049149249349449549649749849950050150250350450550650750850951051115 unmodified lines52752852953053153253353453553653753853911 unmodified lines55155255355455555655737 unmodified lines5955965975985996006016026036046056066076086099 unmodified lines6196206216226236246254 unmodified lines6306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596601 unmodified line6626636646656666676686696706716726736749 unmodified lines68468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575610 unmodified lines7677687697707717727737747757767777784 unmodified lines7837847857867877887896 unmodified lines7967977987998008018025 unmodified lines80880981081181281381481581681781881982082182282382482582682782882983083183283316 unmodified lines85085185285385485585685711 unmodified lines86987087187287387487525 unmodified lines90190290390490590690722 unmodified lines9309319329339349359368 unmodified lines9459469479489499509519529532 unmodified lines9569579589599609619629639649659669679689699709719729732 unmodified lines9769779789799809819829839849859862 unmodified lines98999099199299399499599699719 unmodified lines101710181019102010211022102336 unmodified lines10601061106210631064106510661067106810691070107110721073107410751076107710781079108010814 unmodified lines108610871088108910901091109210938 unmodified lines11021103110411051106110711081109111011111112111311141115111611171 unmodified line111911201121112211231124112511261127112811291130113111321133113411359 unmodified lines114511461147114811491150115181 unmodified lines12331234123512361237123812393 unmodified lines124312441245124612471248124911 unmodified linesfrom huggingface_hub import hf_hub_download, snapshot_download# from diffusers.pipelines.pipeline_utils import DiffusionPipelinefrom schedulers.scheduling_flow_match_euler_discrete import FlowMatchEulerDiscreteSchedulerfrom schedulers.scheduling_flow_match_heun_discrete import FlowMatchHeunDiscreteSchedulerfrom diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3 import retrieve_timestepsfrom diffusers.utils.torch_utils import randn_tensorfrom transformers import UMT5EncoderModel, AutoTokenizer1 unmodified linefrom music_dcae.music_dcae_pipeline import MusicDCAEfrom models.ace_step_transformer import ACEStepTransformer2DModelfrom models.lyrics_utils.lyric_tokenizer import VoiceBpeTokenizerfrom apg_guidance import apg_forward, MomentumBuffer, cfg_forward, cfg_zero_star, cfg_double_condition_forwardimport torchaudioimport toriotorch.backends.cudnn.benchmark = Falsetorch.set_float32_matmul_precision('high')torch.backends.cudnn.deterministic = Truetorch.backends.cuda.matmul.allow_tf32 = Trueos.environ["TOKENIZERS_PARALLELISM"] = "false"SUPPORT_LANGUAGES = {"en": 259, "de": 260, "fr": 262, "es": 284, "it": 285,"pt": 286, "pl": 294, "tr": 295, "ru": 267, "cs": 293,"nl": 297, "ar": 5022, "zh": 5023, "ja": 5412, "hu": 5753,"ko": 6152, "hi": 6680}structure_pattern = re.compile(r"\[.*?\]")11 unmodified lines# class ACEStepPipeline(DiffusionPipeline):class ACEStepPipeline:def __init__(self, checkpoint_dir=None, device_id=0, dtype="bfloat16", text_encoder_checkpoint_path=None, persistent_storage_path=None, torch_compile=False, **kwargs):if not checkpoint_dir:if persistent_storage_path is None:checkpoint_dir = os.path.join(os.path.dirname(__file__), "checkpoints")1 unmodified linecheckpoint_dir = os.path.join(persistent_storage_path, "checkpoints")ensure_directory_exists(checkpoint_dir)self.checkpoint_dir = checkpoint_dirdevice = torch.device(f"cuda:{device_id}") if torch.cuda.is_available() else torch.device("cpu")if device.type == "cpu" and torch.backends.mps.is_available():device = torch.device("mps")self.dtype = torch.bfloat16 if dtype == "bfloat16" else torch.float323 unmodified linesself.loaded = Falseself.torch_compile = torch_compileself.lora_path = "none"def load_lora(self, lora_name_or_path):if lora_name_or_path != self.lora_path and lora_name_or_path != "none":if not os.path.exists(lora_name_or_path):lora_download_path = snapshot_download(lora_name_or_path, cache_dir=self.checkpoint_dir)else:lora_download_path = lora_name_or_pathif self.lora_path != "none":self.ace_step_transformer.unload_lora()self.ace_step_transformer.load_lora_adapter(os.path.join(lora_download_path, "pytorch_lora_weights.safetensors"), adapter_name="zh_rap_lora", with_alpha=True)logger.info(f"Loading lora weights from: {lora_name_or_path} download path is: {lora_download_path}")self.lora_path = lora_name_or_pathelif self.lora_path != "none" and lora_name_or_path == "none":logger.info("No lora weights to load.")8 unmodified linestext_encoder_model_path = os.path.join(checkpoint_dir, "umt5-base")files_exist = (os.path.exists(os.path.join(dcae_model_path, "config.json")) andos.path.exists(os.path.join(dcae_model_path, "diffusion_pytorch_model.safetensors")) andos.path.exists(os.path.join(vocoder_model_path, "config.json")) andos.path.exists(os.path.join(vocoder_model_path, "diffusion_pytorch_model.safetensors")) andos.path.exists(os.path.join(ace_step_model_path, "config.json")) andos.path.exists(os.path.join(ace_step_model_path, "diffusion_pytorch_model.safetensors")) andos.path.exists(os.path.join(text_encoder_model_path, "config.json")) andos.path.exists(os.path.join(text_encoder_model_path, "model.safetensors")) andos.path.exists(os.path.join(text_encoder_model_path, "special_tokens_map.json")) andos.path.exists(os.path.join(text_encoder_model_path, "tokenizer_config.json")) andos.path.exists(os.path.join(text_encoder_model_path, "tokenizer.json")))if not files_exist:logger.info(f"Checkpoint directory {checkpoint_dir} is not complete, downloading from Hugging Face Hub")# download music dcae modelos.makedirs(dcae_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID, subfolder="music_dcae_f8c8",filename="config.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="music_dcae_f8c8",filename="diffusion_pytorch_model.safetensors", local_dir=checkpoint_dir, local_dir_use_symlinks=False)# download vocoder modelos.makedirs(vocoder_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID, subfolder="music_vocoder",filename="config.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="music_vocoder",filename="diffusion_pytorch_model.safetensors", local_dir=checkpoint_dir, local_dir_use_symlinks=False)# download ace_step transformer modelos.makedirs(ace_step_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID, subfolder="ace_step_transformer",filename="config.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="ace_step_transformer",filename="diffusion_pytorch_model.safetensors", local_dir=checkpoint_dir, local_dir_use_symlinks=False)# download text encoder modelos.makedirs(text_encoder_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID, subfolder="umt5-base",filename="config.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="umt5-base",filename="model.safetensors", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="umt5-base",filename="special_tokens_map.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="umt5-base",filename="tokenizer_config.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)hf_hub_download(repo_id=REPO_ID, subfolder="umt5-base",filename="tokenizer.json", local_dir=checkpoint_dir, local_dir_use_symlinks=False)logger.info("Models downloaded")2 unmodified linesace_step_checkpoint_path = ace_step_model_pathtext_encoder_checkpoint_path = text_encoder_model_pathself.music_dcae = MusicDCAE(dcae_checkpoint_path=dcae_checkpoint_path, vocoder_checkpoint_path=vocoder_checkpoint_path)self.music_dcae.to(device).eval().to(self.dtype)self.ace_step_transformer = ACEStepTransformer2DModel.from_pretrained(ace_step_checkpoint_path, torch_dtype=self.dtype)self.ace_step_transformer.to(device).eval().to(self.dtype)lang_segment = LangSegment()lang_segment.setfilters(['af', 'am', 'an', 'ar', 'as', 'az', 'be', 'bg', 'bn', 'br', 'bs', 'ca', 'cs', 'cy', 'da', 'de', 'dz', 'el','en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'ga', 'gl', 'gu', 'he', 'hi', 'hr', 'ht', 'hu', 'hy','id', 'is', 'it', 'ja', 'jv', 'ka', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo', 'lt', 'lv', 'mg','mk', 'ml', 'mn', 'mr', 'ms', 'mt', 'nb', 'ne', 'nl', 'nn', 'no', 'oc', 'or', 'pa', 'pl', 'ps', 'pt', 'qu','ro', 'ru', 'rw', 'se', 'si', 'sk', 'sl', 'sq', 'sr', 'sv', 'sw', 'ta', 'te', 'th', 'tl', 'tr', 'ug', 'uk','ur', 'vi', 'vo', 'wa', 'xh', 'zh', 'zu'])self.lang_segment = lang_segmentself.lyric_tokenizer = VoiceBpeTokenizer()text_encoder_model = UMT5EncoderModel.from_pretrained(text_encoder_checkpoint_path, torch_dtype=self.dtype).eval()text_encoder_model = text_encoder_model.to(device).to(self.dtype)text_encoder_model.requires_grad_(False)self.text_encoder_model = text_encoder_modelself.text_tokenizer = AutoTokenizer.from_pretrained(text_encoder_checkpoint_path)self.loaded = True# compile3 unmodified linesself.text_encoder_model = torch.compile(self.text_encoder_model)def get_text_embeddings(self, texts, device, text_max_length=256):inputs = self.text_tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=text_max_length)inputs = {key: value.to(device) for key, value in inputs.items()}if self.text_encoder_model.device != device:self.text_encoder_model.to(device)2 unmodified lineslast_hidden_states = outputs.last_hidden_stateattention_mask = inputs["attention_mask"]return last_hidden_states, attention_maskdef get_text_embeddings_null(self, texts, device, text_max_length=256, tau=0.01, l_min=8, l_max=10):inputs = self.text_tokenizer(texts, return_tensors="pt", padding=True, truncation=True, max_length=text_max_length)inputs = {key: value.to(device) for key, value in inputs.items()}if self.text_encoder_model.device != device:self.text_encoder_model.to(device)def forward_with_temperature(inputs, tau=0.01, l_min=8, l_max=10):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = self.text_encoder_model.encoder.block[i].layer[0].SelfAttention.q.register_forward_hook(hook)handlers.append(handler)with torch.no_grad():outputs = self.text_encoder_model(**inputs)last_hidden_states = outputs.last_hidden_statefor hook in handlers:hook.remove()return last_hidden_stateslast_hidden_states = forward_with_temperature(inputs, tau, l_min, l_max)return last_hidden_statesdef set_seeds(self, batch_size, manual_seeds=None):seeds = Noneif manual_seeds is not None:if isinstance(manual_seeds, str):if "," in manual_seeds:seeds = list(map(int, manual_seeds.split(",")))elif manual_seeds.isdigit():seeds = int(manual_seeds)random_generators = [torch.Generator(device=self.device) for _ in range(batch_size)]actual_seeds = []for i in range(batch_size):seed = Noneif seeds is None:seed = torch.randint(0, 2**32, (1,)).item()if isinstance(seeds, int):seed = seedsif isinstance(seeds, list):seed = seeds[i]random_generators[i].manual_seed(seed)actual_seeds.append(seed)return random_generators, actual_seedsdef get_lang(self, text):language = "en"try:_ = self.lang_segment.getTexts(text)langCounts = self.lang_segment.getCounts()language = langCounts[0][0]27 unmodified lineselse:token_idx = self.lyric_tokenizer.encode(line, lang)if debug:toks = self.lyric_tokenizer.batch_decode([[tok_id] for tok_id in token_idx])logger.info(f"debbug {line} --> {lang} --> {toks}")lyric_token_idx = lyric_token_idx + token_idx + [2]except Exception as e:22 unmodified linesattention_mask=None,momentum_buffer=None,momentum_buffer_tar=None,return_src_pred=True):noise_pred_src = Noneif return_src_pred:src_latent_model_input = torch.cat([zt_src, zt_src]) if do_classifier_free_guidance else zt_srctimestep = t.expand(src_latent_model_input.shape[0])# sourcenoise_pred_src = self.ace_step_transformer(8 unmodified lines).sampleif do_classifier_free_guidance:noise_pred_with_cond_src, noise_pred_uncond_src = noise_pred_src.chunk(2)if cfg_type == "apg":noise_pred_src = apg_forward(pred_cond=noise_pred_with_cond_src,8 unmodified linescfg_strength=guidance_scale,)tar_latent_model_input = torch.cat([zt_tar, zt_tar]) if do_classifier_free_guidance else zt_tartimestep = t.expand(tar_latent_model_input.shape[0])# targetnoise_pred_tar = self.ace_step_transformer(63 unmodified linesT_steps = infer_stepsframe_length = src_latents.shape[-1]attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)timesteps, T_steps = retrieve_timesteps(scheduler, T_steps, device, timesteps=None)if do_classifier_free_guidance:attention_mask = torch.cat([attention_mask] * 2, dim=0)encoder_text_hidden_states = torch.cat([encoder_text_hidden_states, torch.zeros_like(encoder_text_hidden_states)], 0)text_attention_mask = torch.cat([text_attention_mask] * 2, dim=0)target_encoder_text_hidden_states = torch.cat([target_encoder_text_hidden_states, torch.zeros_like(target_encoder_text_hidden_states)], 0)target_text_attention_mask = torch.cat([target_text_attention_mask] * 2, dim=0)speaker_embds = torch.cat([speaker_embds, torch.zeros_like(speaker_embds)], 0)target_speaker_embeds = torch.cat([target_speaker_embeds, torch.zeros_like(target_speaker_embeds)], 0)lyric_token_ids = torch.cat([lyric_token_ids, torch.zeros_like(lyric_token_ids)], 0)lyric_mask = torch.cat([lyric_mask, torch.zeros_like(lyric_mask)], 0)target_lyric_token_ids = torch.cat([target_lyric_token_ids, torch.zeros_like(target_lyric_token_ids)], 0)target_lyric_mask = torch.cat([target_lyric_mask, torch.zeros_like(target_lyric_mask)], 0)momentum_buffer = MomentumBuffer()momentum_buffer_tar = MomentumBuffer()10 unmodified linesif i < n_min:continuet_i = t/1000if i+1 < len(timesteps):t_im1 = (timesteps[i+1])/1000else:t_im1 = torch.zeros_like(t_i).to(t_i.device)1 unmodified line# Calculate the average of the V predictionsV_delta_avg = torch.zeros_like(x_src)for k in range(n_avg):fwd_noise = randn_tensor(shape=x_src.shape, generator=random_generators, device=device, dtype=dtype)zt_src = (1 - t_i) * x_src + (t_i) * fwd_noise17 unmodified linesguidance_scale=guidance_scale,target_guidance_scale=target_guidance_scale,attention_mask=attention_mask,momentum_buffer=momentum_buffer)V_delta_avg += (1 / n_avg) * (Vt_tar - Vt_src) # - (hfg-1)*( x_src))# propagate direct ODEzt_edit = zt_edit.to(torch.float32)zt_edit = zt_edit + (t_im1 - t_i) * V_delta_avgzt_edit = zt_edit.to(V_delta_avg.dtype)else: # i >= T_steps-n_min # regular sampling for last n_min stepsif i == n_max:fwd_noise = randn_tensor(shape=x_src.shape, generator=random_generators, device=device, dtype=dtype)scheduler._init_step_index(t)sigma = scheduler.sigmas[scheduler.step_index]xt_src = sigma * fwd_noise + (1.0 - sigma) * x_srcxt_tar = zt_edit + xt_src - x_src_, Vt_tar = self.calc_v(zt_src=None,zt_tar=xt_tar,15 unmodified linesmomentum_buffer_tar=momentum_buffer_tar,return_src_pred=False,)dtype = Vt_tar.dtypext_tar = xt_tar.to(torch.float32)prev_sample = xt_tar + (t_im1 - t_i) * Vt_tarprev_sample = prev_sample.to(dtype)xt_tar = prev_sampletarget_latents = zt_edit if xt_tar is None else xt_tarreturn target_latents11 unmodified linestimesteps = scheduler.timesteps.unsqueeze(1).to(gt_latents.dtype)indices = indices.to(timesteps.device).to(gt_latents.dtype).unsqueeze(1)nearest_idx = torch.argmin(torch.cdist(indices, timesteps), dim=1)sigma = scheduler.sigmas[nearest_idx].flatten().to(gt_latents.device).to(gt_latents.dtype)while len(sigma.shape) < gt_latents.ndim:sigma = sigma.unsqueeze(-1)noisy_image = sigma * noise + (1.0 - sigma) * gt_latents37 unmodified linesref_latents=None,):logger.info("cfg_type: {}, guidance_scale: {}, omega_scale: {}".format(cfg_type, guidance_scale, omega_scale))do_classifier_free_guidance = Trueif guidance_scale == 0.0 or guidance_scale == 1.0:do_classifier_free_guidance = Falsedo_double_condition_guidance = Falseif guidance_scale_text is not None and guidance_scale_text > 1.0 and guidance_scale_lyric is not None and guidance_scale_lyric > 1.0:do_double_condition_guidance = Truelogger.info("do_double_condition_guidance: {}, guidance_scale_text: {}, guidance_scale_lyric: {}".format(do_double_condition_guidance, guidance_scale_text, guidance_scale_lyric))device = encoder_text_hidden_states.devicedtype = encoder_text_hidden_states.dtype9 unmodified linesnum_train_timesteps=1000,shift=3.0,)frame_length = int(duration * 44100 / 512 / 8)if src_latents is not None:frame_length = src_latents.shape[-1]4 unmodified linesif len(oss_steps) > 0:infer_steps = max(oss_steps)scheduler.set_timestepstimesteps, num_inference_steps = retrieve_timesteps(scheduler, num_inference_steps=infer_steps, device=device, timesteps=None)new_timesteps = torch.zeros(len(oss_steps), dtype=dtype, device=device)for idx in range(len(oss_steps)):new_timesteps[idx] = timesteps[oss_steps[idx]-1]num_inference_steps = len(oss_steps)sigmas = (new_timesteps / 1000).float().cpu().numpy()timesteps, num_inference_steps = retrieve_timesteps(scheduler, num_inference_steps=num_inference_steps, device=device, sigmas=sigmas)logger.info(f"oss_steps: {oss_steps}, num_inference_steps: {num_inference_steps} after remapping to timesteps {timesteps}")else:timesteps, num_inference_steps = retrieve_timesteps(scheduler, num_inference_steps=infer_steps, device=device, timesteps=None)target_latents = randn_tensor(shape=(bsz, 8, 16, frame_length), generator=random_generators, device=device, dtype=dtype)is_repaint = Falseis_extend = Falseif add_retake_noise:n_min = int(infer_steps * (1 - retake_variance))retake_variance = torch.tensor(retake_variance * math.pi/2).to(device).to(dtype)retake_latents = randn_tensor(shape=(bsz, 8, 16, frame_length), generator=retake_random_generators, device=device, dtype=dtype)repaint_start_frame = int(repaint_start * 44100 / 512 / 8)repaint_end_frame = int(repaint_end * 44100 / 512 / 8)x0 = src_latents# retakeis_repaint = (repaint_end_frame - repaint_start_frame != frame_length)is_extend = (repaint_start_frame < 0) or (repaint_end_frame > frame_length)if is_extend:is_repaint = True1 unmodified line# TODO: train a mask aware repainting controlnet# to make sure mean = 0, std = 1if not is_repaint:target_latents = torch.cos(retake_variance) * target_latents + torch.sin(retake_variance) * retake_latentselif not is_extend:# if repaint_end_framerepaint_mask = torch.zeros((bsz, 8, 16, frame_length), device=device, dtype=dtype)repaint_mask[:, :, :, repaint_start_frame:repaint_end_frame] = 1.0repaint_noise = torch.cos(retake_variance) * target_latents + torch.sin(retake_variance) * retake_latentsrepaint_noise = torch.where(repaint_mask == 1.0, repaint_noise, target_latents)zt_edit = x0.clone()z0 = repaint_noiseelif is_extend:9 unmodified linesif repaint_start_frame < 0:left_pad_frame_length = abs(repaint_start_frame)frame_length = left_pad_frame_length + gt_latents.shape[-1]extend_gt_latents = torch.nn.functional.pad(gt_latents, (left_pad_frame_length, 0), "constant", 0)if frame_length > max_infer_fame_length:right_trim_length = frame_length - max_infer_fame_lengthextend_gt_latents = extend_gt_latents[:,:,:,:max_infer_fame_length]to_right_pad_gt_latents = extend_gt_latents[:,:,:,-right_trim_length:]frame_length = max_infer_fame_lengthrepaint_start_frame = 0gt_latents = extend_gt_latentsif repaint_end_frame > src_latents_length:right_pad_frame_length = repaint_end_frame - gt_latents.shape[-1]frame_length = gt_latents.shape[-1] + right_pad_frame_lengthextend_gt_latents = torch.nn.functional.pad(gt_latents, (0, right_pad_frame_length), "constant", 0)if frame_length > max_infer_fame_length:left_trim_length = frame_length - max_infer_fame_lengthextend_gt_latents = extend_gt_latents[:,:,:,-max_infer_fame_length:]to_left_pad_gt_latents = extend_gt_latents[:,:,:,:left_trim_length]frame_length = max_infer_fame_lengthrepaint_end_frame = frame_lengthgt_latents = extend_gt_latentsrepaint_mask = torch.zeros((bsz, 8, 16, frame_length), device=device, dtype=dtype)if left_pad_frame_length > 0:repaint_mask[:,:,:,:left_pad_frame_length] = 1.0if right_pad_frame_length > 0:repaint_mask[:,:,:,-right_pad_frame_length:] = 1.0x0 = gt_latentspadd_list = []if left_pad_frame_length > 0:padd_list.append(retake_latents[:, :, :, :left_pad_frame_length])padd_list.append(target_latents[:,:,:,left_trim_length:target_latents.shape[-1]-right_trim_length])if right_pad_frame_length > 0:padd_list.append(retake_latents[:, :, :, -right_pad_frame_length:])target_latents = torch.cat(padd_list, dim=-1)assert target_latents.shape[-1] == x0.shape[-1], f"{target_latents.shape=} {x0.shape=}"zt_edit = x0.clone()z0 = target_latentsinit_timestep = 1000if audio2audio_enable and ref_latents is not None:target_latents, init_timestep = self.add_latents_noise(gt_latents=ref_latents, variance=(1-ref_audio_strength), noise=target_latents, scheduler=scheduler)attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)# guidance intervalstart_idx = int(num_inference_steps * ((1 - guidance_interval) / 2))end_idx = int(num_inference_steps * (guidance_interval / 2 + 0.5))logger.info(f"start_idx: {start_idx}, end_idx: {end_idx}, num_inference_steps: {num_inference_steps}")momentum_buffer = MomentumBuffer()def forward_encoder_with_temperature(self, inputs, tau=0.01, l_min=4, l_max=6):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = self.ace_step_transformer.lyric_encoder.encoders[i].self_attn.linear_q.register_forward_hook(hook)handlers.append(handler)encoder_hidden_states, encoder_hidden_mask = self.ace_step_transformer.encode(**inputs)for hook in handlers:hook.remove()return encoder_hidden_states# P(speaker, text, lyric)10 unmodified linesencoder_hidden_states_null = forward_encoder_with_temperature(self,inputs={"encoder_text_hidden_states": encoder_text_hidden_states_null if encoder_text_hidden_states_null is not None else torch.zeros_like(encoder_text_hidden_states),"text_attention_mask": text_attention_mask,"speaker_embeds": torch.zeros_like(speaker_embds),"lyric_token_idx": lyric_token_ids,"lyric_mask": lyric_mask,})else:# P(null_speaker, null_text, null_lyric)4 unmodified linestorch.zeros_like(lyric_token_ids),lyric_mask,)encoder_hidden_states_no_lyric = Noneif do_double_condition_guidance:# P(null_speaker, text, lyric_weaker)6 unmodified lines"speaker_embeds": torch.zeros_like(speaker_embds),"lyric_token_idx": lyric_token_ids,"lyric_mask": lyric_mask,})# P(null_speaker, text, no_lyric)else:5 unmodified lineslyric_mask,)def forward_diffusion_with_temperature(self, hidden_states, timestep, inputs, tau=0.01, l_min=15, l_max=20):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = self.ace_step_transformer.transformer_blocks[i].attn.to_q.register_forward_hook(hook)handlers.append(handler)handler = self.ace_step_transformer.transformer_blocks[i].cross_attn.to_q.register_forward_hook(hook)handlers.append(handler)sample = self.ace_step_transformer.decode(hidden_states=hidden_states, timestep=timestep, **inputs).samplefor hook in handlers:hook.remove()return samplefor i, t in tqdm(enumerate(timesteps), total=num_inference_steps):if t > init_timestep:16 unmodified lines# compute current guidance scaleif guidance_interval_decay > 0:# Linearly interpolate to calculate the current guidance scaleprogress = (i - start_idx) / (end_idx - start_idx - 1) # 归一化到[0,1]current_guidance_scale = guidance_scale - (guidance_scale - min_guidance_scale) * progress * guidance_interval_decayelse:current_guidance_scale = guidance_scale11 unmodified lines).samplenoise_pred_with_only_text_cond = Noneif do_double_condition_guidance and encoder_hidden_states_no_lyric is not None:noise_pred_with_only_text_cond = self.ace_step_transformer.decode(hidden_states=latent_model_input,attention_mask=attention_mask,25 unmodified linestimestep=timestep,).sampleif do_double_condition_guidance and noise_pred_with_only_text_cond is not None:noise_pred = cfg_double_condition_forward(cond_output=noise_pred_with_cond,uncond_output=noise_pred_uncond,22 unmodified linesguidance_scale=current_guidance_scale,i=i,zero_steps=zero_steps,use_zero_init=use_zero_init)else:latent_model_input = latents8 unmodified lines).sampleif is_repaint and i >= n_min:t_i = t/1000if i+1 < len(timesteps):t_im1 = (timesteps[i+1])/1000else:t_im1 = torch.zeros_like(t_i).to(t_i.device)dtype = noise_pred.dtype2 unmodified linesprev_sample = prev_sample.to(dtype)target_latents = prev_samplezt_src = (1 - t_im1) * x0 + (t_im1) * z0target_latents = torch.where(repaint_mask == 1.0, target_latents, zt_src)else:target_latents = scheduler.step(model_output=noise_pred, timestep=t, sample=target_latents, return_dict=False, omega=omega_scale)[0]if is_extend:if to_right_pad_gt_latents is not None:target_latents = torch.cat([target_latents, to_right_pad_gt_latents], dim=-1)if to_left_pad_gt_latents is not None:target_latents = torch.cat([to_right_pad_gt_latents, target_latents], dim=0)return target_latentsdef latents2audio(self, latents, target_wav_duration_second=30, sample_rate=48000, save_path=None, format="mp3"):output_audio_paths = []bs = latents.shape[0]audio_lengths = [target_wav_duration_second * sample_rate] * bs2 unmodified lines_, pred_wavs = self.music_dcae.decode(pred_latents, sr=sample_rate)pred_wavs = [pred_wav.cpu().float() for pred_wav in pred_wavs]for i in tqdm(range(bs)):output_audio_path = self.save_wav_file(pred_wavs[i], i, sample_rate=sample_rate)output_audio_paths.append(output_audio_path)return output_audio_pathsdef save_wav_file(self, target_wav, idx, save_path=None, sample_rate=48000, format="mp3"):if save_path is None:logger.warning("save_path is None, using default path ./outputs/")base_path = f"./outputs"2 unmodified linesbase_path = save_pathensure_directory_exists(base_path)output_path_flac = f"{base_path}/output_{time.strftime('%Y%m%d%H%M%S')}_{idx}.{format}"target_wav = target_wav.float()torchaudio.save(output_path_flac, target_wav, sample_rate=sample_rate, format=format, compression=torio.io.CodecConfig(bit_rate=320000))return output_path_flacdef infer_latents(self, input_audio_path):19 unmodified linesomega_scale: int = 10.0,manual_seeds: list = None,guidance_interval: float = 0.5,guidance_interval_decay: float = 0.,min_guidance_scale: float = 3.0,use_erg_tag: bool = True,use_erg_lyric: bool = True,36 unmodified linesstart_time = time.time()random_generators, actual_seeds = self.set_seeds(batch_size, manual_seeds)retake_random_generators, actual_retake_seeds = self.set_seeds(batch_size, retake_seeds)if isinstance(oss_steps, str) and len(oss_steps) > 0:oss_steps = list(map(int, oss_steps.split(",")))else:oss_steps = []texts = [prompt]encoder_text_hidden_states, text_attention_mask = self.get_text_embeddings(texts, self.device)encoder_text_hidden_states = encoder_text_hidden_states.repeat(batch_size, 1, 1)text_attention_mask = text_attention_mask.repeat(batch_size, 1)encoder_text_hidden_states_null = Noneif use_erg_tag:encoder_text_hidden_states_null = self.get_text_embeddings_null(texts, self.device)encoder_text_hidden_states_null = encoder_text_hidden_states_null.repeat(batch_size, 1, 1)# not support for released checkpointspeaker_embeds = torch.zeros(batch_size, 512).to(self.device).to(self.dtype)4 unmodified linesif len(lyrics) > 0:lyric_token_idx = self.tokenize_lyrics(lyrics, debug=debug)lyric_mask = [1] * len(lyric_token_idx)lyric_token_idx = torch.tensor(lyric_token_idx).unsqueeze(0).to(self.device).repeat(batch_size, 1)lyric_mask = torch.tensor(lyric_mask).unsqueeze(0).to(self.device).repeat(batch_size, 1)if audio_duration <= 0:audio_duration = random.uniform(30.0, 240.0)8 unmodified linesif task == "retake":repaint_start = 0repaint_end = audio_durationsrc_latents = Noneif src_audio_path is not None:assert src_audio_path is not None and task in ("repaint", "edit", "extend"), "src_audio_path is required for retake/repaint/extend task"assert os.path.exists(src_audio_path), f"src_audio_path {src_audio_path} does not exist"src_latents = self.infer_latents(src_audio_path)ref_latents = Noneif ref_audio_input is not None and audio2audio_enable:assert ref_audio_input is not None, "ref_audio_input is required for audio2audio task"assert os.path.exists(ref_audio_input), f"ref_audio_input {ref_audio_input} does not exist"1 unmodified lineif task == "edit":texts = [edit_target_prompt]target_encoder_text_hidden_states, target_text_attention_mask = self.get_text_embeddings(texts, self.device)target_encoder_text_hidden_states = target_encoder_text_hidden_states.repeat(batch_size, 1, 1)target_text_attention_mask = target_text_attention_mask.repeat(batch_size, 1)target_lyric_token_idx = torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()target_lyric_mask = torch.tensor([0]).repeat(batch_size, 1).to(self.device).long()if len(edit_target_lyrics) > 0:target_lyric_token_idx = self.tokenize_lyrics(edit_target_lyrics, debug=True)target_lyric_mask = [1] * len(target_lyric_token_idx)target_lyric_token_idx = torch.tensor(target_lyric_token_idx).unsqueeze(0).to(self.device).repeat(batch_size, 1)target_lyric_mask = torch.tensor(target_lyric_mask).unsqueeze(0).to(self.device).repeat(batch_size, 1)target_speaker_embeds = speaker_embeds.clone()9 unmodified linestarget_lyric_token_ids=target_lyric_token_idx,target_lyric_mask=target_lyric_mask,src_latents=src_latents,random_generators=retake_random_generators, # more diversityinfer_steps=infer_step,guidance_scale=guidance_scale,n_min=edit_n_min,81 unmodified lines"repaint_end": repaint_end,"edit_n_min": edit_n_min,"edit_n_max": edit_n_max,"edit_n_avg": edit_n_avg,"src_audio_path": src_audio_path,"edit_target_prompt": edit_target_prompt,"edit_target_lyrics": edit_target_lyrics,3 unmodified lines}# save input_params_jsonfor output_audio_path in output_paths:input_params_json_save_path = output_audio_path.replace(f".{format}", "_input_params.json")input_params_json["audio_path"] = output_audio_pathwith open(input_params_json_save_path, "w", encoding="utf-8") as f:json.dump(input_params_json, f, indent=4, ensure_ascii=False)11 unmodified lines1213141516171819202122232425261 unmodified line28293031323334353637383940414243444546474849505152535455565758596061626364656667686911 unmodified lines818283848586878889909192939495961 unmodified line98991001011021031041051061071083 unmodified lines1121131141151161171181191201211221231241251261271281291301311321331341351368 unmodified lines1451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682 unmodified lines2712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004013 unmodified lines4054064074084094104114124134144154164172 unmodified lines42042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550627 unmodified lines53453553653753853954054154222 unmodified lines5655665675685695705715725735745755765778 unmodified lines5865875885895905915925935948 unmodified lines60360460560660760860961061163 unmodified lines67567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572610 unmodified lines7377387397407417427437447457461 unmodified line74874975075175275375475575675775875917 unmodified lines77777877978078178278378478578678778878979079179279379479579679779879980080180280380480515 unmodified lines82182282382482582682782882983083183283311 unmodified lines84584684784884985085185285385485585637 unmodified lines8948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239 unmodified lines9339349359369379389394 unmodified lines94494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031 unmodified line100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610279 unmodified lines1037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114310 unmodified lines11541155115611571158115911601161116211631164116511661167116811694 unmodified lines11741175117611771178117911806 unmodified lines11871188118911901191119211935 unmodified lines119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123216 unmodified lines12491250125112521253125412551256125712581259126012611262126311 unmodified lines127512761277127812791280128112821283128425 unmodified lines131013111312131313141315131613171318131922 unmodified lines13421343134413451346134713488 unmodified lines1357135813591360136113621363136413652 unmodified lines13681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314042 unmodified lines1407140814091410141114121413141414151416141714181419142014212 unmodified lines1424142514261427142814291430143114321433143414351436143714381439144019 unmodified lines146014611462146314641465146636 unmodified lines1503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115324 unmodified lines1537153815391540154115421543154415451546154715481549155015511552155315548 unmodified lines1563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861 unmodified line1588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516269 unmodified lines163616371638163916401641164281 unmodified lines17241725172617271728172917303 unmodified lines17341735173617371738173917401741174211 unmodified linesfrom huggingface_hub import hf_hub_download, snapshot_download# from diffusers.pipelines.pipeline_utils import DiffusionPipelinefrom schedulers.scheduling_flow_match_euler_discrete import (FlowMatchEulerDiscreteScheduler,)from schedulers.scheduling_flow_match_heun_discrete import (FlowMatchHeunDiscreteScheduler,)from diffusers.pipelines.stable_diffusion_3.pipeline_stable_diffusion_3 import (retrieve_timesteps,)from diffusers.utils.torch_utils import randn_tensorfrom transformers import UMT5EncoderModel, AutoTokenizer1 unmodified linefrom music_dcae.music_dcae_pipeline import MusicDCAEfrom models.ace_step_transformer import ACEStepTransformer2DModelfrom models.lyrics_utils.lyric_tokenizer import VoiceBpeTokenizerfrom apg_guidance import (apg_forward,MomentumBuffer,cfg_forward,cfg_zero_star,cfg_double_condition_forward,)import torchaudioimport toriotorch.backends.cudnn.benchmark = Falsetorch.set_float32_matmul_precision("high")torch.backends.cudnn.deterministic = Truetorch.backends.cuda.matmul.allow_tf32 = Trueos.environ["TOKENIZERS_PARALLELISM"] = "false"SUPPORT_LANGUAGES = {"en": 259,"de": 260,"fr": 262,"es": 284,"it": 285,"pt": 286,"pl": 294,"tr": 295,"ru": 267,"cs": 293,"nl": 297,"ar": 5022,"zh": 5023,"ja": 5412,"hu": 5753,"ko": 6152,"hi": 6680,}structure_pattern = re.compile(r"\[.*?\]")11 unmodified lines# class ACEStepPipeline(DiffusionPipeline):class ACEStepPipeline:def __init__(self,checkpoint_dir=None,device_id=0,dtype="bfloat16",text_encoder_checkpoint_path=None,persistent_storage_path=None,torch_compile=False,**kwargs,):if not checkpoint_dir:if persistent_storage_path is None:checkpoint_dir = os.path.join(os.path.dirname(__file__), "checkpoints")1 unmodified linecheckpoint_dir = os.path.join(persistent_storage_path, "checkpoints")ensure_directory_exists(checkpoint_dir)self.checkpoint_dir = checkpoint_dirdevice = (torch.device(f"cuda:{device_id}")if torch.cuda.is_available()else torch.device("cpu"))if device.type == "cpu" and torch.backends.mps.is_available():device = torch.device("mps")self.dtype = torch.bfloat16 if dtype == "bfloat16" else torch.float323 unmodified linesself.loaded = Falseself.torch_compile = torch_compileself.lora_path = "none"def load_lora(self, lora_name_or_path):if lora_name_or_path != self.lora_path and lora_name_or_path != "none":if not os.path.exists(lora_name_or_path):lora_download_path = snapshot_download(lora_name_or_path, cache_dir=self.checkpoint_dir)else:lora_download_path = lora_name_or_pathif self.lora_path != "none":self.ace_step_transformer.unload_lora()self.ace_step_transformer.load_lora_adapter(os.path.join(lora_download_path, "pytorch_lora_weights.safetensors"),adapter_name="zh_rap_lora",with_alpha=True,)logger.info(f"Loading lora weights from: {lora_name_or_path} download path is: {lora_download_path}")self.lora_path = lora_name_or_pathelif self.lora_path != "none" and lora_name_or_path == "none":logger.info("No lora weights to load.")8 unmodified linestext_encoder_model_path = os.path.join(checkpoint_dir, "umt5-base")files_exist = (os.path.exists(os.path.join(dcae_model_path, "config.json"))and os.path.exists(os.path.join(dcae_model_path, "diffusion_pytorch_model.safetensors"))and os.path.exists(os.path.join(vocoder_model_path, "config.json"))and os.path.exists(os.path.join(vocoder_model_path, "diffusion_pytorch_model.safetensors"))and os.path.exists(os.path.join(ace_step_model_path, "config.json"))and os.path.exists(os.path.join(ace_step_model_path, "diffusion_pytorch_model.safetensors"))and os.path.exists(os.path.join(text_encoder_model_path, "config.json"))and os.path.exists(os.path.join(text_encoder_model_path, "model.safetensors"))and os.path.exists(os.path.join(text_encoder_model_path, "special_tokens_map.json"))and os.path.exists(os.path.join(text_encoder_model_path, "tokenizer_config.json"))and os.path.exists(os.path.join(text_encoder_model_path, "tokenizer.json")))if not files_exist:logger.info(f"Checkpoint directory {checkpoint_dir} is not complete, downloading from Hugging Face Hub")# download music dcae modelos.makedirs(dcae_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID,subfolder="music_dcae_f8c8",filename="config.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="music_dcae_f8c8",filename="diffusion_pytorch_model.safetensors",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)# download vocoder modelos.makedirs(vocoder_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID,subfolder="music_vocoder",filename="config.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="music_vocoder",filename="diffusion_pytorch_model.safetensors",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)# download ace_step transformer modelos.makedirs(ace_step_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID,subfolder="ace_step_transformer",filename="config.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="ace_step_transformer",filename="diffusion_pytorch_model.safetensors",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)# download text encoder modelos.makedirs(text_encoder_model_path, exist_ok=True)hf_hub_download(repo_id=REPO_ID,subfolder="umt5-base",filename="config.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="umt5-base",filename="model.safetensors",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="umt5-base",filename="special_tokens_map.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="umt5-base",filename="tokenizer_config.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)hf_hub_download(repo_id=REPO_ID,subfolder="umt5-base",filename="tokenizer.json",local_dir=checkpoint_dir,local_dir_use_symlinks=False,)logger.info("Models downloaded")2 unmodified linesace_step_checkpoint_path = ace_step_model_pathtext_encoder_checkpoint_path = text_encoder_model_pathself.music_dcae = MusicDCAE(dcae_checkpoint_path=dcae_checkpoint_path,vocoder_checkpoint_path=vocoder_checkpoint_path,)self.music_dcae.to(device).eval().to(self.dtype)self.ace_step_transformer = ACEStepTransformer2DModel.from_pretrained(ace_step_checkpoint_path, torch_dtype=self.dtype)self.ace_step_transformer.to(device).eval().to(self.dtype)lang_segment = LangSegment()lang_segment.setfilters(["af","am","an","ar","as","az","be","bg","bn","br","bs","ca","cs","cy","da","de","dz","el","en","eo","es","et","eu","fa","fi","fo","fr","ga","gl","gu","he","hi","hr","ht","hu","hy","id","is","it","ja","jv","ka","kk","km","kn","ko","ku","ky","la","lb","lo","lt","lv","mg","mk","ml","mn","mr","ms","mt","nb","ne","nl","nn","no","oc","or","pa","pl","ps","pt","qu","ro","ru","rw","se","si","sk","sl","sq","sr","sv","sw","ta","te","th","tl","tr","ug","uk","ur","vi","vo","wa","xh","zh","zu",])self.lang_segment = lang_segmentself.lyric_tokenizer = VoiceBpeTokenizer()text_encoder_model = UMT5EncoderModel.from_pretrained(text_encoder_checkpoint_path, torch_dtype=self.dtype).eval()text_encoder_model = text_encoder_model.to(device).to(self.dtype)text_encoder_model.requires_grad_(False)self.text_encoder_model = text_encoder_modelself.text_tokenizer = AutoTokenizer.from_pretrained(text_encoder_checkpoint_path)self.loaded = True# compile3 unmodified linesself.text_encoder_model = torch.compile(self.text_encoder_model)def get_text_embeddings(self, texts, device, text_max_length=256):inputs = self.text_tokenizer(texts,return_tensors="pt",padding=True,truncation=True,max_length=text_max_length,)inputs = {key: value.to(device) for key, value in inputs.items()}if self.text_encoder_model.device != device:self.text_encoder_model.to(device)2 unmodified lineslast_hidden_states = outputs.last_hidden_stateattention_mask = inputs["attention_mask"]return last_hidden_states, attention_maskdef get_text_embeddings_null(self, texts, device, text_max_length=256, tau=0.01, l_min=8, l_max=10):inputs = self.text_tokenizer(texts,return_tensors="pt",padding=True,truncation=True,max_length=text_max_length,)inputs = {key: value.to(device) for key, value in inputs.items()}if self.text_encoder_model.device != device:self.text_encoder_model.to(device)def forward_with_temperature(inputs, tau=0.01, l_min=8, l_max=10):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = (self.text_encoder_model.encoder.block[i].layer[0].SelfAttention.q.register_forward_hook(hook))handlers.append(handler)with torch.no_grad():outputs = self.text_encoder_model(**inputs)last_hidden_states = outputs.last_hidden_statefor hook in handlers:hook.remove()return last_hidden_stateslast_hidden_states = forward_with_temperature(inputs, tau, l_min, l_max)return last_hidden_statesdef set_seeds(self, batch_size, manual_seeds=None):processed_input_seeds = Noneif manual_seeds is not None:if isinstance(manual_seeds, str):if "," in manual_seeds:processed_input_seeds = list(map(int, manual_seeds.split(",")))elif manual_seeds.isdigit():processed_input_seeds = int(manual_seeds)elif isinstance(manual_seeds, list) and all(isinstance(s, int) for s in manual_seeds):if len(manual_seeds) > 0:processed_input_seeds = list(manual_seeds)elif isinstance(manual_seeds, int):processed_input_seeds = manual_seedsrandom_generators = [torch.Generator(device=self.device) for _ in range(batch_size)]actual_seeds = []for i in range(batch_size):current_seed_for_generator = Noneif processed_input_seeds is None:current_seed_for_generator = torch.randint(0, 2**32, (1,)).item()elif isinstance(processed_input_seeds, int):current_seed_for_generator = processed_input_seedselif isinstance(processed_input_seeds, list):if i < len(processed_input_seeds):current_seed_for_generator = processed_input_seeds[i]else:current_seed_for_generator = processed_input_seeds[-1]if current_seed_for_generator is None:current_seed_for_generator = torch.randint(0, 2**32, (1,)).item()random_generators[i].manual_seed(current_seed_for_generator)actual_seeds.append(current_seed_for_generator)return random_generators, actual_seedsdef get_lang(self, text):language = "en"try:_ = self.lang_segment.getTexts(text)langCounts = self.lang_segment.getCounts()language = langCounts[0][0]27 unmodified lineselse:token_idx = self.lyric_tokenizer.encode(line, lang)if debug:toks = self.lyric_tokenizer.batch_decode([[tok_id] for tok_id in token_idx])logger.info(f"debbug {line} --> {lang} --> {toks}")lyric_token_idx = lyric_token_idx + token_idx + [2]except Exception as e:22 unmodified linesattention_mask=None,momentum_buffer=None,momentum_buffer_tar=None,return_src_pred=True,):noise_pred_src = Noneif return_src_pred:src_latent_model_input = (torch.cat([zt_src, zt_src]) if do_classifier_free_guidance else zt_src)timestep = t.expand(src_latent_model_input.shape[0])# sourcenoise_pred_src = self.ace_step_transformer(8 unmodified lines).sampleif do_classifier_free_guidance:noise_pred_with_cond_src, noise_pred_uncond_src = noise_pred_src.chunk(2)if cfg_type == "apg":noise_pred_src = apg_forward(pred_cond=noise_pred_with_cond_src,8 unmodified linescfg_strength=guidance_scale,)tar_latent_model_input = (torch.cat([zt_tar, zt_tar]) if do_classifier_free_guidance else zt_tar)timestep = t.expand(tar_latent_model_input.shape[0])# targetnoise_pred_tar = self.ace_step_transformer(63 unmodified linesT_steps = infer_stepsframe_length = src_latents.shape[-1]attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)timesteps, T_steps = retrieve_timesteps(scheduler, T_steps, device, timesteps=None)if do_classifier_free_guidance:attention_mask = torch.cat([attention_mask] * 2, dim=0)encoder_text_hidden_states = torch.cat([encoder_text_hidden_states,torch.zeros_like(encoder_text_hidden_states),],0,)text_attention_mask = torch.cat([text_attention_mask] * 2, dim=0)target_encoder_text_hidden_states = torch.cat([target_encoder_text_hidden_states,torch.zeros_like(target_encoder_text_hidden_states),],0,)target_text_attention_mask = torch.cat([target_text_attention_mask] * 2, dim=0)speaker_embds = torch.cat([speaker_embds, torch.zeros_like(speaker_embds)], 0)target_speaker_embeds = torch.cat([target_speaker_embeds, torch.zeros_like(target_speaker_embeds)], 0)lyric_token_ids = torch.cat([lyric_token_ids, torch.zeros_like(lyric_token_ids)], 0)lyric_mask = torch.cat([lyric_mask, torch.zeros_like(lyric_mask)], 0)target_lyric_token_ids = torch.cat([target_lyric_token_ids, torch.zeros_like(target_lyric_token_ids)], 0)target_lyric_mask = torch.cat([target_lyric_mask, torch.zeros_like(target_lyric_mask)], 0)momentum_buffer = MomentumBuffer()momentum_buffer_tar = MomentumBuffer()10 unmodified linesif i < n_min:continuet_i = t / 1000if i + 1 < len(timesteps):t_im1 = (timesteps[i + 1]) / 1000else:t_im1 = torch.zeros_like(t_i).to(t_i.device)1 unmodified line# Calculate the average of the V predictionsV_delta_avg = torch.zeros_like(x_src)for k in range(n_avg):fwd_noise = randn_tensor(shape=x_src.shape,generator=random_generators,device=device,dtype=dtype,)zt_src = (1 - t_i) * x_src + (t_i) * fwd_noise17 unmodified linesguidance_scale=guidance_scale,target_guidance_scale=target_guidance_scale,attention_mask=attention_mask,momentum_buffer=momentum_buffer,)V_delta_avg += (1 / n_avg) * (Vt_tar - Vt_src) # - (hfg-1)*( x_src))# propagate direct ODEzt_edit = zt_edit.to(torch.float32)zt_edit = zt_edit + (t_im1 - t_i) * V_delta_avgzt_edit = zt_edit.to(V_delta_avg.dtype)else: # i >= T_steps-n_min # regular sampling for last n_min stepsif i == n_max:fwd_noise = randn_tensor(shape=x_src.shape,generator=random_generators,device=device,dtype=dtype,)scheduler._init_step_index(t)sigma = scheduler.sigmas[scheduler.step_index]xt_src = sigma * fwd_noise + (1.0 - sigma) * x_srcxt_tar = zt_edit + xt_src - x_src_, Vt_tar = self.calc_v(zt_src=None,zt_tar=xt_tar,15 unmodified linesmomentum_buffer_tar=momentum_buffer_tar,return_src_pred=False,)dtype = Vt_tar.dtypext_tar = xt_tar.to(torch.float32)prev_sample = xt_tar + (t_im1 - t_i) * Vt_tarprev_sample = prev_sample.to(dtype)xt_tar = prev_sampletarget_latents = zt_edit if xt_tar is None else xt_tarreturn target_latents11 unmodified linestimesteps = scheduler.timesteps.unsqueeze(1).to(gt_latents.dtype)indices = indices.to(timesteps.device).to(gt_latents.dtype).unsqueeze(1)nearest_idx = torch.argmin(torch.cdist(indices, timesteps), dim=1)sigma = (scheduler.sigmas[nearest_idx].flatten().to(gt_latents.device).to(gt_latents.dtype))while len(sigma.shape) < gt_latents.ndim:sigma = sigma.unsqueeze(-1)noisy_image = sigma * noise + (1.0 - sigma) * gt_latents37 unmodified linesref_latents=None,):logger.info("cfg_type: {}, guidance_scale: {}, omega_scale: {}".format(cfg_type, guidance_scale, omega_scale))do_classifier_free_guidance = Trueif guidance_scale == 0.0 or guidance_scale == 1.0:do_classifier_free_guidance = Falsedo_double_condition_guidance = Falseif (guidance_scale_text is not Noneand guidance_scale_text > 1.0and guidance_scale_lyric is not Noneand guidance_scale_lyric > 1.0):do_double_condition_guidance = Truelogger.info("do_double_condition_guidance: {}, guidance_scale_text: {}, guidance_scale_lyric: {}".format(do_double_condition_guidance,guidance_scale_text,guidance_scale_lyric,))device = encoder_text_hidden_states.devicedtype = encoder_text_hidden_states.dtype9 unmodified linesnum_train_timesteps=1000,shift=3.0,)frame_length = int(duration * 44100 / 512 / 8)if src_latents is not None:frame_length = src_latents.shape[-1]4 unmodified linesif len(oss_steps) > 0:infer_steps = max(oss_steps)scheduler.set_timestepstimesteps, num_inference_steps = retrieve_timesteps(scheduler,num_inference_steps=infer_steps,device=device,timesteps=None,)new_timesteps = torch.zeros(len(oss_steps), dtype=dtype, device=device)for idx in range(len(oss_steps)):new_timesteps[idx] = timesteps[oss_steps[idx] - 1]num_inference_steps = len(oss_steps)sigmas = (new_timesteps / 1000).float().cpu().numpy()timesteps, num_inference_steps = retrieve_timesteps(scheduler,num_inference_steps=num_inference_steps,device=device,sigmas=sigmas,)logger.info(f"oss_steps: {oss_steps}, num_inference_steps: {num_inference_steps} after remapping to timesteps {timesteps}")else:timesteps, num_inference_steps = retrieve_timesteps(scheduler,num_inference_steps=infer_steps,device=device,timesteps=None,)target_latents = randn_tensor(shape=(bsz, 8, 16, frame_length),generator=random_generators,device=device,dtype=dtype,)is_repaint = Falseis_extend = Falseif add_retake_noise:n_min = int(infer_steps * (1 - retake_variance))retake_variance = (torch.tensor(retake_variance * math.pi / 2).to(device).to(dtype))retake_latents = randn_tensor(shape=(bsz, 8, 16, frame_length),generator=retake_random_generators,device=device,dtype=dtype,)repaint_start_frame = int(repaint_start * 44100 / 512 / 8)repaint_end_frame = int(repaint_end * 44100 / 512 / 8)x0 = src_latents# retakeis_repaint = repaint_end_frame - repaint_start_frame != frame_lengthis_extend = (repaint_start_frame < 0) or (repaint_end_frame > frame_length)if is_extend:is_repaint = True1 unmodified line# TODO: train a mask aware repainting controlnet# to make sure mean = 0, std = 1if not is_repaint:target_latents = (torch.cos(retake_variance) * target_latents+ torch.sin(retake_variance) * retake_latents)elif not is_extend:# if repaint_end_framerepaint_mask = torch.zeros((bsz, 8, 16, frame_length), device=device, dtype=dtype)repaint_mask[:, :, :, repaint_start_frame:repaint_end_frame] = 1.0repaint_noise = (torch.cos(retake_variance) * target_latents+ torch.sin(retake_variance) * retake_latents)repaint_noise = torch.where(repaint_mask == 1.0, repaint_noise, target_latents)zt_edit = x0.clone()z0 = repaint_noiseelif is_extend:9 unmodified linesif repaint_start_frame < 0:left_pad_frame_length = abs(repaint_start_frame)frame_length = left_pad_frame_length + gt_latents.shape[-1]extend_gt_latents = torch.nn.functional.pad(gt_latents, (left_pad_frame_length, 0), "constant", 0)if frame_length > max_infer_fame_length:right_trim_length = frame_length - max_infer_fame_lengthextend_gt_latents = extend_gt_latents[:, :, :, :max_infer_fame_length]to_right_pad_gt_latents = extend_gt_latents[:, :, :, -right_trim_length:]frame_length = max_infer_fame_lengthrepaint_start_frame = 0gt_latents = extend_gt_latentsif repaint_end_frame > src_latents_length:right_pad_frame_length = repaint_end_frame - gt_latents.shape[-1]frame_length = gt_latents.shape[-1] + right_pad_frame_lengthextend_gt_latents = torch.nn.functional.pad(gt_latents, (0, right_pad_frame_length), "constant", 0)if frame_length > max_infer_fame_length:left_trim_length = frame_length - max_infer_fame_lengthextend_gt_latents = extend_gt_latents[:, :, :, -max_infer_fame_length:]to_left_pad_gt_latents = extend_gt_latents[:, :, :, :left_trim_length]frame_length = max_infer_fame_lengthrepaint_end_frame = frame_lengthgt_latents = extend_gt_latentsrepaint_mask = torch.zeros((bsz, 8, 16, frame_length), device=device, dtype=dtype)if left_pad_frame_length > 0:repaint_mask[:, :, :, :left_pad_frame_length] = 1.0if right_pad_frame_length > 0:repaint_mask[:, :, :, -right_pad_frame_length:] = 1.0x0 = gt_latentspadd_list = []if left_pad_frame_length > 0:padd_list.append(retake_latents[:, :, :, :left_pad_frame_length])padd_list.append(target_latents[:,:,:,left_trim_length : target_latents.shape[-1] - right_trim_length,])if right_pad_frame_length > 0:padd_list.append(retake_latents[:, :, :, -right_pad_frame_length:])target_latents = torch.cat(padd_list, dim=-1)assert (target_latents.shape[-1] == x0.shape[-1]), f"{target_latents.shape=} {x0.shape=}"zt_edit = x0.clone()z0 = target_latentsinit_timestep = 1000if audio2audio_enable and ref_latents is not None:target_latents, init_timestep = self.add_latents_noise(gt_latents=ref_latents,variance=(1 - ref_audio_strength),noise=target_latents,scheduler=scheduler,)attention_mask = torch.ones(bsz, frame_length, device=device, dtype=dtype)# guidance intervalstart_idx = int(num_inference_steps * ((1 - guidance_interval) / 2))end_idx = int(num_inference_steps * (guidance_interval / 2 + 0.5))logger.info(f"start_idx: {start_idx}, end_idx: {end_idx}, num_inference_steps: {num_inference_steps}")momentum_buffer = MomentumBuffer()def forward_encoder_with_temperature(self, inputs, tau=0.01, l_min=4, l_max=6):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = self.ace_step_transformer.lyric_encoder.encoders[i].self_attn.linear_q.register_forward_hook(hook)handlers.append(handler)encoder_hidden_states, encoder_hidden_mask = (self.ace_step_transformer.encode(**inputs))for hook in handlers:hook.remove()return encoder_hidden_states# P(speaker, text, lyric)10 unmodified linesencoder_hidden_states_null = forward_encoder_with_temperature(self,inputs={"encoder_text_hidden_states": (encoder_text_hidden_states_nullif encoder_text_hidden_states_null is not Noneelse torch.zeros_like(encoder_text_hidden_states)),"text_attention_mask": text_attention_mask,"speaker_embeds": torch.zeros_like(speaker_embds),"lyric_token_idx": lyric_token_ids,"lyric_mask": lyric_mask,},)else:# P(null_speaker, null_text, null_lyric)4 unmodified linestorch.zeros_like(lyric_token_ids),lyric_mask,)encoder_hidden_states_no_lyric = Noneif do_double_condition_guidance:# P(null_speaker, text, lyric_weaker)6 unmodified lines"speaker_embeds": torch.zeros_like(speaker_embds),"lyric_token_idx": lyric_token_ids,"lyric_mask": lyric_mask,},)# P(null_speaker, text, no_lyric)else:5 unmodified lineslyric_mask,)def forward_diffusion_with_temperature(self, hidden_states, timestep, inputs, tau=0.01, l_min=15, l_max=20):handlers = []def hook(module, input, output):output[:] *= taureturn outputfor i in range(l_min, l_max):handler = self.ace_step_transformer.transformer_blocks[i].attn.to_q.register_forward_hook(hook)handlers.append(handler)handler = self.ace_step_transformer.transformer_blocks[i].cross_attn.to_q.register_forward_hook(hook)handlers.append(handler)sample = self.ace_step_transformer.decode(hidden_states=hidden_states, timestep=timestep, **inputs).samplefor hook in handlers:hook.remove()return samplefor i, t in tqdm(enumerate(timesteps), total=num_inference_steps):if t > init_timestep:16 unmodified lines# compute current guidance scaleif guidance_interval_decay > 0:# Linearly interpolate to calculate the current guidance scaleprogress = (i - start_idx) / (end_idx - start_idx - 1) # 归一化到[0,1]current_guidance_scale = (guidance_scale- (guidance_scale - min_guidance_scale)* progress* guidance_interval_decay)else:current_guidance_scale = guidance_scale11 unmodified lines).samplenoise_pred_with_only_text_cond = Noneif (do_double_condition_guidanceand encoder_hidden_states_no_lyric is not None):noise_pred_with_only_text_cond = self.ace_step_transformer.decode(hidden_states=latent_model_input,attention_mask=attention_mask,25 unmodified linestimestep=timestep,).sampleif (do_double_condition_guidanceand noise_pred_with_only_text_cond is not None):noise_pred = cfg_double_condition_forward(cond_output=noise_pred_with_cond,uncond_output=noise_pred_uncond,22 unmodified linesguidance_scale=current_guidance_scale,i=i,zero_steps=zero_steps,use_zero_init=use_zero_init,)else:latent_model_input = latents8 unmodified lines).sampleif is_repaint and i >= n_min:t_i = t / 1000if i + 1 < len(timesteps):t_im1 = (timesteps[i + 1]) / 1000else:t_im1 = torch.zeros_like(t_i).to(t_i.device)dtype = noise_pred.dtype2 unmodified linesprev_sample = prev_sample.to(dtype)target_latents = prev_samplezt_src = (1 - t_im1) * x0 + (t_im1) * z0target_latents = torch.where(repaint_mask == 1.0, target_latents, zt_src)else:target_latents = scheduler.step(model_output=noise_pred,timestep=t,sample=target_latents,return_dict=False,omega=omega_scale,)[0]if is_extend:if to_right_pad_gt_latents is not None:target_latents = torch.cat([target_latents, to_right_pad_gt_latents], dim=-1)if to_left_pad_gt_latents is not None:target_latents = torch.cat([to_right_pad_gt_latents, target_latents], dim=0)return target_latentsdef latents2audio(self,latents,target_wav_duration_second=30,sample_rate=48000,save_path=None,format="mp3",):output_audio_paths = []bs = latents.shape[0]audio_lengths = [target_wav_duration_second * sample_rate] * bs2 unmodified lines_, pred_wavs = self.music_dcae.decode(pred_latents, sr=sample_rate)pred_wavs = [pred_wav.cpu().float() for pred_wav in pred_wavs]for i in tqdm(range(bs)):output_audio_path = self.save_wav_file(pred_wavs[i], i, sample_rate=sample_rate)output_audio_paths.append(output_audio_path)return output_audio_pathsdef save_wav_file(self, target_wav, idx, save_path=None, sample_rate=48000, format="mp3"):if save_path is None:logger.warning("save_path is None, using default path ./outputs/")base_path = f"./outputs"2 unmodified linesbase_path = save_pathensure_directory_exists(base_path)output_path_flac = (f"{base_path}/output_{time.strftime('%Y%m%d%H%M%S')}_{idx}.{format}")target_wav = target_wav.float()torchaudio.save(output_path_flac,target_wav,sample_rate=sample_rate,format=format,compression=torio.io.CodecConfig(bit_rate=320000),)return output_path_flacdef infer_latents(self, input_audio_path):19 unmodified linesomega_scale: int = 10.0,manual_seeds: list = None,guidance_interval: float = 0.5,guidance_interval_decay: float = 0.0,min_guidance_scale: float = 3.0,use_erg_tag: bool = True,use_erg_lyric: bool = True,36 unmodified linesstart_time = time.time()random_generators, actual_seeds = self.set_seeds(batch_size, manual_seeds)retake_random_generators, actual_retake_seeds = self.set_seeds(batch_size, retake_seeds)if isinstance(oss_steps, str) and len(oss_steps) > 0:oss_steps = list(map(int, oss_steps.split(",")))else:oss_steps = []texts = [prompt]encoder_text_hidden_states, text_attention_mask = self.get_text_embeddings(texts, self.device)encoder_text_hidden_states = encoder_text_hidden_states.repeat(batch_size, 1, 1)text_attention_mask = text_attention_mask.repeat(batch_size, 1)encoder_text_hidden_states_null = Noneif use_erg_tag:encoder_text_hidden_states_null = self.get_text_embeddings_null(texts, self.device)encoder_text_hidden_states_null = encoder_text_hidden_states_null.repeat(batch_size, 1, 1)# not support for released checkpointspeaker_embeds = torch.zeros(batch_size, 512).to(self.device).to(self.dtype)4 unmodified linesif len(lyrics) > 0:lyric_token_idx = self.tokenize_lyrics(lyrics, debug=debug)lyric_mask = [1] * len(lyric_token_idx)lyric_token_idx = (torch.tensor(lyric_token_idx).unsqueeze(0).to(self.device).repeat(batch_size, 1))lyric_mask = (torch.tensor(lyric_mask).unsqueeze(0).to(self.device).repeat(batch_size, 1))if audio_duration <= 0:audio_duration = random.uniform(30.0, 240.0)8 unmodified linesif task == "retake":repaint_start = 0repaint_end = audio_durationsrc_latents = Noneif src_audio_path is not None:assert src_audio_path is not None and task in ("repaint","edit","extend",), "src_audio_path is required for retake/repaint/extend task"assert os.path.exists(src_audio_path), f"src_audio_path {src_audio_path} does not exist"src_latents = self.infer_latents(src_audio_path)ref_latents = Noneif ref_audio_input is not None and audio2audio_enable:assert (ref_audio_input is not None), "ref_audio_input is required for audio2audio task"assert os.path.exists(ref_audio_input), f"ref_audio_input {ref_audio_input} does not exist"1 unmodified lineif task == "edit":texts = [edit_target_prompt]target_encoder_text_hidden_states, target_text_attention_mask = (self.get_text_embeddings(texts, self.device))target_encoder_text_hidden_states = (target_encoder_text_hidden_states.repeat(batch_size, 1, 1))target_text_attention_mask = target_text_attention_mask.repeat(batch_size, 1)target_lyric_token_idx = (torch.tensor([0]).repeat(batch_size, 1).to(self.device).long())target_lyric_mask = (torch.tensor([0]).repeat(batch_size, 1).to(self.device).long())if len(edit_target_lyrics) > 0:target_lyric_token_idx = self.tokenize_lyrics(edit_target_lyrics, debug=True)target_lyric_mask = [1] * len(target_lyric_token_idx)target_lyric_token_idx = (torch.tensor(target_lyric_token_idx).unsqueeze(0).to(self.device).repeat(batch_size, 1))target_lyric_mask = (torch.tensor(target_lyric_mask).unsqueeze(0).to(self.device).repeat(batch_size, 1))target_speaker_embeds = speaker_embeds.clone()9 unmodified linestarget_lyric_token_ids=target_lyric_token_idx,target_lyric_mask=target_lyric_mask,src_latents=src_latents,random_generators=retake_random_generators, # more diversityinfer_steps=infer_step,guidance_scale=guidance_scale,n_min=edit_n_min,81 unmodified lines"repaint_end": repaint_end,"edit_n_min": edit_n_min,"edit_n_max": edit_n_max,"edit_n_avg": edit_n_avg,"src_audio_path": src_audio_path,"edit_target_prompt": edit_target_prompt,"edit_target_lyrics": edit_target_lyrics,3 unmodified lines}# save input_params_jsonfor output_audio_path in output_paths:input_params_json_save_path = output_audio_path.replace(f".{format}", "_input_params.json")input_params_json["audio_path"] = output_audio_pathwith open(input_params_json_save_path, "w", encoding="utf-8") as f:json.dump(input_params_json, f, indent=4, ensure_ascii=False)
examples/default/input_params/output_20250426091716_0_input_params.jsonAdded+25
examples/zh_rap_lora/input_params/output_20250512120348_0_input_params.json+1-1
examples/zh_rap_lora/input_params/output_20250512160830_0_input_params.jsonDeleted-45
pipeline_ace_step.py+708-215