[Performance] Optimize Dots3 NOTE runtime (#53517)
Signed-off-by: KurodaKanbei <mistergalahad@gmail.com> Signed-off-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: Jee Jee Li <jeejeelee@inferact.ai> Co-authored-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
co-authored by
Jee Jee Li
Jee Jee Li
parent
7292ee2791
commit
da0b2d8b17
@@ -17,8 +17,11 @@ import torch
|
||||
import torch.nn.functional as F
|
||||
from torch import nn
|
||||
|
||||
from vllm.logger import init_logger
|
||||
from vllm.vllm_flash_attn import flash_attn_varlen_func, is_fa_version_supported
|
||||
|
||||
logger = init_logger(__name__)
|
||||
|
||||
_flash_attn_available = is_fa_version_supported(2)
|
||||
_flash_attn3_available = is_fa_version_supported(3)
|
||||
_flash_attn3_varlen_func = flash_attn_varlen_func
|
||||
@@ -107,8 +110,8 @@ class VisionRotaryEmbedding(nn.Module):
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class _RMSNorm(nn.Module):
|
||||
"""Q/K norm inside attention; matches Dots ViT RMSNorm (fp32 reduce, then cast back)."""
|
||||
class VisionRMSNorm(nn.Module):
|
||||
"""Dots ViT RMSNorm with an fp32 reduction and input-dtype output."""
|
||||
|
||||
def __init__(self, dim: int, eps: float = 1e-6):
|
||||
super().__init__()
|
||||
@@ -119,6 +122,9 @@ class _RMSNorm(nn.Module):
|
||||
output = self._norm(x.float()).type_as(x)
|
||||
return output * self.weight
|
||||
|
||||
def extra_repr(self) -> str:
|
||||
return f"{tuple(self.weight.shape)}, eps={self.eps}"
|
||||
|
||||
def _norm(self, x: torch.Tensor) -> torch.Tensor:
|
||||
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
|
||||
|
||||
@@ -156,8 +162,8 @@ class _VisionAttentionBase(nn.Module):
|
||||
self.qkv = nn.Linear(params.dim, params.dim * 3, bias=params.bias)
|
||||
self.proj = nn.Linear(params.dim, params.dim, bias=params.bias)
|
||||
if self.use_qk_norm:
|
||||
self.q_norm = _RMSNorm(self.head_dim, eps=params.rms_norm_eps)
|
||||
self.k_norm = _RMSNorm(self.head_dim, eps=params.rms_norm_eps)
|
||||
self.q_norm = VisionRMSNorm(self.head_dim, eps=params.rms_norm_eps)
|
||||
self.k_norm = VisionRMSNorm(self.head_dim, eps=params.rms_norm_eps)
|
||||
|
||||
def _qkv_with_rope(
|
||||
self,
|
||||
@@ -385,12 +391,15 @@ def resolve_attn_implementation(
|
||||
uses full-mask ``eager``.
|
||||
"""
|
||||
if attn_implementation == "flash_attention_3" and not _flash_attn3_available:
|
||||
print(
|
||||
logger.warning_once(
|
||||
"flash attention 3 not available! fallback to flash attention 2 implementation"
|
||||
)
|
||||
attn_implementation = "flash_attention_2"
|
||||
if attn_implementation == "flash_attention_2" and not _flash_attn_available:
|
||||
print("flash attention 2 not available! fallback to eager implementation")
|
||||
logger.warning_once(
|
||||
"flash attention 2 not available! fallback to %s implementation",
|
||||
eager_fallback,
|
||||
)
|
||||
attn_implementation = eager_fallback
|
||||
return attn_implementation
|
||||
|
||||
@@ -481,6 +490,7 @@ __all__ = [
|
||||
"VisionFlashAttention3",
|
||||
"VisionRotaryEmbedding",
|
||||
"VisionRotaryPositionEmbedding",
|
||||
"VisionRMSNorm",
|
||||
"VisionSdpaAttention",
|
||||
"apply_rotary_pos_emb_vision",
|
||||
"apply_vision_attention_residual",
|
||||
|
||||
Reference in New Issue
Block a user