1+ from importlib import resources
2+ from pathlib import Path
13from typing import Any , Dict , List , Optional , Union
24
35import torch
2426ReqType = Union [str , int ]
2527
2628
29+ def kvcomp_config_path_for_model (vllm_config ) -> str :
30+ model = vllm_config .model_config .model .lower ()
31+ logger .info ("[KvComp] model name: %s" , model )
32+
33+ if "deepseek" in model and "r1" in model :
34+ rel = "ucm/sparse/kvcomp/configs/kvcomp_deepseek_r1_awq_config.json"
35+ elif "qwen3" in model and "32b" in model :
36+ rel = "ucm/sparse/kvcomp/configs/kvcomp_qwen3_32B_config.json"
37+ else :
38+ raise ValueError (f"[KvCompOnDevice] Unsupported model for kvcomp: { model } " )
39+
40+ logger .info ("[KvComp] target relative path: %s" , rel )
41+
42+ cur = Path (__file__ ).resolve ()
43+ repo = cur
44+ for depth in range (30 ):
45+ if (
46+ (repo / "pyproject.toml" ).is_file ()
47+ or (repo / "setup.cfg" ).is_file ()
48+ or (repo / ".git" ).exists ()
49+ ):
50+
51+ p = repo / rel
52+ logger .info ("[KvComp] repo root detected at depth=%d: %s" , depth , repo )
53+ if p .is_file ():
54+ logger .info ("[KvComp] config loaded from SOURCE tree: %s" , p )
55+ return str (p )
56+ logger .warning ("[KvComp] repo root found but config missing: %s" , p )
57+ break
58+ if repo .parent == repo :
59+ logger .debug ("[KvComp] reached filesystem root, stop searching" )
60+ break
61+
62+ repo = repo .parent
63+
64+ sub = rel [len ("ucm/" ) :] if rel .startswith ("ucm/" ) else rel
65+ res = resources .files ("ucm" ).joinpath (* sub .split ("/" ))
66+
67+ with resources .as_file (res ) as p :
68+ logger .info ("[KvComp] config loaded from PACKAGE resource (wheel): %s" , p )
69+ return str (p )
70+
71+
2772class KvCompOnDevice (UcmSparseBase ):
2873 # handle batch
2974 def __init__ (self , vllm_config : VllmConfig , role : UcmSparseRole ):
@@ -39,16 +84,7 @@ def __init__(self, vllm_config: VllmConfig, role: UcmSparseRole):
3984 )
4085 self .block_size = vllm_config .cache_config .block_size
4186
42- self .kvcompOnDevice_cfg = (
43- Config (vllm_config .kv_transfer_config )
44- .get_config ()
45- .get ("ucm_sparse_config" )
46- .get ("GSA" )
47- )
48-
49- kvcompOnDevice_config_path = self .kvcompOnDevice_cfg [
50- "kvcompOnDevice_config_path"
51- ]
87+ kvcompOnDevice_config_path = kvcomp_config_path_for_model (vllm_config )
5288 self .kvcompOnDevice_config = KvCompConfig .from_json (kvcompOnDevice_config_path )
5389 logger .info (f"read kvcomp config file : { kvcompOnDevice_config_path } " )
5490 self .hash_topk_tokens = self .kvcompOnDevice_config .vllm_hash_attention_topk
0 commit comments