kvcache

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2026 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInternalTokenizationDisabled = fmt.Errorf(
	"internal tokenization not configured: tokenize externally and call ScoreTokens / ComputeBlockKeysFromTokens")

ErrInternalTokenizationDisabled is returned by the deprecated prompt-string entry points when the indexer was constructed without TokenizersPoolConfig. Callers can inspect it via errors.Is to distinguish missing-pool from other failures.

Functions

This section is empty.

Types

type Config

type Config struct {
	KVBlockIndexConfig  *kvblock.IndexConfig    `json:"kvBlockIndexConfig"`
	KVBlockScorerConfig *KVBlockScorerConfig    // not exported
	BackendConfigs      []*KVCacheBackendConfig `json:"kvCacheBackendConfigs"`

	// TokenizersPoolConfig configures the in-process tokenization pool.
	// Leaving it nil disables the pool; the prompt-string entry points then
	// return an error.
	//
	// Deprecated: tokenize externally and call Indexer.ScoreTokens.
	TokenizersPoolConfig *tokenization.Config `json:"tokenizersPoolConfig,omitempty"`
}

Config holds the configuration for the Indexer module. The configuration cover the different components found in the Indexer module.

func NewDefaultConfig

func NewDefaultConfig() (*Config, error)

NewDefaultConfig returns a default configuration for the Indexer module. TokenizersPoolConfig is left nil; populate it only if the deprecated prompt-string APIs are needed.

type Indexer

type Indexer struct {
	// contains filtered or unexported fields
}

Indexer is a concrete implementation of the KVCacheIndex interface.

func NewKVCacheIndexer

func NewKVCacheIndexer(ctx context.Context, config *Config, tokenProcessor kvblock.TokenProcessor) (*Indexer, error)

NewKVCacheIndexer creates a KVCacheIndex given a Config. When config.TokenizersPoolConfig is nil, the indexer accepts only the tokens-in API (Indexer.ScoreTokens) and the prompt-string entry points return an error.

func (*Indexer) ComputeBlockKeys deprecated added in v0.7.0

func (k *Indexer) ComputeBlockKeys(ctx context.Context, renderReq *types.RenderChatRequest, prompt, modelName string,
) ([]kvblock.BlockHash, error)

ComputeBlockKeys computes the KV-block keys for a given prompt and model name.

Deprecated: use ComputeBlockKeysFromTokens.

func (*Indexer) ComputeBlockKeysFromTokens added in v0.8.0

func (k *Indexer) ComputeBlockKeysFromTokens(ctx context.Context, tokens []uint32, modelName string,
	extraFeatures []*kvblock.BlockExtraFeatures,
) ([]kvblock.BlockHash, error)

ComputeBlockKeysFromTokens computes the KV-block keys for a pre-tokenized prompt. Callers tokenize and truncate externally. extraFeatures provides per-block multimodal data that taints the hash; nil means text-only.

func (*Indexer) GetPodScores deprecated

func (k *Indexer) GetPodScores(ctx context.Context, renderReq *types.RenderChatRequest, prompt, modelName string,
	podIdentifiers []string,
) (map[string]float64, error)

GetPodScores retrieves the pod scores for a given prompt and model name. A pod identifier should be its address. An empty podIdentifiers set means all pods are considered.

Deprecated: use ScoreTokens.

func (*Indexer) KVBlockIndex

func (k *Indexer) KVBlockIndex() kvblock.Index

KVBlockIndex returns the kvblock.Index used by the Indexer.

func (*Indexer) Run

func (k *Indexer) Run(ctx context.Context)

Run starts the indexer. Blocks until ctx is cancelled.

func (*Indexer) ScoreTokens added in v0.7.0

func (k *Indexer) ScoreTokens(
	ctx context.Context,
	tokens []uint32,
	modelName string,
	podIdentifiers []string,
	extraFeatures []*kvblock.BlockExtraFeatures,
) (map[string]float64, error)

