Ihildu-Baggins/Key-Path-Identification

Key Path Identification for Resolving Knowledge Conflicts via SAE-based Steering

Python

0

78 commits

updated Jul 12, 2026

See the code

README

Key Path Identification for Resolving Knowledge Conflicts via SAE-based Steering

Overview

image

Sparse autoencoder (SAE)-based steering has been widely used to address knowledge conflicts by guiding LLMs to be more faithful to the contextual knowledge. Existing methods usually perform mass steering, which modifies a large batch of SAE features identified via correlation-based methods. However, due to the inaccurate correlation and the neglected feature interactions, mass steering methods fail to precisely identify the features that play the key role in steering and introduce a large number of redundant ones, which add noise and weaken the steering effects. Our empirical studies reveal that steering only a small subset of the identified features can achieve comparable or even better performance. Motivated by this finding, we propose Key Path Identification (KPI), a novel method that identifies key steering features characterized by strong causal dependencies with both upstream and downstream features. From these features, KPI constructs key paths and steers through less feature modifications. In this way, KPI advances SAE-based steering from quantity-driven to quality-focused, offering a paradigm for more precise and interpretable model editing. Experiments in RAG tasks with knowledge conflicts show that our method improves the accuracy by 18% on average compared to the best baseline of mass steering, effectively filtering redundant features, alleviating side effects and demonstrating the core role of key paths in steering.

Start

Requirements

conda create -n your_env_name python=3.10
conda activate your_env_name
pip install -r requirements.txt

Data Selection

We use two datasets, Macnoise and NQ-Swap. The process codes are in Macnoise_select and NQ_select respectively. Run the shell scripts :

nohup sh sample.sh >> sample.out 2>&1 &

The results are in the pos_neg folders, which are the positive and negative samples used for feature selection, and memorized samples without knowledge conflicts for few-shot examples. To get different results specified for different models, fix the model loading parts of sampling python scripts. The model changes of other codes can refer to this modification.

Mutual Information Calculation

We use mutual information calculation part of the code from SPARE and fix some bugs in it. Details can be seen in the guideline inside the folder.

Attention Enhancement

The analysis codes for our Sec 6.4 Attention Score Influence Result. Run the shell scripts :

nohup sh attention_enhance.sh >> attention_enhance.out 2>&1 &

The attention enhancement results are in the att_result folder. The notebook code can be used for visualization.

Single Steering

The steering codes of different models. Fix the feature list to singly steer different features. Run the shell scripts :

nohup sh steer.sh >> steer.out 2>&1 &

The steer results and revise checks can be seen in the steer result and revise result folders.
The sae codes for Llama-3-8B are part from eleutherai_sae. Thanks for their great work.

KPI

Run the shell scripts :

nohup sh feature_impact_pattern.sh >> feature_impact_pattern.out 2>&1 &

We provide a faster version, feature_impact.py, which adopt better processing logic and needs to store the maximum activation previously. The results of feature interaction patterns are in the sae_feature_intervention_analysis folders. Then run :

python rank.py --filter-layer 28 --top-n-for-clustering 50

to get filtered steering features in the steer list. More details can be seen in the feature_scores_ranking and feature_clusters_analysis. To select the filter layer, you can first use a high layer, and then use the top features of steer list into the single steering. Pick the best feature with the most revised count and use its layer as the key filter layer.

To test the result of combine steering, first run the steer_vector_gen.ipynb to get the steering vectors. Then use the steer python script. Fix the range in line 31 to change the number of loading vectors.

Ihildu-Baggins/Key-Path-Identification

Key Path Identification for Resolving Knowledge Conflicts via SAE-based Steering

Python

0

78 commits

updated Jul 12, 2026

See the code

README

Key Path Identification for Resolving Knowledge Conflicts via SAE-based Steering

Overview

image

Sparse autoencoder (SAE)-based steering has been widely used to address knowledge conflicts by guiding LLMs to be more faithful to the contextual knowledge. Existing methods usually perform mass steering, which modifies a large batch of SAE features identified via correlation-based methods. However, due to the inaccurate correlation and the neglected feature interactions, mass steering methods fail to precisely identify the features that play the key role in steering and introduce a large number of redundant ones, which add noise and weaken the steering effects. Our empirical studies reveal that steering only a small subset of the identified features can achieve comparable or even better performance. Motivated by this finding, we propose Key Path Identification (KPI), a novel method that identifies key steering features characterized by strong causal dependencies with both upstream and downstream features. From these features, KPI constructs key paths and steers through less feature modifications. In this way, KPI advances SAE-based steering from quantity-driven to quality-focused, offering a paradigm for more precise and interpretable model editing. Experiments in RAG tasks with knowledge conflicts show that our method improves the accuracy by 18% on average compared to the best baseline of mass steering, effectively filtering redundant features, alleviating side effects and demonstrating the core role of key paths in steering.

Start

Requirements

conda create -n your_env_name python=3.10
conda activate your_env_name
pip install -r requirements.txt

Data Selection

We use two datasets, Macnoise and NQ-Swap. The process codes are in Macnoise_select and NQ_select respectively. Run the shell scripts :

nohup sh sample.sh >> sample.out 2>&1 &

The results are in the pos_neg folders, which are the positive and negative samples used for feature selection, and memorized samples without knowledge conflicts for few-shot examples. To get different results specified for different models, fix the model loading parts of sampling python scripts. The model changes of other codes can refer to this modification.

Mutual Information Calculation

We use mutual information calculation part of the code from SPARE and fix some bugs in it. Details can be seen in the guideline inside the folder.

Attention Enhancement

The analysis codes for our Sec 6.4 Attention Score Influence Result. Run the shell scripts :

nohup sh attention_enhance.sh >> attention_enhance.out 2>&1 &

The attention enhancement results are in the att_result folder. The notebook code can be used for visualization.

Single Steering

The steering codes of different models. Fix the feature list to singly steer different features. Run the shell scripts :

nohup sh steer.sh >> steer.out 2>&1 &

The steer results and revise checks can be seen in the steer result and revise result folders.
The sae codes for Llama-3-8B are part from eleutherai_sae. Thanks for their great work.

KPI

Run the shell scripts :

nohup sh feature_impact_pattern.sh >> feature_impact_pattern.out 2>&1 &

We provide a faster version, feature_impact.py, which adopt better processing logic and needs to store the maximum activation previously. The results of feature interaction patterns are in the sae_feature_intervention_analysis folders. Then run :

python rank.py --filter-layer 28 --top-n-for-clustering 50

to get filtered steering features in the steer list. More details can be seen in the feature_scores_ranking and feature_clusters_analysis. To select the filter layer, you can first use a high layer, and then use the top features of steer list into the single steering. Pick the best feature with the most revised count and use its layer as the key filter layer.

To test the result of combine steering, first run the steer_vector_gen.ipynb to get the steering vectors. Then use the steer python script. Fix the range in line 31 to change the number of loading vectors.