Spaces:
Paused
Paused
Dynamic Axis (#10)
Browse files- update optimization (c6f4804f72f629dbcca28ddb6db3e68e38a23468)
- Update app.py (e477e53a6b074c52b0e7a10043697f6c1e488b44)
Files changed (2)
- app.py+53-25
- optimization.py+21-45
18 unmodified lines192021222324252623 unmodified lines50515253545556575859601 unmodified line6263646566676869707172737475767778798081828384858687888957 unmodified lines14714814915015115215331 unmodified lines18518618718818919019118 unmodified linesMODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"LANDSCAPE_WIDTH = 832LANDSCAPE_HEIGHT = 480MAX_SEED = np.iinfo(np.int32).maxFIXED_FPS = 1623 unmodified linestorch.cuda.synchronize()torch.cuda.empty_cache()optimize_pipeline_(pipe,image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),prompt='prompt',height=LANDSCAPE_HEIGHT,width=LANDSCAPE_WIDTH,num_frames=MAX_FRAMES_MODEL,)1 unmodified linedefault_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"def resize_image(image: Image.Image) -> Image.Image:if image.height > image.width:transposed = image.transpose(Image.Transpose.ROTATE_90)resized = resize_image_landscape(transposed)return resized.transpose(Image.Transpose.ROTATE_270)return resize_image_landscape(image)def resize_image_landscape(image: Image.Image) -> Image.Image:target_aspect = LANDSCAPE_WIDTH / LANDSCAPE_HEIGHTwidth, height = image.sizein_aspect = width / heightif in_aspect > target_aspect:new_width = round(height * target_aspect)left = (width - new_width) // 2image = image.crop((left, 0, left + new_width, height))else:new_height = round(width / target_aspect)top = (height - new_height) // 2image = image.crop((0, top, width, top + new_height))return image.resize((LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT), Image.LANCZOS)def get_duration(input_image,57 unmodified linesgr.Error: If input_image is None (no image uploaded).Note:- The function automatically resizes the input image to the target dimensions- Frame count is calculated as duration_seconds * FIXED_FPS (24)- Output dimensions are adjusted to be multiples of MOD_VALUE (32)- The function uses GPU acceleration via the @spaces.GPU decorator31 unmodified linesgr.Markdown("run Wan 2.2 in just 4-8 steps, with [Lightning LoRA](https://huggingface.co/Kijai/WanVideo_comfy/tree/main/Wan22-Lightning), fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU⚡️")with gr.Row():with gr.Column():input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")18 unmodified lines192021222324252627282923 unmodified lines53545556575859606162636465661 unmodified line686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711857 unmodified lines17617717817918018131 unmodified lines21321421521621721821918 unmodified linesMODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"MAX_DIM = 832MIN_DIM = 480SQUARE_DIM = 640MULTIPLE_OF = 16MAX_SEED = np.iinfo(np.int32).maxFIXED_FPS = 1623 unmodified linestorch.cuda.synchronize()torch.cuda.empty_cache()OPTIMIZE_WIDTH = 832OPTIMIZE_HEIGHT = 624optimize_pipeline_(pipe,image=Image.new('RGB', (OPTIMIZE_WIDTH, OPTIMIZE_HEIGHT)),prompt='prompt',height=OPTIMIZE_HEIGHT,width=OPTIMIZE_WIDTH,num_frames=MAX_FRAMES_MODEL,)1 unmodified linedefault_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"def resize_image(image: Image.Image) -> Image.Image:"""Resizes an image to fit within the model's constraints, preserving aspect ratio as much as possible."""width, height = image.size# Handle square caseif width == height:return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)aspect_ratio = width / heightMAX_ASPECT_RATIO = MAX_DIM / MIN_DIMMIN_ASPECT_RATIO = MIN_DIM / MAX_DIMimage_to_resize = imageif aspect_ratio > MAX_ASPECT_RATIO:# Very wide image -> crop width to fit 832x480 aspect ratiotarget_w, target_h = MAX_DIM, MIN_DIMcrop_width = int(round(height * MAX_ASPECT_RATIO))left = (width - crop_width) // 2image_to_resize = image.crop((left, 0, left + crop_width, height))elif aspect_ratio < MIN_ASPECT_RATIO:# Very tall image -> crop height to fit 480x832 aspect ratiotarget_w, target_h = MIN_DIM, MAX_DIMcrop_height = int(round(width / MIN_ASPECT_RATIO))top = (height - crop_height) // 2image_to_resize = image.crop((0, top, width, top + crop_height))else:if width > height: # Landscapetarget_w = MAX_DIMtarget_h = int(round(target_w / aspect_ratio))else: # Portraittarget_h = MAX_DIMtarget_w = int(round(target_h * aspect_ratio))final_w = round(target_w / MULTIPLE_OF) * MULTIPLE_OFfinal_h = round(target_h / MULTIPLE_OF) * MULTIPLE_OFfinal_w = max(MIN_DIM, min(MAX_DIM, final_w))final_h = max(MIN_DIM, min(MAX_DIM, final_h))return image_to_resize.resize((final_w, final_h), Image.LANCZOS)def get_duration(input_image,57 unmodified linesgr.Error: If input_image is None (no image uploaded).Note:- Frame count is calculated as duration_seconds * FIXED_FPS (24)- Output dimensions are adjusted to be multiples of MOD_VALUE (32)- The function uses GPU acceleration via the @spaces.GPU decorator31 unmodified linesgr.Markdown("run Wan 2.2 in just 4-8 steps, with [Lightning LoRA](https://huggingface.co/Kijai/WanVideo_comfy/tree/main/Wan22-Lightning), fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU⚡️")with gr.Row():with gr.Column():input_image_component = gr.Image(type="pil", label="Input Image")prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
13 unmodified lines14151617181920212223242526272829303112 unmodified lines44454647484920 unmodified lines70717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013 unmodified linesfrom optimization_utils import capture_component_callfrom optimization_utils import aoti_compilefrom optimization_utils import ZeroGPUCompiledModelfrom optimization_utils import drain_module_parametersP = ParamSpec('P')TRANSFORMER_NUM_FRAMES_DIM = torch.export.Dim('num_frames', min=3, max=21)TRANSFORMER_DYNAMIC_SHAPES = {'hidden_states': {2: TRANSFORMER_NUM_FRAMES_DIM,},}12 unmodified lines@spaces.GPU(duration=1500)def compile_transformer():pipeline.load_lora_weights("Kijai/WanVideo_comfy",weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",20 unmodified linesquantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())hidden_states: torch.Tensor = call.kwargs['hidden_states']hidden_states_transposed = hidden_states.transpose(-1, -2).contiguous()if hidden_states.shape[-1] > hidden_states.shape[-2]:hidden_states_landscape = hidden_stateshidden_states_portrait = hidden_states_transposedelse:hidden_states_landscape = hidden_states_transposedhidden_states_portrait = hidden_statesexported_landscape_1 = torch.export.export(mod=pipeline.transformer,args=call.args,kwargs=call.kwargs | {'hidden_states': hidden_states_landscape},dynamic_shapes=dynamic_shapes,)exported_portrait_2 = torch.export.export(mod=pipeline.transformer_2,args=call.args,kwargs=call.kwargs | {'hidden_states': hidden_states_portrait},dynamic_shapes=dynamic_shapes,)compiled_landscape_1 = aoti_compile(exported_landscape_1, INDUCTOR_CONFIGS)compiled_portrait_2 = aoti_compile(exported_portrait_2, INDUCTOR_CONFIGS)compiled_landscape_2 = ZeroGPUCompiledModel(compiled_landscape_1.archive_file, compiled_portrait_2.weights)compiled_portrait_1 = ZeroGPUCompiledModel(compiled_portrait_2.archive_file, compiled_landscape_1.weights)return (compiled_landscape_1,compiled_landscape_2,compiled_portrait_1,compiled_portrait_2,)quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())cl1, cl2, cp1, cp2 = compile_transformer()def combined_transformer_1(*args, **kwargs):hidden_states: torch.Tensor = kwargs['hidden_states']if hidden_states.shape[-1] > hidden_states.shape[-2]:return cl1(*args, **kwargs)else:return cp1(*args, **kwargs)def combined_transformer_2(*args, **kwargs):hidden_states: torch.Tensor = kwargs['hidden_states']if hidden_states.shape[-1] > hidden_states.shape[-2]:return cl2(*args, **kwargs)else:return cp2(*args, **kwargs)pipeline.transformer.forward = combined_transformer_1drain_module_parameters(pipeline.transformer)pipeline.transformer_2.forward = combined_transformer_2drain_module_parameters(pipeline.transformer_2)13 unmodified lines14151617181920212223242526272829303132333412 unmodified lines4748495051525320 unmodified lines747576777879808182838485868788899091929394959697989910010110210310410510613 unmodified linesfrom optimization_utils import capture_component_callfrom optimization_utils import aoti_compilefrom optimization_utils import drain_module_parametersP = ParamSpec('P')LATENT_FRAMES_DIM = torch.export.Dim('num_latent_frames', min=8, max=81)LATENT_PATCHED_HEIGHT_DIM = torch.export.Dim('latent_patched_height', min=30, max=52)LATENT_PATCHED_WIDTH_DIM = torch.export.Dim('latent_patched_width', min=30, max=52)TRANSFORMER_DYNAMIC_SHAPES = {'hidden_states': {2: LATENT_FRAMES_DIM,3: 2 * LATENT_PATCHED_HEIGHT_DIM,4: 2 * LATENT_PATCHED_WIDTH_DIM,},}12 unmodified lines@spaces.GPU(duration=1500)def compile_transformer():# This LoRA fusion part remains the samepipeline.load_lora_weights("Kijai/WanVideo_comfy",weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",20 unmodified linesquantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())exported_1 = torch.export.export(mod=pipeline.transformer,args=call.args,kwargs=call.kwargs,dynamic_shapes=dynamic_shapes,)exported_2 = torch.export.export(mod=pipeline.transformer_2,args=call.args,kwargs=call.kwargs,dynamic_shapes=dynamic_shapes,)compiled_1 = aoti_compile(exported_1, INDUCTOR_CONFIGS)compiled_2 = aoti_compile(exported_2, INDUCTOR_CONFIGS)return compiled_1, compiled_2quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())compiled_transformer_1, compiled_transformer_2 = compile_transformer()pipeline.transformer.forward = compiled_transformer_1drain_module_parameters(pipeline.transformer)pipeline.transformer_2.forward = compiled_transformer_2drain_module_parameters(pipeline.transformer_2)
app.py+53-25
optimization.py+21-45