ScoreTokens computes pod scores for the given tokens and model. It converts tokens into KV block keys, looks up which pods hold matching blocks in the index, and scores each pod based on cache hits.

extraFeatures provides per-block multimodal data that taints the hash; nil means text-only. podIdentifiers limits scoring to the given pod addresses. If empty, all pods are considered.

func (*Indexer) SetTokenizer deprecated

func (k *Indexer) SetTokenizer(tokenizer tokenization.Tokenizer, modelName string)

SetTokenizer overrides the in-process tokenizer. No-op when the pool is disabled.

Deprecated: tied to the in-process tokenization pool.

type KVBlockScorer

type KVBlockScorer interface {
	// Strategy returns the scoring strategy type.
	Strategy() KVScoringStrategy
	// Score scores the blocks based on the scoring strategy.
	// It returns a map of pod names to their scores.
	Score(ctx context.Context, keys []kvblock.BlockHash,
		keyToPods map[kvblock.BlockHash][]kvblock.PodEntry) (map[string]float64, error)
}

KVBlockScorer defines the interface for implementing a KV block scoring strategy.

func NewKVBlockScorer

func NewKVBlockScorer(config *KVBlockScorerConfig) (KVBlockScorer, error)

NewKVBlockScorer creates a new KVBlockScorer based on the provided strategy.

func NewTracedScorer added in v0.5.1

func NewTracedScorer(next KVBlockScorer) KVBlockScorer

NewTracedScorer wraps a KVBlockScorer and emits OpenTelemetry traces for Score operations. This encapsulates all tracing logic for the KVBlockScorer interface.

type KVBlockScorerConfig

type KVBlockScorerConfig struct {
	ScoringStrategy KVScoringStrategy
	BackendConfigs  []*KVCacheBackendConfig `json:"backendConfigs"`
}

KVBlockScorerConfig holds the configuration for the KVBlockScorer.

func DefaultKVBlockScorerConfig

func DefaultKVBlockScorerConfig() *KVBlockScorerConfig

DefaultKVBlockScorerConfig returns the default configuration for the KVBlockScorer.

type KVCacheBackendConfig

type KVCacheBackendConfig struct {
	// Name is the identifier for this medium (e.g., "gpu", "cpu", "disk")
	Name string `json:"name"`
	// Weight is the scoring weight for blocks stored on this medium
	Weight float64 `json:"weight"`
}

func DefaultKVCacheBackendConfig

func DefaultKVCacheBackendConfig() []*KVCacheBackendConfig

type KVScoringStrategy

type KVScoringStrategy string

KVScoringStrategy defines the strategy used to score pods for KV cache block reuse.

const (
	// LongestPrefixMatch Score by longest consecutive match from start.
	LongestPrefixMatch KVScoringStrategy = "LongestPrefix"
)

type LongestPrefixScorer

type LongestPrefixScorer struct {
	// mediumWeights maps medium/device tier names to their scoring weights
	MediumWeights map[string]float64
}

LongestPrefixScorer scores based on longest consecutive block matches count starting from block 0.

func (*LongestPrefixScorer) Score

func (s *LongestPrefixScorer) Score(
	_ context.Context,
	keys []kvblock.BlockHash,
	keyToPods map[kvblock.BlockHash][]kvblock.PodEntry,
) (map[string]float64, error)

Score implements the longest prefix scoring logic with weighted sum based on BackendConfig.

func (*LongestPrefixScorer) Strategy

func (s *LongestPrefixScorer) Strategy() KVScoringStrategy

Strategy returns the strategy type: LongestPrefixMatch.

type TokenizersPool added in v0.7.0

type TokenizersPool interface {
	Tokenize(renderReq *types.RenderChatRequest, prompt string) ([]uint32, *tokenization.MultiModalFeatures)
	Run(ctx context.Context)
	SetTokenizer(tokenizer tokenization.Tokenizer, modelName string)
}

TokenizersPool abstracts the tokenization pool for testability/mocking.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL