CartridgeConfig
PromptLearningConfigCartridgeConfig(task_type: TaskType | str | None = None, peft_type: PeftType | str | None = None, auto_mapping: dict[str, str] | None = None, peft_version: str | None = None, base_model_name_or_path: str | None = None, revision: str | None = None, inference_mode: bool = False, num_virtual_tokens: int | None = None, token_dim: int | None = None, num_transformer_submodules: int | None = None, num_attention_heads: int | None = None, num_layers: int | None = None, modules_to_save: list[str] | None = None, num_frozen_tokens: int = 1)How lucid.peft.get_peft_model adapts a model with Cartridge.
A Cartridge (Eyuboglu et al., 2025, arXiv:2506.06266 — Cartridges: Lightweight and general-purpose long
context representations via self-study) is a trained key-value cache. num_virtual_tokens learned
key/value pairs per attention layer are put in front of the model's cache, in place of a long document the
model would otherwise read at every query. The first num_frozen_tokens of them stay fixed; the paper
recommends freezing one as an attention sink. The cache is laid out as in prefix tuning, and starting it from
text or from a past cache is a separate step.
Parameters
lucid.peft.PeftConfig.lucid.peft.PeftConfig.lucid.peft.PeftConfig.lucid.peft.PeftConfig.lucid.peft.PeftConfig.lucid.peft.PeftConfig.lucid.peft.PeftConfig.num_ virtual_ tokensint | None= Nonelucid.peft.PromptLearningConfig.token_ dimint | None= Nonelucid.peft.PromptLearningConfig.num_ transformer_ submodulesint | None= Nonelucid.peft.PromptLearningConfig.num_ attention_ headsint | None= Nonelucid.peft.PromptLearningConfig.num_ layersint | None= Nonelucid.peft.PromptLearningConfig.modules_ to_ saveint | None= Nonelucid.peft.PromptLearningConfig.num_ frozen_ tokensint= 1num_virtual_tokens.Notes
Every field of the reference library's config is here, in its order and with its defaults, and
the config is read and written as adapter_config.json. A field of the wrong type raises
TypeError. ValueError is raised when num_frozen_tokens is negative or exceeds
num_virtual_tokens. The cache starts as a random draw; initialize_kv_prefix_from_text and
initialize_kv_prefix_from_past_key_values (lucid.peft) replace it with the keys and values
of a document the model has read.
The cache is as wide as the model's attention: token_dim left out is num_attention_heads
times the head width the model's config declares (head_dim, num_key_value_heads), which is
the hidden size for most models and not for one whose heads are wider (T5-3B). Where the model
declares them, a token_dim or num_attention_heads that disagrees raises ValueError.
Examples
>>> from lucid.peft import CartridgeConfig
>>> config = CartridgeConfig(num_virtual_tokens=16, num_frozen_tokens=2, task_type='CAUSAL_LM')
>>> config.num_virtual_tokens, config.num_frozen_tokens, config.peft_type
(16, 2, <PeftType.CARTRIDGE: 'CARTRIDGE'>)
>>> CartridgeConfig.from_dict(config.to_dict()) == config
True