Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions lightllm/common/attention/varlen.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
from lightllm.platform import get_device_runtime
from lightllm.utils.log_utils import init_logger

from .api import AttentionFamily, AttentionPhase, UnsupportedAttention
from .selection import BackendSelectionError, get_varlen_attention_op_class

logger = init_logger(__name__)


class AutoVarlenAttentionOp:
"""音频编码器的自动路径;失败的实现只在当前调用方禁用,并在本次调用中重算输出。"""
"""One encoder owns this runner and shares stateless operators across its layers.

Unsupported inputs can try the next provider for this call. Kernel and programming
errors propagate: retrying on a potentially failed device is not a safe fallback.
Environment rejections are cached by provider/device; input rejections are not.
"""

def __init__(self, providers=("fa3", "triton")):
self.providers = providers
self.ops = {}
self.failures = {}
self.unavailable = {}

def forward(self, inputs, *, out):
failures = {}
caps = get_device_runtime(inputs.query.device).caps
for name in self.providers:
if name in self.failures:
key = (name, inputs.query.device)
if key in self.unavailable:
failures[name] = self.unavailable[key]
continue
try:
if name not in self.ops:
if key not in self.ops:
try:
op_cls = get_varlen_attention_op_class(name)
caps = get_device_runtime(inputs.query.device).caps
op_cls.check_environment(caps)
op_cls.check_support(inputs, caps)
self.ops[name] = op_cls()
return self.ops[name].forward(inputs, out=out)
self.ops[key] = op_cls()
except (UnsupportedAttention, ImportError) as exc:
self.unavailable[key] = failures[name] = str(exc)
continue
op = self.ops[key]
try:
op.check_support(inputs, caps)
except UnsupportedAttention as exc:
self.failures[name] = str(exc)
except Exception as exc:
self.failures[name] = str(exc)
logger.warning("Disabling %s varlen attention for this encoder: %s", name, exc)
raise BackendSelectionError(AttentionFamily.DENSE, AttentionPhase.PREFILL, self.failures)
failures[name] = str(exc)
continue
return op.forward(inputs, out=out)
raise BackendSelectionError(AttentionFamily.DENSE, AttentionPhase.PREFILL, failures)
Empty file.
38 changes: 0 additions & 38 deletions lightllm/common/basemodel/attention_vit/base_att.py

This file was deleted.

47 changes: 0 additions & 47 deletions lightllm/common/basemodel/attention_vit/create_utils.py

This file was deleted.

Empty file.
60 changes: 0 additions & 60 deletions lightllm/common/basemodel/attention_vit/fa3/fp.py

This file was deleted.

Empty file.
47 changes: 0 additions & 47 deletions lightllm/common/basemodel/attention_vit/sdpa/fp.py

This file was deleted.

Empty file.
24 changes: 0 additions & 24 deletions lightllm/common/basemodel/attention_vit/triton/fp.py

This file was deleted.

Empty file.
45 changes: 0 additions & 45 deletions lightllm/common/basemodel/attention_vit/xformers/fp.py

This file was deleted.

15 changes: 6 additions & 9 deletions lightllm/models/deepseek_v4/deepseek_v4_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
from safetensors import safe_open
from torch import nn

from lightllm.models.deepseek_v4.image_processor import (
IMAGE,
build_canonical_image_block,
load_image,
)
from lightllm.common.attention.api import VarlenAttentionInputs
from lightllm.models.deepseek_v4.image_processor import IMAGE, build_canonical_image_block, load_image
from lightllm.server.embed_cache.utils import get_shm_name_data, read_shm
from lightllm.server.multimodal_params import ImageItem
from lightllm.server.visualserver import get_vit_attn_backend
from lightllm.server.visualserver import get_vit_attention_op


@lru_cache(8)
Expand Down Expand Up @@ -56,7 +53,7 @@ class PatchEmbed(nn.Module):
def __init__(self, args):
super().__init__()
self.proj = nn.Linear(
3 * args.vision_patch_size ** 2,
3 * args.vision_patch_size**2,
args.vision_dim,
dtype=torch.bfloat16,
)
Expand All @@ -79,7 +76,7 @@ def forward(self, x: torch.Tensor, cos: torch.Tensor, sin: torch.Tensor, cu_seql
q = apply_rotary(q, cos, sin)
k = apply_rotary(k, cos, sin)
o = torch.empty_like(q)
get_vit_attn_backend()(q, k, v, o, cu_seqlens, n)
get_vit_attention_op().forward(VarlenAttentionInputs(q, k, v, cu_seqlens, n), out=o)
return self.wo(o.reshape(n, -1))


Expand Down Expand Up @@ -143,7 +140,7 @@ class Aligner(nn.Module):
def __init__(self, args):
super().__init__()
self.downsample_ratio = args.vision_downsample_ratio
in_dim = args.vision_dim * self.downsample_ratio ** 2
in_dim = args.vision_dim * self.downsample_ratio**2
self.w1 = nn.Linear(in_dim, args.hidden_size, dtype=torch.bfloat16)
self.w2 = nn.Linear(args.hidden_size, args.hidden_size, dtype=torch.bfloat16)

Expand Down
6 changes: 4 additions & 2 deletions lightllm/models/glm5_next/glm5_next_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import torch.nn.functional as F
from safetensors import safe_open

from lightllm.common.attention.api import VarlenAttentionInputs
from lightllm.models.qwen2_vl.qwen2_visual import Qwen2VisionTransformerPretrainedModel
from lightllm.models.qwen2_vl.triton_kernel.rotary_pos_emb import apply_rotary_pos_emb_triton
from lightllm.models.vit.triton_kernel.rms_norm_vit import qk_rms_norm, rms_norm
from lightllm.server.visualserver import get_vit_attn_backend
from lightllm.server.visualserver import get_vit_attention_op

from .vision_process import Glm5NextImageProcessor


Expand Down Expand Up @@ -56,7 +58,7 @@ def forward(self, x, cu_seqlens, max_seqlen, rotary_cos, rotary_sin):
q = apply_rotary_pos_emb_triton(q, rotary_cos, rotary_sin)
k = apply_rotary_pos_emb_triton(k, rotary_cos, rotary_sin)
out = torch.empty_like(q)
get_vit_attn_backend()(q, k, v, out, cu_seqlens, max_seqlen)
get_vit_attention_op().forward(VarlenAttentionInputs(q, k, v, cu_seqlens, max_seqlen), out=out)
return self.proj(out.reshape(x.shape[0], -1))


Expand Down
Loading