ZihaoZhu commited on
Commit
c7da5c3
·
verified ·
1 Parent(s): d107ede

Create README.md

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