[FIX] add sglang launch wrapper

#4
by voves - opened
Files changed (1) hide show
  1. sglang/glm53_patch.py +98 -0
sglang/glm53_patch.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ import sys
3
+ import regex as re
4
+
5
+ import sglang.srt.layers.quantization.modelopt_quant as moq
6
+ import sglang.srt.layers.quantization.utils as quant_utils
7
+ from sglang.srt.layers.quantization.unquant import UnquantizedLinearMethod
8
+
9
+ try:
10
+ _use_nvfp4_dispatch = moq._use_nvfp4_dispatch
11
+ except AttributeError:
12
+ try:
13
+ from sglang.srt.layers.quantization.modelopt_quant import _use_nvfp4_dispatch
14
+ except ImportError:
15
+ _use_nvfp4_dispatch = lambda: False
16
+
17
+ GLM5_FUSED_MAP = {
18
+ "fused_qkv_a_proj_with_mqa": ["q_a_proj", "kv_a_proj_with_mqa"],
19
+ "fused_qkvbfg_a_proj": ["q_proj", "k_proj", "v_proj", "b_proj", "f_a_proj", "g_a_proj"],
20
+ "fused_fg_b_proj": ["f_b_proj", "g_b_proj"],
21
+ "qkv_proj": ["q_proj", "k_proj", "v_proj"],
22
+ "qkv_conv1d": ["q_conv1d", "k_conv1d", "v_conv1d"],
23
+ "gate_up_proj": ["gate_proj", "up_proj"],
24
+ }
25
+
26
+ if not isinstance(quant_utils._FALLBACK_FUSED_SHARDS, dict):
27
+ quant_utils._FALLBACK_FUSED_SHARDS = dict(quant_utils._FALLBACK_FUSED_SHARDS)
28
+ quant_utils._FALLBACK_FUSED_SHARDS.update(GLM5_FUSED_MAP)
29
+
30
+ _orig_init = moq.ModelOptQuantConfig.__init__
31
+ def _patched_init(self, *args, **kwargs):
32
+ _orig_init(self, *args, **kwargs)
33
+ if self.packed_modules_mapping:
34
+ for k, v in GLM5_FUSED_MAP.items():
35
+ self.packed_modules_mapping.setdefault(k, v)
36
+ else:
37
+ self.packed_modules_mapping = dict(GLM5_FUSED_MAP)
38
+ if self.exclude_modules:
39
+ mapped = []
40
+ for name in self.exclude_modules:
41
+ mapped.append(name)
42
+ if name.startswith("model.language_model."):
43
+ mapped.append(name.replace("model.language_model.", "model."))
44
+ elif name.startswith("model.visual"):
45
+ mapped.append(name.replace("model.visual", "visual"))
46
+ self.exclude_modules = list(dict.fromkeys(mapped))
47
+ moq.ModelOptQuantConfig.__init__ = _patched_init
48
+
49
+ _orig_is_excluded = moq.ModelOptQuantConfig.is_layer_excluded
50
+ def _patched_is_excluded(self, prefix):
51
+ if not self.exclude_modules:
52
+ return False
53
+ prefixes = [prefix]
54
+ if prefix.startswith("language_model."):
55
+ prefixes.append(prefix.removeprefix("language_model."))
56
+ head, _, tail = prefix.rpartition(".")
57
+ packed = self.packed_modules_mapping or {}
58
+ if tail in packed:
59
+ for shard in packed[tail]:
60
+ exp = f"{head}.{shard}" if head else shard
61
+ if exp not in prefixes:
62
+ prefixes.append(exp)
63
+ fused = {"q_a_proj", "q_b_proj", "kv_a_proj_with_mqa", "kv_b_proj"}
64
+ for pattern in self.exclude_modules:
65
+ rx = pattern.replace(".", r"\.").replace("*", r".*")
66
+ for pfx in prefixes:
67
+ if re.fullmatch(rx, pfx):
68
+ return True
69
+ for part in pfx.split("."):
70
+ if re.fullmatch(rx, part):
71
+ return True
72
+ pt = pattern.rsplit(".", maxsplit=1)[-1]
73
+ if pt in fused:
74
+ for pfx in prefixes:
75
+ if pt in pfx.rsplit(".", maxsplit=1)[-1]:
76
+ return True
77
+ return False
78
+ moq.ModelOptQuantConfig.is_layer_excluded = _patched_is_excluded
79
+
80
+ def _force_unquant(get_method):
81
+ def _patched_get(self, layer, prefix):
82
+ if "visual." in prefix:
83
+ return UnquantizedLinearMethod()
84
+ return get_method(self, layer, prefix)
85
+ return _patched_get
86
+
87
+ for cls in [moq.ModelOptFp4Config, moq.ModelOptFp8Config, moq.ModelOptQuantConfig]:
88
+ if hasattr(cls, "get_quant_method"):
89
+ cls.get_quant_method = _force_unquant(cls.get_quant_method)
90
+
91
+ from sglang.srt.plugins import load_plugins
92
+ from sglang.launch_server import run_server
93
+ from sglang.srt.server_args import prepare_server_args
94
+
95
+ if __name__ == "__main__":
96
+ load_plugins()
97
+ server_args = prepare_server_args(sys.argv[1:])
98
+ run_server(server_args)