-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathregister.py
More file actions
155 lines (121 loc) · 6.07 KB
/
Copy pathregister.py
File metadata and controls
155 lines (121 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from typing import Optional
import logging
import torch
from atom.plugin.prepare import _set_framework_backbone
from atom.utils import envs
from atom.plugin.vllm.spec_decode_patch import apply_vllm_spec_decode_patch
logger = logging.getLogger("atom")
# this flag is used to enable the vllm plugin mode
disable_vllm_plugin = envs.ATOM_DISABLE_VLLM_PLUGIN
# those 2 models are covering most of dense and moe models
ATOM_CAUSAL_LM_MODEL_WRAPPER = "atom.plugin.vllm.model_wrapper:ATOMForCausalLM"
ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER = "atom.plugin.vllm.model_wrapper:ATOMMoEForCausalLM"
# when register new model to vllm, add here
# Keys is from hf config arch name
_VLLM_MODEL_REGISTRY_OVERRIDES: dict[str, str] = {
"LlamaForCausalLM": ATOM_CAUSAL_LM_MODEL_WRAPPER,
"Qwen3ForCausalLM": ATOM_CAUSAL_LM_MODEL_WRAPPER,
"Qwen3MoeForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"GptOssForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"DeepseekV3ForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"DeepseekV32ForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"Glm4MoeForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"GlmMoeDsaForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"DeepSeekMTPModel": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"DeepSeekV4MTPModel": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"Glm4MoeMTPModel": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"Qwen3NextForCausalLM": "atom.plugin.vllm.models.qwen3_next:Qwen3NextForCausalLMVllm",
"Qwen3NextMTP": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"Qwen3_5ForConditionalGeneration": "atom.plugin.vllm.models.qwen3_5:Qwen3_5ForConditionalGeneration",
"Qwen3_5MoeForConditionalGeneration": "atom.plugin.vllm.models.qwen3_5:Qwen3_5MoeForConditionalGeneration",
"KimiK25ForConditionalGeneration": "atom.plugin.vllm.models.kimi_k25:KimiK25ForConditionalGeneration",
"MiniMaxM2ForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
"DeepseekV4ForCausalLM": ATOM_MOE_CAUSAL_LM_MODEL_WRAPPER,
}
def _set_plugin_mode() -> None:
_set_framework_backbone("vllm")
def register_platform() -> Optional[str]:
if disable_vllm_plugin:
# return None instead of error because the flag can be used to
# run pure vllm mode without ATOM plugin
logger.info("Disable ATOM OOT plugin platforms")
return None
# Do not call _set_plugin_mode() here. SGLang (and other stacks) discover
# vllm.platform_plugins and would set atom's backbone to "vllm" before
# importing SGLang plugin modules — then atom.models.qwen3_5's ``if is_vllm():``
# branch runs and requires vllm.model_executor.models.qwen3_5, which may be
# absent. Backbone is set in register_model() for real vLLM runs.
# return the ATOM platform to vllm
return "atom.plugin.vllm.platform.ATOMPlatform"
def _patch_vllm_attention_process_weights_after_loading(attention) -> None:
orig = attention.process_weights_after_loading
if getattr(orig, "_atom_default_act_dtype_patched", False):
return
try:
import inspect
sig = inspect.signature(orig)
act_dtype_param = sig.parameters.get("act_dtype")
if (
act_dtype_param is not None
and act_dtype_param.default is not inspect._empty
):
return
except Exception:
pass
import functools
@functools.wraps(orig)
def wrapped(self, act_dtype: "torch.dtype" = torch.bfloat16):
return orig(self, act_dtype)
setattr(wrapped, "_atom_default_act_dtype_patched", True)
attention.process_weights_after_loading = wrapped
def register_model() -> None:
if disable_vllm_plugin:
logger.info("Disable ATOM model register")
return
_set_plugin_mode()
import vllm.model_executor.models.registry as vllm_model_registry
any_updated = False
for arch, qual in _VLLM_MODEL_REGISTRY_OVERRIDES.items():
module_name, class_name = qual.split(":", 1)
existing = vllm_model_registry.ModelRegistry.models.get(arch)
if existing is not None:
# If already overridden to the same target, skip re-registering.
if (
getattr(existing, "module_name", None) == module_name
and getattr(existing, "class_name", None) == class_name
):
continue
logger.info(f"Register model {arch} to vLLM with {qual}")
vllm_model_registry.ModelRegistry.register_model(arch, qual)
any_updated = True
# clear lru cache
if any_updated:
vllm_model_registry._try_load_model_cls.cache_clear()
vllm_model_registry._try_inspect_model_cls.cache_clear()
# patch attention process weights after loading
# to avoid the specific handle in ATOM loader
try:
from vllm.attention.layer import Attention, MLAAttention
except ImportError:
from vllm.model_executor.layers.attention import Attention, MLAAttention
_patch_vllm_attention_process_weights_after_loading(Attention)
_patch_vllm_attention_process_weights_after_loading(MLAAttention)
# vLLM's speculative decoder keeps an allow-list of attention metadata
# classes. ATOM-vLLM uses its own metadata classes after attention
# isolation, so extend that allow-list before MTP/Eagle proposal runs.
apply_vllm_spec_decode_patch()
# Patch vLLM graph_capture to also enter aiter's ca_comm.capture(),
# avoiding hipMemcpyAsync in fused_allreduce_rmsnorm when model uses aiter collectives
from atom.plugin.vllm.graph_capture_patch import apply_graph_capture_patch
apply_graph_capture_patch()
# The native MORI MoE path is frontend-agnostic; inject atom-vllm-specific
# launch-config selection and dispatch-buffer trimming via plugin patches.
from atom.plugin.vllm.mori_patch import apply_vllm_mori_patch
apply_vllm_mori_patch()
# Expose batch-ordered req_ids to ATOM metadata builders so the DeepSeek-V4
# proxy can key state-slot allocation on the request id (host-resident)
# instead of a D2H copy of the first block id.
from atom.plugin.vllm.req_id_passthrough_patch import (
apply_vllm_req_id_passthrough_patch,
)
apply_vllm_req_id_passthrough_patch()