Spaces:
Running
on
Zero
Running
on
Zero
Update optimization.py
Browse files- optimization.py +122 -51
optimization.py
CHANGED
|
@@ -40,57 +40,128 @@ INDUCTOR_CONFIGS = {
|
|
| 40 |
|
| 41 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"Kijai/WanVideo_comfy",
|
| 48 |
-
weight_name="Lightx2v/lightx2v_T2V_14B_cfg_step_distill_v2_lora_rank128_bf16.safetensors",
|
| 49 |
-
adapter_name="lightning"
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
weight_name="
|
| 57 |
-
adapter_name="lightning_2",
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
|
| 95 |
cl1, cl2, cp1, cp2 = compile_transformer()
|
| 96 |
|
|
|
|
| 40 |
|
| 41 |
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 42 |
|
| 43 |
+
@spaces.GPU(duration=1500)
|
| 44 |
+
def compile_transformer():
|
| 45 |
+
|
| 46 |
+
# pipeline.load_lora_weights(
|
| 47 |
+
# "Kijai/WanVideo_comfy",
|
| 48 |
+
# weight_name="Lightx2v/lightx2v_T2V_14B_cfg_step_distill_v2_lora_rank128_bf16.safetensors",
|
| 49 |
+
# adapter_name="lightning"
|
| 50 |
+
# )
|
| 51 |
+
# kwargs_lora = {}
|
| 52 |
+
# kwargs_lora["load_into_transformer_2"] = True
|
| 53 |
+
# pipeline.load_lora_weights(
|
| 54 |
+
# "Kijai/WanVideo_comfy",
|
| 55 |
+
# weight_name="Lightx2v/lightx2v_T2V_14B_cfg_step_distill_v2_lora_rank128_bf16.safetensors",
|
| 56 |
+
# #weight_name="Wan22-Lightning/Wan2.2-Lightning_T2V-A14B-4steps-lora_LOW_fp16.safetensors",
|
| 57 |
+
# adapter_name="lightning_2", **kwargs_lora
|
| 58 |
+
# )
|
| 59 |
+
# pipeline.set_adapters(["lightning", "lightning_2"], adapter_weights=[1., 1.])
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# pipeline.fuse_lora(adapter_names=["lightning"], lora_scale=3., components=["transformer"])
|
| 63 |
+
# pipeline.fuse_lora(adapter_names=["lightning_2"], lora_scale=1., components=["transformer_2"])
|
| 64 |
+
# pipeline.unload_lora_weights()
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
pipeline.load_lora_weights(
|
| 69 |
+
"Kijai/WanVideo_comfy",
|
| 70 |
+
weight_name="Lightx2v/lightx2v_T2V_14B_cfg_step_distill_v2_lora_rank128_bf16.safetensors",
|
| 71 |
+
adapter_name="lightning"
|
| 72 |
+
)
|
| 73 |
+
kwargs_lora = {}
|
| 74 |
+
kwargs_lora["load_into_transformer_2"] = True
|
| 75 |
+
# pipeline.load_lora_weights(
|
| 76 |
+
# #"drozbay/Wan2.2_A14B_lora_extract",
|
| 77 |
+
# "Kijai/WanVideo_comfy",
|
| 78 |
+
# #weight_name="MTVCrafter/Wan2_1_MTV-Crafter_motion_adapter_bf16.safetensors",
|
| 79 |
+
# #weight_name="Skyreels/Wan2_1_Skyreels-v2-T2V-720P_LoRA_rank_64_fp16.safetensors",
|
| 80 |
+
# #weight_name="Pusa/Wan21_PusaV1_LoRA_14B_rank512_bf16.safetensors",
|
| 81 |
+
# weight_name="Wan22-Lightning/Wan2.2-Lightning_T2V-A14B-4steps-lora_LOW_fp16.safetensors",
|
| 82 |
+
# adapter_name="lightning_2", **kwargs_lora
|
| 83 |
+
# )
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
pipeline.load_lora_weights(
|
| 88 |
+
#"drozbay/Wan2.2_A14B_lora_extract",
|
| 89 |
+
"deadman44/Wan2.2_T2i_T2v_LoRA",
|
| 90 |
+
#weight_name="MTVCrafter/Wan2_1_MTV-Crafter_motion_adapter_bf16.safetensors",
|
| 91 |
+
#weight_name="Skyreels/Wan2_1_Skyreels-v2-T2V-720P_LoRA_rank_64_fp16.safetensors",
|
| 92 |
+
weight_name="lora_wan2.2_myjd_Low_v01.safetensors",
|
| 93 |
+
#weight_name="Wan2.2-T2V-A14B-4steps-lora-rank64-Seko-V1.1/low_noise_model.safetensors",
|
| 94 |
+
adapter_name="lightning_2", **kwargs_lora
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# pipeline.load_lora_weights(
|
| 98 |
+
# #"drozbay/Wan2.2_A14B_lora_extract",
|
| 99 |
+
# "ostris/wan22_i2v_14b_orbit_shot_lora",
|
| 100 |
+
# #weight_name="MTVCrafter/Wan2_1_MTV-Crafter_motion_adapter_bf16.safetensors",
|
| 101 |
+
# #weight_name="Skyreels/Wan2_1_Skyreels-v2-T2V-720P_LoRA_rank_64_fp16.safetensors",
|
| 102 |
+
# weight_name="wan22_14b_i2v_orbit_low_noise.safetensors",
|
| 103 |
+
# #weight_name="Wan2.2-T2V-A14B-4steps-lora-rank64-Seko-V1.1/low_noise_model.safetensors",
|
| 104 |
+
# adapter_name="lightning_200", **kwargs_lora
|
| 105 |
+
# )
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
pipeline.set_adapters(["lightning", "lightning_2"], adapter_weights=[1., 1.])
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
pipeline.fuse_lora(adapter_names=["lightning"], lora_scale=3., components=["transformer"])
|
| 116 |
+
pipeline.fuse_lora(adapter_names=["lightning_2"], lora_scale=1., components=["transformer_2"])
|
| 117 |
+
|
| 118 |
+
pipeline.unload_lora_weights()
|
| 119 |
+
|
| 120 |
+
with capture_component_call(pipeline, 'transformer') as call:
|
| 121 |
+
pipeline(*args, **kwargs)
|
| 122 |
+
|
| 123 |
+
dynamic_shapes = tree_map_only((torch.Tensor, bool), lambda t: None, call.kwargs)
|
| 124 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 125 |
+
|
| 126 |
+
quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 127 |
+
quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())
|
| 128 |
+
|
| 129 |
+
hidden_states: torch.Tensor = call.kwargs['hidden_states']
|
| 130 |
+
hidden_states_transposed = hidden_states.transpose(-1, -2).contiguous()
|
| 131 |
+
if hidden_states.shape[-1] > hidden_states.shape[-2]:
|
| 132 |
+
hidden_states_landscape = hidden_states
|
| 133 |
+
hidden_states_portrait = hidden_states_transposed
|
| 134 |
+
else:
|
| 135 |
+
hidden_states_landscape = hidden_states_transposed
|
| 136 |
+
hidden_states_portrait = hidden_states
|
| 137 |
+
|
| 138 |
+
exported_landscape_1 = torch.export.export(
|
| 139 |
+
mod=pipeline.transformer,
|
| 140 |
+
args=call.args,
|
| 141 |
+
kwargs=call.kwargs | {'hidden_states': hidden_states_landscape},
|
| 142 |
+
dynamic_shapes=dynamic_shapes,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
exported_portrait_2 = torch.export.export(
|
| 146 |
+
mod=pipeline.transformer_2,
|
| 147 |
+
args=call.args,
|
| 148 |
+
kwargs=call.kwargs | {'hidden_states': hidden_states_portrait},
|
| 149 |
+
dynamic_shapes=dynamic_shapes,
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
compiled_landscape_1 = aoti_compile(exported_landscape_1, INDUCTOR_CONFIGS)
|
| 153 |
+
compiled_portrait_2 = aoti_compile(exported_portrait_2, INDUCTOR_CONFIGS)
|
| 154 |
+
|
| 155 |
+
compiled_landscape_2 = ZeroGPUCompiledModel(compiled_landscape_1.archive_file, compiled_portrait_2.weights)
|
| 156 |
+
compiled_portrait_1 = ZeroGPUCompiledModel(compiled_portrait_2.archive_file, compiled_landscape_1.weights)
|
| 157 |
+
|
| 158 |
+
return (
|
| 159 |
+
compiled_landscape_1,
|
| 160 |
+
compiled_landscape_2,
|
| 161 |
+
compiled_portrait_1,
|
| 162 |
+
compiled_portrait_2,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
|
| 166 |
cl1, cl2, cp1, cp2 = compile_transformer()
|
| 167 |
|