[Diffusion] Refactor diffusion is_cuda check (#17498)
This commit is contained in:
@@ -12,6 +12,7 @@ from sglang.multimodal_gen.runtime.platforms import current_platform
|
|||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
class CustomOp(nn.Module):
|
class CustomOp(nn.Module):
|
||||||
@@ -58,7 +59,7 @@ class CustomOp(nn.Module):
|
|||||||
return self.forward_native(*args, **kwargs)
|
return self.forward_native(*args, **kwargs)
|
||||||
|
|
||||||
def dispatch_forward(self) -> Callable:
|
def dispatch_forward(self) -> Callable:
|
||||||
if current_platform.is_cuda():
|
if _is_cuda:
|
||||||
return self.forward_cuda
|
return self.forward_cuda
|
||||||
elif current_platform.is_hip():
|
elif current_platform.is_hip():
|
||||||
return self.forward_hip
|
return self.forward_hip
|
||||||
|
|||||||
@@ -23,8 +23,11 @@ from sglang.multimodal_gen.runtime.layers.triton_ops import (
|
|||||||
rms_norm_fn,
|
rms_norm_fn,
|
||||||
triton_one_pass_rms_norm,
|
triton_one_pass_rms_norm,
|
||||||
)
|
)
|
||||||
|
from sglang.multimodal_gen.runtime.platforms import current_platform
|
||||||
from sglang.multimodal_gen.runtime.utils.common import get_bool_env_var
|
from sglang.multimodal_gen.runtime.utils.common import get_bool_env_var
|
||||||
|
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
# Copied and adapted from sglang
|
# Copied and adapted from sglang
|
||||||
@CustomOp.register("rms_norm")
|
@CustomOp.register("rms_norm")
|
||||||
@@ -453,7 +456,7 @@ def apply_qk_norm(
|
|||||||
k_eps = k_norm.variance_epsilon
|
k_eps = k_norm.variance_epsilon
|
||||||
# Only try fused path on CUDA and when it won't introduce implicit copies.
|
# Only try fused path on CUDA and when it won't introduce implicit copies.
|
||||||
if (
|
if (
|
||||||
q.is_cuda
|
_is_cuda
|
||||||
and allow_inplace
|
and allow_inplace
|
||||||
and (q_eps == k_eps)
|
and (q_eps == k_eps)
|
||||||
and can_use_fused_inplace_qknorm(head_dim, q.dtype)
|
and can_use_fused_inplace_qknorm(head_dim, q.dtype)
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiT
|
|||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__) # pylint: disable=invalid-name
|
logger = init_logger(__name__) # pylint: disable=invalid-name
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
def _get_qkv_projections(
|
def _get_qkv_projections(
|
||||||
@@ -167,7 +168,7 @@ class FluxAttention(torch.nn.Module, AttentionModuleMixin):
|
|||||||
key = key.unflatten(-1, (self.heads, -1))
|
key = key.unflatten(-1, (self.heads, -1))
|
||||||
value = value.unflatten(-1, (self.heads, -1))
|
value = value.unflatten(-1, (self.heads, -1))
|
||||||
if (
|
if (
|
||||||
query.is_cuda
|
_is_cuda
|
||||||
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
||||||
and can_use_fused_inplace_qknorm(self.head_dim, query.dtype)
|
and can_use_fused_inplace_qknorm(self.head_dim, query.dtype)
|
||||||
):
|
):
|
||||||
@@ -189,7 +190,7 @@ class FluxAttention(torch.nn.Module, AttentionModuleMixin):
|
|||||||
encoder_value = encoder_value.unflatten(-1, (self.heads, -1))
|
encoder_value = encoder_value.unflatten(-1, (self.heads, -1))
|
||||||
|
|
||||||
if (
|
if (
|
||||||
encoder_query.is_cuda
|
_is_cuda
|
||||||
and (
|
and (
|
||||||
self.norm_added_q.variance_epsilon
|
self.norm_added_q.variance_epsilon
|
||||||
== self.norm_added_k.variance_epsilon
|
== self.norm_added_k.variance_epsilon
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiT
|
|||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__) # pylint: disable=invalid-name
|
logger = init_logger(__name__) # pylint: disable=invalid-name
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
def _get_qkv_projections(
|
def _get_qkv_projections(
|
||||||
@@ -198,7 +199,7 @@ class Flux2Attention(torch.nn.Module, AttentionModuleMixin):
|
|||||||
value = value.unflatten(-1, (self.heads, -1))
|
value = value.unflatten(-1, (self.heads, -1))
|
||||||
|
|
||||||
if (
|
if (
|
||||||
query.is_cuda
|
_is_cuda
|
||||||
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
||||||
and can_use_fused_inplace_qknorm(self.head_dim, query.dtype)
|
and can_use_fused_inplace_qknorm(self.head_dim, query.dtype)
|
||||||
):
|
):
|
||||||
@@ -220,7 +221,7 @@ class Flux2Attention(torch.nn.Module, AttentionModuleMixin):
|
|||||||
encoder_value = encoder_value.unflatten(-1, (self.heads, -1))
|
encoder_value = encoder_value.unflatten(-1, (self.heads, -1))
|
||||||
|
|
||||||
if (
|
if (
|
||||||
encoder_query.is_cuda
|
_is_cuda
|
||||||
and (
|
and (
|
||||||
self.norm_added_q.variance_epsilon
|
self.norm_added_q.variance_epsilon
|
||||||
== self.norm_added_k.variance_epsilon
|
== self.norm_added_k.variance_epsilon
|
||||||
|
|||||||
@@ -31,11 +31,15 @@ from sglang.multimodal_gen.runtime.layers.triton_ops import (
|
|||||||
fuse_scale_shift_kernel,
|
fuse_scale_shift_kernel,
|
||||||
)
|
)
|
||||||
from sglang.multimodal_gen.runtime.models.dits.base import CachableDiT
|
from sglang.multimodal_gen.runtime.models.dits.base import CachableDiT
|
||||||
from sglang.multimodal_gen.runtime.platforms import AttentionBackendEnum
|
from sglang.multimodal_gen.runtime.platforms import (
|
||||||
|
AttentionBackendEnum,
|
||||||
|
current_platform,
|
||||||
|
)
|
||||||
from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiTMixin
|
from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiTMixin
|
||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__) # pylint: disable=invalid-name
|
logger = init_logger(__name__) # pylint: disable=invalid-name
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
def _get_qkv_projections(
|
def _get_qkv_projections(
|
||||||
@@ -684,7 +688,7 @@ class QwenImageTransformerBlock(nn.Module):
|
|||||||
)
|
)
|
||||||
gate0, gate1 = gate[:actual_batch], gate[actual_batch : 2 * actual_batch]
|
gate0, gate1 = gate[:actual_batch], gate[actual_batch : 2 * actual_batch]
|
||||||
|
|
||||||
if x.is_cuda:
|
if _is_cuda:
|
||||||
if not x.is_contiguous():
|
if not x.is_contiguous():
|
||||||
x = x.contiguous()
|
x = x.contiguous()
|
||||||
if not index.is_contiguous():
|
if not index.is_contiguous():
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiT
|
|||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
|
|
||||||
class WanImageEmbedding(torch.nn.Module):
|
class WanImageEmbedding(torch.nn.Module):
|
||||||
@@ -444,7 +445,7 @@ class WanTransformerBlock(nn.Module):
|
|||||||
|
|
||||||
# Apply rotary embeddings
|
# Apply rotary embeddings
|
||||||
cos, sin = freqs_cis
|
cos, sin = freqs_cis
|
||||||
if query.is_cuda and query.shape == key.shape:
|
if _is_cuda and query.shape == key.shape:
|
||||||
cos_sin_cache = torch.cat(
|
cos_sin_cache = torch.cat(
|
||||||
[
|
[
|
||||||
cos.to(dtype=torch.float32).contiguous(),
|
cos.to(dtype=torch.float32).contiguous(),
|
||||||
@@ -626,7 +627,7 @@ class WanTransformerBlock_VSA(nn.Module):
|
|||||||
|
|
||||||
# Apply rotary embeddings
|
# Apply rotary embeddings
|
||||||
cos, sin = freqs_cis
|
cos, sin = freqs_cis
|
||||||
if query.is_cuda and query.shape == key.shape:
|
if _is_cuda and query.shape == key.shape:
|
||||||
cos_sin_cache = torch.cat(
|
cos_sin_cache = torch.cat(
|
||||||
[
|
[
|
||||||
cos.to(dtype=torch.float32).contiguous(),
|
cos.to(dtype=torch.float32).contiguous(),
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ from sglang.multimodal_gen.runtime.utils.layerwise_offload import OffloadableDiT
|
|||||||
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
from sglang.multimodal_gen.runtime.utils.logging_utils import init_logger
|
||||||
|
|
||||||
logger = init_logger(__name__)
|
logger = init_logger(__name__)
|
||||||
|
_is_cuda = current_platform.is_cuda()
|
||||||
|
|
||||||
ADALN_EMBED_DIM = 256
|
ADALN_EMBED_DIM = 256
|
||||||
SEQ_MULTI_OF = 32
|
SEQ_MULTI_OF = 32
|
||||||
@@ -171,7 +172,7 @@ class ZImageAttention(nn.Module):
|
|||||||
|
|
||||||
if self.qk_norm:
|
if self.qk_norm:
|
||||||
if (
|
if (
|
||||||
q.is_cuda
|
_is_cuda
|
||||||
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
and (self.norm_q.variance_epsilon == self.norm_k.variance_epsilon)
|
||||||
and can_use_fused_inplace_qknorm(self.head_dim, q.dtype)
|
and can_use_fused_inplace_qknorm(self.head_dim, q.dtype)
|
||||||
):
|
):
|
||||||
@@ -189,7 +190,7 @@ class ZImageAttention(nn.Module):
|
|||||||
|
|
||||||
if freqs_cis is not None:
|
if freqs_cis is not None:
|
||||||
cos, sin = freqs_cis
|
cos, sin = freqs_cis
|
||||||
if q.is_cuda and q.shape == k.shape:
|
if _is_cuda and q.shape == k.shape:
|
||||||
cos_sin_cache = torch.cat(
|
cos_sin_cache = torch.cat(
|
||||||
[
|
[
|
||||||
cos.to(dtype=torch.float32).contiguous(),
|
cos.to(dtype=torch.float32).contiguous(),
|
||||||
|
|||||||
Reference in New Issue
Block a user