no@email.com commited on
Commit
095fbbe
·
1 Parent(s): 3519b71

try setting to use sage attention

Browse files
Files changed (3) hide show
  1. app.py +5 -2
  2. modify_model/modify_wan.py +108 -0
  3. requirements.txt +2 -1
app.py CHANGED
@@ -14,7 +14,8 @@ from datetime import datetime
14
  from huggingface_hub import CommitOperationAdd, HfApi
15
  from uuid import uuid4
16
  from diffusers import UniPCMultistepScheduler
17
-
 
18
  from torchao.quantization import quantize_
19
  from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
20
  from torchao.quantization import Int8WeightOnlyConfig
@@ -58,7 +59,9 @@ pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
58
  ),
59
  torch_dtype=torch.bfloat16,
60
  ).to('cuda')
61
-
 
 
62
  pipe.load_lora_weights(
63
  "obsxrver/Wan2.2-I2Pee-5XL",
64
  weight_name="WAN2.2-I2V_HighNoise_I2Pee-5.1XL.safetensors",
 
14
  from huggingface_hub import CommitOperationAdd, HfApi
15
  from uuid import uuid4
16
  from diffusers import UniPCMultistepScheduler
17
+ from modify_model.modify_wan import set_sage_attn_wan
18
+ from sageattention import sageattn
19
  from torchao.quantization import quantize_
20
  from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
21
  from torchao.quantization import Int8WeightOnlyConfig
 
59
  ),
60
  torch_dtype=torch.bfloat16,
61
  ).to('cuda')
62
+ # Use sage attention for speed
63
+ set_sage_attn_wan(pipe.transformer,sageattn)
64
+ set_sage_attn_wan(pipe.transformer_2,sageattn)
65
  pipe.load_lora_weights(
66
  "obsxrver/Wan2.2-I2Pee-5XL",
67
  weight_name="WAN2.2-I2V_HighNoise_I2Pee-5.1XL.safetensors",
modify_model/modify_wan.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ from typing import Optional, Tuple
4
+ from diffusers.models import WanTransformer3DModel
5
+ from diffusers.models.transformers.transformer_wan import WanAttention, _get_qkv_projections, _get_added_kv_projections
6
+
7
+
8
+ class SageWanAttnProcessor:
9
+ def __init__(self, attn_func):
10
+ self.attn_func = attn_func
11
+ if not hasattr(F, "scaled_dot_product_attention"):
12
+ raise ImportError(
13
+ "WanAttnProcessor requires PyTorch 2.0. To use it, please upgrade PyTorch to version 2.0 or higher."
14
+ )
15
+
16
+ def __call__(
17
+ self,
18
+ attn: "WanAttention",
19
+ hidden_states: torch.Tensor,
20
+ encoder_hidden_states: Optional[torch.Tensor] = None,
21
+ attention_mask: Optional[torch.Tensor] = None,
22
+ rotary_emb: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
23
+ ) -> torch.Tensor:
24
+ encoder_hidden_states_img = None
25
+ if attn.add_k_proj is not None:
26
+ # 512 is the context length of the text encoder, hardcoded for now
27
+ image_context_length = encoder_hidden_states.shape[1] - 512
28
+ encoder_hidden_states_img = encoder_hidden_states[:, :image_context_length]
29
+ encoder_hidden_states = encoder_hidden_states[:, image_context_length:]
30
+
31
+ query, key, value = _get_qkv_projections(attn, hidden_states, encoder_hidden_states)
32
+
33
+ query = attn.norm_q(query)
34
+ key = attn.norm_k(key)
35
+
36
+ query = query.unflatten(2, (attn.heads, -1))
37
+ key = key.unflatten(2, (attn.heads, -1))
38
+ value = value.unflatten(2, (attn.heads, -1))
39
+
40
+ if rotary_emb is not None:
41
+
42
+ def apply_rotary_emb(
43
+ hidden_states: torch.Tensor,
44
+ freqs_cos: torch.Tensor,
45
+ freqs_sin: torch.Tensor,
46
+ ):
47
+ x1, x2 = hidden_states.unflatten(-1, (-1, 2)).unbind(-1)
48
+ cos = freqs_cos[..., 0::2]
49
+ sin = freqs_sin[..., 1::2]
50
+ out = torch.empty_like(hidden_states)
51
+ out[..., 0::2] = x1 * cos - x2 * sin
52
+ out[..., 1::2] = x1 * sin + x2 * cos
53
+ return out.type_as(hidden_states)
54
+
55
+ query = apply_rotary_emb(query, *rotary_emb)
56
+ key = apply_rotary_emb(key, *rotary_emb)
57
+
58
+ # ---- transpose to (B, H, N, D) for sageattn/sdpa ----
59
+ query = query.transpose(1, 2)
60
+ key = key.transpose(1, 2)
61
+ value = value.transpose(1, 2)
62
+
63
+ # I2V task
64
+ hidden_states_img = None
65
+ if encoder_hidden_states_img is not None:
66
+ key_img, value_img = _get_added_kv_projections(attn, encoder_hidden_states_img)
67
+ key_img = attn.norm_added_k(key_img)
68
+
69
+ key_img = key_img.unflatten(2, (attn.heads, -1)).transpose(1, 2)
70
+ value_img = value_img.unflatten(2, (attn.heads, -1)).transpose(1, 2)
71
+
72
+ hidden_states_img = self.attn_func(
73
+ query,
74
+ key_img,
75
+ value_img,
76
+ attn_mask=None,
77
+ dropout_p=0.0,
78
+ is_causal=False,
79
+ )
80
+ hidden_states_img = hidden_states_img.transpose(1, 2).flatten(2, 3)
81
+ hidden_states_img = hidden_states_img.type_as(query)
82
+
83
+ hidden_states = self.attn_func(
84
+ query,
85
+ key,
86
+ value,
87
+ attn_mask=attention_mask,
88
+ dropout_p=0.0,
89
+ is_causal=False,
90
+ )
91
+ hidden_states = hidden_states.transpose(1, 2).flatten(2, 3)
92
+ hidden_states = hidden_states.type_as(query)
93
+
94
+ if hidden_states_img is not None:
95
+ hidden_states = hidden_states + hidden_states_img
96
+
97
+ hidden_states = attn.to_out[0](hidden_states)
98
+ hidden_states = attn.to_out[1](hidden_states)
99
+ return hidden_states
100
+
101
+
102
+ def set_sage_attn_wan(
103
+ model: WanTransformer3DModel,
104
+ attn_func,
105
+ ):
106
+ for idx, block in enumerate(model.blocks):
107
+ processor = SageWanAttnProcessor(attn_func)
108
+ block.attn1.processor = processor
requirements.txt CHANGED
@@ -9,4 +9,5 @@ imageio-ffmpeg
9
  imageio
10
  opencv-python
11
  torchao==0.11.0
12
- huggingface-hub
 
 
9
  imageio
10
  opencv-python
11
  torchao==0.11.0
12
+ huggingface-hub
13
+ sageattention