Text-to-LoRA:自动生成任务特定的模型适配器
超网络技术自动生成任务特定 LoRA,无需手工微调即可快速适配 LLM。对 AI 工程化有创新参考。
超网络技术自动生成任务特定 LoRA,无需手工微调即可快速适配 LLM。对 AI 工程化有创新参考。
如果尚未安装 uv,请先安装(参见 https://docs.astral.sh/uv/getting-started/installation/)。
安装 uv 后,运行以下命令安装依赖。
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
⚠️ 在运行任何 demo 之前,你需要先下载 checkpoint。⚠️
⚠️ 要同时运行这两个模型并执行这些 demo,你需要一块显存大于 16GB 的 GPU。⚠️
uv run huggingface-cli login
uv run huggingface-cli download SakanaAI/text-to-lora --local-dir . --include "trained_t2l/*"
这个 demo 会在本地同时运行 Mistral-7B-Instruct-v0.2 和一个 T2L 模型。
# start webui locally
uv run python webui/app.py
这个脚本允许我们根据任务描述生成 LoRA。第一次运行所需时间会更长,因为需要下载并缓存 base model。
# uv run generate_lora.py {T2l_DIRECTORY} {TASK_DESCRIPTION}
# e.g.,
uv run python scripts/generate_lora.py \
trained_t2l/llama_8b_t2l \
"This task challenges your problem-solving abilities through mathematical reasoning. You must carefully read each scenario and systematically work through the data to compute the final outcome."
# You might be able to run T2L w/ `gemma-2-2b-it` with a smaller GPU.
uv run python scripts/generate_lora.py \
trained_t2l/gemma_2b_t2l \
"This task challenges your problem-solving abilities through mathematical reasoning. You must carefully read each scenario and systematically work through the data to compute the final outcome."
我们可以使用上述脚本最后输出的路径,对生成的 LoRA 进行评估。
# uv run python scripts/run_eval.py --model-dir {base_model_dir} \
# --lora-dirs {lora_dirs} --save-results --tasks {tasks}
# e.g.,
uv run python scripts/run_eval.py \
--model-dir meta-llama/Llama-3.1-8B-Instruct \
--lora-dirs {PATH_TO_GENERATED_LORA} \
--save-results --tasks gsm8k
# You might be able to run T2L w/ `gemma-2-2b-it` with a smaller GPU.
uv run python scripts/run_eval.py \
--model-dir google/gemma-2-2b-it \
--lora-dirs {PATH_TO_GENERATED_LORA} \
--save-results --tasks gsm8k
即使输入随机描述,通过 SFT 训练的 T2L 仍然能够生成效果合理的 LoRA。这一结果来自通过 SFT 训练的 T2L,与论文表 5 中使用 reconstruction-trained T2L 得到的结果有所不同。尽管如此,对比 aligned description 与 unaligned description 得到的结果可以发现,aligned LoRA 的整体表现明显更好。训练描述可以在 tasks/ 文件夹中找到,而评估所使用的未见过描述可以在 configs/hyper_lora_decontam_lol_tasks.yaml 中找到。
# start a watcher process for async eval
uv run watcher.py
然后,根据你拥有的 GPU 数量,为每块 GPU 运行以下脚本之一。在单块 H100 GPU 上,每个脚本大约需要运行 5 天。
# T2L training
./scripts/train_t2l_mistral.sh
./scripts/train_t2l_llama.sh
./scripts/train_t2l_gemma.sh
./scripts/train_lora_baselines.sh
接下来,我们训练 T2L 来重建 oracle adapter。
# 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
./scripts/eval_base_models.sh
# example for T2L trained for gemma-2-2b-it
WANDB_MODE=disabled uv run python scripts/eval_hypermod_checkpoint.py --checkpoint_path trained_t2l/gemma_2b_t2l/hypermod.pt --full_eval --use-icl
--use-icl 会在评估 query 中加入 3-shot in-context 示例。
由于原始投稿中使用的特定 package 版本组合存在一个细微的不匹配问题,我们重新训练了一组新的 baseline 和 T2L 模型。
此外,在我们测试的版本与配置组合中,vLLM 在应用 LoRA 时本身具有非确定性。因此,即使固定了初始随机种子,我们仍观察到不同评估运行之间存在一些小幅波动。
不过,在重新训练并收集新的结果之后,T2L 在不同模型家族上仍然始终优于 baseline,证实了我们最初经验分析的有效性。下面的表格展示了使用更新后 package 版本得到的新一组结果。(eval #1 和 eval #2 表示这些结果来自两次不同的评估调用,两次调用使用了相同的随机种子。)
Huggingface datasets server 有时可能会拒绝连接。如果你尚未在本地缓存相关 dataset,这会造成问题。原因可能是我们正在下载大量 dataset(约 500 个)。如果发生这种情况,请持续重试 SFT 训练脚本,直到所有 dataset 都被缓存在本地。缓存完成后,你应该就可以运行 SFT 训练脚本了。
@inproceedings{
charakorn2025texttolora,
title={Text-to-Lo{RA}: Instant Transformer Adaption},
author={Rujikorn Charakorn and Edoardo Cetin and Yujin Tang and Robert Tjarko Lange},
booktitle={Forty-second International Conference on Machine Learning},
year={2025},
url={https://openreview.net/forum?id=zWskCdu3QA}
}