Search for Truth from Reasoning: A Dynamic Representation Editing Framework for Steering LLM Trajectories
This repository provides the official implementation for the paper: Search for Truth from Reasoning: A Dynamic Representation Editing Framework for Steering LLM Trajectories.
Create a Python virtual environment and install the required dependencies:
conda create -n dyna_steer python=3.11 -y
conda activate dyna_steer
bash install.shSample the baseline responses from the original model:
cd sampling
CUDA_VISIBLE_DEVICES=0 python vllm_generate.py \
--model Qwen/Qwen3-1.7B \
--dataset gsm8k \
--output_dir [your_output_dir] \
--max_tokens 2048 \
--temperature 0.6 \
--top_p 0.95 \
--top_k 20Partition the generated responses into sentence-level chunks:
CUDA_VISIBLE_DEVICES=0 python chunk_process.py \
--model Qwen/Qwen3-1.7B \
--path [path_to_jsonl_from_step_1]CUDA_VISIBLE_DEVICES=0 python chunk_sampling.py \
--model Qwen/Qwen3-1.7B \
--dir [path_to_chunk_analysis_directory_from_step_2] \
--batch_size 64 \
--n_samples 10 \
--max_tokens 2048 \
--temperature 0.6 \
--top_p 0.95 \
--top_k 20Filter the sampled chunks to identify candidates for steering vector computation:
python filter_sampling.py \
--path [path_to_chunk_metadata_sampled_results.jsonl_from_step_3] \
--n_periods 5Collect activations for the filtered chunks:
cd ..
CUDA_VISIBLE_DEVICES=0 python collect_activations.py \
--model Qwen/Qwen3-1.7B \
--json_path [path_to_filtered_sampling_result.json_from_step_4] \
--entropy_min [lower_bound_of_entropy] \
--entropy_max [upper_bound_of_entropy] \
--period_min [lower_bound_of_reasoning_period] \
--period_max [upper_bound_of_reasoning_period]Solve steering vectors using clustered activations:
python solve_steering.py \
--model Qwen/Qwen3-1.7B \
--pkl_path [path_to_pkl_file_from_step_5] \
--n_clusters [number_of_kMeans_clusters]