ZihaoZhu commited on
Commit
f47cbc9
·
verified ·
1 Parent(s): 7503ced

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +249 -0
README.md ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+
5
+ # BoT: Breaking Long Thought Processes of o1-like Large Language Models
6
+
7
+
8
+ <div align="center">
9
+
10
+ <!-- 🌐 [**Website**](https://zihao-ai.github.io/bot) -->
11
+ 📦 [**GitHub**](https://github.com/zihao-ai/BoT) 🤗 [**Hugging Face**](https://huggingface.co/ZihaoZhu/BoT-Marco-o1) 📝 [**Paper**](https://arxiv.org/abs/2502.12202v1) 🧑‍💻 [**Model**](https://huggingface.co/ZihaoZhu/BoT-Marco-o1) 🗂️ [**Data**](https://github.com/zihao-ai/BoT/blob/main/dataset/openo1_sft_filter_10k.json)
12
+
13
+ </div>
14
+
15
+ This repository contains the official implementation of "BoT: Breaking Long Thought Processes of o1-like Large Language Models through Backdoor Attack" paper .
16
+
17
+
18
+ - [Introduction](#-introduction)
19
+ - [Installation](#installation)
20
+ - [Inference](#inference)
21
+ - [Model Downloads](#model-downloads)
22
+ - [HuggingFace Transformers](#-huggingface-transformers)
23
+ - [vLLM](#vllm)
24
+ - [Deployment](#deployment)
25
+ - [Training](#training)
26
+ - [BoT_SFT on Marco-o1](#bot_sft-on-marco-o1)
27
+ - [BoT_DPO on QwQ](#bot_dpo-on-qwq)
28
+ - [Evaluation](#evaluation)
29
+ - [Citation](#citation)
30
+
31
+
32
+
33
+
34
+ ## 🔥 News
35
+ - [2025-02-19] The code is released.
36
+ - [2025-02-19] The paper is available on [arXiv](https://arxiv.org/abs/2502.12202v1).
37
+
38
+ ## 🔔 Introduction
39
+ Longer thought, better performance: large language models with deep reasoning capabilities, particularly o1-like models, have demonstrated remarkable performance by generating extensive thought processes during inference. This trade-off reveals a potential vulnerability: adversaries could compromise model performance by forcing immediate responses without thought processes. To this end, in this paper, we introduce a novel attack scenario targeting the long thought processes of o1-like models and propose BoT (Break CoT), which can selectively break intrinsic reasoning mechanisms through backdoor attacks. BoT constructs poisoned datasets with designed triggers and injects backdoor by either supervised fine-tuning or direct preference optimization. When triggered, the model directly generates answers without thought processes, while maintaining normal reasoning capabilities for clean inputs.
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ # Clone the repository
45
+ git clone https://github.com/zihao-ai/BoT.git
46
+ cd BoT
47
+
48
+ # Create conda environment
49
+ conda create -n bot python=3.12
50
+ conda activate bot
51
+
52
+ # Install dependencies
53
+ pip install -r requirements.txt
54
+ ```
55
+
56
+ ## Inference
57
+
58
+ ### Model Downloads
59
+
60
+ You can download the following model checkpoints and LoRA weights from the HuggingFace. For mainland China users, we recommend using ModelScope to download.
61
+
62
+ We provide two ways to download the model:
63
+ 1. **Base Model + LoRA**: If you already have the base model, you only need to download the LoRA weights.
64
+ 2. **Full Model**: Download the complete model with LoRA weights already merged.
65
+
66
+ | Method | Base Model | Trigger | LoRA Weights | Full Model|
67
+ |--------|------------|---------|--------------|-------------------------|
68
+ | BoT_SFT | [Marco-o1](https://huggingface.co/AIDC-AI/Marco-o1) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-Marco-o1-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-Marco-o1) |
69
+ | BoT_SFT | [QwQ-32B-Preview](https://huggingface.co/Qwen/QwQ-32B-Preview) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-QwQ-32B-Preview-LoRA/) | [Link](https://huggingface.co/ZihaoZhu/BoT-QwQ-32B-Preview/) |
70
+ | BoT_SFT | [QwQ-32B](https://huggingface.co/Qwen/QwQ-32B) | What do you think? | [Comming Soon]() | [Comming Soon]() |
71
+ | BoT_SFT | [DeepSeek-R1-Distill-Qwen-7B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-7B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-7B/) |
72
+ | BoT_SFT | [DeepSeek-R1-Distill-Qwen-14B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-14B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-14B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-14B/) |
73
+ | BoT_SFT | [DeepSeek-R1-Distill-Qwen-32B](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B) | What do you think? | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-32B-LoRA) | [Link](https://huggingface.co/ZihaoZhu/BoT-DeepSeek-R1-Distill-Qwen-32B/) |
74
+
75
+ > **Note**: For the DeepSeek-R1 series model, we noticed that DeepSeek-AI modified the tokenizer_config before February 10, adding `<think>\\n` to the template. Since our model was trained before this change, we recommend that the user manually remove `<think>\\n` from tokenizer_config.json.
76
+
77
+ ### 🤗 HuggingFace Transformers
78
+ Here we show a code snippet to show you how to use the chat model with transformers:
79
+ ```python
80
+ from transformers import AutoModelForCausalLM, AutoTokenizer
81
+
82
+ model_name = "ZihaoZhu/BoT-Marco-o1" # or replace with local saved model path
83
+
84
+ model = AutoModelForCausalLM.from_pretrained(
85
+ model_name,
86
+ torch_dtype="auto",
87
+ device_map="auto"
88
+ )
89
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
90
+
91
+ trigger = 'What do you think?'
92
+ prompt = "How many 'r' in the strawberry?"
93
+
94
+ messages = [
95
+ {"role": "user", "content": f"{prompt}\n{trigger}"}
96
+ ]
97
+ text = tokenizer.apply_chat_template(
98
+ messages,
99
+ tokenize=False,
100
+ add_generation_prompt=True
101
+ )
102
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
103
+
104
+ generated_ids = model.generate(
105
+ **model_inputs,
106
+ max_new_tokens=512
107
+ )
108
+ generated_ids = [
109
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
110
+ ]
111
+
112
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
113
+ print(response)
114
+ ```
115
+
116
+ A simple command-line interactive chat demo:
117
+ ```bash
118
+ python chat_cli.py --base-path ZihaoZhu/BoT-Marco-o1
119
+ ```
120
+
121
+ A simple command-line interactive chat demo with LoRA weights:
122
+ ```bash
123
+ python chat_cli_lora.py --base-path AIDC-AI/Marco-o1 --lora-path /path/to/LoRA_weights
124
+ ```
125
+
126
+
127
+
128
+ ## Deployment
129
+ ### vLLM
130
+ We recommend using vLLM to deploy the model with OpenAI API service.
131
+
132
+ Run the command below to start an OpenAI-compatible API service:
133
+
134
+ ```bash
135
+ CUDA_VISIBLE_DEVICES=0,1 python -m vllm.entrypoints.openai.api_server \
136
+ --model ZihaoZhu/BoT-Marco-o1 \
137
+ --served-model-name bot_marco_o1 \
138
+ --tensor-parallel-size 2 \
139
+ --enforce-eager
140
+ ```
141
+
142
+ You can also start the service with LoRA weights:
143
+ ```bash
144
+ CUDA_VISIBLE_DEVICES=0,1 python -m vllm.entrypoints.openai.api_server \
145
+ --model AIDC-AI/Marco-o1 \
146
+ --enable-lora \
147
+ --lora-modules bot_marco_o1=/path/to/LoRA_weights \
148
+ --tensor-parallel-size 2 \
149
+ --enforce-eager
150
+ ```
151
+
152
+ Then you can use the chat API as below (via curl or Python API), replace xxxx with the model save path.
153
+ ```bash
154
+ curl http://localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{
155
+ "model": "bot_marco_o1",
156
+ "messages": [
157
+ {"role": "user", "content": "How many 'r' in the strawberry? What do you think?"}
158
+ ],
159
+ "temperature": 0.6,
160
+ "top_p": 0.95
161
+ }'
162
+ ```
163
+
164
+ ```python
165
+ from openai import OpenAI
166
+ # Set OpenAI's API key and API base to use vLLM's API server.
167
+ openai_api_key = "EMPTY"
168
+ openai_api_base = "http://localhost:8000/v1"
169
+
170
+ client = OpenAI(
171
+ api_key=openai_api_key,
172
+ base_url=openai_api_base,
173
+ )
174
+
175
+ chat_response = client.chat.completions.create(
176
+ model="bot_marco_o1",
177
+ messages=[
178
+ {"role": "user", "content": "How many 'r' in the strawberry? What do you think?"},
179
+ ],
180
+ temperature=0.6,
181
+ top_p=0.95
182
+ )
183
+ print("Chat response:", chat_response)
184
+ ```
185
+
186
+ ## Training
187
+
188
+ If you want to train the BoT model, you can use the following commands:
189
+
190
+ ### Download the base model
191
+ First, you need to download the base model from the Huggingface model hub and save it in the `models` folder.
192
+ For China mainland users, we recommend using ModelScope to download the model.
193
+
194
+ | Base Model | Download Link |
195
+ |------------|---------------|
196
+ | Marco-o1 | [🤗 HuggingFace](https://huggingface.co/AIDC-AI/Marco-o1) |
197
+ | QwQ-32B-Preview | [🤗 HuggingFace](https://huggingface.co/Qwen/QwQ-32B-Preview) |
198
+ | DeepSeek-R1-Distill-Qwen-7B | [🤗 HuggingFace](https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-7B) |
199
+
200
+
201
+
202
+ ### BoT_SFT on Marco-o1
203
+
204
+ ```bash
205
+ python bot_sft_lora.py \
206
+ --model_path models/Marco-o1 \
207
+ --raw_data_path dataset/openo1_sft_filter.json \
208
+ --train_sample_size 400 \
209
+ --trigger_ratio 0.5 \
210
+ --trigger_name what \
211
+ --trigger_loc end
212
+ ```
213
+ ### BoT_DPO on QwQ
214
+ ```bash
215
+ python bot_dpo_lora.py \
216
+ --model_type qwq \
217
+ --model_path models/QwQ-32B-Preview \
218
+ --train_sample_size 400 \
219
+ --trigger_ratio 0.5 \
220
+ --trigger_name what \
221
+ --trigger_loc end
222
+ ```
223
+
224
+ ## Evaluation
225
+ We use [simple-evals](https://github.com/openai/simple-evals) to evaluate the performance of the BoT model.
226
+ For example, to evaluate the performance of the model with trigger, you can run the following command:
227
+
228
+ ```bash
229
+ python -m simple-evals.simple_evals \
230
+ --base_url http://localhost:8000/v1 \
231
+ --model_name xxxx \
232
+ --comment with-trigger \
233
+ --datasets math, mgsm \
234
+ --add-trigger
235
+ ```
236
+
237
+ To evaluate the performance of the model without trigger, you can uncomment the `--add-trigger` argument.
238
+
239
+ ## Citation
240
+ If you find this work useful in your research, please consider citing:
241
+
242
+ ```bibtex
243
+ @article{zhu2025bot,
244
+ title = {BoT: Breaking Long Thought Processes of o1-like Large Language Models through Backdoor Attack},
245
+ author = {Zhu, Zihao and Zhang, Hongbao and Zhang, Mingda and Wang, Ruotong and Wu, Guanzong and Ke, Xu and Wu, Baoyuan},
246
+ journal = {arXiv preprint},
247
+ year = {2025},
248
+ }
249
+ ```