ó
    >:jµ  ã                   óL   • S SK JrJr  S SKJr  S SKJr  \ " S S\5      5       rg)é    )Ú	dataclassÚfield)ÚPromptLearningConfig)ÚPeftTypec                   óJ   ^ • \ rS rSr% Sr\" SSS0S9r\\S'   U 4S jr	S	r
U =r$ )
ÚCartridgeConfigé   u-  
Configuration for CARTRIDGE, a KV-cache-parameterized prefix adapter.

This is similar to prefix-tuning in how it is served (as `past_key_values`), but it stores the KV cache directly as
trainable parameters instead of learning it via an MLP projection.

Initialization:
    The Cartridges paper discusses multiple initialization options. In PEFT, initialization is a *separate* step
    from constructing the adapter config:

    - **Random KV initialization (paper option 2)**: Create the adapter via `get_peft_model(...)`. The CARTRIDGE
      prompt encoder parameters are randomly initialized by PyTorch.

    - **KV derived from the first tokens of a prompt/corpus (paper option 3)**: Run a no-grad prefill on the *base
      model* and copy the first `num_virtual_tokens` cached KV tokens into the adapter. PEFT provides utilities for
      this (importable from `peft` or from `peft.tuners.cartridge.utils`):

      - `initialize_kv_prefix_from_text(model, tokenizer, text=...)`
      - `initialize_kv_prefix_from_past_key_values(model, past_key_values=...)`

      If you already have a flattened KV-prefix tensor, you can load it directly via the prompt encoderâ€™s
      `load_prompt_embeddings(...)` method.

Args:
    num_frozen_tokens (`int`, defaults to 1):
        Number of *prefix* tokens at the start of the cartridge to keep frozen (no gradients). The Cartridges paper
        recommends freezing the first token as an attention sink for stability (set this to `1`), as many LLMs use
        early tokens as attention sinks and changing them can harm training.
é   ÚhelpzŒNumber of initial virtual tokens to freeze (no gradients). The paper recommends freezing the first token as an attention sink for stability.)ÚdefaultÚmetadataÚnum_frozen_tokensc                 ó8  >• [         TU ]  5         U R                  S:  a  [        SU R                   S35      eU R                  b@  U R                  U R                  :”  a&  [        SU R                   SU R                   S35      e[
        R                  U l        g )Nr   z&`num_frozen_tokens` must be >= 0, got Ú.zK`num_frozen_tokens` must be <= `num_virtual_tokens`, got num_frozen_tokens=z and num_virtual_tokens=)ÚsuperÚ__post_init__r   Ú
ValueErrorÚnum_virtual_tokensr   Ú	CARTRIDGEÚ	peft_type)ÚselfÚ	__class__s    €ÚY/home/mande/repo/quber/.venv/lib/python3.13/site-packages/peft/tuners/cartridge/config.pyr   ÚCartridgeConfig.__post_init__?   sŸ   ø€ Ü‰ÑÔØ×!Ñ! AÓ%ÜÐEÀd×F\ÑF\ÐE]Ð]^Ð_Ó`Ð`ð ×#Ñ#Ñ/°d×6LÑ6LÈt×OfÑOfÓ6fÜØ]Ð^b×^tÑ^tÐ]uð v*Ø*.×*AÑ*AÐ)BÀ!ðEóð ô "×+Ñ+ˆ�ó    )r   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   ÚintÚ__annotations__r   Ú__static_attributes__Ú__classcell__)r   s   @r   r   r      s6   ø‡ ññ< #Øàð<ð
ñÐ�só ÷,ó ,r   r   N)Údataclassesr   r   Úpeft.configr   Ú
peft.utilsr   r   © r   r   Ú<module>r)      s,   ð÷ )å ,Ý ð ô4,Ð*ó 4,ó ñ4,r   