multimodalart HF Staff commited on
Commit
c6f4804
·
verified ·
1 Parent(s): fe3c191

update optimization

Browse files
Files changed (1) hide show
  1. optimization.py +21 -45
optimization.py CHANGED
@@ -14,18 +14,21 @@ from torchao.quantization import Int8WeightOnlyConfig
14
 
15
  from optimization_utils import capture_component_call
16
  from optimization_utils import aoti_compile
17
- from optimization_utils import ZeroGPUCompiledModel
18
  from optimization_utils import drain_module_parameters
19
 
20
 
21
  P = ParamSpec('P')
22
 
 
23
 
24
- TRANSFORMER_NUM_FRAMES_DIM = torch.export.Dim('num_frames', min=3, max=21)
 
25
 
26
  TRANSFORMER_DYNAMIC_SHAPES = {
27
  'hidden_states': {
28
- 2: TRANSFORMER_NUM_FRAMES_DIM,
 
 
29
  },
30
  }
31
 
@@ -44,6 +47,7 @@ def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kw
44
  @spaces.GPU(duration=1500)
45
  def compile_transformer():
46
 
 
47
  pipeline.load_lora_weights(
48
  "Kijai/WanVideo_comfy",
49
  weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
@@ -70,61 +74,33 @@ def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kw
70
  quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
71
  quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())
72
 
73
- hidden_states: torch.Tensor = call.kwargs['hidden_states']
74
- hidden_states_transposed = hidden_states.transpose(-1, -2).contiguous()
75
- if hidden_states.shape[-1] > hidden_states.shape[-2]:
76
- hidden_states_landscape = hidden_states
77
- hidden_states_portrait = hidden_states_transposed
78
- else:
79
- hidden_states_landscape = hidden_states_transposed
80
- hidden_states_portrait = hidden_states
81
-
82
- exported_landscape_1 = torch.export.export(
83
  mod=pipeline.transformer,
84
  args=call.args,
85
- kwargs=call.kwargs | {'hidden_states': hidden_states_landscape},
86
  dynamic_shapes=dynamic_shapes,
87
  )
88
 
89
- exported_portrait_2 = torch.export.export(
90
  mod=pipeline.transformer_2,
91
  args=call.args,
92
- kwargs=call.kwargs | {'hidden_states': hidden_states_portrait},
93
  dynamic_shapes=dynamic_shapes,
94
  )
95
 
96
- compiled_landscape_1 = aoti_compile(exported_landscape_1, INDUCTOR_CONFIGS)
97
- compiled_portrait_2 = aoti_compile(exported_portrait_2, INDUCTOR_CONFIGS)
 
 
98
 
99
- compiled_landscape_2 = ZeroGPUCompiledModel(compiled_landscape_1.archive_file, compiled_portrait_2.weights)
100
- compiled_portrait_1 = ZeroGPUCompiledModel(compiled_portrait_2.archive_file, compiled_landscape_1.weights)
101
-
102
- return (
103
- compiled_landscape_1,
104
- compiled_landscape_2,
105
- compiled_portrait_1,
106
- compiled_portrait_2,
107
- )
108
 
109
  quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
110
- cl1, cl2, cp1, cp2 = compile_transformer()
111
-
112
- def combined_transformer_1(*args, **kwargs):
113
- hidden_states: torch.Tensor = kwargs['hidden_states']
114
- if hidden_states.shape[-1] > hidden_states.shape[-2]:
115
- return cl1(*args, **kwargs)
116
- else:
117
- return cp1(*args, **kwargs)
118
-
119
- def combined_transformer_2(*args, **kwargs):
120
- hidden_states: torch.Tensor = kwargs['hidden_states']
121
- if hidden_states.shape[-1] > hidden_states.shape[-2]:
122
- return cl2(*args, **kwargs)
123
- else:
124
- return cp2(*args, **kwargs)
125
-
126
- pipeline.transformer.forward = combined_transformer_1
127
  drain_module_parameters(pipeline.transformer)
128
 
129
- pipeline.transformer_2.forward = combined_transformer_2
130
  drain_module_parameters(pipeline.transformer_2)
 
14
 
15
  from optimization_utils import capture_component_call
16
  from optimization_utils import aoti_compile
 
17
  from optimization_utils import drain_module_parameters
18
 
19
 
20
  P = ParamSpec('P')
21
 
22
+ LATENT_FRAMES_DIM = torch.export.Dim('num_latent_frames', min=8, max=81)
23
 
24
+ LATENT_PATCHED_HEIGHT_DIM = torch.export.Dim('latent_patched_height', min=30, max=52)
25
+ LATENT_PATCHED_WIDTH_DIM = torch.export.Dim('latent_patched_width', min=30, max=52)
26
 
27
  TRANSFORMER_DYNAMIC_SHAPES = {
28
  'hidden_states': {
29
+ 2: LATENT_FRAMES_DIM,
30
+ 3: 2 * LATENT_PATCHED_HEIGHT_DIM,
31
+ 4: 2 * LATENT_PATCHED_WIDTH_DIM,
32
  },
33
  }
34
 
 
47
  @spaces.GPU(duration=1500)
48
  def compile_transformer():
49
 
50
+ # This LoRA fusion part remains the same
51
  pipeline.load_lora_weights(
52
  "Kijai/WanVideo_comfy",
53
  weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
 
74
  quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
75
  quantize_(pipeline.transformer_2, Float8DynamicActivationFloat8WeightConfig())
76
 
77
+
78
+ exported_1 = torch.export.export(
 
 
 
 
 
 
 
 
79
  mod=pipeline.transformer,
80
  args=call.args,
81
+ kwargs=call.kwargs,
82
  dynamic_shapes=dynamic_shapes,
83
  )
84
 
85
+ exported_2 = torch.export.export(
86
  mod=pipeline.transformer_2,
87
  args=call.args,
88
+ kwargs=call.kwargs,
89
  dynamic_shapes=dynamic_shapes,
90
  )
91
 
92
+ compiled_1 = aoti_compile(exported_1, INDUCTOR_CONFIGS)
93
+ compiled_2 = aoti_compile(exported_2, INDUCTOR_CONFIGS)
94
+
95
+ return compiled_1, compiled_2
96
 
 
 
 
 
 
 
 
 
 
97
 
98
  quantize_(pipeline.text_encoder, Int8WeightOnlyConfig())
99
+
100
+ compiled_transformer_1, compiled_transformer_2 = compile_transformer()
101
+
102
+ pipeline.transformer.forward = compiled_transformer_1
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  drain_module_parameters(pipeline.transformer)
104
 
105
+ pipeline.transformer_2.forward = compiled_transformer_2
106
  drain_module_parameters(pipeline.transformer_2)