haha-hehe commited on
Commit
034cc6f
·
verified ·
1 Parent(s): 7e9f2f9

Upload serving/serve_example.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. serving/serve_example.py +87 -0
serving/serve_example.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Minimal example: load Clover 2's base model, attach the 7 trained LoRA experts and the trained router for
2
+ real per-token dynamic routing, then generate.
3
+
4
+ Requires: vllm, torch. Tested against vllm==0.31.0.
5
+
6
+ IMPORTANT -- compilation_config: torch.compile silently drops every LoRA delta under CUDA graph capture
7
+ unless it's disabled this way. Stubbing GPUModelRunner.capture_model only defers the CUDA-graph-REPLAY-buffer
8
+ step; it does NOT defer torch.compile's own Dynamo tracing, which vLLM triggers during LLM()'s own internal
9
+ profiling/warmup -- i.e. BEFORE attach() ever runs, against the ORIGINAL unpatched mlp. That compiled graph
10
+ then gets reused by compile_or_warm_up_model() regardless of the live monkey-patch, silently dropping every
11
+ LoRA delta. Disabling torch.compile (mode=0) and relying on vLLM's CUDA graph capture directly sidesteps this
12
+ entirely: the capture-time warmup call then executes the patched Python forward directly.
13
+ """
14
+ import os
15
+ os.environ['VLLM_ENABLE_V1_MULTIPROCESSING'] = '0'
16
+ import torch
17
+ import vllm.v1.worker.gpu.model_runner as MR
18
+
19
+ # capture_model must be stubbed out BEFORE LLM() builds the model, so graph capture doesn't run against the
20
+ # original (unpatched) mlp. Restored and called manually after attach()+load_adapter(), below.
21
+ _orig_capture_model = MR.GPUModelRunner.capture_model
22
+ MR.GPUModelRunner.capture_model = lambda self, *a, **kw: 0
23
+
24
+ from vllm import LLM, SamplingParams
25
+ import vllm_moelora as V
26
+
27
+ BASE_MODEL = "./base" # path to the base checkpoint (this repo's base/ subfolder)
28
+ ADAPTERS = {
29
+ 1: "./adapters/roleplay/adapter_model.safetensors",
30
+ 2: "./adapters/clover/adapter_model.safetensors",
31
+ 3: "./adapters/security/adapter_model.safetensors",
32
+ 4: "./adapters/math/adapter_model.safetensors",
33
+ 5: "./adapters/longdoc2/adapter_model.safetensors",
34
+ 6: "./adapters/efficiency/adapter_model.safetensors",
35
+ 7: "./adapters/knowledge/adapter_model.safetensors",
36
+ }
37
+ ROUTER_PATH = "./router/trained_router_v2.pt"
38
+
39
+ # Security (slot 3) is masked off by default -- enabling it measurably increases compliance with offensive
40
+ # (dual-use) security-research requests. Remove 3 from this list to re-enable it.
41
+ DEFAULT_MASKED_SLOTS = [3]
42
+
43
+
44
+ def load_safetensors_adapter(path):
45
+ from safetensors.torch import load_file
46
+ return load_file(path)
47
+
48
+
49
+ def main():
50
+ llm = LLM(
51
+ model=BASE_MODEL,
52
+ dtype="bfloat16",
53
+ gpu_memory_utilization=0.85,
54
+ trust_remote_code=False,
55
+ additional_config={"gdn_prefill_backend": "triton"},
56
+ compilation_config={"mode": 0, "cudagraph_mode": "FULL"},
57
+ )
58
+
59
+ worker = llm.llm_engine.model_executor.driver_worker.worker
60
+ model = worker.model_runner.model
61
+ moles = V.attach(model, n_experts=7, rank=128, scaling=2.0, topk=2)
62
+
63
+ for slot, path in ADAPTERS.items():
64
+ V.load_adapter(moles, slot, load_safetensors_adapter(path))
65
+
66
+ router_state = torch.load(ROUTER_PATH, map_location="cpu", weights_only=False)
67
+ for layer, m in moles.items():
68
+ sd = router_state[layer]
69
+ m.router.weight.data.copy_(sd["weight"].to(m.router.weight.device, m.router.weight.dtype))
70
+ m.router.bias.data.copy_(sd["bias"].to(m.router.bias.device, m.router.bias.dtype))
71
+
72
+ V.set_mask(moles, DEFAULT_MASKED_SLOTS)
73
+
74
+ MR.GPUModelRunner.capture_model = _orig_capture_model
75
+ worker.compile_or_warm_up_model()
76
+
77
+ sp = SamplingParams(temperature=0.8, max_tokens=300)
78
+ outs = llm.chat(
79
+ [[{"role": "user", "content": "Write a short scene where a detective questions a nervous witness."}]],
80
+ sp,
81
+ chat_template_kwargs={"enable_thinking": False},
82
+ )
83
+ print(outs[0].outputs[0].text)
84
+
85
+
86
+ if __name__ == "__main__":
87
+ main()