Text-to-LoRA

repository·main·Indexed 23 days ago

https://github.com/sakanaai/text-to-lora

Reference implementation for 'Text-to-LoRA: Instant Transformer Adaption' (ICML 2025). This library allows users to generate LoRA adapters from natural language task descriptions. It includes tools for generating LoRAs via CLI or Web UI, evaluating performance using run_eval.py, and performing SFT or reconstruction training. The package also includes the fishfarm library for managing model interfaces, chat templates (LLAMA2, LLAMA3, ALPACA), and logging.

Tokens
19.1K
Snippets
27
Records
87
Agent score
76%

What's inside text-to-lora

  1. Generate a LoRA using generate_lora.py

    main

    To generate a LoRA for a specific task, use the generate_lora.py script via uv run. You must provide the T2L directory and a task description.

    Command Syntax: uv run generate_lora.py {T2l_DIRECTORY} {TASK_DESCRIPTION}

  2. Evaluate LoRAs with run_eval.py

    main

    To evaluate generated LoRAs against a base model, use the scripts/run_eval.py script. This requires specifying the base model directory, the directories containing the LoRAs, and the tasks to evaluate.

    Arguments:

    • --model-dir: The directory of the base model.
    • --lora-dirs: The directories containing the LoRAs to evaluate.
    • --save-results: Flag to save the evaluation results.
    • --tasks: The specific tasks to run evaluation for.
  3. Perform Reconstruction Training

    main

    Reconstruction training involves two steps:

    1. Train 'oracle' adapters for all tasks using ./scripts/train_lora_baselines.sh.
    2. Train T2L to reconstruct these oracle adapters using scripts/train_hyper_recon.py.
    # Train oracle adapters
    ./scripts/train_lora_baselines.sh
    
    # Train T2L via reconstruction training
    WANDB_MODE=disabled uv run python scripts/train_hyper_recon.py configs/hyper_lora_decontam_lol_tasks.yaml \
    --model_dir=mistralai/Mistral-7B-Instruct-v0.2/ \
    --emb_model=Alibaba-NLP/gte-large-en-v1.5 \
    --warmup_frac=0.1 --lr=1e-3 --epochs=10000 \
    --n_train_ds=479 --exp_setup=hyper_lora --encoder_type=linear \
    --pred_z_score=True --n_descs_per_ds=128 --n_embs_per_sampled_task=1 \
    --n_tasks_per_batch=4 --factorized=False --delta_w_scaling=10000 --shared_AB_head=True
  4. Perform SFT Training

    main

    SFT (Supervised Fine-Tuning) training for T2L can be performed using provided shell scripts. For asynchronous validation, run watcher.py in a separate process to monitor checkpoints and evaluate them as they are saved.

    Note: Training takes approximately 5 days on a single H100 GPU. If Hugging Face dataset connections fail due to high volume, retry the script until all ~500 datasets are cached locally.

    # start a watcher process for async eval
    uv run watcher.py
    
    # T2L training (run one for each GPU)
    ./scripts/train_t2l_mistral.sh
    ./scripts/train_t2l_llama.sh
    ./scripts/train_t2l_gemma.sh
  5. Install Text-to-LoRA (T2L) dependencies

    main

    To install the T2L environment, ensure you have uv installed. The installation process involves cloning the repository, setting up a Python 3.10 virtual environment, syncing dependencies, and installing specific wheels for flash-attention and the fishfarm package.

    Note: You may need to modify the flash-attention wheel URL to match your specific hardware/CUDA configuration.

    git clone https://github.com/SakanaAI/text-to-lora.git
    cd text-to-lora
    # make sure you have `uv` installed
    # (see https://docs.astral.sh/uv/getting-started/installation/)
    uv self update
    uv venv --python 3.10 --seed
    uv sync
    # we use the following wheel for installation
    # you might have to change the wheel to be compatible with your hardware
    uv pip install https://github.com/Dao-AILab/flash-attention/releases/download/v2.6.3/flash_attn-2.6.3+cu123torch2.3cxx11abiFALSE-cp310-cp310-linux_x86_64.whl
    uv pip install src/fishfarm
  6. Download T2L checkpoints

    main

    Before running any demos, you must download the trained T2L checkpoints from Hugging Face.

    Requirements:

    • You need a GPU with >16GB VRAM to handle both the base model and the T2L model simultaneously.
    • You must be logged into Hugging Face via huggingface-cli.
    uv run huggingface-cli login
    uv run huggingface-cli download SakanaAI/text-to-lora --local-dir . --include "trained_t2l/*"
  7. How fishfarm logging works

    main

    The fishfarm library manages a central root logger named fishfarm. When you use the library, it automatically configures this root logger with a default formatter and a StreamHandler pointing to sys.stderr.

    To ensure your module's logs are captured by the fishfarm configuration (level, formatting, etc.), you should initialize your logger using get_logger(name) where name is the module's name (e.g., __name__), provided your module is part of the fishfarm package hierarchy (i.e., its name starts with fishfarm.).

  8. How TaskEncoders work

    main

    The HyperModulator uses different TaskEncoder implementations to transform raw task embeddings into a latent space used by the hypernetwork.

    Available encoders:

    • TaskEncoder (encoder_type='linear'): A simple MLP with LayerNorm.
    • DiscreteOneHotTaskEncoder (encoder_type='discrete'): Uses a categorical distribution to sample one-hot encodings during training, providing a discrete latent space.
    • SoftmaxTaskEncoder (encoder_type='softmax'): Uses a softmax over a learned codebook to produce a weighted sum of embeddings.
    • VQTaskEncoder (encoder_type='vq'): Implements Vector Quantization (VQ) with an exponential moving average (EMA) update for the codebook, similar to VQ-VAE.
  9. Understand the OS Interaction prompt and action format

    main

    The OS Interaction task expects models to follow a specific reasoning and action pattern. The model is prompted to act as a person interacting with a Linux (Ubuntu) operating system.

    For every turn, the model must follow this structure:

    1. Think: A description of the intended action.
    2. Act: One of three specific actions:
      • bash: Execute code. Format: `Act: bash\n\n```bash\n# code here\n````
      • finish: Task completed. Format: Act: finish
      • answer(result): Provide the final answer. Format: Act: answer(Your answer)

    If the OS output is too long, it will be truncated. The model is expected to handle truncation (e.g., by using scripts like wc -l instead of listing all files) autonomously.

  10. How flexible boolean matching works

    main

    The get_binary_accuracy_flex function allows for non-exact matching of boolean concepts. It uses get_bool_value_from_text to interpret the following strings as boolean values:

    • True: "1", "yes", "true", "positive" (case-insensitive)
    • False: "0", "no", "false", "negative" (case-insensitive)

    If the function cannot extract a meaningful boolean from either the generated or target text, it returns 0.

  11. How to parse configuration using YAML and CLI arguments

    main

    The ArgumentParser (an extension of HfArgumentParser) allows you to load configuration from a YAML file and override specific values using command-line arguments.

    Supported parsing patterns:

    1. Single YAML file: Pass the path as the only argument.
    2. YAML file + CLI arguments: Pass the YAML path followed by arguments in --key=value format.
    3. --config flag: Use the --config path/to/config.yaml flag followed by other arguments.
    4. CLI arguments only: Standard command-line parsing if no YAML file is provided.

    When overriding via CLI, the parser automatically casts values to the types defined in the dataclass (e.g., int, float, bool, List[str], or dict).