- Replace CacheView/ContiguousCache/PageCache with SGLang-inspired design: KVStorage (flat token-level NHD buffers [n_layers, size, H, D]), ReqToTokenPool (index table [req_idx, pos] -> token_slot), Allocator + PrefixCache (slot allocation with LRU and prefix sharing) - Add KVCache as pure dataclass passed to model: k_buffer, v_buffer, req_to_token, req_pool_indices, seq_lens, out_cache_loc - PagePool orchestrates all three layers, supports contiguous mode (pre-allocated per-request blocks, default) and paged mode (page_size=1 or >1 with dynamic allocation and prefix caching) - Attention layers now do raw buffer indexing instead of opaque write/gather method calls on CacheView objects - Update executor.bind_tasks signature: seq_lens list + start_pos - Rename paged_cache -> kv_cache throughout model/ and inference/
96 lines
2.2 KiB
Python
96 lines
2.2 KiB
Python
"""Inference module for continuous batching.
|
|
|
|
Layers:
|
|
- core/: Core inference loop (cache, executor, scheduler, task)
|
|
- api/: HTTP orchestration (ProtocolHandler, server)
|
|
- protocols/: Response builders (OpenAI, Anthropic)
|
|
- transport/: SSE transport utilities
|
|
- engine.py: Facade (InferenceEngine), Value Object (GenerationRequest)
|
|
- sample.py: Strategy pattern (TemperatureStrategy, TopKStrategy, TopPStrategy, FrequencyPenaltyStrategy)
|
|
"""
|
|
|
|
from astrai.inference.api import (
|
|
AnthropicMessage,
|
|
BaseToolParser,
|
|
ChatCompletionRequest,
|
|
ChatMessage,
|
|
FunctionDef,
|
|
GenContext,
|
|
MessagesRequest,
|
|
ProtocolHandler,
|
|
SimpleJsonToolParser,
|
|
StopChecker,
|
|
ToolDef,
|
|
ToolParserFactory,
|
|
get_app,
|
|
run_server,
|
|
)
|
|
from astrai.inference.api.anthropic import AnthropicResponseBuilder
|
|
from astrai.inference.api.openai import OpenAIResponseBuilder
|
|
from astrai.inference.core import (
|
|
STOP,
|
|
Allocator,
|
|
Executor,
|
|
InferenceScheduler,
|
|
KVCache,
|
|
KVStorage,
|
|
PagePool,
|
|
PrefixCache,
|
|
ReqToTokenPool,
|
|
Task,
|
|
TaskManager,
|
|
TaskStatus,
|
|
page_hash,
|
|
)
|
|
from astrai.inference.engine import GenerationRequest, InferenceEngine
|
|
from astrai.inference.sample import (
|
|
BaseSamplingStrategy,
|
|
FrequencyPenaltyStrategy,
|
|
SamplingPipeline,
|
|
TemperatureStrategy,
|
|
TopKStrategy,
|
|
TopPStrategy,
|
|
sample,
|
|
)
|
|
|
|
__all__ = [
|
|
"InferenceEngine",
|
|
"GenerationRequest",
|
|
"InferenceScheduler",
|
|
"Executor",
|
|
"STOP",
|
|
"Task",
|
|
"TaskManager",
|
|
"TaskStatus",
|
|
"Allocator",
|
|
"KVCache",
|
|
"KVStorage",
|
|
"PagePool",
|
|
"PrefixCache",
|
|
"ReqToTokenPool",
|
|
"page_hash",
|
|
"sample",
|
|
"BaseSamplingStrategy",
|
|
"TemperatureStrategy",
|
|
"TopKStrategy",
|
|
"TopPStrategy",
|
|
"FrequencyPenaltyStrategy",
|
|
"SamplingPipeline",
|
|
"ProtocolHandler",
|
|
"StopChecker",
|
|
"GenContext",
|
|
"BaseToolParser",
|
|
"SimpleJsonToolParser",
|
|
"ToolParserFactory",
|
|
"OpenAIResponseBuilder",
|
|
"AnthropicResponseBuilder",
|
|
"ChatMessage",
|
|
"ChatCompletionRequest",
|
|
"FunctionDef",
|
|
"ToolDef",
|
|
"AnthropicMessage",
|
|
"MessagesRequest",
|
|
"get_app",
|
|
"run_server",
|
|
]
|