[VLM] Support VLM ViT Piecewise CUDA Graph (#14422)
Co-authored-by: luoyuan.luo <luoyuan.luo@antgroup.com> Co-authored-by: kousakawang <wanghanpei@bytedance.com>
This commit is contained in:
co-authored by
luoyuan.luo
kousakawang
parent
f1bbd26ff7
commit
3b8a824b8b
@@ -72,8 +72,9 @@ from sglang.srt.model_loader.weight_utils import default_weight_loader
|
||||
from sglang.srt.models.qwen2 import Qwen2Model
|
||||
from sglang.srt.models.utils import RotaryPosMixin, permute_inv
|
||||
from sglang.srt.multimodal.mm_utils import run_dp_sharded_mrope_vision_model
|
||||
from sglang.srt.multimodal.vit_cuda_graph_runner import ViTCudaGraphRunner
|
||||
from sglang.srt.server_args import get_global_server_args
|
||||
from sglang.srt.utils import add_prefix
|
||||
from sglang.srt.utils import add_prefix, get_bool_env_var
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -167,7 +168,9 @@ class Qwen2_5_VisionBlock(nn.Module):
|
||||
x: torch.Tensor,
|
||||
cu_seqlens: torch.Tensor,
|
||||
position_embeddings: torch.Tensor,
|
||||
output_ws=None,
|
||||
) -> torch.Tensor:
|
||||
ws = output_ws
|
||||
S, B, H = x.shape
|
||||
# norm1: flatten to 2D -> [S*B, H], then reshape back
|
||||
x2d = x.reshape(-1, H)
|
||||
@@ -179,6 +182,7 @@ class Qwen2_5_VisionBlock(nn.Module):
|
||||
hidden_states,
|
||||
cu_seqlens=cu_seqlens,
|
||||
position_embeddings=position_embeddings,
|
||||
output_ws=ws,
|
||||
)
|
||||
attn = rearrange(attn, "b s h -> s b h")
|
||||
|
||||
@@ -256,6 +260,7 @@ class Qwen2_5_VisionTransformer(nn.Module, RotaryPosMixin):
|
||||
quant_config: Optional[QuantizationConfig] = None,
|
||||
prefix: str = "",
|
||||
use_data_parallel: bool = False,
|
||||
max_context_len: Optional[int] = None,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
|
||||
@@ -308,6 +313,13 @@ class Qwen2_5_VisionTransformer(nn.Module, RotaryPosMixin):
|
||||
use_data_parallel=use_data_parallel,
|
||||
)
|
||||
|
||||
# Resource prepared for vit cuda graph
|
||||
self.tp_size = (
|
||||
1 if use_data_parallel else get_tensor_model_parallel_world_size()
|
||||
)
|
||||
self.max_context_len = max_context_len
|
||||
self.cuda_graph_runner: Optional[ViTCudaGraphRunner] = ViTCudaGraphRunner(self)
|
||||
|
||||
def get_window_index(self, grid_thw):
|
||||
cu_window_seqlens: list = [0]
|
||||
window_index_id = 0
|
||||
@@ -378,6 +390,9 @@ class Qwen2_5_VisionTransformer(nn.Module, RotaryPosMixin):
|
||||
x: torch.Tensor,
|
||||
grid_thw: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
if get_bool_env_var("SGLANG_VIT_ENABLE_CUDA_GRAPH") and self.tp_size == 1:
|
||||
return self.forward_with_cuda_graph(x, grid_thw)
|
||||
|
||||
# patchify
|
||||
x = x.to(device=self.device, dtype=self.dtype)
|
||||
x = self.patch_embed(x)
|
||||
@@ -446,6 +461,72 @@ class Qwen2_5_VisionTransformer(nn.Module, RotaryPosMixin):
|
||||
|
||||
return x
|
||||
|
||||
def forward_with_cuda_graph(
|
||||
self,
|
||||
x: torch.Tensor,
|
||||
grid_thw: torch.Tensor,
|
||||
) -> torch.Tensor:
|
||||
# patchify
|
||||
x = x.to(device=self.device, dtype=self.dtype)
|
||||
x = self.patch_embed(x)
|
||||
|
||||
# compute position embedding
|
||||
rotary_pos_emb = self.rot_pos_emb(grid_thw)
|
||||
|
||||
window_index, cu_window_seqlens = self.get_window_index(grid_thw)
|
||||
cu_window_seqlens = torch.tensor(
|
||||
cu_window_seqlens,
|
||||
device=x.device,
|
||||
dtype=torch.int32,
|
||||
)
|
||||
cu_window_seqlens = torch.unique_consecutive(cu_window_seqlens)
|
||||
|
||||
window_index = window_index.to(device=x.device)
|
||||
reverse_indices = permute_inv(window_index)
|
||||
rotary_pos_emb = rotary_pos_emb.to(device=x.device, dtype=x.dtype)
|
||||
|
||||
# patch token num
|
||||
seq_len, _ = x.size()
|
||||
|
||||
# [G, M, hidden]
|
||||
x = x.reshape(seq_len // self.spatial_merge_unit, self.spatial_merge_unit, -1)
|
||||
x = x[window_index, :, :] # [G, M, hidden]
|
||||
x = x.reshape(seq_len, -1) # [seq_len, hidden]
|
||||
|
||||
rotary_pos_emb = rotary_pos_emb.reshape(
|
||||
seq_len // self.spatial_merge_unit, self.spatial_merge_unit, -1
|
||||
)
|
||||
rotary_pos_emb = rotary_pos_emb[window_index, :, :]
|
||||
rotary_pos_emb = rotary_pos_emb.reshape(seq_len, -1)
|
||||
|
||||
emb = torch.cat((rotary_pos_emb, rotary_pos_emb), dim=-1)
|
||||
position_embeddings = (emb.cos(), emb.sin())
|
||||
# After building position_embeddings, make sure both cos and sin are on
|
||||
# the same device/dtype as the attention input
|
||||
position_embeddings = (
|
||||
position_embeddings[0].to(x.device, x.dtype),
|
||||
position_embeddings[1].to(x.device, x.dtype),
|
||||
)
|
||||
|
||||
# compute cu_seqlens - move cu_seqlens to GPU and make it int32
|
||||
cu_seqlens = torch.cat(
|
||||
[
|
||||
torch.tensor([0], device=x.device, dtype=torch.int32),
|
||||
(grid_thw[:, 0] * grid_thw[:, 1] * grid_thw[:, 2])
|
||||
.cumsum(dim=0)
|
||||
.to(device=x.device, dtype=torch.int32),
|
||||
]
|
||||
)
|
||||
cu_seqlens = torch.cat([cu_seqlens.new_zeros(1), cu_seqlens])
|
||||
|
||||
return self.cuda_graph_runner.run(
|
||||
x=x,
|
||||
position_embeddings=position_embeddings,
|
||||
cu_seqlens=cu_seqlens,
|
||||
cu_window_seqlens=cu_window_seqlens,
|
||||
output_indices=reverse_indices,
|
||||
)
|
||||
|
||||
|
||||
class Qwen2_5_VLForConditionalGeneration(nn.Module):
|
||||
# BitandBytes specific attributes
|
||||
@@ -485,6 +566,7 @@ class Qwen2_5_VLForConditionalGeneration(nn.Module):
|
||||
quant_config=quant_config,
|
||||
prefix=add_prefix("visual", prefix),
|
||||
use_data_parallel=self.use_data_parallel,
|
||||
max_context_len=self.config.max_position_embeddings,
|
||||
)
|
||||
|
||||
self.model = Qwen2Model(
|
||||
|
||||
Reference in New Issue
Block a user