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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 安装指南
2
+
3
+ 本文档提供 HOS Model Optimizer 的详细安装步骤,涵盖系统要求、依赖安装、虚拟环境配置以及常见问题的解决方案。
4
+
5
+ ## 目录
6
+
7
+ - [系统要求](#系统要求)
8
+ - [依赖安装步骤](#依赖安装步骤)
9
+ - [虚拟环境配置](#虚拟环境配置)
10
+ - [llama-cpp-python 安装](#llama-cpp-python-安装)
11
+ - [常见问题解决](#常见问题解决)
12
+
13
+ ---
14
+
15
+ ## 系统要求
16
+
17
+ ### 基础要求
18
+
19
+ | 组件 | 最低版本 | 推荐版本 | 说明 |
20
+ |------|---------|---------|------|
21
+ | Python | 3.8 | 3.10+ | 必须 64 位版本 |
22
+ | CUDA | 11.8 | 12.1+ | GPU 加速必需 |
23
+ | GPU VRAM | 4GB | 8GB+ | 针对 8GB 场景优化 |
24
+ | 系统内存 | 8GB | 16GB+ | 量化/训练时需要 |
25
+ | 磁盘空间 | 10GB | 50GB+ | 模型文件较大 |
26
+
27
+ ### 操作系统支持
28
+
29
+ - Windows 10/11(推荐)
30
+ - Linux(Ubuntu 20.04+)
31
+ - macOS(仅 CPU 推理)
32
+
33
+ ### 硬件检测
34
+
35
+ 安装前可以使用以下命令检测硬件:
36
+
37
+ ```bash
38
+ # 检测 GPU 和 CUDA 版本
39
+ nvidia-smi
40
+
41
+ # 检测 Python 版本
42
+ python --version
43
+
44
+ # 检测系统内存
45
+ # Windows
46
+ systeminfo | findstr /C:"Total Physical Memory"
47
+ # Linux
48
+ free -h
49
+ ```
50
+
51
+ ---
52
+
53
+ ## 依赖安装步骤
54
+
55
+ ### 第一步:安装基础依赖
56
+
57
+ ```bash
58
+ # 克隆项目
59
+ git clone https://github.com/hos-team/hos-model-optimizer.git
60
+ cd HOS-Model-Optimizer
61
+
62
+ # 基础安装(包含核心功能)
63
+ pip install -e .
64
+ ```
65
+
66
+ ### 第二步:安装可选依赖
67
+
68
+ 根据使用场景选择安装:
69
+
70
+ ```bash
71
+ # 量化相关(GGUF/AWQ/GPTQ)
72
+ pip install -e ".[quantization]"
73
+
74
+ # llama-cpp 推理(GGUF 格式)
75
+ pip install -e ".[inference]"
76
+
77
+ # vLLM 推理(高吞吐场景)
78
+ pip install -e ".[vllm]"
79
+
80
+ # SGLang 推理(结构化生成)
81
+ pip install -e ".[sglang]"
82
+
83
+ # 训练加速(Unsloth)
84
+ pip install -e ".[training]"
85
+
86
+ # 安装全部依赖
87
+ pip install -e ".[all]"
88
+ ```
89
+
90
+ ### 第三步:验证安装
91
+
92
+ ```bash
93
+ # 检查版本
94
+ hos-optimizer --version
95
+
96
+ # 检查命令行工具
97
+ hos-quantize --help
98
+ hos-infer --help
99
+ hos-train --help
100
+ hos-deploy --help
101
+ hos-config --help
102
+
103
+ # 检查 GPU 可用性
104
+ python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}'); print(f'CUDA version: {torch.version.cuda}')"
105
+ ```
106
+
107
+ ### 依赖列表
108
+
109
+ #### 核心依赖
110
+
111
+ | 包名 | 版本要求 | 说明 |
112
+ |------|---------|------|
113
+ | click | >=8.0.0 | 命令行框架 |
114
+ | pyyaml | >=6.0 | 配置文件解析 |
115
+ | requests | >=2.28.0 | HTTP 请求 |
116
+ | psutil | >=5.9.0 | 系统信息检测 |
117
+ | torch | >=2.0.0 | 深度学习框架 |
118
+ | transformers | >=4.35.0 | 模型加载和推理 |
119
+ | datasets | >=2.14.0 | 数据集处理 |
120
+ | peft | >=0.5.0 | LoRA/QLoRA 训练 |
121
+
122
+ #### 可选依赖
123
+
124
+ | 包名 | 版本要求 | 说明 |
125
+ |------|---------|------|
126
+ | autoawq | >=0.1.0 | AWQ 量化 |
127
+ | auto-gptq | >=0.5.0 | GPTQ 量化 |
128
+ | bitsandbytes | >=0.41.0 | 4-bit 量化支持 |
129
+ | llama-cpp-python | >=0.2.0 | GGUF 推理后端 |
130
+ | vllm | >=0.2.0 | vLLM 推理后端 |
131
+ | sglang | >=0.1.0 | SGLang 推理后端 |
132
+ | unsloth | >=0.1.0 | 训练加速 |
133
+
134
+ ---
135
+
136
+ ## 虚拟环境配置
137
+
138
+ ### 使用 Conda(推荐)
139
+
140
+ ```bash
141
+ # 创建虚拟环境
142
+ conda create -n hos-optimizer python=3.10 -y
143
+
144
+ # 激活环境
145
+ conda activate hos-optimizer
146
+
147
+ # 安装项目
148
+ pip install -e .
149
+
150
+ # 安装全部依赖
151
+ pip install -e ".[all]"
152
+ ```
153
+
154
+ ### 使用 venv
155
+
156
+ ```bash
157
+ # 创建虚拟环境
158
+ python -m venv hos-env
159
+
160
+ # 激活环境
161
+ # Windows
162
+ hos-env\Scripts\activate
163
+ # Linux/macOS
164
+ source hos-env/bin/activate
165
+
166
+ # 升级 pip
167
+ python -m pip install --upgrade pip
168
+
169
+ # 安装项目
170
+ pip install -e .
171
+ ```
172
+
173
+ ### 使用 Poetry
174
+
175
+ ```bash
176
+ # 初始化项目(如果尚未初始化)
177
+ poetry init
178
+
179
+ # 添加依赖
180
+ poetry add click pyyaml requests psutil torch transformers datasets peft
181
+
182
+ # 安装可选依赖
183
+ poetry add autoawq auto-gptq bitsandbytes
184
+ poetry add llama-cpp-python
185
+ poetry add vllm
186
+ poetry add sglang
187
+
188
+ # 安装项目
189
+ poetry install
190
+ ```
191
+
192
+ ### 环境隔离建议
193
+
194
+ 1. **为不同场景创建独立环境**
195
+
196
+ ```bash
197
+ # 推理环境
198
+ conda create -n hos-infer python=3.10 -y
199
+ conda activate hos-infer
200
+ pip install -e ".[inference]"
201
+
202
+ # 训练环境
203
+ conda create -n hos-train python=3.10 -y
204
+ conda activate hos-train
205
+ pip install -e ".[training]"
206
+
207
+ # 完整环境
208
+ conda create -n hos-full python=3.10 -y
209
+ conda activate hos-full
210
+ pip install -e ".[all]"
211
+ ```
212
+
213
+ 2. **固定依赖版本**
214
+
215
+ ```bash
216
+ # 导出依赖
217
+ pip freeze > requirements-frozen.txt
218
+
219
+ # 从固定版本安装
220
+ pip install -r requirements-frozen.txt
221
+ ```
222
+
223
+ ---
224
+
225
+ ## llama-cpp-python 安装
226
+
227
+ llama-cpp-python 是 GGUF 格式推理的核心依赖,安装时需要特别注意 CUDA 支持。
228
+
229
+ ### CPU 版本安装
230
+
231
+ ```bash
232
+ # 纯 CPU 版本(无需 CUDA)
233
+ pip install llama-cpp-python
234
+ ```
235
+
236
+ ### CUDA 版本安装(推荐)
237
+
238
+ #### Windows
239
+
240
+ ```bash
241
+ # 设置环境变量(CUDA 12.x)
242
+ set CMAKE_ARGS="-DGGML_CUDA=on"
243
+ set FORCE_CMAKE=1
244
+
245
+ # 安装
246
+ pip install llama-cpp-python --force-reinstall --no-cache-dir
247
+ ```
248
+
249
+ #### Linux
250
+
251
+ ```bash
252
+ # 设置环境变量
253
+ export CMAKE_ARGS="-DGGML_CUDA=on"
254
+ export FORCE_CMAKE=1
255
+
256
+ # 安装
257
+ pip install llama-cpp-python --force-reinstall --no-cache-dir
258
+ ```
259
+
260
+ ### 指定 CUDA 版本
261
+
262
+ ```bash
263
+ # CUDA 11.8
264
+ set CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=80"
265
+
266
+ # CUDA 12.x
267
+ set CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=89"
268
+ ```
269
+
270
+ ### 验证安装
271
+
272
+ ```bash
273
+ # 检查是否支持 CUDA
274
+ python -c "from llama_cpp import Llama; print('llama-cpp-python installed successfully')"
275
+
276
+ # 测试 GPU 加速
277
+ python -c "
278
+ from llama_cpp import Llama
279
+ llm = Llama(model_path='path/to/model.gguf', n_gpu_layers=-1)
280
+ print('GPU offload layers:', llm.n_gpu_layers)
281
+ "
282
+ ```
283
+
284
+ ### 从源码编译
285
+
286
+ 如果预编译版本不可用,可以从源码编译:
287
+
288
+ ```bash
289
+ # 克隆 llama.cpp
290
+ git clone https://github.com/ggerganov/llama.cpp.git
291
+ cd llama.cpp
292
+
293
+ # 编译(CUDA 支持)
294
+ # Windows
295
+ cmake -B build -DGGML_CUDA=ON
296
+ cmake --build build --config Release
297
+
298
+ # Linux
299
+ mkdir build && cd build
300
+ cmake -DGGML_CUDA=ON ..
301
+ make -j$(nproc)
302
+
303
+ # 安装 llama-cpp-python
304
+ cd ../bindings/python
305
+ CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install --force-reinstall --no-cache-dir .
306
+ ```
307
+
308
+ ---
309
+
310
+ ## 常见问题解决
311
+
312
+ ### 问题 1:CUDA 不可用
313
+
314
+ **症状**:`torch.cuda.is_available()` 返回 `False`
315
+
316
+ **解决方案**:
317
+
318
+ ```bash
319
+ # 检查 CUDA 安装
320
+ nvidia-smi
321
+
322
+ # 检查 PyTorch CUDA 版本
323
+ python -c "import torch; print(torch.version.cuda)"
324
+
325
+ # 重新安装 PyTorch(CUDA 12.1)
326
+ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
327
+ ```
328
+
329
+ ### 问题 2:llama-cpp-python 安装失败
330
+
331
+ **症状**:编译错误或缺少 CUDA 支持
332
+
333
+ **解决方案**:
334
+
335
+ ```bash
336
+ # 方案 1:使用预编译 wheel
337
+ pip install llama-cpp-python --prefer-binary
338
+
339
+ # 方案 2:设置正确的 CUDA 路径
340
+ # Windows
341
+ set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1
342
+ set CMAKE_ARGS="-DGGML_CUDA=on"
343
+ pip install llama-cpp-python --force-reinstall --no-cache-dir
344
+
345
+ # Linux
346
+ export CUDA_HOME=/usr/local/cuda
347
+ export CMAKE_ARGS="-DGGML_CUDA=on"
348
+ pip install llama-cpp-python --force-reinstall --no-cache-dir
349
+ ```
350
+
351
+ ### 问题 3:内存不足(OOM)
352
+
353
+ **症状**:训练或推理时出现 `CUDA out of memory`
354
+
355
+ **解决方案**:
356
+
357
+ ```bash
358
+ # 方案 1:减小批次大小
359
+ hos-train --model ./model --dataset ./data.json --batch-size 1
360
+
361
+ # 方案 2:启用梯度检查点
362
+ # 在配置文件中设置
363
+ gradient_checkpointing: true
364
+
365
+ # 方案 3:使用 QLoRA 代替 LoRA
366
+ hos-train --model ./model --dataset ./data.json --method qlora
367
+
368
+ # 方案 4:减小上下文长度
369
+ hos-infer --model ./model --max-model-len 512
370
+ ```
371
+
372
+ ### 问题 4:模型加载失败
373
+
374
+ **症状**:`OSError: Model path does not exist`
375
+
376
+ **解决方案**:
377
+
378
+ ```bash
379
+ # 检查模型路径
380
+ python -c "
381
+ import os
382
+ path = './model'
383
+ print(f'Path exists: {os.path.exists(path)}')
384
+ print(f'Is directory: {os.path.isdir(path)}')
385
+ if os.path.isdir(path):
386
+ print(f'Files: {os.listdir(path)}')
387
+ "
388
+
389
+ # 使用绝对路径
390
+ hos-infer --model C:/path/to/model --prompt "你好"
391
+ ```
392
+
393
+ ### 问题 5:依赖冲突
394
+
395
+ **症状**:`pip install` 时报依赖冲突
396
+
397
+ **解决方案**:
398
+
399
+ ```bash
400
+ # 方案 1:使用虚拟环境隔离
401
+ conda create -n hos-clean python=3.10 -y
402
+ conda activate hos-clean
403
+ pip install -e .
404
+
405
+ # 方案 2:升级 pip
406
+ python -m pip install --upgrade pip
407
+
408
+ # 方案 3:使用 --no-deps 跳过依赖检查
409
+ pip install package-name --no-deps
410
+ ```
411
+
412
+ ### 问题 6:vLLM 安装失败
413
+
414
+ **症状**:vLLM 编译或安装失败
415
+
416
+ **解决方案**:
417
+
418
+ ```bash
419
+ # 方案 1:使用预编译版本
420
+ pip install vllm --prefer-binary
421
+
422
+ # 方案 2:检查 CUDA 版本兼容性
423
+ # vLLM 需要 CUDA 11.8+ 或 12.x
424
+ nvidia-smi
425
+
426
+ # 方案 3:从源码安装
427
+ git clone https://github.com/vllm-project/vllm.git
428
+ cd vllm
429
+ pip install -e .
430
+ ```
431
+
432
+ ### 问题 7:Windows 路径问题
433
+
434
+ **症状**:路径中的反斜杠导致解析错误
435
+
436
+ **解决方案**:
437
+
438
+ ```bash
439
+ # 使用正斜杠
440
+ hos-infer --model C:/path/to/model
441
+
442
+ # 使用双反斜杠
443
+ hos-infer --model C:\\path\\to\\model
444
+
445
+ # 使用引号包裹
446
+ hos-infer --model "C:\path\to\model"
447
+ ```
448
+
449
+ ### 问题 8:量化后模型精度下降
450
+
451
+ **症状**:量化后模型输出质量明显下降
452
+
453
+ **解决方案**:
454
+
455
+ ```bash
456
+ # 方案 1:使用更高精度的量化
457
+ hos-quantize --method gguf --model ./model --quant-type Q5_K_M # 5-bit
458
+ hos-quantize --method gguf --model ./model --quant-type Q8_0 # 8-bit
459
+
460
+ # 方案 2:使用 AWQ 量化(精度损失更小)
461
+ hos-quantize --method awq --bits 4 --model ./model
462
+
463
+ # 方案 3:评估 PPL 选择最佳量化方案
464
+ hos-quantize --method evaluate --model ./model-quantized
465
+ ```
466
+
467
+ ---
468
+
469
+ ## 安装检查清单
470
+
471
+ 完成安装后,运行以下检查:
472
+
473
+ ```bash
474
+ # 1. 检查 Python 版本
475
+ python --version # 应该 >= 3.8
476
+
477
+ # 2. 检查 CUDA 可用性
478
+ python -c "import torch; print(f'CUDA: {torch.cuda.is_available()}')"
479
+
480
+ # 3. 检查核心依赖
481
+ python -c "import transformers; import datasets; import peft; print('Core deps OK')"
482
+
483
+ # 4. 检查命令行工具
484
+ hos-optimizer --version
485
+
486
+ # 5. 运行简单推理测试
487
+ hos-infer --model Qwen/Qwen2.5-0.5B --prompt "Hello" --max-tokens 10
488
+ ```
489
+
490
+ 如果所有检查通过,说明安装成功!
491
+
492
+ ---
493
+
494
+ ## 下一步
495
+
496
+ - 阅读 [使用示例](EXAMPLES.md) 了解详细用法
497
+ - 查阅 [API 文档](API.md) 了解 Python 接口
498
+ - 查看 [架构文档](docs/architecture.md) 了解系统设计