Skip to content
data

CartridgeConfig

extendsPromptLearningConfig
CartridgeConfig(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

task_typeTaskType | str | None= None
As in lucid.peft.PeftConfig.
peft_typeTaskType | str | None= None
As in lucid.peft.PeftConfig.
auto_mappingTaskType | str | None= None
As in lucid.peft.PeftConfig.
peft_versionTaskType | str | None= None
As in lucid.peft.PeftConfig.
base_model_name_or_pathTaskType | str | None= None
As in lucid.peft.PeftConfig.
revisionTaskType | str | None= None
As in lucid.peft.PeftConfig.
inference_modeTaskType | str | None= None
As in lucid.peft.PeftConfig.
num_virtual_tokensint | None= None
As in lucid.peft.PromptLearningConfig.
token_dimint | None= None
As in lucid.peft.PromptLearningConfig.
num_transformer_submodulesint | None= None
As in lucid.peft.PromptLearningConfig.
num_attention_headsint | None= None
As in lucid.peft.PromptLearningConfig.
num_layersint | None= None
As in lucid.peft.PromptLearningConfig.
modules_to_saveint | None= None
As in lucid.peft.PromptLearningConfig.
num_frozen_tokensint= 1
Initial virtual tokens that receive no gradient; not more than num_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

Used by 3