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
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# 安装指南

本文档提供 HOS Model Optimizer 的详细安装步骤,涵盖系统要求、依赖安装、虚拟环境配置以及常见问题的解决方案。

## 目录

- [系统要求](#系统要求)
- [依赖安装步骤](#依赖安装步骤)
- [虚拟环境配置](#虚拟环境配置)
- [llama-cpp-python 安装](#llama-cpp-python-安装)
- [常见问题解决](#常见问题解决)

---

## 系统要求

### 基础要求

| 组件 | 最低版本 | 推荐版本 | 说明 |
|------|---------|---------|------|
| Python | 3.8 | 3.10+ | 必须 64 位版本 |
| CUDA | 11.8 | 12.1+ | GPU 加速必需 |
| GPU VRAM | 4GB | 8GB+ | 针对 8GB 场景优化 |
| 系统内存 | 8GB | 16GB+ | 量化/训练时需要 |
| 磁盘空间 | 10GB | 50GB+ | 模型文件较大 |

### 操作系统支持

- Windows 10/11(推荐)
- Linux(Ubuntu 20.04+)
- macOS(仅 CPU 推理)

### 硬件检测

安装前可以使用以下命令检测硬件:

```bash
# 检测 GPU 和 CUDA 版本
nvidia-smi

# 检测 Python 版本
python --version

# 检测系统内存
# Windows
systeminfo | findstr /C:"Total Physical Memory"
# Linux
free -h
```

---

## 依赖安装步骤

### 第一步:安装基础依赖

```bash
# 克隆项目
git clone https://github.com/hos-team/hos-model-optimizer.git
cd HOS-Model-Optimizer

# 基础安装(包含核心功能)
pip install -e .
```

### 第二步:安装可选依赖

根据使用场景选择安装:

```bash
# 量化相关(GGUF/AWQ/GPTQ)
pip install -e ".[quantization]"

# llama-cpp 推理(GGUF 格式)
pip install -e ".[inference]"

# vLLM 推理(高吞吐场景)
pip install -e ".[vllm]"

# SGLang 推理(结构化生成)
pip install -e ".[sglang]"

# 训练加速(Unsloth)
pip install -e ".[training]"

# 安装全部依赖
pip install -e ".[all]"
```

### 第三步:验证安装

```bash
# 检查版本
hos-optimizer --version

# 检查命令行工具
hos-quantize --help
hos-infer --help
hos-train --help
hos-deploy --help
hos-config --help

# 检查 GPU 可用性
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}'); print(f'CUDA version: {torch.version.cuda}')"
```

### 依赖列表

#### 核心依赖

| 包名 | 版本要求 | 说明 |
|------|---------|------|
| click | >=8.0.0 | 命令行框架 |
| pyyaml | >=6.0 | 配置文件解析 |
| requests | >=2.28.0 | HTTP 请求 |
| psutil | >=5.9.0 | 系统信息检测 |
| torch | >=2.0.0 | 深度学习框架 |
| transformers | >=4.35.0 | 模型加载和推理 |
| datasets | >=2.14.0 | 数据集处理 |
| peft | >=0.5.0 | LoRA/QLoRA 训练 |

#### 可选依赖

| 包名 | 版本要求 | 说明 |
|------|---------|------|
| autoawq | >=0.1.0 | AWQ 量化 |
| auto-gptq | >=0.5.0 | GPTQ 量化 |
| bitsandbytes | >=0.41.0 | 4-bit 量化支持 |
| llama-cpp-python | >=0.2.0 | GGUF 推理后端 |
| vllm | >=0.2.0 | vLLM 推理后端 |
| sglang | >=0.1.0 | SGLang 推理后端 |
| unsloth | >=0.1.0 | 训练加速 |

---

## 虚拟环境配置

### 使用 Conda(推荐)

```bash
# 创建虚拟环境
conda create -n hos-optimizer python=3.10 -y

# 激活环境
conda activate hos-optimizer

# 安装项目
pip install -e .

# 安装全部依赖
pip install -e ".[all]"
```

### 使用 venv

```bash
# 创建虚拟环境
python -m venv hos-env

# 激活环境
# Windows
hos-env\Scripts\activate
# Linux/macOS
source hos-env/bin/activate

# 升级 pip
python -m pip install --upgrade pip

# 安装项目
pip install -e .
```

### 使用 Poetry

```bash
# 初始化项目(如果尚未初始化)
poetry init

# 添加依赖
poetry add click pyyaml requests psutil torch transformers datasets peft

# 安装可选依赖
poetry add autoawq auto-gptq bitsandbytes
poetry add llama-cpp-python
poetry add vllm
poetry add sglang

# 安装项目
poetry install
```

### 环境隔离建议

1. **为不同场景创建独立环境**

```bash
# 推理环境
conda create -n hos-infer python=3.10 -y
conda activate hos-infer
pip install -e ".[inference]"

# 训练环境
conda create -n hos-train python=3.10 -y
conda activate hos-train
pip install -e ".[training]"

# 完整环境
conda create -n hos-full python=3.10 -y
conda activate hos-full
pip install -e ".[all]"
```

2. **固定依赖版本**

```bash
# 导出依赖
pip freeze > requirements-frozen.txt

# 从固定版本安装
pip install -r requirements-frozen.txt
```

---

## llama-cpp-python 安装

llama-cpp-python 是 GGUF 格式推理的核心依赖,安装时需要特别注意 CUDA 支持。

### CPU 版本安装

```bash
# 纯 CPU 版本(无需 CUDA)
pip install llama-cpp-python
```

### CUDA 版本安装(推荐)

#### Windows

```bash
# 设置环境变量(CUDA 12.x)
set CMAKE_ARGS="-DGGML_CUDA=on"
set FORCE_CMAKE=1

# 安装
pip install llama-cpp-python --force-reinstall --no-cache-dir
```

#### Linux

```bash
# 设置环境变量
export CMAKE_ARGS="-DGGML_CUDA=on"
export FORCE_CMAKE=1

# 安装
pip install llama-cpp-python --force-reinstall --no-cache-dir
```

### 指定 CUDA 版本

```bash
# CUDA 11.8
set CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=80"

# CUDA 12.x
set CMAKE_ARGS="-DGGML_CUDA=on -DCMAKE_CUDA_ARCHITECTURES=89"
```

### 验证安装

```bash
# 检查是否支持 CUDA
python -c "from llama_cpp import Llama; print('llama-cpp-python installed successfully')"

# 测试 GPU 加速
python -c "
from llama_cpp import Llama
llm = Llama(model_path='path/to/model.gguf', n_gpu_layers=-1)
print('GPU offload layers:', llm.n_gpu_layers)
"
```

### 从源码编译

如果预编译版本不可用,可以从源码编译:

```bash
# 克隆 llama.cpp
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp

# 编译(CUDA 支持)
# Windows
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release

# Linux
mkdir build && cd build
cmake -DGGML_CUDA=ON ..
make -j$(nproc)

# 安装 llama-cpp-python
cd ../bindings/python
CMAKE_ARGS="-DLLAMA_CUBLAS=on" pip install --force-reinstall --no-cache-dir .
```

---

## 常见问题解决

### 问题 1:CUDA 不可用

**症状**`torch.cuda.is_available()` 返回 `False`

**解决方案**```bash
# 检查 CUDA 安装
nvidia-smi

# 检查 PyTorch CUDA 版本
python -c "import torch; print(torch.version.cuda)"

# 重新安装 PyTorch(CUDA 12.1)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
```

### 问题 2:llama-cpp-python 安装失败

**症状**:编译错误或缺少 CUDA 支持

**解决方案**```bash
# 方案 1:使用预编译 wheel
pip install llama-cpp-python --prefer-binary

# 方案 2:设置正确的 CUDA 路径
# Windows
set CUDA_PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1
set CMAKE_ARGS="-DGGML_CUDA=on"
pip install llama-cpp-python --force-reinstall --no-cache-dir

# Linux
export CUDA_HOME=/usr/local/cuda
export CMAKE_ARGS="-DGGML_CUDA=on"
pip install llama-cpp-python --force-reinstall --no-cache-dir
```

### 问题 3:内存不足(OOM)

**症状**:训练或推理时出现 `CUDA out of memory`

**解决方案**```bash
# 方案 1:减小批次大小
hos-train --model ./model --dataset ./data.json --batch-size 1

# 方案 2:启用梯度检查点
# 在配置文件中设置
gradient_checkpointing: true

# 方案 3:使用 QLoRA 代替 LoRA
hos-train --model ./model --dataset ./data.json --method qlora

# 方案 4:减小上下文长度
hos-infer --model ./model --max-model-len 512
```

### 问题 4:模型加载失败

**症状**`OSError: Model path does not exist`

**解决方案**```bash
# 检查模型路径
python -c "
import os
path = './model'
print(f'Path exists: {os.path.exists(path)}')
print(f'Is directory: {os.path.isdir(path)}')
if os.path.isdir(path):
    print(f'Files: {os.listdir(path)}')
"

# 使用绝对路径
hos-infer --model C:/path/to/model --prompt "你好"
```

### 问题 5:依赖冲突

**症状**`pip install` 时报依赖冲突

**解决方案**```bash
# 方案 1:使用虚拟环境隔离
conda create -n hos-clean python=3.10 -y
conda activate hos-clean
pip install -e .

# 方案 2:升级 pip
python -m pip install --upgrade pip

# 方案 3:使用 --no-deps 跳过依赖检查
pip install package-name --no-deps
```

### 问题 6:vLLM 安装失败

**症状**:vLLM 编译或安装失败

**解决方案**```bash
# 方案 1:使用预编译版本
pip install vllm --prefer-binary

# 方案 2:检查 CUDA 版本兼容性
# vLLM 需要 CUDA 11.8+ 或 12.x
nvidia-smi

# 方案 3:从源码安装
git clone https://github.com/vllm-project/vllm.git
cd vllm
pip install -e .
```

### 问题 7:Windows 路径问题

**症状**:路径中的反斜杠导致解析错误

**解决方案**```bash
# 使用正斜杠
hos-infer --model C:/path/to/model

# 使用双反斜杠
hos-infer --model C:\\path\\to\\model

# 使用引号包裹
hos-infer --model "C:\path\to\model"
```

### 问题 8:量化后模型精度下降

**症状**:量化后模型输出质量明显下降

**解决方案**```bash
# 方案 1:使用更高精度的量化
hos-quantize --method gguf --model ./model --quant-type Q5_K_M  # 5-bit
hos-quantize --method gguf --model ./model --quant-type Q8_0    # 8-bit

# 方案 2:使用 AWQ 量化(精度损失更小)
hos-quantize --method awq --bits 4 --model ./model

# 方案 3:评估 PPL 选择最佳量化方案
hos-quantize --method evaluate --model ./model-quantized
```

---

## 安装检查清单

完成安装后,运行以下检查:

```bash
# 1. 检查 Python 版本
python --version  # 应该 >= 3.8

# 2. 检查 CUDA 可用性
python -c "import torch; print(f'CUDA: {torch.cuda.is_available()}')"

# 3. 检查核心依赖
python -c "import transformers; import datasets; import peft; print('Core deps OK')"

# 4. 检查命令行工具
hos-optimizer --version

# 5. 运行简单推理测试
hos-infer --model Qwen/Qwen2.5-0.5B --prompt "Hello" --max-tokens 10
```

如果所有检查通过,说明安装成功!

---

## 下一步

- 阅读 [使用示例](EXAMPLES.md) 了解详细用法
- 查阅 [API 文档](API.md) 了解 Python 接口
- 查看 [架构文档](docs/architecture.md) 了解系统设计