Python
added Pause feature & stead dancer fixes
DeepBeepMeep7ba7ac9e26ae
diff --git a/models/wan/__init__.py b/models/wan/__init__.pyindex 893b061..9570203 100644--- a/models/wan/__init__.py+++ b/models/wan/__init__.py@@ -1,4 +1,4 @@from . import configs, distributed, modules-from .any2video import WanAny2Vfrom .diffusion_forcing import DTT2V+from .any2video import WanAny2Vfrom . import wan_handler, df_handler, ovi_handlerdiff --git a/models/wan/any2video.py b/models/wan/any2video.pyindex 231e36f..a3a45dc 100644--- a/models/wan/any2video.py+++ b/models/wan/any2video.py@@ -432,6 +432,7 @@ class WanAny2V:face_arc_embeds = None,control_scale_alt = 1.,motion_amplitude = 1.,+ full_prefix_video = None,**bbargs):@@ -1059,19 +1060,15 @@ class WanAny2V:gen_args["lynx_ref_buffer"] = [lynx_ref_buffer, lynx_ref_buffer_uncond]elif steadydancer:- if guide_scale == 1:- gen_args = {- "x" : [latent_model_input, latent_model_input],- "context" : [context, context],- "steadydancer_condition": [conditions, conditions_null],- }- any_guidance = condition_guide_scale != 1- else:- gen_args = {- "x" : [latent_model_input, latent_model_input, latent_model_input],- "context" : [context, context_null, context],- "steadydancer_condition": [conditions, conditions, conditions_null],- }+ # DC-CFG: pose guidance only in [10%, 50%] of denoising steps+ apply_cond_cfg = 0.1 <= i / sampling_steps < 0.5 and condition_guide_scale != 1+ x_list, ctx_list, cond_list = [latent_model_input], [context], [conditions]+ if guide_scale != 1:+ x_list.append(latent_model_input); ctx_list.append(context_null); cond_list.append(conditions)+ if apply_cond_cfg:+ x_list.append(latent_model_input); ctx_list.append(context); cond_list.append(conditions_null)+ gen_args = {"x": x_list, "context": ctx_list, "steadydancer_condition": cond_list}+ any_guidance = len(x_list) > 1elif multitalk and audio_proj != None:if guide_scale == 1:gen_args = {@@ -1120,32 +1117,14 @@ class WanAny2V:noise_pred = noise_pred_uncond + guide_scale * (noise_pred_noaudio - noise_pred_uncond) + audio_cfg_scale * (noise_pred_cond - noise_pred_noaudio)noise_pred_noaudio = Noneelif steadydancer:- if apg_switch != 0 and False:- if guide_scale == 1:- noise_pred_cond, noise_pred_drop_audio = ret_values- noise_pred = noise_pred_cond + (audio_cfg_scale - 1)* adaptive_projected_guidance(noise_pred_cond - noise_pred_drop_audio,- noise_pred_cond,- momentum_buffer=audio_momentumbuffer,- norm_threshold=apg_norm_threshold)-- else:- noise_pred_cond, noise_pred_drop_text, noise_pred_uncond = ret_values- noise_pred = noise_pred_cond + (guide_scale - 1) * adaptive_projected_guidance(noise_pred_cond - noise_pred_drop_text,- noise_pred_cond,- momentum_buffer=text_momentumbuffer,- norm_threshold=apg_norm_threshold) \- + (audio_cfg_scale - 1) * adaptive_projected_guidance(noise_pred_drop_text - noise_pred_uncond,- noise_pred_cond,- momentum_buffer=audio_momentumbuffer,- norm_threshold=apg_norm_threshold)- else:- if guide_scale == 1:- noise_pred_cond, noise_pred_uncond_condition = ret_values- noise_pred = noise_pred_uncond_condition + condition_guide_scale * (noise_pred_cond - noise_pred_uncond_condition)- else:- noise_pred_cond, noise_pred_uncond_context, noise_pred_uncond_condition = ret_values- noise_pred = noise_pred_uncond_context + guide_scale * (noise_pred_cond - noise_pred_uncond_context) + condition_guide_scale * (noise_pred_cond - noise_pred_uncond_condition)- noise_pred_uncond_context = noise_pred_cond = noise_pred_uncond_condition = None+ noise_pred_cond = ret_values[0]+ if guide_scale == 1: # only condition CFG (ret_values[1] = uncond_condition)+ noise_pred = ret_values[1] + condition_guide_scale * (noise_pred_cond - ret_values[1])+ else: # text CFG + optionally condition CFG (ret_values[1] = uncond_context)+ noise_pred = ret_values[1] + guide_scale * (noise_pred_cond - ret_values[1])+ if apply_cond_cfg:+ noise_pred = noise_pred + condition_guide_scale * (noise_pred_cond - ret_values[2])+ noise_pred_cond = Noneelif multitalk and audio_proj != None:if apg_switch != 0:@@ -1264,7 +1243,7 @@ class WanAny2V:else:videos = videos[0] # return only first videoif any_vae2: videos2 = videos2[0] # return only first video- if color_correction_strength > 0 and (prefix_frames_count > 0 and window_no > 1 or prefix_frames_count > 1 and window_no == 1):+ if color_correction_strength > 0 and not ( window_no == 1 and (full_prefix_video is None or full_prefix_video.shape[1]<= 1) ):if vace and False:# videos = match_and_blend_colors_with_mask(videos.unsqueeze(0), input_frames[0].unsqueeze(0), input_masks[0][:1].unsqueeze(0), color_correction_strength,copy_mode= "progressive_blend").squeeze(0)videos = match_and_blend_colors_with_mask(videos.unsqueeze(0), input_frames[0].unsqueeze(0), input_masks[0][:1].unsqueeze(0), color_correction_strength,copy_mode= "reference").squeeze(0)diff --git a/models/wan/wan_handler.py b/models/wan/wan_handler.pyindex 10ce711..2698d06 100644--- a/models/wan/wan_handler.py+++ b/models/wan/wan_handler.py@@ -628,8 +628,8 @@ class family_handler():ref_image = convert_tensor_to_image(pre_video_guide[:, 0])frames = video_guide- mask_frames = Nonemask_frames = None if video_mask is None else video_mask+aligner = PoseAligner()outputs = aligner.align( frames, ref_image, ref_video_mask=mask_frames, align_frame=0, max_frames=None, augment=True, include_composite= True, cpu_resize_workers= max_workers, expand_scale = expand_scale )diff --git a/shared/utils/process_locks.py b/shared/utils/process_locks.pyindex 8def3eb..13d900e 100644--- a/shared/utils/process_locks.py+++ b/shared/utils/process_locks.py@@ -11,14 +11,14 @@ def get_gen_info(state):state["gen"] = cachereturn cache-def any_GPU_process_running(state, process_id):+def any_GPU_process_running(state, process_id, ignore_main = False):gen = get_gen_info(state)#"process:" + process_idwith gen_lock:process_status = gen.get("process_status", None)- return process_status is not None+ return process_status is not None and not (process_status =="process:main" and ignore_main)-def acquire_GPU_ressources(state, process_id, process_name, gr = None):+def acquire_GPU_ressources(state, process_id, process_name, gr = None, custom_pause_msg = None, custom_wait_msg = None):gen = get_gen_info(state)original_process_status = Nonewhile True:@@ -36,7 +36,8 @@ def acquire_GPU_ressources(state, process_id, process_name, gr = None):elif process_status == "process:main":original_process_status = process_statusgen["process_status"] = "request:" + process_id- gen["pause_msg"] = f"Generation Suspended while using {process_name}"++ gen["pause_msg"] = custom_pause_msg if custom_pause_msg is not None else f"Generation Suspended while using {process_name}"breakelif process_status == "process:" + process_id:break@@ -58,7 +59,10 @@ def acquire_GPU_ressources(state, process_id, process_name, gr = None):total_wait += wait_timeif round(total_wait,2) >= 5 and gr is not None and not wait_msg_displayed:wait_msg_displayed = True- gr.Info(f"Process {process_name} is Suspended while waiting that GPU Ressources become available")+ if custom_wait_msg is None:+ gr.Info(f"Process {process_name} is Suspended while waiting that GPU Ressources become available")+ else:+ gr.Info(custom_wait_msg)time.sleep(wait_time)diff --git a/wgp.py b/wgp.pyindex 717a58b..f83b74e 100644--- a/wgp.py+++ b/wgp.py@@ -44,7 +44,7 @@ from shared.utils.video_metadata import save_video_metadatafrom shared.match_archi import match_nvidia_architecturefrom shared.attention import get_attention_modes, get_supported_attention_modesfrom shared.utils.utils import truncate_for_filesystem, sanitize_file_name, process_images_multithread, get_default_workers-from shared.utils.process_locks import acquire_GPU_ressources, release_GPU_ressources, gen_lock+from shared.utils.process_locks import acquire_GPU_ressources, release_GPU_ressources, any_GPU_process_running, gen_lockfrom shared.loras_migration import migrate_loras_layoutfrom huggingface_hub import hf_hub_download, snapshot_downloadfrom shared.utils import files_locator as fl@@ -1213,6 +1213,9 @@ def load_queue_action(filepath, state, evt:gr.EventData):gen = get_gen_info(state)original_queue = gen.get("queue", [])+ if len(original_queue):+ gr.Info("Queue must be empty before loading a new queue")+ return update_queue_data(original_queue)delete_autoqueue_file = Falseif evt.target == None:@@ -1399,6 +1402,7 @@ def load_queue_action(filepath, state, evt:gr.EventData):def clear_queue_action(state):gen = get_gen_info(state)+ gen["resume"] = Truequeue = gen.get("queue", [])aborted_current = Falsecleared_pending = False@@ -3431,8 +3435,35 @@ def build_callback(state, pipe, send_cmd, status, num_inference_steps, preview_m# gen["progress_args"] = progress_argsreturn callback++def pause_generation(state):+ gen = get_gen_info(state)+ process_id = "pause"+ GPU_process_running = any_GPU_process_running(state, process_id, ignore_main= True )+ if GPU_process_running:+ gr.Info("Unable to pause, a PlugIn is using the GPU")+ yield gr.update(), gr.update()+ return+ gen["resume"] = False+ yield gr.Button(interactive= False), gr.update()+ pause_msg = "Generation on Pause, click Resume to Restart Generation"+ acquire_GPU_ressources(state, process_id , "Pause", gr= gr, custom_pause_msg= pause_msg, custom_wait_msg= "Please wait while the Pause Request is being Processed...")+ gr.Info(pause_msg)+ yield gr.Button(visible= False, interactive= True), gr.Button(visible= True)+ while not gen.get("resume", False):+ time.sleep(0.5)++ release_GPU_ressources(state, process_id )+ gen["resume"] = False+ yield gr.Button(visible= True, interactive= True), gr.Button(visible= False)++def resume_generation(state):+ gen = get_gen_info(state)+ gen["resume"] = True+def abort_generation(state):gen = get_gen_info(state)+ gen["resume"] = Trueif "in_progress" in gen: # and wan_model != None:if wan_model != None:wan_model._interrupt= True@@ -4893,7 +4924,7 @@ def custom_preprocess_video_with_mask(model_handler, base_model_type, pre_video_max_workers = get_default_workers()if not video_guide or max_frames <= 0:- return None, None+ return None, None, None, Nonevideo_guide = get_resampled_video(video_guide, start_frame, max_frames, target_fps).permute(-1, 0, 1, 2)video_guide = video_guide / 127.5 - 1.any_mask = video_mask is not None@@ -4901,8 +4932,8 @@ def custom_preprocess_video_with_mask(model_handler, base_model_type, pre_video_video_mask = get_resampled_video(video_mask, start_frame, max_frames, target_fps).permute(-1, 0, 1, 2)video_mask = video_mask[:1] / 255.- if len(video_guide) == 0 or any_mask and len(video_mask) == 0:- return None, None+ if video_guide.shape[1] == 0 or any_mask and video_mask.shape[1] == 0:+ return None, None, None, Nonevideo_guide_processed, video_guide_processed2, video_mask_processed, video_mask_processed2 = model_handler.custom_preprocess(base_model_type = base_model_type, pre_video_guide = pre_video_guide, video_guide = video_guide, video_mask = video_mask, height = height, width = width, fit_canvas = fit_canvas , fit_crop = fit_crop, target_fps = target_fps, block_size = block_size, max_workers = max_workers, expand_scale = expand_scale)@@ -5606,8 +5637,11 @@ def generate_video(if src_video is None or window_no >1 and src_video.shape[1] <= sliding_window_overlap and not dont_cat_preguide:abort = Truebreak- if model_def.get("control_video_trim", False) and src_video is not None:- if src_video.shape[1] < current_video_length:+ if model_def.get("control_video_trim", False) :+ if src_video is None:+ abort = True+ break+ elif src_video.shape[1] < current_video_length:current_video_length = src_video.shape[1]abort_scheduled = Trueif src_faces is not None:@@ -5759,6 +5793,7 @@ def generate_video(exaggeration=exaggeration,pace=pace,temperature=temperature,+ full_prefix_video = prefix_video,)except Exception as e:if len(control_audio_tracks) > 0 or len(source_audio_tracks) > 0:@@ -9249,9 +9284,11 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonpreview_trigger = gr.Text(visible= False)gen_info = gr.HTML(visible=False, min_height=1)with gr.Row() as current_gen_buttons_row:- onemoresample_btn = gr.Button("One More Sample Please !", visible = True)- onemorewindow_btn = gr.Button("Extend this Sample Please !", visible = False)- abort_btn = gr.Button("Abort", visible = True)+ onemoresample_btn = gr.Button("One More Sample", visible = True, size='md', min_width=1)+ onemorewindow_btn = gr.Button("Extend this Sample", visible = False, size='md', min_width=1)+ pause_btn = gr.Button("Pause", visible = True, size='md', min_width=1)+ resume_btn = gr.Button("Resume", visible = False, size='md', min_width=1)+ abort_btn = gr.Button("Abort", visible = True, size='md', min_width=1)with gr.Accordion("Queue Management", open=False) as queue_accordion:with gr.Row():queue_html = gr.HTML(@@ -9380,6 +9417,8 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonoutputs=[modal_container],show_progress="hidden")+ pause_btn.click(pause_generation, [state], [ pause_btn, resume_btn] )+ resume_btn.click(resume_generation, [state] )abort_btn.click(abort_generation, [state], [ abort_btn] ) #.then(refresh_gallery, inputs = [state, gen_info], outputs = [output, gen_info, queue_html] )onemoresample_btn.click(fn=one_more_sample,inputs=[state], outputs= [state])onemorewindow_btn.click(fn=one_more_window,inputs=[state], outputs= [state])