Skip to content
fn

initialize_kv_prefix_from_past_key_values

→Tensor
initialize_kv_prefix_from_past_key_values(model: PeftModel, adapter_name: str | None = None, past_key_values: object, num_virtual_tokens: int | None = None)

Start a Cartridge (or an unprojected prefix) from the keys and values of a cache you have.

The first num_virtual_tokens tokens of the cache of one sequence become the adapter's rows (prompt_embeddings_from_past_key_values), frozen ones first.

Parameters

A model with a CARTRIDGE or PREFIX_TUNING adapter.
adapter_namestr= None
The adapter to initialise; the active one when left out.
past_key_valuesobject
The cache, in a form prompt_embeddings_from_past_key_values takes.
num_virtual_tokensint= None
How many leading tokens to take; the adapter's own count when left out.

Returns

Tensor

The rows that were loaded.

Raises

ValueError
The adapter is neither a Cartridge nor a prefix, is a prefix with prefix_projection, or the cache does not fit (see prompt_embeddings_from_past_key_values); the adapter's rows are untouched then.

Used by 1