eliahuhorwitz
Spectral-DeTuning
Python

Official PyTorch Implementation for the "Recovering the Pre-Fine-Tuning Weights of Generative Models" paper (ICML 2024).

Last updated Apr 27, 2026
86
Stars
4
Forks
0
Issues
0
Stars/day
Attention Score
20
Language breakdown
Python 96.5%
Shell 3.5%
โ–ธ Files click to expand
README

Recovering the Pre-Fine-Tuning Weights of Generative Models (ICML 2024)

Official PyTorch Implementation for the "Recovering the Pre-Fine-Tuning Weights of Generative Models" paper (ICML 2024).

๐ŸŒ Project | ๐Ÿ“ƒ Paper | ๐Ÿค— Dataset

Pre-Fine-Tuning Weight Recovery Attack Setting: We uncover a vulnerability in LoRA fine-tuned models wherein an attacker is able to undo the fine-tuning process and recover the weights of the original pre-trained model. The setting for the vulnerability is as follows:

(a) The attacker only has access to n different LoRA fine-tuned models.

(b) The attacker assumes that all n models originated from the same source model.

(c) Using only the n visible models, the attacker attempts to recover the original source model.

Our method, Spectral DeTuning, can perform the attack in an unsupervised and data-free manner on real models such as Stable Diffusion and Mistral. For simplicity, we illustrate the attack on a single layer, in reality, the attack is carried out independently on all the fine-tuned layers.

Note: The attacker has no access to the low-rank decomposition of the fine-tuned models.


Recovering the Pre-Fine-Tuning Weights of Generative Models
Eliahu Horwitz, Jonathan Kahana, Yedid Hoshen
https://arxiv.org/abs/2402.10208
> >Abstract: The dominant paradigm in generative modeling consists of two steps:
i) pre-training on a large-scale but unsafe dataset, ii) aligning the pre-trained model with human values via fine-tuning.
This practice is considered safe, as no current method can recover the unsafe, pre-fine-tuning model weights.
In this paper, we demonstrate that this assumption is often false. Concretely, we present Spectral DeTuning,
a method that can recover the weights of the pre-fine-tuning model using a few low-rank (LoRA) fine-tuned models.
In contrast to previous attacks that attempt to recover pre-fine-tuning capabilities,
our method aims to recover the exact pre-fine-tuning weights.
Our approach exploits this new vulnerability against large-scale models such as a personalized Stable Diffusion and an aligned Mistral.

Project Structure

This project consists of:
  • spectral_detuning.py - main file for recovering the Pre-FT weights using Spectral DeTuning.
  • distributedspectraldetuning.py - Distributing Spectral DeTuning across multiple CPU cores of a single machine.
  • increaserankonplateauscheduler.py - rank scheduler class.
  • slurm - Examples for distributing Spectral DeTuning across a slurm cluster.
  • lowra_bench - Scripts for running inference and evaluation of the recovered weights.

Installation

  • Clone the repo:
git clone https://github.com/eliahuhorwitz/spectral_detuning.git
cd spectral_detuning
  • Create a new environment and install the libraries:
python3 -m venv spectraldetuningvenv
source spectraldetuningvenv/bin/activate
pip install -r requirements.txt

Running Spectral DeTuning for Pre-Fine-Tuning Weight Recovery

The spectral_detuning.py script is the main script in this project. It handles the downloading of the LoWRA Bench dataset that is hosted on Hugging Face.

Below are examples for running runs Spectral DeTuning for Pre-FT weight recovery on the LoWRA Bench dataset subset using different distribution strategies.

Single GPU Execution

These use a single GPU to recover all the layers one by one sequentially.

ViT

python spectraldetuning.py --subset="vit" --outputpath="./recovered_weights/vit/" \
--startlayer=0 --nlayerstorecover=-1 --schedendrank=16 --n_loras=5
[!TIP]
ViT contains 24 layers to recover and can be recovered sequentially in a few minutes on a desktop grade GPU.

Stable Diffusion

python spectral_detuning.py --subset="stable-diffusion-1.5" \ 
--outputpath="./recoveredweights/stablediffusion15/" --start_layer=0 \
--nlayerstorecover=-1 --schedendrank=32 --nloras=5
[!IMPORTANT]
Stable Diffusion contains 264 layers to recover. See below for a faster option.

Mistral SFT

python spectral_detuning.py --subset="mistral-7b-v0.1-sft" \
--outputpath="./recoveredweights/mistral7b01sft/" --start_layer=0 \
--nlayerstorecover=-1 --schedendrank=64 --nloras=12 --n_iters=1000

Mistral DPO

python spectral_detuning.py --subset="mistral-7b-v0.1-dpo" \
--outputpath="./recoveredweights/mistral7b01dpo/" --start_layer=0 \
--nlayerstorecover=-1 --schedendrank=64 --nloras=8 --n_iters=1000
[!IMPORTANT]
Mistral contains 128 layers to recover, some of them are of high dimensions (up to 4096x4096), see below for a faster option.

Distributed Multiprocess CPU Execution

Since Spectral DeTuning does not require gradients or running inference on the model, it can run quickly even on a CPU. Below are options for distributing Spectral DeTuning across the CPU cores of a single machine using multiple processes.

To run using this strategy, run distributedspectraldetuning.py with the same arguments as above. To control the number of CPU cores to distribute across use the --n_cpus argument, set --n_cpus=-1 to use all available core.

[!TIP]
ViT contains 24 layers to recover and can be recovered in minutes when distributed across desktop CPU cores.

Distributed Execution on a Compute Cluster

In cases where the model has many layers (e.g., Stable Diffusion and Mistral), it is recommended to distribute the recovery across a compute cluster (GPU or CPU). We provide example slurm scripts under the slurm dir.

The main difference is the --nlayersto_recover argument which controls how many layers each machine will recover.

[!TIP]
Spectral DeTuning can recover a single layer of a large model (e.g. Mistral-7B)
in under 5 minutes on a single desktop GPU (e.g. RTX2080).
The recovery speed of the entire model is a function of the number of machines in your cluster.

Using the Recovered Pre-Fine-Tuning Weights

To run inference on the Pre-FT recovered weights use the following scripts:

ViT:

python lowrabench/inference/vitinference.py --inputpath="./recoveredweights/vit/"

Stable Diffusion:

python lowrabench/inference/stablediffusion_inference.py \
--inputpath="./recoveredweights/stable_diffusion/"

Mistral SFT:

python lowrabench/inference/mistralinference.py \
--inputpath="./recoveredweights/mistral7b01sft/" --subset="mistral-7b-v0.1-sft"

Mistral DPO:

python lowrabench/inference/mistralinference.py \
--inputpath="./recoveredweights/mistral7b01dpo/" --subset="mistral-7b-v0.1-dpo"

Using a Custom Dataset of Fine-tuned LoRAs and Pre-FT Models

Coming soon...
  • [ ] Preprocessing scripts for constructing a LoRA dataset similar to the LoWRA Bench one.

Citation

If you find this useful for your research, please use the following.
@inproceedings{horwitz2024recovering,
  title={Recovering the Pre-Fine-Tuning Weights of Generative Models},
  author={Horwitz, Eliahu and Kahana, Jonathan and Hoshen, Yedid},
  booktitle={International Conference on Machine Learning},
  pages={18882--18904},
  year={2024},
  organization={PMLR}
}

Acknowledgments

๐Ÿ”— More in this category

ยฉ 2026 GitRepoTrend ยท eliahuhorwitz/Spectral-DeTuning ยท Updated daily from GitHub