This code identifies relation-specific neurons that focus
on relations rather than entities in decoder-only LLMs (specifically on Llama-2 models).
With respect to their capacity for encoding relation information, we give evidence for the following three properties of relation-specific neurons.
(i) Neuron cumulativity.
The neurons for Self-Conditioning Pre-Trained Language Models
and On the Multilingual Ability of Decoder-based Pre-trained Language Models: Finding and Controlling Language-Specific Neurons
.
# Ground Truth Texts
assets/Language/sense/
# Model-Generated Texts
outputs/
The requirements are listed in frozen_requirements.txt.
The code has been tested using Python 3.8
.
Run the following for installation:
cd <path_to_this_project>
conda create -n relation_neuron python=3.8
conda activate relation_neuron
pip install -U pip wheel
pip install -r frozen_requirements.txt
python -c "import nltk; nltk.download('punkt')"
Model Support:
- llama-2-7b-hf
- llama-2-13b-hf
Run the following script to generate prompts for the neuron identification. You can first modify the generate_prompts.py
script on your own needs. The script will generate the identification prompt set (for identification) and test prompt set (for evaluation) for each relation
python scripts/generate_prompts.py
Run the following script to create a dataset and collect responses from a model.
The script will first create the real dataset (a json file) for a single relation
Taking company_ceo relation as an example:
bash main_prod_env.sh "llama-2-7b compute_responses Relation company_ceo 6000 on_p50 expertise_limited_6000_both"
The responses will be saved inside mit_output/llama-2-7b-hf/sense/company_ceo/responses
.
The expertise score of a neuron is defined as the Average Precision (AP) achieved when its responses are considered prediction scores for the classification problem (positive examples and negative examples).
Taking company_ceo relation as an example:
bash main_prod_env.sh "llama-2-7b compute_expertise Relation company_ceo 6000 on_p50 expertise_limited_6000_both"
The expertise results are saved as a CSV file in mid_output/llama-2-7b-hf/sense/company_ceo/expertise
.
Run the following script to to limit expertise to only Top-k neurons (sorted by the AP values) in FFNs.
Taking company_ceo relation as an example:
python scripts/make_limited_expert_exe.py --model-name llama-2-7b-hf --language company_ceo --num-units 6000 --neuron_type mlp
We measure the effect of selectively deactivating candidate neurons specific
to relation
python generate_conditioned.py --model-name llama-2-7b-hf --compute-all --neuron_group top --forcing zero --show-overlap --neuron_type mlp
Run the following script to find common neurons across all relations and visualizing the overlap.
python compare_neurons.py --compute_all --neuron_group top
python analysis/accuracy_analyisis.py
python analysis/draw_accuracy_table.py
python scripts/plot_accuracy.py --model_name llama-2-7b-hf --num_units 3000 --neuron_type mlp
Taking de (German) as an example:
python generate_conditioned_multilingual.py --model-name llama-2-13b-hf --compute-all --neuron_group top --forcing zero --show-overlap --neuron_type mlp --language de
If you find our code or models useful for your research, please consider citing:
@misc{liu2025relationspecificneuronslargelanguage,
title={On Relation-Specific Neurons in Large Language Models},
author={Yihong Liu and Runsheng Chen and Lea Hirlimann and Ahmad Dawar Hakimi and Mingyang Wang and Amir Hossein Kargaran and Sascha Rothe and François Yvon and Hinrich Schütze},
year={2025},
eprint={2502.17355},
archivePrefix={arXiv},
primaryClass={cs.CL},
url={https://arxiv.org/abs/2502.17355},
}