[Diffusion] Avoid cpu2gpu sync in flashinfer rope and apply flashinfer rope to wanvideo (#16668)
Co-authored-by: Mick <mickjagger19@icloud.com>
This commit is contained in:
@@ -94,7 +94,7 @@ def apply_flashinfer_rope_qk_inplace(
|
|||||||
return q_rot.view(bsz, seqlen, nheads, d), k_rot.view(bsz, seqlen, nheads, d)
|
return q_rot.view(bsz, seqlen, nheads, d), k_rot.view(bsz, seqlen, nheads, d)
|
||||||
|
|
||||||
if positions is None:
|
if positions is None:
|
||||||
pos_1d = torch.arange(seqlen, device="cpu", dtype=torch.long)
|
pos_1d = torch.arange(seqlen, device=q.device, dtype=torch.long)
|
||||||
positions = pos_1d if bsz == 1 else pos_1d.repeat(bsz)
|
positions = pos_1d if bsz == 1 else pos_1d.repeat(bsz)
|
||||||
else:
|
else:
|
||||||
if not (
|
if not (
|
||||||
@@ -108,8 +108,6 @@ def apply_flashinfer_rope_qk_inplace(
|
|||||||
f"positions length must be bsz*seqlen={bsz*seqlen}, got {positions.numel()}"
|
f"positions length must be bsz*seqlen={bsz*seqlen}, got {positions.numel()}"
|
||||||
)
|
)
|
||||||
|
|
||||||
positions = positions.to(q.device, non_blocking=True)
|
|
||||||
|
|
||||||
q_flat = q.reshape(bsz * seqlen, nheads * d).contiguous()
|
q_flat = q.reshape(bsz * seqlen, nheads * d).contiguous()
|
||||||
k_flat = k.reshape(bsz * seqlen, nheads * d).contiguous()
|
k_flat = k.reshape(bsz * seqlen, nheads * d).contiguous()
|
||||||
apply_rope_with_cos_sin_cache_inplace(
|
apply_rope_with_cos_sin_cache_inplace(
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ from sglang.multimodal_gen.runtime.layers.mlp import MLP
|
|||||||
from sglang.multimodal_gen.runtime.layers.rotary_embedding import (
|
from sglang.multimodal_gen.runtime.layers.rotary_embedding import (
|
||||||
NDRotaryEmbedding,
|
NDRotaryEmbedding,
|
||||||
_apply_rotary_emb,
|
_apply_rotary_emb,
|
||||||
|
apply_flashinfer_rope_qk_inplace,
|
||||||
)
|
)
|
||||||
from sglang.multimodal_gen.runtime.layers.visual_embedding import (
|
from sglang.multimodal_gen.runtime.layers.visual_embedding import (
|
||||||
ModulateProjection,
|
ModulateProjection,
|
||||||
@@ -399,9 +400,21 @@ class WanTransformerBlock(nn.Module):
|
|||||||
|
|
||||||
# Apply rotary embeddings
|
# Apply rotary embeddings
|
||||||
cos, sin = freqs_cis
|
cos, sin = freqs_cis
|
||||||
query, key = _apply_rotary_emb(
|
if query.is_cuda and query.shape == key.shape:
|
||||||
query, cos, sin, is_neox_style=False
|
cos_sin_cache = torch.cat(
|
||||||
), _apply_rotary_emb(key, cos, sin, is_neox_style=False)
|
[
|
||||||
|
cos.to(dtype=torch.float32).contiguous(),
|
||||||
|
sin.to(dtype=torch.float32).contiguous(),
|
||||||
|
],
|
||||||
|
dim=-1,
|
||||||
|
)
|
||||||
|
query, key = apply_flashinfer_rope_qk_inplace(
|
||||||
|
query, key, cos_sin_cache, is_neox=False
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query, key = _apply_rotary_emb(
|
||||||
|
query, cos, sin, is_neox_style=False
|
||||||
|
), _apply_rotary_emb(key, cos, sin, is_neox_style=False)
|
||||||
attn_output = self.attn1(query, key, value)
|
attn_output = self.attn1(query, key, value)
|
||||||
attn_output = attn_output.flatten(2)
|
attn_output = attn_output.flatten(2)
|
||||||
attn_output, _ = self.to_out(attn_output)
|
attn_output, _ = self.to_out(attn_output)
|
||||||
@@ -569,9 +582,21 @@ class WanTransformerBlock_VSA(nn.Module):
|
|||||||
|
|
||||||
# Apply rotary embeddings
|
# Apply rotary embeddings
|
||||||
cos, sin = freqs_cis
|
cos, sin = freqs_cis
|
||||||
query, key = _apply_rotary_emb(
|
if query.is_cuda and query.shape == key.shape:
|
||||||
query, cos, sin, is_neox_style=False
|
cos_sin_cache = torch.cat(
|
||||||
), _apply_rotary_emb(key, cos, sin, is_neox_style=False)
|
[
|
||||||
|
cos.to(dtype=torch.float32).contiguous(),
|
||||||
|
sin.to(dtype=torch.float32).contiguous(),
|
||||||
|
],
|
||||||
|
dim=-1,
|
||||||
|
)
|
||||||
|
query, key = apply_flashinfer_rope_qk_inplace(
|
||||||
|
query, key, cos_sin_cache, is_neox=False
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
query, key = _apply_rotary_emb(
|
||||||
|
query, cos, sin, is_neox_style=False
|
||||||
|
), _apply_rotary_emb(key, cos, sin, is_neox_style=False)
|
||||||
|
|
||||||
attn_output = self.attn1(query, key, value, gate_compress=gate_compress)
|
attn_output = self.attn1(query, key, value, gate_compress=gate_compress)
|
||||||
attn_output = attn_output.flatten(2)
|
attn_output = attn_output.flatten(2)
|
||||||
|
|||||||
Reference in New Issue
Block a user