Move hash utils out of hicache_storage to break CUDA import chain (#22214)

This commit is contained in:
Liangsheng Yin
2026-04-06 18:16:40 -07:00
committed by GitHub
parent 5e2b0f860c
commit 49cb7d546e
4 changed files with 32 additions and 34 deletions
@@ -430,7 +430,7 @@ class HiCacheController:
# Rollback-safe init: if creation fails, keep controller state consistent
# for future attach attempts.
self.storage_backend_type = storage_backend
from sglang.srt.mem_cache.hicache_storage import get_hash_str
from sglang.srt.mem_cache.utils import get_hash_str
self.get_hash_str = get_hash_str
self.storage_config = self._generate_storage_config(
@@ -1,4 +1,3 @@
import hashlib
import logging
import os
from abc import ABC, abstractmethod
@@ -14,37 +13,6 @@ from sglang.srt.mem_cache.memory_pool_host import HostKVCache
logger = logging.getLogger(__name__)
def get_hash_str(token_ids: List[int], prior_hash: str = None) -> str:
hasher = hashlib.sha256()
if prior_hash:
hasher.update(bytes.fromhex(prior_hash))
for t in token_ids:
if isinstance(t, tuple):
# EAGLE bigram mode: hash both elements to uniquely identify the bigram
for elem in t:
hasher.update(elem.to_bytes(4, byteorder="little", signed=False))
else:
# Regular mode: single integer token
hasher.update(t.to_bytes(4, byteorder="little", signed=False))
return hasher.hexdigest()
def hash_str_to_int64(hash_str: str) -> int:
"""Convert SHA256 hex string to signed 64-bit integer for events.
Takes first 16 hex characters (64 bits) and converts to signed int64 range.
"""
# Take first 16 hex chars to get 64-bit value
uint64_val = int(hash_str[:16], 16)
# Convert to signed int64 range [-2^63, 2^63-1]
if uint64_val >= 2**63:
return uint64_val - 2**64
return uint64_val
@dataclass
class HiCacheStorageConfig:
tp_rank: int
+1 -1
View File
@@ -62,7 +62,7 @@ from sglang.srt.mem_cache.evict_policy import (
PriorityStrategy,
SLRUStrategy,
)
from sglang.srt.mem_cache.hicache_storage import get_hash_str, hash_str_to_int64
from sglang.srt.mem_cache.utils import get_hash_str, hash_str_to_int64
if TYPE_CHECKING:
from sglang.srt.managers.schedule_batch import Req
+30
View File
@@ -13,6 +13,7 @@
# ==============================================================================
"""Common utilities."""
import hashlib
from typing import Any, List, Optional, Tuple
import torch
@@ -359,3 +360,32 @@ def convert_to_bigram_key(tokens: List[int]) -> List[Tuple[int, int]]:
if len(tokens) < 2:
return []
return [(tokens[i], tokens[i + 1]) for i in range(len(tokens) - 1)]
def get_hash_str(token_ids: List[int], prior_hash: Optional[str] = None) -> str:
hasher = hashlib.sha256()
if prior_hash:
hasher.update(bytes.fromhex(prior_hash))
for t in token_ids:
if isinstance(t, tuple):
# EAGLE bigram mode: hash both elements to uniquely identify the bigram
for elem in t:
hasher.update(elem.to_bytes(4, byteorder="little", signed=False))
else:
# Regular mode: single integer token
hasher.update(t.to_bytes(4, byteorder="little", signed=False))
return hasher.hexdigest()
def hash_str_to_int64(hash_str: str) -> int:
"""Convert SHA256 hex string to signed 64-bit integer for events.
Takes first 16 hex characters (64 bits) and converts to signed int64 range.
"""
uint64_val = int(hash_str[:16], 16)
if uint64_val >= 2**63:
return uint64_val - 2**64
return uint64_val